aboutsummaryrefslogtreecommitdiff
path: root/src/vpn/gnunet-service-dns.c
blob: 168446156c8f78a493dc6bb34d6f7d8024541201 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
/*
     This file is part of GNUnet.
     (C) 2009 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file vpn/gnunet-service-dns.c
 * @author Philipp Toelke
 */
#include "platform.h"
#include "gnunet_getopt_lib.h"
#include "gnunet_service_lib.h"
#include "gnunet_network_lib.h"
#include "gnunet_os_lib.h"
#include "gnunet-service-dns-p.h"
#include "gnunet_protocols.h"
#include "gnunet_applications.h"
#include "gnunet-vpn-packet.h"
#include "gnunet_container_lib.h"
#include "gnunet-dns-parser.h"
#include "gnunet_dht_service.h"
#include "gnunet_block_lib.h"
#include "block_dns.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_mesh_service.h"
#include "gnunet_signatures.h"

struct GNUNET_MESH_Handle *mesh_handle;

/**
 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
 * sent through gnunet. The port of this socket will not be hijacked.
 */
static struct GNUNET_NETWORK_Handle *dnsout;

/**
 * The port bound to the socket dnsout
 */
static unsigned short dnsoutport;

/**
 * A handle to the DHT-Service
 */
static struct GNUNET_DHT_Handle *dht;

/**
 * The configuration to use
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * A list of DNS-Responses that have to be sent to the requesting client
 */
static struct answer_packet_list *head;

/**
 * The tail of the list of DNS-responses
 */
static struct answer_packet_list *tail;

/**
 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
 * some information needed to handle this query
 *
 * It currently allocates at least
 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
 * = 17 MiB on 64 bit.
 * = 11 MiB on 32 bit.
 */
static struct {
    unsigned valid:1;
    struct GNUNET_SERVER_Client* client;
    struct GNUNET_MESH_Tunnel *tunnel;
    uint32_t local_ip;
    uint32_t remote_ip;
    uint16_t local_port;
    char* name;
    uint8_t namelen;
} query_states[UINT16_MAX];

/**
 * A struct used to give more than one value as
 * closure to receive_dht
 */
struct receive_dht_cls {
    uint16_t id;
    struct GNUNET_DHT_GetHandle* handle;
};

/**
 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
 */
static void
hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    return;

  char port_s[6];
  char *virt_dns;
  struct GNUNET_OS_Process *proc;

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
                                             &virt_dns))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "No entry 'VIRTDNS' in configuration!\n");
      exit (1);
    }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
  snprintf (port_s, 6, "%d", dnsoutport);
  if (NULL != (proc = GNUNET_OS_start_process (NULL,
                                               NULL,
                                               "gnunet-helper-hijack-dns",
                                               "gnunet-hijack-dns",
                                               port_s, virt_dns, NULL)))
    GNUNET_OS_process_close (proc);
  GNUNET_free (virt_dns);
}

/**
 * Delete the hijacking-routes
 */
static void
unhijack (unsigned short port)
{
  char port_s[6];
  char *virt_dns;
  struct GNUNET_OS_Process *proc;

  if (GNUNET_SYSERR ==
      GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
                                             &virt_dns))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "No entry 'VIRTDNS' in configuration!\n");
      exit (1);
    }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
  snprintf (port_s, 6, "%d", port);
  if (NULL != (proc = GNUNET_OS_start_process (NULL,
                                               NULL,
                                               "gnunet-helper-hijack-dns",
                                               "gnunet-hijack-dns",
                                               "-d", port_s, virt_dns, NULL)))
    GNUNET_OS_process_close (proc);
  GNUNET_free (virt_dns);
}

/**
 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
 * system.
 */
static size_t
send_answer(void* cls, size_t size, void* buf) {
    struct answer_packet_list* query = head;
    size_t len = ntohs(query->pkt.hdr.size);

    GNUNET_assert(len <= size);

    memcpy(buf, &query->pkt.hdr, len);

    GNUNET_CONTAINER_DLL_remove (head, tail, query);

    GNUNET_free(query);

    /* When more data is to be sent, reschedule */
    if (head != NULL)
      GNUNET_SERVER_notify_transmit_ready(cls,
					  ntohs(head->pkt.hdr.size),
					  GNUNET_TIME_UNIT_FOREVER_REL,
					  &send_answer,
					  cls);

    return len;
}

