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_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"
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;
53 static struct GNUNET_NETWORK_Handle *dnsout6;
56 * The port bound to the socket dnsout
58 static unsigned short dnsoutport;
61 * A handle to the DHT-Service
63 static struct GNUNET_DHT_Handle *dht;
66 * The configuration to use
68 static const struct GNUNET_CONFIGURATION_Handle *cfg;
71 * A list of DNS-Responses that have to be sent to the requesting client
73 static struct answer_packet_list *head;
76 * The tail of the list of DNS-responses
78 static struct answer_packet_list *tail;
81 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
82 * some information needed to handle this query
84 * It currently allocates at least
85 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
92 struct GNUNET_SERVER_Client *client;
93 struct GNUNET_MESH_Tunnel *tunnel;
101 } query_states[UINT16_MAX + 1];
104 * A struct used to give more than one value as
105 * closure to receive_dht
107 struct receive_dht_cls
110 struct GNUNET_DHT_GetHandle *handle;
113 struct tunnel_notify_queue
115 struct tunnel_notify_queue *next;
116 struct tunnel_notify_queue *prev;
119 GNUNET_CONNECTION_TransmitReadyNotify cb;
124 struct tunnel_notify_queue *head, *tail;
125 struct GNUNET_MESH_TransmitHandle *th;
129 send_answer (void *cls, size_t size, void *buf);
132 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
137 if (head->client == client)
139 GNUNET_CONNECTION_notify_transmit_ready_cancel (server_notify);
141 GNUNET_SERVER_notify_transmit_ready (head->next->client,
142 ntohs (head->next->pkt.hdr.size),
143 GNUNET_TIME_UNIT_FOREVER_REL,
147 struct answer_packet_list *element = head;
149 while (element != NULL)
151 if (element->client == client)
153 GNUNET_SERVER_client_drop (client);
154 GNUNET_CONTAINER_DLL_remove (head, tail, element);
155 struct answer_packet_list *t = element;
157 element = element->next;
161 element = element->next;
166 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
169 hijack (void *cls GNUNET_UNUSED, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 "Delaying the hijacking, port is still %d!\n", dnsoutport);
178 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
184 struct GNUNET_OS_Process *proc;
187 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "No entry 'VIRTDNS' in configuration!\n");
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
195 snprintf (port_s, 6, "%d", dnsoutport);
198 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
199 "gnunet-hijack-dns", port_s, virt_dns, NULL)))
201 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
202 GNUNET_OS_process_close (proc);
204 GNUNET_free (virt_dns);
208 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
209 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
210 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
212 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
221 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
224 GNUNET_free (tunnel_ctx);
228 * Delete the hijacking-routes
231 unhijack (unsigned short port)
235 struct GNUNET_OS_Process *proc;
238 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS", &virt_dns))
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "No entry 'VIRTDNS' in configuration!\n");
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
246 snprintf (port_s, 6, "%d", port);
249 GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-hijack-dns",
250 "gnunet-hijack-dns", "-d", port_s, virt_dns,
253 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
254 GNUNET_OS_process_close (proc);
256 GNUNET_free (virt_dns);
260 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
264 send_answer (void *cls, size_t size, void *buf)
266 server_notify = NULL;
267 struct answer_packet_list *query = head;
268 size_t len = ntohs (query->pkt.hdr.size);
270 GNUNET_assert (len <= size);
272 memcpy (buf, &query->pkt.hdr, len);
274 GNUNET_CONTAINER_DLL_remove (head, tail, query);
276 /* When more data is to be sent, reschedule */
279 GNUNET_SERVER_notify_transmit_ready (head->client,
280 ntohs (head->pkt.hdr.size),
281 GNUNET_TIME_UNIT_FOREVER_REL,
284 GNUNET_SERVER_client_drop (query->client);
289 GNUNET_NETWORK_STRUCT_BEGIN
293 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
294 struct GNUNET_MessageHeader hdr;
297 GNUNET_NETWORK_STRUCT_END
299 struct tunnel_cls *remote_pending[UINT16_MAX];
302 mesh_send_response (void *cls, size_t size, void *buf)
304 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
305 struct GNUNET_MessageHeader *hdr = buf;
307 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel **) (sz + 1);
308 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
310 GNUNET_MESH_tunnel_set_data (*tunnel, NULL);
312 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS);
313 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
317 *sz + sizeof (struct GNUNET_MessageHeader));
319 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
321 memcpy (hdr + 1, dns, *sz);
322 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (*tunnel);
326 struct tunnel_notify_queue *element = s->head;
327 struct tunnel_notify_queue *head = s->head;
328 struct tunnel_notify_queue *tail = s->tail;
330 GNUNET_CONTAINER_DLL_remove (head, tail, element);
333 GNUNET_MESH_notify_transmit_ready (*tunnel, GNUNET_NO, 42,
334 GNUNET_TIME_relative_divide
335 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
336 (const struct GNUNET_PeerIdentity *)
337 NULL, element->len, element->cb,
343 return ntohs (hdr->size);
347 mesh_send (void *cls, size_t size, void *buf)
349 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
351 GNUNET_MESH_tunnel_set_data (cls_->tunnel, NULL);
353 GNUNET_assert (cls_->hdr.size <= size);
355 size = cls_->hdr.size;
356 cls_->hdr.size = htons (cls_->hdr.size);
358 memcpy (buf, &cls_->hdr, size);
360 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
364 struct tunnel_notify_queue *element = s->head;
365 struct tunnel_notify_queue *head = s->head;
366 struct tunnel_notify_queue *tail = s->tail;;
368 GNUNET_CONTAINER_DLL_remove (head, tail, element);
371 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_NO, 42,
372 GNUNET_TIME_relative_divide
373 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
374 (const struct GNUNET_PeerIdentity *)
375 NULL, element->len, element->cb,
378 GNUNET_free (element);
386 mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
387 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
391 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Connected to peer %s, %x, sending query with id %d\n",
395 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
397 struct tunnel_state *s = GNUNET_MESH_tunnel_get_data (cls_->tunnel);
402 GNUNET_MESH_notify_transmit_ready (cls_->tunnel, GNUNET_YES, 42,
403 GNUNET_TIME_UNIT_MINUTES, NULL,
404 cls_->hdr.size, mesh_send, cls);
409 struct tunnel_notify_queue *head = s->head;
410 struct tunnel_notify_queue *tail = s->tail;
412 struct tunnel_notify_queue *element =
413 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
415 element->len = cls_->hdr.size;
416 element->cb = mesh_send;
418 GNUNET_CONTAINER_DLL_insert_tail (head, tail, element);
424 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
426 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
429 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
431 struct tunnel_state *s = GNUNET_malloc (sizeof *s);
438 GNUNET_MESH_tunnel_create (mesh_handle, s, mesh_connect, NULL, cls_);
440 GNUNET_MESH_peer_request_connect_by_type (cls_->tunnel,
441 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER);
443 remote_pending[cls_->dns.s.id] = cls_;
447 receive_mesh_query (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
448 void **ctx GNUNET_UNUSED,
449 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
450 const struct GNUNET_MessageHeader *message,
451 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
453 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
455 struct sockaddr_in dest;
457 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
459 memset (&dest, 0, sizeof dest);
460 dest.sin_port = htons (53);
464 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "EXTERNAL_DNS",
466 1 != inet_pton (AF_INET, dns_resolver, &dest.sin_addr))
467 inet_pton (AF_INET, "8.8.8.8", &dest.sin_addr);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n",
471 query_states[dns->s.id].tunnel = tunnel;
472 query_states[dns->s.id].valid = GNUNET_YES;
476 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
478 if (pdns->queries[i]->qtype == htons (28) ||
479 pdns->queries[i]->qtype == htons (1))
481 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
485 free_parsed_dns_packet (pdns);
487 GNUNET_NETWORK_socket_sendto (dnsout, dns,
488 ntohs (message->size) -
489 sizeof (struct GNUNET_MessageHeader),
490 (struct sockaddr *) &dest, sizeof dest);
492 return GNUNET_SYSERR;
496 receive_mesh_answer (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
497 void **ctx GNUNET_UNUSED,
498 const struct GNUNET_PeerIdentity *sender,
499 const struct GNUNET_MessageHeader *message,
500 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
502 /* TODo: size check */
503 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
505 /* They sent us a packet we were not waiting for */
506 if (remote_pending[dns->s.id] == NULL ||
507 remote_pending[dns->s.id]->tunnel != tunnel)
510 GNUNET_free (remote_pending[dns->s.id]);
511 remote_pending[dns->s.id] = NULL;
513 if (query_states[dns->s.id].valid != GNUNET_YES)
514 return GNUNET_SYSERR;
515 query_states[dns->s.id].valid = GNUNET_NO;
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Received answer from peer %s, dns-id %d\n", GNUNET_i2s (sender),
521 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 */
522 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
524 struct answer_packet_list *answer =
525 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
526 sizeof (struct answer_packet));
528 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
529 answer->pkt.hdr.size = htons (len);
531 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
533 if (ntohs (pdns->s.ancount) < 1)
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n",
536 ntohs (pdns->s.ancount));
537 free_parsed_dns_packet (pdns);
538 GNUNET_free (answer);
544 while (i < ntohs (pdns->s.ancount) && ntohs (pdns->answers[i]->type) != 28 &&
545 ntohs (pdns->answers[i]->type) != 1)
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer contains %d.\n",
548 ntohs (pdns->answers[i]->type));
552 if (i >= ntohs (pdns->s.ancount))
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Answer does not contain any usable answers.\n");
556 free_parsed_dns_packet (pdns);
557 GNUNET_free (answer);
561 answer->pkt.addrsize = ntohs (pdns->answers[i]->data_len);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n",
563 answer->pkt.addrsize);
564 memcpy (answer->pkt.addr, pdns->answers[i]->data,
565 ntohs (pdns->answers[i]->data_len));
567 memcpy (answer->pkt.from, query_states[dns->s.id].remote_ip,
568 query_states[dns->s.id].addrlen);
569 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip,
570 query_states[dns->s.id].addrlen);
571 answer->pkt.addrlen = query_states[dns->s.id].addrlen;
572 answer->pkt.dst_port = query_states[dns->s.id].local_port;
574 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
576 dpkt->s.id = dns->s.id;
580 dpkt->s.qdcount = htons (1);
581 dpkt->s.ancount = htons (1);
583 memcpy (dpkt->data, query_states[dns->s.id].name,
584 query_states[dns->s.id].namelen);
585 GNUNET_free (query_states[dns->s.id].name);
586 query_states[dns->s.id].name = NULL;
588 struct dns_query_line *dque =
589 (struct dns_query_line *) (dpkt->data +
590 (query_states[dns->s.id].namelen));
592 struct dns_record_line *drec_data =
593 (struct dns_record_line *) (dpkt->data +
594 (query_states[dns->s.id].namelen) +
595 sizeof (struct dns_query_line) + 2);
596 if (htons (28) == query_states[dns->s.id].qtype)
598 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
599 dque->type = htons (28); /* AAAA */
600 drec_data->type = htons (28); /* AAAA */
601 drec_data->data_len = htons (16);
603 else if (htons (1) == query_states[dns->s.id].qtype)
605 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
606 dque->type = htons (1); /* A */
607 drec_data->type = htons (1); /* A */
608 drec_data->data_len = htons (4);
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "dns-answer with pending qtype = %d\n",
613 query_states[dns->s.id].qtype);
616 dque->class = htons (1); /* IN */
619 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
620 sizeof (struct dns_query_line));
621 memcpy (anname, "\xc0\x0c", 2);
622 drec_data->class = htons (1); /* IN */
624 drec_data->ttl = pdns->answers[i]->ttl;
626 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
627 * filled in by the daemon-vpn */
628 answer->pkt.addroffset =
629 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
630 (unsigned long) (&answer->pkt)));
632 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
633 answer->client = query_states[dns->s.id].client;
635 if (server_notify == NULL)
637 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
638 len, GNUNET_TIME_UNIT_FOREVER_REL,
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Sent answer of length %d on to client, addroffset = %d\n", len,
643 answer->pkt.addroffset);
645 free_parsed_dns_packet (pdns);
651 send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
656 struct dns_pkt_parsed *pdns = (struct dns_pkt_parsed *) cls;
658 unsigned short id = pdns->s.id;
660 free_parsed_dns_packet (pdns);
662 if (query_states[id].valid != GNUNET_YES)
664 query_states[id].valid = GNUNET_NO;
666 GNUNET_assert (query_states[id].namelen == 74);
668 size_t len = sizeof (struct answer_packet) - 1 + sizeof (struct dns_static) + 74 /* this is the length of a reverse ipv6-lookup */
669 + sizeof (struct dns_query_line) + 2 /* To hold the pointer (as defined in RFC1035) to the name */
670 + sizeof (struct dns_record_line) - 1 -
671 2 /* We do not know the lenght of the answer yet */ ;
673 struct answer_packet_list *answer =
674 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
675 sizeof (struct answer_packet));
677 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
678 answer->pkt.hdr.size = htons (len);
679 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
681 memcpy (answer->pkt.from, query_states[id].remote_ip,
682 query_states[id].addrlen);
683 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
685 answer->pkt.dst_port = query_states[id].local_port;
687 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
693 dpkt->s.qdcount = htons (1);
694 dpkt->s.ancount = htons (1);
696 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
697 GNUNET_free (query_states[id].name);
698 query_states[id].name = NULL;
700 struct dns_query_line *dque =
701 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
702 dque->type = htons (12); /* PTR */
703 dque->class = htons (1); /* IN */
706 (char *) (dpkt->data + (query_states[id].namelen) +
707 sizeof (struct dns_query_line));
708 memcpy (anname, "\xc0\x0c", 2);
710 struct dns_record_line *drec_data =
711 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
712 sizeof (struct dns_query_line) + 2);
713 drec_data->type = htons (12); /* AAAA */
714 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);
722 /* Calculate at which offset in the packet the length of the name and the
723 * name, it is filled in by the daemon-vpn */
724 answer->pkt.addroffset =
725 htons ((unsigned short) ((unsigned long) (&drec_data->data_len) -
726 (unsigned long) (&answer->pkt)));
728 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
729 answer->client = query_states[id].client;
731 if (server_notify == NULL)
733 GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len,
734 GNUNET_TIME_UNIT_FOREVER_REL,
739 * Receive a block from the dht.
742 receive_dht (void *cls, struct GNUNET_TIME_Absolute exp GNUNET_UNUSED,
743 const GNUNET_HashCode * key GNUNET_UNUSED,
744 const struct GNUNET_PeerIdentity *get_path GNUNET_UNUSED,
745 unsigned int get_path_length GNUNET_UNUSED,
746 const struct GNUNET_PeerIdentity *put_path GNUNET_UNUSED,
747 unsigned int put_path_length GNUNET_UNUSED,
748 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
751 unsigned short id = ((struct receive_dht_cls *) cls)->id;
752 struct GNUNET_DHT_GetHandle *handle =
753 ((struct receive_dht_cls *) cls)->handle;
756 GNUNET_DHT_get_stop (handle);
758 GNUNET_assert (type == GNUNET_BLOCK_TYPE_DNS);
760 /* If no query with this id is pending, ignore the block */
761 if (query_states[id].valid != GNUNET_YES)
763 query_states[id].valid = GNUNET_NO;
765 const struct GNUNET_DNS_Record *rec = data;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Got block of size %d, peer: %08x, desc: %08x\n", size,
769 *((unsigned int *) &rec->peer),
770 *((unsigned int *) &rec->service_descriptor));
772 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 */
773 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
775 struct answer_packet_list *answer =
776 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
777 sizeof (struct answer_packet));
779 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
780 answer->pkt.hdr.size = htons (len);
781 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
782 answer->client = query_states[id].client;
784 GNUNET_CRYPTO_hash (&rec->peer,
785 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
786 &answer->pkt.service_descr.peer);
788 memcpy (&answer->pkt.service_descr.service_descriptor,
789 &rec->service_descriptor, sizeof (GNUNET_HashCode));
790 memcpy (&answer->pkt.service_descr.service_type, &rec->service_type,
791 sizeof (answer->pkt.service_descr.service_type));
792 memcpy (&answer->pkt.service_descr.ports, &rec->ports,
793 sizeof (answer->pkt.service_descr.ports));
795 memcpy (answer->pkt.from, query_states[id].remote_ip,
796 query_states[id].addrlen);
797 memcpy (answer->pkt.to, query_states[id].local_ip, query_states[id].addrlen);
798 answer->pkt.addrlen = query_states[id].addrlen;
800 answer->pkt.dst_port = query_states[id].local_port;
802 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
808 dpkt->s.qdcount = htons (1);
809 dpkt->s.ancount = htons (1);
811 memcpy (dpkt->data, query_states[id].name, query_states[id].namelen);
812 GNUNET_free (query_states[id].name);
813 query_states[id].name = NULL;
815 struct dns_query_line *dque =
816 (struct dns_query_line *) (dpkt->data + (query_states[id].namelen));
817 dque->type = htons (28); /* AAAA */
818 dque->class = htons (1); /* IN */
821 (char *) (dpkt->data + (query_states[id].namelen) +
822 sizeof (struct dns_query_line));
823 memcpy (anname, "\xc0\x0c", 2);
825 struct dns_record_line *drec_data =
826 (struct dns_record_line *) (dpkt->data + (query_states[id].namelen) +
827 sizeof (struct dns_query_line) + 2);
828 drec_data->type = htons (28); /* AAAA */
829 drec_data->class = htons (1); /* IN */
831 /* FIXME: read the TTL from block:
832 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
834 * But how to get the seconds out of this?
836 drec_data->ttl = htonl (3600);
837 drec_data->data_len = htons (16);
839 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
840 * filled in by the daemon-vpn */
841 answer->pkt.addroffset =
842 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
843 (unsigned long) (&answer->pkt)));
845 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
847 if (server_notify == NULL)
849 GNUNET_SERVER_notify_transmit_ready (answer->client, len,
850 GNUNET_TIME_UNIT_FOREVER_REL,
855 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
858 rehijack (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
859 const struct GNUNET_MessageHeader *message GNUNET_UNUSED)
861 unhijack (dnsoutport);
862 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
868 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
871 receive_query (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
872 const struct GNUNET_MessageHeader *message)
874 struct query_packet *pkt = (struct query_packet *) message;
875 struct dns_pkt *dns = (struct dns_pkt *) pkt->data;
876 struct dns_pkt_parsed *pdns = parse_dns_packet (dns);
878 query_states[dns->s.id].valid = GNUNET_YES;
879 query_states[dns->s.id].client = client;
880 GNUNET_SERVER_client_keep (client);
881 memcpy (query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen);
882 query_states[dns->s.id].addrlen = pkt->addrlen;
883 query_states[dns->s.id].local_port = pkt->src_port;
884 memcpy (query_states[dns->s.id].remote_ip, pkt->orig_to, pkt->addrlen);
885 query_states[dns->s.id].namelen = strlen ((char *) dns->data) + 1;
886 if (query_states[dns->s.id].name != NULL)
887 GNUNET_free (query_states[dns->s.id].name);
888 query_states[dns->s.id].name =
889 GNUNET_malloc (query_states[dns->s.id].namelen);
890 memcpy (query_states[dns->s.id].name, dns->data,
891 query_states[dns->s.id].namelen);
895 for (i = 0; i < ntohs (pdns->s.qdcount); i++)
897 if (pdns->queries[i]->qtype == htons (28) ||
898 pdns->queries[i]->qtype == htons (1))
900 query_states[dns->s.id].qtype = pdns->queries[i]->qtype;
905 /* The query is for a .gnunet-address */
906 if (pdns->queries[0]->namelen > 9 &&
907 0 == strncmp (pdns->queries[0]->name + (pdns->queries[0]->namelen - 9),
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
913 GNUNET_CRYPTO_hash (pdns->queries[0]->name, pdns->queries[0]->namelen,
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Getting with key %08x, len is %d\n",
917 *((unsigned int *) &key), pdns->queries[0]->namelen);
919 struct receive_dht_cls *cls =
920 GNUNET_malloc (sizeof (struct receive_dht_cls));
924 GNUNET_DHT_get_start (dht, GNUNET_TIME_UNIT_MINUTES,
925 GNUNET_BLOCK_TYPE_DNS, &key,
926 5 /* DEFAULT_GET_REPLICATION */ ,
927 GNUNET_DHT_RO_NONE, NULL, 0, &receive_dht, cls);
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n",
933 pdns->queries[0]->name, pdns->queries[0]->namelen);
935 /* This is a PTR-Query. Check if it is for "our" network */
936 if (htons (pdns->queries[0]->qtype) == 12 && 74 == pdns->queries[0]->namelen)
941 "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.";
943 unsigned long long ipv6prefix;
944 unsigned int comparelen;
946 GNUNET_assert (GNUNET_OK ==
947 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn",
950 inet_pton (AF_INET6, ipv6addr, ipv6);
951 GNUNET_free (ipv6addr);
953 GNUNET_assert (GNUNET_OK ==
954 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
957 GNUNET_assert (ipv6prefix < 127);
958 ipv6prefix = (ipv6prefix + 7) / 8;
960 for (i = ipv6prefix; i < 16; i++)
963 for (i = 0; i < 16; i++)
965 unsigned char c1 = ipv6[i] >> 4;
966 unsigned char c2 = ipv6[i] & 0xf;
969 ipv6rev[62 - (4 * i)] = c1 + '0';
971 ipv6rev[62 - (4 * i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
974 ipv6rev[62 - ((4 * i) + 2)] = c2 + '0';
976 ipv6rev[62 - ((4 * i) + 2)] = c2 + 87;
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
979 comparelen = 10 + 4 * ipv6prefix;
981 strncmp (pdns->queries[0]->name +
982 (pdns->queries[0]->namelen - comparelen),
983 ipv6rev + (74 - comparelen), comparelen))
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
987 GNUNET_SCHEDULER_add_now (send_rev_query, pdns);
993 unsigned char virt_dns_bytes[16];
995 if (pkt->addrlen == 4)
1000 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
1003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1004 "No entry 'VIRTDNS' in configuration!\n");
1008 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
1010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing 'VIRTDNS': %s; %m!\n",
1015 GNUNET_free (virt_dns);
1017 else if (pkt->addrlen == 16)
1021 if (GNUNET_SYSERR ==
1022 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS6",
1025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1026 "No entry 'VIRTDNS6' in configuration!\n");
1030 if (1 != inet_pton (AF_INET6, virt_dns, &virt_dns_bytes))
1032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1033 "Error parsing 'VIRTDNS6': %s; %m!\n", virt_dns);
1037 GNUNET_free (virt_dns);
1044 if (memcmp (virt_dns_bytes, pkt->orig_to, pkt->addrlen) == 0)
1046 /* This is a packet that was sent directly to the virtual dns-server
1048 * This means we have to send this query over gnunet
1052 sizeof (struct GNUNET_MESH_Tunnel *) +
1053 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1054 sizeof (struct query_packet) +
1056 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1058 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1060 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1063 memcpy (&cls_->dns, dns,
1064 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1065 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1067 if (ntohs (pdns->s.qdcount) == 1)
1069 if (ntohs (pdns->queries[0]->qtype) == 1)
1070 pdns->queries[0]->qtype = htons (28);
1071 else if (ntohs (pdns->queries[0]->qtype) == 28)
1072 pdns->queries[0]->qtype = htons (1);
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "not sending second packet\n");
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending second packet\n");
1079 struct dns_pkt *rdns = unparse_dns_packet (pdns);
1081 sizeof (struct GNUNET_MESH_Tunnel *) +
1082 sizeof (struct GNUNET_MessageHeader) + (ntohs (message->size) -
1083 sizeof (struct query_packet) +
1085 struct tunnel_cls *cls_ = GNUNET_malloc (size);
1087 cls_->hdr.size = size - sizeof (struct GNUNET_MESH_Tunnel *);
1089 cls_->hdr.type = ntohs (GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS);
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
1092 memcpy (&cls_->dns, rdns,
1093 cls_->hdr.size - sizeof (struct GNUNET_MessageHeader));
1094 GNUNET_SCHEDULER_add_now (send_mesh_query, cls_);
1102 /* The query should be sent to the network */
1103 if (pkt->addrlen == 4)
1105 struct sockaddr_in dest;
1107 memset (&dest, 0, sizeof dest);
1108 dest.sin_port = htons (53);
1109 memcpy (&dest.sin_addr.s_addr, pkt->orig_to, pkt->addrlen);
1111 GNUNET_NETWORK_socket_sendto (dnsout, dns,
1112 ntohs (pkt->hdr.size) -
1113 sizeof (struct query_packet) + 1,
1114 (struct sockaddr *) &dest, sizeof dest);
1116 else if (pkt->addrlen == 16)
1118 struct sockaddr_in6 dest;
1120 memset (&dest, 0, sizeof dest);
1121 dest.sin6_port = htons (53);
1122 memcpy (&dest.sin6_addr, pkt->orig_to, pkt->addrlen);
1124 GNUNET_NETWORK_socket_sendto (dnsout6, dns,
1125 ntohs (pkt->hdr.size) -
1126 sizeof (struct query_packet) + 1,
1127 (struct sockaddr *) &dest, sizeof dest);
1131 free_parsed_dns_packet (pdns);
1134 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1138 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1141 read_response6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1146 struct sockaddr_in6 addr;
1148 dnsout6 = GNUNET_NETWORK_socket_create (AF_INET6, SOCK_DGRAM, 0);
1149 if (dnsout6 == NULL)
1151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create socket: %m\n");
1152 return GNUNET_SYSERR;
1154 memset (&addr, 0, sizeof (struct sockaddr_in6));
1156 addr.sin6_family = AF_INET6;
1157 int err = GNUNET_NETWORK_socket_bind (dnsout6,
1158 (struct sockaddr *) &addr,
1159 sizeof (struct sockaddr_in6));
1161 if (err != GNUNET_OK)
1163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1164 return GNUNET_SYSERR;
1167 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1168 &read_response6, NULL);
1176 struct sockaddr_in addr;
1178 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
1180 return GNUNET_SYSERR;
1181 memset (&addr, 0, sizeof (struct sockaddr_in));
1183 addr.sin_family = AF_INET;
1184 int err = GNUNET_NETWORK_socket_bind (dnsout,
1185 (struct sockaddr *) &addr,
1186 sizeof (struct sockaddr_in));
1188 if (err != GNUNET_OK)
1190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not bind a port: %m\n");
1191 return GNUNET_SYSERR;
1194 /* Read the port we bound to */
1195 socklen_t addrlen = sizeof (struct sockaddr_in);
1198 getsockname (GNUNET_NETWORK_get_fd (dnsout), (struct sockaddr *) &addr,
1201 dnsoutport = htons (addr.sin_port);
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
1205 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1206 &read_response, NULL);
1212 handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1216 * Read a response-packet of the UDP-Socket
1219 read_response6 (void *cls GNUNET_UNUSED,
1220 const struct GNUNET_SCHEDULER_TaskContext *tc)
1222 struct sockaddr_in6 addr;
1223 socklen_t addrlen = sizeof (addr);
1227 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1230 memset (&addr, 0, sizeof addr);
1233 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout6), FIONREAD, &len))
1235 (void) open_port6 ();
1239 /* port the code above? */
1243 unsigned char buf[len];
1244 struct dns_pkt *dns = (struct dns_pkt *) buf;
1246 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1247 (struct sockaddr *) &addr, &addrlen);
1251 (void) open_port6 ();
1255 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1257 memcpy (addr_, &addr, sizeof addr);
1258 handle_response (dns, addr_, 4, r);
1260 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout6,
1261 &read_response6, NULL);
1265 * Read a response-packet of the UDP-Socket
1268 read_response (void *cls GNUNET_UNUSED,
1269 const struct GNUNET_SCHEDULER_TaskContext *tc)
1271 struct sockaddr_in addr;
1272 socklen_t addrlen = sizeof (addr);
1276 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
1279 memset (&addr, 0, sizeof addr);
1282 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
1284 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "ioctl");
1285 unhijack (dnsoutport);
1286 if (GNUNET_YES == open_port ())
1287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1291 /* port the code above? */
1295 unsigned char buf[len];
1296 struct dns_pkt *dns = (struct dns_pkt *) buf;
1298 r = GNUNET_NETWORK_socket_recvfrom (dnsout, buf, sizeof (buf),
1299 (struct sockaddr *) &addr, &addrlen);
1303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "recvfrom");
1304 unhijack (dnsoutport);
1305 if (GNUNET_YES == open_port ())
1306 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &hijack, NULL);
1310 struct sockaddr *addr_ = GNUNET_malloc (sizeof addr);
1312 memcpy (addr_, &addr, sizeof addr);
1313 handle_response (dns, addr_, 4, r);
1315 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
1316 &read_response, NULL);
1320 handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen,
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n",
1327 if (query_states[dns->s.id].valid == GNUNET_YES)
1329 if (query_states[dns->s.id].tunnel != NULL)
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Answer to query %d for a remote peer!\n", ntohs (dns->s.id));
1333 /* This response should go through a tunnel */
1335 GNUNET_malloc (4 + sizeof (struct GNUNET_MESH_Tunnel *) + r);
1337 struct GNUNET_MESH_Tunnel **t = (struct GNUNET_MESH_Tunnel **) (c + 1);
1339 *t = query_states[dns->s.id].tunnel;
1340 memcpy (t + 1, dns, r);
1341 struct tunnel_state *s =
1342 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel);
1346 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
1348 GNUNET_TIME_UNIT_MINUTES, NULL,
1351 GNUNET_MessageHeader),
1352 mesh_send_response, c);
1356 struct tunnel_notify_queue *element =
1357 GNUNET_malloc (sizeof (struct tunnel_notify_queue));
1359 element->len = r + sizeof (struct GNUNET_MessageHeader);
1360 element->cb = mesh_send_response;
1362 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, element);
1367 query_states[dns->s.id].valid = GNUNET_NO;
1369 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1370 struct answer_packet_list *answer =
1371 GNUNET_malloc (len + sizeof (struct answer_packet_list) -
1372 (sizeof (struct answer_packet)));
1373 answer->pkt.hdr.type =
1374 htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS);
1375 answer->pkt.hdr.size = htons (len);
1376 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1377 answer->pkt.addrlen = addrlen;
1380 struct sockaddr_in6 *addr_ = (struct sockaddr_in6 *) addr;
1382 memcpy (answer->pkt.from, &addr_->sin6_addr, addrlen);
1383 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1385 else if (addrlen == 4)
1387 struct sockaddr_in *addr_ = (struct sockaddr_in *) addr;
1389 memcpy (answer->pkt.from, &addr_->sin_addr.s_addr, addrlen);
1390 memcpy (answer->pkt.to, query_states[dns->s.id].local_ip, addrlen);
1394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "addrlen = %d\n", addrlen);
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n",
1399 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1400 memcpy (answer->pkt.data, dns, r);
1401 answer->client = query_states[dns->s.id].client;
1403 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1405 if (server_notify == NULL)
1407 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
1409 GNUNET_TIME_UNIT_FOREVER_REL,
1410 &send_answer, NULL);
1418 * Task run during shutdown.
1424 cleanup_task (void *cls GNUNET_UNUSED,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc)
1427 GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1429 unhijack (dnsoutport);
1430 GNUNET_DHT_disconnect (dht);
1431 GNUNET_MESH_disconnect (mesh_handle);
1435 * @brief Create a port-map from udp and tcp redirects
1437 * @param udp_redirects
1438 * @param tcp_redirects
1443 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1446 char *cpy, *hostname, *redirect;
1448 unsigned int count = 0;
1451 if (NULL != udp_redirects)
1453 cpy = GNUNET_strdup (udp_redirects);
1454 for (redirect = strtok (cpy, " "); redirect != NULL;
1455 redirect = strtok (NULL, " "))
1457 if (NULL == (hostname = strstr (redirect, ":")))
1459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1460 "Warning: option %s is not formatted correctly!\n",
1465 local_port = atoi (redirect);
1466 if (!((local_port > 0) && (local_port < 65536)))
1467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1468 "Warning: %s is not a correct port.", redirect);
1470 ret |= (0xFFFF & htons (local_port));
1484 if (NULL != tcp_redirects)
1486 cpy = GNUNET_strdup (tcp_redirects);
1487 for (redirect = strtok (cpy, " "); redirect != NULL;
1488 redirect = strtok (NULL, " "))
1490 if (NULL == (hostname = strstr (redirect, ":")))
1492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1493 "Warning: option %s is not formatted correctly!\n",
1498 local_port = atoi (redirect);
1499 if (!((local_port > 0) && (local_port < 65536)))
1500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1501 "Warning: %s is not a correct port.", redirect);
1503 ret |= (0xFFFF & htons (local_port));
1518 GNUNET_free_non_null (cpy);
1523 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1524 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1526 size_t size = sizeof (struct GNUNET_DNS_Record);
1527 struct GNUNET_DNS_Record data;
1529 memset (&data, 0, size);
1531 data.purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1532 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1534 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1536 *((unsigned long long *) &data.service_descriptor));
1538 data.service_type = service_type;
1541 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1543 data.expiration_time =
1544 GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute
1545 (GNUNET_TIME_relative_multiply
1546 (GNUNET_TIME_UNIT_HOURS, 2)));
1548 /* Sign the block */
1550 GNUNET_CRYPTO_rsa_sign (my_private_key, &data.purpose, &data.signature))
1552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Putting with key %08x, size = %d\n",
1557 *((unsigned int *) &data.service_descriptor), size);
1559 GNUNET_DHT_put (dht, &data.service_descriptor,
1560 5 /* DEFAULT_PUT_REPLICATION */ ,
1561 GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_DNS, size,
1563 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1564 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1569 * @brief Publishes the record defined by the section section
1571 * @param cls closure
1572 * @param section the current section
1575 publish_iterate (void *cls GNUNET_UNUSED, const char *section)
1577 char *udp_redirects;
1578 char *tcp_redirects;
1579 char *alternative_names;
1580 char *alternative_name;
1583 if ((strlen (section) < 8) ||
1584 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1588 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
1590 udp_redirects = NULL;
1592 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1594 tcp_redirects = NULL;
1597 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
1600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1601 if (keyfile != NULL)
1602 GNUNET_free (keyfile);
1606 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1607 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1608 GNUNET_free (keyfile);
1609 GNUNET_assert (my_private_key != NULL);
1611 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1612 uint32_t service_type = 0;
1614 if (NULL != udp_redirects)
1615 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1617 if (NULL != tcp_redirects)
1618 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1620 service_type = htonl (service_type);
1623 publish_name (section, ports, service_type, my_private_key);
1625 GNUNET_CONFIGURATION_get_value_string (cfg, section, "ALTERNATIVE_NAMES",
1626 &alternative_names))
1628 for (alternative_name = strtok (alternative_names, " ");
1629 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1632 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1633 strcpy (altname, alternative_name);
1634 strcpy (altname + strlen (alternative_name) + 1, section);
1635 altname[strlen (alternative_name)] = '.';
1637 publish_name (altname, ports, service_type, my_private_key);
1639 GNUNET_free (alternative_names);
1641 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1642 GNUNET_free_non_null (udp_redirects);
1643 GNUNET_free_non_null (tcp_redirects);
1647 * Publish a DNS-record in the DHT.
1650 publish_names (void *cls GNUNET_UNUSED,
1651 const struct GNUNET_SCHEDULER_TaskContext *tc)
1653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1656 GNUNET_CONFIGURATION_iterate_sections (cfg, &publish_iterate, NULL);
1658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS, &publish_names, NULL);
1662 * @param cls closure
1663 * @param server the initialized server
1664 * @param cfg_ configuration to use
1667 run (void *cls, struct GNUNET_SERVER_Handle *server,
1668 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1670 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1671 /* callback, cls, type, size */
1672 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS, 0},
1673 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1674 sizeof (struct GNUNET_MessageHeader)},
1678 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1679 {receive_mesh_query, GNUNET_MESSAGE_TYPE_VPN_REMOTE_QUERY_DNS, 0},
1680 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_VPN_REMOTE_ANSWER_DNS, 0},
1684 static GNUNET_MESH_ApplicationType apptypes[] = {
1685 GNUNET_APPLICATION_TYPE_END,
1686 GNUNET_APPLICATION_TYPE_END
1689 if (GNUNET_YES != open_port6 ())
1691 GNUNET_SCHEDULER_shutdown ();
1695 if (GNUNET_YES != open_port ())
1697 GNUNET_SCHEDULER_shutdown ();
1702 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1703 apptypes[0] = GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER;
1705 GNUNET_MESH_connect (cfg_, 42, NULL, new_tunnel, clean_tunnel,
1706 mesh_handlers, apptypes);
1709 dht = GNUNET_DHT_connect (cfg, 1024);
1710 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1711 GNUNET_SERVER_add_handlers (server, handlers);
1712 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
1713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
1718 * The main function for the dns service.
1720 * @param argc number of arguments from the command line
1721 * @param argv command line arguments
1722 * @return 0 ok, 1 on error
1725 main (int argc, char *const *argv)
1727 return (GNUNET_OK ==
1728 GNUNET_SERVICE_run (argc, argv, "dns", GNUNET_SERVICE_OPTION_NONE,
1729 &run, NULL)) ? 0 : 1;