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 dns/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_dns_service.h"
32 #include "gnunet_connection_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_applications.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_dnsparser_lib.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"
47 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
48 * some information needed to handle this query
50 * It currently allocates at least
51 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
58 struct GNUNET_SERVER_Client *client;
59 struct GNUNET_MESH_Tunnel *tunnel;
67 } query_states[UINT16_MAX + 1];
70 * A struct used to give more than one value as
71 * closure to receive_dht
73 struct receive_dht_cls
76 struct GNUNET_DHT_GetHandle *handle;
79 struct tunnel_notify_queue
81 struct tunnel_notify_queue *next;
82 struct tunnel_notify_queue *prev;
85 GNUNET_CONNECTION_TransmitReadyNotify cb;
90 struct tunnel_notify_queue *head, *tail;
91 struct GNUNET_MESH_TransmitHandle *th;
95 struct answer_packet_list
97 struct answer_packet_list *next;
98 struct answer_packet_list *prev;
99 struct GNUNET_SERVER_Client *client;
100 struct answer_packet pkt;
103 GNUNET_NETWORK_STRUCT_BEGIN
106 struct GNUNET_MESH_Tunnel *tunnel;
107 struct GNUNET_MessageHeader hdr;
110 GNUNET_NETWORK_STRUCT_END
112 struct tunnel_cls *remote_pending[UINT16_MAX];
115 static struct GNUNET_MESH_Handle *mesh_handle;
117 static struct GNUNET_CONNECTION_TransmitHandle *server_notify;
120 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
121 * sent through gnunet. The port of this socket will not be hijacked.
123 static struct GNUNET_NETWORK_Handle *dnsout;
125 static struct GNUNET_NETWORK_Handle *dnsout6;
128 * The port bound to the socket dnsout
130 static unsigned short dnsoutport;
133 * A handle to the DHT-Service
135 static struct GNUNET_DHT_Handle *dht;
138 * The configuration to use
140 static const struct GNUNET_CONFIGURATION_Handle *cfg;
143 * A list of DNS-Responses that have to be sent to the requesting client
145 static struct answer_packet_list *head;
148 * The tail of the list of DNS-responses
150 static struct answer_packet_list *tail;
154 send_answer (void *cls, size_t size, void *buf);
157 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
162 if (head->client == client)
164 GNUNET_CONNECTION_notify_transmit_ready_cancel (server_notify);
166 GNUNET_SERVER_notify_transmit_ready (head->next->client,
167 ntohs (head->next->pkt.hdr.size),
168 GNUNET_TIME_UNIT_FOREVER_REL,
172 struct answer_packet_list *element = head;
174 while (element != NULL)
176 if (element->client == client)
178 GNUNET_SERVER_client_drop (client);
179 GNUNET_CONTAINER_DLL_remove (head, tail, element);
180 struct answer_packet_list *t = element;
182 element = element->next;
186 element = element->next;
191 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
194 hijack (void *cls GNUNET_UNUSED, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 "Delaying the hijacking, port is still %d!\n", dnsoutport);
203 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
209 struct GNUNET_OS_Process *proc;
212 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 "No entry 'VIRTDNS' in configuration!\n");
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
220 snprintf (port_s, 6, "%d", dnsoutport);
223 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
224 "gnunet-hijack-dns", port_s, virt_dns, NULL)))
226 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
227 GNUNET_OS_process_close (proc);
229 GNUNET_free (virt_dns);
233 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
234 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
235 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
237 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
246 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
249 GNUNET_free (tunnel_ctx);
253 * Delete the hijacking-routes
256 unhijack (unsigned short port)
260 struct GNUNET_OS_Process *proc;
263 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "No entry 'VIRTDNS' in configuration!\n");
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
271 snprintf (port_s, 6, "%d", port);
274 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
275 "gnunet-hijack-dns", "-d", port_s, virt_dns,
278 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
279 GNUNET_OS_process_close (proc);
281 GNUNET_free (virt_dns);
285 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
289 send_answer (void *cls, size_t size, void *buf)
291 server_notify = NULL;
292 struct answer_packet_list *query = head;
293 size_t len = ntohs (query->pkt.hdr.size);
295 GNUNET_assert (len <= size);
297 memcpy (buf, &query->pkt.hdr, len);
299 GNUNET_CONTAINER_DLL_remove (head, tail, query);
301 /* When more data is to be sent, reschedule */
304 GNUNET_SERVER_notify_transmit_ready (head->client,
305 ntohs (head->pkt.hdr.size),
306 GNUNET_TIME_UNIT_FOREVER_REL,
309 GNUNET_SERVER_client_drop (query->client);
316 mesh_send_response (void *cls, size_t size, void *buf)
318 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
319 struct GNUNET_MessageHeader *hdr = buf;
321 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
322 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
324 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
326 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
327 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
331 *sz + sizeof (struct GNUNET_MessageHeader));
333 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
335 memcpy (hdr + 1, dns, *sz);
336 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);
340 struct tunnel_notify_queue *element = s->head;
341 struct tunnel_notify_queue *head = s->head;
342 struct tunnel_notify_queue *tail = s->tail;
344 GNUNET_CONTAINER_DLL_remove (head, tail, element);
347 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
348 GNUNET_TIME_relative_divide
349 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
350 (const struct GNUNET_PeerIdentity *)
351 NULL, element->len, element->cb,
357 return ntohs (hdr->size);
361 mesh_send (void *cls, size_t size, void *buf)
363 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
365 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
367 GNUNET_assert (cls_->hdr.size <= size);
369 size = cls_->hdr.size;
370 cls_->hdr.size = htons (cls_->hdr.size);
372 memcpy (buf, &cls_->hdr, size);
374 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
378 struct tunnel_notify_queue *element = s->head;
379 struct tunnel_notify_queue *head = s->head;
380 struct tunnel_notify_queue *tail = s->tail;;
382 GNUNET_CONTAINER_DLL_remove (head, tail, element);
385 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_NO, 42,
386 GNUNET_TIME_relative_divide
387 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
388 (const struct GNUNET_PeerIdentity *)
389 NULL, element->len, element->cb,
392 GNUNET_free (element);
400 mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
401 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
405 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Connected to peer %s, %x, sending query with id %d\n",
409 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
411 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
416 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_YES, 42,
417 GNUNET_TIME_UNIT_MINUTES, NULL,
418 cls_->hdr.size, mesh_send, cls);
423 struct tunnel_notify_queue *head = s->head;
424 struct tunnel_notify_queue *tail = s->tail;
426 struct tunnel_notify_queue *element =
427 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
429 element->len = cls_->hdr.size;
430 element->cb = mesh_send;
432 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
438 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
440 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
443 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
445 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
452 GNUNET_MESH_tunnel_create (mesh_handle, s, mesh_connect, NULL, cls_);
454 GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel,
455 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER);
457 remote_pending[cls_->dns.s.id] = cls_;
461 receive_mesh_query (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
462 void **ctx GNUNET_UNUSED,
463 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
464 const struct GNUNET_MessageHeader *message,
465 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
467 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
469 struct sockaddr_in dest;
471 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
473 memset (&dest, 0, sizeof dest);
474 dest.sin_port = htons (53);
478 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "EXTERNAL_DNS",
480 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr))
481 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
485 query_states[dns->s.id].tunnel = tunnel;
486 query_states[dns->s.id].valid = GNUNET_YES;
490 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
492 if (pdns->queries[i]->qtype == htons (28) ||
493 pdns->queries[i]->qtype == htons (1))
495 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
499 free_parsed_dns_packet (pdns);
501 GNUNET_NETWORK_socket_sendto (dnsout, dns,
502 ntohs (message->size) -
503 sizeof (struct GNUNET_MessageHeader),
504 (struct sockaddr *) &dest, sizeof dest);
506 return GNUNET_SYSERR;
510 receive_mesh_answer (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
511 void **ctx GNUNET_UNUSED,
512 const struct GNUNET_PeerIdentity *sender,
513 const struct GNUNET_MessageHeader *message,
514 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
516 /* TODo: size check */
517 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
519 /* They sent us a packet we were not waiting for */
520 if (remote_pending[dns->s.id] == NULL ||
521 remote_pending[dns->s.id]->tunnel != tunnel)
524 GNUNET_free (remote_pending[dns->s.id]);
525 remote_pending[dns->s.id] = NULL;
527 if (query_states[dns->s.id].valid != GNUNET_YES)
528 return GNUNET_SYSERR;
529 query_states[dns->s.id].valid = GNUNET_NO;
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
535 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 */
536 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
538 struct answer_packet_list *answer =
539 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
540 sizeof (struct answer_packet));
542 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
543 answer->pkt.hdr.size = htons (len);
545 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
547 if (ntohs (pdns->s.ancount) < 1)
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n",
550 ntohs (pdns->s.ancount));
551 free_parsed_dns_packet (pdns);
552 GNUNET_free (answer);
558 while (i < ntohs (pdns->s.ancount) && ntohs (pdns->answers[i]->type) != 28 &&
559 ntohs (pdns->answers[i]->type) != 1)
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n",
562 ntohs (pdns->answers[i]->type));
566 if (i >= ntohs (pdns->s.ancount))
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Answer does not contain any usable answers.\n");
570 free_parsed_dns_packet (pdns);
571 GNUNET_free (answer);
575 answer->pkt.addrsize = ntohs (pdns->answers[i]->data_len);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n",
577 answer->pkt.addrsize);
578 memcpy (answer->pkt.addr, pdns->answers[i]->data,
579 ntohs (pdns->answers[i]->data_len));
581 memcpy (answer->pkt.from, query_states[dns->s.id].remote_ip,
582 query_states[dns->s.id].addrlen);
583 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip,
584 query_states[dns->s.id].addrlen);
585 answer->pkt.addrlen = query_states[dns->s.id].addrlen;
586 answer->pkt.dst_port = query_states[dns->s.id].local_port;
588 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
590 dpkt->s.id = dns->s.id;
594 dpkt->s.qdcount = htons (1);
595 dpkt->s.ancount = htons (1);
597 memcpy (dpkt->data, query_states[dns->s.id].name,
598 query_states[dns->s.id].namelen);
599 GNUNET_free (query_states[dns->s.id].name);
600 query_states[dns->s.id].name = NULL;
602 struct dns_query_line *dque =
603 (struct dns_query_line *) (dpkt->data +
604 (query_states[dns->s.id].namelen));
606 struct dns_record_line *drec_data =
607 (struct dns_record_line *) (dpkt->data +
608 (query_states[dns->s.id].namelen) +
609 sizeof (struct dns_query_line) + 2);
610 if (htons (28) == query_states[dns->s.id].qtype)
612 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
613 dque->type = htons (28); /* AAAA */
614 drec_data->type = htons (28); /* AAAA */
615 drec_data->data_len = htons (16);
617 else if (htons (1) == query_states[dns->s.id].qtype)
619 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
620 dque->type = htons (1); /* A */
621 drec_data->type = htons (1); /* A */
622 drec_data->data_len = htons (4);
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n",
627 query_states[dns->s.id].qtype);
630 dque->class = htons (1); /* IN */
633 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
634 sizeof (struct dns_query_line));
635 memcpy (anname, "\xc0\x0c", 2);
636 drec_data->class = htons (1); /* IN */
638 drec_data->ttl = pdns->answers[i]->ttl;
640 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
641 * filled in by the daemon-vpn */
642 answer->pkt.addroffset =
643 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
644 (unsigned long) (&answer->pkt)));
646 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
647 answer->client = query_states[dns->s.id].client;
649 if (server_notify == NULL)
651 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
652 len, GNUNET_TIME_UNIT_FOREVER_REL,
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Sent answer of length %d on to client, addroffset = %d\n", len,
657 answer->pkt.addroffset);
659 free_parsed_dns_packet (pdns);
665 send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
667 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
670 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
672 unsigned short id = pdns->s.id;
674 free_parsed_dns_packet (pdns);
676 if (query_states[id].valid != GNUNET_YES)
678 query_states[id].valid = GNUNET_NO;
680 GNUNET_assert (query_states[id].namelen == 74);
682 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */
683 + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
684 + sizeof (struct dns_record_line) - 1 -
685 2 /* We do not know the lenght of the answer yet */ ;
687 struct answer_packet_list *answer =
688 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
689 sizeof (struct answer_packet));
691 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
692 answer->pkt.hdr.size = htons (len);
693 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
695 memcpy (answer->pkt.from, query_states[id].remote_ip,
696 query_states[id].addrlen);
697 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
699 answer->pkt.dst_port = query_states[id].local_port;
701 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
707 dpkt->s.qdcount = htons (1);
708 dpkt->s.ancount = htons (1);
710 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
711 GNUNET_free (query_states[id].name);
712 query_states[id].name = NULL;
714 struct dns_query_line *dque =
715 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
716 dque->type = htons (12); /* PTR */
717 dque->class = htons (1); /* IN */
720 (char *) (dpkt->data + (query_states[id].namelen) +
721 sizeof (struct dns_query_line));
722 memcpy (anname, "\xc0\x0c", 2);
724 struct dns_record_line *drec_data =
725 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
726 sizeof (struct dns_query_line) + 2);
727 drec_data->type = htons (12); /* AAAA */
728 drec_data->class = htons (1); /* IN */
729 /* FIXME: read the TTL from block:
730 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
732 * But how to get the seconds out of this?
734 drec_data->ttl = htonl (3600);
736 /* Calculate at which offset in the packet the length of the name and the
737 * name, it is filled in by the daemon-vpn */
738 answer->pkt.addroffset =
739 htons ((unsigned short) ((unsigned long) (&drec_data->data_len) -
740 (unsigned long) (&answer->pkt)));
742 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
743 answer->client = query_states[id].client;
745 if (server_notify == NULL)
747 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
748 GNUNET_TIME_UNIT_FOREVER_REL,
753 * Receive a block from the dht.
756 receive_dht (void *cls, struct GNUNET_TIME_Absolute exp GNUNET_UNUSED,
757 const GNUNET_HashCode * key GNUNET_UNUSED,
758 const struct GNUNET_PeerIdentity *get_path GNUNET_UNUSED,
759 unsigned int get_path_length GNUNET_UNUSED,
760 const struct GNUNET_PeerIdentity *put_path GNUNET_UNUSED,
761 unsigned int put_path_length GNUNET_UNUSED,
762 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
765 unsigned short id = ((struct receive_dht_cls *) cls)->id;
766 struct GNUNET_DHT_GetHandle *handle =
767 ((struct receive_dht_cls *) cls)->handle;
770 GNUNET_DHT_get_stop (handle);
772 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
774 /* If no query with this id is pending, ignore the block */
775 if (query_states[id].valid != GNUNET_YES)
777 query_states[id].valid = GNUNET_NO;
779 const struct GNUNET_DNS_Record *rec = data;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Got block of size %d, peer: %08x, desc: %08x\n", size,
783 *((unsigned int *) &rec->peer),
784 *((unsigned int *) &rec->service_descriptor));
786 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 */
787 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
789 struct answer_packet_list *answer =
790 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
791 sizeof (struct answer_packet));
793 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
794 answer->pkt.hdr.size = htons (len);
795 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
796 answer->client = query_states[id].client;
798 GNUNET_CRYPTO_hash (&rec->peer,
799 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
800 &answer->pkt.service_descr.peer);
802 memcpy (&answer->pkt.service_descr.service_descriptor,
803 &rec->service_descriptor, sizeof (GNUNET_HashCode));
804 memcpy (&answer->pkt.service_descr.service_type, &rec->service_type,
805 sizeof (answer->pkt.service_descr.service_type));
806 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
807 sizeof (answer->pkt.service_descr.ports));
809 memcpy (answer->pkt.from, query_states[id].remote_ip,
810 query_states[id].addrlen);
811 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
812 answer->pkt.addrlen = query_states[id].addrlen;
814 answer->pkt.dst_port = query_states[id].local_port;
816 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
822 dpkt->s.qdcount = htons (1);
823 dpkt->s.ancount = htons (1);
825 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
826 GNUNET_free (query_states[id].name);
827 query_states[id].name = NULL;
829 struct dns_query_line *dque =
830 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
831 dque->type = htons (28); /* AAAA */
832 dque->class = htons (1); /* IN */
835 (char *) (dpkt->data + (query_states[id].namelen) +
836 sizeof (struct dns_query_line));
837 memcpy (anname, "\xc0\x0c", 2);
839 struct dns_record_line *drec_data =
840 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
841 sizeof (struct dns_query_line) + 2);
842 drec_data->type = htons (28); /* AAAA */
843 drec_data->class = htons (1); /* IN */
845 /* FIXME: read the TTL from block:
846 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
848 * But how to get the seconds out of this?
850 drec_data->ttl = htonl (3600);
851 drec_data->data_len = htons (16);
853 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
854 * filled in by the daemon-vpn */
855 answer->pkt.addroffset =
856 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
857 (unsigned long) (&answer->pkt)));
859 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
861 if (server_notify == NULL)
863 GNUNET_SERVER_notify_transmit_ready (answer->client, len,
864 GNUNET_TIME_UNIT_FOREVER_REL,
869 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
872 rehijack (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
873 const struct GNUNET_MessageHeader *message GNUNET_UNUSED)
875 unhijack (dnsoutport);
876 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
878 GNUNET_SERVER_receive_done (client, GNUNET_OK);
882 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
885 receive_query (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
886 const struct GNUNET_MessageHeader *message)
888 struct query_packet *pkt = (struct query_packet *) message;
889 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
890 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
892 query_states[dns->s.id].valid = GNUNET_YES;
893 query_states[dns->s.id].client = client;
894 GNUNET_SERVER_client_keep (client);
895 memcpy (query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen);
896 query_states[dns->s.id].addrlen = pkt->addrlen;
897 query_states[dns->s.id].local_port = pkt->src_port;
898 memcpy (query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen);
899 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
900 if (query_states[dns->s.id].name != NULL)
901 GNUNET_free (query_states[dns->s.id].name);
902 query_states[dns->s.id].name =
903 GNUNET_malloc (query_states[dns->s.id].namelen);
904 memcpy (query_states[dns->s.id].name, dns->data,
905 query_states[dns->s.id].namelen);
909 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
911 if (pdns->queries[i]->qtype == htons (28) ||
912 pdns->queries[i]->qtype == htons (1))
914 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
919 /* The query is for a .gnunet-address */
920 if (pdns->queries[0]->namelen > 9 &&
921 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
927 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n",
931 *((unsigned int *) &key), pdns->queries[0]->namelen);
933 struct receive_dht_cls *cls =
934 GNUNET_malloc (sizeof (struct receive_dht_cls));
938 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
939 GNUNET_BLOCK_TYPE_DNS, &key,
940 5 /* DEFAULT_GET_REPLICATION */ ,
941 GNUNET_DHT_RO_NONE, NULL, 0, &receive_dht, cls);
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
947 pdns->queries[0]->name, pdns->queries[0]->namelen);
949 /* This is a PTR-Query. Check if it is for "our" network */
950 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
955 "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.";
957 unsigned long long ipv6prefix;
958 unsigned int comparelen;
960 GNUNET_assert (GNUNET_OK ==
961 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
964 inet_pton (AF_INET6, ipv6addr, ipv6);
965 GNUNET_free (ipv6addr);
967 GNUNET_assert (GNUNET_OK ==
968 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
971 GNUNET_assert (ipv6prefix < 127);
972 ipv6prefix = (ipv6prefix + 7) / 8;
974 for (i = ipv6prefix; i < 16; i++)
977 for (i = 0; i < 16; i++)
979 unsigned char c1 = ipv6[i] >> 4;
980 unsigned char c2 = ipv6[i] & 0xf;
983 ipv6rev[62 - (4 * i)] = c1 + '0';
985 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
988 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
990 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
993 comparelen = 10 + 4 * ipv6prefix;
995 strncmp (pdns->queries[0]->name +
996 (pdns->queries[0]->namelen - comparelen),
997 ipv6rev + (74 - comparelen), comparelen))
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
1001 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
1007 unsigned char virt_dns_bytes[16];
1009 if (pkt->addrlen == 4)
1013 if (GNUNET_SYSERR ==
1014 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
1017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1018 "No entry 'VIRTDNS' in configuration!\n");
1022 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
1024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
1029 GNUNET_free (virt_dns);
1031 else if (pkt->addrlen == 16)
1035 if (GNUNET_SYSERR ==
1036 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6",
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1040 "No entry 'VIRTDNS6' in configuration!\n");
1044 if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes))
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1047 "Error parsing 'VIRTDNS6': %s; %m!\n", virt_dns);
1051 GNUNET_free (virt_dns);
1058 if (memcmp (virt_dns_bytes, pkt->orig_to, pkt->addrlen) == 0)
1060 /* This is a packet that was sent directly to the virtual dns-server
1062 * This means we have to send this query over gnunet
1066 sizeof (struct GNUNET_MESH_Tunnel *) +
1067 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1068 sizeof (struct query_packet) +
1070 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1072 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1074 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1077 memcpy (&cls_->dns, dns,
1078 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1079 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1081 if (ntohs (pdns->s.qdcount) == 1)
1083 if (ntohs (pdns->queries[0]->qtype) == 1)
1084 pdns->queries[0]->qtype = htons (28);
1085 else if (ntohs (pdns->queries[0]->qtype) == 28)
1086 pdns->queries[0]->qtype = htons (1);
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n");
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n");
1093 struct dns_pkt *rdns = unparse_dns_packet (pdns);
1095 sizeof (struct GNUNET_MESH_Tunnel *) +
1096 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1097 sizeof (struct query_packet) +
1099 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1101 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1103 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1106 memcpy (&cls_->dns, rdns,
1107 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1108 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1116 /* The query should be sent to the network */
1117 if (pkt->addrlen == 4)
1119 struct sockaddr_in dest;
1121 memset (&dest, 0, sizeof dest);
1122 dest.sin_port = htons (53);
1123 memcpy (&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen);
1125 GNUNET_NETWORK_socket_sendto (dnsout, dns,
1126 ntohs (pkt->hdr.size) -
1127 sizeof (struct query_packet) + 1,
1128 (struct sockaddr *) &dest, sizeof dest);
1130 else if (pkt->addrlen == 16)
1132 struct sockaddr_in6 dest;
1134 memset (&dest, 0, sizeof dest);
1135 dest.sin6_port = htons (53);
1136 memcpy (&dest.sin6_addr, pkt->orig_to, pkt->addrlen);
1138 GNUNET_NETWORK_socket_sendto (dnsout6, dns,
1139 ntohs (pkt->hdr.size) -
1140 sizeof (struct query_packet) + 1,
1141 (struct sockaddr *) &dest, sizeof dest);
1145 free_parsed_dns_packet (pdns);
1148 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1152 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1155 read_response6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1160 struct sockaddr_in6 addr;
1162 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0);
1163 if (dnsout6 == NULL)
1165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n");
1166 return GNUNET_SYSERR;
1168 memset (&addr, 0, sizeof (struct sockaddr_in6));
1170 addr.sin6_family = AF_INET6;
1171 int err = GNUNET_NETWORK_socket_bind (dnsout6,
1172 (struct sockaddr *) &addr,
1173 sizeof (struct sockaddr_in6));
1175 if (err != GNUNET_OK)
1177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1178 return GNUNET_SYSERR;
1181 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1182 &read_response6, NULL);
1190 struct sockaddr_in addr;
1192 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1194 return GNUNET_SYSERR;
1195 memset (&addr, 0, sizeof (struct sockaddr_in));
1197 addr.sin_family = AF_INET;
1198 int err = GNUNET_NETWORK_socket_bind (dnsout,
1199 (struct sockaddr *) &addr,
1200 sizeof (struct sockaddr_in));
1202 if (err != GNUNET_OK)
1204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1205 return GNUNET_SYSERR;
1208 /* Read the port we bound to */
1209 socklen_t addrlen = sizeof (struct sockaddr_in);
1212 getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr,
1215 dnsoutport = htons (addr.sin_port);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
1219 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1220 &read_response, NULL);
1226 handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1230 * Read a response-packet of the UDP-Socket
1233 read_response6 (void *cls GNUNET_UNUSED,
1234 const struct GNUNET_SCHEDULER_TaskContext *tc)
1236 struct sockaddr_in6 addr;
1237 socklen_t addrlen = sizeof (addr);
1241 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1244 memset (&addr, 0, sizeof addr);
1247 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len))
1249 (void) open_port6 ();
1253 /* port the code above? */
1257 unsigned char buf[len];
1258 struct dns_pkt *dns = (struct dns_pkt *) buf;
1260 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1261 (struct sockaddr *) &addr, &addrlen);
1265 (void) open_port6 ();
1269 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1271 memcpy (addr_, &addr, sizeof addr);
1272 handle_response (dns, addr_, 4, r);
1274 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1275 &read_response6, NULL);
1279 * Read a response-packet of the UDP-Socket
1282 read_response (void *cls GNUNET_UNUSED,
1283 const struct GNUNET_SCHEDULER_TaskContext *tc)
1285 struct sockaddr_in addr;
1286 socklen_t addrlen = sizeof (addr);
1290 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1293 memset (&addr, 0, sizeof addr);
1296 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
1298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
1299 unhijack (dnsoutport);
1300 if (GNUNET_YES == open_port ())
1301 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1305 /* port the code above? */
1309 unsigned char buf[len];
1310 struct dns_pkt *dns = (struct dns_pkt *) buf;
1312 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1313 (struct sockaddr *) &addr, &addrlen);
1317 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1318 unhijack (dnsoutport);
1319 if (GNUNET_YES == open_port ())
1320 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1324 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1326 memcpy (addr_, &addr, sizeof addr);
1327 handle_response (dns, addr_, 4, r);
1329 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1330 &read_response, NULL);
1334 handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1341 if (query_states[dns->s.id].valid == GNUNET_YES)
1343 if (query_states[dns->s.id].tunnel != NULL)
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "Answer to query %d for a remote peer!\n", ntohs (dns->s.id));
1347 /* This response should go through a tunnel */
1349 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1351 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1353 *t = query_states[dns->s.id].tunnel;
1354 memcpy (t + 1, dns, r);
1355 struct tunnel_state *s =
1356 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel);
1360 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1362 GNUNET_TIME_UNIT_MINUTES, NULL,
1365 GNUNET_MessageHeader),
1366 mesh_send_response, c);
1370 struct tunnel_notify_queue *element =
1371 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1373 element->len = r + sizeof (struct GNUNET_MessageHeader);
1374 element->cb = mesh_send_response;
1376 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
1381 query_states[dns->s.id].valid = GNUNET_NO;
1383 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1384 struct answer_packet_list *answer =
1385 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
1386 (sizeof (struct answer_packet)));
1387 answer->pkt.hdr.type =
1388 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1389 answer->pkt.hdr.size = htons (len);
1390 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1391 answer->pkt.addrlen = addrlen;
1394 struct sockaddr_in6 *addr_ = (struct sockaddr_in6 *) addr;
1396 memcpy (answer->pkt.from, &addr_->sin6_addr, addrlen);
1397 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1399 else if (addrlen == 4)
1401 struct sockaddr_in *addr_ = (struct sockaddr_in *) addr;
1403 memcpy (answer->pkt.from, &addr_->sin_addr.s_addr, addrlen);
1404 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen);
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n",
1413 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1414 memcpy (answer->pkt.data, dns, r);
1415 answer->client = query_states[dns->s.id].client;
1417 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1419 if (server_notify == NULL)
1421 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1423 GNUNET_TIME_UNIT_FOREVER_REL,
1424 &send_answer, NULL);
1432 * Task run during shutdown.
1438 cleanup_task (void *cls GNUNET_UNUSED,
1439 const struct GNUNET_SCHEDULER_TaskContext *tc)
1441 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1443 unhijack (dnsoutport);
1444 GNUNET_DHT_disconnect (dht);
1445 GNUNET_MESH_disconnect (mesh_handle);
1449 * @brief Create a port-map from udp and tcp redirects
1451 * @param udp_redirects
1452 * @param tcp_redirects
1457 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1460 char *cpy, *hostname, *redirect;
1462 unsigned int count = 0;
1465 if (NULL != udp_redirects)
1467 cpy = GNUNET_strdup (udp_redirects);
1468 for (redirect = strtok (cpy, " "); redirect != NULL;
1469 redirect = strtok (NULL, " "))
1471 if (NULL == (hostname = strstr (redirect, ":")))
1473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1474 "Warning: option %s is not formatted correctly!\n",
1479 local_port = atoi (redirect);
1480 if (!((local_port > 0) && (local_port < 65536)))
1481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1482 "Warning: %s is not a correct port.", redirect);
1484 ret |= (0xFFFF & htons (local_port));
1498 if (NULL != tcp_redirects)
1500 cpy = GNUNET_strdup (tcp_redirects);
1501 for (redirect = strtok (cpy, " "); redirect != NULL;
1502 redirect = strtok (NULL, " "))
1504 if (NULL == (hostname = strstr (redirect, ":")))
1506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1507 "Warning: option %s is not formatted correctly!\n",
1512 local_port = atoi (redirect);
1513 if (!((local_port > 0) && (local_port < 65536)))
1514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1515 "Warning: %s is not a correct port.", redirect);
1517 ret |= (0xFFFF & htons (local_port));
1532 GNUNET_free_non_null (cpy);
1537 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1538 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1540 size_t size = sizeof (struct GNUNET_DNS_Record);
1541 struct GNUNET_DNS_Record data;
1543 memset (&data, 0, size);
1545 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1546 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1548 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1550 *((unsigned long long *) &data.service_descriptor));
1552 data.service_type = service_type;
1555 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1557 data.expiration_time =
1558 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute
1559 (GNUNET_TIME_relative_multiply
1560 (GNUNET_TIME_UNIT_HOURS, 2)));
1562 /* Sign the block */
1564 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
1566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1571 *((unsigned int *) &data.service_descriptor), size);
1573 GNUNET_DHT_put (dht, &data.service_descriptor,
1574 5 /* DEFAULT_PUT_REPLICATION */ ,
1575 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1577 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1578 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1583 * @brief Publishes the record defined by the section section
1585 * @param cls closure
1586 * @param section the current section
1589 publish_iterate (void *cls GNUNET_UNUSED, const char *section)
1591 char *udp_redirects;
1592 char *tcp_redirects;
1593 char *alternative_names;
1594 char *alternative_name;
1597 if ((strlen (section) < 8) ||
1598 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1602 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1604 udp_redirects = NULL;
1606 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1608 tcp_redirects = NULL;
1611 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1614 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1615 if (keyfile != NULL)
1616 GNUNET_free (keyfile);
1620 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1621 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1622 GNUNET_free (keyfile);
1623 GNUNET_assert (my_private_key != NULL);
1625 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1626 uint32_t service_type = 0;
1628 if (NULL != udp_redirects)
1629 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1631 if (NULL != tcp_redirects)
1632 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1634 service_type = htonl (service_type);
1637 publish_name (section, ports, service_type, my_private_key);
1639 GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES",
1640 &alternative_names))
1642 for (alternative_name = strtok (alternative_names, " ");
1643 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1646 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1647 strcpy (altname, alternative_name);
1648 strcpy (altname + strlen (alternative_name) + 1, section);
1649 altname[strlen (alternative_name)] = '.';
1651 publish_name (altname, ports, service_type, my_private_key);
1653 GNUNET_free (alternative_names);
1655 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1656 GNUNET_free_non_null (udp_redirects);
1657 GNUNET_free_non_null (tcp_redirects);
1661 * Publish a DNS-record in the DHT.
1664 publish_names (void *cls GNUNET_UNUSED,
1665 const struct GNUNET_SCHEDULER_TaskContext *tc)
1667 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1670 GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL);
1672 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL);
1676 * @param cls closure
1677 * @param server the initialized server
1678 * @param cfg_ configuration to use
1681 run (void *cls, struct GNUNET_SERVER_Handle *server,
1682 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1684 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1685 /* callback, cls, type, size */
1686 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0},
1687 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1688 sizeof (struct GNUNET_MessageHeader)},
1692 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1693 {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0},
1694 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0},
1698 static GNUNET_MESH_ApplicationType apptypes[] = {
1699 GNUNET_APPLICATION_TYPE_END,
1700 GNUNET_APPLICATION_TYPE_END
1703 if (GNUNET_YES != open_port6 ())
1705 GNUNET_SCHEDULER_shutdown ();
1709 if (GNUNET_YES != open_port ())
1711 GNUNET_SCHEDULER_shutdown ();
1716 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1717 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1719 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel,
1720 mesh_handlers, apptypes);
1723 dht = GNUNET_DHT_connect (cfg, 1024);
1724 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1725 GNUNET_SERVER_add_handlers (server, handlers);
1726 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1727 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1732 * The main function for the dns service.
1734 * @param argc number of arguments from the command line
1735 * @param argv command line arguments
1736 * @return 0 ok, 1 on error
1739 main (int argc, char *const *argv)
1741 return (GNUNET_OK ==
1742 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1743 &run, NULL)) ? 0 : 1;