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
88 struct GNUNET_SERVER_Client* client;
89 struct GNUNET_MESH_Tunnel *tunnel;
95 } query_states[UINT16_MAX];
98 * A struct used to give more than one value as
99 * closure to receive_dht
101 struct receive_dht_cls {
103 struct GNUNET_DHT_GetHandle* handle;
106 struct tunnel_notify_queue
108 struct tunnel_notify_queue* next;
109 struct tunnel_notify_queue* prev;
112 GNUNET_CONNECTION_TransmitReadyNotify cb;
116 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
119 hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
121 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
126 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Delaying the hijacking, port is still %d!\n", dnsoutport);
127 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
133 struct GNUNET_OS_Process *proc;
136 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140 "No entry 'VIRTDNS' in configuration!\n");
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
145 snprintf (port_s, 6, "%d", dnsoutport);
146 if (NULL != (proc = GNUNET_OS_start_process (NULL,
148 "gnunet-helper-hijack-dns",
150 port_s, virt_dns, NULL)))
151 GNUNET_OS_process_close (proc);
152 GNUNET_free (virt_dns);
156 * Delete the hijacking-routes
159 unhijack (unsigned short port)
163 struct GNUNET_OS_Process *proc;
166 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "No entry 'VIRTDNS' in configuration!\n");
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
175 snprintf (port_s, 6, "%d", port);
176 if (NULL != (proc = GNUNET_OS_start_process (NULL,
178 "gnunet-helper-hijack-dns",
180 "-d", port_s, virt_dns, NULL)))
181 GNUNET_OS_process_close (proc);
182 GNUNET_free (virt_dns);
186 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
190 send_answer(void* cls, size_t size, void* buf) {
191 struct answer_packet_list* query = head;
192 size_t len = ntohs(query->pkt.hdr.size);
194 GNUNET_assert(len <= size);
196 memcpy(buf, &query->pkt.hdr, len);
198 GNUNET_CONTAINER_DLL_remove (head, tail, query);
202 /* When more data is to be sent, reschedule */
204 GNUNET_SERVER_notify_transmit_ready(cls,
205 ntohs(head->pkt.hdr.size),
206 GNUNET_TIME_UNIT_FOREVER_REL,
214 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
215 struct GNUNET_MessageHeader hdr;
219 struct tunnel_cls *remote_pending[UINT16_MAX];
222 mesh_send_response (void *cls, size_t size, void *buf)
224 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
225 struct GNUNET_MessageHeader *hdr = buf;
227 struct GNUNET_MESH_Tunnel **tunnel = (struct GNUNET_MESH_Tunnel**)(sz+1);
228 struct dns_pkt *dns = (struct dns_pkt *) (tunnel + 1);
229 hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
230 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
234 *sz + sizeof (struct GNUNET_MessageHeader));
236 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
238 memcpy (hdr + 1, dns, *sz);
240 if (NULL != GNUNET_MESH_tunnel_get_head(*tunnel))
242 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(*tunnel);
243 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(*tunnel);
244 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(*tunnel);
246 GNUNET_CONTAINER_DLL_remove(head, tail, element);
248 GNUNET_MESH_tunnel_set_head(*tunnel, head);
249 GNUNET_MESH_tunnel_set_tail(*tunnel, tail);
250 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (*tunnel,
253 GNUNET_TIME_relative_divide
254 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
255 (const struct GNUNET_PeerIdentity *)
257 element->cb, element->cls);
258 /* save the handle */
259 GNUNET_MESH_tunnel_set_data(*tunnel, th);
264 return ntohs (hdr->size);
268 mesh_send (void *cls, size_t size, void *buf)
270 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
271 GNUNET_MESH_tunnel_set_data(cls_->tunnel, NULL);
273 GNUNET_assert(cls_->hdr.size <= size);
275 size = cls_->hdr.size;
276 cls_->hdr.size = htons(cls_->hdr.size);
278 memcpy(buf, &cls_->hdr, size);
280 if (NULL != GNUNET_MESH_tunnel_get_head(cls_->tunnel))
282 struct tunnel_notify_queue* element = GNUNET_MESH_tunnel_get_head(cls_->tunnel);
283 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(cls_->tunnel);
284 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(cls_->tunnel);
286 GNUNET_CONTAINER_DLL_remove(head, tail, element);
288 GNUNET_MESH_tunnel_set_head(cls_->tunnel, head);
289 GNUNET_MESH_tunnel_set_tail(cls_->tunnel, tail);
290 struct GNUNET_MESH_TransmitHandle* th = GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
293 GNUNET_TIME_relative_divide
294 (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
295 (const struct GNUNET_PeerIdentity *)
297 element->cb, element->cls);
298 /* save the handle */
299 GNUNET_MESH_tunnel_set_data(cls_->tunnel, th);
300 GNUNET_free(element);
308 mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
309 const struct GNUNET_TRANSPORT_ATS_Information *atsi
310 __attribute__ ((unused)))
314 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Connected to peer %s, %x, sending query with id %d\n",
317 GNUNET_i2s (peer), peer, ntohs (cls_->dns.s.id));
319 if (NULL == GNUNET_MESH_tunnel_get_data (cls_->tunnel))
321 struct GNUNET_MESH_TransmitHandle *th =
322 GNUNET_MESH_notify_transmit_ready (cls_->tunnel,
325 GNUNET_TIME_UNIT_MINUTES,
331 GNUNET_MESH_tunnel_set_data (cls_->tunnel, th);
335 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(cls_->tunnel);
336 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(cls_->tunnel);
338 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
340 element->len = cls_->hdr.size;
341 element->cb = mesh_send;
343 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
344 GNUNET_MESH_tunnel_set_head(cls_->tunnel, head);
345 GNUNET_MESH_tunnel_set_tail(cls_->tunnel, tail);
351 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
356 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
358 cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
359 GNUNET_TIME_UNIT_HOURS,
360 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
365 remote_pending[cls_->dns.s.id] = cls_;
369 receive_mesh_query (void *cls __attribute__((unused)),
370 struct GNUNET_MESH_Tunnel *tunnel,
371 void **ctx __attribute__((unused)),
372 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
373 const struct GNUNET_MessageHeader *message,
374 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
376 struct dns_pkt *dns = (struct dns_pkt*)(message + 1);
378 struct sockaddr_in dest;
379 memset(&dest, 0, sizeof dest);
380 dest.sin_port = htons(53);
381 /* TODO: read from config */
382 inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Querying for remote, id=%d\n", ntohs(dns->s.id));
385 query_states[dns->s.id].tunnel = tunnel;
386 query_states[dns->s.id].valid = GNUNET_YES;
388 GNUNET_NETWORK_socket_sendto(dnsout,
390 ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
391 (struct sockaddr*) &dest,
394 return GNUNET_SYSERR;
398 receive_mesh_answer (void *cls __attribute__((unused)),
399 struct GNUNET_MESH_Tunnel *tunnel,
400 void **ctx __attribute__((unused)),
401 const struct GNUNET_PeerIdentity *sender,
402 const struct GNUNET_MessageHeader *message,
403 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
405 /* TODo: size check */
406 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
408 /* They sent us a packet we were not waiting for */
409 if (remote_pending[dns->s.id] == NULL
410 || remote_pending[dns->s.id]->tunnel != tunnel)
413 GNUNET_free(remote_pending[dns->s.id]);
414 remote_pending[dns->s.id] = NULL;
416 if (query_states[dns->s.id].valid != GNUNET_YES)
417 return GNUNET_SYSERR;
418 query_states[dns->s.id].valid = GNUNET_NO;
420 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id));
422 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 */
423 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
425 struct answer_packet_list *answer =
426 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
427 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
429 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
430 answer->pkt.hdr.size = htons (len);
432 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
434 if (ntohs(pdns->s.ancount) < 1)
436 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount));
437 free_parsed_dns_packet(pdns);
441 answer->pkt.addrsize = ntohs(pdns->answers[0]->data_len);
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n", answer->pkt.addrsize);
443 memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
445 answer->pkt.from = query_states[dns->s.id].remote_ip;
447 answer->pkt.to = query_states[dns->s.id].local_ip;
448 answer->pkt.dst_port = query_states[dns->s.id].local_port;
450 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
452 dpkt->s.id = dns->s.id;
456 dpkt->s.qdcount = htons (1);
457 dpkt->s.ancount = htons (1);
459 memcpy (dpkt->data, query_states[dns->s.id].name,
460 query_states[dns->s.id].namelen);
461 GNUNET_free (query_states[dns->s.id].name);
462 query_states[dns->s.id].name = NULL;
464 struct dns_query_line *dque =
465 (struct dns_query_line *) (dpkt->data +
466 (query_states[dns->s.id].namelen));
468 struct dns_record_line *drec_data =
469 (struct dns_record_line *) (dpkt->data +
470 (query_states[dns->s.id].namelen) +
471 sizeof (struct dns_query_line) + 2);
472 if (16 == answer->pkt.addrsize)
474 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA;
475 dque->type = htons (28); /* AAAA */
476 drec_data->type = htons (28); /* AAAA */
477 drec_data->data_len = htons (16);
481 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE_A;
482 dque->type = htons (1); /* A */
483 drec_data->type = htons (1); /* A*/
484 drec_data->data_len = htons (4);
486 dque->class = htons (1); /* IN */
489 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
490 sizeof (struct dns_query_line));
491 memcpy (anname, "\xc0\x0c", 2);
492 drec_data->class = htons (1); /* IN */
494 drec_data->ttl = pdns->answers[0]->ttl;
496 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
497 * filled in by the daemon-vpn */
498 answer->pkt.addroffset =
499 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
500 (unsigned long) (&answer->pkt)));
502 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
504 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
506 GNUNET_TIME_UNIT_FOREVER_REL,
508 query_states[dns->s.id].client);
510 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer of length %d on to client, addroffset = %d\n", len, answer->pkt.addroffset);
512 free_parsed_dns_packet(pdns);
518 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
519 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
522 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
524 unsigned short id = pdns->s.id;
526 free_parsed_dns_packet(pdns);
528 if (query_states[id].valid != GNUNET_YES) return;
529 query_states[id].valid = GNUNET_NO;
531 GNUNET_assert(query_states[id].namelen == 74);
533 size_t len = sizeof(struct answer_packet) - 1 \
534 + sizeof(struct dns_static) \
535 + 74 /* this is the length of a reverse ipv6-lookup */ \
536 + sizeof(struct dns_query_line) \
537 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
538 + sizeof(struct dns_record_line) - 1 \
539 - 2 /* We do not know the lenght of the answer yet*/;
541 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
542 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
544 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
545 answer->pkt.hdr.size = htons(len);
546 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
548 answer->pkt.from = query_states[id].remote_ip;
550 answer->pkt.to = query_states[id].local_ip;
551 answer->pkt.dst_port = query_states[id].local_port;
553 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
559 dpkt->s.qdcount = htons(1);
560 dpkt->s.ancount = htons(1);
562 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
563 GNUNET_free(query_states[id].name);
565 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
566 dque->type = htons(12); /* PTR */
567 dque->class = htons(1); /* IN */
569 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
570 memcpy(anname, "\xc0\x0c", 2);
572 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
573 drec_data->type = htons(12); /* AAAA */
574 drec_data->class = htons(1); /* IN */
575 /* FIXME: read the TTL from block:
576 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
578 * But how to get the seconds out of this?
580 drec_data->ttl = htonl(3600);
582 /* Calculate at which offset in the packet the length of the name and the
583 * name, it is filled in by the daemon-vpn */
584 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
586 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
588 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
590 GNUNET_TIME_UNIT_FOREVER_REL,
592 query_states[id].client);
596 * Receive a block from the dht.
599 receive_dht(void *cls,
600 struct GNUNET_TIME_Absolute exp __attribute__((unused)),
601 const GNUNET_HashCode *key __attribute__((unused)),
602 const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)),
603 const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)),
604 enum GNUNET_BLOCK_Type type,
608 unsigned short id = ((struct receive_dht_cls*)cls)->id;
609 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
612 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
614 /* If no query with this id is pending, ignore the block */
615 if (query_states[id].valid != GNUNET_YES) return;
616 query_states[id].valid = GNUNET_NO;
618 const struct GNUNET_DNS_Record* rec = data;
619 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
620 "Got block of size %d, peer: %08x, desc: %08x\n",
622 *((unsigned int*)&rec->peer),
623 *((unsigned int*)&rec->service_descriptor));
625 size_t len = sizeof(struct answer_packet) - 1 \
626 + sizeof(struct dns_static) \
627 + query_states[id].namelen \
628 + sizeof(struct dns_query_line) \
629 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
630 + sizeof(struct dns_record_line) - 1 \
631 + 16; /* To hold the IPv6-Address */
633 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
634 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
636 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
637 answer->pkt.hdr.size = htons(len);
638 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
640 GNUNET_CRYPTO_hash(&rec->peer,
641 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
642 &answer->pkt.service_descr.peer);
644 memcpy(&answer->pkt.service_descr.service_descriptor,
645 &rec->service_descriptor,
646 sizeof(GNUNET_HashCode));
647 memcpy(&answer->pkt.service_descr.service_type,
649 sizeof(answer->pkt.service_descr.service_type));
650 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
652 answer->pkt.from = query_states[id].remote_ip;
654 answer->pkt.to = query_states[id].local_ip;
655 answer->pkt.dst_port = query_states[id].local_port;
657 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
663 dpkt->s.qdcount = htons(1);
664 dpkt->s.ancount = htons(1);
666 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
667 GNUNET_free(query_states[id].name);
669 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
670 dque->type = htons(28); /* AAAA */
671 dque->class = htons(1); /* IN */
673 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
674 memcpy(anname, "\xc0\x0c", 2);
676 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
677 drec_data->type = htons(28); /* AAAA */
678 drec_data->class = htons(1); /* IN */
680 /* FIXME: read the TTL from block:
681 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
683 * But how to get the seconds out of this?
685 drec_data->ttl = htonl(3600);
686 drec_data->data_len = htons(16);
688 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
689 * filled in by the daemon-vpn */
690 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
692 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
694 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
696 GNUNET_TIME_UNIT_FOREVER_REL,
698 query_states[id].client);
700 GNUNET_DHT_get_stop(handle);
704 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
707 rehijack(void *cls __attribute__((unused)),
708 struct GNUNET_SERVER_Client *client,
709 const struct GNUNET_MessageHeader *message __attribute__((unused))) {
710 unhijack(dnsoutport);
711 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
713 GNUNET_SERVER_receive_done(client, GNUNET_OK);
717 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
720 receive_query(void *cls __attribute__((unused)),
721 struct GNUNET_SERVER_Client *client,
722 const struct GNUNET_MessageHeader *message) {
723 struct query_packet* pkt = (struct query_packet*)message;
724 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
725 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
727 query_states[dns->s.id].valid = GNUNET_YES;
728 query_states[dns->s.id].client = client;
729 query_states[dns->s.id].local_ip = pkt->orig_from;
730 query_states[dns->s.id].local_port = pkt->src_port;
731 query_states[dns->s.id].remote_ip = pkt->orig_to;
732 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
733 if (query_states[dns->s.id].name != NULL)
734 GNUNET_free(query_states[dns->s.id].name);
735 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
736 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
738 /* The query is for a .gnunet-address */
739 if (pdns->queries[0]->namelen > 9 &&
740 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
744 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
746 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
747 "Getting with key %08x, len is %d\n",
748 *((unsigned int*)&key),
749 pdns->queries[0]->namelen);
751 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
754 cls->handle = GNUNET_DHT_get_start(dht,
755 GNUNET_TIME_UNIT_MINUTES,
756 GNUNET_BLOCK_TYPE_DNS,
758 DEFAULT_GET_REPLICATION,
770 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
772 /* This is a PTR-Query. Check if it is for "our" network */
773 if (htons(pdns->queries[0]->qtype) == 12 &&
774 74 == pdns->queries[0]->namelen)
778 char ipv6rev[74] = "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.";
780 unsigned long long ipv6prefix;
781 unsigned int comparelen;
783 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
784 inet_pton (AF_INET6, ipv6addr, ipv6);
785 GNUNET_free(ipv6addr);
787 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
788 GNUNET_assert(ipv6prefix < 127);
789 ipv6prefix = (ipv6prefix + 7)/8;
791 for (i = ipv6prefix; i < 16; i++)
794 for (i = 0; i < 16; i++)
796 unsigned char c1 = ipv6[i] >> 4;
797 unsigned char c2 = ipv6[i] & 0xf;
800 ipv6rev[62-(4*i)] = c1 + '0';
802 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
805 ipv6rev[62-((4*i)+2)] = c2 + '0';
807 ipv6rev[62-((4*i)+2)] = c2 + 87;
809 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
810 comparelen = 10 + 4*ipv6prefix;
811 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
812 ipv6rev + (74 - comparelen),
815 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
817 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
824 unsigned int virt_dns_bytes;
826 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
830 "No entry 'VIRTDNS' in configuration!\n");
834 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
841 GNUNET_free(virt_dns);
843 if (virt_dns_bytes == pkt->orig_to)
845 /* This is a packet that was sent directly to the virtual dns-server
847 * This means we have to send this query over gnunet
850 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
851 struct tunnel_cls *cls_ = GNUNET_malloc(size);
852 cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
854 cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
855 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
857 memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader));
858 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
864 /* The query should be sent to the network */
866 struct sockaddr_in dest;
867 memset(&dest, 0, sizeof dest);
868 dest.sin_port = htons(53);
869 dest.sin_addr.s_addr = pkt->orig_to;
871 GNUNET_NETWORK_socket_sendto(dnsout,
873 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
874 (struct sockaddr*) &dest,
878 free_parsed_dns_packet(pdns);
881 GNUNET_SERVER_receive_done(client, GNUNET_OK);
884 static void read_response (void *cls,
885 const struct GNUNET_SCHEDULER_TaskContext *tc);
890 struct sockaddr_in addr;
892 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
894 return GNUNET_SYSERR;
895 memset (&addr, 0, sizeof (struct sockaddr_in));
897 addr.sin_family = AF_INET;
898 int err = GNUNET_NETWORK_socket_bind (dnsout,
899 (struct sockaddr *) &addr,
900 sizeof (struct sockaddr_in));
902 if (err != GNUNET_OK)
904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
905 "Could not bind a port: %m\n");
906 return GNUNET_SYSERR;
909 /* Read the port we bound to */
910 socklen_t addrlen = sizeof (struct sockaddr_in);
911 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
912 (struct sockaddr *) &addr, &addrlen);
914 dnsoutport = htons (addr.sin_port);
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bound to port %d.\n", dnsoutport);
918 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
919 &read_response, NULL);
925 * Read a response-packet of the UDP-Socket
928 read_response (void *cls
929 __attribute__ ((unused)),
930 const struct GNUNET_SCHEDULER_TaskContext *tc)
932 struct sockaddr_in addr;
933 socklen_t addrlen = sizeof (addr);
937 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
940 memset (&addr, 0, sizeof addr);
943 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
945 unhijack (dnsoutport);
946 if (GNUNET_YES == open_port ())
947 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
951 /* port the code above? */
955 unsigned char buf[len];
956 struct dns_pkt *dns = (struct dns_pkt *) buf;
958 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
961 (struct sockaddr *) &addr, &addrlen);
965 unhijack (dnsoutport);
966 if (GNUNET_YES == open_port ())
967 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
971 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d\n", ntohs(dns->s.id));
973 if (query_states[dns->s.id].valid == GNUNET_YES)
975 if (query_states[dns->s.id].tunnel != NULL)
977 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer to query %d for a remote peer!\n", ntohs(dns->s.id));
978 /* This response should go through a tunnel */
979 uint32_t *c = GNUNET_malloc (4 + sizeof(struct GNUNET_MESH_Tunnel*) + r);
981 struct GNUNET_MESH_Tunnel** t = (struct GNUNET_MESH_Tunnel**)(c + 1);
982 *t = query_states[dns->s.id].tunnel;
983 memcpy (t + 1, dns, r);
985 GNUNET_MESH_tunnel_get_data (query_states[dns->s.id].tunnel))
987 struct GNUNET_MESH_TransmitHandle *th =
988 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
991 GNUNET_TIME_UNIT_MINUTES,
995 GNUNET_MessageHeader),
996 mesh_send_response, c);
997 GNUNET_MESH_tunnel_set_data (query_states[dns->s.id].tunnel,
1002 struct tunnel_notify_queue* head = GNUNET_MESH_tunnel_get_head(query_states[dns->s.id].tunnel);
1003 struct tunnel_notify_queue* tail = GNUNET_MESH_tunnel_get_tail(query_states[dns->s.id].tunnel);
1005 struct tunnel_notify_queue* element = GNUNET_malloc(sizeof(struct tunnel_notify_queue));
1007 element->len = r+sizeof(struct GNUNET_MessageHeader);
1008 element->cb = mesh_send_response;
1010 GNUNET_CONTAINER_DLL_insert_tail(head, tail, element);
1011 GNUNET_MESH_tunnel_set_head(query_states[dns->s.id].tunnel, head);
1012 GNUNET_MESH_tunnel_set_tail(query_states[dns->s.id].tunnel, tail);
1017 query_states[dns->s.id].valid = GNUNET_NO;
1019 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
1020 struct answer_packet_list *answer =
1021 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
1022 answer->pkt.hdr.type =
1023 htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
1024 answer->pkt.hdr.size = htons (len);
1025 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
1026 answer->pkt.from = addr.sin_addr.s_addr;
1027 answer->pkt.to = query_states[dns->s.id].local_ip;
1028 answer->pkt.dst_port = query_states[dns->s.id].local_port;
1029 memcpy (answer->pkt.data, buf, r);
1031 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
1033 GNUNET_SERVER_notify_transmit_ready (query_states
1034 [dns->s.id].client, len,
1035 GNUNET_TIME_UNIT_FOREVER_REL,
1037 query_states[dns->s.
1042 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1043 dnsout, &read_response, NULL);
1048 * Task run during shutdown.
1054 cleanup_task (void *cls __attribute__((unused)),
1055 const struct GNUNET_SCHEDULER_TaskContext *tc)
1057 GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
1059 unhijack(dnsoutport);
1060 GNUNET_DHT_disconnect(dht);
1061 GNUNET_MESH_disconnect(mesh_handle);
1065 * @brief Create a port-map from udp and tcp redirects
1067 * @param udp_redirects
1068 * @param tcp_redirects
1073 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
1076 char* cpy, *hostname, *redirect;
1077 int local_port, count = 0;
1079 if (NULL != udp_redirects)
1081 cpy = GNUNET_strdup (udp_redirects);
1082 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
1084 if (NULL == (hostname = strstr (redirect, ":")))
1086 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
1090 local_port = atoi (redirect);
1091 if (!((local_port > 0) && (local_port < 65536)))
1092 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
1094 ret |= (0xFFFF & htons(local_port));
1108 if (NULL != tcp_redirects)
1110 cpy = GNUNET_strdup (tcp_redirects);
1111 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
1113 if (NULL == (hostname = strstr (redirect, ":")))
1115 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
1119 local_port = atoi (redirect);
1120 if (!((local_port > 0) && (local_port < 65536)))
1121 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
1123 ret |= (0xFFFF & htons(local_port));
1144 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1145 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1147 size_t size = sizeof (struct GNUNET_DNS_Record);
1148 struct GNUNET_DNS_Record data;
1149 memset (&data, 0, size);
1152 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1153 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1155 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1157 *((unsigned long long *) &data.service_descriptor));
1159 data.service_type = service_type;
1162 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1164 data.expiration_time =
1165 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1167 /* Sign the block */
1168 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1169 &data.purpose, &data.signature))
1171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Putting with key %08x, size = %d\n",
1177 *((unsigned int *) &data.service_descriptor), size);
1179 GNUNET_DHT_put (dht,
1180 &data.service_descriptor,
1181 DEFAULT_PUT_REPLICATION,
1183 GNUNET_BLOCK_TYPE_DNS,
1186 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1187 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1191 * @brief Publishes the record defined by the section section
1193 * @param cls closure
1194 * @param section the current section
1197 publish_iterate (void *cls __attribute__((unused)), const char *section)
1199 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
1202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1204 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1207 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1208 "UDP_REDIRECTS", &udp_redirects);
1209 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1212 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1217 if (keyfile != NULL)
1218 GNUNET_free (keyfile);
1222 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1223 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1224 GNUNET_free (keyfile);
1225 GNUNET_assert (my_private_key != NULL);
1227 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1228 uint32_t service_type = 0;
1230 if (NULL != udp_redirects)
1231 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1233 if (NULL != tcp_redirects)
1234 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1236 service_type = htonl (service_type);
1239 publish_name (section, ports, service_type, my_private_key);
1241 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1242 "ALTERNATIVE_NAMES",
1243 &alternative_names);
1244 for (alternative_name = strtok (alternative_names, " ");
1245 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1248 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1249 strcpy (altname, alternative_name);
1250 strcpy (altname + strlen (alternative_name) + 1, section);
1251 altname[strlen (alternative_name)] = '.';
1253 publish_name (altname, ports, service_type, my_private_key);
1256 GNUNET_free_non_null(alternative_names);
1257 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1258 GNUNET_free_non_null (udp_redirects);
1259 GNUNET_free_non_null (tcp_redirects);
1263 * Publish a DNS-record in the DHT.
1266 publish_names (void *cls __attribute__((unused)),
1267 const struct GNUNET_SCHEDULER_TaskContext *tc) {
1268 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1271 GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL);
1273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1279 * @param cls closure
1280 * @param server the initialized server
1281 * @param cfg_ configuration to use
1285 struct GNUNET_SERVER_Handle *server,
1286 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1288 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1289 /* callback, cls, type, size */
1290 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1291 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1292 sizeof (struct GNUNET_MessageHeader)},
1296 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1297 {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1298 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1302 static GNUNET_MESH_ApplicationType *apptypes;
1304 if (GNUNET_YES != open_port ())
1306 GNUNET_SCHEDULER_shutdown();
1311 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1312 apptypes = (GNUNET_MESH_ApplicationType[])
1314 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1315 GNUNET_APPLICATION_TYPE_END};
1317 apptypes = (GNUNET_MESH_ApplicationType[])
1319 GNUNET_APPLICATION_TYPE_END};
1322 GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1327 for (i = 0; i < 65536; i++)
1329 query_states[i].valid = GNUNET_NO;
1332 dht = GNUNET_DHT_connect (cfg, 1024);
1334 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1336 GNUNET_SERVER_add_handlers (server, handlers);
1337 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1338 &cleanup_task, cls);
1342 * The main function for the dns service.
1344 * @param argc number of arguments from the command line
1345 * @param argv command line arguments
1346 * @return 0 ok, 1 on error
1349 main (int argc, char *const *argv)
1351 return (GNUNET_OK ==
1352 GNUNET_SERVICE_run (argc,
1355 GNUNET_SERVICE_OPTION_NONE,
1356 &run, NULL)) ? 0 : 1;