struct tunnel_cls {
    struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
    struct GNUNET_MessageHeader hdr;
    struct dns_pkt dns;
};

struct tunnel_cls *remote_pending[UINT16_MAX];

static size_t
mesh_send_response (void *cls, size_t size, void *buf)
{
  GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
  struct GNUNET_MessageHeader *hdr = buf;
  uint32_t *sz = cls;
  struct dns_pkt *dns = (struct dns_pkt *) (sz + 1);
  hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
  hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
              *sz + sizeof (struct GNUNET_MessageHeader));

  GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));

  memcpy (hdr + 1, dns, *sz);
  GNUNET_free (cls);
  return ntohs (hdr->size);
}

static size_t
mesh_send (void *cls, size_t size, void *buf)
{
  struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;

  GNUNET_assert(cls_->hdr.size <= size);

  size = cls_->hdr.size;
  cls_->hdr.size = htons(cls_->hdr.size);

  memcpy(buf, &cls_->hdr, size);
  return size;
}


void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) {
  if (NULL == peer) return;
  struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s, sending query with id %d\n", GNUNET_i2s(peer), ntohs(cls_->dns.s.id));

  GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
                                    GNUNET_YES,
                                    42,
                                    GNUNET_TIME_UNIT_MINUTES,
                                    NULL,
                                    cls_->hdr.size,
                                    mesh_send,
                                    cls);
}


static void
send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
    return;

  struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;

  cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
                                           GNUNET_TIME_UNIT_HOURS,
                                           GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
                                           mesh_connect,
                                           NULL,
                                           cls_);

  remote_pending[cls_->dns.s.id] = cls_;
}

static int
receive_mesh_query (void *cls __attribute__((unused)),
                    struct GNUNET_MESH_Tunnel *tunnel,
                    void **ctx __attribute__((unused)),
                    const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
                    const struct GNUNET_MessageHeader *message,
                    const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
{
  struct dns_pkt *dns = (struct dns_pkt*)(message + 1);

  struct sockaddr_in dest;
  memset(&dest, 0, sizeof dest);
  dest.sin_port = htons(53);
  /* TODO: read from config */
  inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);

  query_states[dns->s.id].tunnel = tunnel;
  query_states[dns->s.id].valid = GNUNET_YES;

  GNUNET_NETWORK_socket_sendto(dnsout,
                               dns,
                               ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
                               (struct sockaddr*) &dest,
                               sizeof dest);

  return GNUNET_SYSERR;
}

