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_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 struct GNUNET_SCHEDULER_Task * 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 struct GNUNET_SCHEDULER_Task * t4;
119 * Task for IPv6 socket.
121 static struct GNUNET_SCHEDULER_Task * 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.
175 do_shutdown (void *cls)
179 GNUNET_SCHEDULER_cancel (t4);
184 GNUNET_SCHEDULER_cancel (t6);
187 if (NULL != listen_socket4)
189 GNUNET_NETWORK_socket_close (listen_socket4);
190 listen_socket4 = NULL;
192 if (NULL != listen_socket6)
194 GNUNET_NETWORK_socket_close (listen_socket6);
195 listen_socket6 = NULL;
199 GNUNET_IDENTITY_cancel (id_op);
202 if (NULL != identity)
204 GNUNET_IDENTITY_disconnect (identity);
209 GNUNET_GNS_disconnect (gns);
212 if (NULL != dns_stub)
214 GNUNET_DNSSTUB_stop (dns_stub);
221 * Send the response for the given request and clean up.
223 * @param request context for the request.
226 send_response (struct Request *request)
232 GNUNET_DNSPARSER_pack (request->packet,
233 UINT16_MAX /* is this not too much? */,
237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
238 _("Failed to pack DNS response into UDP packet!\n"));
243 GNUNET_NETWORK_socket_sendto (request->lsock,
247 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
250 GNUNET_SCHEDULER_cancel (request->timeout_task);
251 GNUNET_DNSPARSER_free_packet (request->packet);
252 GNUNET_free (request);
257 * Task run on timeout. Cleans up request.
259 * @param cls `struct Request *` of the request to clean up
262 do_timeout (void *cls)
264 struct Request *request = cls;
266 if (NULL != request->packet)
267 GNUNET_DNSPARSER_free_packet (request->packet);
268 if (NULL != request->lookup)
269 GNUNET_GNS_lookup_cancel (request->lookup);
270 if (NULL != request->dns_lookup)
271 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
272 GNUNET_free (request);
277 * Iterator called on obtained result for a DNS lookup
280 * @param rs the request socket
281 * @param dns the DNS udp payload
282 * @param r size of the DNS payload
285 dns_result_processor (void *cls,
286 struct GNUNET_DNSSTUB_RequestSocket *rs,
287 const struct GNUNET_TUN_DnsHeader *dns,
290 struct Request *request = cls;
292 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
293 send_response (request);
298 * Iterator called on obtained result for a GNS lookup.
301 * @param rd_count number of records in @a rd
302 * @param rd the records in reply
305 result_processor (void *cls,
307 const struct GNUNET_GNSRECORD_Data *rd)
309 struct Request *request = cls;
310 struct GNUNET_DNSPARSER_Packet *packet;
312 struct GNUNET_DNSPARSER_Record rec;
314 request->lookup = NULL;
315 packet = request->packet;
316 packet->flags.query_or_response = 1;
317 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
318 packet->flags.checking_disabled = 0;
319 packet->flags.authenticated_data = 1;
320 packet->flags.zero = 0;
321 packet->flags.recursion_available = 1;
322 packet->flags.message_truncated = 0;
323 packet->flags.authoritative_answer = 0;
324 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
325 for (i=0;i<rd_count;i++)
327 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
328 // here? Or should we do this in libgnunetgns?
329 rec.expiration_time.abs_value_us = rd[i].expiration_time;
330 switch (rd[i].record_type)
332 case GNUNET_DNSPARSER_TYPE_A:
333 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
334 rec.name = GNUNET_strdup (packet->queries[0].name);
335 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
336 rec.type = GNUNET_DNSPARSER_TYPE_A;
337 rec.data.raw.data = GNUNET_new (struct in_addr);
338 GNUNET_memcpy (rec.data.raw.data,
341 rec.data.raw.data_len = sizeof (struct in_addr);
342 GNUNET_array_append (packet->answers,
346 case GNUNET_DNSPARSER_TYPE_AAAA:
347 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
348 rec.name = GNUNET_strdup (packet->queries[0].name);
349 rec.data.raw.data = GNUNET_new (struct in6_addr);
350 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
351 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
352 GNUNET_memcpy (rec.data.raw.data,
355 rec.data.raw.data_len = sizeof (struct in6_addr);
356 GNUNET_array_append (packet->answers,
360 case GNUNET_DNSPARSER_TYPE_CNAME:
361 rec.name = GNUNET_strdup (packet->queries[0].name);
362 rec.data.hostname = GNUNET_strdup (rd[i].data);
363 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
364 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
365 GNUNET_memcpy (rec.data.hostname,
368 GNUNET_array_append (packet->answers,
377 send_response (request);
382 * Handle DNS request.
384 * @param lsock socket to use for sending the reply
385 * @param addr address to use for sending the reply
386 * @param addr_len number of bytes in @a addr
387 * @param udp_msg DNS request payload
388 * @param udp_msg_size number of bytes in @a udp_msg
391 handle_request (struct GNUNET_NETWORK_Handle *lsock,
397 struct Request *request;
398 struct GNUNET_DNSPARSER_Packet *packet;
404 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
408 _("Cannot parse DNS request from %s\n"),
409 GNUNET_a2s (addr, addr_len));
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
414 packet->queries[0].name,
415 (unsigned int) packet->flags.query_or_response,
416 (int) packet->num_answers,
417 (int) packet->num_authority_records,
418 (int) packet->num_additional_records);
419 if ( (0 != packet->flags.query_or_response) ||
420 (0 != packet->num_answers) ||
421 (0 != packet->num_authority_records))
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
424 _("Received malformed DNS request from %s\n"),
425 GNUNET_a2s (addr, addr_len));
426 GNUNET_DNSPARSER_free_packet (packet);
429 if ( (1 != packet->num_queries) )
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
432 _("Received unsupported DNS request from %s\n"),
433 GNUNET_a2s (addr, addr_len));
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], addr, addr_len);
443 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
446 name = GNUNET_strdup (packet->queries[0].name);
447 name_len = strlen (name);
451 if ( (name_len > strlen (fcfs_suffix)) &&
452 (0 == strcasecmp (fcfs_suffix,
453 &name[name_len - strlen (fcfs_suffix)])) )
455 /* replace ".fcfs.zkey.eu" with ".gnu" */
456 strcpy (&name[name_len - strlen (fcfs_suffix)],
458 use_gns = GNUNET_YES;
460 else if ( (name_len > strlen (dns_suffix)) &&
461 (0 == strcasecmp (dns_suffix,
462 &name[name_len - strlen (dns_suffix)])) )
464 /* replace ".zkey.eu" with ".zkey" */
465 strcpy (&name[name_len - strlen (dns_suffix)],
467 use_gns = GNUNET_YES;
468 } else if ( (name_len > strlen (".gnu")) &&
469 (0 == strcasecmp (".gnu",
470 &name[name_len - strlen (".gnu")])) )
473 use_gns = GNUNET_YES;
475 if (GNUNET_YES == use_gns)
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Calling GNS on `%s'\n",
480 type = packet->queries[0].type;
481 request->lookup = GNUNET_GNS_lookup (gns,
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Using DNS resolver IP `%s' to resolve `%s'\n",
495 GNUNET_DNSPARSER_free_packet (request->packet);
496 request->packet = NULL;
497 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
500 &dns_result_processor,
508 * Task to read IPv4 DNS packets.
510 * @param cls the 'listen_socket4'
513 read_dns4 (void *cls)
515 struct sockaddr_in v4;
518 const struct GNUNET_SCHEDULER_TaskContext *tc;
520 GNUNET_assert (listen_socket4 == cls);
521 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
525 tc = GNUNET_SCHEDULER_get_task_context ();
526 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
527 return; /* shutdown? */
528 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
532 return; /* read error!? */
537 addrlen = sizeof (v4);
538 GNUNET_break (size ==
539 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
542 (struct sockaddr *) &v4,
544 handle_request (listen_socket4,
554 * Task to read IPv6 DNS packets.
556 * @param cls the 'listen_socket6'
559 read_dns6 (void *cls)
561 struct sockaddr_in6 v6;
564 const struct GNUNET_SCHEDULER_TaskContext *tc;
566 GNUNET_assert (listen_socket6 == cls);
567 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
571 tc = GNUNET_SCHEDULER_get_task_context ();
572 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
573 return; /* shutdown? */
574 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
578 return; /* read error!? */
583 addrlen = sizeof (v6);
584 GNUNET_break (size ==
585 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
588 (struct sockaddr *) &v6,
590 handle_request (listen_socket6, &v6, addrlen,
602 if (NULL == dns_suffix)
603 dns_suffix = DNS_SUFFIX;
604 if (NULL == fcfs_suffix)
605 fcfs_suffix = FCFS_SUFFIX;
606 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
608 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
610 GNUNET_GNS_disconnect (gns);
614 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
617 if (NULL != listen_socket4)
619 struct sockaddr_in v4;
621 memset (&v4, 0, sizeof (v4));
622 v4.sin_family = AF_INET;
623 #if HAVE_SOCKADDR_IN_SIN_LEN
624 v4.sin_len = sizeof (v4);
626 v4.sin_port = htons (listen_port);
628 GNUNET_NETWORK_socket_bind (listen_socket4,
629 (struct sockaddr *) &v4,
632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
633 GNUNET_NETWORK_socket_close (listen_socket4);
634 listen_socket4 = NULL;
637 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
640 if (NULL != listen_socket6)
642 struct sockaddr_in6 v6;
644 memset (&v6, 0, sizeof (v6));
645 v6.sin6_family = AF_INET6;
646 #if HAVE_SOCKADDR_IN_SIN_LEN
647 v6.sin6_len = sizeof (v6);
649 v6.sin6_port = htons (listen_port);
651 GNUNET_NETWORK_socket_bind (listen_socket6,
652 (struct sockaddr *) &v6,
655 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
656 GNUNET_NETWORK_socket_close (listen_socket6);
657 listen_socket6 = NULL;
660 if ( (NULL == listen_socket4) &&
661 (NULL == listen_socket6) )
663 GNUNET_GNS_disconnect (gns);
665 GNUNET_DNSSTUB_stop (dns_stub);
669 if (NULL != listen_socket4)
670 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
674 if (NULL != listen_socket6)
675 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
683 * Method called to inform about the egos of this peer.
685 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
686 * this function is only called ONCE, and 'NULL' being passed in
687 * @a ego does indicate an error (i.e. name is taken or no default
688 * value is known). If @a ego is non-NULL and if '*ctx'
689 * is set in those callbacks, the value WILL be passed to a subsequent
690 * call to the identity callback of #GNUNET_IDENTITY_connect (if
691 * that one was not NULL).
693 * @param cls closure, NULL
694 * @param ego ego handle
695 * @param ctx context for application to store data for this ego
696 * (during the lifetime of this process, initially NULL)
697 * @param name name assigned by the user for this ego,
698 * NULL if the user just deleted the ego and it
699 * must thus no longer be used
702 identity_cb (void *cls,
703 struct GNUNET_IDENTITY_Ego *ego,
710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
711 _("No ego configured for `dns2gns` subsystem\n"));
714 GNUNET_IDENTITY_ego_get_public_key (ego,
721 * Main function that will be run.
724 * @param args remaining command-line arguments
725 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
726 * @param c configuration
732 const struct GNUNET_CONFIGURATION_Handle *c)
738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
739 _("No DNS server specified!\n"));
742 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
743 if (NULL == gns_zone_str)
745 identity = GNUNET_IDENTITY_connect (cfg,
747 id_op = GNUNET_IDENTITY_get (identity,
753 if ( (NULL == gns_zone_str) ||
755 GNUNET_CRYPTO_ecdsa_public_key_from_string (gns_zone_str,
756 strlen (gns_zone_str),
759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
760 _("No valid GNS zone specified!\n"));
761 GNUNET_SCHEDULER_shutdown ();
769 * The main function for the dns2gns daemon.
771 * @param argc number of arguments from the command line
772 * @param argv command line arguments
773 * @return 0 ok, 1 on error
779 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
781 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
782 &GNUNET_GETOPT_set_string, &dns_ip},
783 {'f', "fcfs", "NAME",
784 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
785 &GNUNET_GETOPT_set_string, &fcfs_suffix},
786 {'s', "suffix", "SUFFIX",
787 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
788 &GNUNET_GETOPT_set_string, &dns_suffix},
789 {'p', "port", "UDPPORT",
790 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"), 1,
791 &GNUNET_GETOPT_set_uint, &listen_port},
792 {'z', "zone", "PUBLICKEY",
793 gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
794 &GNUNET_GETOPT_set_string, &gns_zone_str},
795 GNUNET_GETOPT_OPTION_END
800 GNUNET_STRINGS_get_utf8_args (argc, argv,
803 GNUNET_log_setup ("gnunet-dns2gns",
808 GNUNET_PROGRAM_run (argc, argv,
810 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
812 &run, NULL)) ? 0 : 1;
813 GNUNET_free ((void*) argv);
817 /* end of gnunet-dns2gns.c */