Answer an icmp-request
[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-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.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_signatures.h"
41
42 /**
43  * The scheduler to use throughout the service
44  */
45 static struct GNUNET_SCHEDULER_Handle *sched;
46
47 /**
48  * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
49  * sent through gnunet. The port of this socket will not be hijacked.
50  */
51 static struct GNUNET_NETWORK_Handle *dnsout;
52
53 /**
54  * The port bound to the socket dnsout
55  */
56 static unsigned short dnsoutport;
57
58 /**
59  * A handle to the DHT-Service
60  */
61 static struct GNUNET_DHT_Handle *dht;
62
63 /**
64  * The configuration to use
65  */
66 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67
68 /**
69  * A list of DNS-Responses that have to be sent to the requesting client
70  */
71 static struct answer_packet_list *head;
72
73 /**
74  * The tail of the list of DNS-responses
75  */
76 static struct answer_packet_list *tail;
77
78 /**
79  * A structure containing a mapping from network-byte-ordered DNS-id to
80  * some information needed to handle this query
81  *
82  * It currently allocates at least
83  * (1 + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
84  * = 1.7 MiB on 64 bit.
85  * = 1.2 MiB on 32 bit.
86  */
87 static struct {
88     unsigned valid:1;
89     struct GNUNET_SERVER_Client* client;
90     unsigned local_ip:32;
91     unsigned remote_ip:32;
92     unsigned local_port:16;
93     char* name;
94     unsigned namelen:8;
95 } query_states[65536];
96
97 /**
98  * A struct used to give more than one value as
99  * closure to receive_dht
100  */
101 struct receive_dht_cls {
102     unsigned short id;
103     struct GNUNET_DHT_GetHandle* handle;
104 };
105
106 /**
107  * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
108  */
109 static void
110 hijack(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
111     char port_s[6];
112
113     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
114     snprintf(port_s, 6, "%d", dnsoutport);
115     GNUNET_OS_process_close (GNUNET_OS_start_process(NULL,
116                                                      NULL,
117                                                      "gnunet-helper-hijack-dns",
118                                                      "gnunet-hijack-dns",
119                                                      port_s,
120                                                      NULL));
121 }
122
123 /**
124  * Delete the hijacking-routes
125  */
126 static void
127 unhijack(unsigned short port) {
128     char port_s[6];
129
130     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
131     snprintf(port_s, 6, "%d", port);
132     GNUNET_OS_start_process(NULL,
133                             NULL,
134                             "gnunet-helper-hijack-dns",
135                             "gnunet-hijack-dns",
136                             "-d",
137                             port_s,
138                             NULL);
139 }
140
141 /**
142  * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
143  * system.
144  */
145 static size_t
146 send_answer(void* cls, size_t size, void* buf) {
147     struct answer_packet_list* query = head;
148     size_t len = ntohs(query->pkt.hdr.size);
149
150     GNUNET_assert(len <= size);
151
152     memcpy(buf, &query->pkt.hdr, len);
153
154     GNUNET_CONTAINER_DLL_remove (head, tail, query);
155
156     GNUNET_free(query);
157
158     /* When more data is to be sent, reschedule */
159     if (head != NULL)
160       GNUNET_SERVER_notify_transmit_ready(cls,
161                                           ntohs(head->pkt.hdr.size),
162                                           GNUNET_TIME_UNIT_FOREVER_REL,
163                                           &send_answer,
164                                           cls);
165
166     return len;
167 }
168
169 static void
170 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
171     struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
172
173     unsigned short id = pdns->s.id;
174
175     if (query_states[id].valid != GNUNET_YES) return;
176     query_states[id].valid = GNUNET_NO;
177
178     GNUNET_assert(query_states[id].namelen == 74);
179
180     size_t len = sizeof(struct answer_packet) - 1 \
181                  + sizeof(struct dns_static) \
182                  + 74 /* this is the length of a reverse ipv6-lookup */ \
183                  + sizeof(struct dns_query_line) \
184                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
185                  + sizeof(struct dns_record_line) - 1 \
186                  - 2 /* We do not know the lenght of the answer yet*/ \
187                  - 2 /* No idea why... */ ;
188
189     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
190     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
191
192     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
193     answer->pkt.hdr.size = htons(len);
194     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
195
196     answer->pkt.from = query_states[id].remote_ip;
197
198     answer->pkt.to = query_states[id].local_ip;
199     answer->pkt.dst_port = query_states[id].local_port;
200
201     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
202
203     dpkt->s.id = id;
204     dpkt->s.aa = 1;
205     dpkt->s.qr = 1;
206     dpkt->s.ra = 1;
207     dpkt->s.qdcount = htons(1);
208     dpkt->s.ancount = htons(1);
209
210     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
211     GNUNET_free(query_states[id].name);
212
213     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
214     dque->type = htons(12); /* PTR */
215     dque->class = htons(1); /* IN */
216
217     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
218     memcpy(anname, (char[]){0xc0, 0x0c}, 2);
219
220     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
221     drec_data->type = htons(12); /* AAAA */
222     drec_data->class = htons(1); /* IN */
223     drec_data->ttl = htonl(3600); /* FIXME: read from block */
224
225     /* Calculate at which offset in the packet the length of the name and the
226      * name, it is filled in by the daemon-vpn */
227     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
228
229     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
230
231     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
232                                         len,
233                                         GNUNET_TIME_UNIT_FOREVER_REL,
234                                         &send_answer,
235                                         query_states[id].client);
236
237     /*
238      * build
239      * complete dns-packet with empty name in the answer
240      * provide offsett of the name
241      */
242 }
243
244 /**
245  * Receive a block from the dht.
246  */
247 static void
248 receive_dht(void *cls,
249             struct GNUNET_TIME_Absolute exp,
250             const GNUNET_HashCode *key,
251             const struct GNUNET_PeerIdentity *const *get_path,
252             const struct GNUNET_PeerIdentity *const *put_path,
253             enum GNUNET_BLOCK_Type type,
254             size_t size,
255             const void *data) {
256
257     unsigned short id = ((struct receive_dht_cls*)cls)->id;
258     struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
259     GNUNET_free(cls);
260
261     GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
262
263     /* If no query with this id is pending, ignore the block */
264     if (query_states[id].valid != GNUNET_YES) return;
265     query_states[id].valid = GNUNET_NO;
266
267     const struct GNUNET_DNS_Record* rec = data;
268     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
269                "Got block of size %d, peer: %08x, desc: %08x\n",
270                size,
271                *((unsigned int*)&rec->peer),
272                *((unsigned int*)&rec->service_descriptor));
273
274     size_t len = sizeof(struct answer_packet) - 1 \
275                  + sizeof(struct dns_static) \
276                  + query_states[id].namelen \
277                  + sizeof(struct dns_query_line) \
278                  + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
279                  + sizeof(struct dns_record_line) - 1 \
280                  + 16; /* To hold the IPv6-Address */
281
282     struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
283     memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
284
285     answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
286     answer->pkt.hdr.size = htons(len);
287     answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
288
289     GNUNET_CRYPTO_hash(&rec->peer,
290                        sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
291                        &answer->pkt.peer);
292
293     memcpy(&answer->pkt.service_descriptor,
294            &rec->service_descriptor,
295            sizeof(GNUNET_HashCode));
296     memcpy(&answer->pkt.service_type,
297            &rec->service_type,
298            sizeof(answer->pkt.service_type));
299     memcpy(&answer->pkt.ports, &rec->ports, sizeof(answer->pkt.ports));
300
301     answer->pkt.from = query_states[id].remote_ip;
302
303     answer->pkt.to = query_states[id].local_ip;
304     answer->pkt.dst_port = query_states[id].local_port;
305
306     struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
307
308     dpkt->s.id = id;
309     dpkt->s.aa = 1;
310     dpkt->s.qr = 1;
311     dpkt->s.ra = 1;
312     dpkt->s.qdcount = htons(1);
313     dpkt->s.ancount = htons(1);
314
315     memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
316     GNUNET_free(query_states[id].name);
317
318     struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
319     dque->type = htons(28); /* AAAA */
320     dque->class = htons(1); /* IN */
321
322     char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
323     memcpy(anname, (char[]){0xc0, 0x0c}, 2);
324
325     struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
326     drec_data->type = htons(28); /* AAAA */
327     drec_data->class = htons(1); /* IN */
328     drec_data->ttl = htonl(3600); /* FIXME: read from block */
329     drec_data->data_len = htons(16);
330
331     /* Calculate at which offset in the packet the IPv6-Address belongs, it is
332      * filled in by the daemon-vpn */
333     answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
334
335     GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
336
337     GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
338                                         len,
339                                         GNUNET_TIME_UNIT_FOREVER_REL,
340                                         &send_answer,
341                                         query_states[id].client);
342
343     GNUNET_DHT_get_stop(handle);
344 }
345
346 /**
347  * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
348  */
349 static void
350 rehijack(void *cls,
351          struct GNUNET_SERVER_Client *client,
352          const struct GNUNET_MessageHeader *message) {
353     unhijack(dnsoutport);
354     GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
355
356     GNUNET_SERVER_receive_done(client, GNUNET_OK);
357 }
358
359 /**
360  * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
361  */
362 static void
363 receive_query(void *cls,
364               struct GNUNET_SERVER_Client *client,
365               const struct GNUNET_MessageHeader *message) {
366     struct query_packet* pkt = (struct query_packet*)message;
367     struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
368     struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
369
370     query_states[dns->s.id].valid = GNUNET_YES;
371     query_states[dns->s.id].client = client;
372     query_states[dns->s.id].local_ip = pkt->orig_from;
373     query_states[dns->s.id].local_port = pkt->src_port;
374     query_states[dns->s.id].remote_ip = pkt->orig_to;
375     query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
376     query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
377     memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
378
379     /* The query is for a .gnunet-address */
380     if (pdns->queries[0]->namelen > 9 &&
381         0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
382       {
383         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
384         GNUNET_HashCode key;
385         GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
386
387         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
388                    "Getting with key %08x, len is %d\n",
389                    *((unsigned int*)&key),
390                    pdns->queries[0]->namelen);
391
392         struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
393         cls->id = dns->s.id;
394
395         cls->handle = GNUNET_DHT_get_start(dht,
396                                            GNUNET_TIME_UNIT_MINUTES,
397                                            GNUNET_BLOCK_TYPE_DNS,
398                                            &key,
399                                            GNUNET_DHT_RO_NONE,
400                                            NULL,
401                                            0,
402                                            NULL,
403                                            0,
404                                            receive_dht,
405                                            cls);
406
407         goto outfree;
408       }
409
410     /* The query is for a PTR of a previosly resolved virtual IP */
411     if (htons(pdns->queries[0]->qtype) == 12 &&
412         pdns->queries[0]->namelen > 19 &&
413         0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 19), ".4.3.2.1.ip6.arpa.", 19))
414       {
415         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
416
417         GNUNET_SCHEDULER_add_now(sched, send_rev_query, pdns);
418
419         goto out;
420       }
421
422     /* The query should be sent to the network */
423
424     struct sockaddr_in dest;
425     memset(&dest, 0, sizeof dest);
426     dest.sin_port = htons(53);
427     dest.sin_addr.s_addr = pkt->orig_to;
428
429     GNUNET_NETWORK_socket_sendto(dnsout,
430                                  dns,
431                                  ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
432                                  (struct sockaddr*) &dest,
433                                  sizeof dest);
434
435 outfree:
436     free_parsed_dns_packet(pdns);
437     pdns = NULL;
438 out:
439     GNUNET_SERVER_receive_done(client, GNUNET_OK);
440 }
441
442 /**
443  * Read a response-packet of the UDP-Socket
444  */
445 static void
446 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
447     unsigned char buf[65536];
448     struct dns_pkt* dns = (struct dns_pkt*)buf;
449
450     if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
451       return;
452
453     struct sockaddr_in addr;
454     memset(&addr, 0, sizeof addr);
455     unsigned int addrlen = sizeof addr;
456
457     int r;
458     r = GNUNET_NETWORK_socket_recvfrom(dnsout,
459                                        buf,
460                                        65536,
461                                        (struct sockaddr*)&addr,
462                                        &addrlen);
463
464     /* if (r < 0) FIXME */
465
466     if (query_states[dns->s.id].valid == GNUNET_YES) {
467         query_states[dns->s.id].valid = GNUNET_NO;
468
469         size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
470         struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
471         answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
472         answer->pkt.hdr.size = htons(len);
473         answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
474         answer->pkt.from = addr.sin_addr.s_addr;
475         answer->pkt.to = query_states[dns->s.id].local_ip;
476         answer->pkt.dst_port = query_states[dns->s.id].local_port;
477         memcpy(answer->pkt.data, buf, r);
478
479         GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
480
481         GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client,
482                                             len,
483                                             GNUNET_TIME_UNIT_FOREVER_REL,
484                                             &send_answer,
485                                             query_states[dns->s.id].client);
486     }
487
488     GNUNET_SCHEDULER_add_read_net(sched,
489                                   GNUNET_TIME_UNIT_FOREVER_REL,
490                                   dnsout,
491                                   &read_response,
492                                   NULL);
493 }
494
495
496 /**
497  * Task run during shutdown.
498  *
499  * @param cls unused
500  * @param tc unused
501  */
502 static void
503 cleanup_task (void *cls,
504               const struct GNUNET_SCHEDULER_TaskContext *tc)
505 {
506   unhijack(dnsoutport);
507   GNUNET_DHT_disconnect(dht);
508 }
509
510 /**
511  * Publish a DNS-record in the DHT. This is up to now just for testing.
512  */
513 static void
514 publish_name (void *cls,
515               const struct GNUNET_SCHEDULER_TaskContext *tc) {
516     if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
517       return;
518
519     char* name = "philipptoelke.gnunet.";
520     size_t size = sizeof(struct GNUNET_DNS_Record);
521     struct GNUNET_DNS_Record data;
522     memset(&data, 0, size);
523
524     data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
525     data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
526
527     GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
528
529     char* keyfile;
530     if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, "GNUNETD",
531                                                              "HOSTKEY", &keyfile))
532       {
533         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
534         if (keyfile != NULL) GNUNET_free(keyfile);
535         return;
536       }
537
538     struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
539     GNUNET_free(keyfile);
540
541     GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
542
543     data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
544
545   /* Sign the block */
546     if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
547                                             &data.purpose,
548                                             &data.signature))
549       {
550         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
551         return;
552       }
553     GNUNET_CRYPTO_rsa_key_free(my_private_key);
554
555     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
556                "Putting with key %08x\n",
557                *((unsigned int*)&data.service_descriptor));
558
559     GNUNET_DHT_put(dht,
560                    &data.service_descriptor,
561                    GNUNET_DHT_RO_NONE,
562                    GNUNET_BLOCK_TYPE_DNS,
563                    size,
564                    (char*)&data,
565                    GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
566                    GNUNET_TIME_UNIT_MINUTES,
567                    NULL,
568                    NULL);
569
570     GNUNET_SCHEDULER_add_delayed (sched,
571                                   GNUNET_TIME_UNIT_HOURS,
572                                   publish_name,
573                                   NULL);
574 }
575
576 /**
577  * @param cls closure
578  * @param sched scheduler to use
579  * @param server the initialized server
580  * @param cfg configuration to use
581  */
582 static void
583 run (void *cls,
584      struct GNUNET_SCHEDULER_Handle *sched_,
585      struct GNUNET_SERVER_Handle *server,
586      const struct GNUNET_CONFIGURATION_Handle *cfg_)
587 {
588   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
589       /* callback, cls, type, size */
590         {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
591         {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
592         {NULL, NULL, 0, 0}
593   };
594
595   cfg = cfg_;
596   sched = sched_;
597
598   unsigned int i;
599   for (i = 0; i < 65536; i++) {
600       query_states[i].valid = GNUNET_NO;
601   }
602
603   dht = GNUNET_DHT_connect(sched, cfg, 1024);
604
605   struct sockaddr_in addr;
606
607   dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
608   if (dnsout == NULL)
609     return;
610   memset(&addr, 0, sizeof(struct sockaddr_in));
611
612   int err = GNUNET_NETWORK_socket_bind (dnsout,
613                                         (struct sockaddr*)&addr,
614                                         sizeof(struct sockaddr_in));
615
616   if (err != GNUNET_YES) {
617       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
618       return;
619   }
620
621   /* Read the port we bound to */
622   socklen_t addrlen = sizeof(struct sockaddr_in);
623   err = getsockname(GNUNET_NETWORK_get_fd(dnsout),
624                     (struct sockaddr*) &addr,
625                     &addrlen);
626
627   dnsoutport = htons(addr.sin_port);
628
629   GNUNET_SCHEDULER_add_now (sched, publish_name, NULL);
630
631   GNUNET_SCHEDULER_add_read_net(sched, GNUNET_TIME_UNIT_FOREVER_REL, dnsout, &read_response, NULL);
632
633   GNUNET_SERVER_add_handlers (server, handlers);
634   GNUNET_SCHEDULER_add_delayed (sched,
635                                 GNUNET_TIME_UNIT_FOREVER_REL,
636                                 &cleanup_task,
637                                 cls);
638 }
639
640 /**
641  * The main function for the dns service.
642  *
643  * @param argc number of arguments from the command line
644  * @param argv command line arguments
645  * @return 0 ok, 1 on error
646  */
647 int
648 main (int argc, char *const *argv)
649 {
650   return (GNUNET_OK ==
651           GNUNET_SERVICE_run (argc,
652                               argv,
653                               "dns",
654                               GNUNET_SERVICE_OPTION_NONE,
655                               &run, NULL)) ? 0 : 1;
656 }