static int
receive_mesh_answer (void *cls __attribute__((unused)),
                     struct GNUNET_MESH_Tunnel *tunnel,
                     void **ctx __attribute__((unused)),
                     const struct GNUNET_PeerIdentity *sender,
                     const struct GNUNET_MessageHeader *message,
                     const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
{
  /* TODo: size check */
  struct dns_pkt *dns = (struct dns_pkt *) (message + 1);

  /* They sent us a packet we were not waiting for */
  if (remote_pending[dns->s.id] == NULL
      || remote_pending[dns->s.id]->tunnel != tunnel)
    return GNUNET_SYSERR;
  if (query_states[dns->s.id].valid != GNUNET_YES)
    return GNUNET_SYSERR;
  query_states[dns->s.id].valid = GNUNET_NO;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id));

  size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + query_states[dns->s.id].namelen + sizeof (struct dns_query_line) + 2    /* To hold the pointer (as defined in RFC1035) to the name */
    + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */

  struct answer_packet_list *answer =
    GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
  memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));

  answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
  answer->pkt.hdr.size = htons (len);
  answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE;

  struct dns_pkt_parsed* pdns = parse_dns_packet(dns);

  if (ntohs(pdns->s.ancount) < 1)
    {
      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount));
      free_parsed_dns_packet(pdns);
      return GNUNET_OK;
    }

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The first answer has the length %d\n", pdns->answers[0]->data_len);
  answer->pkt.addrsize = pdns->answers[0]->data_len;
  memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));

  answer->pkt.from = query_states[dns->s.id].remote_ip;

  answer->pkt.to = query_states[dns->s.id].local_ip;
  answer->pkt.dst_port = query_states[dns->s.id].local_port;

  struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;

  dpkt->s.id = dns->s.id;
  dpkt->s.aa = 1;
  dpkt->s.qr = 1;
  dpkt->s.ra = 1;
  dpkt->s.qdcount = htons (1);
  dpkt->s.ancount = htons (1);

  memcpy (dpkt->data, query_states[dns->s.id].name,
          query_states[dns->s.id].namelen);
  GNUNET_free (query_states[dns->s.id].name);

  struct dns_query_line *dque =
    (struct dns_query_line *) (dpkt->data +
                               (query_states[dns->s.id].namelen));
  dque->type = htons (28);      /* AAAA */
  dque->class = htons (1);      /* IN */

  char *anname =
    (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
              sizeof (struct dns_query_line));
  memcpy (anname, "\xc0\x0c", 2);

  struct dns_record_line *drec_data =
    (struct dns_record_line *) (dpkt->data +
                                (query_states[dns->s.id].namelen) +
                                sizeof (struct dns_query_line) + 2);
  drec_data->type = htons (28); /* AAAA */
  drec_data->class = htons (1); /* IN */

  drec_data->ttl = pdns->answers[0]->ttl;
  drec_data->data_len = htons (16);

  /* Calculate at which offset in the packet the IPv6-Address belongs, it is
   * filled in by the daemon-vpn */
  answer->pkt.addroffset =
    htons ((unsigned short) ((unsigned long) (&drec_data->data) -
                             (unsigned long) (&answer->pkt)));

  GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);

  GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
                                       len,
                                       GNUNET_TIME_UNIT_FOREVER_REL,
                                       &send_answer,
                                       query_states[dns->s.id].client);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer on to client\n");

  free_parsed_dns_packet(pdns);
  return GNUNET_OK;
}


static void
send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
    if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
      return;

    struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;

    unsigned short id = pdns->s.id;

    free_parsed_dns_packet(pdns);

    if (query_states[id].valid != GNUNET_YES) return;
    query_states[id].valid = GNUNET_NO;

    GNUNET_assert(query_states[id].namelen == 74);

    size_t len = sizeof(struct answer_packet) - 1 \
		 + sizeof(struct dns_static) \
		 + 74 /* this is the length of a reverse ipv6-lookup */ \
		 + sizeof(struct dns_query_line) \
		 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
		 + sizeof(struct dns_record_line) - 1 \
		 - 2 /* We do not know the lenght of the answer yet*/;

    struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
    memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));

    answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
    answer->pkt.hdr.size = htons(len);
    answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;

    answer->pkt.from = query_states[id].remote_ip;

    answer->pkt.to = query_states[id].local_ip;
    answer->pkt.dst_port = query_states[id].local_port;

    struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;

    dpkt->s.id = id;
    dpkt->s.aa = 1;
    dpkt->s.qr = 1;
    dpkt->s.ra = 1;
    dpkt->s.qdcount = htons(1);
    dpkt->s.ancount = htons(1);

    memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
    GNUNET_free(query_states[id].name);

    struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
    dque->type = htons(12); /* PTR */
    dque->class = htons(1); /* IN */

    char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
    memcpy(anname, "\xc0\x0c", 2);

    struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
    drec_data->type = htons(12); /* AAAA */
    drec_data->class = htons(1); /* IN */
    /* FIXME: read the TTL from block:
     * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
     *
     * But how to get the seconds out of this?
     */
    drec_data->ttl = htonl(3600);

    /* Calculate at which offset in the packet the length of the name and the
     * name, it is filled in by the daemon-vpn */
    answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));

    GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);

    GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
					len,
					GNUNET_TIME_UNIT_FOREVER_REL,
					&send_answer,
					query_states[id].client);
}

/**
 * Receive a block from the dht.
 */
