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