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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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;
97 * Handle to GNS resolver.
99 struct GNUNET_GNS_Handle *gns;
104 struct GNUNET_DNSSTUB_Context *dns_stub;
107 * Listen socket for IPv4.
109 static struct GNUNET_NETWORK_Handle *listen_socket4;
112 * Listen socket for IPv6.
114 static struct GNUNET_NETWORK_Handle *listen_socket6;
117 * Task for IPv4 socket.
119 static struct GNUNET_SCHEDULER_Task *t4;
122 * Task for IPv6 socket.
124 static struct GNUNET_SCHEDULER_Task *t6;
132 * UDP Port we listen on for inbound DNS requests.
134 static unsigned int listen_port = 53;
137 * Configuration to use.
139 static const struct GNUNET_CONFIGURATION_Handle *cfg;
143 * Task run on shutdown. Cleans up everything.
148 do_shutdown (void *cls)
153 GNUNET_SCHEDULER_cancel (t4);
158 GNUNET_SCHEDULER_cancel (t6);
161 if (NULL != listen_socket4)
163 GNUNET_NETWORK_socket_close (listen_socket4);
164 listen_socket4 = NULL;
166 if (NULL != listen_socket6)
168 GNUNET_NETWORK_socket_close (listen_socket6);
169 listen_socket6 = NULL;
173 GNUNET_GNS_disconnect (gns);
176 if (NULL != dns_stub)
178 GNUNET_DNSSTUB_stop (dns_stub);
185 * Send the response for the given request and clean up.
187 * @param request context for the request.
190 send_response (struct Request *request)
197 GNUNET_DNSPARSER_pack (request->packet,
198 UINT16_MAX /* is this not too much? */,
202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
203 _("Failed to pack DNS response into UDP packet!\n"));
207 sret = GNUNET_NETWORK_socket_sendto (request->lsock,
213 (size != (size_t) sret) )
214 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
218 GNUNET_SCHEDULER_cancel (request->timeout_task);
219 GNUNET_DNSPARSER_free_packet (request->packet);
220 GNUNET_free (request->udp_msg);
221 GNUNET_free (request);
226 * Task run on timeout. Cleans up request.
228 * @param cls `struct Request *` of the request to clean up
231 do_timeout (void *cls)
233 struct Request *request = cls;
235 if (NULL != request->packet)
236 GNUNET_DNSPARSER_free_packet (request->packet);
237 if (NULL != request->lookup)
238 GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
239 if (NULL != request->dns_lookup)
240 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
241 GNUNET_free (request->udp_msg);
242 GNUNET_free (request);
247 * Iterator called on obtained result for a DNS lookup
250 * @param rs the request socket
251 * @param dns the DNS udp payload
252 * @param r size of the DNS payload
255 dns_result_processor (void *cls,
256 struct GNUNET_DNSSTUB_RequestSocket *rs,
257 const struct GNUNET_TUN_DnsHeader *dns,
260 struct Request *request = cls;
265 /* DNSSTUB gave up, so we trigger timeout early */
266 GNUNET_SCHEDULER_cancel (request->timeout_task);
267 do_timeout (request);
270 if (request->original_request_id != dns->id)
272 /* for a another query, ignore */
275 request->packet = GNUNET_DNSPARSER_parse ((char*)dns,
277 send_response (request);
282 * Iterator called on obtained result for a GNS lookup.
285 * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
286 * @param rd_count number of records in @a rd
287 * @param rd the records in reply
290 result_processor (void *cls,
293 const struct GNUNET_GNSRECORD_Data *rd)
295 struct Request *request = cls;
296 struct GNUNET_DNSPARSER_Packet *packet;
297 struct GNUNET_DNSPARSER_Record rec;
299 request->lookup = NULL;
300 if (GNUNET_NO == was_gns)
302 /* TLD not configured for GNS, fall back to DNS */
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Using DNS resolver IP `%s' to resolve `%s'\n",
306 request->packet->queries[0].name);
307 request->original_request_id = request->packet->id;
308 GNUNET_DNSPARSER_free_packet (request->packet);
309 request->packet = NULL;
310 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
312 request->udp_msg_size,
313 &dns_result_processor,
317 packet = request->packet;
318 packet->flags.query_or_response = 1;
319 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
320 packet->flags.checking_disabled = 0;
321 packet->flags.authenticated_data = 1;
322 packet->flags.zero = 0;
323 packet->flags.recursion_available = 1;
324 packet->flags.message_truncated = 0;
325 packet->flags.authoritative_answer = 0;
326 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
327 for (uint32_t i=0;i<rd_count;i++)
329 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
330 // here? Or should we do this in libgnunetgns?
331 rec.expiration_time.abs_value_us = rd[i].expiration_time;
332 switch (rd[i].record_type)
334 case GNUNET_DNSPARSER_TYPE_A:
335 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
336 rec.name = GNUNET_strdup (packet->queries[0].name);
337 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
338 rec.type = GNUNET_DNSPARSER_TYPE_A;
339 rec.data.raw.data = GNUNET_new (struct in_addr);
340 GNUNET_memcpy (rec.data.raw.data,
343 rec.data.raw.data_len = sizeof (struct in_addr);
344 GNUNET_array_append (packet->answers,
348 case GNUNET_DNSPARSER_TYPE_AAAA:
349 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
350 rec.name = GNUNET_strdup (packet->queries[0].name);
351 rec.data.raw.data = GNUNET_new (struct in6_addr);
352 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
353 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
354 GNUNET_memcpy (rec.data.raw.data,
357 rec.data.raw.data_len = sizeof (struct in6_addr);
358 GNUNET_array_append (packet->answers,
362 case GNUNET_DNSPARSER_TYPE_CNAME:
363 rec.name = GNUNET_strdup (packet->queries[0].name);
364 rec.data.hostname = GNUNET_strdup (rd[i].data);
365 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
366 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
367 GNUNET_memcpy (rec.data.hostname,
370 GNUNET_array_append (packet->answers,
379 send_response (request);
384 * Handle DNS request.
386 * @param lsock socket to use for sending the reply
387 * @param addr address to use for sending the reply
388 * @param addr_len number of bytes in @a addr
389 * @param udp_msg DNS request payload
390 * @param udp_msg_size number of bytes in @a udp_msg
393 handle_request (struct GNUNET_NETWORK_Handle *lsock,
399 struct Request *request;
400 struct GNUNET_DNSPARSER_Packet *packet;
402 packet = GNUNET_DNSPARSER_parse (udp_msg,
406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
407 _("Cannot parse DNS request from %s\n"),
408 GNUNET_a2s (addr, addr_len));
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
413 packet->queries[0].name,
414 (unsigned int) packet->flags.query_or_response,
415 (int) packet->num_answers,
416 (int) packet->num_authority_records,
417 (int) packet->num_additional_records);
418 if ( (0 != packet->flags.query_or_response) ||
419 (0 != packet->num_answers) ||
420 (0 != packet->num_authority_records))
422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
423 _("Received malformed DNS request from %s\n"),
424 GNUNET_a2s (addr, addr_len));
425 GNUNET_DNSPARSER_free_packet (packet);
428 if ( (1 != packet->num_queries) )
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
431 _("Received unsupported DNS request from %s\n"),
434 GNUNET_DNSPARSER_free_packet (packet);
437 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
438 request->lsock = lsock;
439 request->packet = packet;
440 request->addr = &request[1];
441 request->addr_len = addr_len;
442 GNUNET_memcpy (&request[1],
445 request->udp_msg_size = udp_msg_size;
446 request->udp_msg = GNUNET_memdup (udp_msg,
448 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Calling GNS on `%s'\n",
453 packet->queries[0].name);
454 request->lookup = GNUNET_GNS_lookup_with_tld (gns,
455 packet->queries[0].name,
456 packet->queries[0].type,
464 * Task to read IPv4 DNS packets.
466 * @param cls the 'listen_socket4'
469 read_dns4 (void *cls)
471 struct sockaddr_in v4;
474 const struct GNUNET_SCHEDULER_TaskContext *tc;
476 GNUNET_assert (listen_socket4 == cls);
477 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
481 tc = GNUNET_SCHEDULER_get_task_context ();
482 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
483 return; /* shutdown? */
484 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
488 return; /* read error!? */
494 addrlen = sizeof (v4);
495 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
498 (struct sockaddr *) &v4,
502 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
506 GNUNET_break (size == sret);
507 handle_request (listen_socket4,
517 * Task to read IPv6 DNS packets.
519 * @param cls the 'listen_socket6'
522 read_dns6 (void *cls)
524 struct sockaddr_in6 v6;
527 const struct GNUNET_SCHEDULER_TaskContext *tc;
529 GNUNET_assert (listen_socket6 == cls);
530 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
534 tc = GNUNET_SCHEDULER_get_task_context ();
535 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
536 return; /* shutdown? */
537 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
541 return; /* read error!? */
547 addrlen = sizeof (v6);
548 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
551 (struct sockaddr *) &v6,
555 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
559 GNUNET_break (size == sret);
560 handle_request (listen_socket6,
570 * Main function that will be run.
573 * @param args remaining command-line arguments
574 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
575 * @param c configuration
581 const struct GNUNET_CONFIGURATION_Handle *c)
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590 _("No DNS server specified!\n"));
593 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
595 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
597 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
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 */