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 UDP-Socket through which DNS-Resolves will be sent if they are not to be
47 * sent through gnunet. The port of this socket will not be hijacked.
49 static struct GNUNET_NETWORK_Handle *dnsout;
52 * The port bound to the socket dnsout
54 static unsigned short dnsoutport;
57 * A handle to the DHT-Service
59 static struct GNUNET_DHT_Handle *dht;
62 * The configuration to use
64 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 * The handle to the service-configuration
69 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 struct GNUNET_OS_Process *proc;
121 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
125 "No entry 'VIRTDNS' in configuration!\n");
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
130 snprintf (port_s, 6, "%d", dnsoutport);
131 if (NULL != (proc = GNUNET_OS_start_process (NULL,
133 "gnunet-helper-hijack-dns",
135 port_s, virt_dns, NULL)))
136 GNUNET_OS_process_close (proc);
137 GNUNET_free (virt_dns);
141 * Delete the hijacking-routes
144 unhijack (unsigned short port)
148 struct GNUNET_OS_Process *proc;
151 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "No entry 'VIRTDNS' in configuration!\n");
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
160 snprintf (port_s, 6, "%d", port);
161 if (NULL != (proc = GNUNET_OS_start_process (NULL,
163 "gnunet-helper-hijack-dns",
165 "-d", port_s, virt_dns, NULL)))
166 GNUNET_OS_process_close (proc);
167 GNUNET_free (virt_dns);
171 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
175 send_answer(void* cls, size_t size, void* buf) {
176 struct answer_packet_list* query = head;
177 size_t len = ntohs(query->pkt.hdr.size);
179 GNUNET_assert(len <= size);
181 memcpy(buf, &query->pkt.hdr, len);
183 GNUNET_CONTAINER_DLL_remove (head, tail, query);
187 /* When more data is to be sent, reschedule */
189 GNUNET_SERVER_notify_transmit_ready(cls,
190 ntohs(head->pkt.hdr.size),
191 GNUNET_TIME_UNIT_FOREVER_REL,
199 struct GNUNET_MESH_Tunnel *tunnel;
200 struct GNUNET_MessageHeader hdr;
204 struct tunnel_cls *remote_pending[UINT16_MAX];
207 mesh_send_response (void *cls, size_t size, void *buf)
209 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
210 struct GNUNET_MessageHeader *hdr = buf;
212 struct dns_pkt *dns = (struct dns_pkt *) (sz + 1);
213 hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
214 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
218 *sz + sizeof (struct GNUNET_MessageHeader));
220 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
222 memcpy (hdr + 1, dns, *sz);
224 return ntohs (hdr->size);
228 mesh_send (void *cls, size_t size, void *buf)
230 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
232 GNUNET_assert(cls_->hdr.size <= size);
234 size = cls_->hdr.size;
235 cls_->hdr.size = htons(cls_->hdr.size);
237 memcpy(buf, &cls_->hdr, size);
242 void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi) {
243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %x\n", *((unsigned long*)peer));
244 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
246 GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
249 GNUNET_TIME_UNIT_MINUTES,
258 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
263 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
265 cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
266 GNUNET_TIME_UNIT_HOURS,
267 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
272 remote_pending[cls_->dns.s.id] = cls_;
276 receive_mesh_query (void *cls,
277 struct GNUNET_MESH_Tunnel *tunnel,
279 const struct GNUNET_PeerIdentity *sender,
280 const struct GNUNET_MessageHeader *message,
281 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
283 struct dns_pkt *dns = (struct dns_pkt*)(message + 1);
285 struct sockaddr_in dest;
286 memset(&dest, 0, sizeof dest);
287 dest.sin_port = htons(53);
288 /* TODO: read from config */
289 inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
291 query_states[dns->s.id].tunnel = tunnel;
292 query_states[dns->s.id].valid = GNUNET_YES;
294 GNUNET_NETWORK_socket_sendto(dnsout,
296 ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
297 (struct sockaddr*) &dest,
300 return GNUNET_SYSERR;
304 receive_mesh_answer (void *cls,
305 struct GNUNET_MESH_Tunnel *tunnel,
307 const struct GNUNET_PeerIdentity *sender,
308 const struct GNUNET_MessageHeader *message,
309 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
311 /* TODo: size check */
312 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
314 /* They sent us a packet we were not waiting for */
315 if (remote_pending[dns->s.id] == NULL
316 || remote_pending[dns->s.id]->tunnel != tunnel)
317 return GNUNET_SYSERR;
318 if (query_states[dns->s.id].valid != GNUNET_YES)
319 return GNUNET_SYSERR;
320 query_states[dns->s.id].valid = GNUNET_NO;
322 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 */
323 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
325 struct answer_packet_list *answer =
326 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
327 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
329 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
330 answer->pkt.hdr.size = htons (len);
331 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE;
333 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
335 if (ntohs(pdns->s.ancount) < 1)
337 free_parsed_dns_packet(pdns);
341 answer->pkt.addrsize = pdns->answers[0]->data_len;
342 memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
344 answer->pkt.from = query_states[dns->s.id].remote_ip;
346 answer->pkt.to = query_states[dns->s.id].local_ip;
347 answer->pkt.dst_port = query_states[dns->s.id].local_port;
349 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
351 dpkt->s.id = dns->s.id;
355 dpkt->s.qdcount = htons (1);
356 dpkt->s.ancount = htons (1);
358 memcpy (dpkt->data, query_states[dns->s.id].name,
359 query_states[dns->s.id].namelen);
360 GNUNET_free (query_states[dns->s.id].name);
362 struct dns_query_line *dque =
363 (struct dns_query_line *) (dpkt->data +
364 (query_states[dns->s.id].namelen));
365 dque->type = htons (28); /* AAAA */
366 dque->class = htons (1); /* IN */
369 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
370 sizeof (struct dns_query_line));
371 memcpy (anname, "\xc0\x0c", 2);
373 struct dns_record_line *drec_data =
374 (struct dns_record_line *) (dpkt->data +
375 (query_states[dns->s.id].namelen) +
376 sizeof (struct dns_query_line) + 2);
377 drec_data->type = htons (28); /* AAAA */
378 drec_data->class = htons (1); /* IN */
380 drec_data->ttl = pdns->answers[0]->ttl;
381 drec_data->data_len = htons (16);
383 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
384 * filled in by the daemon-vpn */
385 answer->pkt.addroffset =
386 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
387 (unsigned long) (&answer->pkt)));
389 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
391 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
393 GNUNET_TIME_UNIT_FOREVER_REL,
395 query_states[dns->s.id].client);
397 free_parsed_dns_packet(pdns);
403 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
404 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
407 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
409 unsigned short id = pdns->s.id;
411 free_parsed_dns_packet(pdns);
413 if (query_states[id].valid != GNUNET_YES) return;
414 query_states[id].valid = GNUNET_NO;
416 GNUNET_assert(query_states[id].namelen == 74);
418 size_t len = sizeof(struct answer_packet) - 1 \
419 + sizeof(struct dns_static) \
420 + 74 /* this is the length of a reverse ipv6-lookup */ \
421 + sizeof(struct dns_query_line) \
422 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
423 + sizeof(struct dns_record_line) - 1 \
424 - 2 /* We do not know the lenght of the answer yet*/;
426 struct answer_packet_list* answer = 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);
431 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
433 answer->pkt.from = query_states[id].remote_ip;
435 answer->pkt.to = query_states[id].local_ip;
436 answer->pkt.dst_port = query_states[id].local_port;
438 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
444 dpkt->s.qdcount = htons(1);
445 dpkt->s.ancount = htons(1);
447 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
448 GNUNET_free(query_states[id].name);
450 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
451 dque->type = htons(12); /* PTR */
452 dque->class = htons(1); /* IN */
454 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
455 memcpy(anname, "\xc0\x0c", 2);
457 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
458 drec_data->type = htons(12); /* AAAA */
459 drec_data->class = htons(1); /* IN */
460 /* FIXME: read the TTL from block:
461 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
463 * But how to get the seconds out of this?
465 drec_data->ttl = htonl(3600);
467 /* Calculate at which offset in the packet the length of the name and the
468 * name, it is filled in by the daemon-vpn */
469 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
471 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
473 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
475 GNUNET_TIME_UNIT_FOREVER_REL,
477 query_states[id].client);
481 * Receive a block from the dht.
484 receive_dht(void *cls,
485 struct GNUNET_TIME_Absolute exp,
486 const GNUNET_HashCode *key,
487 const struct GNUNET_PeerIdentity *const *get_path,
488 const struct GNUNET_PeerIdentity *const *put_path,
489 enum GNUNET_BLOCK_Type type,
493 unsigned short id = ((struct receive_dht_cls*)cls)->id;
494 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
497 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
499 /* If no query with this id is pending, ignore the block */
500 if (query_states[id].valid != GNUNET_YES) return;
501 query_states[id].valid = GNUNET_NO;
503 const struct GNUNET_DNS_Record* rec = data;
504 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
505 "Got block of size %d, peer: %08x, desc: %08x\n",
507 *((unsigned int*)&rec->peer),
508 *((unsigned int*)&rec->service_descriptor));
510 size_t len = sizeof(struct answer_packet) - 1 \
511 + sizeof(struct dns_static) \
512 + query_states[id].namelen \
513 + sizeof(struct dns_query_line) \
514 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
515 + sizeof(struct dns_record_line) - 1 \
516 + 16; /* To hold the IPv6-Address */
518 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
519 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
521 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
522 answer->pkt.hdr.size = htons(len);
523 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
525 GNUNET_CRYPTO_hash(&rec->peer,
526 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
527 &answer->pkt.service_descr.peer);
529 memcpy(&answer->pkt.service_descr.service_descriptor,
530 &rec->service_descriptor,
531 sizeof(GNUNET_HashCode));
532 memcpy(&answer->pkt.service_descr.service_type,
534 sizeof(answer->pkt.service_descr.service_type));
535 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
537 answer->pkt.from = query_states[id].remote_ip;
539 answer->pkt.to = query_states[id].local_ip;
540 answer->pkt.dst_port = query_states[id].local_port;
542 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
548 dpkt->s.qdcount = htons(1);
549 dpkt->s.ancount = htons(1);
551 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
552 GNUNET_free(query_states[id].name);
554 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
555 dque->type = htons(28); /* AAAA */
556 dque->class = htons(1); /* IN */
558 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
559 memcpy(anname, "\xc0\x0c", 2);
561 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
562 drec_data->type = htons(28); /* AAAA */
563 drec_data->class = htons(1); /* IN */
565 /* FIXME: read the TTL from block:
566 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
568 * But how to get the seconds out of this?
570 drec_data->ttl = htonl(3600);
571 drec_data->data_len = htons(16);
573 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
574 * filled in by the daemon-vpn */
575 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
577 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
579 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
581 GNUNET_TIME_UNIT_FOREVER_REL,
583 query_states[id].client);
585 GNUNET_DHT_get_stop(handle);
589 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
593 struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message) {
595 unhijack(dnsoutport);
596 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
598 GNUNET_SERVER_receive_done(client, GNUNET_OK);
602 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
605 receive_query(void *cls,
606 struct GNUNET_SERVER_Client *client,
607 const struct GNUNET_MessageHeader *message) {
608 struct query_packet* pkt = (struct query_packet*)message;
609 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
610 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
612 query_states[dns->s.id].valid = GNUNET_YES;
613 query_states[dns->s.id].client = client;
614 query_states[dns->s.id].local_ip = pkt->orig_from;
615 query_states[dns->s.id].local_port = pkt->src_port;
616 query_states[dns->s.id].remote_ip = pkt->orig_to;
617 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
618 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
619 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
621 /* The query is for a .gnunet-address */
622 if (pdns->queries[0]->namelen > 9 &&
623 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
625 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
627 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
629 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
630 "Getting with key %08x, len is %d\n",
631 *((unsigned int*)&key),
632 pdns->queries[0]->namelen);
634 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
637 cls->handle = GNUNET_DHT_get_start(dht,
638 GNUNET_TIME_UNIT_MINUTES,
639 GNUNET_BLOCK_TYPE_DNS,
641 DEFAULT_GET_REPLICATION,
653 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
655 /* This is a PTR-Query. Check if it is for "our" network */
656 if (htons(pdns->queries[0]->qtype) == 12 &&
657 74 == pdns->queries[0]->namelen)
661 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.";
663 unsigned long long ipv6prefix;
664 unsigned int comparelen;
666 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
667 inet_pton (AF_INET6, ipv6addr, ipv6);
668 GNUNET_free(ipv6addr);
670 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
671 GNUNET_assert(ipv6prefix < 127);
672 ipv6prefix = (ipv6prefix + 7)/8;
674 for (i = ipv6prefix; i < 16; i++)
677 for (i = 0; i < 16; i++)
679 unsigned char c1 = ipv6[i] >> 4;
680 unsigned char c2 = ipv6[i] & 0xf;
683 ipv6rev[62-(4*i)] = c1 + '0';
685 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
688 ipv6rev[62-((4*i)+2)] = c2 + '0';
690 ipv6rev[62-((4*i)+2)] = c2 + 87;
692 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
693 comparelen = 10 + 4*ipv6prefix;
694 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
695 ipv6rev + (74 - comparelen),
698 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
700 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
709 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713 "No entry 'VIRTDNS' in configuration!\n");
717 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
724 GNUNET_free(virt_dns);
726 if (virt_dns_bytes == pkt->orig_to)
728 /* This is a packet that was sent directly to the virtual dns-server
730 * This means we have to send this query over gnunet
733 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
734 struct tunnel_cls *cls_ = GNUNET_malloc(size);
735 cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
737 cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
739 memcpy(&cls_->dns, dns, cls_->hdr.size);
740 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
746 /* The query should be sent to the network */
748 struct sockaddr_in dest;
749 memset(&dest, 0, sizeof dest);
750 dest.sin_port = htons(53);
751 dest.sin_addr.s_addr = pkt->orig_to;
753 GNUNET_NETWORK_socket_sendto(dnsout,
755 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
756 (struct sockaddr*) &dest,
760 free_parsed_dns_packet(pdns);
763 GNUNET_SERVER_receive_done(client, GNUNET_OK);
766 static void read_response (void *cls,
767 const struct GNUNET_SCHEDULER_TaskContext *tc);
772 struct sockaddr_in addr;
774 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
777 memset (&addr, 0, sizeof (struct sockaddr_in));
779 int err = GNUNET_NETWORK_socket_bind (dnsout,
780 (struct sockaddr *) &addr,
781 sizeof (struct sockaddr_in));
783 if (err != GNUNET_YES)
785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
786 "Could not bind a port, exiting\n");
790 /* Read the port we bound to */
791 socklen_t addrlen = sizeof (struct sockaddr_in);
792 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
793 (struct sockaddr *) &addr, &addrlen);
795 dnsoutport = htons (addr.sin_port);
797 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
798 &read_response, NULL);
802 * Read a response-packet of the UDP-Socket
805 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
807 struct sockaddr_in addr;
808 socklen_t addrlen = sizeof (addr);
812 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
815 memset (&addr, 0, sizeof addr);
818 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
820 unhijack (dnsoutport);
822 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
826 /* port the code above? */
830 unsigned char buf[len];
831 struct dns_pkt *dns = (struct dns_pkt *) buf;
833 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
836 (struct sockaddr *) &addr, &addrlen);
840 unhijack (dnsoutport);
842 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
846 if (query_states[dns->s.id].valid == GNUNET_YES)
848 if (query_states[dns->s.id].tunnel != NULL)
850 uint32_t *c = GNUNET_malloc(4 + r);
853 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
856 GNUNET_TIME_UNIT_MINUTES,
857 NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
861 query_states[dns->s.id].valid = GNUNET_NO;
863 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
864 struct answer_packet_list *answer =
865 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
866 answer->pkt.hdr.type =
867 htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
868 answer->pkt.hdr.size = htons (len);
869 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
870 answer->pkt.from = addr.sin_addr.s_addr;
871 answer->pkt.to = query_states[dns->s.id].local_ip;
872 answer->pkt.dst_port = query_states[dns->s.id].local_port;
873 memcpy (answer->pkt.data, buf, r);
875 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
877 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
879 GNUNET_TIME_UNIT_FOREVER_REL,
881 query_states[dns->s.id].
886 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
887 dnsout, &read_response, NULL);
892 * Task run during shutdown.
898 cleanup_task (void *cls,
899 const struct GNUNET_SCHEDULER_TaskContext *tc)
901 unhijack(dnsoutport);
902 GNUNET_DHT_disconnect(dht);
906 * @brief Create a port-map from udp and tcp redirects
908 * @param udp_redirects
909 * @param tcp_redirects
914 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
917 char* cpy, *hostname, *redirect;
918 int local_port, count = 0;
920 if (NULL != udp_redirects)
922 cpy = GNUNET_strdup (udp_redirects);
923 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
925 if (NULL == (hostname = strstr (redirect, ":")))
927 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
931 local_port = atoi (redirect);
932 if (!((local_port > 0) && (local_port < 65536)))
933 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
935 ret |= (0xFFFF & htons(local_port));
949 if (NULL != tcp_redirects)
951 cpy = GNUNET_strdup (tcp_redirects);
952 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
954 if (NULL == (hostname = strstr (redirect, ":")))
956 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
960 local_port = atoi (redirect);
961 if (!((local_port > 0) && (local_port < 65536)))
962 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
964 ret |= (0xFFFF & htons(local_port));
985 publish_name (const char *name, uint64_t ports, uint32_t service_type,
986 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
988 size_t size = sizeof (struct GNUNET_DNS_Record);
989 struct GNUNET_DNS_Record data;
990 memset (&data, 0, size);
993 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
994 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
996 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
998 *((unsigned long long *) &data.service_descriptor));
1000 data.service_type = service_type;
1003 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1005 data.expiration_time =
1006 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1008 /* Sign the block */
1009 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1010 &data.purpose, &data.signature))
1012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "Putting with key %08x, size = %d\n",
1018 *((unsigned int *) &data.service_descriptor), size);
1020 GNUNET_DHT_put (dht,
1021 &data.service_descriptor,
1022 DEFAULT_PUT_REPLICATION,
1024 GNUNET_BLOCK_TYPE_DNS,
1027 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1028 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1032 * @brief Publishes the record defined by the section section
1034 * @param cls closure
1035 * @param section the current section
1038 publish_iterate (void *cls, const char *section)
1040 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1043 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
1044 "UDP_REDIRECTS", &udp_redirects);
1045 GNUNET_CONFIGURATION_get_value_string (servicecfg, section, "TCP_REDIRECTS",
1048 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1052 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1053 if (keyfile != NULL)
1054 GNUNET_free (keyfile);
1058 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1059 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1060 GNUNET_free (keyfile);
1061 GNUNET_assert (my_private_key != NULL);
1063 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1064 uint32_t service_type = 0;
1066 if (NULL != udp_redirects)
1067 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1069 if (NULL != tcp_redirects)
1070 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1072 service_type = htonl (service_type);
1075 publish_name (section, ports, service_type, my_private_key);
1077 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
1078 "ALTERNATIVE_NAMES",
1079 &alternative_names);
1080 for (alternative_name = strtok (alternative_names, " ");
1081 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1084 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1085 strcpy (altname, alternative_name);
1086 strcpy (altname + strlen (alternative_name) + 1, section);
1087 altname[strlen (alternative_name)] = '.';
1089 publish_name (altname, ports, service_type, my_private_key);
1092 GNUNET_free_non_null(alternative_names);
1093 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1094 GNUNET_free_non_null (udp_redirects);
1095 GNUNET_free_non_null (tcp_redirects);
1099 * Publish a DNS-record in the DHT. This is up to now just for testing.
1102 publish_names (void *cls,
1103 const struct GNUNET_SCHEDULER_TaskContext *tc) {
1105 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1108 if (NULL != servicecfg)
1109 GNUNET_CONFIGURATION_destroy(servicecfg);
1111 GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
1113 servicecfg = GNUNET_CONFIGURATION_create();
1114 if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
1116 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
1117 GNUNET_CONFIGURATION_iterate_sections(servicecfg, publish_iterate, NULL);
1119 if (NULL != services)
1120 GNUNET_free(services);
1122 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1128 * @param cls closure
1129 * @param server the initialized server
1130 * @param cfg_ configuration to use
1134 struct GNUNET_SERVER_Handle *server,
1135 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1137 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1138 /* callback, cls, type, size */
1139 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1140 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1141 sizeof (struct GNUNET_MessageHeader)},
1145 static struct GNUNET_MESH_MessageHandler *mesh_handlers;
1147 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg_, "dns", "PROVIDE_EXIT"))
1148 mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
1149 {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1153 mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
1154 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1158 const static GNUNET_MESH_ApplicationType apptypes[] =
1159 { GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1160 GNUNET_APPLICATION_TYPE_END
1163 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1168 for (i = 0; i < 65536; i++)
1170 query_states[i].valid = GNUNET_NO;
1173 dht = GNUNET_DHT_connect (cfg, 1024);
1177 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1179 GNUNET_SERVER_add_handlers (server, handlers);
1180 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1181 &cleanup_task, cls);
1185 * The main function for the dns service.
1187 * @param argc number of arguments from the command line
1188 * @param argv command line arguments
1189 * @return 0 ok, 1 on error
1192 main (int argc, char *const *argv)
1194 return (GNUNET_OK ==
1195 GNUNET_SERVICE_run (argc,
1198 GNUNET_SERVICE_OPTION_NONE,
1199 &run, NULL)) ? 0 : 1;