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;
108 * Handle to GNS resolver.
110 struct GNUNET_GNS_Handle *gns;
115 struct GNUNET_DNSSTUB_Context *dns_stub;
118 * Listen socket for IPv4.
120 static struct GNUNET_NETWORK_Handle *listen_socket4;
123 * Listen socket for IPv6.
125 static struct GNUNET_NETWORK_Handle *listen_socket6;
128 * Task for IPv4 socket.
130 static struct GNUNET_SCHEDULER_Task *t4;
133 * Task for IPv6 socket.
135 static struct GNUNET_SCHEDULER_Task *t6;
143 * UDP Port we listen on for inbound DNS requests.
145 static unsigned int listen_port = 53;
148 * Configuration to use.
150 static const struct GNUNET_CONFIGURATION_Handle *cfg;
154 * Task run on shutdown. Cleans up everything.
159 do_shutdown (void *cls)
164 GNUNET_SCHEDULER_cancel (t4);
169 GNUNET_SCHEDULER_cancel (t6);
172 if (NULL != listen_socket4)
174 GNUNET_NETWORK_socket_close (listen_socket4);
175 listen_socket4 = NULL;
177 if (NULL != listen_socket6)
179 GNUNET_NETWORK_socket_close (listen_socket6);
180 listen_socket6 = NULL;
184 GNUNET_GNS_disconnect (gns);
187 if (NULL != dns_stub)
189 GNUNET_DNSSTUB_stop (dns_stub);
196 * Send the response for the given request and clean up.
198 * @param request context for the request.
201 send_response (struct Request *request)
208 GNUNET_DNSPARSER_pack (request->packet,
209 UINT16_MAX /* is this not too much? */,
213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214 _("Failed to pack DNS response into UDP packet!\n"));
218 sret = GNUNET_NETWORK_socket_sendto (request->lsock,
224 (size != (size_t) sret) )
225 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
229 GNUNET_SCHEDULER_cancel (request->timeout_task);
230 GNUNET_DNSPARSER_free_packet (request->packet);
231 GNUNET_free (request->udp_msg);
232 GNUNET_free (request);
237 * Task run on timeout. Cleans up request.
239 * @param cls `struct Request *` of the request to clean up
242 do_timeout (void *cls)
244 struct Request *request = cls;
246 if (NULL != request->packet)
247 GNUNET_DNSPARSER_free_packet (request->packet);
248 if (NULL != request->lookup)
249 GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
250 if (NULL != request->dns_lookup)
251 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
252 GNUNET_free (request->udp_msg);
253 GNUNET_free (request);
258 * Iterator called on obtained result for a DNS lookup
261 * @param dns the DNS udp payload
262 * @param r size of the DNS payload
265 dns_result_processor (void *cls,
266 const struct GNUNET_TUN_DnsHeader *dns,
269 struct Request *request = cls;
273 /* DNSSTUB gave up, so we trigger timeout early */
274 GNUNET_SCHEDULER_cancel (request->timeout_task);
275 do_timeout (request);
278 if (request->original_request_id != dns->id)
280 /* for a another query, ignore */
283 request->packet = GNUNET_DNSPARSER_parse ((char*)dns,
285 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
286 send_response (request);
291 * Iterator called on obtained result for a GNS lookup.
294 * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
295 * @param rd_count number of records in @a rd
296 * @param rd the records in reply
299 result_processor (void *cls,
302 const struct GNUNET_GNSRECORD_Data *rd)
304 struct Request *request = cls;
305 struct GNUNET_DNSPARSER_Packet *packet;
306 struct GNUNET_DNSPARSER_Record rec;
308 request->lookup = NULL;
309 if (GNUNET_NO == was_gns)
311 /* TLD not configured for GNS, fall back to DNS */
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Using DNS resolver IP `%s' to resolve `%s'\n",
315 request->packet->queries[0].name);
316 request->original_request_id = request->packet->id;
317 GNUNET_DNSPARSER_free_packet (request->packet);
318 request->packet = NULL;
319 request->dns_lookup = GNUNET_DNSSTUB_resolve (dns_stub,
321 request->udp_msg_size,
322 &dns_result_processor,
326 packet = request->packet;
327 packet->flags.query_or_response = 1;
328 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
329 packet->flags.checking_disabled = 0;
330 packet->flags.authenticated_data = 1;
331 packet->flags.zero = 0;
332 packet->flags.recursion_available = 1;
333 packet->flags.message_truncated = 0;
334 packet->flags.authoritative_answer = 0;
335 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
336 for (uint32_t i=0;i<rd_count;i++)
338 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
339 // here? Or should we do this in libgnunetgns?
340 rec.expiration_time.abs_value_us = rd[i].expiration_time;
341 switch (rd[i].record_type)
343 case GNUNET_DNSPARSER_TYPE_A:
344 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
345 rec.name = GNUNET_strdup (packet->queries[0].name);
346 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
347 rec.type = GNUNET_DNSPARSER_TYPE_A;
348 rec.data.raw.data = GNUNET_new (struct in_addr);
349 GNUNET_memcpy (rec.data.raw.data,
352 rec.data.raw.data_len = sizeof (struct in_addr);
353 GNUNET_array_append (packet->answers,
357 case GNUNET_DNSPARSER_TYPE_AAAA:
358 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
359 rec.name = GNUNET_strdup (packet->queries[0].name);
360 rec.data.raw.data = GNUNET_new (struct in6_addr);
361 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
362 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
363 GNUNET_memcpy (rec.data.raw.data,
366 rec.data.raw.data_len = sizeof (struct in6_addr);
367 GNUNET_array_append (packet->answers,
371 case GNUNET_DNSPARSER_TYPE_CNAME:
372 rec.name = GNUNET_strdup (packet->queries[0].name);
373 rec.data.hostname = GNUNET_strdup (rd[i].data);
374 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
375 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
376 GNUNET_memcpy (rec.data.hostname,
379 GNUNET_array_append (packet->answers,
388 send_response (request);
393 * Handle DNS request.
395 * @param lsock socket to use for sending the reply
396 * @param addr address to use for sending the reply
397 * @param addr_len number of bytes in @a addr
398 * @param udp_msg DNS request payload
399 * @param udp_msg_size number of bytes in @a udp_msg
402 handle_request (struct GNUNET_NETWORK_Handle *lsock,
408 struct Request *request;
409 struct GNUNET_DNSPARSER_Packet *packet;
411 packet = GNUNET_DNSPARSER_parse (udp_msg,
415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
416 _("Cannot parse DNS request from %s\n"),
417 GNUNET_a2s (addr, addr_len));
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
422 packet->queries[0].name,
423 (unsigned int) packet->flags.query_or_response,
424 (int) packet->num_answers,
425 (int) packet->num_authority_records,
426 (int) packet->num_additional_records);
427 if ( (0 != packet->flags.query_or_response) ||
428 (0 != packet->num_answers) ||
429 (0 != packet->num_authority_records))
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
432 _("Received malformed DNS request from %s\n"),
433 GNUNET_a2s (addr, addr_len));
434 GNUNET_DNSPARSER_free_packet (packet);
437 if ( (1 != packet->num_queries) )
439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
440 _("Received unsupported DNS request from %s\n"),
443 GNUNET_DNSPARSER_free_packet (packet);
446 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
447 request->lsock = lsock;
448 request->packet = packet;
449 request->addr = &request[1];
450 request->addr_len = addr_len;
451 GNUNET_memcpy (&request[1],
454 request->udp_msg_size = udp_msg_size;
455 request->udp_msg = GNUNET_memdup (udp_msg,
457 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Calling GNS on `%s'\n",
462 packet->queries[0].name);
463 request->lookup = GNUNET_GNS_lookup_with_tld (gns,
464 packet->queries[0].name,
465 packet->queries[0].type,
473 * Task to read IPv4 DNS packets.
475 * @param cls the 'listen_socket4'
478 read_dns4 (void *cls)
480 struct sockaddr_in v4;
483 const struct GNUNET_SCHEDULER_TaskContext *tc;
485 GNUNET_assert (listen_socket4 == cls);
486 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
490 tc = GNUNET_SCHEDULER_get_task_context ();
491 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
492 return; /* shutdown? */
493 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
497 return; /* read error!? */
503 addrlen = sizeof (v4);
504 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
507 (struct sockaddr *) &v4,
511 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
515 GNUNET_break (size == sret);
516 handle_request (listen_socket4,
526 * Task to read IPv6 DNS packets.
528 * @param cls the 'listen_socket6'
531 read_dns6 (void *cls)
533 struct sockaddr_in6 v6;
536 const struct GNUNET_SCHEDULER_TaskContext *tc;
538 GNUNET_assert (listen_socket6 == cls);
539 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
543 tc = GNUNET_SCHEDULER_get_task_context ();
544 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
545 return; /* shutdown? */
546 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
550 return; /* read error!? */
556 addrlen = sizeof (v6);
557 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
560 (struct sockaddr *) &v6,
564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
568 GNUNET_break (size == sret);
569 handle_request (listen_socket6,
579 * Main function that will be run.
582 * @param args remaining command-line arguments
583 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
584 * @param c configuration
590 const struct GNUNET_CONFIGURATION_Handle *c)
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 _("No DNS server specified!\n"));
603 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
605 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
607 GNUNET_assert (NULL != (dns_stub = GNUNET_DNSSTUB_start (128)));
609 GNUNET_DNSSTUB_add_dns_ip (dns_stub,
612 GNUNET_DNSSTUB_stop (dns_stub);
613 GNUNET_GNS_disconnect (gns);
618 /* Get address to bind to */
619 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "dns2gns",
623 //No address specified
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
625 "Don't know what to bind to...\n");
626 GNUNET_free (addr_str);
627 GNUNET_SCHEDULER_shutdown ();
630 if (1 != inet_pton (AF_INET, addr_str, &address))
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
633 "Unable to parse address %s\n",
635 GNUNET_free (addr_str);
636 GNUNET_SCHEDULER_shutdown ();
639 GNUNET_free (addr_str);
640 /* Get address to bind to */
641 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c, "dns2gns",
645 //No address specified
646 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
647 "Don't know what to bind6 to...\n");
648 GNUNET_free (addr_str);
649 GNUNET_SCHEDULER_shutdown ();
652 if (1 != inet_pton (AF_INET6, addr_str, &address6))
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 "Unable to parse IPv6 address %s\n",
657 GNUNET_free (addr_str);
658 GNUNET_SCHEDULER_shutdown ();
661 GNUNET_free (addr_str);
663 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
666 if (NULL != listen_socket4)
668 struct sockaddr_in v4;
670 memset (&v4, 0, sizeof (v4));
671 v4.sin_family = AF_INET;
672 v4.sin_addr.s_addr = address;
673 #if HAVE_SOCKADDR_IN_SIN_LEN
674 v4.sin_len = sizeof (v4);
676 v4.sin_port = htons (listen_port);
678 GNUNET_NETWORK_socket_bind (listen_socket4,
679 (struct sockaddr *) &v4,
682 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
683 GNUNET_NETWORK_socket_close (listen_socket4);
684 listen_socket4 = NULL;
687 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
690 if (NULL != listen_socket6)
692 struct sockaddr_in6 v6;
694 memset (&v6, 0, sizeof (v6));
695 v6.sin6_family = AF_INET6;
696 v6.sin6_addr = address6;
697 #if HAVE_SOCKADDR_IN_SIN_LEN
698 v6.sin6_len = sizeof (v6);
700 v6.sin6_port = htons (listen_port);
702 GNUNET_NETWORK_socket_bind (listen_socket6,
703 (struct sockaddr *) &v6,
706 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
707 GNUNET_NETWORK_socket_close (listen_socket6);
708 listen_socket6 = NULL;
711 if ( (NULL == listen_socket4) &&
712 (NULL == listen_socket6) )
714 GNUNET_GNS_disconnect (gns);
716 GNUNET_DNSSTUB_stop (dns_stub);
720 if (NULL != listen_socket4)
721 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
725 if (NULL != listen_socket6)
726 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
734 * The main function for the dns2gns daemon.
736 * @param argc number of arguments from the command line
737 * @param argv command line arguments
738 * @return 0 ok, 1 on error
744 struct GNUNET_GETOPT_CommandLineOption options[] = {
745 GNUNET_GETOPT_option_string ('d',
748 gettext_noop ("IP of recursive DNS resolver to use (required)"),
750 GNUNET_GETOPT_option_uint ('p',
753 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"),
755 GNUNET_GETOPT_OPTION_END
760 GNUNET_STRINGS_get_utf8_args (argc, argv,
763 GNUNET_log_setup ("gnunet-dns2gns",
768 GNUNET_PROGRAM_run (argc, argv,
770 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
772 &run, NULL)) ? 0 : 1;
773 GNUNET_free ((void*) argv);
777 /* end of gnunet-dns2gns.c */