debug the dns
[oweals/gnunet.git] / src / vpn / gnunet-service-dns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-service-dns.c
23  * @author Philipp Toelke
24  */
25 #include "platform.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_service_lib.h"
28 #include "gnunet_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include "gnunet-vpn-packet.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet-dns-parser.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_block_lib.h"
38 #include "block_dns.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_mesh_service.h"
41 #include "gnunet_signatures.h"
42
43 struct GNUNET_MESH_Handle *mesh_handle;
44
45 /**
46  * The tunnel to send queries
47  */
48 static struct GNUNET_MESH_Tunnel* dns_tunnel;
49
50 /**
51  * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
52  * sent through gnunet. The port of this socket will not be hijacked.
53  */
54 static struct GNUNET_NETWORK_Handle *dnsout;
55
56 /**
57  * The port bound to the socket dnsout
58  */
59 static unsigned short dnsoutport;
60
61 /**
62  * A handle to the DHT-Service
63  */
64 static struct GNUNET_DHT_Handle *dht;
65
66 /**
67  * The configuration to use
68  */
69 static const struct GNUNET_CONFIGURATION_Handle *cfg;
70
71 /**
72  * A list of DNS-Responses that have to be sent to the requesting client
73  */
74 static struct answer_packet_list *head;
75
76 /**
77  * The tail of the list of DNS-responses
78  */
79 static struct answer_packet_list *tail;
80
81 /**
82  * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
83  * some information needed to handle this query
84  *
85  * It currently allocates at least
86  * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
87  * = 17 MiB on 64 bit.
88  * = 11 MiB on 32 bit.
89  */
90 static struct {
91     unsigned valid:1;
92     struct GNUNET_SERVER_Client* client;
93     struct GNUNET_MESH_Tunnel *tunnel;
94     uint32_t local_ip;
95     uint32_t remote_ip;
96     uint16_t local_port;
97     char* name;
98     uint8_t namelen;
99 } query_states[UINT16_MAX];
100
101 /**
102  * A struct used to give more than one value as
103  * closure to receive_dht
104  */
105 struct receive_dht_cls {
106     uint16_t id;
107     struct GNUNET_DHT_GetHandle* handle;
108 };
109
110 /**
111  * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
112  */
113 static void
114 hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
117     return;
118
119   char port_s[6];
120   char *virt_dns;
121   struct GNUNET_OS_Process *proc;
122
123   if (GNUNET_SYSERR ==
124       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
125                                              &virt_dns))
126     {
127       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128                   "No entry 'VIRTDNS' in configuration!\n");
129       exit (1);
130     }
131
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
133   snprintf (port_s, 6, "%d", dnsoutport);
134   if (NULL != (proc = GNUNET_OS_start_process (NULL,
135                                                NULL,
136                                                "gnunet-helper-hijack-dns",
137                                                "gnunet-hijack-dns",
138                                                port_s, virt_dns, NULL)))
139     GNUNET_OS_process_close (proc);
140   GNUNET_free (virt_dns);
141 }
142
143 /**
144  * Delete the hijacking-routes
145  */
146 static void
147 unhijack (unsigned short port)
148 {
149   char port_s[6];
150   char *virt_dns;
151   struct GNUNET_OS_Process *proc;
152
153   if (GNUNET_SYSERR ==
154       GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
155                                              &virt_dns))
156     {
157       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158                   "No entry 'VIRTDNS' in configuration!\n");
159       exit (1);
160     }
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
163   snprintf (port_s, 6, "%d", port);
164   if (NULL != (proc = GNUNET_OS_start_process (NULL,
165                                                NULL,
166                                                "gnunet-helper-hijack-dns",
167                                                "gnunet-hijack-dns",
168                                                "-d", port_s, virt_dns, NULL)))
169     GNUNET_OS_process_close (proc);
170   GNUNET_free (virt_dns);
171 }
172
173 /**
174  * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
175  * system.
176  */
177 static size_t
178 send_answer(void* cls, size_t size, void* buf) {
179     struct answer_packet_list* query = head;
180     size_t len = ntohs(query->pkt.hdr.size);
181
182     GNUNET_assert(len <= size);
183
184     memcpy(buf, &query->pkt.hdr, len);
185
186     GNUNET_CONTAINER_DLL_remove (head, tail, query);
187
188     GNUNET_free(query);
189
190     /* When more data is to be sent, reschedule */
191     if (head != NULL)
192       GNUNET_SERVER_notify_transmit_ready(cls,
193                                           ntohs(head->pkt.hdr.size),
194                                           GNUNET_TIME_UNIT_FOREVER_REL,
195                                           &send_answer,
196                                           cls);
197
198     return len;
199 }
200
201 struct tunnel_cls {
202     struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
203     struct GNUNET_MessageHeader hdr;
204     struct dns_pkt dns;
205 };
206
207 struct tunnel_cls *remote_pending[UINT16_MAX];
208
209 static size_t
210 mesh_send_response (void *cls, size_t size, void *buf)
211 {
212   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
213   struct GNUNET_MessageHeader *hdr = buf;
214   uint32_t *sz = cls;
215   struct dns_pkt *dns = (struct dns_pkt *) (sz + 1);
216   hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
217   hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
218
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220               "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
221               *sz + sizeof (struct GNUNET_MessageHeader));
222
223   GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
224
225   memcpy (hdr + 1, dns, *sz);
226   GNUNET_free (cls);
227   return ntohs (hdr->size);
228 }
229
230 static size_t
231 mesh_send (void *cls, size_t size, void *buf)
232 {
233   struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
234
235   GNUNET_assert(cls_->hdr.size <= size);
236
237   size = cls_->hdr.size;
238   cls_->hdr.size = htons(cls_->hdr.size);
239
240   memcpy(buf, &cls_->hdr, size);
241   return size;
242 }
243
244
245 void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) {
246   if (NULL == peer) return;
247   struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
248   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s, sending query with id %d\n", GNUNET_i2s(peer), ntohs(cls_->dns.s.id));
249
250   GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
251                                     GNUNET_YES,
252                                     42,
253                                     GNUNET_TIME_UNIT_MINUTES,
254                                     NULL,
255                                     cls_->hdr.size,
256                                     mesh_send,
257                                     cls);
258 }
259
260
261 static void
262 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
263 {
264   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
265     return;
266
267   struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
268
269   if (NULL == dns_tunnel)
270     dns_tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
271                                                           GNUNET_TIME_UNIT_HOURS,
272                                                           GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
273                                                           mesh_connect,
274                                                           NULL,
275                                                           cls_);
276   cls_->tunnel = dns_tunnel;
277   remote_pending[cls_->dns.s.id] = cls_;
278 }
279
280 static int
281 receive_mesh_query (void *cls __attribute__((unused)),
282                     struct GNUNET_MESH_Tunnel *tunnel,
283                     void **ctx __attribute__((unused)),
284                     const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
285                     const struct GNUNET_MessageHeader *message,
286                     const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
287 {
288   struct dns_pkt *dns = (struct dns_pkt*)(message + 1);
289
290   struct sockaddr_in dest;
291   memset(&dest, 0, sizeof dest);
292   dest.sin_port = htons(53);
293   /* TODO: read from config */
294   inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
295
296   query_states[dns->s.id].tunnel = tunnel;
297   query_states[dns->s.id].valid = GNUNET_YES;
298
299   GNUNET_NETWORK_socket_sendto(dnsout,
300                                dns,
301                                ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
302                                (struct sockaddr*) &dest,
303                                sizeof dest);
304
305   return GNUNET_SYSERR;
306 }
307
308 static int
309 receive_mesh_answer (void *cls __attribute__((unused)),
310                      struct GNUNET_MESH_Tunnel *tunnel,
311                      void **ctx __attribute__((unused)),
312                      const struct GNUNET_PeerIdentity *sender,
313                      const struct GNUNET_MessageHeader *message,
314                      const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
315 {
316   /* TODo: size check */
317   struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
318
319   /* They sent us a packet we were not waiting for */
320   if (remote_pending[dns->s.id] == NULL
321       || remote_pending[dns->s.id]->tunnel != tunnel)
322     return GNUNET_SYSERR;
323   if (query_states[dns->s.id].valid != GNUNET_YES)
324     return GNUNET_SYSERR;
325   query_states[dns->s.id].valid = GNUNET_NO;
326
327   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id));
328
329   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 */
330     + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
331
332   struct answer_packet_list *answer =
333     GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
334   memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
335
336   answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
337   answer->pkt.hdr.size = htons (len);
338   answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE;
339
340   struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
341
342   if (ntohs(pdns->s.ancount) < 1)
343     {
344       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount));
345       free_parsed_dns_packet(pdns);
346       return GNUNET_OK;
347     }
348
349   answer->pkt.addrsize = ntohs(pdns->answers[0]->data_len);
350   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n", answer->pkt.addrsize);
351   memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
352
353   answer->pkt.from = query_states[dns->s.id].remote_ip;
354
355   answer->pkt.to = query_states[dns->s.id].local_ip;
356   answer->pkt.dst_port = query_states[dns->s.id].local_port;
357
358   struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
359
360   dpkt->s.id = dns->s.id;
361   dpkt->s.aa = 1;
362   dpkt->s.qr = 1;
363   dpkt->s.ra = 1;
364   dpkt->s.qdcount = htons (1);
365   dpkt->s.ancount = htons (1);
366
367   memcpy (dpkt->data, query_states[dns->s.id].name,
368           query_states[dns->s.id].namelen);
369   GNUNET_free (query_states[dns->s.id].name);
370
371   struct dns_query_line *dque =
372     (struct dns_query_line *) (dpkt->data +
373                                (query_states[dns->s.id].namelen));
374   dque->type = htons (28);      /* AAAA */
375   dque->class = htons (1);      /* IN */
376
377   char *anname =
378     (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
379               sizeof (struct dns_query_line));
380   memcpy (anname, "\xc0\x0c", 2);
381
382   struct dns_record_line *drec_data =
383     (struct dns_record_line *) (dpkt->data +
384                                 (query_states[dns->s.id].namelen) +
385                                 sizeof (struct dns_query_line) + 2);
386   drec_data->type = htons (28); /* AAAA */
387   drec_data->class = htons (1); /* IN */
388
389   drec_data->ttl = pdns->answers[0]->ttl;
390   drec_data->data_len = htons (16);
391
392   /* Calculate at which offset in the packet the IPv6-Address belongs, it is
393    * filled in by the daemon-vpn */
394   answer->pkt.addroffset =
395     htons ((unsigned short) ((unsigned long) (&drec_data->data) -
396                              (unsigned long) (&answer->pkt)));
397
398   GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
399
400   GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
401                                        len,
402                                        GNUNET_TIME_UNIT_FOREVER_REL,
403                                        &send_answer,
404                                        query_states[dns->s.id].client);
405
406   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer of length %d on to client, addroffset = %d\n", len, answer->pkt.addroffset);
407
408   free_parsed_dns_packet(pdns);
409   return GNUNET_OK;
410 }
411
412
413 static void
414 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
415     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
416       return;
417
418     struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
419
420     unsigned short id = pdns->s.id;
421
422     free_parsed_dns_packet(pdns);
423
424     if (query_states[id].valid != GNUNET_YES) return;
425     query_states[id].valid = GNUNET_NO;
426
427     GNUNET_assert(query_states[id].namelen == 74);
428
429     size_t len = sizeof(struct answer_packet) - 1 \
430                  + sizeof(struct dns_static) \
431                  + 74 /* this is the length of a reverse ipv6-lookup */ \
432                  + sizeof(struct dns_query_line) \
433                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
434                  + sizeof(struct dns_record_line) - 1 \
435                  - 2 /* We do not know the lenght of the answer yet*/;
436
437     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
438     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
439
440     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
441     answer->pkt.hdr.size = htons(len);
442     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
443
444     answer->pkt.from = query_states[id].remote_ip;
445
446     answer->pkt.to = query_states[id].local_ip;
447     answer->pkt.dst_port = query_states[id].local_port;
448
449     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
450
451     dpkt->s.id = id;
452     dpkt->s.aa = 1;
453     dpkt->s.qr = 1;
454     dpkt->s.ra = 1;
455     dpkt->s.qdcount = htons(1);
456     dpkt->s.ancount = htons(1);
457
458     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
459     GNUNET_free(query_states[id].name);
460
461     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
462     dque->type = htons(12); /* PTR */
463     dque->class = htons(1); /* IN */
464
465     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
466     memcpy(anname, "\xc0\x0c", 2);
467
468     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
469     drec_data->type = htons(12); /* AAAA */
470     drec_data->class = htons(1); /* IN */
471     /* FIXME: read the TTL from block:
472      * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
473      *
474      * But how to get the seconds out of this?
475      */
476     drec_data->ttl = htonl(3600);
477
478     /* Calculate at which offset in the packet the length of the name and the
479      * name, it is filled in by the daemon-vpn */
480     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
481
482     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
483
484     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
485                                         len,
486                                         GNUNET_TIME_UNIT_FOREVER_REL,
487                                         &send_answer,
488                                         query_states[id].client);
489 }
490
491 /**
492  * Receive a block from the dht.
493  */
494 static void
495 receive_dht(void *cls,
496             struct GNUNET_TIME_Absolute exp __attribute__((unused)),
497             const GNUNET_HashCode *key __attribute__((unused)),
498             const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)),
499             const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)),
500             enum GNUNET_BLOCK_Type type,
501             size_t size,
502             const void *data) {
503
504     unsigned short id = ((struct receive_dht_cls*)cls)->id;
505     struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
506     GNUNET_free(cls);
507
508     GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
509
510     /* If no query with this id is pending, ignore the block */
511     if (query_states[id].valid != GNUNET_YES) return;
512     query_states[id].valid = GNUNET_NO;
513
514     const struct GNUNET_DNS_Record* rec = data;
515     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
516                "Got block of size %d, peer: %08x, desc: %08x\n",
517                size,
518                *((unsigned int*)&rec->peer),
519                *((unsigned int*)&rec->service_descriptor));
520
521     size_t len = sizeof(struct answer_packet) - 1 \
522                  + sizeof(struct dns_static) \
523                  + query_states[id].namelen \
524                  + sizeof(struct dns_query_line) \
525                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
526                  + sizeof(struct dns_record_line) - 1 \
527                  + 16; /* To hold the IPv6-Address */
528
529     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
530     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
531
532     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
533     answer->pkt.hdr.size = htons(len);
534     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
535
536     GNUNET_CRYPTO_hash(&rec->peer,
537                        sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
538                        &answer->pkt.service_descr.peer);
539
540     memcpy(&answer->pkt.service_descr.service_descriptor,
541            &rec->service_descriptor,
542            sizeof(GNUNET_HashCode));
543     memcpy(&answer->pkt.service_descr.service_type,
544            &rec->service_type,
545            sizeof(answer->pkt.service_descr.service_type));
546     memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
547
548     answer->pkt.from = query_states[id].remote_ip;
549
550     answer->pkt.to = query_states[id].local_ip;
551     answer->pkt.dst_port = query_states[id].local_port;
552
553     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
554
555     dpkt->s.id = id;
556     dpkt->s.aa = 1;
557     dpkt->s.qr = 1;
558     dpkt->s.ra = 1;
559     dpkt->s.qdcount = htons(1);
560     dpkt->s.ancount = htons(1);
561
562     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
563     GNUNET_free(query_states[id].name);
564
565     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
566     dque->type = htons(28); /* AAAA */
567     dque->class = htons(1); /* IN */
568
569     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
570     memcpy(anname, "\xc0\x0c", 2);
571
572     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
573     drec_data->type = htons(28); /* AAAA */
574     drec_data->class = htons(1); /* IN */
575
576     /* FIXME: read the TTL from block:
577      * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
578      *
579      * But how to get the seconds out of this?
580      */
581     drec_data->ttl = htonl(3600);
582     drec_data->data_len = htons(16);
583
584     /* Calculate at which offset in the packet the IPv6-Address belongs, it is
585      * filled in by the daemon-vpn */
586     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
587
588     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
589
590     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
591                                         len,
592                                         GNUNET_TIME_UNIT_FOREVER_REL,
593                                         &send_answer,
594                                         query_states[id].client);
595
596     GNUNET_DHT_get_stop(handle);
597 }
598
599 /**
600  * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
601  */
602 static void
603 rehijack(void *cls __attribute__((unused)),
604          struct GNUNET_SERVER_Client *client,
605          const struct GNUNET_MessageHeader *message __attribute__((unused))) {
606     unhijack(dnsoutport);
607     GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
608
609     GNUNET_SERVER_receive_done(client, GNUNET_OK);
610 }
611
612 /**
613  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
614  */
615 static void
616 receive_query(void *cls __attribute__((unused)),
617               struct GNUNET_SERVER_Client *client,
618               const struct GNUNET_MessageHeader *message) {
619     struct query_packet* pkt = (struct query_packet*)message;
620     struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
621     struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
622
623     query_states[dns->s.id].valid = GNUNET_YES;
624     query_states[dns->s.id].client = client;
625     query_states[dns->s.id].local_ip = pkt->orig_from;
626     query_states[dns->s.id].local_port = pkt->src_port;
627     query_states[dns->s.id].remote_ip = pkt->orig_to;
628     query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
629     query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
630     memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
631
632     /* The query is for a .gnunet-address */
633     if (pdns->queries[0]->namelen > 9 &&
634         0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
635       {
636         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
637         GNUNET_HashCode key;
638         GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
639
640         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
641                    "Getting with key %08x, len is %d\n",
642                    *((unsigned int*)&key),
643                    pdns->queries[0]->namelen);
644
645         struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
646         cls->id = dns->s.id;
647
648         cls->handle = GNUNET_DHT_get_start(dht,
649                                            GNUNET_TIME_UNIT_MINUTES,
650                                            GNUNET_BLOCK_TYPE_DNS,
651                                            &key,
652                                            DEFAULT_GET_REPLICATION,
653                                            GNUNET_DHT_RO_NONE,
654                                            NULL,
655                                            0,
656                                            NULL,
657                                            0,
658                                            receive_dht,
659                                            cls);
660
661         goto outfree;
662       }
663
664     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
665
666     /* This is a PTR-Query. Check if it is for "our" network */
667     if (htons(pdns->queries[0]->qtype) == 12 &&
668         74 == pdns->queries[0]->namelen)
669       {
670         char* ipv6addr;
671         char ipv6[16];
672         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.";
673         unsigned int i;
674         unsigned long long ipv6prefix;
675         unsigned int comparelen;
676
677         GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
678         inet_pton (AF_INET6, ipv6addr, ipv6);
679         GNUNET_free(ipv6addr);
680
681         GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
682         GNUNET_assert(ipv6prefix < 127);
683         ipv6prefix = (ipv6prefix + 7)/8;
684
685         for (i = ipv6prefix; i < 16; i++)
686           ipv6[i] = 0;
687
688         for (i = 0; i < 16; i++)
689           {
690             unsigned char c1 = ipv6[i] >> 4;
691             unsigned char c2 = ipv6[i] & 0xf;
692
693             if (c1 <= 9)
694               ipv6rev[62-(4*i)] = c1 + '0';
695             else
696               ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
697
698             if (c2 <= 9)
699               ipv6rev[62-((4*i)+2)] = c2 + '0';
700             else
701               ipv6rev[62-((4*i)+2)] = c2 + 87;
702           }
703         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
704         comparelen = 10 + 4*ipv6prefix;
705         if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
706                         ipv6rev + (74 - comparelen),
707                         comparelen))
708           {
709             GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
710
711             GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
712
713             goto out;
714           }
715       }
716
717     char* virt_dns;
718     unsigned int virt_dns_bytes;
719     if (GNUNET_SYSERR ==
720         GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
721                                                &virt_dns))
722       {
723         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
724                     "No entry 'VIRTDNS' in configuration!\n");
725         exit (1);
726       }
727
728     if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
729       {
730         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731                     "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
732         exit(1);
733       }
734
735     GNUNET_free(virt_dns);
736
737     if (virt_dns_bytes == pkt->orig_to)
738       {
739         /* This is a packet that was sent directly to the virtual dns-server
740          *
741          * This means we have to send this query over gnunet
742          */
743
744         size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
745         struct tunnel_cls *cls_ =  GNUNET_malloc(size);
746         cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
747
748         cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
749         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
750
751         memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader));
752         GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
753
754         goto out;
755       }
756
757
758     /* The query should be sent to the network */
759
760     struct sockaddr_in dest;
761     memset(&dest, 0, sizeof dest);
762     dest.sin_port = htons(53);
763     dest.sin_addr.s_addr = pkt->orig_to;
764
765     GNUNET_NETWORK_socket_sendto(dnsout,
766                                  dns,
767                                  ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
768                                  (struct sockaddr*) &dest,
769                                  sizeof dest);
770
771 outfree:
772     free_parsed_dns_packet(pdns);
773     pdns = NULL;
774 out:
775     GNUNET_SERVER_receive_done(client, GNUNET_OK);
776 }
777
778 static void read_response (void *cls,
779                            const struct GNUNET_SCHEDULER_TaskContext *tc);
780
781 static void
782 open_port ()
783 {
784   struct sockaddr_in addr;
785
786   dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
787   if (dnsout == NULL)
788     return;
789   memset (&addr, 0, sizeof (struct sockaddr_in));
790
791   int err = GNUNET_NETWORK_socket_bind (dnsout,
792                                         (struct sockaddr *) &addr,
793                                         sizeof (struct sockaddr_in));
794
795   if (err != GNUNET_YES)
796     {
797       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
798                   "Could not bind a port, exiting\n");
799       return;
800     }
801
802   /* Read the port we bound to */
803   socklen_t addrlen = sizeof (struct sockaddr_in);
804   err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
805                      (struct sockaddr *) &addr, &addrlen);
806
807   dnsoutport = htons (addr.sin_port);
808
809   GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
810                                  &read_response, NULL);
811 }
812
813 /**
814  * Read a response-packet of the UDP-Socket
815  */
816 static void
817 read_response (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
818 {
819   struct sockaddr_in addr;
820   socklen_t addrlen = sizeof (addr);
821   int r;
822   int len;
823
824   if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
825     return;
826
827   memset (&addr, 0, sizeof addr);
828
829 #ifndef MINGW
830   if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
831     {
832       unhijack (dnsoutport);
833       open_port ();
834       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
835       return;
836     }
837 #else
838   /* port the code above? */
839   len = 65536;
840 #endif
841   {
842     unsigned char buf[len];
843     struct dns_pkt *dns = (struct dns_pkt *) buf;
844
845     r = GNUNET_NETWORK_socket_recvfrom (dnsout,
846                                         buf,
847                                         sizeof (buf),
848                                         (struct sockaddr *) &addr, &addrlen);
849
850     if (r < 0)
851       {
852         unhijack (dnsoutport);
853         open_port ();
854         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
855         return;
856       }
857
858     if (query_states[dns->s.id].valid == GNUNET_YES)
859       {
860         if (query_states[dns->s.id].tunnel != NULL)
861           {
862             uint32_t *c = GNUNET_malloc(4 + r);
863             *c = r;
864             memcpy(c+1, dns, r);
865             GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
866                                                GNUNET_YES,
867                                                32,
868                                                GNUNET_TIME_UNIT_MINUTES,
869                                                NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
870           }
871         else
872           {
873             query_states[dns->s.id].valid = GNUNET_NO;
874
875             size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
876             struct answer_packet_list *answer =
877               GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
878             answer->pkt.hdr.type =
879               htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
880             answer->pkt.hdr.size = htons (len);
881             answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
882             answer->pkt.from = addr.sin_addr.s_addr;
883             answer->pkt.to = query_states[dns->s.id].local_ip;
884             answer->pkt.dst_port = query_states[dns->s.id].local_port;
885             memcpy (answer->pkt.data, buf, r);
886
887             GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
888
889             GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
890                                                  client, len,
891                                                  GNUNET_TIME_UNIT_FOREVER_REL,
892                                                  &send_answer,
893                                                  query_states[dns->s.id].
894                                                  client);
895           }
896       }
897   }
898   GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
899                                  dnsout, &read_response, NULL);
900 }
901
902
903 /**
904  * Task run during shutdown.
905  *
906  * @param cls unused
907  * @param tc unused
908  */
909 static void
910 cleanup_task (void *cls __attribute__((unused)),
911               const struct GNUNET_SCHEDULER_TaskContext *tc)
912 {
913   GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
914
915   unhijack(dnsoutport);
916   GNUNET_DHT_disconnect(dht);
917 }
918
919 /**
920  * @brief Create a port-map from udp and tcp redirects
921  *
922  * @param udp_redirects
923  * @param tcp_redirects
924  *
925  * @return 
926  */
927 uint64_t
928 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
929 {
930   uint64_t ret = 0;
931   char* cpy, *hostname, *redirect;
932   int local_port, count = 0;
933
934   if (NULL != udp_redirects)
935     {
936       cpy = GNUNET_strdup (udp_redirects);
937       for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
938         {
939           if (NULL == (hostname = strstr (redirect, ":")))
940             {
941               GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
942               continue;
943             }
944           hostname[0] = '\0';
945           local_port = atoi (redirect);
946           if (!((local_port > 0) && (local_port < 65536)))
947             GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
948
949           ret |= (0xFFFF & htons(local_port));
950           ret <<= 16;
951           count ++;
952
953           if(count > 4)
954             {
955               ret = 0;
956               goto out;
957             }
958         }
959       GNUNET_free(cpy);
960       cpy = NULL;
961     }
962
963   if (NULL != tcp_redirects)
964     {
965       cpy = GNUNET_strdup (tcp_redirects);
966       for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
967         {
968           if (NULL == (hostname = strstr (redirect, ":")))
969             {
970               GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
971               continue;
972             }
973           hostname[0] = '\0';
974           local_port = atoi (redirect);
975           if (!((local_port > 0) && (local_port < 65536)))
976             GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
977
978           ret |= (0xFFFF & htons(local_port));
979           ret <<= 16;
980           count ++;
981
982           if(count > 4)
983             {
984               ret = 0;
985               goto out;
986             }
987         }
988       GNUNET_free(cpy);
989       cpy = NULL;
990     }
991
992 out:
993   if (NULL != cpy)
994     GNUNET_free(cpy);
995   return ret;
996 }
997
998 void
999 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1000               struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1001 {
1002   size_t size = sizeof (struct GNUNET_DNS_Record);
1003   struct GNUNET_DNS_Record data;
1004   memset (&data, 0, size);
1005
1006   data.purpose.size =
1007     htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1008   data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1009
1010   GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1011   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1012               *((unsigned long long *) &data.service_descriptor));
1013
1014   data.service_type = service_type;
1015   data.ports = ports;
1016
1017   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1018
1019   data.expiration_time =
1020     GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1021
1022   /* Sign the block */
1023   if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1024                                            &data.purpose, &data.signature))
1025     {
1026       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1027       return;
1028     }
1029
1030   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031               "Putting with key %08x, size = %d\n",
1032               *((unsigned int *) &data.service_descriptor), size);
1033
1034   GNUNET_DHT_put (dht,
1035                   &data.service_descriptor,
1036                   DEFAULT_PUT_REPLICATION,
1037                   GNUNET_DHT_RO_NONE,
1038                   GNUNET_BLOCK_TYPE_DNS,
1039                   size,
1040                   (char *) &data,
1041                   GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1042                   GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1043 }
1044
1045 /**
1046  * @brief Publishes the record defined by the section section
1047  *
1048  * @param cls closure
1049  * @param section the current section
1050  */
1051 void
1052 publish_iterate (void *cls __attribute__((unused)), const char *section)
1053 {
1054   if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
1055     return;
1056
1057   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1058
1059   char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1060     *keyfile;
1061
1062   GNUNET_CONFIGURATION_get_value_string (cfg, section,
1063                                          "UDP_REDIRECTS", &udp_redirects);
1064   GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1065                                          &tcp_redirects);
1066
1067   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1068                                                             "HOSTKEY",
1069                                                             &keyfile))
1070     {
1071       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1072       if (keyfile != NULL)
1073         GNUNET_free (keyfile);
1074       return;
1075     }
1076
1077   struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1078     GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1079   GNUNET_free (keyfile);
1080   GNUNET_assert (my_private_key != NULL);
1081
1082   uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1083   uint32_t service_type = 0;
1084
1085   if (NULL != udp_redirects)
1086     service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1087
1088   if (NULL != tcp_redirects)
1089     service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1090
1091   service_type = htonl (service_type);
1092
1093
1094   publish_name (section, ports, service_type, my_private_key);
1095
1096   GNUNET_CONFIGURATION_get_value_string (cfg, section,
1097                                          "ALTERNATIVE_NAMES",
1098                                          &alternative_names);
1099   for (alternative_name = strtok (alternative_names, " ");
1100        alternative_name != NULL; alternative_name = strtok (NULL, " "))
1101     {
1102       char *altname =
1103         alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1104       strcpy (altname, alternative_name);
1105       strcpy (altname + strlen (alternative_name) + 1, section);
1106       altname[strlen (alternative_name)] = '.';
1107
1108       publish_name (altname, ports, service_type, my_private_key);
1109     }
1110
1111   GNUNET_free_non_null(alternative_names);
1112   GNUNET_CRYPTO_rsa_key_free (my_private_key);
1113   GNUNET_free_non_null (udp_redirects);
1114   GNUNET_free_non_null (tcp_redirects);
1115 }
1116
1117 /**
1118  * Publish a DNS-record in the DHT.
1119  */
1120 static void
1121 publish_names (void *cls __attribute__((unused)),
1122                const struct GNUNET_SCHEDULER_TaskContext *tc) {
1123     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1124       return;
1125
1126     GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL);
1127
1128     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1129                                   publish_names,
1130                                   NULL);
1131 }
1132
1133 /**
1134  * @param cls closure
1135  * @param server the initialized server
1136  * @param cfg_ configuration to use
1137  */
1138 static void
1139 run (void *cls,
1140      struct GNUNET_SERVER_Handle *server,
1141      const struct GNUNET_CONFIGURATION_Handle *cfg_)
1142 {
1143   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1144     /* callback, cls, type, size */
1145     {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1146     {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1147      sizeof (struct GNUNET_MessageHeader)},
1148     {NULL, NULL, 0, 0}
1149   };
1150
1151   static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1152     {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1153     {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1154     {NULL, 0, 0}
1155   };
1156
1157   static GNUNET_MESH_ApplicationType *apptypes;
1158
1159   if (GNUNET_YES ==
1160       GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1161     apptypes = (GNUNET_MESH_ApplicationType[])
1162     {
1163     GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1164     GNUNET_APPLICATION_TYPE_END};
1165   else
1166   apptypes = (GNUNET_MESH_ApplicationType[])
1167   {
1168   GNUNET_APPLICATION_TYPE_END};
1169
1170   mesh_handle =
1171     GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1172
1173   cfg = cfg_;
1174
1175   unsigned int i;
1176   for (i = 0; i < 65536; i++)
1177     {
1178       query_states[i].valid = GNUNET_NO;
1179     }
1180
1181   dht = GNUNET_DHT_connect (cfg, 1024);
1182
1183   open_port ();
1184
1185   GNUNET_SCHEDULER_add_now (publish_names, NULL);
1186
1187   GNUNET_SERVER_add_handlers (server, handlers);
1188   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1189                                 &cleanup_task, cls);
1190 }
1191
1192 /**
1193  * The main function for the dns service.
1194  *
1195  * @param argc number of arguments from the command line
1196  * @param argv command line arguments
1197  * @return 0 ok, 1 on error
1198  */
1199 int
1200 main (int argc, char *const *argv)
1201 {
1202   return (GNUNET_OK ==
1203           GNUNET_SERVICE_run (argc,
1204                               argv,
1205                               "dns",
1206                               GNUNET_SERVICE_OPTION_NONE,
1207                               &run, NULL)) ? 0 : 1;
1208 }