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 * A list of DNS-Responses that have to be sent to the requesting client
69 static struct answer_packet_list *head;
72 * The tail of the list of DNS-responses
74 static struct answer_packet_list *tail;
77 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
78 * some information needed to handle this query
80 * It currently allocates at least
81 * (1 + machine-width + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
87 struct GNUNET_SERVER_Client* client;
88 struct GNUNET_MESH_Tunnel *tunnel;
94 } query_states[UINT16_MAX];
97 * A struct used to give more than one value as
98 * closure to receive_dht
100 struct receive_dht_cls {
102 struct GNUNET_DHT_GetHandle* handle;
106 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
109 hijack (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
111 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
116 struct GNUNET_OS_Process *proc;
119 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
123 "No entry 'VIRTDNS' in configuration!\n");
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
128 snprintf (port_s, 6, "%d", dnsoutport);
129 if (NULL != (proc = GNUNET_OS_start_process (NULL,
131 "gnunet-helper-hijack-dns",
133 port_s, virt_dns, NULL)))
134 GNUNET_OS_process_close (proc);
135 GNUNET_free (virt_dns);
139 * Delete the hijacking-routes
142 unhijack (unsigned short port)
146 struct GNUNET_OS_Process *proc;
149 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 "No entry 'VIRTDNS' in configuration!\n");
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
158 snprintf (port_s, 6, "%d", port);
159 if (NULL != (proc = GNUNET_OS_start_process (NULL,
161 "gnunet-helper-hijack-dns",
163 "-d", port_s, virt_dns, NULL)))
164 GNUNET_OS_process_close (proc);
165 GNUNET_free (virt_dns);
169 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
173 send_answer(void* cls, size_t size, void* buf) {
174 struct answer_packet_list* query = head;
175 size_t len = ntohs(query->pkt.hdr.size);
177 GNUNET_assert(len <= size);
179 memcpy(buf, &query->pkt.hdr, len);
181 GNUNET_CONTAINER_DLL_remove (head, tail, query);
185 /* When more data is to be sent, reschedule */
187 GNUNET_SERVER_notify_transmit_ready(cls,
188 ntohs(head->pkt.hdr.size),
189 GNUNET_TIME_UNIT_FOREVER_REL,
197 struct GNUNET_MESH_Tunnel *tunnel GNUNET_PACKED;
198 struct GNUNET_MessageHeader hdr;
202 struct tunnel_cls *remote_pending[UINT16_MAX];
205 mesh_send_response (void *cls, size_t size, void *buf)
207 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
208 struct GNUNET_MessageHeader *hdr = buf;
210 struct dns_pkt *dns = (struct dns_pkt *) (sz + 1);
211 hdr->type = htons (GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS);
212 hdr->size = htons (*sz + sizeof (struct GNUNET_MessageHeader));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Sending response, size=%d, sz=%d, sz+hdr=%d\n", size, *sz,
216 *sz + sizeof (struct GNUNET_MessageHeader));
218 GNUNET_assert (size >= (*sz + sizeof (struct GNUNET_MessageHeader)));
220 memcpy (hdr + 1, dns, *sz);
222 return ntohs (hdr->size);
226 mesh_send (void *cls, size_t size, void *buf)
228 struct tunnel_cls *cls_ = (struct tunnel_cls *) cls;
230 GNUNET_assert(cls_->hdr.size <= size);
232 size = cls_->hdr.size;
233 cls_->hdr.size = htons(cls_->hdr.size);
235 memcpy(buf, &cls_->hdr, size);
240 void mesh_connect (void* cls, const struct GNUNET_PeerIdentity* peer, const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused))) {
241 if (NULL == peer) return;
242 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected to peer %s, sending query with id %d\n", GNUNET_i2s(peer), ntohs(cls_->dns.s.id));
245 GNUNET_MESH_notify_transmit_ready(cls_->tunnel,
248 GNUNET_TIME_UNIT_MINUTES,
257 send_mesh_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
262 struct tunnel_cls *cls_ = (struct tunnel_cls*)cls;
264 cls_->tunnel = GNUNET_MESH_peer_request_connect_by_type(mesh_handle,
265 GNUNET_TIME_UNIT_HOURS,
266 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
271 remote_pending[cls_->dns.s.id] = cls_;
275 receive_mesh_query (void *cls __attribute__((unused)),
276 struct GNUNET_MESH_Tunnel *tunnel,
277 void **ctx __attribute__((unused)),
278 const struct GNUNET_PeerIdentity *sender __attribute__((unused)),
279 const struct GNUNET_MessageHeader *message,
280 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
282 struct dns_pkt *dns = (struct dns_pkt*)(message + 1);
284 struct sockaddr_in dest;
285 memset(&dest, 0, sizeof dest);
286 dest.sin_port = htons(53);
287 /* TODO: read from config */
288 inet_pton(AF_INET, "8.8.8.8", &dest.sin_addr);
290 query_states[dns->s.id].tunnel = tunnel;
291 query_states[dns->s.id].valid = GNUNET_YES;
293 GNUNET_NETWORK_socket_sendto(dnsout,
295 ntohs(message->size) - sizeof(struct GNUNET_MessageHeader),
296 (struct sockaddr*) &dest,
299 return GNUNET_SYSERR;
303 receive_mesh_answer (void *cls __attribute__((unused)),
304 struct GNUNET_MESH_Tunnel *tunnel,
305 void **ctx __attribute__((unused)),
306 const struct GNUNET_PeerIdentity *sender,
307 const struct GNUNET_MessageHeader *message,
308 const struct GNUNET_TRANSPORT_ATS_Information *atsi __attribute__((unused)))
310 /* TODo: size check */
311 struct dns_pkt *dns = (struct dns_pkt *) (message + 1);
313 /* They sent us a packet we were not waiting for */
314 if (remote_pending[dns->s.id] == NULL
315 || remote_pending[dns->s.id]->tunnel != tunnel)
316 return GNUNET_SYSERR;
317 if (query_states[dns->s.id].valid != GNUNET_YES)
318 return GNUNET_SYSERR;
319 query_states[dns->s.id].valid = GNUNET_NO;
321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received answer from peer %s, dns-id %d\n", GNUNET_i2s(sender), ntohs(dns->s.id));
323 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 */
324 + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */
326 struct answer_packet_list *answer =
327 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
328 memset (answer, 0, len + 2 * sizeof (struct answer_packet_list *));
330 answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
331 answer->pkt.hdr.size = htons (len);
332 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REMOTE;
334 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
336 if (ntohs(pdns->s.ancount) < 1)
338 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Answer only contains %d answers.\n", ntohs(pdns->s.ancount));
339 free_parsed_dns_packet(pdns);
343 answer->pkt.addrsize = pdns->answers[0]->data_len;
344 memcpy(answer->pkt.addr, pdns->answers[0]->data, ntohs(pdns->answers[0]->data_len));
346 answer->pkt.from = query_states[dns->s.id].remote_ip;
348 answer->pkt.to = query_states[dns->s.id].local_ip;
349 answer->pkt.dst_port = query_states[dns->s.id].local_port;
351 struct dns_pkt *dpkt = (struct dns_pkt *) answer->pkt.data;
353 dpkt->s.id = dns->s.id;
357 dpkt->s.qdcount = htons (1);
358 dpkt->s.ancount = htons (1);
360 memcpy (dpkt->data, query_states[dns->s.id].name,
361 query_states[dns->s.id].namelen);
362 GNUNET_free (query_states[dns->s.id].name);
364 struct dns_query_line *dque =
365 (struct dns_query_line *) (dpkt->data +
366 (query_states[dns->s.id].namelen));
367 dque->type = htons (28); /* AAAA */
368 dque->class = htons (1); /* IN */
371 (char *) (dpkt->data + (query_states[dns->s.id].namelen) +
372 sizeof (struct dns_query_line));
373 memcpy (anname, "\xc0\x0c", 2);
375 struct dns_record_line *drec_data =
376 (struct dns_record_line *) (dpkt->data +
377 (query_states[dns->s.id].namelen) +
378 sizeof (struct dns_query_line) + 2);
379 drec_data->type = htons (28); /* AAAA */
380 drec_data->class = htons (1); /* IN */
382 drec_data->ttl = pdns->answers[0]->ttl;
383 drec_data->data_len = htons (16);
385 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
386 * filled in by the daemon-vpn */
387 answer->pkt.addroffset =
388 htons ((unsigned short) ((unsigned long) (&drec_data->data) -
389 (unsigned long) (&answer->pkt)));
391 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
393 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client,
395 GNUNET_TIME_UNIT_FOREVER_REL,
397 query_states[dns->s.id].client);
399 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sent answer on to client\n");
401 free_parsed_dns_packet(pdns);
407 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
408 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
411 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
413 unsigned short id = pdns->s.id;
415 free_parsed_dns_packet(pdns);
417 if (query_states[id].valid != GNUNET_YES) return;
418 query_states[id].valid = GNUNET_NO;
420 GNUNET_assert(query_states[id].namelen == 74);
422 size_t len = sizeof(struct answer_packet) - 1 \
423 + sizeof(struct dns_static) \
424 + 74 /* this is the length of a reverse ipv6-lookup */ \
425 + sizeof(struct dns_query_line) \
426 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
427 + sizeof(struct dns_record_line) - 1 \
428 - 2 /* We do not know the lenght of the answer yet*/;
430 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
431 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
433 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
434 answer->pkt.hdr.size = htons(len);
435 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
437 answer->pkt.from = query_states[id].remote_ip;
439 answer->pkt.to = query_states[id].local_ip;
440 answer->pkt.dst_port = query_states[id].local_port;
442 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
448 dpkt->s.qdcount = htons(1);
449 dpkt->s.ancount = htons(1);
451 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
452 GNUNET_free(query_states[id].name);
454 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
455 dque->type = htons(12); /* PTR */
456 dque->class = htons(1); /* IN */
458 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
459 memcpy(anname, "\xc0\x0c", 2);
461 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
462 drec_data->type = htons(12); /* AAAA */
463 drec_data->class = htons(1); /* IN */
464 /* FIXME: read the TTL from block:
465 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
467 * But how to get the seconds out of this?
469 drec_data->ttl = htonl(3600);
471 /* Calculate at which offset in the packet the length of the name and the
472 * name, it is filled in by the daemon-vpn */
473 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
475 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
477 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
479 GNUNET_TIME_UNIT_FOREVER_REL,
481 query_states[id].client);
485 * Receive a block from the dht.
488 receive_dht(void *cls,
489 struct GNUNET_TIME_Absolute exp __attribute__((unused)),
490 const GNUNET_HashCode *key __attribute__((unused)),
491 const struct GNUNET_PeerIdentity *const *get_path __attribute__((unused)),
492 const struct GNUNET_PeerIdentity *const *put_path __attribute__((unused)),
493 enum GNUNET_BLOCK_Type type,
497 unsigned short id = ((struct receive_dht_cls*)cls)->id;
498 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
501 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
503 /* If no query with this id is pending, ignore the block */
504 if (query_states[id].valid != GNUNET_YES) return;
505 query_states[id].valid = GNUNET_NO;
507 const struct GNUNET_DNS_Record* rec = data;
508 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
509 "Got block of size %d, peer: %08x, desc: %08x\n",
511 *((unsigned int*)&rec->peer),
512 *((unsigned int*)&rec->service_descriptor));
514 size_t len = sizeof(struct answer_packet) - 1 \
515 + sizeof(struct dns_static) \
516 + query_states[id].namelen \
517 + sizeof(struct dns_query_line) \
518 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
519 + sizeof(struct dns_record_line) - 1 \
520 + 16; /* To hold the IPv6-Address */
522 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
523 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
525 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
526 answer->pkt.hdr.size = htons(len);
527 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
529 GNUNET_CRYPTO_hash(&rec->peer,
530 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
531 &answer->pkt.service_descr.peer);
533 memcpy(&answer->pkt.service_descr.service_descriptor,
534 &rec->service_descriptor,
535 sizeof(GNUNET_HashCode));
536 memcpy(&answer->pkt.service_descr.service_type,
538 sizeof(answer->pkt.service_descr.service_type));
539 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
541 answer->pkt.from = query_states[id].remote_ip;
543 answer->pkt.to = query_states[id].local_ip;
544 answer->pkt.dst_port = query_states[id].local_port;
546 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
552 dpkt->s.qdcount = htons(1);
553 dpkt->s.ancount = htons(1);
555 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
556 GNUNET_free(query_states[id].name);
558 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
559 dque->type = htons(28); /* AAAA */
560 dque->class = htons(1); /* IN */
562 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
563 memcpy(anname, "\xc0\x0c", 2);
565 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
566 drec_data->type = htons(28); /* AAAA */
567 drec_data->class = htons(1); /* IN */
569 /* FIXME: read the TTL from block:
570 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
572 * But how to get the seconds out of this?
574 drec_data->ttl = htonl(3600);
575 drec_data->data_len = htons(16);
577 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
578 * filled in by the daemon-vpn */
579 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
581 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
583 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
585 GNUNET_TIME_UNIT_FOREVER_REL,
587 query_states[id].client);
589 GNUNET_DHT_get_stop(handle);
593 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
596 rehijack(void *cls __attribute__((unused)),
597 struct GNUNET_SERVER_Client *client,
598 const struct GNUNET_MessageHeader *message __attribute__((unused))) {
599 unhijack(dnsoutport);
600 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
602 GNUNET_SERVER_receive_done(client, GNUNET_OK);
606 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
609 receive_query(void *cls __attribute__((unused)),
610 struct GNUNET_SERVER_Client *client,
611 const struct GNUNET_MessageHeader *message) {
612 struct query_packet* pkt = (struct query_packet*)message;
613 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
614 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
616 query_states[dns->s.id].valid = GNUNET_YES;
617 query_states[dns->s.id].client = client;
618 query_states[dns->s.id].local_ip = pkt->orig_from;
619 query_states[dns->s.id].local_port = pkt->src_port;
620 query_states[dns->s.id].remote_ip = pkt->orig_to;
621 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
622 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
623 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
625 /* The query is for a .gnunet-address */
626 if (pdns->queries[0]->namelen > 9 &&
627 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
629 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
631 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
633 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
634 "Getting with key %08x, len is %d\n",
635 *((unsigned int*)&key),
636 pdns->queries[0]->namelen);
638 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
641 cls->handle = GNUNET_DHT_get_start(dht,
642 GNUNET_TIME_UNIT_MINUTES,
643 GNUNET_BLOCK_TYPE_DNS,
645 DEFAULT_GET_REPLICATION,
657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
659 /* This is a PTR-Query. Check if it is for "our" network */
660 if (htons(pdns->queries[0]->qtype) == 12 &&
661 74 == pdns->queries[0]->namelen)
665 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.";
667 unsigned long long ipv6prefix;
668 unsigned int comparelen;
670 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
671 inet_pton (AF_INET6, ipv6addr, ipv6);
672 GNUNET_free(ipv6addr);
674 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
675 GNUNET_assert(ipv6prefix < 127);
676 ipv6prefix = (ipv6prefix + 7)/8;
678 for (i = ipv6prefix; i < 16; i++)
681 for (i = 0; i < 16; i++)
683 unsigned char c1 = ipv6[i] >> 4;
684 unsigned char c2 = ipv6[i] & 0xf;
687 ipv6rev[62-(4*i)] = c1 + '0';
689 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
692 ipv6rev[62-((4*i)+2)] = c2 + '0';
694 ipv6rev[62-((4*i)+2)] = c2 + 87;
696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
697 comparelen = 10 + 4*ipv6prefix;
698 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
699 ipv6rev + (74 - comparelen),
702 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
704 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
711 unsigned int virt_dns_bytes;
713 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
717 "No entry 'VIRTDNS' in configuration!\n");
721 if (1 != inet_pton (AF_INET, virt_dns, &virt_dns_bytes))
723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
724 "Error parsing 'VIRTDNS': %s; %m!\n", virt_dns);
728 GNUNET_free(virt_dns);
730 if (virt_dns_bytes == pkt->orig_to)
732 /* This is a packet that was sent directly to the virtual dns-server
734 * This means we have to send this query over gnunet
737 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + (ntohs(message->size) - sizeof(struct query_packet) + 1);
738 struct tunnel_cls *cls_ = GNUNET_malloc(size);
739 cls_->hdr.size = size - sizeof(struct GNUNET_MESH_Tunnel*);
741 cls_->hdr.type = ntohs(GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS);
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "size: %d\n", size);
744 memcpy(&cls_->dns, dns, cls_->hdr.size - sizeof(struct GNUNET_MessageHeader));
745 GNUNET_SCHEDULER_add_now(send_mesh_query, cls_);
751 /* The query should be sent to the network */
753 struct sockaddr_in dest;
754 memset(&dest, 0, sizeof dest);
755 dest.sin_port = htons(53);
756 dest.sin_addr.s_addr = pkt->orig_to;
758 GNUNET_NETWORK_socket_sendto(dnsout,
760 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
761 (struct sockaddr*) &dest,
765 free_parsed_dns_packet(pdns);
768 GNUNET_SERVER_receive_done(client, GNUNET_OK);
771 static void read_response (void *cls,
772 const struct GNUNET_SCHEDULER_TaskContext *tc);
777 struct sockaddr_in addr;
779 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
782 memset (&addr, 0, sizeof (struct sockaddr_in));
784 int err = GNUNET_NETWORK_socket_bind (dnsout,
785 (struct sockaddr *) &addr,
786 sizeof (struct sockaddr_in));
788 if (err != GNUNET_YES)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 "Could not bind a port, exiting\n");
795 /* Read the port we bound to */
796 socklen_t addrlen = sizeof (struct sockaddr_in);
797 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
798 (struct sockaddr *) &addr, &addrlen);
800 dnsoutport = htons (addr.sin_port);
802 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
803 &read_response, NULL);
807 * Read a response-packet of the UDP-Socket
810 read_response (void *cls __attribute__((unused)), const struct GNUNET_SCHEDULER_TaskContext *tc)
812 struct sockaddr_in addr;
813 socklen_t addrlen = sizeof (addr);
817 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
820 memset (&addr, 0, sizeof addr);
823 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout), FIONREAD, &len))
825 unhijack (dnsoutport);
827 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
831 /* port the code above? */
835 unsigned char buf[len];
836 struct dns_pkt *dns = (struct dns_pkt *) buf;
838 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
841 (struct sockaddr *) &addr, &addrlen);
845 unhijack (dnsoutport);
847 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
851 if (query_states[dns->s.id].valid == GNUNET_YES)
853 if (query_states[dns->s.id].tunnel != NULL)
855 uint32_t *c = GNUNET_malloc(4 + r);
858 GNUNET_MESH_notify_transmit_ready (query_states[dns->s.id].tunnel,
861 GNUNET_TIME_UNIT_MINUTES,
862 NULL, r + sizeof(struct GNUNET_MessageHeader), mesh_send_response, c);
866 query_states[dns->s.id].valid = GNUNET_NO;
868 size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
869 struct answer_packet_list *answer =
870 GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *));
871 answer->pkt.hdr.type =
872 htons (GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
873 answer->pkt.hdr.size = htons (len);
874 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
875 answer->pkt.from = addr.sin_addr.s_addr;
876 answer->pkt.to = query_states[dns->s.id].local_ip;
877 answer->pkt.dst_port = query_states[dns->s.id].local_port;
878 memcpy (answer->pkt.data, buf, r);
880 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer);
882 GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].
884 GNUNET_TIME_UNIT_FOREVER_REL,
886 query_states[dns->s.id].
891 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
892 dnsout, &read_response, NULL);
897 * Task run during shutdown.
903 cleanup_task (void *cls __attribute__((unused)),
904 const struct GNUNET_SCHEDULER_TaskContext *tc)
906 GNUNET_assert(0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN));
908 unhijack(dnsoutport);
909 GNUNET_DHT_disconnect(dht);
913 * @brief Create a port-map from udp and tcp redirects
915 * @param udp_redirects
916 * @param tcp_redirects
921 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
924 char* cpy, *hostname, *redirect;
925 int local_port, count = 0;
927 if (NULL != udp_redirects)
929 cpy = GNUNET_strdup (udp_redirects);
930 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
932 if (NULL == (hostname = strstr (redirect, ":")))
934 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
938 local_port = atoi (redirect);
939 if (!((local_port > 0) && (local_port < 65536)))
940 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
942 ret |= (0xFFFF & htons(local_port));
956 if (NULL != tcp_redirects)
958 cpy = GNUNET_strdup (tcp_redirects);
959 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
961 if (NULL == (hostname = strstr (redirect, ":")))
963 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
967 local_port = atoi (redirect);
968 if (!((local_port > 0) && (local_port < 65536)))
969 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
971 ret |= (0xFFFF & htons(local_port));
992 publish_name (const char *name, uint64_t ports, uint32_t service_type,
993 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
995 size_t size = sizeof (struct GNUNET_DNS_Record);
996 struct GNUNET_DNS_Record data;
997 memset (&data, 0, size);
1000 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
1001 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
1003 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
1005 *((unsigned long long *) &data.service_descriptor));
1007 data.service_type = service_type;
1010 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
1012 data.expiration_time =
1013 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
1015 /* Sign the block */
1016 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
1017 &data.purpose, &data.signature))
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Putting with key %08x, size = %d\n",
1025 *((unsigned int *) &data.service_descriptor), size);
1027 GNUNET_DHT_put (dht,
1028 &data.service_descriptor,
1029 DEFAULT_PUT_REPLICATION,
1031 GNUNET_BLOCK_TYPE_DNS,
1034 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
1035 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
1039 * @brief Publishes the record defined by the section section
1041 * @param cls closure
1042 * @param section the current section
1045 publish_iterate (void *cls __attribute__((unused)), const char *section)
1047 if ((strlen(section) < 8) || (0 != strcmp (".gnunet.", section + (strlen(section) - 8))))
1050 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Parsing dns-name %s\n", section);
1052 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
1055 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1056 "UDP_REDIRECTS", &udp_redirects);
1057 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
1060 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
1064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
1065 if (keyfile != NULL)
1066 GNUNET_free (keyfile);
1070 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
1071 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1072 GNUNET_free (keyfile);
1073 GNUNET_assert (my_private_key != NULL);
1075 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
1076 uint32_t service_type = 0;
1078 if (NULL != udp_redirects)
1079 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
1081 if (NULL != tcp_redirects)
1082 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
1084 service_type = htonl (service_type);
1087 publish_name (section, ports, service_type, my_private_key);
1089 GNUNET_CONFIGURATION_get_value_string (cfg, section,
1090 "ALTERNATIVE_NAMES",
1091 &alternative_names);
1092 for (alternative_name = strtok (alternative_names, " ");
1093 alternative_name != NULL; alternative_name = strtok (NULL, " "))
1096 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
1097 strcpy (altname, alternative_name);
1098 strcpy (altname + strlen (alternative_name) + 1, section);
1099 altname[strlen (alternative_name)] = '.';
1101 publish_name (altname, ports, service_type, my_private_key);
1104 GNUNET_free_non_null(alternative_names);
1105 GNUNET_CRYPTO_rsa_key_free (my_private_key);
1106 GNUNET_free_non_null (udp_redirects);
1107 GNUNET_free_non_null (tcp_redirects);
1111 * Publish a DNS-record in the DHT.
1114 publish_names (void *cls __attribute__((unused)),
1115 const struct GNUNET_SCHEDULER_TaskContext *tc) {
1116 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1119 GNUNET_CONFIGURATION_iterate_sections(cfg, publish_iterate, NULL);
1121 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
1127 * @param cls closure
1128 * @param server the initialized server
1129 * @param cfg_ configuration to use
1133 struct GNUNET_SERVER_Handle *server,
1134 const struct GNUNET_CONFIGURATION_Handle *cfg_)
1136 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1137 /* callback, cls, type, size */
1138 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
1139 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK,
1140 sizeof (struct GNUNET_MessageHeader)},
1144 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1145 {receive_mesh_query, GNUNET_MESSAGE_TYPE_REMOTE_QUERY_DNS, 0},
1146 {receive_mesh_answer, GNUNET_MESSAGE_TYPE_REMOTE_ANSWER_DNS, 0},
1150 static GNUNET_MESH_ApplicationType *apptypes;
1153 GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT"))
1154 apptypes = (GNUNET_MESH_ApplicationType[])
1156 GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
1157 GNUNET_APPLICATION_TYPE_END};
1159 apptypes = (GNUNET_MESH_ApplicationType[])
1161 GNUNET_APPLICATION_TYPE_END};
1164 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;