static void
receive_dht(void *cls,
	    struct GNUNET_TIME_Absolute exp __attribute__((unused)),
	    const GNUNET_HashCode *key __attribute__((unused)),
	    const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)),
	    const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)),
	    enum GNUNET_BLOCK_Type type,
	    size_t size,
	    const void *data) {

    unsigned short id = ((struct receive_dht_cls*)cls)->id;
    struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
    GNUNET_free(cls);

    GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);

    /* If no query with this id is pending, ignore the block */
    if (query_states[id].valid != GNUNET_YES) return;
    query_states[id].valid = GNUNET_NO;

    const struct GNUNET_DNS_Record* rec = data;
    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
	       "Got block of size %d, peer: %08x, desc: %08x\n",
	       size,
	       *((unsigned int*)&rec->peer),
	       *((unsigned int*)&rec->service_descriptor));

    size_t len = sizeof(struct answer_packet) - 1 \
		 + sizeof(struct dns_static) \
		 + query_states[id].namelen \
		 + sizeof(struct dns_query_line) \
		 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
		 + sizeof(struct dns_record_line) - 1 \
		 + 16; /* To hold the IPv6-Address */

    struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
    memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));

    answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
    answer->pkt.hdr.size = htons(len);
    answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;

    GNUNET_CRYPTO_hash(&rec->peer,
		       sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
		       &answer->pkt.service_descr.peer);

    memcpy(&answer->pkt.service_descr.service_descriptor,
	   &rec->service_descriptor,
	   sizeof(GNUNET_HashCode));
    memcpy(&answer->pkt.service_descr.service_type,
	   &rec->service_type,
	   sizeof(answer->pkt.service_descr.service_type));
    memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));

    answer->pkt.from = query_states[id].remote_ip;

    answer->pkt.to = query_states[id].local_ip;
    answer->pkt.dst_port = query_states[id].local_port;

    struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;

    dpkt->s.id = id;
    dpkt->s.aa = 1;
    dpkt->s.qr = 1;
    dpkt->s.ra = 1;
    dpkt->s.qdcount = htons(1);
    dpkt->s.ancount = htons(1);

    memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
    GNUNET_free(query_states[id].name);

    struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
    dque->type = htons(28); /* AAAA */
    dque->class = htons(1); /* IN */

    char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
    memcpy(anname, "\xc0\x0c", 2);

    struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
    drec_data->type = htons(28); /* AAAA */
    drec_data->class = htons(1); /* IN */

    /* FIXME: read the TTL from block:
     * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
     *
     * But how to get the seconds out of this?
     */
    drec_data->ttl = htonl(3600);
    drec_data->data_len = htons(16);

    /* Calculate at which offset in the packet the IPv6-Address belongs, it is
     * filled in by the daemon-vpn */
    answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));

    GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);

    GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
					len,
					GNUNET_TIME_UNIT_FOREVER_REL,
					&send_answer,
					query_states[id].client);

    GNUNET_DHT_get_stop(handle);
}

/**
 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
 */
static void
rehijack(void *cls __attribute__((unused)),
	 struct GNUNET_SERVER_Client *client,
	 const struct GNUNET_MessageHeader *message __attribute__((unused))) {
    unhijack(dnsoutport);
    GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);

    GNUNET_SERVER_receive_done(client, GNUNET_OK);
}

/**
 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
 */
