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_container_lib.h"
34 #include "gnunet-dns-parser.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_block_lib.h"
37 #include "block_dns.h"
38 #include "gnunet_crypto_lib.h"
39 #include "gnunet_signatures.h"
42 * The UDP-Socket through which DNS-Resolves will be sent if they are not to be
43 * sent through gnunet. The port of this socket will not be hijacked.
45 static struct GNUNET_NETWORK_Handle *dnsout;
48 * The port bound to the socket dnsout
50 static unsigned short dnsoutport;
53 * A handle to the DHT-Service
55 static struct GNUNET_DHT_Handle *dht;
58 * The configuration to use
60 static const struct GNUNET_CONFIGURATION_Handle *cfg;
63 * The handle to the service-configuration
65 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
68 * A list of DNS-Responses that have to be sent to the requesting client
70 static struct answer_packet_list *head;
73 * The tail of the list of DNS-responses
75 static struct answer_packet_list *tail;
78 * A structure containing a mapping from network-byte-ordered DNS-id (16 bit) to
79 * some information needed to handle this query
81 * It currently allocates at least
82 * (1 + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
83 * = 1.7 MiB on 64 bit.
84 * = 1.2 MiB on 32 bit.
88 struct GNUNET_SERVER_Client* client;
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct GNUNET_OS_Process *proc;
116 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
119 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
120 "No entry 'VIRTDNS' in configuration!\n");
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
125 snprintf (port_s, 6, "%d", dnsoutport);
126 if (NULL != (proc = GNUNET_OS_start_process (NULL,
128 "gnunet-helper-hijack-dns",
130 port_s, virt_dns, NULL)))
131 GNUNET_OS_process_close (proc);
132 GNUNET_free (virt_dns);
136 * Delete the hijacking-routes
139 unhijack (unsigned short port)
143 struct GNUNET_OS_Process *proc;
146 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "No entry 'VIRTDNS' in configuration!\n");
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
155 snprintf (port_s, 6, "%d", port);
156 if (NULL != (proc = GNUNET_OS_start_process (NULL,
158 "gnunet-helper-hijack-dns",
160 "-d", port_s, virt_dns, NULL)))
161 GNUNET_OS_process_close (proc);
162 GNUNET_free (virt_dns);
166 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
170 send_answer(void* cls, size_t size, void* buf) {
171 struct answer_packet_list* query = head;
172 size_t len = ntohs(query->pkt.hdr.size);
174 GNUNET_assert(len <= size);
176 memcpy(buf, &query->pkt.hdr, len);
178 GNUNET_CONTAINER_DLL_remove (head, tail, query);
182 /* When more data is to be sent, reschedule */
184 GNUNET_SERVER_notify_transmit_ready(cls,
185 ntohs(head->pkt.hdr.size),
186 GNUNET_TIME_UNIT_FOREVER_REL,
194 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
195 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
197 unsigned short id = pdns->s.id;
199 free_parsed_dns_packet(pdns);
201 if (query_states[id].valid != GNUNET_YES) return;
202 query_states[id].valid = GNUNET_NO;
204 GNUNET_assert(query_states[id].namelen == 74);
206 size_t len = sizeof(struct answer_packet) - 1 \
207 + sizeof(struct dns_static) \
208 + 74 /* this is the length of a reverse ipv6-lookup */ \
209 + sizeof(struct dns_query_line) \
210 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
211 + sizeof(struct dns_record_line) - 1 \
212 - 2 /* We do not know the lenght of the answer yet*/;
214 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
215 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
217 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
218 answer->pkt.hdr.size = htons(len);
219 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
221 answer->pkt.from = query_states[id].remote_ip;
223 answer->pkt.to = query_states[id].local_ip;
224 answer->pkt.dst_port = query_states[id].local_port;
226 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
232 dpkt->s.qdcount = htons(1);
233 dpkt->s.ancount = htons(1);
235 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
236 GNUNET_free(query_states[id].name);
238 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
239 dque->type = htons(12); /* PTR */
240 dque->class = htons(1); /* IN */
242 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
243 memcpy(anname, "\xc0\x0c", 2);
245 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
246 drec_data->type = htons(12); /* AAAA */
247 drec_data->class = htons(1); /* IN */
248 /* FIXME: read the TTL from block:
249 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
251 * But how to get the seconds out of this?
253 drec_data->ttl = htonl(3600);
255 /* Calculate at which offset in the packet the length of the name and the
256 * name, it is filled in by the daemon-vpn */
257 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
259 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
261 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
263 GNUNET_TIME_UNIT_FOREVER_REL,
265 query_states[id].client);
269 * Receive a block from the dht.
272 receive_dht(void *cls,
273 struct GNUNET_TIME_Absolute exp,
274 const GNUNET_HashCode *key,
275 const struct GNUNET_PeerIdentity *const *get_path,
276 const struct GNUNET_PeerIdentity *const *put_path,
277 enum GNUNET_BLOCK_Type type,
281 unsigned short id = ((struct receive_dht_cls*)cls)->id;
282 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
285 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
287 /* If no query with this id is pending, ignore the block */
288 if (query_states[id].valid != GNUNET_YES) return;
289 query_states[id].valid = GNUNET_NO;
291 const struct GNUNET_DNS_Record* rec = data;
292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
293 "Got block of size %d, peer: %08x, desc: %08x\n",
295 *((unsigned int*)&rec->peer),
296 *((unsigned int*)&rec->service_descriptor));
298 size_t len = sizeof(struct answer_packet) - 1 \
299 + sizeof(struct dns_static) \
300 + query_states[id].namelen \
301 + sizeof(struct dns_query_line) \
302 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
303 + sizeof(struct dns_record_line) - 1 \
304 + 16; /* To hold the IPv6-Address */
306 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
307 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
309 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
310 answer->pkt.hdr.size = htons(len);
311 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
313 GNUNET_CRYPTO_hash(&rec->peer,
314 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
315 &answer->pkt.service_descr.peer);
317 memcpy(&answer->pkt.service_descr.service_descriptor,
318 &rec->service_descriptor,
319 sizeof(GNUNET_HashCode));
320 memcpy(&answer->pkt.service_descr.service_type,
322 sizeof(answer->pkt.service_descr.service_type));
323 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
325 answer->pkt.from = query_states[id].remote_ip;
327 answer->pkt.to = query_states[id].local_ip;
328 answer->pkt.dst_port = query_states[id].local_port;
330 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
336 dpkt->s.qdcount = htons(1);
337 dpkt->s.ancount = htons(1);
339 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
340 GNUNET_free(query_states[id].name);
342 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
343 dque->type = htons(28); /* AAAA */
344 dque->class = htons(1); /* IN */
346 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
347 memcpy(anname, "\xc0\x0c", 2);
349 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
350 drec_data->type = htons(28); /* AAAA */
351 drec_data->class = htons(1); /* IN */
353 /* FIXME: read the TTL from block:
354 * GNUNET_TIME_absolute_get_remaining(rec->expiration_time)
356 * But how to get the seconds out of this?
358 drec_data->ttl = htonl(3600);
359 drec_data->data_len = htons(16);
361 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
362 * filled in by the daemon-vpn */
363 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
365 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
367 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
369 GNUNET_TIME_UNIT_FOREVER_REL,
371 query_states[id].client);
373 GNUNET_DHT_get_stop(handle);
377 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
381 struct GNUNET_SERVER_Client *client,
382 const struct GNUNET_MessageHeader *message) {
383 unhijack(dnsoutport);
384 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
386 GNUNET_SERVER_receive_done(client, GNUNET_OK);
390 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
393 receive_query(void *cls,
394 struct GNUNET_SERVER_Client *client,
395 const struct GNUNET_MessageHeader *message) {
396 struct query_packet* pkt = (struct query_packet*)message;
397 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
398 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
400 query_states[dns->s.id].valid = GNUNET_YES;
401 query_states[dns->s.id].client = client;
402 query_states[dns->s.id].local_ip = pkt->orig_from;
403 query_states[dns->s.id].local_port = pkt->src_port;
404 query_states[dns->s.id].remote_ip = pkt->orig_to;
405 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
406 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
407 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
409 /* The query is for a .gnunet-address */
410 if (pdns->queries[0]->namelen > 9 &&
411 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
415 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
417 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
418 "Getting with key %08x, len is %d\n",
419 *((unsigned int*)&key),
420 pdns->queries[0]->namelen);
422 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
425 cls->handle = GNUNET_DHT_get_start(dht,
426 GNUNET_TIME_UNIT_MINUTES,
427 GNUNET_BLOCK_TYPE_DNS,
429 DEFAULT_GET_REPLICATION,
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
442 /* The query is for a PTR of a previosly resolved virtual IP */
443 if (htons(pdns->queries[0]->qtype) == 12 &&
444 74 == pdns->queries[0]->namelen)
448 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.";
450 unsigned long long ipv6prefix;
451 unsigned int comparelen;
453 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
454 inet_pton (AF_INET6, ipv6addr, ipv6);
455 GNUNET_free(ipv6addr);
457 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
458 GNUNET_assert(ipv6prefix < 127);
459 ipv6prefix = (ipv6prefix + 7)/8;
461 for (i = ipv6prefix; i < 16; i++)
464 for (i = 0; i < 16; i++)
466 unsigned char c1 = ipv6[i] >> 4;
467 unsigned char c2 = ipv6[i] & 0xf;
470 ipv6rev[62-(4*i)] = c1 + '0';
472 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
475 ipv6rev[62-((4*i)+2)] = c2 + '0';
477 ipv6rev[62-((4*i)+2)] = c2 + 87;
479 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
480 comparelen = 10 + 4*ipv6prefix;
481 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
482 ipv6rev + (74 - comparelen),
485 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
487 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
493 /* The query should be sent to the network */
495 struct sockaddr_in dest;
496 memset(&dest, 0, sizeof dest);
497 dest.sin_port = htons(53);
498 dest.sin_addr.s_addr = pkt->orig_to;
500 GNUNET_NETWORK_socket_sendto(dnsout,
502 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
503 (struct sockaddr*) &dest,
507 free_parsed_dns_packet(pdns);
510 GNUNET_SERVER_receive_done(client, GNUNET_OK);
513 static void read_response (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc);
519 struct sockaddr_in addr;
521 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
524 memset (&addr, 0, sizeof (struct sockaddr_in));
526 int err = GNUNET_NETWORK_socket_bind (dnsout,
527 (struct sockaddr *) &addr,
528 sizeof (struct sockaddr_in));
530 if (err != GNUNET_YES)
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 "Could not bind a port, exiting\n");
537 /* Read the port we bound to */
538 socklen_t addrlen = sizeof (struct sockaddr_in);
539 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
540 (struct sockaddr *) &addr, &addrlen);
542 dnsoutport = htons (addr.sin_port);
544 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
545 &read_response, NULL);
549 * Read a response-packet of the UDP-Socket
552 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
553 struct sockaddr_in addr;
554 socklen_t addrlen = sizeof (addr);
558 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
561 memset(&addr, 0, sizeof addr);
564 if (0 != ioctl (GNUNET_NETWORK_get_fd (dnsout),
567 unhijack(dnsoutport);
569 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
573 /* port the code above? */
577 unsigned char buf[len];
578 struct dns_pkt* dns = (struct dns_pkt*)buf;
580 r = GNUNET_NETWORK_socket_recvfrom (dnsout,
583 (struct sockaddr*)&addr,
588 unhijack(dnsoutport);
590 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
594 if (query_states[dns->s.id].valid == GNUNET_YES)
596 query_states[dns->s.id].valid = GNUNET_NO;
598 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
599 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
600 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
601 answer->pkt.hdr.size = htons(len);
602 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
603 answer->pkt.from = addr.sin_addr.s_addr;
604 answer->pkt.to = query_states[dns->s.id].local_ip;
605 answer->pkt.dst_port = query_states[dns->s.id].local_port;
606 memcpy(answer->pkt.data, buf, r);
608 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
610 GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client,
612 GNUNET_TIME_UNIT_FOREVER_REL,
614 query_states[dns->s.id].client);
617 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
625 * Task run during shutdown.
631 cleanup_task (void *cls,
632 const struct GNUNET_SCHEDULER_TaskContext *tc)
634 unhijack(dnsoutport);
635 GNUNET_DHT_disconnect(dht);
639 * @brief Create a port-map from udp and tcp redirects
641 * @param udp_redirects
642 * @param tcp_redirects
647 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
650 char* cpy, *hostname, *redirect;
651 int local_port, count = 0;
653 if (NULL != udp_redirects)
655 cpy = GNUNET_strdup (udp_redirects);
656 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
658 if (NULL == (hostname = strstr (redirect, ":")))
660 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
664 local_port = atoi (redirect);
665 if (!((local_port > 0) && (local_port < 65536)))
666 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
668 ret |= (0xFFFF & htons(local_port));
682 if (NULL != tcp_redirects)
684 cpy = GNUNET_strdup (tcp_redirects);
685 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
687 if (NULL == (hostname = strstr (redirect, ":")))
689 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
693 local_port = atoi (redirect);
694 if (!((local_port > 0) && (local_port < 65536)))
695 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
697 ret |= (0xFFFF & htons(local_port));
718 publish_name (const char *name, uint64_t ports, uint32_t service_type,
719 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
721 size_t size = sizeof (struct GNUNET_DNS_Record);
722 struct GNUNET_DNS_Record data;
723 memset (&data, 0, size);
726 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
727 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
729 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
731 *((unsigned long long *) &data.service_descriptor));
733 data.service_type = service_type;
736 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
738 data.expiration_time =
739 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
742 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
743 &data.purpose, &data.signature))
745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Putting with key %08x, size = %d\n",
751 *((unsigned int *) &data.service_descriptor), size);
754 &data.service_descriptor,
755 DEFAULT_PUT_REPLICATION,
757 GNUNET_BLOCK_TYPE_DNS,
760 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
761 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
765 * @brief Publishes the record defined by the section section
768 * @param section the current section
771 publish_iterate (void *cls, const char *section)
773 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
776 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
777 "UDP_REDIRECTS", &udp_redirects);
778 GNUNET_CONFIGURATION_get_value_string (servicecfg, section, "TCP_REDIRECTS",
781 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
787 GNUNET_free (keyfile);
791 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
792 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
793 GNUNET_free (keyfile);
794 GNUNET_assert (my_private_key != NULL);
796 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
797 uint32_t service_type = 0;
799 if (NULL != udp_redirects)
800 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
802 if (NULL != tcp_redirects)
803 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
805 service_type = htonl (service_type);
808 publish_name (section, ports, service_type, my_private_key);
810 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
813 for (alternative_name = strtok (alternative_names, " ");
814 alternative_name != NULL; alternative_name = strtok (NULL, " "))
817 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
818 strcpy (altname, alternative_name);
819 strcpy (altname + strlen (alternative_name) + 1, section);
820 altname[strlen (alternative_name)] = '.';
822 publish_name (altname, ports, service_type, my_private_key);
825 GNUNET_free_non_null(alternative_names);
826 GNUNET_CRYPTO_rsa_key_free (my_private_key);
827 GNUNET_free_non_null (udp_redirects);
828 GNUNET_free_non_null (tcp_redirects);
832 * Publish a DNS-record in the DHT. This is up to now just for testing.
835 publish_names (void *cls,
836 const struct GNUNET_SCHEDULER_TaskContext *tc) {
838 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
841 if (NULL != servicecfg)
842 GNUNET_CONFIGURATION_destroy(servicecfg);
844 GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
846 servicecfg = GNUNET_CONFIGURATION_create();
847 if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
849 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
850 GNUNET_CONFIGURATION_iterate_sections(servicecfg, publish_iterate, NULL);
852 if (NULL != services)
853 GNUNET_free(services);
855 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
862 * @param server the initialized server
863 * @param cfg_ configuration to use
867 struct GNUNET_SERVER_Handle *server,
868 const struct GNUNET_CONFIGURATION_Handle *cfg_)
870 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
871 /* callback, cls, type, size */
872 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
873 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
880 for (i = 0; i < 65536; i++) {
881 query_states[i].valid = GNUNET_NO;
884 dht = GNUNET_DHT_connect(cfg, 1024);
888 GNUNET_SCHEDULER_add_now (publish_names, NULL);
890 GNUNET_SERVER_add_handlers (server, handlers);
891 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
897 * The main function for the dns service.
899 * @param argc number of arguments from the command line
900 * @param argv command line arguments
901 * @return 0 ok, 1 on error
904 main (int argc, char *const *argv)
907 GNUNET_SERVICE_run (argc,
910 GNUNET_SERVICE_OPTION_NONE,
911 &run, NULL)) ? 0 : 1;