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