static void
receive_query(void *cls __attribute__((unused)),
	      struct GNUNET_SERVER_Client *client,
	      const struct GNUNET_MessageHeader *message) {
    struct query_packet* pkt = (struct query_packet*)message;
    struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
    struct dns_pkt_parsed* pdns = parse_dns_packet(dns);

    query_states[dns->s.id].valid = GNUNET_YES;
    query_states[dns->s.id].client = client;
    query_states[dns->s.id].local_ip = pkt->orig_from;
    query_states[dns->s.id].local_port = pkt->src_port;
    query_states[dns->s.id].remote_ip = pkt->orig_to;
    query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
    query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
    memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);

    /* The query is for a .gnunet-address */
    if (pdns->queries[0]->namelen > 9 &&
	0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
      {
	GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
	GNUNET_HashCode key;
	GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);

	GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
		   "Getting with key %08x, len is %d\n",
		   *((unsigned int*)&key),
		   pdns->queries[0]->namelen);

	struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
	cls->id = dns->s.id;

	cls->handle = GNUNET_DHT_get_start(dht,
					   GNUNET_TIME_UNIT_MINUTES,
					   GNUNET_BLOCK_TYPE_DNS,
					   &key,
					   DEFAULT_GET_REPLICATION,
					   GNUNET_DHT_RO_NONE,
					   NULL,
					   0,
					   NULL,
					   0,
					   receive_dht,
					   cls);

	goto outfree;
      }

    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);

    /* This is a PTR-Query. Check if it is for "our" network */
    if (htons(pdns->queries[0]->qtype) == 12 &&
	74 == pdns->queries[0]->namelen)
      {
        char* ipv6addr;
        char ipv6[16];
        char ipv6rev[74] = "X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.X.ip6.arpa.";
        unsigned int i;
        unsigned long long ipv6prefix;
        unsigned int comparelen;

        GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
        inet_pton (AF_INET6, ipv6addr, ipv6);
        GNUNET_free(ipv6addr);

        GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
        GNUNET_assert(ipv6prefix < 127);
        ipv6prefix = (ipv6prefix + 7)/8;

        for (i = ipv6prefix; i < 16; i++)
          ipv6[i] = 0;

	for (i = 0; i < 16; i++)
	  {
	    unsigned char c1 = ipv6[i] >> 4;
	    unsigned char c2 = ipv6[i] & 0xf;

	    if (c1 <= 9)
              ipv6rev[62-(4*i)] = c1 + '0';
	    else
              ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */

	    if (c2 <= 9)
              ipv6rev[62-((4*i)+2)] = c2 + '0';
	    else
              ipv6rev[62-((4*i)+2)] = c2 + 87;
	  }
        GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
        comparelen = 10 + 4*ipv6prefix;
        if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
                        ipv6rev + (74 - comparelen),
                        comparelen))
          {
            GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");

            GNUNET_SCHEDULER_add_now(send_rev_query, pdns);

            goto out;
          }
      }

    char* virt_dns;
    unsigned int virt_dns_bytes;
    if (GNUNET_SYSERR ==
        GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
                                               &virt_dns))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "No entry 'VIRTDNS' in configuration!\n");
        exit (1);
      }

    if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
        exit(1);
      }

    GNUNET_free(virt_dns);

    if (virt_dns_bytes == pkt->orig_to)
      {
        /* This is a packet that was sent directly to the virtual dns-server
         *
         * This means we have to send this query over gnunet
         */

        size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
        struct tunnel_cls *cls_ =  GNUNET_malloc(size);
        cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);

        cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
        GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);

        memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader));
        GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);

        goto out;
      }


    /* The query should be sent to the network */

    struct sockaddr_in dest;
    memset(&dest, 0, sizeof dest);
    dest.sin_port = htons(53);
    dest.sin_addr.s_addr = pkt->orig_to;

    GNUNET_NETWORK_socket_sendto(dnsout,
				 dns,
				 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
				 (struct sockaddr*) &dest,
				 sizeof dest);

outfree:
    free_parsed_dns_packet(pdns);
    pdns = NULL;
out:
    GNUNET_SERVER_receive_done(client, GNUNET_OK);
}

static void read_response (void *cls,
                           const struct GNUNET_SCHEDULER_TaskContext *tc);

static void
open_port ()
{
  struct sockaddr_in addr;

  dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
  if (dnsout == NULL)
    return;
  memset (&addr, 0, sizeof (struct sockaddr_in));

  int err = GNUNET_NETWORK_socket_bind (dnsout,
                                        (struct sockaddr *) &addr,
                                        sizeof (struct sockaddr_in));

  if (err != GNUNET_YES)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "Could not bind a port, exiting\n");
      return;
    }

  /* Read the port we bound to */
  socklen_t addrlen = sizeof (struct sockaddr_in);
  err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
                     (struct sockaddr *) &addr, &addrlen);

  dnsoutport = htons (addr.sin_port);

  GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
                                 &read_response, NULL);
}

/**
 * Read a response-packet of the UDP-Socket
 */
