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_identity_service.h>
30 #include <gnunet_dnsstub_lib.h>
34 * Timeout for DNS requests.
36 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
41 #define DNS_SUFFIX ".zkey.eu"
46 #define FCFS_SUFFIX "fcfs.zkey.eu"
49 * Data kept per request.
54 * Socket to use for sending the reply.
56 struct GNUNET_NETWORK_Handle *lsock;
59 * Destination address to use.
64 * Initially, this is the DNS request, it will then be
65 * converted to the DNS response.
67 struct GNUNET_DNSPARSER_Packet *packet;
70 * Our GNS request handle.
72 struct GNUNET_GNS_LookupRequest *lookup;
75 * Our DNS request handle
77 struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
80 * Task run on timeout or shutdown to clean up without
83 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
86 * Number of bytes in 'addr'.
94 * Handle to GNS resolver.
96 struct GNUNET_GNS_Handle *gns;
101 struct GNUNET_DNSSTUB_Context *dns_stub;
104 * Listen socket for IPv4.
106 static struct GNUNET_NETWORK_Handle *listen_socket4;
109 * Listen socket for IPv6.
111 static struct GNUNET_NETWORK_Handle *listen_socket6;
114 * Task for IPv4 socket.
116 static GNUNET_SCHEDULER_TaskIdentifier t4;
119 * Task for IPv6 socket.
121 static GNUNET_SCHEDULER_TaskIdentifier t6;
124 * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu'
126 static char *dns_suffix;
129 * FCFS suffix, suffix of FCFS-authority in DNS; defaults to 'fcfs.zkey.eu'.
131 static char *fcfs_suffix;
139 * UDP Port we listen on for inbound DNS requests.
141 static unsigned int listen_port = 53;
144 * Which GNS zone do we translate incoming DNS requests to?
146 static struct GNUNET_CRYPTO_EcdsaPublicKey my_zone;
149 * '-z' option with the main zone to use.
151 static char *gns_zone_str;
154 * Configuration to use.
156 static const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Connection to identity service.
161 static struct GNUNET_IDENTITY_Handle *identity;
164 * Request for our ego.
166 static struct GNUNET_IDENTITY_Operation *id_op;
170 * Task run on shutdown. Cleans up everything.
173 * @param tc scheduler context
176 do_shutdown (void *cls,
177 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 if (GNUNET_SCHEDULER_NO_TASK != t4)
180 GNUNET_SCHEDULER_cancel (t4);
181 if (GNUNET_SCHEDULER_NO_TASK != t6)
182 GNUNET_SCHEDULER_cancel (t6);
183 if (NULL != listen_socket4)
185 GNUNET_NETWORK_socket_close (listen_socket4);
186 listen_socket4 = NULL;
188 if (NULL != listen_socket6)
190 GNUNET_NETWORK_socket_close (listen_socket6);
191 listen_socket6 = NULL;
195 GNUNET_IDENTITY_cancel (id_op);
198 if (NULL != identity)
200 GNUNET_IDENTITY_disconnect (identity);
205 GNUNET_GNS_disconnect (gns);
208 if (NULL != dns_stub)
210 GNUNET_DNSSTUB_stop (dns_stub);
217 * Send the response for the given request and clean up.
219 * @param request context for the request.
222 send_response (struct Request *request)
228 GNUNET_DNSPARSER_pack (request->packet,
229 UINT16_MAX /* is this not too much? */,
233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
234 _("Failed to pack DNS response into UDP packet!\n"));
239 GNUNET_NETWORK_socket_sendto (request->lsock,
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
246 GNUNET_SCHEDULER_cancel (request->timeout_task);
247 GNUNET_DNSPARSER_free_packet (request->packet);
248 GNUNET_free (request);
253 * Task run on timeout. Cleans up request.
255 * @param cls 'struct Request' of the request to clean up
256 * @param tc scheduler context
259 do_timeout (void *cls,
260 const struct GNUNET_SCHEDULER_TaskContext *tc)
262 struct Request *request = cls;
264 if (NULL != request->packet)
265 GNUNET_DNSPARSER_free_packet (request->packet);
266 if (NULL != request->lookup)
267 GNUNET_GNS_lookup_cancel (request->lookup);
268 if (NULL != request->dns_lookup)
269 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
270 GNUNET_free (request);
275 * Iterator called on obtained result for a DNS lookup
278 * @param rs the request socket
279 * @param dns the DNS udp payload
280 * @param r size of the DNS payload
283 dns_result_processor (void *cls,
284 struct GNUNET_DNSSTUB_RequestSocket *rs,
285 const struct GNUNET_TUN_DnsHeader *dns,
288 struct Request *request = cls;
290 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
291 send_response (request);
296 * Iterator called on obtained result for a GNS lookup.
299 * @param rd_count number of records in @a rd
300 * @param rd the records in reply
303 result_processor (void *cls,
305 const struct GNUNET_GNSRECORD_Data *rd)
307 struct Request *request = cls;
308 struct GNUNET_DNSPARSER_Packet *packet;
310 struct GNUNET_DNSPARSER_Record rec;
312 request->lookup = NULL;
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 (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 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 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 = strdup (rd[i].data);
361 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
362 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
363 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;
402 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
406 _("Cannot parse DNS request from %s\n"),
407 GNUNET_a2s (addr, addr_len));
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
412 packet->queries[0].name,
413 (unsigned int) packet->flags.query_or_response,
414 (int) packet->num_answers,
415 (int) packet->num_authority_records,
416 (int) packet->num_additional_records);
417 if ( (0 != packet->flags.query_or_response) ||
418 (0 != packet->num_answers) ||
419 (0 != packet->num_authority_records))
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422 _("Received malformed DNS request from %s\n"),
423 GNUNET_a2s (addr, addr_len));
424 GNUNET_DNSPARSER_free_packet (packet);
427 if ( (1 != packet->num_queries) )
429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
430 _("Received unsupported DNS request from %s\n"),
431 GNUNET_a2s (addr, addr_len));
432 GNUNET_DNSPARSER_free_packet (packet);
435 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
436 request->lsock = lsock;
437 request->packet = packet;
438 request->addr = &request[1];
439 request->addr_len = addr_len;
440 memcpy (&request[1], addr, addr_len);
441 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
444 name = GNUNET_strdup (packet->queries[0].name);
445 name_len = strlen (name);
449 if ( (name_len > strlen (fcfs_suffix)) &&
450 (0 == strcasecmp (fcfs_suffix,
451 &name[name_len - strlen (fcfs_suffix)])) )
453 /* replace ".fcfs.zkey.eu" with ".gnu" */
454 strcpy (&name[name_len - strlen (fcfs_suffix)],
456 use_gns = GNUNET_YES;
458 else if ( (name_len > strlen (dns_suffix)) &&
459 (0 == strcasecmp (dns_suffix,
460 &name[name_len - strlen (dns_suffix)])) )
462 /* replace ".zkey.eu" with ".zkey" */
463 strcpy (&name[name_len - strlen (dns_suffix)],
465 use_gns = GNUNET_YES;
466 } else if ( (name_len > strlen (".gnu")) &&
467 (0 == strcasecmp (".gnu",
468 &name[name_len - strlen (".gnu")])) )
471 use_gns = GNUNET_YES;
473 if (GNUNET_YES == use_gns)
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Calling GNS on `%s'\n",
478 type = packet->queries[0].type;
479 request->lookup = GNUNET_GNS_lookup (gns,
484 NULL /* no shorten */,
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Using DNS resolver IP `%s' to resolve `%s'\n",
494 GNUNET_DNSPARSER_free_packet (request->packet);
495 request->packet = NULL;
496 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
499 &dns_result_processor,
507 * Task to read IPv4 DNS packets.
509 * @param cls the 'listen_socket4'
510 * @param tc scheduler context
513 read_dns4 (void *cls,
514 const struct GNUNET_SCHEDULER_TaskContext *tc)
516 struct sockaddr_in v4;
520 GNUNET_assert (listen_socket4 == cls);
521 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
525 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
526 return; /* shutdown? */
527 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
531 return; /* read error!? */
536 addrlen = sizeof (v4);
537 GNUNET_break (size ==
538 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
541 (struct sockaddr *) &v4,
543 handle_request (listen_socket4, &v4, addrlen,
550 * Task to read IPv6 DNS packets.
552 * @param cls the 'listen_socket6'
553 * @param tc scheduler context
556 read_dns6 (void *cls,
557 const struct GNUNET_SCHEDULER_TaskContext *tc)
559 struct sockaddr_in6 v6;
563 GNUNET_assert (listen_socket6 == cls);
564 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
568 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
569 return; /* shutdown? */
570 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
574 return; /* read error!? */
579 addrlen = sizeof (v6);
580 GNUNET_break (size ==
581 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
584 (struct sockaddr *) &v6,
586 handle_request (listen_socket6, &v6, addrlen,
598 if (NULL == dns_suffix)
599 dns_suffix = DNS_SUFFIX;
600 if (NULL == fcfs_suffix)
601 fcfs_suffix = FCFS_SUFFIX;
602 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
604 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
606 GNUNET_GNS_disconnect (gns);
610 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
613 if (NULL != listen_socket4)
615 struct sockaddr_in v4;
617 memset (&v4, 0, sizeof (v4));
618 v4.sin_family = AF_INET;
619 #if HAVE_SOCKADDR_IN_SIN_LEN
620 v4.sin_len = sizeof (v4);
622 v4.sin_port = htons (listen_port);
624 GNUNET_NETWORK_socket_bind (listen_socket4,
625 (struct sockaddr *) &v4,
628 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
629 GNUNET_NETWORK_socket_close (listen_socket4);
630 listen_socket4 = NULL;
633 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
636 if (NULL != listen_socket6)
638 struct sockaddr_in6 v6;
640 memset (&v6, 0, sizeof (v6));
641 v6.sin6_family = AF_INET6;
642 #if HAVE_SOCKADDR_IN_SIN_LEN
643 v6.sin6_len = sizeof (v6);
645 v6.sin6_port = htons (listen_port);
647 GNUNET_NETWORK_socket_bind (listen_socket6,
648 (struct sockaddr *) &v6,
651 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
652 GNUNET_NETWORK_socket_close (listen_socket6);
653 listen_socket6 = NULL;
656 if ( (NULL == listen_socket4) &&
657 (NULL == listen_socket6) )
659 GNUNET_GNS_disconnect (gns);
661 GNUNET_DNSSTUB_stop (dns_stub);
665 if (NULL != listen_socket4)
666 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
670 if (NULL != listen_socket6)
671 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
680 * Method called to inform about the egos of this peer.
682 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
683 * this function is only called ONCE, and 'NULL' being passed in
684 * @a ego does indicate an error (i.e. name is taken or no default
685 * value is known). If @a ego is non-NULL and if '*ctx'
686 * is set in those callbacks, the value WILL be passed to a subsequent
687 * call to the identity callback of #GNUNET_IDENTITY_connect (if
688 * that one was not NULL).
690 * @param cls closure, NULL
691 * @param ego ego handle
692 * @param ctx context for application to store data for this ego
693 * (during the lifetime of this process, initially NULL)
694 * @param name name assigned by the user for this ego,
695 * NULL if the user just deleted the ego and it
696 * must thus no longer be used
699 identity_cb (void *cls,
700 struct GNUNET_IDENTITY_Ego *ego,
707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
708 _("No ego configured for `dns2gns` subsystem\n"));
711 GNUNET_IDENTITY_ego_get_public_key (ego,
718 * Main function that will be run.
721 * @param args remaining command-line arguments
722 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
723 * @param c configuration
726 run (void *cls, char *const *args, const char *cfgfile,
727 const struct GNUNET_CONFIGURATION_Handle *c)
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
734 _("No DNS server specified!\n"));
737 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
739 if (NULL == gns_zone_str)
741 identity = GNUNET_IDENTITY_connect (cfg,
743 id_op = GNUNET_IDENTITY_get (identity,
749 if ( (NULL == gns_zone_str) ||
751 GNUNET_CRYPTO_ecdsa_public_key_from_string (gns_zone_str,
752 strlen (gns_zone_str),
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 _("No valid GNS zone specified!\n"));
757 GNUNET_SCHEDULER_shutdown ();
765 * The main function for the dns2gns daemon.
767 * @param argc number of arguments from the command line
768 * @param argv command line arguments
769 * @return 0 ok, 1 on error
775 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
777 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
778 &GNUNET_GETOPT_set_string, &dns_ip},
779 {'f', "fcfs", "NAME",
780 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
781 &GNUNET_GETOPT_set_string, &fcfs_suffix},
782 {'s', "suffix", "SUFFIX",
783 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
784 &GNUNET_GETOPT_set_string, &dns_suffix},
785 {'p', "port", "UDPPORT",
786 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
787 &GNUNET_GETOPT_set_uint, &listen_port},
788 {'z', "zone", "PUBLICKEY",
789 gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
790 &GNUNET_GETOPT_set_string, &gns_zone_str},
791 GNUNET_GETOPT_OPTION_END
795 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
798 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
801 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
802 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
804 &run, NULL)) ? 0 : 1;
805 GNUNET_free ((void*) argv);
809 /* end of gnunet-dns2gns.c */