2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file vpn/gnunet-service-dns.c
23 * @author Philipp Toelke
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"
44 struct GNUNET_MESH_Handle *mesh_handle;
46 struct GNUNET_CONNECTION_TransmitHandle *server_notify;
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.
52 static struct GNUNET_NETWORK_Handle *dnsout;
55 * The port bound to the socket dnsout
57 static unsigned short dnsoutport;
60 * A handle to the DHT-Service
62 static struct GNUNET_DHT_Handle *dht;
65 * The configuration to use
67 static const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * A list of DNS-Responses that have to be sent to the requesting client
72 static struct answer_packet_list *head;
75 * The tail of the list of DNS-responses
77 static struct answer_packet_list *tail;
80 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
81 * some information needed to handle this query
83 * It currently allocates at least
84 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
91 struct GNUNET_SERVER_Client *client;
92 struct GNUNET_MESH_Tunnel *tunnel;
98 } query_states[UINT16_MAX+1];
101 * A struct used to give more than one value as
102 * closure to receive_dht
104 struct receive_dht_cls
107 struct GNUNET_DHT_GetHandle *handle;
110 struct tunnel_notify_queue
112 struct tunnel_notify_queue *next;
113 struct tunnel_notify_queue *prev;
116 GNUNET_CONNECTION_TransmitReadyNotify cb;
120 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
124 __attribute__ ((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
126 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
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);
139 struct GNUNET_OS_Process *proc;
142 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 "No entry 'VIRTDNS' in configuration!\n");
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
150 snprintf (port_s, 6, "%d", dnsoutport);
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);
160 * Delete the hijacking-routes
163 unhijack (unsigned short port)
167 struct GNUNET_OS_Process *proc;
170 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "No entry 'VIRTDNS' in configuration!\n");
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
178 snprintf (port_s, 6, "%d", port);
181 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
182 "gnunet-hijack-dns", "-d", port_s, virt_dns,
185 GNUNET_OS_process_wait (proc);
186 GNUNET_OS_process_close (proc);
188 GNUNET_free (virt_dns);
192 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
196 send_answer (void *cls, size_t size, void *buf)
198 server_notify = NULL;
199 struct answer_packet_list *query = head;
200 size_t len = ntohs (query->pkt.hdr.size);
202 GNUNET_assert (len <= size);
204 memcpy (buf, &query->pkt.hdr, len);
206 GNUNET_CONTAINER_DLL_remove (head, tail, query);
210 /* When more data is to be sent, reschedule */
212 server_notify = GNUNET_SERVER_notify_transmit_ready (cls, ntohs (head->pkt.hdr.size),
213 GNUNET_TIME_UNIT_FOREVER_REL,
221 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
222 struct GNUNET_MessageHeader hdr;
226 struct tunnel_cls *remote_pending[UINT16_MAX];
229 mesh_send_response (void *cls, size_t size, void *buf)
231 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
232 struct GNUNET_MessageHeader *hdr = buf;
234 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
235 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
237 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
239 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
240 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
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));
246 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
248 memcpy (hdr + 1, dns, *sz);
250 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
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);
256 GNUNET_CONTAINER_DLL_remove (head, tail, element);
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,
264 GNUNET_TIME_relative_divide
265 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
266 (const struct GNUNET_PeerIdentity *)
268 element->cb, element->cls);
270 /* save the handle */
271 GNUNET_MESH_tunnel_set_data (*tunnel, th);
276 return ntohs (hdr->size);
280 mesh_send (void *cls, size_t size, void *buf)
282 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
284 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
286 GNUNET_assert (cls_->hdr.size <= size);
288 size = cls_->hdr.size;
289 cls_->hdr.size = htons (cls_->hdr.size);
291 memcpy (buf, &cls_->hdr, size);
293 if (NULL != GNUNET_MESH_tunnel_get_head (cls_->tunnel))
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);
302 GNUNET_CONTAINER_DLL_remove (head, tail, element);
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,
310 GNUNET_TIME_relative_divide
311 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
312 (const struct GNUNET_PeerIdentity *)
314 element->cb, element->cls);
316 /* save the handle */
317 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
318 GNUNET_free (element);
326 mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
327 const struct GNUNET_TRANSPORT_ATS_Information *atsi
328 __attribute__ ((unused)))
332 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
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));
338 if (NULL == GNUNET_MESH_tunnel_get_data (cls_->tunnel))
340 struct GNUNET_MESH_TransmitHandle *th =
341 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
344 GNUNET_TIME_UNIT_MINUTES,
350 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
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);
359 struct tunnel_notify_queue *element =
360 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
362 element->len = cls_->hdr.size;
363 element->cb = mesh_send;
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);
373 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
378 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
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_);
386 remote_pending[cls_->dns.s.id] = cls_;
390 receive_mesh_query (void *cls
391 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
393 __attribute__ ((unused)),
394 const struct GNUNET_PeerIdentity *sender
395 __attribute__ ((unused)),
396 const struct GNUNET_MessageHeader *message,
397 const struct GNUNET_TRANSPORT_ATS_Information *atsi
398 __attribute__ ((unused)))
400 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
402 struct sockaddr_in dest;
404 memset (&dest, 0, sizeof dest);
405 dest.sin_port = htons (53);
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);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
413 query_states[dns->s.id].tunnel = tunnel;
414 query_states[dns->s.id].valid = GNUNET_YES;
416 GNUNET_NETWORK_socket_sendto (dnsout, dns,
417 ntohs (message->size) -
418 sizeof (struct GNUNET_MessageHeader),
419 (struct sockaddr *) &dest, sizeof dest);
421 return GNUNET_SYSERR;
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_TRANSPORT_ATS_Information *atsi
432 __attribute__ ((unused)))
434 /* TODo: size check */
435 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
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)
442 GNUNET_free (remote_pending[dns->s.id]);
443 remote_pending[dns->s.id] = NULL;
445 if (query_states[dns->s.id].valid != GNUNET_YES)
446 return GNUNET_SYSERR;
447 query_states[dns->s.id].valid = GNUNET_NO;
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
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 */
456 struct answer_packet_list *answer =
457 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
459 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
460 answer->pkt.hdr.size = htons (len);
462 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
464 if (ntohs (pdns->s.ancount) < 1)
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);
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));
479 answer->pkt.from = query_states[dns->s.id].remote_ip;
481 answer->pkt.to = query_states[dns->s.id].local_ip;
482 answer->pkt.dst_port = query_states[dns->s.id].local_port;
484 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
486 dpkt->s.id = dns->s.id;
490 dpkt->s.qdcount = htons (1);
491 dpkt->s.ancount = htons (1);
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;
498 struct dns_query_line *dque =
499 (struct dns_query_line *) (dpkt->data +
500 (query_states[dns->s.id].namelen));
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)
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);
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);
520 dque->class = htons (1); /* IN */
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 */
528 drec_data->ttl = pdns->answers[0]->ttl;
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)));
536 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
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,
542 query_states[dns->s.id].client);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Sent answer of length %d on to client, addroffset = %d\n", len,
546 answer->pkt.addroffset);
548 free_parsed_dns_packet (pdns);
554 send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
556 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
559 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
561 unsigned short id = pdns->s.id;
563 free_parsed_dns_packet (pdns);
565 if (query_states[id].valid != GNUNET_YES)
567 query_states[id].valid = GNUNET_NO;
569 GNUNET_assert (query_states[id].namelen == 74);
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 */ ;
576 struct answer_packet_list *answer =
577 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
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;
583 answer->pkt.from = query_states[id].remote_ip;
585 answer->pkt.to = query_states[id].local_ip;
586 answer->pkt.dst_port = query_states[id].local_port;
588 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
594 dpkt->s.qdcount = htons (1);
595 dpkt->s.ancount = htons (1);
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;
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 */
607 (char *) (dpkt->data + (query_states[id].namelen) +
608 sizeof (struct dns_query_line));
609 memcpy (anname, "\xc0\x0c", 2);
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)
619 * But how to get the seconds out of this?
621 drec_data->ttl = htonl (3600);
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)));
629 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
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);
638 * Receive a block from the dht.
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 *const *get_path
645 __attribute__ ((unused)),
646 const struct GNUNET_PeerIdentity *const *put_path
647 __attribute__ ((unused)), enum GNUNET_BLOCK_Type type, size_t size,
651 unsigned short id = ((struct receive_dht_cls *) cls)->id;
652 struct GNUNET_DHT_GetHandle *handle =
653 ((struct receive_dht_cls *) cls)->handle;
656 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
658 /* If no query with this id is pending, ignore the block */
659 if (query_states[id].valid != GNUNET_YES)
661 query_states[id].valid = GNUNET_NO;
663 const struct GNUNET_DNS_Record *rec = data;
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Got block of size %d, peer: %08x, desc: %08x\n", size,
667 *((unsigned int *) &rec->peer),
668 *((unsigned int *) &rec->service_descriptor));
670 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 */
671 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
673 struct answer_packet_list *answer =
674 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
676 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
677 answer->pkt.hdr.size = htons (len);
678 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
680 GNUNET_CRYPTO_hash (&rec->peer,
681 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
682 &answer->pkt.service_descr.peer);
684 memcpy (&answer->pkt.service_descr.service_descriptor,
685 &rec->service_descriptor, sizeof (GNUNET_HashCode));
686 memcpy (&answer->pkt.service_descr.service_type, &rec->service_type,
687 sizeof (answer->pkt.service_descr.service_type));
688 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
689 sizeof (answer->pkt.service_descr.ports));
691 answer->pkt.from = query_states[id].remote_ip;
693 answer->pkt.to = query_states[id].local_ip;
694 answer->pkt.dst_port = query_states[id].local_port;
696 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
702 dpkt->s.qdcount = htons (1);
703 dpkt->s.ancount = htons (1);
705 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
706 GNUNET_free (query_states[id].name);
707 query_states[id].name = NULL;
709 struct dns_query_line *dque =
710 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
711 dque->type = htons (28); /* AAAA */
712 dque->class = htons (1); /* IN */
715 (char *) (dpkt->data + (query_states[id].namelen) +
716 sizeof (struct dns_query_line));
717 memcpy (anname, "\xc0\x0c", 2);
719 struct dns_record_line *drec_data =
720 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
721 sizeof (struct dns_query_line) + 2);
722 drec_data->type = htons (28); /* AAAA */
723 drec_data->class = htons (1); /* IN */
725 /* FIXME: read the TTL from block:
726 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
728 * But how to get the seconds out of this?
730 drec_data->ttl = htonl (3600);
731 drec_data->data_len = htons (16);
733 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
734 * filled in by the daemon-vpn */
735 answer->pkt.addroffset =
736 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
737 (unsigned long) (&answer->pkt)));
739 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
741 if (server_notify == NULL)
742 server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
743 GNUNET_TIME_UNIT_FOREVER_REL,
744 &send_answer, query_states[id].client);
746 GNUNET_DHT_get_stop (handle);
750 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
754 __attribute__ ((unused)), struct GNUNET_SERVER_Client *client,
755 const struct GNUNET_MessageHeader *message __attribute__ ((unused)))
757 unhijack (dnsoutport);
758 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
764 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
767 receive_query (void *cls
768 __attribute__ ((unused)), struct GNUNET_SERVER_Client *client,
769 const struct GNUNET_MessageHeader *message)
771 struct query_packet *pkt = (struct query_packet *) message;
772 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
773 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
775 query_states[dns->s.id].valid = GNUNET_YES;
776 query_states[dns->s.id].client = client;
777 query_states[dns->s.id].local_ip = pkt->orig_from;
778 query_states[dns->s.id].local_port = pkt->src_port;
779 query_states[dns->s.id].remote_ip = pkt->orig_to;
780 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
781 if (query_states[dns->s.id].name != NULL)
782 GNUNET_free (query_states[dns->s.id].name);
783 query_states[dns->s.id].name =
784 GNUNET_malloc (query_states[dns->s.id].namelen);
785 memcpy (query_states[dns->s.id].name, dns->data,
786 query_states[dns->s.id].namelen);
788 /* The query is for a .gnunet-address */
789 if (pdns->queries[0]->namelen > 9 &&
790 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
796 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n",
800 *((unsigned int *) &key), pdns->queries[0]->namelen);
802 struct receive_dht_cls *cls =
803 GNUNET_malloc (sizeof (struct receive_dht_cls));
807 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
808 GNUNET_BLOCK_TYPE_DNS, &key,
809 DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
810 0, NULL, 0, receive_dht, cls);
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
816 pdns->queries[0]->name, pdns->queries[0]->namelen);
818 /* This is a PTR-Query. Check if it is for "our" network */
819 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
824 "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 long long ipv6prefix;
827 unsigned int comparelen;
829 GNUNET_assert (GNUNET_OK ==
830 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
833 inet_pton (AF_INET6, ipv6addr, ipv6);
834 GNUNET_free (ipv6addr);
836 GNUNET_assert (GNUNET_OK ==
837 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
840 GNUNET_assert (ipv6prefix < 127);
841 ipv6prefix = (ipv6prefix + 7) / 8;
843 for (i = ipv6prefix; i < 16; i++)
846 for (i = 0; i < 16; i++)
848 unsigned char c1 = ipv6[i] >> 4;
849 unsigned char c2 = ipv6[i] & 0xf;
852 ipv6rev[62 - (4 * i)] = c1 + '0';
854 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
857 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
859 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
862 comparelen = 10 + 4 * ipv6prefix;
864 strncmp (pdns->queries[0]->name +
865 (pdns->queries[0]->namelen - comparelen),
866 ipv6rev + (74 - comparelen), comparelen))
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
870 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
877 unsigned int virt_dns_bytes;
880 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
883 "No entry 'VIRTDNS' in configuration!\n");
887 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
894 GNUNET_free (virt_dns);
896 if (virt_dns_bytes == pkt->orig_to)
898 /* This is a packet that was sent directly to the virtual dns-server
900 * This means we have to send this query over gnunet
904 sizeof (struct GNUNET_MESH_Tunnel *) +
905 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
906 sizeof (struct query_packet) +
908 struct tunnel_cls *cls_ = GNUNET_malloc (size);
910 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
912 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
915 memcpy (&cls_->dns, dns,
916 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
917 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
923 /* The query should be sent to the network */
925 struct sockaddr_in dest;
927 memset (&dest, 0, sizeof dest);
928 dest.sin_port = htons (53);
929 dest.sin_addr.s_addr = pkt->orig_to;
931 GNUNET_NETWORK_socket_sendto (dnsout, dns,
932 ntohs (pkt->hdr.size) -
933 sizeof (struct query_packet) + 1,
934 (struct sockaddr *) &dest, sizeof dest);
937 free_parsed_dns_packet (pdns);
940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
944 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
949 struct sockaddr_in addr;
951 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
953 return GNUNET_SYSERR;
954 memset (&addr, 0, sizeof (struct sockaddr_in));
956 addr.sin_family = AF_INET;
957 int err = GNUNET_NETWORK_socket_bind (dnsout,
958 (struct sockaddr *) &addr,
959 sizeof (struct sockaddr_in));
961 if (err != GNUNET_OK)
963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
964 return GNUNET_SYSERR;
967 /* Read the port we bound to */
968 socklen_t addrlen = sizeof (struct sockaddr_in);
971 getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr,
974 dnsoutport = htons (addr.sin_port);
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
978 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
979 &read_response, NULL);
985 * Read a response-packet of the UDP-Socket
988 read_response (void *cls
989 __attribute__ ((unused)),
990 const struct GNUNET_SCHEDULER_TaskContext *tc)
992 struct sockaddr_in addr;
993 socklen_t addrlen = sizeof (addr);
997 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1000 memset (&addr, 0, sizeof addr);
1003 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
1005 unhijack (dnsoutport);
1006 if (GNUNET_YES == open_port ())
1007 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
1011 /* port the code above? */
1015 unsigned char buf[len];
1016 struct dns_pkt *dns = (struct dns_pkt *) buf;
1018 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1019 (struct sockaddr *) &addr, &addrlen);
1023 unhijack (dnsoutport);
1024 if (GNUNET_YES == open_port ())
1025 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1032 if (query_states[dns->s.id].valid == GNUNET_YES)
1034 if (query_states[dns->s.id].tunnel != NULL)
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 "Answer to query %d for a remote peer!\n",
1039 /* This response should go through a tunnel */
1041 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1043 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1045 *t = query_states[dns->s.id].tunnel;
1046 memcpy (t + 1, dns, r);
1048 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel))
1050 struct GNUNET_MESH_TransmitHandle *th =
1051 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1054 GNUNET_TIME_UNIT_MINUTES,
1058 GNUNET_MessageHeader),
1059 mesh_send_response, c);
1061 GNUNET_MESH_tunnel_set_data (query_states[dns->s.id].tunnel, th);
1065 struct tunnel_notify_queue *head =
1066 GNUNET_MESH_tunnel_get_head (query_states[dns->s.id].tunnel);
1067 struct tunnel_notify_queue *tail =
1068 GNUNET_MESH_tunnel_get_tail (query_states[dns->s.id].tunnel);
1070 struct tunnel_notify_queue *element =
1071 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1073 element->len = r + sizeof (struct GNUNET_MessageHeader);
1074 element->cb = mesh_send_response;
1076 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
1077 GNUNET_MESH_tunnel_set_head (query_states[dns->s.id].tunnel, head);
1078 GNUNET_MESH_tunnel_set_tail (query_states[dns->s.id].tunnel, tail);
1083 query_states[dns->s.id].valid = GNUNET_NO;
1085 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1086 struct answer_packet_list *answer =
1087 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
1088 answer->pkt.hdr.type =
1089 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1090 answer->pkt.hdr.size = htons (len);
1091 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1092 answer->pkt.from = addr.sin_addr.s_addr;
1093 answer->pkt.to = query_states[dns->s.id].local_ip;
1094 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1095 memcpy (answer->pkt.data, buf, r);
1097 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1099 if (server_notify == NULL)
1100 server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1101 len, GNUNET_TIME_UNIT_FOREVER_REL,
1103 query_states[dns->s.id].client);
1107 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1108 &read_response, NULL);
1113 * Task run during shutdown.
1119 cleanup_task (void *cls
1120 __attribute__ ((unused)),
1121 const struct GNUNET_SCHEDULER_TaskContext *tc)
1123 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1125 unhijack (dnsoutport);
1126 GNUNET_DHT_disconnect (dht);
1127 GNUNET_MESH_disconnect (mesh_handle);
1131 * @brief Create a port-map from udp and tcp redirects
1133 * @param udp_redirects
1134 * @param tcp_redirects
1139 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1142 char *cpy, *hostname, *redirect;
1144 unsigned int count = 0;
1147 if (NULL != udp_redirects)
1149 cpy = GNUNET_strdup (udp_redirects);
1150 for (redirect = strtok (cpy, " "); redirect != NULL;
1151 redirect = strtok (NULL, " "))
1153 if (NULL == (hostname = strstr (redirect, ":")))
1155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1156 "Warning: option %s is not formatted correctly!\n",
1161 local_port = atoi (redirect);
1162 if (!((local_port > 0) && (local_port < 65536)))
1163 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1164 "Warning: %s is not a correct port.", redirect);
1166 ret |= (0xFFFF & htons (local_port));
1180 if (NULL != tcp_redirects)
1182 cpy = GNUNET_strdup (tcp_redirects);
1183 for (redirect = strtok (cpy, " "); redirect != NULL;
1184 redirect = strtok (NULL, " "))
1186 if (NULL == (hostname = strstr (redirect, ":")))
1188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1189 "Warning: option %s is not formatted correctly!\n",
1194 local_port = atoi (redirect);
1195 if (!((local_port > 0) && (local_port < 65536)))
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 "Warning: %s is not a correct port.", redirect);
1199 ret |= (0xFFFF & htons (local_port));
1214 GNUNET_free_non_null (cpy);
1219 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1220 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1222 size_t size = sizeof (struct GNUNET_DNS_Record);
1223 struct GNUNET_DNS_Record data;
1225 memset (&data, 0, size);
1227 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1228 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1230 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1232 *((unsigned long long *) &data.service_descriptor));
1234 data.service_type = service_type;
1237 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1239 data.expiration_time =
1240 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
1241 (GNUNET_TIME_UNIT_HOURS, 2));
1243 /* Sign the block */
1245 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
1247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1252 *((unsigned int *) &data.service_descriptor), size);
1254 GNUNET_DHT_put (dht, &data.service_descriptor, DEFAULT_PUT_REPLICATION,
1255 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1257 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1258 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1263 * @brief Publishes the record defined by the section section
1265 * @param cls closure
1266 * @param section the current section
1269 publish_iterate (void *cls __attribute__ ((unused)), const char *section)
1271 char *udp_redirects;
1272 char *tcp_redirects;
1273 char *alternative_names;
1274 char *alternative_name;
1277 if ((strlen (section) < 8) ||
1278 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1282 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1284 udp_redirects = NULL;
1286 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1288 tcp_redirects = NULL;
1291 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1295 if (keyfile != NULL)
1296 GNUNET_free (keyfile);
1300 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1301 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1302 GNUNET_free (keyfile);
1303 GNUNET_assert (my_private_key != NULL);
1305 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1306 uint32_t service_type = 0;
1308 if (NULL != udp_redirects)
1309 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1311 if (NULL != tcp_redirects)
1312 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1314 service_type = htonl (service_type);
1317 publish_name (section, ports, service_type, my_private_key);
1319 GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES",
1320 &alternative_names))
1322 for (alternative_name = strtok (alternative_names, " ");
1323 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1326 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1327 strcpy (altname, alternative_name);
1328 strcpy (altname + strlen (alternative_name) + 1, section);
1329 altname[strlen (alternative_name)] = '.';
1331 publish_name (altname, ports, service_type, my_private_key);
1333 GNUNET_free (alternative_names);
1335 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1336 GNUNET_free_non_null (udp_redirects);
1337 GNUNET_free_non_null (tcp_redirects);
1341 * Publish a DNS-record in the DHT.
1344 publish_names (void *cls
1345 __attribute__ ((unused)),
1346 const struct GNUNET_SCHEDULER_TaskContext *tc)
1348 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1351 GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL);
1353 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL);
1357 * @param cls closure
1358 * @param server the initialized server
1359 * @param cfg_ configuration to use
1362 run (void *cls, struct GNUNET_SERVER_Handle *server,
1363 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1365 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1366 /* callback, cls, type, size */
1367 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0},
1368 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1369 sizeof (struct GNUNET_MessageHeader)},
1373 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1374 {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0},
1375 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0},
1379 static GNUNET_MESH_ApplicationType apptypes[] = {
1380 GNUNET_APPLICATION_TYPE_END,
1381 GNUNET_APPLICATION_TYPE_END
1385 if (GNUNET_YES != open_port ())
1387 GNUNET_SCHEDULER_shutdown ();
1392 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1393 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1394 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1397 dht = GNUNET_DHT_connect (cfg, 1024);
1398 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1399 GNUNET_SERVER_add_handlers (server, handlers);
1400 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1405 * The main function for the dns service.
1407 * @param argc number of arguments from the command line
1408 * @param argv command line arguments
1409 * @return 0 ok, 1 on error
1412 main (int argc, char *const *argv)
1414 return (GNUNET_OK ==
1415 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1416 &run, NULL)) ? 0 : 1;