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 lookup.
267 * @param rd_count number of records in @a rd
268 * @param rd the records in reply
271 result_processor (void *cls,
273 const struct GNUNET_NAMESTORE_RecordData *rd)
275 struct Request *request = cls;
276 struct GNUNET_DNSPARSER_Packet *packet;
278 struct GNUNET_DNSPARSER_Record rec;
280 request->lookup = NULL;
281 packet = request->packet;
282 packet->flags.query_or_response = 1;
283 packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
284 packet->flags.checking_disabled = 0;
285 packet->flags.authenticated_data = 1;
286 packet->flags.zero = 0;
287 packet->flags.recursion_available = 1;
288 packet->flags.message_truncated = 0;
289 packet->flags.authoritative_answer = 0;
290 //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
291 for (i=0;i<rd_count;i++)
293 // FIXME: do we need to hanlde #GNUNET_NAMESTORE_RF_SHADOW_RECORD
294 // here? Or should we do this in libgnunetgns?
295 rec.expiration_time.abs_value_us = rd[i].expiration_time;
296 switch (rd[i].record_type)
298 case GNUNET_DNSPARSER_TYPE_A:
299 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
300 rec.name = GNUNET_strdup (packet->queries[0].name);
301 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
302 rec.type = GNUNET_DNSPARSER_TYPE_A;
303 rec.data.raw.data = GNUNET_new (struct in_addr);
304 memcpy (rec.data.raw.data,
307 rec.data.raw.data_len = sizeof (struct in_addr);
308 GNUNET_array_append (packet->answers,
312 case GNUNET_DNSPARSER_TYPE_AAAA:
313 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
314 rec.name = GNUNET_strdup (packet->queries[0].name);
315 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
316 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
317 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
318 memcpy (rec.data.raw.data,
321 rec.data.raw.data_len = sizeof (struct in6_addr);
322 GNUNET_array_append (packet->answers,
326 case GNUNET_DNSPARSER_TYPE_CNAME:
327 rec.name = GNUNET_strdup (packet->queries[0].name);
328 rec.data.hostname = strdup (rd[i].data);
329 rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
330 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
331 memcpy (rec.data.hostname,
334 GNUNET_array_append (packet->answers,
343 send_response (request);
348 * Handle DNS request.
350 * @param lsock socket to use for sending the reply
351 * @param addr address to use for sending the reply
352 * @param addr_len number of bytes in @a addr
353 * @param udp_msg DNS request payload
354 * @param udp_msg_size number of bytes in @a udp_msg
357 handle_request (struct GNUNET_NETWORK_Handle *lsock,
363 struct Request *request;
364 struct GNUNET_DNSPARSER_Packet *packet;
370 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374 _("Cannot parse DNS request from %s\n"),
375 GNUNET_a2s (addr, addr_len));
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
380 packet->queries[0].name,
381 (unsigned int) packet->flags.query_or_response,
382 (int) packet->num_answers,
383 (int) packet->num_authority_records,
384 (int) packet->num_additional_records);
385 if ( (0 != packet->flags.query_or_response) ||
386 (0 != packet->num_answers) ||
387 (0 != packet->num_authority_records))
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 _("Received malformed DNS request from %s\n"),
391 GNUNET_a2s (addr, addr_len));
392 GNUNET_DNSPARSER_free_packet (packet);
395 if ( (1 != packet->num_queries) )
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
398 _("Received unsupported DNS request from %s\n"),
399 GNUNET_a2s (addr, addr_len));
400 GNUNET_DNSPARSER_free_packet (packet);
403 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
404 request->lsock = lsock;
405 request->packet = packet;
406 request->addr = &request[1];
407 request->addr_len = addr_len;
408 memcpy (&request[1], addr, addr_len);
409 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
412 name = GNUNET_strdup (packet->queries[0].name);
413 name_len = strlen (name);
417 if ( (name_len > strlen (fcfs_suffix)) &&
418 (0 == strcasecmp (fcfs_suffix,
419 &name[name_len - strlen (fcfs_suffix)])) )
421 /* replace ".fcfs.zkey.eu" with ".gnu" */
422 strcpy (&name[name_len - strlen (fcfs_suffix)],
424 use_gns = GNUNET_YES;
425 } else if ( (name_len > strlen (dns_suffix)) &&
426 (0 == strcasecmp (dns_suffix,
427 &name[name_len - strlen (dns_suffix)])) )
429 /* replace ".zkey.eu" with ".zkey" */
430 strcpy (&name[name_len - strlen (dns_suffix)],
432 use_gns = GNUNET_YES;
433 } else if ( (name_len > strlen (".gnu")) &&
434 (0 == strcasecmp (".gnu",
435 &name[name_len - strlen (".gnu")])) )
438 use_gns = GNUNET_YES;
440 if (GNUNET_YES == use_gns)
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Calling GNS on `%s'\n",
445 type = packet->queries[0].type;
446 request->lookup = GNUNET_GNS_lookup (gns,
451 NULL /* no shorten */,
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Using DNS resolver IP `%s' to resolve `%s'\n",
461 GNUNET_DNSPARSER_free_packet (request->packet);
462 request->packet = NULL;
463 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
466 &dns_result_processor,
474 * Task to read IPv4 DNS packets.
476 * @param cls the 'listen_socket4'
477 * @param tc scheduler context
480 read_dns4 (void *cls,
481 const struct GNUNET_SCHEDULER_TaskContext *tc)
483 struct sockaddr_in v4;
487 GNUNET_assert (listen_socket4 == cls);
488 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
492 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
493 return; /* shutdown? */
494 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
498 return; /* read error!? */
503 addrlen = sizeof (v4);
504 GNUNET_break (size ==
505 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
508 (struct sockaddr *) &v4,
510 handle_request (listen_socket4, &v4, addrlen,
517 * Task to read IPv6 DNS packets.
519 * @param cls the 'listen_socket6'
520 * @param tc scheduler context
523 read_dns6 (void *cls,
524 const struct GNUNET_SCHEDULER_TaskContext *tc)
526 struct sockaddr_in6 v6;
530 GNUNET_assert (listen_socket6 == cls);
531 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
535 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
536 return; /* shutdown? */
537 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
541 return; /* read error!? */
546 addrlen = sizeof (v6);
547 GNUNET_break (size ==
548 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
551 (struct sockaddr *) &v6,
553 handle_request (listen_socket6, &v6, addrlen,
560 * Main function that will be run.
563 * @param args remaining command-line arguments
564 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
565 * @param cfg configuration
568 run (void *cls, char *const *args, const char *cfgfile,
569 const struct GNUNET_CONFIGURATION_Handle *cfg)
573 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
574 _("No DNS server specified!\n"));
577 if ( (NULL == gns_zone_str) ||
579 GNUNET_CRYPTO_ecc_public_key_from_string (gns_zone_str,
580 strlen (gns_zone_str),
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 _("No valid GNS zone specified!\n"));
588 if (NULL == dns_suffix)
589 dns_suffix = DNS_SUFFIX;
590 if (NULL == fcfs_suffix)
591 fcfs_suffix = FCFS_SUFFIX;
592 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
594 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
596 GNUNET_GNS_disconnect (gns);
600 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
603 if (NULL != listen_socket4)
605 struct sockaddr_in v4;
607 memset (&v4, 0, sizeof (v4));
608 v4.sin_family = AF_INET;
609 #if HAVE_SOCKADDR_IN_SIN_LEN
610 v4.sin_len = sizeof (v4);
612 v4.sin_port = htons (listen_port);
614 GNUNET_NETWORK_socket_bind (listen_socket4,
615 (struct sockaddr *) &v4,
619 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
620 GNUNET_NETWORK_socket_close (listen_socket4);
621 listen_socket4 = NULL;
624 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
627 if (NULL != listen_socket6)
629 struct sockaddr_in6 v6;
631 memset (&v6, 0, sizeof (v6));
632 v6.sin6_family = AF_INET6;
633 #if HAVE_SOCKADDR_IN_SIN_LEN
634 v6.sin6_len = sizeof (v6);
636 v6.sin6_port = htons (listen_port);
638 GNUNET_NETWORK_socket_bind (listen_socket6,
639 (struct sockaddr *) &v6,
643 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
644 GNUNET_NETWORK_socket_close (listen_socket6);
645 listen_socket6 = NULL;
648 if ( (NULL == listen_socket4) &&
649 (NULL == listen_socket6) )
651 GNUNET_GNS_disconnect (gns);
653 GNUNET_DNSSTUB_stop (dns_stub);
657 if (NULL != listen_socket4)
658 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
662 if (NULL != listen_socket6)
663 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
668 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
674 * The main function for the fcfs daemon.
676 * @param argc number of arguments from the command line
677 * @param argv command line arguments
678 * @return 0 ok, 1 on error
684 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
686 gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
687 &GNUNET_GETOPT_set_string, &dns_ip},
688 {'f', "fcfs", "NAME",
689 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
690 &GNUNET_GETOPT_set_string, &fcfs_suffix},
691 {'s', "suffix", "SUFFIX",
692 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
693 &GNUNET_GETOPT_set_string, &dns_suffix},
694 {'p', "port", "UDPPORT",
695 gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
696 &GNUNET_GETOPT_set_uint, &listen_port},
697 {'z', "zone", "PUBLICKEY",
698 gettext_noop ("Public key of the GNS zone to use (required)"), 1,
699 &GNUNET_GETOPT_set_string, &gns_zone_str},
700 GNUNET_GETOPT_OPTION_END
704 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
707 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
710 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
711 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
713 &run, NULL)) ? 0 : 1;
714 GNUNET_free ((void*) argv);
718 /* end of gnunet-dns2gns.c */