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_EccPublicKey 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;
171 * Task run on shutdown. Cleans up everything.
174 * @param tc scheduler context
177 do_shutdown (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
180 if (GNUNET_SCHEDULER_NO_TASK != t4)
181 GNUNET_SCHEDULER_cancel (t4);
182 if (GNUNET_SCHEDULER_NO_TASK != t6)
183 GNUNET_SCHEDULER_cancel (t6);
184 if (NULL != listen_socket4)
186 GNUNET_NETWORK_socket_close (listen_socket4);
187 listen_socket4 = NULL;
189 if (NULL != listen_socket6)
191 GNUNET_NETWORK_socket_close (listen_socket6);
192 listen_socket6 = NULL;
194 GNUNET_GNS_disconnect (gns);
196 GNUNET_DNSSTUB_stop (dns_stub);
202 * Send the response for the given request and clean up.
204 * @param request context for the request.
207 send_response (struct Request *request)
213 GNUNET_DNSPARSER_pack (request->packet,
214 UINT16_MAX /* is this not too much? */,
218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
219 _("Failed to pack DNS response into UDP packet!\n"));
224 GNUNET_NETWORK_socket_sendto (request->lsock,
228 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
231 GNUNET_SCHEDULER_cancel (request->timeout_task);
232 GNUNET_DNSPARSER_free_packet (request->packet);
233 GNUNET_free (request);
238 * Task run on timeout. Cleans up request.
240 * @param cls 'struct Request' of the request to clean up
241 * @param tc scheduler context
244 do_timeout (void *cls,
245 const struct GNUNET_SCHEDULER_TaskContext *tc)
247 struct Request *request = cls;
249 if (NULL != request->packet)
250 GNUNET_DNSPARSER_free_packet (request->packet);
251 if (NULL != request->lookup)
252 GNUNET_GNS_lookup_cancel (request->lookup);
253 if (NULL != request->dns_lookup)
254 GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
255 GNUNET_free (request);
260 * Iterator called on obtained result for a DNS lookup
263 * @param rs the request socket
264 * @param dns the DNS udp payload
265 * @param r size of the DNS payload
268 dns_result_processor (void *cls,
269 struct GNUNET_DNSSTUB_RequestSocket *rs,
270 const struct GNUNET_TUN_DnsHeader *dns,
273 struct Request *request = cls;
275 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
276 send_response (request);
281 * Iterator called on obtained result for a GNS lookup.
284 * @param rd_count number of records in @a rd
285 * @param rd the records in reply
288 result_processor (void *cls,
290 const struct GNUNET_NAMESTORE_RecordData *rd)
292 struct Request *request = cls;
293 struct GNUNET_DNSPARSER_Packet *packet;
295 struct GNUNET_DNSPARSER_Record rec;
297 request->lookup = NULL;
298 packet = request->packet;
299 packet->flags.query_or_response = 1;
300 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
301 packet->flags.checking_disabled = 0;
302 packet->flags.authenticated_data = 1;
303 packet->flags.zero = 0;
304 packet->flags.recursion_available = 1;
305 packet->flags.message_truncated = 0;
306 packet->flags.authoritative_answer = 0;
307 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
308 for (i=0;i<rd_count;i++)
310 // FIXME: do we need to hanlde #GNUNET_NAMESTORE_RF_SHADOW_RECORD
311 // here? Or should we do this in libgnunetgns?
312 rec.expiration_time.abs_value_us = rd[i].expiration_time;
313 switch (rd[i].record_type)
315 case GNUNET_DNSPARSER_TYPE_A:
316 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
317 rec.name = GNUNET_strdup (packet->queries[0].name);
318 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
319 rec.type = GNUNET_DNSPARSER_TYPE_A;
320 rec.data.raw.data = GNUNET_new (struct in_addr);
321 memcpy (rec.data.raw.data,
324 rec.data.raw.data_len = sizeof (struct in_addr);
325 GNUNET_array_append (packet->answers,
329 case GNUNET_DNSPARSER_TYPE_AAAA:
330 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
331 rec.name = GNUNET_strdup (packet->queries[0].name);
332 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
333 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
334 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
335 memcpy (rec.data.raw.data,
338 rec.data.raw.data_len = sizeof (struct in6_addr);
339 GNUNET_array_append (packet->answers,
343 case GNUNET_DNSPARSER_TYPE_CNAME:
344 rec.name = GNUNET_strdup (packet->queries[0].name);
345 rec.data.hostname = strdup (rd[i].data);
346 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
347 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
348 memcpy (rec.data.hostname,
351 GNUNET_array_append (packet->answers,
360 send_response (request);
365 * Handle DNS request.
367 * @param lsock socket to use for sending the reply
368 * @param addr address to use for sending the reply
369 * @param addr_len number of bytes in @a addr
370 * @param udp_msg DNS request payload
371 * @param udp_msg_size number of bytes in @a udp_msg
374 handle_request (struct GNUNET_NETWORK_Handle *lsock,
380 struct Request *request;
381 struct GNUNET_DNSPARSER_Packet *packet;
387 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
390 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
391 _("Cannot parse DNS request from %s\n"),
392 GNUNET_a2s (addr, addr_len));
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
397 packet->queries[0].name,
398 (unsigned int) packet->flags.query_or_response,
399 (int) packet->num_answers,
400 (int) packet->num_authority_records,
401 (int) packet->num_additional_records);
402 if ( (0 != packet->flags.query_or_response) ||
403 (0 != packet->num_answers) ||
404 (0 != packet->num_authority_records))
406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
407 _("Received malformed DNS request from %s\n"),
408 GNUNET_a2s (addr, addr_len));
409 GNUNET_DNSPARSER_free_packet (packet);
412 if ( (1 != packet->num_queries) )
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415 _("Received unsupported DNS request from %s\n"),
416 GNUNET_a2s (addr, addr_len));
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 memcpy (&request[1], addr, addr_len);
426 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
429 name = GNUNET_strdup (packet->queries[0].name);
430 name_len = strlen (name);
434 if ( (name_len > strlen (fcfs_suffix)) &&
435 (0 == strcasecmp (fcfs_suffix,
436 &name[name_len - strlen (fcfs_suffix)])) )
438 /* replace ".fcfs.zkey.eu" with ".gnu" */
439 strcpy (&name[name_len - strlen (fcfs_suffix)],
441 use_gns = GNUNET_YES;
442 } else if ( (name_len > strlen (dns_suffix)) &&
443 (0 == strcasecmp (dns_suffix,
444 &name[name_len - strlen (dns_suffix)])) )
446 /* replace ".zkey.eu" with ".zkey" */
447 strcpy (&name[name_len - strlen (dns_suffix)],
449 use_gns = GNUNET_YES;
450 } else if ( (name_len > strlen (".gnu")) &&
451 (0 == strcasecmp (".gnu",
452 &name[name_len - strlen (".gnu")])) )
455 use_gns = GNUNET_YES;
457 if (GNUNET_YES == use_gns)
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Calling GNS on `%s'\n",
462 type = packet->queries[0].type;
463 request->lookup = GNUNET_GNS_lookup (gns,
468 NULL /* no shorten */,
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Using DNS resolver IP `%s' to resolve `%s'\n",
478 GNUNET_DNSPARSER_free_packet (request->packet);
479 request->packet = NULL;
480 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
483 &dns_result_processor,
491 * Task to read IPv4 DNS packets.
493 * @param cls the 'listen_socket4'
494 * @param tc scheduler context
497 read_dns4 (void *cls,
498 const struct GNUNET_SCHEDULER_TaskContext *tc)
500 struct sockaddr_in v4;
504 GNUNET_assert (listen_socket4 == cls);
505 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
509 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
510 return; /* shutdown? */
511 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
515 return; /* read error!? */
520 addrlen = sizeof (v4);
521 GNUNET_break (size ==
522 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
525 (struct sockaddr *) &v4,
527 handle_request (listen_socket4, &v4, addrlen,
534 * Task to read IPv6 DNS packets.
536 * @param cls the 'listen_socket6'
537 * @param tc scheduler context
540 read_dns6 (void *cls,
541 const struct GNUNET_SCHEDULER_TaskContext *tc)
543 struct sockaddr_in6 v6;
547 GNUNET_assert (listen_socket6 == cls);
548 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
552 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
553 return; /* shutdown? */
554 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
558 return; /* read error!? */
563 addrlen = sizeof (v6);
564 GNUNET_break (size ==
565 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
568 (struct sockaddr *) &v6,
570 handle_request (listen_socket6, &v6, addrlen,
582 if (NULL == dns_suffix)
583 dns_suffix = DNS_SUFFIX;
584 if (NULL == fcfs_suffix)
585 fcfs_suffix = FCFS_SUFFIX;
586 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
588 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
590 GNUNET_GNS_disconnect (gns);
594 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
597 if (NULL != listen_socket4)
599 struct sockaddr_in v4;
601 memset (&v4, 0, sizeof (v4));
602 v4.sin_family = AF_INET;
603 #if HAVE_SOCKADDR_IN_SIN_LEN
604 v4.sin_len = sizeof (v4);
606 v4.sin_port = htons (listen_port);
608 GNUNET_NETWORK_socket_bind (listen_socket4,
609 (struct sockaddr *) &v4,
613 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
614 GNUNET_NETWORK_socket_close (listen_socket4);
615 listen_socket4 = NULL;
618 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
621 if (NULL != listen_socket6)
623 struct sockaddr_in6 v6;
625 memset (&v6, 0, sizeof (v6));
626 v6.sin6_family = AF_INET6;
627 #if HAVE_SOCKADDR_IN_SIN_LEN
628 v6.sin6_len = sizeof (v6);
630 v6.sin6_port = htons (listen_port);
632 GNUNET_NETWORK_socket_bind (listen_socket6,
633 (struct sockaddr *) &v6,
637 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
638 GNUNET_NETWORK_socket_close (listen_socket6);
639 listen_socket6 = NULL;
642 if ( (NULL == listen_socket4) &&
643 (NULL == listen_socket6) )
645 GNUNET_GNS_disconnect (gns);
647 GNUNET_DNSSTUB_stop (dns_stub);
651 if (NULL != listen_socket4)
652 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
656 if (NULL != listen_socket6)
657 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
666 * Method called to inform about the egos of this peer.
668 * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
669 * this function is only called ONCE, and 'NULL' being passed in
670 * @a ego does indicate an error (i.e. name is taken or no default
671 * value is known). If @a ego is non-NULL and if '*ctx'
672 * is set in those callbacks, the value WILL be passed to a subsequent
673 * call to the identity callback of #GNUNET_IDENTITY_connect (if
674 * that one was not NULL).
676 * @param cls closure, NULL
677 * @param ego ego handle
678 * @param ctx context for application to store data for this ego
679 * (during the lifetime of this process, initially NULL)
680 * @param name name assigned by the user for this ego,
681 * NULL if the user just deleted the ego and it
682 * must thus no longer be used
685 identity_cb (void *cls,
686 struct GNUNET_IDENTITY_Ego *ego,
693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
694 _("No ego configured for `dns2gns` subsystem\n"));
697 GNUNET_IDENTITY_ego_get_public_key (ego,
704 * Main function that will be run.
707 * @param args remaining command-line arguments
708 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
709 * @param c configuration
712 run (void *cls, char *const *args, const char *cfgfile,
713 const struct GNUNET_CONFIGURATION_Handle *c)
719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720 _("No DNS server specified!\n"));
723 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
725 if (NULL == gns_zone_str)
727 identity = GNUNET_IDENTITY_connect (cfg,
729 id_op = GNUNET_IDENTITY_get (identity,
735 if ( (NULL == gns_zone_str) ||
737 GNUNET_CRYPTO_ecc_public_key_from_string (gns_zone_str,
738 strlen (gns_zone_str),
741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 _("No valid GNS zone specified!\n"));
743 GNUNET_SCHEDULER_shutdown ();
751 * The main function for the dns2gns daemon.
753 * @param argc number of arguments from the command line
754 * @param argv command line arguments
755 * @return 0 ok, 1 on error
761 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
763 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
764 &GNUNET_GETOPT_set_string, &dns_ip},
765 {'f', "fcfs", "NAME",
766 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
767 &GNUNET_GETOPT_set_string, &fcfs_suffix},
768 {'s', "suffix", "SUFFIX",
769 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
770 &GNUNET_GETOPT_set_string, &dns_suffix},
771 {'p', "port", "UDPPORT",
772 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
773 &GNUNET_GETOPT_set_uint, &listen_port},
774 {'z', "zone", "PUBLICKEY",
775 gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
776 &GNUNET_GETOPT_set_string, &gns_zone_str},
777 GNUNET_GETOPT_OPTION_END
781 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
784 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
787 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
788 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
790 &run, NULL)) ? 0 : 1;
791 GNUNET_free ((void*) argv);
795 /* end of gnunet-dns2gns.c */