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