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-vpn-packet.h"
33 #include "gnunet-vpn-pretty-print.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_signatures.h"
43 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
44 * sent through gnunet. The port of this socket will not be hijacked.
46 static struct GNUNET_NETWORK_Handle *dnsout;
49 * The port bound to the socket dnsout
51 static unsigned short dnsoutport;
54 * A handle to the DHT-Service
56 static struct GNUNET_DHT_Handle *dht;
59 * The configuration to use
61 static const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * A list of DNS-Responses that have to be sent to the requesting client
66 static struct answer_packet_list *head;
69 * The tail of the list of DNS-responses
71 static struct answer_packet_list *tail;
74 * A structure containing a mapping from network-byte-ordered DNS-id to
75 * some information needed to handle this query
77 * It currently allocates at least
78 * (1 + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
79 * = 1.7 MiB on 64 bit.
80 * = 1.2 MiB on 32 bit.
84 struct GNUNET_SERVER_Client* client;
86 unsigned remote_ip:32;
87 unsigned local_port:16;
90 } query_states[65536];
93 * A struct used to give more than one value as
94 * closure to receive_dht
96 struct receive_dht_cls {
98 struct GNUNET_DHT_GetHandle* handle;
102 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
105 hijack(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tc) {
109 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "VIRTDNS", &virt_dns))
111 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'VIRTDNS' in configuration!\n");
115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
116 snprintf(port_s, 6, "%d", dnsoutport);
117 GNUNET_OS_process_close (GNUNET_OS_start_process(NULL,
119 "gnunet-helper-hijack-dns",
127 * Delete the hijacking-routes
130 unhijack(unsigned short port) {
134 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "VIRTDNS", &virt_dns))
136 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'VIRTDNS' in configuration!\n");
140 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
141 snprintf(port_s, 6, "%d", port);
142 GNUNET_OS_start_process(NULL,
144 "gnunet-helper-hijack-dns",
153 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
157 send_answer(void* cls, size_t size, void* buf) {
158 struct answer_packet_list* query = head;
159 size_t len = ntohs(query->pkt.hdr.size);
161 GNUNET_assert(len <= size);
163 memcpy(buf, &query->pkt.hdr, len);
165 GNUNET_CONTAINER_DLL_remove (head, tail, query);
169 /* When more data is to be sent, reschedule */
171 GNUNET_SERVER_notify_transmit_ready(cls,
172 ntohs(head->pkt.hdr.size),
173 GNUNET_TIME_UNIT_FOREVER_REL,
181 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
182 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
184 unsigned short id = pdns->s.id;
186 if (query_states[id].valid != GNUNET_YES) return;
187 query_states[id].valid = GNUNET_NO;
189 GNUNET_assert(query_states[id].namelen == 74);
191 size_t len = sizeof(struct answer_packet) - 1 \
192 + sizeof(struct dns_static) \
193 + 74 /* this is the length of a reverse ipv6-lookup */ \
194 + sizeof(struct dns_query_line) \
195 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
196 + sizeof(struct dns_record_line) - 1 \
197 - 2 /* We do not know the lenght of the answer yet*/ \
198 - 2 /* No idea why... */ ;
200 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
201 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
203 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
204 answer->pkt.hdr.size = htons(len);
205 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
207 answer->pkt.from = query_states[id].remote_ip;
209 answer->pkt.to = query_states[id].local_ip;
210 answer->pkt.dst_port = query_states[id].local_port;
212 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
218 dpkt->s.qdcount = htons(1);
219 dpkt->s.ancount = htons(1);
221 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
222 GNUNET_free(query_states[id].name);
224 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
225 dque->type = htons(12); /* PTR */
226 dque->class = htons(1); /* IN */
228 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
229 memcpy(anname, (char[]){0xc0, 0x0c}, 2);
231 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
232 drec_data->type = htons(12); /* AAAA */
233 drec_data->class = htons(1); /* IN */
234 drec_data->ttl = htonl(3600); /* FIXME: read from block */
236 /* Calculate at which offset in the packet the length of the name and the
237 * name, it is filled in by the daemon-vpn */
238 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
240 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
242 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
244 GNUNET_TIME_UNIT_FOREVER_REL,
246 query_states[id].client);
250 * complete dns-packet with empty name in the answer
251 * provide offsett of the name
256 * Receive a block from the dht.
259 receive_dht(void *cls,
260 struct GNUNET_TIME_Absolute exp,
261 const GNUNET_HashCode *key,
262 const struct GNUNET_PeerIdentity *const *get_path,
263 const struct GNUNET_PeerIdentity *const *put_path,
264 enum GNUNET_BLOCK_Type type,
268 unsigned short id = ((struct receive_dht_cls*)cls)->id;
269 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
272 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
274 /* If no query with this id is pending, ignore the block */
275 if (query_states[id].valid != GNUNET_YES) return;
276 query_states[id].valid = GNUNET_NO;
278 const struct GNUNET_DNS_Record* rec = data;
279 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
280 "Got block of size %d, peer: %08x, desc: %08x\n",
282 *((unsigned int*)&rec->peer),
283 *((unsigned int*)&rec->service_descriptor));
285 size_t len = sizeof(struct answer_packet) - 1 \
286 + sizeof(struct dns_static) \
287 + query_states[id].namelen \
288 + sizeof(struct dns_query_line) \
289 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
290 + sizeof(struct dns_record_line) - 1 \
291 + 16; /* To hold the IPv6-Address */
293 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
294 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
296 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
297 answer->pkt.hdr.size = htons(len);
298 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
300 GNUNET_CRYPTO_hash(&rec->peer,
301 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
302 &answer->pkt.service_descr.peer);
304 memcpy(&answer->pkt.service_descr.service_descriptor,
305 &rec->service_descriptor,
306 sizeof(GNUNET_HashCode));
307 memcpy(&answer->pkt.service_descr.service_type,
309 sizeof(answer->pkt.service_descr.service_type));
310 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
312 answer->pkt.from = query_states[id].remote_ip;
314 answer->pkt.to = query_states[id].local_ip;
315 answer->pkt.dst_port = query_states[id].local_port;
317 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
323 dpkt->s.qdcount = htons(1);
324 dpkt->s.ancount = htons(1);
326 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
327 GNUNET_free(query_states[id].name);
329 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
330 dque->type = htons(28); /* AAAA */
331 dque->class = htons(1); /* IN */
333 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
334 memcpy(anname, (char[]){0xc0, 0x0c}, 2);
336 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
337 drec_data->type = htons(28); /* AAAA */
338 drec_data->class = htons(1); /* IN */
339 drec_data->ttl = htonl(3600); /* FIXME: read from block */
340 drec_data->data_len = htons(16);
342 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
343 * filled in by the daemon-vpn */
344 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
346 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
348 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
350 GNUNET_TIME_UNIT_FOREVER_REL,
352 query_states[id].client);
354 GNUNET_DHT_get_stop(handle);
358 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
362 struct GNUNET_SERVER_Client *client,
363 const struct GNUNET_MessageHeader *message) {
364 unhijack(dnsoutport);
365 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
367 GNUNET_SERVER_receive_done(client, GNUNET_OK);
371 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
374 receive_query(void *cls,
375 struct GNUNET_SERVER_Client *client,
376 const struct GNUNET_MessageHeader *message) {
377 struct query_packet* pkt = (struct query_packet*)message;
378 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
379 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
381 query_states[dns->s.id].valid = GNUNET_YES;
382 query_states[dns->s.id].client = client;
383 query_states[dns->s.id].local_ip = pkt->orig_from;
384 query_states[dns->s.id].local_port = pkt->src_port;
385 query_states[dns->s.id].remote_ip = pkt->orig_to;
386 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
387 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
388 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
390 /* The query is for a .gnunet-address */
391 if (pdns->queries[0]->namelen > 9 &&
392 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
394 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
396 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
398 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
399 "Getting with key %08x, len is %d\n",
400 *((unsigned int*)&key),
401 pdns->queries[0]->namelen);
403 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
406 cls->handle = GNUNET_DHT_get_start(dht,
407 GNUNET_TIME_UNIT_MINUTES,
408 GNUNET_BLOCK_TYPE_DNS,
410 DEFAULT_GET_REPLICATION,
422 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
423 /* The query is for a PTR of a previosly resolved virtual IP */
424 if (htons(pdns->queries[0]->qtype) == 12 &&
425 74 == pdns->queries[0]->namelen)
429 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.";
431 unsigned long long ipv6prefix;
432 unsigned int comparelen;
434 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
435 inet_pton (AF_INET6, ipv6addr, ipv6);
436 GNUNET_free(ipv6addr);
438 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
439 GNUNET_assert(ipv6prefix < 127);
440 ipv6prefix = (ipv6prefix + 7)/8;
442 for (i = ipv6prefix; i < 16; i++)
445 for (i = 0; i < 16; i++)
447 unsigned char c1 = ipv6[i] >> 4;
448 unsigned char c2 = ipv6[i] & 0xf;
451 ipv6rev[62-(4*i)] = c1 + '0';
453 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
456 ipv6rev[62-((4*i)+2)] = c2 + '0';
458 ipv6rev[62-((4*i)+2)] = c2 + 87;
460 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
461 comparelen = 10 + 4*ipv6prefix;
462 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
463 ipv6rev + (74 - comparelen),
466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
468 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
474 /* The query should be sent to the network */
476 struct sockaddr_in dest;
477 memset(&dest, 0, sizeof dest);
478 dest.sin_port = htons(53);
479 dest.sin_addr.s_addr = pkt->orig_to;
481 GNUNET_NETWORK_socket_sendto(dnsout,
483 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
484 (struct sockaddr*) &dest,
488 free_parsed_dns_packet(pdns);
491 GNUNET_SERVER_receive_done(client, GNUNET_OK);
495 * Read a response-packet of the UDP-Socket
498 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
499 unsigned char buf[65536];
500 struct dns_pkt* dns = (struct dns_pkt*)buf;
502 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
505 struct sockaddr_in addr;
506 memset(&addr, 0, sizeof addr);
507 socklen_t addrlen = sizeof addr;
510 r = GNUNET_NETWORK_socket_recvfrom(dnsout,
513 (struct sockaddr*)&addr,
516 /* if (r < 0) FIXME */
518 if (query_states[dns->s.id].valid == GNUNET_YES) {
519 query_states[dns->s.id].valid = GNUNET_NO;
521 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
522 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
523 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
524 answer->pkt.hdr.size = htons(len);
525 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
526 answer->pkt.from = addr.sin_addr.s_addr;
527 answer->pkt.to = query_states[dns->s.id].local_ip;
528 answer->pkt.dst_port = query_states[dns->s.id].local_port;
529 memcpy(answer->pkt.data, buf, r);
531 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
533 GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client,
535 GNUNET_TIME_UNIT_FOREVER_REL,
537 query_states[dns->s.id].client);
540 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
548 * Task run during shutdown.
554 cleanup_task (void *cls,
555 const struct GNUNET_SCHEDULER_TaskContext *tc)
557 unhijack(dnsoutport);
558 GNUNET_DHT_disconnect(dht);
562 * Publish a DNS-record in the DHT. This is up to now just for testing.
565 publish_name (void *cls,
566 const struct GNUNET_SCHEDULER_TaskContext *tc) {
567 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
570 char* name = "philipptoelke.gnunet.";
571 size_t size = sizeof(struct GNUNET_DNS_Record);
572 struct GNUNET_DNS_Record data;
573 memset(&data, 0, size);
575 data.purpose.size = htonl(size - sizeof(struct GNUNET_CRYPTO_RsaSignature));
576 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
578 GNUNET_CRYPTO_hash(name, strlen(name)+1, &data.service_descriptor);
579 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n", *((unsigned long long*)&data.service_descriptor));
581 data.service_type = htonl(GNUNET_DNS_SERVICE_TYPE_UDP);
582 data.ports = htons(69);
585 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg, "GNUNETD",
586 "HOSTKEY", &keyfile))
588 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
589 if (keyfile != NULL) GNUNET_free(keyfile);
593 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file(keyfile);
594 GNUNET_free(keyfile);
595 GNUNET_assert(my_private_key != NULL);
597 GNUNET_CRYPTO_rsa_key_get_public(my_private_key, &data.peer);
599 data.expiration_time = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS);
602 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign(my_private_key,
606 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
609 GNUNET_CRYPTO_rsa_key_free(my_private_key);
611 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
612 "Putting with key %08x, size = %d\n",
613 *((unsigned int*)&data.service_descriptor),
617 &data.service_descriptor,
618 DEFAULT_PUT_REPLICATION,
620 GNUNET_BLOCK_TYPE_DNS,
623 GNUNET_TIME_relative_to_absolute(GNUNET_TIME_UNIT_HOURS),
624 GNUNET_TIME_UNIT_MINUTES,
628 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
635 * @param server the initialized server
636 * @param cfg_ configuration to use
640 struct GNUNET_SERVER_Handle *server,
641 const struct GNUNET_CONFIGURATION_Handle *cfg_)
643 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
644 /* callback, cls, type, size */
645 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
646 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
653 for (i = 0; i < 65536; i++) {
654 query_states[i].valid = GNUNET_NO;
657 dht = GNUNET_DHT_connect(cfg, 1024);
659 struct sockaddr_in addr;
661 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
664 memset(&addr, 0, sizeof(struct sockaddr_in));
666 int err = GNUNET_NETWORK_socket_bind (dnsout,
667 (struct sockaddr*)&addr,
668 sizeof(struct sockaddr_in));
670 if (err != GNUNET_YES) {
671 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not bind a port, exiting\n");
675 /* Read the port we bound to */
676 socklen_t addrlen = sizeof(struct sockaddr_in);
677 err = getsockname(GNUNET_NETWORK_get_fd(dnsout),
678 (struct sockaddr*) &addr,
681 dnsoutport = htons(addr.sin_port);
683 GNUNET_SCHEDULER_add_now (publish_name, NULL);
685 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, dnsout, &read_response, NULL);
687 GNUNET_SERVER_add_handlers (server, handlers);
688 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
694 * The main function for the dns service.
696 * @param argc number of arguments from the command line
697 * @param argv command line arguments
698 * @return 0 ok, 1 on error
701 main (int argc, char *const *argv)
704 GNUNET_SERVICE_run (argc,
707 GNUNET_SERVICE_OPTION_NONE,
708 &run, NULL)) ? 0 : 1;