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;
47 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
48 * sent through gnunet. The port of this socket will not be hijacked.
50 static struct GNUNET_NETWORK_Handle *dnsout;
53 * The port bound to the socket dnsout
55 static unsigned short dnsoutport;
58 * A handle to the DHT-Service
60 static struct GNUNET_DHT_Handle *dht;
63 * The configuration to use
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * A list of DNS-Responses that have to be sent to the requesting client
70 static struct answer_packet_list *head;
73 * The tail of the list of DNS-responses
75 static struct answer_packet_list *tail;
78 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
79 * some information needed to handle this query
81 * It currently allocates at least
82 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
89 struct GNUNET_SERVER_Client *client;
90 struct GNUNET_MESH_Tunnel *tunnel;
96 } query_states[UINT16_MAX+1];
99 * A struct used to give more than one value as
100 * closure to receive_dht
102 struct receive_dht_cls
105 struct GNUNET_DHT_GetHandle *handle;
108 struct tunnel_notify_queue
110 struct tunnel_notify_queue *next;
111 struct tunnel_notify_queue *prev;
114 GNUNET_CONNECTION_TransmitReadyNotify cb;
118 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
122 __attribute__ ((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
124 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
129 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
130 "Delaying the hijacking, port is still %d!\n", dnsoutport);
131 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
137 struct GNUNET_OS_Process *proc;
140 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
143 "No entry 'VIRTDNS' in configuration!\n");
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
148 snprintf (port_s, 6, "%d", dnsoutport);
151 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
152 "gnunet-hijack-dns", port_s, virt_dns, NULL)))
153 GNUNET_OS_process_close (proc);
154 GNUNET_free (virt_dns);
158 * Delete the hijacking-routes
161 unhijack (unsigned short port)
165 struct GNUNET_OS_Process *proc;
168 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 "No entry 'VIRTDNS' in configuration!\n");
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
176 snprintf (port_s, 6, "%d", port);
179 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
180 "gnunet-hijack-dns", "-d", port_s, virt_dns,
182 GNUNET_OS_process_close (proc);
183 GNUNET_free (virt_dns);
187 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
191 send_answer (void *cls, size_t size, void *buf)
193 struct answer_packet_list *query = head;
194 size_t len = ntohs (query->pkt.hdr.size);
196 GNUNET_assert (len <= size);
198 memcpy (buf, &query->pkt.hdr, len);
200 GNUNET_CONTAINER_DLL_remove (head, tail, query);
204 /* When more data is to be sent, reschedule */
206 GNUNET_SERVER_notify_transmit_ready (cls, ntohs (head->pkt.hdr.size),
207 GNUNET_TIME_UNIT_FOREVER_REL,
215 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
216 struct GNUNET_MessageHeader hdr;
220 struct tunnel_cls *remote_pending[UINT16_MAX];
223 mesh_send_response (void *cls, size_t size, void *buf)
225 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
226 struct GNUNET_MessageHeader *hdr = buf;
228 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
229 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
231 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
232 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
236 *sz + sizeof (struct GNUNET_MessageHeader));
238 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
240 memcpy (hdr + 1, dns, *sz);
242 if (NULL != GNUNET_MESH_tunnel_get_head (*tunnel))
244 struct tunnel_notify_queue *element = GNUNET_MESH_tunnel_get_head (*tunnel);
245 struct tunnel_notify_queue *head = GNUNET_MESH_tunnel_get_head (*tunnel);
246 struct tunnel_notify_queue *tail = GNUNET_MESH_tunnel_get_tail (*tunnel);
248 GNUNET_CONTAINER_DLL_remove (head, tail, element);
250 GNUNET_MESH_tunnel_set_head (*tunnel, head);
251 GNUNET_MESH_tunnel_set_tail (*tunnel, tail);
252 struct GNUNET_MESH_TransmitHandle *th =
253 GNUNET_MESH_notify_transmit_ready (*tunnel,
256 GNUNET_TIME_relative_divide
257 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
258 (const struct GNUNET_PeerIdentity *)
260 element->cb, element->cls);
262 /* save the handle */
263 GNUNET_MESH_tunnel_set_data (*tunnel, th);
268 return ntohs (hdr->size);
272 mesh_send (void *cls, size_t size, void *buf)
274 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
276 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
278 GNUNET_assert (cls_->hdr.size <= size);
280 size = cls_->hdr.size;
281 cls_->hdr.size = htons (cls_->hdr.size);
283 memcpy (buf, &cls_->hdr, size);
285 if (NULL != GNUNET_MESH_tunnel_get_head (cls_->tunnel))
287 struct tunnel_notify_queue *element =
288 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
289 struct tunnel_notify_queue *head =
290 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
291 struct tunnel_notify_queue *tail =
292 GNUNET_MESH_tunnel_get_tail (cls_->tunnel);
294 GNUNET_CONTAINER_DLL_remove (head, tail, element);
296 GNUNET_MESH_tunnel_set_head (cls_->tunnel, head);
297 GNUNET_MESH_tunnel_set_tail (cls_->tunnel, tail);
298 struct GNUNET_MESH_TransmitHandle *th =
299 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
302 GNUNET_TIME_relative_divide
303 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
304 (const struct GNUNET_PeerIdentity *)
306 element->cb, element->cls);
308 /* save the handle */
309 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
310 GNUNET_free (element);
318 mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
319 const struct GNUNET_TRANSPORT_ATS_Information *atsi
320 __attribute__ ((unused)))
324 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Connected to peer %s, %x, sending query with id %d\n",
328 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
330 if (NULL == GNUNET_MESH_tunnel_get_data (cls_->tunnel))
332 struct GNUNET_MESH_TransmitHandle *th =
333 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
336 GNUNET_TIME_UNIT_MINUTES,
342 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
346 struct tunnel_notify_queue *head =
347 GNUNET_MESH_tunnel_get_head (cls_->tunnel);
348 struct tunnel_notify_queue *tail =
349 GNUNET_MESH_tunnel_get_tail (cls_->tunnel);
351 struct tunnel_notify_queue *element =
352 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
354 element->len = cls_->hdr.size;
355 element->cb = mesh_send;
357 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
358 GNUNET_MESH_tunnel_set_head (cls_->tunnel, head);
359 GNUNET_MESH_tunnel_set_tail (cls_->tunnel, tail);
365 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
370 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
373 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
374 GNUNET_TIME_UNIT_HOURS,
375 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
376 mesh_connect, NULL, cls_);
378 remote_pending[cls_->dns.s.id] = cls_;
382 receive_mesh_query (void *cls
383 __attribute__ ((unused)), struct GNUNET_MESH_Tunnel *tunnel,
385 __attribute__ ((unused)),
386 const struct GNUNET_PeerIdentity *sender
387 __attribute__ ((unused)),
388 const struct GNUNET_MessageHeader *message,
389 const struct GNUNET_TRANSPORT_ATS_Information *atsi
390 __attribute__ ((unused)))
392 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
394 struct sockaddr_in dest;
396 memset (&dest, 0, sizeof dest);
397 dest.sin_port = htons (53);
399 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_string(cfg, "dns", "EXTERNAL_DNS", &dns_resolver) ||
400 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr))
401 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
405 query_states[dns->s.id].tunnel = tunnel;
406 query_states[dns->s.id].valid = GNUNET_YES;
408 GNUNET_NETWORK_socket_sendto (dnsout, dns,
409 ntohs (message->size) -
410 sizeof (struct GNUNET_MessageHeader),
411 (struct sockaddr *) &dest, sizeof dest);
413 return GNUNET_SYSERR;
417 receive_mesh_answer (void *cls
418 __attribute__ ((unused)),
419 struct GNUNET_MESH_Tunnel *tunnel, void **ctx
420 __attribute__ ((unused)),
421 const struct GNUNET_PeerIdentity *sender,
422 const struct GNUNET_MessageHeader *message,
423 const struct GNUNET_TRANSPORT_ATS_Information *atsi
424 __attribute__ ((unused)))
426 /* TODo: size check */
427 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
429 /* They sent us a packet we were not waiting for */
430 if (remote_pending[dns->s.id] == NULL ||
431 remote_pending[dns->s.id]->tunnel != tunnel)
434 GNUNET_free (remote_pending[dns->s.id]);
435 remote_pending[dns->s.id] = NULL;
437 if (query_states[dns->s.id].valid != GNUNET_YES)
438 return GNUNET_SYSERR;
439 query_states[dns->s.id].valid = GNUNET_NO;
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
445 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 */
446 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
448 struct answer_packet_list *answer =
449 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
451 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
452 answer->pkt.hdr.size = htons (len);
454 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
456 if (ntohs (pdns->s.ancount) < 1)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n",
459 ntohs (pdns->s.ancount));
460 free_parsed_dns_packet (pdns);
461 GNUNET_free (answer);
465 answer->pkt.addrsize = ntohs (pdns->answers[0]->data_len);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n",
467 answer->pkt.addrsize);
468 memcpy (answer->pkt.addr, pdns->answers[0]->data,
469 ntohs (pdns->answers[0]->data_len));
471 answer->pkt.from = query_states[dns->s.id].remote_ip;
473 answer->pkt.to = query_states[dns->s.id].local_ip;
474 answer->pkt.dst_port = query_states[dns->s.id].local_port;
476 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
478 dpkt->s.id = dns->s.id;
482 dpkt->s.qdcount = htons (1);
483 dpkt->s.ancount = htons (1);
485 memcpy (dpkt->data, query_states[dns->s.id].name,
486 query_states[dns->s.id].namelen);
487 GNUNET_free (query_states[dns->s.id].name);
488 query_states[dns->s.id].name = NULL;
490 struct dns_query_line *dque =
491 (struct dns_query_line *) (dpkt->data +
492 (query_states[dns->s.id].namelen));
494 struct dns_record_line *drec_data =
495 (struct dns_record_line *) (dpkt->data +
496 (query_states[dns->s.id].namelen) +
497 sizeof (struct dns_query_line) + 2);
498 if (16 == answer->pkt.addrsize)
500 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
501 dque->type = htons (28); /* AAAA */
502 drec_data->type = htons (28); /* AAAA */
503 drec_data->data_len = htons (16);
507 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
508 dque->type = htons (1); /* A */
509 drec_data->type = htons (1); /* A */
510 drec_data->data_len = htons (4);
512 dque->class = htons (1); /* IN */
515 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
516 sizeof (struct dns_query_line));
517 memcpy (anname, "\xc0\x0c", 2);
518 drec_data->class = htons (1); /* IN */
520 drec_data->ttl = pdns->answers[0]->ttl;
522 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
523 * filled in by the daemon-vpn */
524 answer->pkt.addroffset =
525 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
526 (unsigned long) (&answer->pkt)));
528 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
530 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, len,
531 GNUNET_TIME_UNIT_FOREVER_REL,
533 query_states[dns->s.id].client);
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Sent answer of length %d on to client, addroffset = %d\n", len,
537 answer->pkt.addroffset);
539 free_parsed_dns_packet (pdns);
545 send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
550 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
552 unsigned short id = pdns->s.id;
554 free_parsed_dns_packet (pdns);
556 if (query_states[id].valid != GNUNET_YES)
558 query_states[id].valid = GNUNET_NO;
560 GNUNET_assert (query_states[id].namelen == 74);
562 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */
563 + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
564 + sizeof (struct dns_record_line) - 1 -
565 2 /* We do not know the lenght of the answer yet */ ;
567 struct answer_packet_list *answer =
568 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
570 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
571 answer->pkt.hdr.size = htons (len);
572 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
574 answer->pkt.from = query_states[id].remote_ip;
576 answer->pkt.to = query_states[id].local_ip;
577 answer->pkt.dst_port = query_states[id].local_port;
579 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
585 dpkt->s.qdcount = htons (1);
586 dpkt->s.ancount = htons (1);
588 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
589 GNUNET_free (query_states[id].name);
590 query_states[id].name = NULL;
592 struct dns_query_line *dque =
593 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
594 dque->type = htons (12); /* PTR */
595 dque->class = htons (1); /* IN */
598 (char *) (dpkt->data + (query_states[id].namelen) +
599 sizeof (struct dns_query_line));
600 memcpy (anname, "\xc0\x0c", 2);
602 struct dns_record_line *drec_data =
603 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
604 sizeof (struct dns_query_line) + 2);
605 drec_data->type = htons (12); /* AAAA */
606 drec_data->class = htons (1); /* IN */
607 /* FIXME: read the TTL from block:
608 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
610 * But how to get the seconds out of this?
612 drec_data->ttl = htonl (3600);
614 /* Calculate at which offset in the packet the length of the name and the
615 * name, it is filled in by the daemon-vpn */
616 answer->pkt.addroffset =
617 htons ((unsigned short) ((unsigned long) (&drec_data->data_len) -
618 (unsigned long) (&answer->pkt)));
620 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
622 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
623 GNUNET_TIME_UNIT_FOREVER_REL,
624 &send_answer, query_states[id].client);
628 * Receive a block from the dht.
631 receive_dht (void *cls, struct GNUNET_TIME_Absolute exp
632 __attribute__ ((unused)), const GNUNET_HashCode * key
633 __attribute__ ((unused)),
634 const struct GNUNET_PeerIdentity *const *get_path
635 __attribute__ ((unused)),
636 const struct GNUNET_PeerIdentity *const *put_path
637 __attribute__ ((unused)), enum GNUNET_BLOCK_Type type, size_t size,
641 unsigned short id = ((struct receive_dht_cls *) cls)->id;
642 struct GNUNET_DHT_GetHandle *handle =
643 ((struct receive_dht_cls *) cls)->handle;
646 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
648 /* If no query with this id is pending, ignore the block */
649 if (query_states[id].valid != GNUNET_YES)
651 query_states[id].valid = GNUNET_NO;
653 const struct GNUNET_DNS_Record *rec = data;
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Got block of size %d, peer: %08x, desc: %08x\n", size,
657 *((unsigned int *) &rec->peer),
658 *((unsigned int *) &rec->service_descriptor));
660 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 */
661 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
663 struct answer_packet_list *answer =
664 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
666 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
667 answer->pkt.hdr.size = htons (len);
668 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
670 GNUNET_CRYPTO_hash (&rec->peer,
671 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
672 &answer->pkt.service_descr.peer);
674 memcpy (&answer->pkt.service_descr.service_descriptor,
675 &rec->service_descriptor, sizeof (GNUNET_HashCode));
676 memcpy (&answer->pkt.service_descr.service_type, &rec->service_type,
677 sizeof (answer->pkt.service_descr.service_type));
678 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
679 sizeof (answer->pkt.service_descr.ports));
681 answer->pkt.from = query_states[id].remote_ip;
683 answer->pkt.to = query_states[id].local_ip;
684 answer->pkt.dst_port = query_states[id].local_port;
686 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
692 dpkt->s.qdcount = htons (1);
693 dpkt->s.ancount = htons (1);
695 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
696 GNUNET_free (query_states[id].name);
697 query_states[id].name = NULL;
699 struct dns_query_line *dque =
700 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
701 dque->type = htons (28); /* AAAA */
702 dque->class = htons (1); /* IN */
705 (char *) (dpkt->data + (query_states[id].namelen) +
706 sizeof (struct dns_query_line));
707 memcpy (anname, "\xc0\x0c", 2);
709 struct dns_record_line *drec_data =
710 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
711 sizeof (struct dns_query_line) + 2);
712 drec_data->type = htons (28); /* AAAA */
713 drec_data->class = htons (1); /* IN */
715 /* FIXME: read the TTL from block:
716 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
718 * But how to get the seconds out of this?
720 drec_data->ttl = htonl (3600);
721 drec_data->data_len = htons (16);
723 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
724 * filled in by the daemon-vpn */
725 answer->pkt.addroffset =
726 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
727 (unsigned long) (&answer->pkt)));
729 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
731 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
732 GNUNET_TIME_UNIT_FOREVER_REL,
733 &send_answer, query_states[id].client);
735 GNUNET_DHT_get_stop (handle);
739 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
743 __attribute__ ((unused)), struct GNUNET_SERVER_Client *client,
744 const struct GNUNET_MessageHeader *message __attribute__ ((unused)))
746 unhijack (dnsoutport);
747 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
753 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
756 receive_query (void *cls
757 __attribute__ ((unused)), struct GNUNET_SERVER_Client *client,
758 const struct GNUNET_MessageHeader *message)
760 struct query_packet *pkt = (struct query_packet *) message;
761 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
762 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
764 query_states[dns->s.id].valid = GNUNET_YES;
765 query_states[dns->s.id].client = client;
766 query_states[dns->s.id].local_ip = pkt->orig_from;
767 query_states[dns->s.id].local_port = pkt->src_port;
768 query_states[dns->s.id].remote_ip = pkt->orig_to;
769 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
770 if (query_states[dns->s.id].name != NULL)
771 GNUNET_free (query_states[dns->s.id].name);
772 query_states[dns->s.id].name =
773 GNUNET_malloc (query_states[dns->s.id].namelen);
774 memcpy (query_states[dns->s.id].name, dns->data,
775 query_states[dns->s.id].namelen);
777 /* The query is for a .gnunet-address */
778 if (pdns->queries[0]->namelen > 9 &&
779 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
785 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n",
789 *((unsigned int *) &key), pdns->queries[0]->namelen);
791 struct receive_dht_cls *cls =
792 GNUNET_malloc (sizeof (struct receive_dht_cls));
796 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
797 GNUNET_BLOCK_TYPE_DNS, &key,
798 DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
799 0, NULL, 0, receive_dht, cls);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
805 pdns->queries[0]->name, pdns->queries[0]->namelen);
807 /* This is a PTR-Query. Check if it is for "our" network */
808 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
813 "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.";
815 unsigned long long ipv6prefix;
816 unsigned int comparelen;
818 GNUNET_assert (GNUNET_OK ==
819 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
822 inet_pton (AF_INET6, ipv6addr, ipv6);
823 GNUNET_free (ipv6addr);
825 GNUNET_assert (GNUNET_OK ==
826 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
829 GNUNET_assert (ipv6prefix < 127);
830 ipv6prefix = (ipv6prefix + 7) / 8;
832 for (i = ipv6prefix; i < 16; i++)
835 for (i = 0; i < 16; i++)
837 unsigned char c1 = ipv6[i] >> 4;
838 unsigned char c2 = ipv6[i] & 0xf;
841 ipv6rev[62 - (4 * i)] = c1 + '0';
843 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
846 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
848 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
851 comparelen = 10 + 4 * ipv6prefix;
853 strncmp (pdns->queries[0]->name +
854 (pdns->queries[0]->namelen - comparelen),
855 ipv6rev + (74 - comparelen), comparelen))
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
859 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
866 unsigned int virt_dns_bytes;
869 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
872 "No entry 'VIRTDNS' in configuration!\n");
876 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
883 GNUNET_free (virt_dns);
885 if (virt_dns_bytes == pkt->orig_to)
887 /* This is a packet that was sent directly to the virtual dns-server
889 * This means we have to send this query over gnunet
893 sizeof (struct GNUNET_MESH_Tunnel *) +
894 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
895 sizeof (struct query_packet) +
897 struct tunnel_cls *cls_ = GNUNET_malloc (size);
899 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
901 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
904 memcpy (&cls_->dns, dns,
905 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
906 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
912 /* The query should be sent to the network */
914 struct sockaddr_in dest;
916 memset (&dest, 0, sizeof dest);
917 dest.sin_port = htons (53);
918 dest.sin_addr.s_addr = pkt->orig_to;
920 GNUNET_NETWORK_socket_sendto (dnsout, dns,
921 ntohs (pkt->hdr.size) -
922 sizeof (struct query_packet) + 1,
923 (struct sockaddr *) &dest, sizeof dest);
926 free_parsed_dns_packet (pdns);
929 GNUNET_SERVER_receive_done (client, GNUNET_OK);
933 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
938 struct sockaddr_in addr;
940 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
942 return GNUNET_SYSERR;
943 memset (&addr, 0, sizeof (struct sockaddr_in));
945 addr.sin_family = AF_INET;
946 int err = GNUNET_NETWORK_socket_bind (dnsout,
947 (struct sockaddr *) &addr,
948 sizeof (struct sockaddr_in));
950 if (err != GNUNET_OK)
952 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
953 return GNUNET_SYSERR;
956 /* Read the port we bound to */
957 socklen_t addrlen = sizeof (struct sockaddr_in);
960 getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr,
963 dnsoutport = htons (addr.sin_port);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
967 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
968 &read_response, NULL);
974 * Read a response-packet of the UDP-Socket
977 read_response (void *cls
978 __attribute__ ((unused)),
979 const struct GNUNET_SCHEDULER_TaskContext *tc)
981 struct sockaddr_in addr;
982 socklen_t addrlen = sizeof (addr);
986 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
989 memset (&addr, 0, sizeof addr);
992 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
994 unhijack (dnsoutport);
995 if (GNUNET_YES == open_port ())
996 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
1000 /* port the code above? */
1004 unsigned char buf[len];
1005 struct dns_pkt *dns = (struct dns_pkt *) buf;
1007 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1008 (struct sockaddr *) &addr, &addrlen);
1012 unhijack (dnsoutport);
1013 if (GNUNET_YES == open_port ())
1014 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1021 if (query_states[dns->s.id].valid == GNUNET_YES)
1023 if (query_states[dns->s.id].tunnel != NULL)
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Answer to query %d for a remote peer!\n",
1028 /* This response should go through a tunnel */
1030 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1032 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1034 *t = query_states[dns->s.id].tunnel;
1035 memcpy (t + 1, dns, r);
1037 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel))
1039 struct GNUNET_MESH_TransmitHandle *th =
1040 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1043 GNUNET_TIME_UNIT_MINUTES,
1047 GNUNET_MessageHeader),
1048 mesh_send_response, c);
1050 GNUNET_MESH_tunnel_set_data (query_states[dns->s.id].tunnel, th);
1054 struct tunnel_notify_queue *head =
1055 GNUNET_MESH_tunnel_get_head (query_states[dns->s.id].tunnel);
1056 struct tunnel_notify_queue *tail =
1057 GNUNET_MESH_tunnel_get_tail (query_states[dns->s.id].tunnel);
1059 struct tunnel_notify_queue *element =
1060 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1062 element->len = r + sizeof (struct GNUNET_MessageHeader);
1063 element->cb = mesh_send_response;
1065 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
1066 GNUNET_MESH_tunnel_set_head (query_states[dns->s.id].tunnel, head);
1067 GNUNET_MESH_tunnel_set_tail (query_states[dns->s.id].tunnel, tail);
1072 query_states[dns->s.id].valid = GNUNET_NO;
1074 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1075 struct answer_packet_list *answer =
1076 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
1077 answer->pkt.hdr.type =
1078 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1079 answer->pkt.hdr.size = htons (len);
1080 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1081 answer->pkt.from = addr.sin_addr.s_addr;
1082 answer->pkt.to = query_states[dns->s.id].local_ip;
1083 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1084 memcpy (answer->pkt.data, buf, r);
1086 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1088 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1089 len, GNUNET_TIME_UNIT_FOREVER_REL,
1091 query_states[dns->s.id].client);
1095 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1096 &read_response, NULL);
1101 * Task run during shutdown.
1107 cleanup_task (void *cls
1108 __attribute__ ((unused)),
1109 const struct GNUNET_SCHEDULER_TaskContext *tc)
1111 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1113 unhijack (dnsoutport);
1114 GNUNET_DHT_disconnect (dht);
1115 GNUNET_MESH_disconnect (mesh_handle);
1119 * @brief Create a port-map from udp and tcp redirects
1121 * @param udp_redirects
1122 * @param tcp_redirects
1127 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1130 char *cpy, *hostname, *redirect;
1132 unsigned int count = 0;
1135 if (NULL != udp_redirects)
1137 cpy = GNUNET_strdup (udp_redirects);
1138 for (redirect = strtok (cpy, " "); redirect != NULL;
1139 redirect = strtok (NULL, " "))
1141 if (NULL == (hostname = strstr (redirect, ":")))
1143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1144 "Warning: option %s is not formatted correctly!\n",
1149 local_port = atoi (redirect);
1150 if (!((local_port > 0) && (local_port < 65536)))
1151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1152 "Warning: %s is not a correct port.", redirect);
1154 ret |= (0xFFFF & htons (local_port));
1168 if (NULL != tcp_redirects)
1170 cpy = GNUNET_strdup (tcp_redirects);
1171 for (redirect = strtok (cpy, " "); redirect != NULL;
1172 redirect = strtok (NULL, " "))
1174 if (NULL == (hostname = strstr (redirect, ":")))
1176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1177 "Warning: option %s is not formatted correctly!\n",
1182 local_port = atoi (redirect);
1183 if (!((local_port > 0) && (local_port < 65536)))
1184 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1185 "Warning: %s is not a correct port.", redirect);
1187 ret |= (0xFFFF & htons (local_port));
1202 GNUNET_free_non_null (cpy);
1207 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1208 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1210 size_t size = sizeof (struct GNUNET_DNS_Record);
1211 struct GNUNET_DNS_Record data;
1213 memset (&data, 0, size);
1215 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1216 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1218 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1220 *((unsigned long long *) &data.service_descriptor));
1222 data.service_type = service_type;
1225 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1227 data.expiration_time =
1228 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
1229 (GNUNET_TIME_UNIT_HOURS, 2));
1231 /* Sign the block */
1233 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1240 *((unsigned int *) &data.service_descriptor), size);
1242 GNUNET_DHT_put (dht, &data.service_descriptor, DEFAULT_PUT_REPLICATION,
1243 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1245 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1246 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1251 * @brief Publishes the record defined by the section section
1253 * @param cls closure
1254 * @param section the current section
1257 publish_iterate (void *cls __attribute__ ((unused)), const char *section)
1259 char *udp_redirects;
1260 char *tcp_redirects;
1261 char *alternative_names;
1262 char *alternative_name;
1265 if ((strlen (section) < 8) ||
1266 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1270 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1272 udp_redirects = NULL;
1274 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1276 tcp_redirects = NULL;
1279 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1283 if (keyfile != NULL)
1284 GNUNET_free (keyfile);
1288 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1289 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1290 GNUNET_free (keyfile);
1291 GNUNET_assert (my_private_key != NULL);
1293 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1294 uint32_t service_type = 0;
1296 if (NULL != udp_redirects)
1297 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1299 if (NULL != tcp_redirects)
1300 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1302 service_type = htonl (service_type);
1305 publish_name (section, ports, service_type, my_private_key);
1307 GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES",
1308 &alternative_names))
1310 for (alternative_name = strtok (alternative_names, " ");
1311 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1314 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1315 strcpy (altname, alternative_name);
1316 strcpy (altname + strlen (alternative_name) + 1, section);
1317 altname[strlen (alternative_name)] = '.';
1319 publish_name (altname, ports, service_type, my_private_key);
1321 GNUNET_free (alternative_names);
1323 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1324 GNUNET_free_non_null (udp_redirects);
1325 GNUNET_free_non_null (tcp_redirects);
1329 * Publish a DNS-record in the DHT.
1332 publish_names (void *cls
1333 __attribute__ ((unused)),
1334 const struct GNUNET_SCHEDULER_TaskContext *tc)
1336 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1339 GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL);
1341 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL);
1345 * @param cls closure
1346 * @param server the initialized server
1347 * @param cfg_ configuration to use
1350 run (void *cls, struct GNUNET_SERVER_Handle *server,
1351 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1353 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1354 /* callback, cls, type, size */
1355 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0},
1356 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1357 sizeof (struct GNUNET_MessageHeader)},
1361 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1362 {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0},
1363 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0},
1367 static GNUNET_MESH_ApplicationType apptypes[] = {
1368 GNUNET_APPLICATION_TYPE_END,
1369 GNUNET_APPLICATION_TYPE_END
1373 if (GNUNET_YES != open_port ())
1375 GNUNET_SCHEDULER_shutdown ();
1380 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1381 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1382 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1385 dht = GNUNET_DHT_connect (cfg, 1024);
1386 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1387 GNUNET_SERVER_add_handlers (server, handlers);
1388 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1393 * The main function for the dns service.
1395 * @param argc number of arguments from the command line
1396 * @param argv command line arguments
1397 * @return 0 ok, 1 on error
1400 main (int argc, char *const *argv)
1402 return (GNUNET_OK ==
1403 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1404 &run, NULL)) ? 0 : 1;