static void
read_response (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct sockaddr_in addr;
  socklen_t addrlen = sizeof (addr);
  int r;
  int len;

  if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
    return;

  memset (&addr, 0, sizeof addr);

#ifndef MINGW
  if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
    {
      unhijack (dnsoutport);
      open_port ();
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
      return;
    }
#else
  /* port the code above? */
  len = 65536;
#endif
  {
    unsigned char buf[len];
    struct dns_pkt *dns = (struct dns_pkt *) buf;

    r = GNUNET_NETWORK_socket_recvfrom (dnsout,
                                        buf,
                                        sizeof (buf),
                                        (struct sockaddr *) &addr, &addrlen);

    if (r < 0)
      {
        unhijack (dnsoutport);
        open_port ();
        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
        return;
      }

    if (query_states[dns->s.id].valid == GNUNET_YES)
      {
        if (query_states[dns->s.id].tunnel != NULL)
          {
            uint32_t *c = GNUNET_malloc(4 + r);
            *c = r;
            memcpy(c+1, dns, r);
            GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
                                               GNUNET_YES,
                                               32,
                                               GNUNET_TIME_UNIT_MINUTES,
                                               NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
          }
        else
          {
            query_states[dns->s.id].valid = GNUNET_NO;

            size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
            struct answer_packet_list *answer =
              GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
            answer->pkt.hdr.type =
              htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
            answer->pkt.hdr.size = htons (len);
            answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
            answer->pkt.from = addr.sin_addr.s_addr;
            answer->pkt.to = query_states[dns->s.id].local_ip;
            answer->pkt.dst_port = query_states[dns->s.id].local_port;
            memcpy (answer->pkt.data, buf, r);

            GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);

            GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
                                                 client, len,
                                                 GNUNET_TIME_UNIT_FOREVER_REL,
                                                 &send_answer,
                                                 query_states[dns->s.id].
                                                 client);
          }
      }
  }
  GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
                                 dnsout, &read_response, NULL);
}


/**
 * Task run during shutdown.
 *
 * @param cls unused
 * @param tc unused
 */
static void
cleanup_task (void *cls __attribute__((unused)),
	      const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));

  unhijack(dnsoutport);
  GNUNET_DHT_disconnect(dht);
}

/**
 * @brief Create a port-map from udp and tcp redirects
 *
 * @param udp_redirects
 * @param tcp_redirects
 *
 * @return 
 */
uint64_t
get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
{
  uint64_t ret = 0;
  char* cpy, *hostname, *redirect;
  int local_port, count = 0;

  if (NULL != udp_redirects)
    {
      cpy = GNUNET_strdup (udp_redirects);
      for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
        {
          if (NULL == (hostname = strstr (redirect, ":")))
            {
              GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
              continue;
            }
          hostname[0] = '\0';
          local_port = atoi (redirect);
          if (!((local_port > 0) && (local_port < 65536)))
            GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);

          ret |= (0xFFFF & htons(local_port));
          ret <<= 16;
          count ++;

          if(count > 4)
            {
              ret = 0;
              goto out;
            }
        }
      GNUNET_free(cpy);
      cpy = NULL;
    }

  if (NULL != tcp_redirects)
    {
      cpy = GNUNET_strdup (tcp_redirects);
      for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
        {
          if (NULL == (hostname = strstr (redirect, ":")))
            {
              GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
              continue;
            }
          hostname[0] = '\0';
          local_port = atoi (redirect);
          if (!((local_port > 0) && (local_port < 65536)))
            GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);

          ret |= (0xFFFF & htons(local_port));
          ret <<= 16;
          count ++;

          if(count > 4)
            {
              ret = 0;
              goto out;
            }
        }
      GNUNET_free(cpy);
      cpy = NULL;
    }

out:
  if (NULL != cpy)
    GNUNET_free(cpy);
  return ret;
}

