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_network_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet-service-dns-p.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_applications.h"
33 #include "gnunet-vpn-packet.h"
34 #include "gnunet_container_lib.h"
35 #include "gnunet-dns-parser.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_block_lib.h"
38 #include "block_dns.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_mesh_service.h"
41 #include "gnunet_signatures.h"
43 struct GNUNET_MESH_Handle *mesh_handle;
46 * The tunnel to send queries
48 static struct GNUNET_MESH_Tunnel* dns_tunnel;
51 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
52 * sent through gnunet. The port of this socket will not be hijacked.
54 static struct GNUNET_NETWORK_Handle *dnsout;
57 * The port bound to the socket dnsout
59 static unsigned short dnsoutport;
62 * A handle to the DHT-Service
64 static struct GNUNET_DHT_Handle *dht;
67 * The configuration to use
69 static const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * A list of DNS-Responses that have to be sent to the requesting client
74 static struct answer_packet_list *head;
77 * The tail of the list of DNS-responses
79 static struct answer_packet_list *tail;
82 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
83 * some information needed to handle this query
85 * It currently allocates at least
86 * (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;
99 } query_states[UINT16_MAX];
102 * A struct used to give more than one value as
103 * closure to receive_dht
105 struct receive_dht_cls {
107 struct GNUNET_DHT_GetHandle* handle;
111 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
114 hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
116 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
121 struct GNUNET_OS_Process *proc;
124 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 "No entry 'VIRTDNS' in configuration!\n");
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
133 snprintf (port_s, 6, "%d", dnsoutport);
134 if (NULL != (proc = GNUNET_OS_start_process (NULL,
136 "gnunet-helper-hijack-dns",
138 port_s, virt_dns, NULL)))
139 GNUNET_OS_process_close (proc);
140 GNUNET_free (virt_dns);
144 * Delete the hijacking-routes
147 unhijack (unsigned short port)
151 struct GNUNET_OS_Process *proc;
154 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158 "No entry 'VIRTDNS' in configuration!\n");
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
163 snprintf (port_s, 6, "%d", port);
164 if (NULL != (proc = GNUNET_OS_start_process (NULL,
166 "gnunet-helper-hijack-dns",
168 "-d", port_s, virt_dns, NULL)))
169 GNUNET_OS_process_close (proc);
170 GNUNET_free (virt_dns);
174 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
178 send_answer(void* cls, size_t size, void* buf) {
179 struct answer_packet_list* query = head;
180 size_t len = ntohs(query->pkt.hdr.size);
182 GNUNET_assert(len <= size);
184 memcpy(buf, &query->pkt.hdr, len);
186 GNUNET_CONTAINER_DLL_remove (head, tail, query);
190 /* When more data is to be sent, reschedule */
192 GNUNET_SERVER_notify_transmit_ready(cls,
193 ntohs(head->pkt.hdr.size),
194 GNUNET_TIME_UNIT_FOREVER_REL,
202 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
203 struct GNUNET_MessageHeader hdr;
207 struct tunnel_cls *remote_pending[UINT16_MAX];
210 mesh_send_response (void *cls, size_t size, void *buf)
212 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
213 struct GNUNET_MessageHeader *hdr = buf;
215 struct dns_pkt *dns = (struct dns_pkt *) (sz + 1);
216 hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
217 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
221 *sz + sizeof (struct GNUNET_MessageHeader));
223 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
225 memcpy (hdr + 1, dns, *sz);
227 return ntohs (hdr->size);
231 mesh_send (void *cls, size_t size, void *buf)
233 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
235 GNUNET_assert(cls_->hdr.size <= size);
237 size = cls_->hdr.size;
238 cls_->hdr.size = htons(cls_->hdr.size);
240 memcpy(buf, &cls_->hdr, size);
245 void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) {
246 if (NULL == peer) return;
247 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
248 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s, sending query with id %d\n", GNUNET_i2s(peer), ntohs(cls_->dns.s.id));
250 GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
253 GNUNET_TIME_UNIT_MINUTES,
262 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
267 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
269 if (NULL == dns_tunnel)
270 dns_tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
271 GNUNET_TIME_UNIT_HOURS,
272 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
276 cls_->tunnel = dns_tunnel;
277 remote_pending[cls_->dns.s.id] = cls_;
281 receive_mesh_query (void *cls __attribute__((unused)),
282 struct GNUNET_MESH_Tunnel *tunnel,
283 void **ctx __attribute__((unused)),
284 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
285 const struct GNUNET_MessageHeader *message,
286 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
288 struct dns_pkt *dns = (struct dns_pkt*)(message + 1);
290 struct sockaddr_in dest;
291 memset(&dest, 0, sizeof dest);
292 dest.sin_port = htons(53);
293 /* TODO: read from config */
294 inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
296 query_states[dns->s.id].tunnel = tunnel;
297 query_states[dns->s.id].valid = GNUNET_YES;
299 GNUNET_NETWORK_socket_sendto(dnsout,
301 ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
302 (struct sockaddr*) &dest,
305 return GNUNET_SYSERR;
309 receive_mesh_answer (void *cls __attribute__((unused)),
310 struct GNUNET_MESH_Tunnel *tunnel,
311 void **ctx __attribute__((unused)),
312 const struct GNUNET_PeerIdentity *sender,
313 const struct GNUNET_MessageHeader *message,
314 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
316 /* TODo: size check */
317 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
319 /* They sent us a packet we were not waiting for */
320 if (remote_pending[dns->s.id] == NULL
321 || remote_pending[dns->s.id]->tunnel != tunnel)
322 return GNUNET_SYSERR;
323 if (query_states[dns->s.id].valid != GNUNET_YES)
324 return GNUNET_SYSERR;
325 query_states[dns->s.id].valid = GNUNET_NO;
327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id));
329 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 */
330 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
332 struct answer_packet_list *answer =
333 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
334 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
336 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
337 answer->pkt.hdr.size = htons (len);
338 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE;
340 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
342 if (ntohs(pdns->s.ancount) < 1)
344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount));
345 free_parsed_dns_packet(pdns);
349 answer->pkt.addrsize = ntohs(pdns->answers[0]->data_len);
350 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The first answer has the addrlen %d\n", answer->pkt.addrsize);
351 memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
353 answer->pkt.from = query_states[dns->s.id].remote_ip;
355 answer->pkt.to = query_states[dns->s.id].local_ip;
356 answer->pkt.dst_port = query_states[dns->s.id].local_port;
358 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
360 dpkt->s.id = dns->s.id;
364 dpkt->s.qdcount = htons (1);
365 dpkt->s.ancount = htons (1);
367 memcpy (dpkt->data, query_states[dns->s.id].name,
368 query_states[dns->s.id].namelen);
369 GNUNET_free (query_states[dns->s.id].name);
371 struct dns_query_line *dque =
372 (struct dns_query_line *) (dpkt->data +
373 (query_states[dns->s.id].namelen));
374 dque->type = htons (28); /* AAAA */
375 dque->class = htons (1); /* IN */
378 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
379 sizeof (struct dns_query_line));
380 memcpy (anname, "\xc0\x0c", 2);
382 struct dns_record_line *drec_data =
383 (struct dns_record_line *) (dpkt->data +
384 (query_states[dns->s.id].namelen) +
385 sizeof (struct dns_query_line) + 2);
386 drec_data->type = htons (28); /* AAAA */
387 drec_data->class = htons (1); /* IN */
389 drec_data->ttl = pdns->answers[0]->ttl;
390 drec_data->data_len = htons (16);
392 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
393 * filled in by the daemon-vpn */
394 answer->pkt.addroffset =
395 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
396 (unsigned long) (&answer->pkt)));
398 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
400 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
402 GNUNET_TIME_UNIT_FOREVER_REL,
404 query_states[dns->s.id].client);
406 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer on to client\n");
408 free_parsed_dns_packet(pdns);
414 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
415 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
418 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
420 unsigned short id = pdns->s.id;
422 free_parsed_dns_packet(pdns);
424 if (query_states[id].valid != GNUNET_YES) return;
425 query_states[id].valid = GNUNET_NO;
427 GNUNET_assert(query_states[id].namelen == 74);
429 size_t len = sizeof(struct answer_packet) - 1 \
430 + sizeof(struct dns_static) \
431 + 74 /* this is the length of a reverse ipv6-lookup */ \
432 + sizeof(struct dns_query_line) \
433 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
434 + sizeof(struct dns_record_line) - 1 \
435 - 2 /* We do not know the lenght of the answer yet*/;
437 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
438 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
440 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
441 answer->pkt.hdr.size = htons(len);
442 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
444 answer->pkt.from = query_states[id].remote_ip;
446 answer->pkt.to = query_states[id].local_ip;
447 answer->pkt.dst_port = query_states[id].local_port;
449 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
455 dpkt->s.qdcount = htons(1);
456 dpkt->s.ancount = htons(1);
458 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
459 GNUNET_free(query_states[id].name);
461 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
462 dque->type = htons(12); /* PTR */
463 dque->class = htons(1); /* IN */
465 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
466 memcpy(anname, "\xc0\x0c", 2);
468 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
469 drec_data->type = htons(12); /* AAAA */
470 drec_data->class = htons(1); /* IN */
471 /* FIXME: read the TTL from block:
472 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
474 * But how to get the seconds out of this?
476 drec_data->ttl = htonl(3600);
478 /* Calculate at which offset in the packet the length of the name and the
479 * name, it is filled in by the daemon-vpn */
480 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
482 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
484 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
486 GNUNET_TIME_UNIT_FOREVER_REL,
488 query_states[id].client);
492 * Receive a block from the dht.
495 receive_dht(void *cls,
496 struct GNUNET_TIME_Absolute exp __attribute__((unused)),
497 const GNUNET_HashCode *key __attribute__((unused)),
498 const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)),
499 const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)),
500 enum GNUNET_BLOCK_Type type,
504 unsigned short id = ((struct receive_dht_cls*)cls)->id;
505 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
508 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
510 /* If no query with this id is pending, ignore the block */
511 if (query_states[id].valid != GNUNET_YES) return;
512 query_states[id].valid = GNUNET_NO;
514 const struct GNUNET_DNS_Record* rec = data;
515 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
516 "Got block of size %d, peer: %08x, desc: %08x\n",
518 *((unsigned int*)&rec->peer),
519 *((unsigned int*)&rec->service_descriptor));
521 size_t len = sizeof(struct answer_packet) - 1 \
522 + sizeof(struct dns_static) \
523 + query_states[id].namelen \
524 + sizeof(struct dns_query_line) \
525 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
526 + sizeof(struct dns_record_line) - 1 \
527 + 16; /* To hold the IPv6-Address */
529 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
530 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
532 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
533 answer->pkt.hdr.size = htons(len);
534 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
536 GNUNET_CRYPTO_hash(&rec->peer,
537 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
538 &answer->pkt.service_descr.peer);
540 memcpy(&answer->pkt.service_descr.service_descriptor,
541 &rec->service_descriptor,
542 sizeof(GNUNET_HashCode));
543 memcpy(&answer->pkt.service_descr.service_type,
545 sizeof(answer->pkt.service_descr.service_type));
546 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
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(28); /* AAAA */
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(28); /* AAAA */
574 drec_data->class = htons(1); /* IN */
576 /* FIXME: read the TTL from block:
577 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
579 * But how to get the seconds out of this?
581 drec_data->ttl = htonl(3600);
582 drec_data->data_len = htons(16);
584 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
585 * filled in by the daemon-vpn */
586 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
588 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
590 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
592 GNUNET_TIME_UNIT_FOREVER_REL,
594 query_states[id].client);
596 GNUNET_DHT_get_stop(handle);
600 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
603 rehijack(void *cls __attribute__((unused)),
604 struct GNUNET_SERVER_Client *client,
605 const struct GNUNET_MessageHeader *message __attribute__((unused))) {
606 unhijack(dnsoutport);
607 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
609 GNUNET_SERVER_receive_done(client, GNUNET_OK);
613 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
616 receive_query(void *cls __attribute__((unused)),
617 struct GNUNET_SERVER_Client *client,
618 const struct GNUNET_MessageHeader *message) {
619 struct query_packet* pkt = (struct query_packet*)message;
620 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
621 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
623 query_states[dns->s.id].valid = GNUNET_YES;
624 query_states[dns->s.id].client = client;
625 query_states[dns->s.id].local_ip = pkt->orig_from;
626 query_states[dns->s.id].local_port = pkt->src_port;
627 query_states[dns->s.id].remote_ip = pkt->orig_to;
628 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
629 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
630 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
632 /* The query is for a .gnunet-address */
633 if (pdns->queries[0]->namelen > 9 &&
634 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
636 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
638 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
640 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
641 "Getting with key %08x, len is %d\n",
642 *((unsigned int*)&key),
643 pdns->queries[0]->namelen);
645 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
648 cls->handle = GNUNET_DHT_get_start(dht,
649 GNUNET_TIME_UNIT_MINUTES,
650 GNUNET_BLOCK_TYPE_DNS,
652 DEFAULT_GET_REPLICATION,
664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
666 /* This is a PTR-Query. Check if it is for "our" network */
667 if (htons(pdns->queries[0]->qtype) == 12 &&
668 74 == pdns->queries[0]->namelen)
672 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.";
674 unsigned long long ipv6prefix;
675 unsigned int comparelen;
677 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
678 inet_pton (AF_INET6, ipv6addr, ipv6);
679 GNUNET_free(ipv6addr);
681 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
682 GNUNET_assert(ipv6prefix < 127);
683 ipv6prefix = (ipv6prefix + 7)/8;
685 for (i = ipv6prefix; i < 16; i++)
688 for (i = 0; i < 16; i++)
690 unsigned char c1 = ipv6[i] >> 4;
691 unsigned char c2 = ipv6[i] & 0xf;
694 ipv6rev[62-(4*i)] = c1 + '0';
696 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
699 ipv6rev[62-((4*i)+2)] = c2 + '0';
701 ipv6rev[62-((4*i)+2)] = c2 + 87;
703 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
704 comparelen = 10 + 4*ipv6prefix;
705 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
706 ipv6rev + (74 - comparelen),
709 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
711 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
718 unsigned int virt_dns_bytes;
720 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
724 "No entry 'VIRTDNS' in configuration!\n");
728 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
735 GNUNET_free(virt_dns);
737 if (virt_dns_bytes == pkt->orig_to)
739 /* This is a packet that was sent directly to the virtual dns-server
741 * This means we have to send this query over gnunet
744 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
745 struct tunnel_cls *cls_ = GNUNET_malloc(size);
746 cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
748 cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
751 memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader));
752 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
758 /* The query should be sent to the network */
760 struct sockaddr_in dest;
761 memset(&dest, 0, sizeof dest);
762 dest.sin_port = htons(53);
763 dest.sin_addr.s_addr = pkt->orig_to;
765 GNUNET_NETWORK_socket_sendto(dnsout,
767 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
768 (struct sockaddr*) &dest,
772 free_parsed_dns_packet(pdns);
775 GNUNET_SERVER_receive_done(client, GNUNET_OK);
778 static void read_response (void *cls,
779 const struct GNUNET_SCHEDULER_TaskContext *tc);
784 struct sockaddr_in addr;
786 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
789 memset (&addr, 0, sizeof (struct sockaddr_in));
791 int err = GNUNET_NETWORK_socket_bind (dnsout,
792 (struct sockaddr *) &addr,
793 sizeof (struct sockaddr_in));
795 if (err != GNUNET_YES)
797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
798 "Could not bind a port, exiting\n");
802 /* Read the port we bound to */
803 socklen_t addrlen = sizeof (struct sockaddr_in);
804 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
805 (struct sockaddr *) &addr, &addrlen);
807 dnsoutport = htons (addr.sin_port);
809 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
810 &read_response, NULL);
814 * Read a response-packet of the UDP-Socket
817 read_response (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
819 struct sockaddr_in addr;
820 socklen_t addrlen = sizeof (addr);
824 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
827 memset (&addr, 0, sizeof addr);
830 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
832 unhijack (dnsoutport);
834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
838 /* port the code above? */
842 unsigned char buf[len];
843 struct dns_pkt *dns = (struct dns_pkt *) buf;
845 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
848 (struct sockaddr *) &addr, &addrlen);
852 unhijack (dnsoutport);
854 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
858 if (query_states[dns->s.id].valid == GNUNET_YES)
860 if (query_states[dns->s.id].tunnel != NULL)
862 uint32_t *c = GNUNET_malloc(4 + r);
865 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
868 GNUNET_TIME_UNIT_MINUTES,
869 NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
873 query_states[dns->s.id].valid = GNUNET_NO;
875 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
876 struct answer_packet_list *answer =
877 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
878 answer->pkt.hdr.type =
879 htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
880 answer->pkt.hdr.size = htons (len);
881 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
882 answer->pkt.from = addr.sin_addr.s_addr;
883 answer->pkt.to = query_states[dns->s.id].local_ip;
884 answer->pkt.dst_port = query_states[dns->s.id].local_port;
885 memcpy (answer->pkt.data, buf, r);
887 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
889 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
891 GNUNET_TIME_UNIT_FOREVER_REL,
893 query_states[dns->s.id].
898 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
899 dnsout, &read_response, NULL);
904 * Task run during shutdown.
910 cleanup_task (void *cls __attribute__((unused)),
911 const struct GNUNET_SCHEDULER_TaskContext *tc)
913 GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
915 unhijack(dnsoutport);
916 GNUNET_DHT_disconnect(dht);
920 * @brief Create a port-map from udp and tcp redirects
922 * @param udp_redirects
923 * @param tcp_redirects
928 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
931 char* cpy, *hostname, *redirect;
932 int local_port, count = 0;
934 if (NULL != udp_redirects)
936 cpy = GNUNET_strdup (udp_redirects);
937 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
939 if (NULL == (hostname = strstr (redirect, ":")))
941 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
945 local_port = atoi (redirect);
946 if (!((local_port > 0) && (local_port < 65536)))
947 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
949 ret |= (0xFFFF & htons(local_port));
963 if (NULL != tcp_redirects)
965 cpy = GNUNET_strdup (tcp_redirects);
966 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
968 if (NULL == (hostname = strstr (redirect, ":")))
970 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
974 local_port = atoi (redirect);
975 if (!((local_port > 0) && (local_port < 65536)))
976 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
978 ret |= (0xFFFF & htons(local_port));
999 publish_name (const char *name, uint64_t ports, uint32_t service_type,
1000 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
1002 size_t size = sizeof (struct GNUNET_DNS_Record);
1003 struct GNUNET_DNS_Record data;
1004 memset (&data, 0, size);
1007 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1008 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1010 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1012 *((unsigned long long *) &data.service_descriptor));
1014 data.service_type = service_type;
1017 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1019 data.expiration_time =
1020 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1022 /* Sign the block */
1023 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1024 &data.purpose, &data.signature))
1026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Putting with key %08x, size = %d\n",
1032 *((unsigned int *) &data.service_descriptor), size);
1034 GNUNET_DHT_put (dht,
1035 &data.service_descriptor,
1036 DEFAULT_PUT_REPLICATION,
1038 GNUNET_BLOCK_TYPE_DNS,
1041 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1042 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1046 * @brief Publishes the record defined by the section section
1048 * @param cls closure
1049 * @param section the current section
1052 publish_iterate (void *cls __attribute__((unused)), const char *section)
1054 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
1057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1059 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1062 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1063 "UDP_REDIRECTS", &udp_redirects);
1064 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1067 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1072 if (keyfile != NULL)
1073 GNUNET_free (keyfile);
1077 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1078 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1079 GNUNET_free (keyfile);
1080 GNUNET_assert (my_private_key != NULL);
1082 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1083 uint32_t service_type = 0;
1085 if (NULL != udp_redirects)
1086 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1088 if (NULL != tcp_redirects)
1089 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1091 service_type = htonl (service_type);
1094 publish_name (section, ports, service_type, my_private_key);
1096 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1097 "ALTERNATIVE_NAMES",
1098 &alternative_names);
1099 for (alternative_name = strtok (alternative_names, " ");
1100 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1103 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1104 strcpy (altname, alternative_name);
1105 strcpy (altname + strlen (alternative_name) + 1, section);
1106 altname[strlen (alternative_name)] = '.';
1108 publish_name (altname, ports, service_type, my_private_key);
1111 GNUNET_free_non_null(alternative_names);
1112 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1113 GNUNET_free_non_null (udp_redirects);
1114 GNUNET_free_non_null (tcp_redirects);
1118 * Publish a DNS-record in the DHT.
1121 publish_names (void *cls __attribute__((unused)),
1122 const struct GNUNET_SCHEDULER_TaskContext *tc) {
1123 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1126 GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL);
1128 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1134 * @param cls closure
1135 * @param server the initialized server
1136 * @param cfg_ configuration to use
1140 struct GNUNET_SERVER_Handle *server,
1141 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1143 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1144 /* callback, cls, type, size */
1145 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1146 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1147 sizeof (struct GNUNET_MessageHeader)},
1151 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1152 {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1153 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1157 static GNUNET_MESH_ApplicationType *apptypes;
1160 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1161 apptypes = (GNUNET_MESH_ApplicationType[])
1163 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1164 GNUNET_APPLICATION_TYPE_END};
1166 apptypes = (GNUNET_MESH_ApplicationType[])
1168 GNUNET_APPLICATION_TYPE_END};
1171 GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1176 for (i = 0; i < 65536; i++)
1178 query_states[i].valid = GNUNET_NO;
1181 dht = GNUNET_DHT_connect (cfg, 1024);
1185 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1187 GNUNET_SERVER_add_handlers (server, handlers);
1188 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1189 &cleanup_task, cls);
1193 * The main function for the dns service.
1195 * @param argc number of arguments from the command line
1196 * @param argv command line arguments
1197 * @return 0 ok, 1 on error
1200 main (int argc, char *const *argv)
1202 return (GNUNET_OK ==
1203 GNUNET_SERVICE_run (argc,
1206 GNUNET_SERVICE_OPTION_NONE,
1207 &run, NULL)) ? 0 : 1;