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 GNUNET_PACKED;
200 struct GNUNET_MessageHeader hdr GNUNET_PACKED;
201 struct dns_pkt dns GNUNET_PACKED;
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);
738 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
740 memcpy(&cls_->dns, dns, cls_->hdr.size);
741 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
747 /* The query should be sent to the network */
749 struct sockaddr_in dest;
750 memset(&dest, 0, sizeof dest);
751 dest.sin_port = htons(53);
752 dest.sin_addr.s_addr = pkt->orig_to;
754 GNUNET_NETWORK_socket_sendto(dnsout,
756 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
757 (struct sockaddr*) &dest,
761 free_parsed_dns_packet(pdns);
764 GNUNET_SERVER_receive_done(client, GNUNET_OK);
767 static void read_response (void *cls,
768 const struct GNUNET_SCHEDULER_TaskContext *tc);
773 struct sockaddr_in addr;
775 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
778 memset (&addr, 0, sizeof (struct sockaddr_in));
780 int err = GNUNET_NETWORK_socket_bind (dnsout,
781 (struct sockaddr *) &addr,
782 sizeof (struct sockaddr_in));
784 if (err != GNUNET_YES)
786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
787 "Could not bind a port, exiting\n");
791 /* Read the port we bound to */
792 socklen_t addrlen = sizeof (struct sockaddr_in);
793 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
794 (struct sockaddr *) &addr, &addrlen);
796 dnsoutport = htons (addr.sin_port);
798 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
799 &read_response, NULL);
803 * Read a response-packet of the UDP-Socket
806 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
808 struct sockaddr_in addr;
809 socklen_t addrlen = sizeof (addr);
813 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
816 memset (&addr, 0, sizeof addr);
819 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
821 unhijack (dnsoutport);
823 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
827 /* port the code above? */
831 unsigned char buf[len];
832 struct dns_pkt *dns = (struct dns_pkt *) buf;
834 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
837 (struct sockaddr *) &addr, &addrlen);
841 unhijack (dnsoutport);
843 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
847 if (query_states[dns->s.id].valid == GNUNET_YES)
849 if (query_states[dns->s.id].tunnel != NULL)
851 uint32_t *c = GNUNET_malloc(4 + r);
854 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
857 GNUNET_TIME_UNIT_MINUTES,
858 NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
862 query_states[dns->s.id].valid = GNUNET_NO;
864 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
865 struct answer_packet_list *answer =
866 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
867 answer->pkt.hdr.type =
868 htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
869 answer->pkt.hdr.size = htons (len);
870 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
871 answer->pkt.from = addr.sin_addr.s_addr;
872 answer->pkt.to = query_states[dns->s.id].local_ip;
873 answer->pkt.dst_port = query_states[dns->s.id].local_port;
874 memcpy (answer->pkt.data, buf, r);
876 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
878 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
880 GNUNET_TIME_UNIT_FOREVER_REL,
882 query_states[dns->s.id].
887 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
888 dnsout, &read_response, NULL);
893 * Task run during shutdown.
899 cleanup_task (void *cls,
900 const struct GNUNET_SCHEDULER_TaskContext *tc)
902 unhijack(dnsoutport);
903 GNUNET_DHT_disconnect(dht);
907 * @brief Create a port-map from udp and tcp redirects
909 * @param udp_redirects
910 * @param tcp_redirects
915 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
918 char* cpy, *hostname, *redirect;
919 int local_port, count = 0;
921 if (NULL != udp_redirects)
923 cpy = GNUNET_strdup (udp_redirects);
924 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
926 if (NULL == (hostname = strstr (redirect, ":")))
928 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
932 local_port = atoi (redirect);
933 if (!((local_port > 0) && (local_port < 65536)))
934 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
936 ret |= (0xFFFF & htons(local_port));
950 if (NULL != tcp_redirects)
952 cpy = GNUNET_strdup (tcp_redirects);
953 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
955 if (NULL == (hostname = strstr (redirect, ":")))
957 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
961 local_port = atoi (redirect);
962 if (!((local_port > 0) && (local_port < 65536)))
963 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
965 ret |= (0xFFFF & htons(local_port));
986 publish_name (const char *name, uint64_t ports, uint32_t service_type,
987 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
989 size_t size = sizeof (struct GNUNET_DNS_Record);
990 struct GNUNET_DNS_Record data;
991 memset (&data, 0, size);
994 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
995 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
997 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
999 *((unsigned long long *) &data.service_descriptor));
1001 data.service_type = service_type;
1004 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1006 data.expiration_time =
1007 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1009 /* Sign the block */
1010 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1011 &data.purpose, &data.signature))
1013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Putting with key %08x, size = %d\n",
1019 *((unsigned int *) &data.service_descriptor), size);
1021 GNUNET_DHT_put (dht,
1022 &data.service_descriptor,
1023 DEFAULT_PUT_REPLICATION,
1025 GNUNET_BLOCK_TYPE_DNS,
1028 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1029 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1033 * @brief Publishes the record defined by the section section
1035 * @param cls closure
1036 * @param section the current section
1039 publish_iterate (void *cls, const char *section)
1041 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1044 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
1045 "UDP_REDIRECTS", &udp_redirects);
1046 GNUNET_CONFIGURATION_get_value_string (servicecfg, section, "TCP_REDIRECTS",
1049 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1054 if (keyfile != NULL)
1055 GNUNET_free (keyfile);
1059 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1060 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1061 GNUNET_free (keyfile);
1062 GNUNET_assert (my_private_key != NULL);
1064 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1065 uint32_t service_type = 0;
1067 if (NULL != udp_redirects)
1068 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1070 if (NULL != tcp_redirects)
1071 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1073 service_type = htonl (service_type);
1076 publish_name (section, ports, service_type, my_private_key);
1078 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
1079 "ALTERNATIVE_NAMES",
1080 &alternative_names);
1081 for (alternative_name = strtok (alternative_names, " ");
1082 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1085 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1086 strcpy (altname, alternative_name);
1087 strcpy (altname + strlen (alternative_name) + 1, section);
1088 altname[strlen (alternative_name)] = '.';
1090 publish_name (altname, ports, service_type, my_private_key);
1093 GNUNET_free_non_null(alternative_names);
1094 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1095 GNUNET_free_non_null (udp_redirects);
1096 GNUNET_free_non_null (tcp_redirects);
1100 * Publish a DNS-record in the DHT. This is up to now just for testing.
1103 publish_names (void *cls,
1104 const struct GNUNET_SCHEDULER_TaskContext *tc) {
1106 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1109 if (NULL != servicecfg)
1110 GNUNET_CONFIGURATION_destroy(servicecfg);
1112 GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
1114 servicecfg = GNUNET_CONFIGURATION_create();
1115 if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
1117 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
1118 GNUNET_CONFIGURATION_iterate_sections(servicecfg, publish_iterate, NULL);
1120 if (NULL != services)
1121 GNUNET_free(services);
1123 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1129 * @param cls closure
1130 * @param server the initialized server
1131 * @param cfg_ configuration to use
1135 struct GNUNET_SERVER_Handle *server,
1136 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1138 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1139 /* callback, cls, type, size */
1140 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1141 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1142 sizeof (struct GNUNET_MessageHeader)},
1146 static struct GNUNET_MESH_MessageHandler *mesh_handlers;
1148 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg_, "dns", "PROVIDE_EXIT"))
1149 mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
1150 {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1154 mesh_handlers = (struct GNUNET_MESH_MessageHandler[]) {
1155 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1159 const static GNUNET_MESH_ApplicationType apptypes[] =
1160 { GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1161 GNUNET_APPLICATION_TYPE_END
1164 mesh_handle = GNUNET_MESH_connect (cfg_, NULL, NULL, mesh_handlers, apptypes);
1169 for (i = 0; i < 65536; i++)
1171 query_states[i].valid = GNUNET_NO;
1174 dht = GNUNET_DHT_connect (cfg, 1024);
1178 GNUNET_SCHEDULER_add_now (publish_names, NULL);
1180 GNUNET_SERVER_add_handlers (server, handlers);
1181 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1182 &cleanup_task, cls);
1186 * The main function for the dns service.
1188 * @param argc number of arguments from the command line
1189 * @param argv command line arguments
1190 * @return 0 ok, 1 on error
1193 main (int argc, char *const *argv)
1195 return (GNUNET_OK ==
1196 GNUNET_SERVICE_run (argc,
1199 GNUNET_SERVICE_OPTION_NONE,
1200 &run, NULL)) ? 0 : 1;