void
publish_name (const char *name, uint64_t ports, uint32_t service_type,
              struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
{
  size_t size = sizeof (struct GNUNET_DNS_Record);
  struct GNUNET_DNS_Record data;
  memset (&data, 0, size);

  data.purpose.size =
    htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
  data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;

  GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
              *((unsigned long long *) &data.service_descriptor));

  data.service_type = service_type;
  data.ports = ports;

  GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);

  data.expiration_time =
    GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));

  /* Sign the block */
  if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
                                           &data.purpose, &data.signature))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
      return;
    }

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Putting with key %08x, size = %d\n",
              *((unsigned int *) &data.service_descriptor), size);

  GNUNET_DHT_put (dht,
                  &data.service_descriptor,
                  DEFAULT_PUT_REPLICATION,
                  GNUNET_DHT_RO_NONE,
                  GNUNET_BLOCK_TYPE_DNS,
                  size,
                  (char *) &data,
                  GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
                  GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
}

/**
 * @brief Publishes the record defined by the section section
 *
 * @param cls closure
 * @param section the current section
 */
void
publish_iterate (void *cls __attribute__((unused)), const char *section)
{
  if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
    return;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);

  char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
    *keyfile;

  GNUNET_CONFIGURATION_get_value_string (cfg, section,
                                         "UDP_REDIRECTS", &udp_redirects);
  GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
                                         &tcp_redirects);

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
                                                            "HOSTKEY",
                                                            &keyfile))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
      if (keyfile != NULL)
        GNUNET_free (keyfile);
      return;
    }

  struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
    GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
  GNUNET_free (keyfile);
  GNUNET_assert (my_private_key != NULL);

  uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
  uint32_t service_type = 0;

  if (NULL != udp_redirects)
    service_type = GNUNET_DNS_SERVICE_TYPE_UDP;

  if (NULL != tcp_redirects)
    service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;

  service_type = htonl (service_type);


  publish_name (section, ports, service_type, my_private_key);

  GNUNET_CONFIGURATION_get_value_string (cfg, section,
                                         "ALTERNATIVE_NAMES",
                                         &alternative_names);
  for (alternative_name = strtok (alternative_names, " ");
       alternative_name != NULL; alternative_name = strtok (NULL, " "))
    {
      char *altname =
        alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
      strcpy (altname, alternative_name);
      strcpy (altname + strlen (alternative_name) + 1, section);
      altname[strlen (alternative_name)] = '.';

      publish_name (altname, ports, service_type, my_private_key);
    }

  GNUNET_free_non_null(alternative_names);
  GNUNET_CRYPTO_rsa_key_free (my_private_key);
  GNUNET_free_non_null (udp_redirects);
  GNUNET_free_non_null (tcp_redirects);
}

/**
 * Publish a DNS-record in the DHT.
 */
static void
publish_names (void *cls __attribute__((unused)),
               const struct GNUNET_SCHEDULER_TaskContext *tc) {
    if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
      return;

    GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL);

    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
                                  publish_names,
                                  NULL);
}

/**
 * @param cls closure
 * @param server the initialized server
 * @param cfg_ configuration to use
 */
static void
run (void *cls,
     struct GNUNET_SERVER_Handle *server,
     const struct GNUNET_CONFIGURATION_Handle *cfg_)
{
  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
    /* callback, cls, type, size */
    {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
    {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
     sizeof (struct GNUNET_MessageHeader)},
    {NULL, NULL, 0, 0}
  };

  static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
    {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
    {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
    {NULL, 0, 0}
  };

  static GNUNET_MESH_ApplicationType *apptypes;

  if (GNUNET_YES ==
      GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
    apptypes = (GNUNET_MESH_ApplicationType[])
    {
    GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
    GNUNET_APPLICATION_TYPE_END};
  else
  apptypes = (GNUNET_MESH_ApplicationType[])
  {
  GNUNET_APPLICATION_TYPE_END};

  mesh_handle =
    GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);

  cfg = cfg_;

  unsigned int i;
  for (i = 0; i < 65536; i++)
    {
      query_states[i].valid = GNUNET_NO;
    }

  dht = GNUNET_DHT_connect (cfg, 1024);

  open_port ();

  GNUNET_SCHEDULER_add_now (publish_names, NULL);

  GNUNET_SERVER_add_handlers (server, handlers);
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
                                &cleanup_task, cls);
}

/**
 * The main function for the dns service.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  return (GNUNET_OK ==
	  GNUNET_SERVICE_run (argc,
			      argv,
			      "dns",
			      GNUNET_SERVICE_OPTION_NONE,
			      &run, NULL)) ? 0 : 1;
}