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 = 2853;
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)
178 GNUNET_SCHEDULER_cancel (t4);
180 GNUNET_SCHEDULER_cancel (t6);
181 if (NULL != listen_socket4)
183 GNUNET_NETWORK_socket_close (listen_socket4);
184 listen_socket4 = NULL;
186 if (NULL != listen_socket6)
188 GNUNET_NETWORK_socket_close (listen_socket6);
189 listen_socket6 = NULL;
193 GNUNET_IDENTITY_cancel (id_op);
196 if (NULL != identity)
198 GNUNET_IDENTITY_disconnect (identity);
203 GNUNET_GNS_disconnect (gns);
206 if (NULL != dns_stub)
208 GNUNET_DNSSTUB_stop (dns_stub);
215 * Send the response for the given request and clean up.
217 * @param request context for the request.
220 send_response (struct Request *request)
226 GNUNET_DNSPARSER_pack (request->packet,
227 UINT16_MAX /* is this not too much? */,
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 _("Failed to pack DNS response into UDP packet!\n"));
237 GNUNET_NETWORK_socket_sendto (request->lsock,
241 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
244 GNUNET_SCHEDULER_cancel (request->timeout_task);
245 GNUNET_DNSPARSER_free_packet (request->packet);
246 GNUNET_free (request);
251 * Task run on timeout. Cleans up request.
253 * @param cls `struct Request *` of the request to clean up
256 do_timeout (void *cls)
258 struct Request *request = cls;
260 if (NULL != request->packet)
261 GNUNET_DNSPARSER_free_packet (request->packet);
262 if (NULL != request->lookup)
263 GNUNET_GNS_lookup_cancel (request->lookup);
264 if (NULL != request->dns_lookup)
265 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
266 GNUNET_free (request);
271 * Iterator called on obtained result for a DNS lookup
274 * @param rs the request socket
275 * @param dns the DNS udp payload
276 * @param r size of the DNS payload
279 dns_result_processor (void *cls,
280 struct GNUNET_DNSSTUB_RequestSocket *rs,
281 const struct GNUNET_TUN_DnsHeader *dns,
284 struct Request *request = cls;
286 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
287 send_response (request);
292 * Iterator called on obtained result for a GNS lookup.
295 * @param rd_count number of records in @a rd
296 * @param rd the records in reply
299 result_processor (void *cls,
301 const struct GNUNET_GNSRECORD_Data *rd)
303 struct Request *request = cls;
304 struct GNUNET_DNSPARSER_Packet *packet;
306 struct GNUNET_DNSPARSER_Record rec;
308 request->lookup = NULL;
309 packet = request->packet;
310 packet->flags.query_or_response = 1;
311 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
312 packet->flags.checking_disabled = 0;
313 packet->flags.authenticated_data = 1;
314 packet->flags.zero = 0;
315 packet->flags.recursion_available = 1;
316 packet->flags.message_truncated = 0;
317 packet->flags.authoritative_answer = 0;
318 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
319 for (i=0;i<rd_count;i++)
321 // FIXME: do we need to hanlde #GNUNET_GNSRECORD_RF_SHADOW_RECORD
322 // here? Or should we do this in libgnunetgns?
323 rec.expiration_time.abs_value_us = rd[i].expiration_time;
324 switch (rd[i].record_type)
326 case GNUNET_DNSPARSER_TYPE_A:
327 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
328 rec.name = GNUNET_strdup (packet->queries[0].name);
329 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
330 rec.type = GNUNET_DNSPARSER_TYPE_A;
331 rec.data.raw.data = GNUNET_new (struct in_addr);
332 memcpy (rec.data.raw.data,
335 rec.data.raw.data_len = sizeof (struct in_addr);
336 GNUNET_array_append (packet->answers,
340 case GNUNET_DNSPARSER_TYPE_AAAA:
341 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
342 rec.name = GNUNET_strdup (packet->queries[0].name);
343 rec.data.raw.data = GNUNET_new (struct in6_addr);
344 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
345 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
346 memcpy (rec.data.raw.data,
349 rec.data.raw.data_len = sizeof (struct in6_addr);
350 GNUNET_array_append (packet->answers,
354 case GNUNET_DNSPARSER_TYPE_CNAME:
355 rec.name = GNUNET_strdup (packet->queries[0].name);
356 rec.data.hostname = GNUNET_strdup (rd[i].data);
357 rec.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
358 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
359 memcpy (rec.data.hostname,
362 GNUNET_array_append (packet->answers,
371 send_response (request);
376 * Handle DNS request.
378 * @param lsock socket to use for sending the reply
379 * @param addr address to use for sending the reply
380 * @param addr_len number of bytes in @a addr
381 * @param udp_msg DNS request payload
382 * @param udp_msg_size number of bytes in @a udp_msg
385 handle_request (struct GNUNET_NETWORK_Handle *lsock,
391 struct Request *request;
392 struct GNUNET_DNSPARSER_Packet *packet;
398 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
402 _("Cannot parse DNS request from %s\n"),
403 GNUNET_a2s (addr, addr_len));
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
408 packet->queries[0].name,
409 (unsigned int) packet->flags.query_or_response,
410 (int) packet->num_answers,
411 (int) packet->num_authority_records,
412 (int) packet->num_additional_records);
413 if ( (0 != packet->flags.query_or_response) ||
414 (0 != packet->num_answers) ||
415 (0 != packet->num_authority_records))
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 _("Received malformed DNS request from %s\n"),
419 GNUNET_a2s (addr, addr_len));
420 GNUNET_DNSPARSER_free_packet (packet);
423 if ( (1 != packet->num_queries) )
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 _("Received unsupported DNS request from %s\n"),
427 GNUNET_a2s (addr, addr_len));
428 GNUNET_DNSPARSER_free_packet (packet);
431 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
432 request->lsock = lsock;
433 request->packet = packet;
434 request->addr = &request[1];
435 request->addr_len = addr_len;
436 memcpy (&request[1], addr, addr_len);
437 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
440 name = GNUNET_strdup (packet->queries[0].name);
441 name_len = strlen (name);
445 if ( (name_len > strlen (fcfs_suffix)) &&
446 (0 == strcasecmp (fcfs_suffix,
447 &name[name_len - strlen (fcfs_suffix)])) )
449 /* replace ".fcfs.zkey.eu" with ".gnu" */
450 strcpy (&name[name_len - strlen (fcfs_suffix)],
452 use_gns = GNUNET_YES;
454 else if ( (name_len > strlen (dns_suffix)) &&
455 (0 == strcasecmp (dns_suffix,
456 &name[name_len - strlen (dns_suffix)])) )
458 /* replace ".zkey.eu" with ".zkey" */
459 strcpy (&name[name_len - strlen (dns_suffix)],
461 use_gns = GNUNET_YES;
462 } else if ( (name_len > strlen (".gnu")) &&
463 (0 == strcasecmp (".gnu",
464 &name[name_len - strlen (".gnu")])) )
467 use_gns = GNUNET_YES;
469 if (GNUNET_YES == use_gns)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Calling GNS on `%s'\n",
474 type = packet->queries[0].type;
475 request->lookup = GNUNET_GNS_lookup (gns,
480 NULL /* no shorten */,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Using DNS resolver IP `%s' to resolve `%s'\n",
490 GNUNET_DNSPARSER_free_packet (request->packet);
491 request->packet = NULL;
492 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
495 &dns_result_processor,
503 * Task to read IPv4 DNS packets.
505 * @param cls the 'listen_socket4'
508 read_dns4 (void *cls)
510 struct sockaddr_in v4;
513 const struct GNUNET_SCHEDULER_TaskContext *tc;
515 GNUNET_assert (listen_socket4 == cls);
516 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
520 tc = GNUNET_SCHEDULER_get_task_context ();
521 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
522 return; /* shutdown? */
523 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
527 return; /* read error!? */
532 addrlen = sizeof (v4);
533 GNUNET_break (size ==
534 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
537 (struct sockaddr *) &v4,
539 handle_request (listen_socket4, &v4, addrlen,
546 * Task to read IPv6 DNS packets.
548 * @param cls the 'listen_socket6'
551 read_dns6 (void *cls)
553 struct sockaddr_in6 v6;
556 const struct GNUNET_SCHEDULER_TaskContext *tc;
558 GNUNET_assert (listen_socket6 == cls);
559 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
563 tc = GNUNET_SCHEDULER_get_task_context ();
564 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
565 return; /* shutdown? */
566 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
570 return; /* read error!? */
575 addrlen = sizeof (v6);
576 GNUNET_break (size ==
577 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
580 (struct sockaddr *) &v6,
582 handle_request (listen_socket6, &v6, addrlen,
594 if (NULL == dns_suffix)
595 dns_suffix = DNS_SUFFIX;
596 if (NULL == fcfs_suffix)
597 fcfs_suffix = FCFS_SUFFIX;
598 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
600 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
602 GNUNET_GNS_disconnect (gns);
606 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
609 if (NULL != listen_socket4)
611 struct sockaddr_in v4;
613 memset (&v4, 0, sizeof (v4));
614 v4.sin_family = AF_INET;
615 #if HAVE_SOCKADDR_IN_SIN_LEN
616 v4.sin_len = sizeof (v4);
618 v4.sin_port = htons (listen_port);
620 GNUNET_NETWORK_socket_bind (listen_socket4,
621 (struct sockaddr *) &v4,
624 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
625 GNUNET_NETWORK_socket_close (listen_socket4);
626 listen_socket4 = NULL;
629 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
632 if (NULL != listen_socket6)
634 struct sockaddr_in6 v6;
636 memset (&v6, 0, sizeof (v6));
637 v6.sin6_family = AF_INET6;
638 #if HAVE_SOCKADDR_IN_SIN_LEN
639 v6.sin6_len = sizeof (v6);
641 v6.sin6_port = htons (listen_port);
643 GNUNET_NETWORK_socket_bind (listen_socket6,
644 (struct sockaddr *) &v6,
647 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
648 GNUNET_NETWORK_socket_close (listen_socket6);
649 listen_socket6 = NULL;
652 if ( (NULL == listen_socket4) &&
653 (NULL == listen_socket6) )
655 GNUNET_GNS_disconnect (gns);
657 GNUNET_DNSSTUB_stop (dns_stub);
661 if (NULL != listen_socket4)
662 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
666 if (NULL != listen_socket6)
667 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
676 * Method called to inform about the egos of this peer.
678 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
679 * this function is only called ONCE, and 'NULL' being passed in
680 * @a ego does indicate an error (i.e. name is taken or no default
681 * value is known). If @a ego is non-NULL and if '*ctx'
682 * is set in those callbacks, the value WILL be passed to a subsequent
683 * call to the identity callback of #GNUNET_IDENTITY_connect (if
684 * that one was not NULL).
686 * @param cls closure, NULL
687 * @param ego ego handle
688 * @param ctx context for application to store data for this ego
689 * (during the lifetime of this process, initially NULL)
690 * @param name name assigned by the user for this ego,
691 * NULL if the user just deleted the ego and it
692 * must thus no longer be used
695 identity_cb (void *cls,
696 struct GNUNET_IDENTITY_Ego *ego,
703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704 _("No ego configured for `dns2gns` subsystem\n"));
707 GNUNET_IDENTITY_ego_get_public_key (ego,
714 * Main function that will be run.
717 * @param args remaining command-line arguments
718 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
719 * @param c configuration
722 run (void *cls, char *const *args, const char *cfgfile,
723 const struct GNUNET_CONFIGURATION_Handle *c)
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730 _("No DNS server specified!\n"));
733 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
735 if (NULL == gns_zone_str)
737 identity = GNUNET_IDENTITY_connect (cfg,
739 id_op = GNUNET_IDENTITY_get (identity,
745 if ( (NULL == gns_zone_str) ||
747 GNUNET_CRYPTO_ecdsa_public_key_from_string (gns_zone_str,
748 strlen (gns_zone_str),
751 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
752 _("No valid GNS zone specified!\n"));
753 GNUNET_SCHEDULER_shutdown ();
761 * The main function for the dns2gns daemon.
763 * @param argc number of arguments from the command line
764 * @param argv command line arguments
765 * @return 0 ok, 1 on error
771 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
773 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
774 &GNUNET_GETOPT_set_string, &dns_ip},
775 {'f', "fcfs", "NAME",
776 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
777 &GNUNET_GETOPT_set_string, &fcfs_suffix},
778 {'s', "suffix", "SUFFIX",
779 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
780 &GNUNET_GETOPT_set_string, &dns_suffix},
781 {'p', "port", "UDPPORT",
782 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"), 1,
783 &GNUNET_GETOPT_set_uint, &listen_port},
784 {'z', "zone", "PUBLICKEY",
785 gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
786 &GNUNET_GETOPT_set_string, &gns_zone_str},
787 GNUNET_GETOPT_OPTION_END
791 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
794 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
797 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
798 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
800 &run, NULL)) ? 0 : 1;
801 GNUNET_free ((void*) argv);
805 /* end of gnunet-dns2gns.c */