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 * The handle to the service-configuration
66 static struct GNUNET_CONFIGURATION_Handle *servicecfg;
69 * A list of DNS-Responses that have to be sent to the requesting client
71 static struct answer_packet_list *head;
74 * The tail of the list of DNS-responses
76 static struct answer_packet_list *tail;
79 * A structure containing a mapping from network-byte-ordered DNS-id to
80 * some information needed to handle this query
82 * It currently allocates at least
83 * (1 + machine-width + 32 + 32 + 16 + machine-width + 8) * 65536 bit
84 * = 1.7 MiB on 64 bit.
85 * = 1.2 MiB on 32 bit.
89 struct GNUNET_SERVER_Client* client;
91 unsigned remote_ip:32;
92 unsigned local_port:16;
95 } query_states[65536];
98 * A struct used to give more than one value as
99 * closure to receive_dht
101 struct receive_dht_cls {
103 struct GNUNET_DHT_GetHandle* handle;
107 * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port.
110 hijack (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 struct GNUNET_OS_Process *proc;
117 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
121 "No entry 'VIRTDNS' in configuration!\n");
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hijacking, port is %d\n", dnsoutport);
126 snprintf (port_s, 6, "%d", dnsoutport);
127 if (NULL != (proc = GNUNET_OS_start_process (NULL,
129 "gnunet-helper-hijack-dns",
131 port_s, virt_dns, NULL)))
132 GNUNET_OS_process_close (proc);
133 GNUNET_free (virt_dns);
137 * Delete the hijacking-routes
140 unhijack (unsigned short port)
144 struct GNUNET_OS_Process *proc;
147 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "VIRTDNS",
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 "No entry 'VIRTDNS' in configuration!\n");
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unHijacking, port is %d\n", port);
156 snprintf (port_s, 6, "%d", port);
157 if (NULL != (proc = GNUNET_OS_start_process (NULL,
159 "gnunet-helper-hijack-dns",
161 "-d", port_s, virt_dns, NULL)))
162 GNUNET_OS_process_close (proc);
163 GNUNET_free (virt_dns);
167 * Send the DNS-Response to the client. Gets called via the notify_transmit_ready-
171 send_answer(void* cls, size_t size, void* buf) {
172 struct answer_packet_list* query = head;
173 size_t len = ntohs(query->pkt.hdr.size);
175 GNUNET_assert(len <= size);
177 memcpy(buf, &query->pkt.hdr, len);
179 GNUNET_CONTAINER_DLL_remove (head, tail, query);
183 /* When more data is to be sent, reschedule */
185 GNUNET_SERVER_notify_transmit_ready(cls,
186 ntohs(head->pkt.hdr.size),
187 GNUNET_TIME_UNIT_FOREVER_REL,
195 send_rev_query(void * cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
196 struct dns_pkt_parsed* pdns = (struct dns_pkt_parsed*) cls;
198 unsigned short id = pdns->s.id;
200 free_parsed_dns_packet(pdns);
202 if (query_states[id].valid != GNUNET_YES) return;
203 query_states[id].valid = GNUNET_NO;
205 GNUNET_assert(query_states[id].namelen == 74);
207 size_t len = sizeof(struct answer_packet) - 1 \
208 + sizeof(struct dns_static) \
209 + 74 /* this is the length of a reverse ipv6-lookup */ \
210 + sizeof(struct dns_query_line) \
211 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
212 + sizeof(struct dns_record_line) - 1 \
213 - 2 /* We do not know the lenght of the answer yet*/;
215 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
216 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
218 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
219 answer->pkt.hdr.size = htons(len);
220 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_REV;
222 answer->pkt.from = query_states[id].remote_ip;
224 answer->pkt.to = query_states[id].local_ip;
225 answer->pkt.dst_port = query_states[id].local_port;
227 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
233 dpkt->s.qdcount = htons(1);
234 dpkt->s.ancount = htons(1);
236 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
237 GNUNET_free(query_states[id].name);
239 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
240 dque->type = htons(12); /* PTR */
241 dque->class = htons(1); /* IN */
243 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
244 memcpy(anname, (char[]){0xc0, 0x0c}, 2);
246 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
247 drec_data->type = htons(12); /* AAAA */
248 drec_data->class = htons(1); /* IN */
249 drec_data->ttl = htonl(3600); /* FIXME: read from block */
251 /* Calculate at which offset in the packet the length of the name and the
252 * name, it is filled in by the daemon-vpn */
253 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data_len)-(unsigned long)(&answer->pkt)));
255 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
257 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
259 GNUNET_TIME_UNIT_FOREVER_REL,
261 query_states[id].client);
265 * complete dns-packet with empty name in the answer
266 * provide offsett of the name
271 * Receive a block from the dht.
274 receive_dht(void *cls,
275 struct GNUNET_TIME_Absolute exp,
276 const GNUNET_HashCode *key,
277 const struct GNUNET_PeerIdentity *const *get_path,
278 const struct GNUNET_PeerIdentity *const *put_path,
279 enum GNUNET_BLOCK_Type type,
283 unsigned short id = ((struct receive_dht_cls*)cls)->id;
284 struct GNUNET_DHT_GetHandle* handle = ((struct receive_dht_cls*)cls)->handle;
287 GNUNET_assert(type == GNUNET_BLOCK_TYPE_DNS);
289 /* If no query with this id is pending, ignore the block */
290 if (query_states[id].valid != GNUNET_YES) return;
291 query_states[id].valid = GNUNET_NO;
293 const struct GNUNET_DNS_Record* rec = data;
294 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
295 "Got block of size %d, peer: %08x, desc: %08x\n",
297 *((unsigned int*)&rec->peer),
298 *((unsigned int*)&rec->service_descriptor));
300 size_t len = sizeof(struct answer_packet) - 1 \
301 + sizeof(struct dns_static) \
302 + query_states[id].namelen \
303 + sizeof(struct dns_query_line) \
304 + 2 /* To hold the pointer (as defined in RFC1035) to the name */ \
305 + sizeof(struct dns_record_line) - 1 \
306 + 16; /* To hold the IPv6-Address */
308 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
309 memset(answer, 0, len + 2*sizeof(struct answer_packet_list*));
311 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
312 answer->pkt.hdr.size = htons(len);
313 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE;
315 GNUNET_CRYPTO_hash(&rec->peer,
316 sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
317 &answer->pkt.service_descr.peer);
319 memcpy(&answer->pkt.service_descr.service_descriptor,
320 &rec->service_descriptor,
321 sizeof(GNUNET_HashCode));
322 memcpy(&answer->pkt.service_descr.service_type,
324 sizeof(answer->pkt.service_descr.service_type));
325 memcpy(&answer->pkt.service_descr.ports, &rec->ports, sizeof(answer->pkt.service_descr.ports));
327 answer->pkt.from = query_states[id].remote_ip;
329 answer->pkt.to = query_states[id].local_ip;
330 answer->pkt.dst_port = query_states[id].local_port;
332 struct dns_pkt *dpkt = (struct dns_pkt*)answer->pkt.data;
338 dpkt->s.qdcount = htons(1);
339 dpkt->s.ancount = htons(1);
341 memcpy(dpkt->data, query_states[id].name, query_states[id].namelen);
342 GNUNET_free(query_states[id].name);
344 struct dns_query_line* dque = (struct dns_query_line*)(dpkt->data+(query_states[id].namelen));
345 dque->type = htons(28); /* AAAA */
346 dque->class = htons(1); /* IN */
348 char* anname = (char*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line));
349 memcpy(anname, (char[]){0xc0, 0x0c}, 2);
351 struct dns_record_line *drec_data = (struct dns_record_line*)(dpkt->data+(query_states[id].namelen)+sizeof(struct dns_query_line)+2);
352 drec_data->type = htons(28); /* AAAA */
353 drec_data->class = htons(1); /* IN */
354 drec_data->ttl = htonl(3600); /* FIXME: read from block */
355 drec_data->data_len = htons(16);
357 /* Calculate at which offset in the packet the IPv6-Address belongs, it is
358 * filled in by the daemon-vpn */
359 answer->pkt.addroffset = htons((unsigned short)((unsigned long)(&drec_data->data)-(unsigned long)(&answer->pkt)));
361 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
363 GNUNET_SERVER_notify_transmit_ready(query_states[id].client,
365 GNUNET_TIME_UNIT_FOREVER_REL,
367 query_states[id].client);
369 GNUNET_DHT_get_stop(handle);
373 * This receives a GNUNET_MESSAGE_TYPE_REHIJACK and rehijacks the DNS
377 struct GNUNET_SERVER_Client *client,
378 const struct GNUNET_MessageHeader *message) {
379 unhijack(dnsoutport);
380 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
382 GNUNET_SERVER_receive_done(client, GNUNET_OK);
386 * This receives the dns-payload from the daemon-vpn and sends it on over the udp-socket
389 receive_query(void *cls,
390 struct GNUNET_SERVER_Client *client,
391 const struct GNUNET_MessageHeader *message) {
392 struct query_packet* pkt = (struct query_packet*)message;
393 struct dns_pkt* dns = (struct dns_pkt*)pkt->data;
394 struct dns_pkt_parsed* pdns = parse_dns_packet(dns);
396 query_states[dns->s.id].valid = GNUNET_YES;
397 query_states[dns->s.id].client = client;
398 query_states[dns->s.id].local_ip = pkt->orig_from;
399 query_states[dns->s.id].local_port = pkt->src_port;
400 query_states[dns->s.id].remote_ip = pkt->orig_to;
401 query_states[dns->s.id].namelen = strlen((char*)dns->data) + 1;
402 query_states[dns->s.id].name = GNUNET_malloc(query_states[dns->s.id].namelen);
403 memcpy(query_states[dns->s.id].name, dns->data, query_states[dns->s.id].namelen);
405 /* The query is for a .gnunet-address */
406 if (pdns->queries[0]->namelen > 9 &&
407 0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - 9), ".gnunet.", 9))
409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for .gnunet!\n");
411 GNUNET_CRYPTO_hash(pdns->queries[0]->name, pdns->queries[0]->namelen, &key);
413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
414 "Getting with key %08x, len is %d\n",
415 *((unsigned int*)&key),
416 pdns->queries[0]->namelen);
418 struct receive_dht_cls* cls = GNUNET_malloc(sizeof(struct receive_dht_cls));
421 cls->handle = GNUNET_DHT_get_start(dht,
422 GNUNET_TIME_UNIT_MINUTES,
423 GNUNET_BLOCK_TYPE_DNS,
425 DEFAULT_GET_REPLICATION,
437 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Query for '%s'; namelen=%d\n", pdns->queries[0]->name, pdns->queries[0]->namelen);
438 /* The query is for a PTR of a previosly resolved virtual IP */
439 if (htons(pdns->queries[0]->qtype) == 12 &&
440 74 == pdns->queries[0]->namelen)
444 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.";
446 unsigned long long ipv6prefix;
447 unsigned int comparelen;
449 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr));
450 inet_pton (AF_INET6, ipv6addr, ipv6);
451 GNUNET_free(ipv6addr);
453 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, "vpn", "IPV6PREFIX", &ipv6prefix));
454 GNUNET_assert(ipv6prefix < 127);
455 ipv6prefix = (ipv6prefix + 7)/8;
457 for (i = ipv6prefix; i < 16; i++)
460 for (i = 0; i < 16; i++)
462 unsigned char c1 = ipv6[i] >> 4;
463 unsigned char c2 = ipv6[i] & 0xf;
466 ipv6rev[62-(4*i)] = c1 + '0';
468 ipv6rev[62-(4*i)] = c1 + 87; /* 87 is the difference between 'a' and 10 */
471 ipv6rev[62-((4*i)+2)] = c2 + '0';
473 ipv6rev[62-((4*i)+2)] = c2 + 87;
475 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "My network is %s'.\n", ipv6rev);
476 comparelen = 10 + 4*ipv6prefix;
477 if(0 == strncmp(pdns->queries[0]->name+(pdns->queries[0]->namelen - comparelen),
478 ipv6rev + (74 - comparelen),
481 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Reverse-Query for .gnunet!\n");
483 GNUNET_SCHEDULER_add_now(send_rev_query, pdns);
489 /* The query should be sent to the network */
491 struct sockaddr_in dest;
492 memset(&dest, 0, sizeof dest);
493 dest.sin_port = htons(53);
494 dest.sin_addr.s_addr = pkt->orig_to;
496 GNUNET_NETWORK_socket_sendto(dnsout,
498 ntohs(pkt->hdr.size) - sizeof(struct query_packet) + 1,
499 (struct sockaddr*) &dest,
503 free_parsed_dns_packet(pdns);
506 GNUNET_SERVER_receive_done(client, GNUNET_OK);
509 static void read_response (void *cls,
510 const struct GNUNET_SCHEDULER_TaskContext *tc);
515 struct sockaddr_in addr;
517 dnsout = GNUNET_NETWORK_socket_create (AF_INET, SOCK_DGRAM, 0);
520 memset (&addr, 0, sizeof (struct sockaddr_in));
522 int err = GNUNET_NETWORK_socket_bind (dnsout,
523 (struct sockaddr *) &addr,
524 sizeof (struct sockaddr_in));
526 if (err != GNUNET_YES)
528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
529 "Could not bind a port, exiting\n");
533 /* Read the port we bound to */
534 socklen_t addrlen = sizeof (struct sockaddr_in);
535 err = getsockname (GNUNET_NETWORK_get_fd (dnsout),
536 (struct sockaddr *) &addr, &addrlen);
538 dnsoutport = htons (addr.sin_port);
540 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, dnsout,
541 &read_response, NULL);
545 * Read a response-packet of the UDP-Socket
548 read_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
549 unsigned char buf[65536];
550 struct dns_pkt* dns = (struct dns_pkt*)buf;
552 if (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
555 struct sockaddr_in addr;
556 memset(&addr, 0, sizeof addr);
557 socklen_t addrlen = sizeof addr;
560 r = GNUNET_NETWORK_socket_recvfrom(dnsout,
563 (struct sockaddr*)&addr,
568 unhijack(dnsoutport);
570 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, hijack, NULL);
574 if (query_states[dns->s.id].valid == GNUNET_YES) {
575 query_states[dns->s.id].valid = GNUNET_NO;
577 size_t len = sizeof(struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */
578 struct answer_packet_list* answer = GNUNET_malloc(len + 2*sizeof(struct answer_packet_list*));
579 answer->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_RESPONSE_DNS);
580 answer->pkt.hdr.size = htons(len);
581 answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP;
582 answer->pkt.from = addr.sin_addr.s_addr;
583 answer->pkt.to = query_states[dns->s.id].local_ip;
584 answer->pkt.dst_port = query_states[dns->s.id].local_port;
585 memcpy(answer->pkt.data, buf, r);
587 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, answer);
589 GNUNET_SERVER_notify_transmit_ready(query_states[dns->s.id].client,
591 GNUNET_TIME_UNIT_FOREVER_REL,
593 query_states[dns->s.id].client);
596 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
604 * Task run during shutdown.
610 cleanup_task (void *cls,
611 const struct GNUNET_SCHEDULER_TaskContext *tc)
613 unhijack(dnsoutport);
614 GNUNET_DHT_disconnect(dht);
618 * @brief Create a port-map from udp and tcp redirects
620 * @param udp_redirects
621 * @param tcp_redirects
626 get_port_from_redirects (const char *udp_redirects, const char *tcp_redirects)
629 char* cpy, *hostname, *redirect;
630 int local_port, count = 0;
632 if (NULL != udp_redirects)
634 cpy = GNUNET_strdup (udp_redirects);
635 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
637 if (NULL == (hostname = strstr (redirect, ":")))
639 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
643 local_port = atoi (redirect);
644 if (!((local_port > 0) && (local_port < 65536)))
645 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
647 ret |= (0xFFFF & htons(local_port));
661 if (NULL != tcp_redirects)
663 cpy = GNUNET_strdup (tcp_redirects);
664 for (redirect = strtok (cpy, " "); redirect != NULL; redirect = strtok (NULL, " "))
666 if (NULL == (hostname = strstr (redirect, ":")))
668 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: option %s is not formatted correctly!\n", redirect);
672 local_port = atoi (redirect);
673 if (!((local_port > 0) && (local_port < 65536)))
674 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Warning: %s is not a correct port.", redirect);
676 ret |= (0xFFFF & htons(local_port));
697 publish_name (const char *name, uint64_t ports, uint32_t service_type,
698 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key)
700 size_t size = sizeof (struct GNUNET_DNS_Record);
701 struct GNUNET_DNS_Record data;
702 memset (&data, 0, size);
705 htonl (size - sizeof (struct GNUNET_CRYPTO_RsaSignature));
706 data.purpose.purpose = GNUNET_SIGNATURE_PURPOSE_DNS_RECORD;
708 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &data.service_descriptor);
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store with key1 %x\n",
710 *((unsigned long long *) &data.service_descriptor));
712 data.service_type = service_type;
715 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &data.peer);
717 data.expiration_time =
718 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 2));
721 if (GNUNET_OK != GNUNET_CRYPTO_rsa_sign (my_private_key,
722 &data.purpose, &data.signature))
724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not sign DNS_Record\n");
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Putting with key %08x, size = %d\n",
730 *((unsigned int *) &data.service_descriptor), size);
733 &data.service_descriptor,
734 DEFAULT_PUT_REPLICATION,
736 GNUNET_BLOCK_TYPE_DNS,
739 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS),
740 GNUNET_TIME_UNIT_MINUTES, NULL, NULL);
744 * @brief Publishes the record defined by the section section
747 * @param section the current section
750 publish_iterate (void *cls, const char *section)
752 char *udp_redirects, *tcp_redirects, *alternative_names, *alternative_name,
755 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
756 "UDP_REDIRECTS", &udp_redirects);
757 GNUNET_CONFIGURATION_get_value_string (servicecfg, section, "TCP_REDIRECTS",
760 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD",
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not read keyfile-value\n");
766 GNUNET_free (keyfile);
770 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key =
771 GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
772 GNUNET_free (keyfile);
773 GNUNET_assert (my_private_key != NULL);
775 uint64_t ports = get_port_from_redirects (udp_redirects, tcp_redirects);
776 uint32_t service_type = 0;
778 if (NULL != udp_redirects)
779 service_type = GNUNET_DNS_SERVICE_TYPE_UDP;
781 if (NULL != tcp_redirects)
782 service_type |= GNUNET_DNS_SERVICE_TYPE_TCP;
784 service_type = htonl (service_type);
787 publish_name (section, ports, service_type, my_private_key);
789 GNUNET_CONFIGURATION_get_value_string (servicecfg, section,
792 for (alternative_name = strtok (alternative_names, " ");
793 alternative_name != NULL; alternative_name = strtok (NULL, " "))
796 alloca (strlen (alternative_name) + strlen (section) + 1 + 1);
797 strcpy (altname, alternative_name);
798 strcpy (altname + strlen (alternative_name) + 1, section);
799 altname[strlen (alternative_name)] = '.';
801 publish_name (altname, ports, service_type, my_private_key);
804 GNUNET_free_non_null(alternative_names);
805 GNUNET_CRYPTO_rsa_key_free (my_private_key);
806 GNUNET_free_non_null (udp_redirects);
807 GNUNET_free_non_null (tcp_redirects);
811 * Publish a DNS-record in the DHT. This is up to now just for testing.
814 publish_names (void *cls,
815 const struct GNUNET_SCHEDULER_TaskContext *tc) {
817 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
820 if (NULL != servicecfg)
821 GNUNET_CONFIGURATION_destroy(servicecfg);
823 GNUNET_CONFIGURATION_get_value_filename(cfg, "dns", "SERVICES", &services);
825 servicecfg = GNUNET_CONFIGURATION_create();
826 if (GNUNET_OK == GNUNET_CONFIGURATION_parse(servicecfg, services))
828 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Parsing services %s\n", services);
829 GNUNET_CONFIGURATION_iterate_sections(servicecfg, publish_iterate, NULL);
831 if (NULL != services)
832 GNUNET_free(services);
834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_HOURS,
841 * @param server the initialized server
842 * @param cfg_ configuration to use
846 struct GNUNET_SERVER_Handle *server,
847 const struct GNUNET_CONFIGURATION_Handle *cfg_)
849 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
850 /* callback, cls, type, size */
851 {&receive_query, NULL, GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS, 0},
852 {&rehijack, NULL, GNUNET_MESSAGE_TYPE_REHIJACK, sizeof(struct GNUNET_MessageHeader)},
859 for (i = 0; i < 65536; i++) {
860 query_states[i].valid = GNUNET_NO;
863 dht = GNUNET_DHT_connect(cfg, 1024);
867 GNUNET_SCHEDULER_add_now (publish_names, NULL);
869 GNUNET_SERVER_add_handlers (server, handlers);
870 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
876 * The main function for the dns service.
878 * @param argc number of arguments from the command line
879 * @param argv command line arguments
880 * @return 0 ok, 1 on error
883 main (int argc, char *const *argv)
886 GNUNET_SERVICE_run (argc,
889 GNUNET_SERVICE_OPTION_NONE,
890 &run, NULL)) ? 0 : 1;