2 This file is part of GNUnet.
3 (C) 2012-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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
40 #define DNS_SUFFIX ".zkey.eu"
45 #define FCFS_SUFFIX "fcfs.zkey.eu"
48 * Data kept per request.
53 * Socket to use for sending the reply.
55 struct GNUNET_NETWORK_Handle *lsock;
58 * Destination address to use.
63 * Initially, this is the DNS request, it will then be
64 * converted to the DNS response.
66 struct GNUNET_DNSPARSER_Packet *packet;
69 * Our GNS request handle.
71 struct GNUNET_GNS_LookupRequest *lookup;
74 * Our DNS request handle
76 struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
79 * Task run on timeout or shutdown to clean up without
82 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
85 * Number of bytes in 'addr'.
93 * Handle to GNS resolver.
95 struct GNUNET_GNS_Handle *gns;
100 struct GNUNET_DNSSTUB_Context *dns_stub;
103 * Listen socket for IPv4.
105 static struct GNUNET_NETWORK_Handle *listen_socket4;
108 * Listen socket for IPv6.
110 static struct GNUNET_NETWORK_Handle *listen_socket6;
113 * Task for IPv4 socket.
115 static GNUNET_SCHEDULER_TaskIdentifier t4;
118 * Task for IPv6 socket.
120 static GNUNET_SCHEDULER_TaskIdentifier t6;
123 * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu'
125 static char *dns_suffix;
128 * FCFS suffix, suffix of FCFS-authority in DNS; defaults to 'fcfs.zkey.eu'.
130 static char *fcfs_suffix;
138 * UDP Port we listen on for inbound DNS requests.
140 static unsigned int listen_port = 53;
143 * Which GNS zone do we translate incoming DNS requests to?
145 static struct GNUNET_CRYPTO_EccPublicKey my_zone;
148 * '-z' option with the main zone to use.
150 static char *gns_zone_str;
154 * Task run on shutdown. Cleans up everything.
157 * @param tc scheduler context
160 do_shutdown (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 if (GNUNET_SCHEDULER_NO_TASK != t4)
164 GNUNET_SCHEDULER_cancel (t4);
165 if (GNUNET_SCHEDULER_NO_TASK != t6)
166 GNUNET_SCHEDULER_cancel (t6);
167 if (NULL != listen_socket4)
169 GNUNET_NETWORK_socket_close (listen_socket4);
170 listen_socket4 = NULL;
172 if (NULL != listen_socket6)
174 GNUNET_NETWORK_socket_close (listen_socket6);
175 listen_socket6 = NULL;
177 GNUNET_GNS_disconnect (gns);
179 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)
196 GNUNET_DNSPARSER_pack (request->packet,
197 UINT16_MAX /* is this not too much? */,
201 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
202 _("Failed to pack DNS response into UDP packet!\n"));
207 GNUNET_NETWORK_socket_sendto (request->lsock,
211 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
214 GNUNET_SCHEDULER_cancel (request->timeout_task);
215 GNUNET_DNSPARSER_free_packet (request->packet);
216 GNUNET_free (request);
221 * Task run on timeout. Cleans up request.
223 * @param cls 'struct Request' of the request to clean up
224 * @param tc scheduler context
227 do_timeout (void *cls,
228 const struct GNUNET_SCHEDULER_TaskContext *tc)
230 struct Request *request = cls;
232 if (NULL != request->packet)
233 GNUNET_DNSPARSER_free_packet (request->packet);
234 if (NULL != request->lookup)
235 GNUNET_GNS_lookup_cancel (request->lookup);
236 if (NULL != request->dns_lookup)
237 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
238 GNUNET_free (request);
243 * Iterator called on obtained result for a DNS lookup
246 * @param rs the request socket
247 * @param dns the DNS udp payload
248 * @param r size of the DNS payload
251 dns_result_processor (void *cls,
252 struct GNUNET_DNSSTUB_RequestSocket *rs,
253 const struct GNUNET_TUN_DnsHeader *dns,
256 struct Request *request = cls;
258 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
259 send_response (request);
264 * Iterator called on obtained result for a GNS
268 * @param rd_count number of records
269 * @param rd the records in reply
272 result_processor (void *cls,
274 const struct GNUNET_NAMESTORE_RecordData *rd)
276 struct Request *request = cls;
277 struct GNUNET_DNSPARSER_Packet *packet;
279 struct GNUNET_DNSPARSER_Record rec;
281 request->lookup = NULL;
282 packet = request->packet;
283 packet->flags.query_or_response = 1;
284 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
285 packet->flags.checking_disabled = 0;
286 packet->flags.authenticated_data = 1;
287 packet->flags.zero = 0;
288 packet->flags.recursion_available = 1;
289 packet->flags.message_truncated = 0;
290 packet->flags.authoritative_answer = 0;
291 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
292 for (i=0;i<rd_count;i++)
294 rec.expiration_time.abs_value_us = rd[i].expiration_time;
295 switch (rd[i].record_type)
297 case GNUNET_DNSPARSER_TYPE_A:
298 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
299 rec.name = GNUNET_strdup (packet->queries[0].name);
300 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
301 rec.type = GNUNET_DNSPARSER_TYPE_A;
302 rec.data.raw.data = GNUNET_malloc (sizeof (struct in_addr));
303 memcpy (rec.data.raw.data,
306 rec.data.raw.data_len = sizeof (struct in_addr);
307 GNUNET_array_append (packet->answers,
311 case GNUNET_DNSPARSER_TYPE_AAAA:
312 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
313 rec.name = GNUNET_strdup (packet->queries[0].name);
314 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
315 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
316 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
317 memcpy (rec.data.raw.data,
320 rec.data.raw.data_len = sizeof (struct in6_addr);
321 GNUNET_array_append (packet->answers,
325 case GNUNET_DNSPARSER_TYPE_CNAME:
326 rec.name = GNUNET_strdup (packet->queries[0].name);
327 rec.data.hostname = strdup (rd[i].data);
328 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
329 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
330 memcpy (rec.data.hostname,
333 GNUNET_array_append (packet->answers,
342 send_response (request);
347 * Handle DNS request.
349 * @param lsock socket to use for sending the reply
350 * @param addr address to use for sending the reply
351 * @param addr_len number of bytes in addr
352 * @param udp_msg DNS request payload
353 * @param udp_msg_size number of bytes in udp_msg
356 handle_request (struct GNUNET_NETWORK_Handle *lsock,
362 struct Request *request;
363 struct GNUNET_DNSPARSER_Packet *packet;
369 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
373 _("Cannot parse DNS request from %s\n"),
374 GNUNET_a2s (addr, addr_len));
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
379 packet->queries[0].name,
380 (unsigned int) packet->flags.query_or_response,
381 (int) packet->num_answers,
382 (int) packet->num_authority_records,
383 (int) packet->num_additional_records);
384 if ( (0 != packet->flags.query_or_response) ||
385 (0 != packet->num_answers) ||
386 (0 != packet->num_authority_records))
388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
389 _("Received malformed DNS request from %s\n"),
390 GNUNET_a2s (addr, addr_len));
391 GNUNET_DNSPARSER_free_packet (packet);
394 if ( (1 != packet->num_queries) )
396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397 _("Received unsupported DNS request from %s\n"),
398 GNUNET_a2s (addr, addr_len));
399 GNUNET_DNSPARSER_free_packet (packet);
402 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
403 request->lsock = lsock;
404 request->packet = packet;
405 request->addr = &request[1];
406 request->addr_len = addr_len;
407 memcpy (&request[1], addr, addr_len);
408 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
411 name = GNUNET_strdup (packet->queries[0].name);
412 name_len = strlen (name);
416 if ( (name_len > strlen (fcfs_suffix)) &&
417 (0 == strcasecmp (fcfs_suffix,
418 &name[name_len - strlen (fcfs_suffix)])) )
420 /* replace ".fcfs.zkey.eu" with ".gnu" */
421 strcpy (&name[name_len - strlen (fcfs_suffix)],
423 use_gns = GNUNET_YES;
424 } else if ( (name_len > strlen (dns_suffix)) &&
425 (0 == strcasecmp (dns_suffix,
426 &name[name_len - strlen (dns_suffix)])) )
428 /* replace ".fcfs.zkey.eu" with ".zkey" */
429 strcpy (&name[name_len - strlen (dns_suffix)],
431 use_gns = GNUNET_YES;
432 } else if ( (name_len > strlen (".gnu")) &&
433 (0 == strcasecmp (".gnu",
434 &name[name_len - strlen (".gnu")])) )
437 use_gns = GNUNET_YES;
439 if (GNUNET_YES == use_gns)
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Calling GNS on `%s'\n",
444 type = packet->queries[0].type;
445 request->lookup = GNUNET_GNS_lookup (gns,
450 NULL /* no shorten */,
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Using DNS resolver IP `%s' to resolve `%s'\n",
460 GNUNET_DNSPARSER_free_packet (request->packet);
461 request->packet = NULL;
462 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
465 &dns_result_processor,
473 * Task to read IPv4 DNS packets.
475 * @param cls the 'listen_socket4'
476 * @param tc scheduler context
479 read_dns4 (void *cls,
480 const struct GNUNET_SCHEDULER_TaskContext *tc)
482 struct sockaddr_in v4;
486 GNUNET_assert (listen_socket4 == cls);
487 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
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!? */
502 addrlen = sizeof (v4);
503 GNUNET_break (size ==
504 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
507 (struct sockaddr *) &v4,
509 handle_request (listen_socket4, &v4, addrlen,
516 * Task to read IPv6 DNS packets.
518 * @param cls the 'listen_socket6'
519 * @param tc scheduler context
522 read_dns6 (void *cls,
523 const struct GNUNET_SCHEDULER_TaskContext *tc)
525 struct sockaddr_in6 v6;
529 GNUNET_assert (listen_socket6 == cls);
530 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
534 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
535 return; /* shutdown? */
536 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
540 return; /* read error!? */
545 addrlen = sizeof (v6);
546 GNUNET_break (size ==
547 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
550 (struct sockaddr *) &v6,
552 handle_request (listen_socket6, &v6, addrlen,
559 * Main function that will be run.
562 * @param args remaining command-line arguments
563 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
564 * @param cfg configuration
567 run (void *cls, char *const *args, const char *cfgfile,
568 const struct GNUNET_CONFIGURATION_Handle *cfg)
572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573 _("No DNS server specified!\n"));
576 if ( (NULL == gns_zone_str) ||
578 GNUNET_CRYPTO_ecc_public_key_from_string (gns_zone_str,
579 strlen (gns_zone_str),
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 _("No valid GNS zone specified!\n"));
587 if (NULL == dns_suffix)
588 dns_suffix = DNS_SUFFIX;
589 if (NULL == fcfs_suffix)
590 fcfs_suffix = FCFS_SUFFIX;
591 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
593 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
595 GNUNET_GNS_disconnect (gns);
599 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
602 if (NULL != listen_socket4)
604 struct sockaddr_in v4;
606 memset (&v4, 0, sizeof (v4));
607 v4.sin_family = AF_INET;
608 #if HAVE_SOCKADDR_IN_SIN_LEN
609 v4.sin_len = sizeof (v4);
611 v4.sin_port = htons (listen_port);
613 GNUNET_NETWORK_socket_bind (listen_socket4,
614 (struct sockaddr *) &v4,
618 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
619 GNUNET_NETWORK_socket_close (listen_socket4);
620 listen_socket4 = NULL;
623 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
626 if (NULL != listen_socket6)
628 struct sockaddr_in6 v6;
630 memset (&v6, 0, sizeof (v6));
631 v6.sin6_family = AF_INET6;
632 #if HAVE_SOCKADDR_IN_SIN_LEN
633 v6.sin6_len = sizeof (v6);
635 v6.sin6_port = htons (listen_port);
637 GNUNET_NETWORK_socket_bind (listen_socket6,
638 (struct sockaddr *) &v6,
642 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
643 GNUNET_NETWORK_socket_close (listen_socket6);
644 listen_socket6 = NULL;
647 if ( (NULL == listen_socket4) &&
648 (NULL == listen_socket6) )
650 GNUNET_GNS_disconnect (gns);
652 GNUNET_DNSSTUB_stop (dns_stub);
656 if (NULL != listen_socket4)
657 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
661 if (NULL != listen_socket6)
662 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
667 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
673 * The main function for the fcfs daemon.
675 * @param argc number of arguments from the command line
676 * @param argv command line arguments
677 * @return 0 ok, 1 on error
683 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
685 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
686 &GNUNET_GETOPT_set_string, &dns_ip},
687 {'f', "fcfs", "NAME",
688 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
689 &GNUNET_GETOPT_set_string, &fcfs_suffix},
690 {'s', "suffix", "SUFFIX",
691 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
692 &GNUNET_GETOPT_set_string, &dns_suffix},
693 {'p', "port", "UDPPORT",
694 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
695 &GNUNET_GETOPT_set_uint, &listen_port},
696 {'z', "zone", "PUBLICKEY",
697 gettext_noop ("Public key of the GNS zone to use (required)"), 1,
698 &GNUNET_GETOPT_set_string, &gns_zone_str},
699 GNUNET_GETOPT_OPTION_END
703 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
706 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
709 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
710 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
712 &run, NULL)) ? 0 : 1;
713 GNUNET_free ((void*) argv);
717 /* end of gnunet-dns2gns.c */