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.
92 * Handle to GNS resolver.
94 struct GNUNET_GNS_Handle *gns;
99 struct GNUNET_DNSSTUB_Context *dns_stub;
102 * Listen socket for IPv4.
104 static struct GNUNET_NETWORK_Handle *listen_socket4;
107 * Listen socket for IPv6.
109 static struct GNUNET_NETWORK_Handle *listen_socket6;
112 * Task for IPv4 socket.
114 static struct GNUNET_SCHEDULER_Task *t4;
117 * Task for IPv6 socket.
119 static struct GNUNET_SCHEDULER_Task *t6;
127 * UDP Port we listen on for inbound DNS requests.
129 static unsigned int listen_port = 53;
132 * Configuration to use.
134 static const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * Task run on shutdown. Cleans up everything.
143 do_shutdown (void *cls)
148 GNUNET_SCHEDULER_cancel (t4);
153 GNUNET_SCHEDULER_cancel (t6);
156 if (NULL != listen_socket4)
158 GNUNET_NETWORK_socket_close (listen_socket4);
159 listen_socket4 = NULL;
161 if (NULL != listen_socket6)
163 GNUNET_NETWORK_socket_close (listen_socket6);
164 listen_socket6 = NULL;
168 GNUNET_GNS_disconnect (gns);
171 if (NULL != dns_stub)
173 GNUNET_DNSSTUB_stop (dns_stub);
180 * Send the response for the given request and clean up.
182 * @param request context for the request.
185 send_response (struct Request *request)
192 GNUNET_DNSPARSER_pack (request->packet,
193 UINT16_MAX /* is this not too much? */,
197 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
198 _("Failed to pack DNS response into UDP packet!\n"));
202 sret = GNUNET_NETWORK_socket_sendto (request->lsock,
208 (size != (size_t) sret) )
209 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
213 GNUNET_SCHEDULER_cancel (request->timeout_task);
214 GNUNET_DNSPARSER_free_packet (request->packet);
215 GNUNET_free (request->udp_msg);
216 GNUNET_free (request);
221 * Task run on timeout. Cleans up request.
223 * @param cls `struct Request *` of the request to clean up
226 do_timeout (void *cls)
228 struct Request *request = cls;
230 if (NULL != request->packet)
231 GNUNET_DNSPARSER_free_packet (request->packet);
232 if (NULL != request->lookup)
233 GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
234 if (NULL != request->dns_lookup)
235 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
236 GNUNET_free (request->udp_msg);
237 GNUNET_free (request);
242 * Iterator called on obtained result for a DNS lookup
245 * @param rs the request socket
246 * @param dns the DNS udp payload
247 * @param r size of the DNS payload
250 dns_result_processor (void *cls,
251 struct GNUNET_DNSSTUB_RequestSocket *rs,
252 const struct GNUNET_TUN_DnsHeader *dns,
255 struct Request *request = cls;
258 request->packet = GNUNET_DNSPARSER_parse ((char*)dns,
260 send_response (request);
265 * Iterator called on obtained result for a GNS lookup.
268 * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
269 * @param rd_count number of records in @a rd
270 * @param rd the records in reply
273 result_processor (void *cls,
276 const struct GNUNET_GNSRECORD_Data *rd)
278 struct Request *request = cls;
279 struct GNUNET_DNSPARSER_Packet *packet;
281 struct GNUNET_DNSPARSER_Record rec;
283 request->lookup = NULL;
284 if (GNUNET_NO == was_gns)
286 /* TLD not configured for GNS, fall back to DNS */
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Using DNS resolver IP `%s' to resolve `%s'\n",
290 request->packet->queries[0].name);
291 GNUNET_DNSPARSER_free_packet (request->packet);
292 request->packet = NULL;
293 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
295 request->udp_msg_size,
296 &dns_result_processor,
300 packet = request->packet;
301 packet->flags.query_or_response = 1;
302 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
303 packet->flags.checking_disabled = 0;
304 packet->flags.authenticated_data = 1;
305 packet->flags.zero = 0;
306 packet->flags.recursion_available = 1;
307 packet->flags.message_truncated = 0;
308 packet->flags.authoritative_answer = 0;
309 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
310 for (i=0;i<rd_count;i++)
312 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
313 // here? Or should we do this in libgnunetgns?
314 rec.expiration_time.abs_value_us = rd[i].expiration_time;
315 switch (rd[i].record_type)
317 case GNUNET_DNSPARSER_TYPE_A:
318 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
319 rec.name = GNUNET_strdup (packet->queries[0].name);
320 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
321 rec.type = GNUNET_DNSPARSER_TYPE_A;
322 rec.data.raw.data = GNUNET_new (struct in_addr);
323 GNUNET_memcpy (rec.data.raw.data,
326 rec.data.raw.data_len = sizeof (struct in_addr);
327 GNUNET_array_append (packet->answers,
331 case GNUNET_DNSPARSER_TYPE_AAAA:
332 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
333 rec.name = GNUNET_strdup (packet->queries[0].name);
334 rec.data.raw.data = GNUNET_new (struct in6_addr);
335 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
336 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
337 GNUNET_memcpy (rec.data.raw.data,
340 rec.data.raw.data_len = sizeof (struct in6_addr);
341 GNUNET_array_append (packet->answers,
345 case GNUNET_DNSPARSER_TYPE_CNAME:
346 rec.name = GNUNET_strdup (packet->queries[0].name);
347 rec.data.hostname = GNUNET_strdup (rd[i].data);
348 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
349 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
350 GNUNET_memcpy (rec.data.hostname,
353 GNUNET_array_append (packet->answers,
362 send_response (request);
367 * Handle DNS request.
369 * @param lsock socket to use for sending the reply
370 * @param addr address to use for sending the reply
371 * @param addr_len number of bytes in @a addr
372 * @param udp_msg DNS request payload
373 * @param udp_msg_size number of bytes in @a udp_msg
376 handle_request (struct GNUNET_NETWORK_Handle *lsock,
382 struct Request *request;
383 struct GNUNET_DNSPARSER_Packet *packet;
385 packet = GNUNET_DNSPARSER_parse (udp_msg,
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 _("Cannot parse DNS request from %s\n"),
391 GNUNET_a2s (addr, addr_len));
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
396 packet->queries[0].name,
397 (unsigned int) packet->flags.query_or_response,
398 (int) packet->num_answers,
399 (int) packet->num_authority_records,
400 (int) packet->num_additional_records);
401 if ( (0 != packet->flags.query_or_response) ||
402 (0 != packet->num_answers) ||
403 (0 != packet->num_authority_records))
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
406 _("Received malformed DNS request from %s\n"),
407 GNUNET_a2s (addr, addr_len));
408 GNUNET_DNSPARSER_free_packet (packet);
411 if ( (1 != packet->num_queries) )
413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414 _("Received unsupported DNS request from %s\n"),
417 GNUNET_DNSPARSER_free_packet (packet);
420 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
421 request->lsock = lsock;
422 request->packet = packet;
423 request->addr = &request[1];
424 request->addr_len = addr_len;
425 GNUNET_memcpy (&request[1],
428 request->udp_msg_size = udp_msg_size;
429 request->udp_msg = GNUNET_memdup (udp_msg,
431 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Calling GNS on `%s'\n",
436 packet->queries[0].name);
437 request->lookup = GNUNET_GNS_lookup_with_tld (gns,
438 packet->queries[0].name,
439 packet->queries[0].type,
447 * Task to read IPv4 DNS packets.
449 * @param cls the 'listen_socket4'
452 read_dns4 (void *cls)
454 struct sockaddr_in v4;
457 const struct GNUNET_SCHEDULER_TaskContext *tc;
459 GNUNET_assert (listen_socket4 == cls);
460 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
464 tc = GNUNET_SCHEDULER_get_task_context ();
465 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
466 return; /* shutdown? */
467 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
471 return; /* read error!? */
477 addrlen = sizeof (v4);
478 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
481 (struct sockaddr *) &v4,
485 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
489 GNUNET_break (size == sret);
490 handle_request (listen_socket4,
500 * Task to read IPv6 DNS packets.
502 * @param cls the 'listen_socket6'
505 read_dns6 (void *cls)
507 struct sockaddr_in6 v6;
510 const struct GNUNET_SCHEDULER_TaskContext *tc;
512 GNUNET_assert (listen_socket6 == cls);
513 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
517 tc = GNUNET_SCHEDULER_get_task_context ();
518 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
519 return; /* shutdown? */
520 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
524 return; /* read error!? */
530 addrlen = sizeof (v6);
531 sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
534 (struct sockaddr *) &v6,
538 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
542 GNUNET_break (size == sret);
543 handle_request (listen_socket6,
553 * Main function that will be run.
556 * @param args remaining command-line arguments
557 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
558 * @param c configuration
564 const struct GNUNET_CONFIGURATION_Handle *c)
572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573 _("No DNS server specified!\n"));
576 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
578 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
580 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
582 GNUNET_GNS_disconnect (gns);
586 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
589 if (NULL != listen_socket4)
591 struct sockaddr_in v4;
593 memset (&v4, 0, sizeof (v4));
594 v4.sin_family = AF_INET;
595 #if HAVE_SOCKADDR_IN_SIN_LEN
596 v4.sin_len = sizeof (v4);
598 v4.sin_port = htons (listen_port);
600 GNUNET_NETWORK_socket_bind (listen_socket4,
601 (struct sockaddr *) &v4,
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
605 GNUNET_NETWORK_socket_close (listen_socket4);
606 listen_socket4 = NULL;
609 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
612 if (NULL != listen_socket6)
614 struct sockaddr_in6 v6;
616 memset (&v6, 0, sizeof (v6));
617 v6.sin6_family = AF_INET6;
618 #if HAVE_SOCKADDR_IN_SIN_LEN
619 v6.sin6_len = sizeof (v6);
621 v6.sin6_port = htons (listen_port);
623 GNUNET_NETWORK_socket_bind (listen_socket6,
624 (struct sockaddr *) &v6,
627 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
628 GNUNET_NETWORK_socket_close (listen_socket6);
629 listen_socket6 = NULL;
632 if ( (NULL == listen_socket4) &&
633 (NULL == listen_socket6) )
635 GNUNET_GNS_disconnect (gns);
637 GNUNET_DNSSTUB_stop (dns_stub);
641 if (NULL != listen_socket4)
642 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
646 if (NULL != listen_socket6)
647 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
655 * The main function for the dns2gns daemon.
657 * @param argc number of arguments from the command line
658 * @param argv command line arguments
659 * @return 0 ok, 1 on error
665 struct GNUNET_GETOPT_CommandLineOption options[] = {
666 GNUNET_GETOPT_option_string ('d',
669 gettext_noop ("IP of recursive DNS resolver to use (required)"),
671 GNUNET_GETOPT_option_uint ('p',
674 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"),
676 GNUNET_GETOPT_OPTION_END
681 GNUNET_STRINGS_get_utf8_args (argc, argv,
684 GNUNET_log_setup ("gnunet-dns2gns",
689 GNUNET_PROGRAM_run (argc, argv,
691 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
693 &run, NULL)) ? 0 : 1;
694 GNUNET_free ((void*) argv);
698 /* end of gnunet-dns2gns.c */