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/>.
19 * @file gnunet-dns2gns.c
20 * @brief DNS server that translates DNS requests to GNS
21 * @author Christian Grothoff
24 #include <gnunet_util_lib.h>
25 #include <gnunet_dnsparser_lib.h>
26 #include <gnunet_gns_service.h>
27 #include <gnunet_dnsstub_lib.h>
31 * Timeout for DNS requests.
33 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
36 * Data kept per request.
41 * Socket to use for sending the reply.
43 struct GNUNET_NETWORK_Handle *lsock;
46 * Destination address to use.
51 * Initially, this is the DNS request, it will then be
52 * converted to the DNS response.
54 struct GNUNET_DNSPARSER_Packet *packet;
57 * Our GNS request handle.
59 struct GNUNET_GNS_LookupWithTldRequest *lookup;
62 * Our DNS request handle
64 struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
67 * Task run on timeout or shutdown to clean up without
70 struct GNUNET_SCHEDULER_Task *timeout_task;
73 * Original UDP request message.
78 * Number of bytes in @e addr.
83 * Number of bytes in @e udp_msg.
88 * ID of the original request.
90 uint16_t original_request_id;
95 * Handle to GNS resolver.
97 struct GNUNET_GNS_Handle *gns;
102 struct GNUNET_DNSSTUB_Context *dns_stub;
105 * Listen socket for IPv4.
107 static struct GNUNET_NETWORK_Handle *listen_socket4;
110 * Listen socket for IPv6.
112 static struct GNUNET_NETWORK_Handle *listen_socket6;
115 * Task for IPv4 socket.
117 static struct GNUNET_SCHEDULER_Task *t4;
120 * Task for IPv6 socket.
122 static struct GNUNET_SCHEDULER_Task *t6;
130 * UDP Port we listen on for inbound DNS requests.
132 static unsigned int listen_port = 53;
135 * Configuration to use.
137 static const struct GNUNET_CONFIGURATION_Handle *cfg;
141 * Task run on shutdown. Cleans up everything.
146 do_shutdown (void *cls)
151 GNUNET_SCHEDULER_cancel (t4);
156 GNUNET_SCHEDULER_cancel (t6);
159 if (NULL != listen_socket4)
161 GNUNET_NETWORK_socket_close (listen_socket4);
162 listen_socket4 = NULL;
164 if (NULL != listen_socket6)
166 GNUNET_NETWORK_socket_close (listen_socket6);
167 listen_socket6 = NULL;
171 GNUNET_GNS_disconnect (gns);
174 if (NULL != dns_stub)
176 GNUNET_DNSSTUB_stop (dns_stub);
183 * Send the response for the given request and clean up.
185 * @param request context for the request.
188 send_response (struct Request *request)
195 GNUNET_DNSPARSER_pack (request->packet,
196 UINT16_MAX /* is this not too much? */,
200 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
201 _("Failed to pack DNS response into UDP packet!\n"));
205 sret = GNUNET_NETWORK_socket_sendto (request->lsock,
211 (size != (size_t) sret) )
212 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
216 GNUNET_SCHEDULER_cancel (request->timeout_task);
217 GNUNET_DNSPARSER_free_packet (request->packet);
218 GNUNET_free (request->udp_msg);
219 GNUNET_free (request);
224 * Task run on timeout. Cleans up request.
226 * @param cls `struct Request *` of the request to clean up
229 do_timeout (void *cls)
231 struct Request *request = cls;
233 if (NULL != request->packet)
234 GNUNET_DNSPARSER_free_packet (request->packet);
235 if (NULL != request->lookup)
236 GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
237 if (NULL != request->dns_lookup)
238 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
239 GNUNET_free (request->udp_msg);
240 GNUNET_free (request);
245 * Iterator called on obtained result for a DNS lookup
248 * @param dns the DNS udp payload
249 * @param r size of the DNS payload
252 dns_result_processor (void *cls,
253 const struct GNUNET_TUN_DnsHeader *dns,
256 struct Request *request = cls;
260 /* DNSSTUB gave up, so we trigger timeout early */
261 GNUNET_SCHEDULER_cancel (request->timeout_task);
262 do_timeout (request);
265 if (request->original_request_id != dns->id)
267 /* for a another query, ignore */
270 request->packet = GNUNET_DNSPARSER_parse ((char*)dns,
272 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
273 send_response (request);
278 * Iterator called on obtained result for a GNS lookup.
281 * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
282 * @param rd_count number of records in @a rd
283 * @param rd the records in reply
286 result_processor (void *cls,
289 const struct GNUNET_GNSRECORD_Data *rd)
291 struct Request *request = cls;
292 struct GNUNET_DNSPARSER_Packet *packet;
293 struct GNUNET_DNSPARSER_Record rec;
295 request->lookup = NULL;
296 if (GNUNET_NO == was_gns)
298 /* TLD not configured for GNS, fall back to DNS */
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Using DNS resolver IP `%s' to resolve `%s'\n",
302 request->packet->queries[0].name);
303 request->original_request_id = request->packet->id;
304 GNUNET_DNSPARSER_free_packet (request->packet);
305 request->packet = NULL;
306 request->dns_lookup = GNUNET_DNSSTUB_resolve (dns_stub,
308 request->udp_msg_size,
309 &dns_result_processor,
313 packet = request->packet;
314 packet->flags.query_or_response = 1;
315 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
316 packet->flags.checking_disabled = 0;
317 packet->flags.authenticated_data = 1;
318 packet->flags.zero = 0;
319 packet->flags.recursion_available = 1;
320 packet->flags.message_truncated = 0;
321 packet->flags.authoritative_answer = 0;
322 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
323 for (uint32_t i=0;i<rd_count;i++)
325 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
326 // here? Or should we do this in libgnunetgns?
327 rec.expiration_time.abs_value_us = rd[i].expiration_time;
328 switch (rd[i].record_type)
330 case GNUNET_DNSPARSER_TYPE_A:
331 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
332 rec.name = GNUNET_strdup (packet->queries[0].name);
333 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
334 rec.type = GNUNET_DNSPARSER_TYPE_A;
335 rec.data.raw.data = GNUNET_new (struct in_addr);
336 GNUNET_memcpy (rec.data.raw.data,
339 rec.data.raw.data_len = sizeof (struct in_addr);
340 GNUNET_array_append (packet->answers,
344 case GNUNET_DNSPARSER_TYPE_AAAA:
345 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
346 rec.name = GNUNET_strdup (packet->queries[0].name);
347 rec.data.raw.data = GNUNET_new (struct in6_addr);
348 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
349 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
350 GNUNET_memcpy (rec.data.raw.data,
353 rec.data.raw.data_len = sizeof (struct in6_addr);
354 GNUNET_array_append (packet->answers,
358 case GNUNET_DNSPARSER_TYPE_CNAME:
359 rec.name = GNUNET_strdup (packet->queries[0].name);
360 rec.data.hostname = GNUNET_strdup (rd[i].data);
361 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
362 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
363 GNUNET_memcpy (rec.data.hostname,
366 GNUNET_array_append (packet->answers,
375 send_response (request);
380 * Handle DNS request.
382 * @param lsock socket to use for sending the reply
383 * @param addr address to use for sending the reply
384 * @param addr_len number of bytes in @a addr
385 * @param udp_msg DNS request payload
386 * @param udp_msg_size number of bytes in @a udp_msg
389 handle_request (struct GNUNET_NETWORK_Handle *lsock,
395 struct Request *request;
396 struct GNUNET_DNSPARSER_Packet *packet;
398 packet = GNUNET_DNSPARSER_parse (udp_msg,
402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
403 _("Cannot parse DNS request from %s\n"),
404 GNUNET_a2s (addr, addr_len));
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
409 packet->queries[0].name,
410 (unsigned int) packet->flags.query_or_response,
411 (int) packet->num_answers,
412 (int) packet->num_authority_records,
413 (int) packet->num_additional_records);
414 if ( (0 != packet->flags.query_or_response) ||
415 (0 != packet->num_answers) ||
416 (0 != packet->num_authority_records))
418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
419 _("Received malformed DNS request from %s\n"),
420 GNUNET_a2s (addr, addr_len));
421 GNUNET_DNSPARSER_free_packet (packet);
424 if ( (1 != packet->num_queries) )
426 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
427 _("Received unsupported DNS request from %s\n"),
430 GNUNET_DNSPARSER_free_packet (packet);
433 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
434 request->lsock = lsock;
435 request->packet = packet;
436 request->addr = &request[1];
437 request->addr_len = addr_len;
438 GNUNET_memcpy (&request[1],
441 request->udp_msg_size = udp_msg_size;
442 request->udp_msg = GNUNET_memdup (udp_msg,
444 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Calling GNS on `%s'\n",
449 packet->queries[0].name);
450 request->lookup = GNUNET_GNS_lookup_with_tld (gns,
451 packet->queries[0].name,
452 packet->queries[0].type,
460 * Task to read IPv4 DNS packets.
462 * @param cls the 'listen_socket4'
465 read_dns4 (void *cls)
467 struct sockaddr_in v4;
470 const struct GNUNET_SCHEDULER_TaskContext *tc;
472 GNUNET_assert (listen_socket4 == cls);
473 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
477 tc = GNUNET_SCHEDULER_get_task_context ();
478 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
479 return; /* shutdown? */
480 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
484 return; /* read error!? */
490 addrlen = sizeof (v4);
491 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
494 (struct sockaddr *) &v4,
498 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
502 GNUNET_break (size == sret);
503 handle_request (listen_socket4,
513 * Task to read IPv6 DNS packets.
515 * @param cls the 'listen_socket6'
518 read_dns6 (void *cls)
520 struct sockaddr_in6 v6;
523 const struct GNUNET_SCHEDULER_TaskContext *tc;
525 GNUNET_assert (listen_socket6 == cls);
526 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
530 tc = GNUNET_SCHEDULER_get_task_context ();
531 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
532 return; /* shutdown? */
533 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
537 return; /* read error!? */
543 addrlen = sizeof (v6);
544 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
547 (struct sockaddr *) &v6,
551 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
555 GNUNET_break (size == sret);
556 handle_request (listen_socket6,
566 * Main function that will be run.
569 * @param args remaining command-line arguments
570 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
571 * @param c configuration
577 const struct GNUNET_CONFIGURATION_Handle *c)
585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
586 _("No DNS server specified!\n"));
589 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
591 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
593 GNUNET_assert (NULL != (dns_stub = GNUNET_DNSSTUB_start (128)));
595 GNUNET_DNSSTUB_add_dns_ip (dns_stub,
598 GNUNET_DNSSTUB_stop (dns_stub);
599 GNUNET_GNS_disconnect (gns);
603 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
606 if (NULL != listen_socket4)
608 struct sockaddr_in v4;
610 memset (&v4, 0, sizeof (v4));
611 v4.sin_family = AF_INET;
612 #if HAVE_SOCKADDR_IN_SIN_LEN
613 v4.sin_len = sizeof (v4);
615 v4.sin_port = htons (listen_port);
617 GNUNET_NETWORK_socket_bind (listen_socket4,
618 (struct sockaddr *) &v4,
621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
622 GNUNET_NETWORK_socket_close (listen_socket4);
623 listen_socket4 = NULL;
626 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
629 if (NULL != listen_socket6)
631 struct sockaddr_in6 v6;
633 memset (&v6, 0, sizeof (v6));
634 v6.sin6_family = AF_INET6;
635 #if HAVE_SOCKADDR_IN_SIN_LEN
636 v6.sin6_len = sizeof (v6);
638 v6.sin6_port = htons (listen_port);
640 GNUNET_NETWORK_socket_bind (listen_socket6,
641 (struct sockaddr *) &v6,
644 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
645 GNUNET_NETWORK_socket_close (listen_socket6);
646 listen_socket6 = NULL;
649 if ( (NULL == listen_socket4) &&
650 (NULL == listen_socket6) )
652 GNUNET_GNS_disconnect (gns);
654 GNUNET_DNSSTUB_stop (dns_stub);
658 if (NULL != listen_socket4)
659 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
663 if (NULL != listen_socket6)
664 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
672 * The main function for the dns2gns daemon.
674 * @param argc number of arguments from the command line
675 * @param argv command line arguments
676 * @return 0 ok, 1 on error
682 struct GNUNET_GETOPT_CommandLineOption options[] = {
683 GNUNET_GETOPT_option_string ('d',
686 gettext_noop ("IP of recursive DNS resolver to use (required)"),
688 GNUNET_GETOPT_option_uint ('p',
691 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"),
693 GNUNET_GETOPT_OPTION_END
698 GNUNET_STRINGS_get_utf8_args (argc, argv,
701 GNUNET_log_setup ("gnunet-dns2gns",
706 GNUNET_PROGRAM_run (argc, argv,
708 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
710 &run, NULL)) ? 0 : 1;
711 GNUNET_free ((void*) argv);
715 /* end of gnunet-dns2gns.c */