2 This file is part of GNUnet.
3 Copyright (C) 2012-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file gnunet-dns2gns.c
22 * @brief DNS server that translates DNS requests to GNS
23 * @author Christian Grothoff
26 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_gns_service.h>
29 #include <gnunet_dnsstub_lib.h>
33 * Timeout for DNS requests.
35 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
38 * Data kept per request.
43 * Socket to use for sending the reply.
45 struct GNUNET_NETWORK_Handle *lsock;
48 * Destination address to use.
53 * Initially, this is the DNS request, it will then be
54 * converted to the DNS response.
56 struct GNUNET_DNSPARSER_Packet *packet;
59 * Our GNS request handle.
61 struct GNUNET_GNS_LookupWithTldRequest *lookup;
64 * Our DNS request handle
66 struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
69 * Task run on timeout or shutdown to clean up without
72 struct GNUNET_SCHEDULER_Task *timeout_task;
75 * Original UDP request message.
80 * Number of bytes in @e addr.
85 * Number of bytes in @e udp_msg.
90 * ID of the original request.
92 uint16_t original_request_id;
96 * The address to bind to
98 static in_addr_t address;
101 * The IPv6 address to bind to
103 static struct in6_addr address6;
107 * Handle to GNS resolver.
109 struct GNUNET_GNS_Handle *gns;
114 struct GNUNET_DNSSTUB_Context *dns_stub;
117 * Listen socket for IPv4.
119 static struct GNUNET_NETWORK_Handle *listen_socket4;
122 * Listen socket for IPv6.
124 static struct GNUNET_NETWORK_Handle *listen_socket6;
127 * Task for IPv4 socket.
129 static struct GNUNET_SCHEDULER_Task *t4;
132 * Task for IPv6 socket.
134 static struct GNUNET_SCHEDULER_Task *t6;
142 * UDP Port we listen on for inbound DNS requests.
144 static unsigned int listen_port = 53;
147 * Configuration to use.
149 static const struct GNUNET_CONFIGURATION_Handle *cfg;
153 * Task run on shutdown. Cleans up everything.
158 do_shutdown (void *cls)
163 GNUNET_SCHEDULER_cancel (t4);
168 GNUNET_SCHEDULER_cancel (t6);
171 if (NULL != listen_socket4)
173 GNUNET_NETWORK_socket_close (listen_socket4);
174 listen_socket4 = NULL;
176 if (NULL != listen_socket6)
178 GNUNET_NETWORK_socket_close (listen_socket6);
179 listen_socket6 = NULL;
183 GNUNET_GNS_disconnect (gns);
186 if (NULL != dns_stub)
188 GNUNET_DNSSTUB_stop (dns_stub);
195 * Fisher-Yates (aka Knuth) Shuffle
197 * @param request context for the request (with answers)
200 shuffle_answers (struct Request *request)
202 unsigned int idx = request->packet->num_answers;
204 struct GNUNET_DNSPARSER_Record tmp_answer;
208 r_idx = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
209 request->packet->num_answers);
211 tmp_answer = request->packet->answers[idx];
212 memcpy (&request->packet->answers[idx], &request->packet->answers[r_idx],
213 sizeof (struct GNUNET_DNSPARSER_Record));
214 memcpy (&request->packet->answers[r_idx], &tmp_answer,
215 sizeof (struct GNUNET_DNSPARSER_Record));
220 * Send the response for the given request and clean up.
222 * @param request context for the request.
225 send_response (struct Request *request)
231 shuffle_answers (request);
233 GNUNET_DNSPARSER_pack (request->packet,
234 UINT16_MAX /* is this not too much? */,
238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
239 _ ("Failed to pack DNS response into UDP packet!\n"));
243 sret = GNUNET_NETWORK_socket_sendto (request->lsock,
249 (size != (size_t) sret))
250 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
254 GNUNET_SCHEDULER_cancel (request->timeout_task);
255 GNUNET_DNSPARSER_free_packet (request->packet);
256 GNUNET_free (request->udp_msg);
257 GNUNET_free (request);
262 * Task run on timeout. Cleans up request.
264 * @param cls `struct Request *` of the request to clean up
267 do_timeout (void *cls)
269 struct Request *request = cls;
271 if (NULL != request->packet)
272 GNUNET_DNSPARSER_free_packet (request->packet);
273 if (NULL != request->lookup)
274 GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
275 if (NULL != request->dns_lookup)
276 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
277 GNUNET_free (request->udp_msg);
278 GNUNET_free (request);
283 * Iterator called on obtained result for a DNS lookup
286 * @param dns the DNS udp payload
287 * @param r size of the DNS payload
290 dns_result_processor (void *cls,
291 const struct GNUNET_TUN_DnsHeader *dns,
294 struct Request *request = cls;
298 /* DNSSTUB gave up, so we trigger timeout early */
299 GNUNET_SCHEDULER_cancel (request->timeout_task);
300 do_timeout (request);
303 if (request->original_request_id != dns->id)
305 /* for a another query, ignore */
308 request->packet = GNUNET_DNSPARSER_parse ((char *) dns,
310 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
311 send_response (request);
316 * Iterator called on obtained result for a GNS lookup.
319 * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
320 * @param rd_count number of records in @a rd
321 * @param rd the records in reply
324 result_processor (void *cls,
327 const struct GNUNET_GNSRECORD_Data *rd)
329 struct Request *request = cls;
330 struct GNUNET_DNSPARSER_Packet *packet;
331 struct GNUNET_DNSPARSER_Record rec;
333 request->lookup = NULL;
334 if (GNUNET_NO == was_gns)
336 /* TLD not configured for GNS, fall back to DNS */
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Using DNS resolver IP `%s' to resolve `%s'\n",
340 request->packet->queries[0].name);
341 request->original_request_id = request->packet->id;
342 GNUNET_DNSPARSER_free_packet (request->packet);
343 request->packet = NULL;
344 request->dns_lookup = GNUNET_DNSSTUB_resolve (dns_stub,
346 request->udp_msg_size,
347 &dns_result_processor,
351 packet = request->packet;
352 packet->flags.query_or_response = 1;
353 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
354 packet->flags.checking_disabled = 0;
355 packet->flags.authenticated_data = 1;
356 packet->flags.zero = 0;
357 packet->flags.recursion_available = 1;
358 packet->flags.message_truncated = 0;
359 packet->flags.authoritative_answer = 0;
360 // packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
361 for (uint32_t i = 0; i < rd_count; i++)
363 rec.expiration_time.abs_value_us = rd[i].expiration_time;
364 switch (rd[i].record_type)
366 case GNUNET_DNSPARSER_TYPE_A:
367 GNUNET_assert (sizeof(struct in_addr) == rd[i].data_size);
368 rec.name = GNUNET_strdup (packet->queries[0].name);
369 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
370 rec.type = GNUNET_DNSPARSER_TYPE_A;
371 rec.data.raw.data = GNUNET_new (struct in_addr);
372 GNUNET_memcpy (rec.data.raw.data,
375 rec.data.raw.data_len = sizeof(struct in_addr);
376 GNUNET_array_append (packet->answers,
381 case GNUNET_DNSPARSER_TYPE_AAAA:
382 GNUNET_assert (sizeof(struct in6_addr) == rd[i].data_size);
383 rec.name = GNUNET_strdup (packet->queries[0].name);
384 rec.data.raw.data = GNUNET_new (struct in6_addr);
385 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
386 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
387 GNUNET_memcpy (rec.data.raw.data,
390 rec.data.raw.data_len = sizeof(struct in6_addr);
391 GNUNET_array_append (packet->answers,
396 case GNUNET_DNSPARSER_TYPE_CNAME:
397 rec.name = GNUNET_strdup (packet->queries[0].name);
398 rec.data.hostname = GNUNET_strdup (rd[i].data);
399 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
400 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
401 GNUNET_memcpy (rec.data.hostname,
404 GNUNET_array_append (packet->answers,
414 send_response (request);
419 * Handle DNS request.
421 * @param lsock socket to use for sending the reply
422 * @param addr address to use for sending the reply
423 * @param addr_len number of bytes in @a addr
424 * @param udp_msg DNS request payload
425 * @param udp_msg_size number of bytes in @a udp_msg
428 handle_request (struct GNUNET_NETWORK_Handle *lsock,
434 struct Request *request;
435 struct GNUNET_DNSPARSER_Packet *packet;
437 packet = GNUNET_DNSPARSER_parse (udp_msg,
441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
442 _ ("Cannot parse DNS request from %s\n"),
443 GNUNET_a2s (addr, addr_len));
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
448 packet->queries[0].name,
449 (unsigned int) packet->flags.query_or_response,
450 (int) packet->num_answers,
451 (int) packet->num_authority_records,
452 (int) packet->num_additional_records);
453 if ((0 != packet->flags.query_or_response) ||
454 (0 != packet->num_answers) ||
455 (0 != packet->num_authority_records))
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
458 _ ("Received malformed DNS request from %s\n"),
459 GNUNET_a2s (addr, addr_len));
460 GNUNET_DNSPARSER_free_packet (packet);
463 if ((1 != packet->num_queries))
465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
466 _ ("Received unsupported DNS request from %s\n"),
469 GNUNET_DNSPARSER_free_packet (packet);
472 request = GNUNET_malloc (sizeof(struct Request) + addr_len);
473 request->lsock = lsock;
474 request->packet = packet;
475 request->addr = &request[1];
476 request->addr_len = addr_len;
477 GNUNET_memcpy (&request[1],
480 request->udp_msg_size = udp_msg_size;
481 request->udp_msg = GNUNET_memdup (udp_msg,
483 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Calling GNS on `%s'\n",
488 packet->queries[0].name);
489 request->lookup = GNUNET_GNS_lookup_with_tld (gns,
490 packet->queries[0].name,
491 packet->queries[0].type,
492 GNUNET_GNS_LO_DEFAULT,
499 * Task to read IPv4 DNS packets.
501 * @param cls the 'listen_socket4'
504 read_dns4 (void *cls)
506 struct sockaddr_in v4;
509 const struct GNUNET_SCHEDULER_TaskContext *tc;
511 GNUNET_assert (listen_socket4 == cls);
512 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
516 tc = GNUNET_SCHEDULER_get_task_context ();
517 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
518 return; /* shutdown? */
519 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
523 return; /* read error!? */
529 addrlen = sizeof(v4);
530 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
533 (struct sockaddr *) &v4,
537 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
541 GNUNET_break (size == sret);
542 handle_request (listen_socket4,
552 * Task to read IPv6 DNS packets.
554 * @param cls the 'listen_socket6'
557 read_dns6 (void *cls)
559 struct sockaddr_in6 v6;
562 const struct GNUNET_SCHEDULER_TaskContext *tc;
564 GNUNET_assert (listen_socket6 == cls);
565 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
569 tc = GNUNET_SCHEDULER_get_task_context ();
570 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
571 return; /* shutdown? */
572 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
576 return; /* read error!? */
582 addrlen = sizeof(v6);
583 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
586 (struct sockaddr *) &v6,
590 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
594 GNUNET_break (size == sret);
595 handle_request (listen_socket6,
605 * Main function that will be run.
608 * @param args remaining command-line arguments
609 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
610 * @param c configuration
616 const struct GNUNET_CONFIGURATION_Handle *c)
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 _ ("No DNS server specified!\n"));
630 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
632 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
634 GNUNET_assert (NULL != (dns_stub = GNUNET_DNSSTUB_start (128)));
636 GNUNET_DNSSTUB_add_dns_ip (dns_stub,
639 GNUNET_DNSSTUB_stop (dns_stub);
640 GNUNET_GNS_disconnect (gns);
645 /* Get address to bind to */
646 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "dns2gns",
650 // No address specified
651 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
652 "Don't know what to bind to...\n");
653 GNUNET_free (addr_str);
654 GNUNET_SCHEDULER_shutdown ();
657 if (1 != inet_pton (AF_INET, addr_str, &address))
659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
660 "Unable to parse address %s\n",
662 GNUNET_free (addr_str);
663 GNUNET_SCHEDULER_shutdown ();
666 GNUNET_free (addr_str);
667 /* Get address to bind to */
668 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "dns2gns",
672 // No address specified
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 "Don't know what to bind6 to...\n");
675 GNUNET_free (addr_str);
676 GNUNET_SCHEDULER_shutdown ();
679 if (1 != inet_pton (AF_INET6, addr_str, &address6))
681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
682 "Unable to parse IPv6 address %s\n",
684 GNUNET_free (addr_str);
685 GNUNET_SCHEDULER_shutdown ();
688 GNUNET_free (addr_str);
690 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
693 if (NULL != listen_socket4)
695 struct sockaddr_in v4;
697 memset (&v4, 0, sizeof(v4));
698 v4.sin_family = AF_INET;
699 v4.sin_addr.s_addr = address;
700 #if HAVE_SOCKADDR_IN_SIN_LEN
701 v4.sin_len = sizeof(v4);
703 v4.sin_port = htons (listen_port);
705 GNUNET_NETWORK_socket_bind (listen_socket4,
706 (struct sockaddr *) &v4,
709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
710 GNUNET_NETWORK_socket_close (listen_socket4);
711 listen_socket4 = NULL;
714 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
717 if (NULL != listen_socket6)
719 struct sockaddr_in6 v6;
721 memset (&v6, 0, sizeof(v6));
722 v6.sin6_family = AF_INET6;
723 v6.sin6_addr = address6;
724 #if HAVE_SOCKADDR_IN_SIN_LEN
725 v6.sin6_len = sizeof(v6);
727 v6.sin6_port = htons (listen_port);
729 GNUNET_NETWORK_socket_bind (listen_socket6,
730 (struct sockaddr *) &v6,
733 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
734 GNUNET_NETWORK_socket_close (listen_socket6);
735 listen_socket6 = NULL;
738 if ((NULL == listen_socket4) &&
739 (NULL == listen_socket6))
741 GNUNET_GNS_disconnect (gns);
743 GNUNET_DNSSTUB_stop (dns_stub);
747 if (NULL != listen_socket4)
748 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
752 if (NULL != listen_socket6)
753 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
761 * The main function for the dns2gns daemon.
763 * @param argc number of arguments from the command line
764 * @param argv command line arguments
765 * @return 0 ok, 1 on error
771 struct GNUNET_GETOPT_CommandLineOption options[] = {
772 GNUNET_GETOPT_option_string ('d',
776 "IP of recursive DNS resolver to use (required)"),
778 GNUNET_GETOPT_option_uint ('p',
782 "UDP port to listen on for inbound DNS requests; default: 2853"),
784 GNUNET_GETOPT_OPTION_END
789 GNUNET_STRINGS_get_utf8_args (argc, argv,
792 GNUNET_log_setup ("gnunet-dns2gns",
797 GNUNET_PROGRAM_run (argc, argv,
799 _ ("GNUnet DNS-to-GNS proxy (a DNS server)"),
801 &run, NULL)) ? 0 : 1;
802 GNUNET_free_nz ((void *) argv);
807 /* end of gnunet-dns2gns.c */