2 This file is part of GNUnet.
3 (C) 2012 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;
125 static char *dns_suffix;
130 static char *fcfs_suffix;
138 * Task run on shutdown. Cleans up everything.
141 * @param tc scheduler context
144 do_shutdown (void *cls,
145 const struct GNUNET_SCHEDULER_TaskContext *tc)
147 if (GNUNET_SCHEDULER_NO_TASK != t4)
148 GNUNET_SCHEDULER_cancel (t4);
149 if (GNUNET_SCHEDULER_NO_TASK != t6)
150 GNUNET_SCHEDULER_cancel (t6);
151 if (NULL != listen_socket4)
153 GNUNET_NETWORK_socket_close (listen_socket4);
154 listen_socket4 = NULL;
156 if (NULL != listen_socket6)
158 GNUNET_NETWORK_socket_close (listen_socket6);
159 listen_socket6 = NULL;
161 GNUNET_GNS_disconnect (gns);
163 GNUNET_DNSSTUB_stop (dns_stub);
169 * Send the response for the given request and clean up.
171 * @param request context for the request.
174 send_response (struct Request *request)
180 GNUNET_DNSPARSER_pack (request->packet,
181 UINT16_MAX /* is this not too much? */,
185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
186 _("Failed to pack DNS response into UDP packet!\n"));
191 GNUNET_NETWORK_socket_sendto (request->lsock,
195 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
198 GNUNET_SCHEDULER_cancel (request->timeout_task);
199 GNUNET_DNSPARSER_free_packet (request->packet);
200 GNUNET_free (request);
205 * Task run on timeout. Cleans up request.
207 * @param cls 'struct Request' of the request to clean up
208 * @param tc scheduler context
211 do_timeout (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct Request *request = cls;
216 if (NULL != request->packet)
217 GNUNET_DNSPARSER_free_packet (request->packet);
218 if (NULL != request->lookup)
219 GNUNET_GNS_cancel_lookup_request (request->lookup);
220 GNUNET_free (request);
224 * Iterator called on obtained result for a DNS
228 * @param rs the request socket
229 * @param dns the DNS udp payload
230 * @param r size of the DNS payload
233 dns_result_processor (void *cls,
234 struct GNUNET_DNSSTUB_RequestSocket *rs,
235 const struct GNUNET_TUN_DnsHeader *dns,
238 struct Request *request = cls;
239 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
240 send_response (request);
244 * Iterator called on obtained result for a GNS
248 * @param rd_count number of records
249 * @param rd the records in reply
252 result_processor (void *cls,
254 const struct GNUNET_NAMESTORE_RecordData *rd)
256 struct Request *request = cls;
257 struct GNUNET_DNSPARSER_Packet *packet;
259 struct GNUNET_DNSPARSER_Record rec;
261 request->lookup = NULL;
262 packet = request->packet;
263 packet->flags.query_or_response = 1;
264 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
265 packet->flags.checking_disabled = 0;
266 packet->flags.authenticated_data = 1;
267 packet->flags.zero = 0;
268 packet->flags.recursion_available = 1;
269 packet->flags.message_truncated = 0;
270 packet->flags.authoritative_answer = 0;
271 //packet->flags.opcode = GNUNET_DNSPARSER_OPCODE_STATUS; // ???
272 for (i=0;i<rd_count;i++)
274 rec.expiration_time.abs_value = rd[i].expiration_time;
275 switch (rd[i].record_type)
277 case GNUNET_DNSPARSER_TYPE_A:
278 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
279 rec.name = GNUNET_strdup (packet->queries[0].name);
280 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
281 rec.type = GNUNET_DNSPARSER_TYPE_A;
282 rec.data.raw.data = GNUNET_malloc (sizeof (struct in_addr));
283 memcpy (rec.data.raw.data,
286 rec.data.raw.data_len = sizeof (struct in_addr);
287 GNUNET_array_append (packet->answers,
291 case GNUNET_DNSPARSER_TYPE_AAAA:
292 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
293 rec.name = GNUNET_strdup (packet->queries[0].name);
294 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
295 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
296 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
297 memcpy (rec.data.raw.data,
300 rec.data.raw.data_len = sizeof (struct in6_addr);
301 GNUNET_array_append (packet->answers,
305 case GNUNET_DNSPARSER_TYPE_CNAME:
306 rec.name = GNUNET_strdup (packet->queries[0].name);
307 rec.data.hostname = strdup (rd[i].data);
308 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
309 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
310 memcpy (rec.data.hostname,
313 GNUNET_array_append (packet->answers,
322 send_response (request);
327 * Handle DNS request.
329 * @param lsock socket to use for sending the reply
330 * @param addr address to use for sending the reply
331 * @param addr_len number of bytes in addr
332 * @param udp_msg DNS request payload
333 * @param udp_msg_size number of bytes in udp_msg
336 handle_request (struct GNUNET_NETWORK_Handle *lsock,
342 struct Request *request;
343 struct GNUNET_DNSPARSER_Packet *packet;
346 enum GNUNET_GNS_RecordType type;
349 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
353 _("Cannot parse DNS request from %s\n"),
354 GNUNET_a2s (addr, addr_len));
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Received request for %s with flags %u, #answers %d, #auth %d, #additional %d\n",
359 packet->queries[0].name,
360 (unsigned int) packet->flags.query_or_response,
361 (int) packet->num_answers,
362 (int) packet->num_authority_records,
363 (int) packet->num_additional_records);
364 if ( (0 != packet->flags.query_or_response) ||
365 (0 != packet->num_answers) ||
366 (0 != packet->num_authority_records))
368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
369 _("Received malformed DNS request from %s\n"),
370 GNUNET_a2s (addr, addr_len));
371 GNUNET_DNSPARSER_free_packet (packet);
374 if ( (1 != packet->num_queries) )
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _("Received unsupported DNS request from %s\n"),
378 GNUNET_a2s (addr, addr_len));
379 GNUNET_DNSPARSER_free_packet (packet);
382 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
383 request->lsock = lsock;
384 request->packet = packet;
385 request->addr = &request[1];
386 request->addr_len = addr_len;
387 memcpy (&request[1], addr, addr_len);
388 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
391 name = GNUNET_strdup (packet->queries[0].name);
392 name_len = strlen (name);
394 if ( (name_len > strlen (dns_suffix)) &&
395 (0 == strcasecmp (dns_suffix,
396 &name[name_len - strlen (dns_suffix)])) )
399 * FIXME: Here we want to query fcfs. But what is fcfs in our
400 * context?? -> we need a PKEY or name
402 if ( (name_len >= strlen (fcfs_suffix)) &&
403 (0 == strcasecmp (fcfs_suffix,
404 &name[name_len - strlen (fcfs_suffix)])) )
406 name[name_len - strlen (fcfs_suffix)] = '\0';
407 if (0 == strcmp (name, ""))
408 strcpy (name, GNUNET_GNS_TLD);
411 if (sizeof (name) < (strlen (GNUNET_GNS_TLD)+strlen (name)))
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Name %s.%s is too long!\n",
417 GNUNET_DNSPARSER_free_packet (request->packet);
420 sprintf (name, "%s.%s", name, GNUNET_GNS_TLD);
425 name[name_len - strlen (dns_suffix)] = '\0';
426 if (0 == strcmp (name, ""))
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 GNUNET_DNSPARSER_free_packet (request->packet);
433 if (sizeof (name) < (strlen (GNUNET_GNS_TLD_ZKEY)+strlen (name)))
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Name %s.%s is too long!\n",
439 GNUNET_DNSPARSER_free_packet (request->packet);
442 sprintf (name, "%s.%s", name, GNUNET_GNS_TLD_ZKEY);
444 name_len = strlen (name);
446 if ( (name_len >= strlen ((GNUNET_GNS_TLD))) &&
447 (0 == strcasecmp (GNUNET_GNS_TLD,
448 &name[name_len - strlen (GNUNET_GNS_TLD)])) )
449 use_gns = GNUNET_YES;
451 if ( (name_len > strlen (GNUNET_GNS_TLD_ZKEY)) &&
452 (0 == strcasecmp (GNUNET_GNS_TLD_ZKEY,
453 &name[name_len - strlen (GNUNET_GNS_TLD_ZKEY)])) )
454 use_gns = GNUNET_YES;
456 if (GNUNET_YES == use_gns)
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 type = packet->queries[0].type;
461 request->lookup = GNUNET_GNS_lookup (gns,
471 /* FIXME: do traditional *DNS* lookup; note that
472 gnunet-service-dns already has code to do this;
473 factor into library to share! Why not use GNUNET_RESOLVER here?*/
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Calling DNS at %s\n", dns_ip);
476 GNUNET_DNSPARSER_free_packet (request->packet);
477 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
480 &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,
577 * Main function that will be run.
580 * @param args remaining command-line arguments
581 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
582 * @param cfg configuration
585 run (void *cls, char *const *args, const char *cfgfile,
586 const struct GNUNET_CONFIGURATION_Handle *cfg)
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 "No DNS server specified!\n");
595 if (NULL == dns_suffix)
596 dns_suffix = DNS_SUFFIX;
597 if (NULL == fcfs_suffix)
598 fcfs_suffix = FCFS_SUFFIX;
599 if (NULL == (gns = GNUNET_GNS_connect (cfg)))
601 if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
603 GNUNET_GNS_disconnect (gns);
607 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
610 if (NULL != listen_socket4)
612 struct sockaddr_in v4;
614 memset (&v4, 0, sizeof (v4));
615 v4.sin_family = AF_INET;
616 #if HAVE_SOCKADDR_IN_SIN_LEN
617 v4.sin_len = sizeof (v4);
619 v4.sin_port = htons (53);
621 GNUNET_NETWORK_socket_bind (listen_socket4,
622 (struct sockaddr *) &v4,
625 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
626 GNUNET_NETWORK_socket_close (listen_socket4);
627 listen_socket4 = NULL;
630 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
633 if (NULL != listen_socket6)
635 struct sockaddr_in6 v6;
637 memset (&v6, 0, sizeof (v6));
638 v6.sin6_family = AF_INET6;
639 #if HAVE_SOCKADDR_IN_SIN_LEN
640 v6.sin6_len = sizeof (v6);
642 v6.sin6_port = htons (53);
644 GNUNET_NETWORK_socket_bind (listen_socket6,
645 (struct sockaddr *) &v6,
648 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
649 GNUNET_NETWORK_socket_close (listen_socket6);
650 listen_socket6 = NULL;
653 if ( (NULL == listen_socket4) &&
654 (NULL == listen_socket6) )
656 GNUNET_GNS_disconnect (gns);
658 GNUNET_DNSSTUB_stop (dns_stub);
662 if (NULL != listen_socket4)
663 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
667 if (NULL != listen_socket6)
668 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
673 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
679 * The main function for the fcfs daemon.
681 * @param argc number of arguments from the command line
682 * @param argv command line arguments
683 * @return 0 ok, 1 on error
689 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
691 gettext_noop ("IP of recursive dns resolver to use (required)"), 1,
692 &GNUNET_GETOPT_set_string, &dns_ip},
693 {'s', "suffix", NULL,
694 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
695 &GNUNET_GETOPT_set_string, &dns_suffix},
697 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
698 &GNUNET_GETOPT_set_string, &fcfs_suffix},
699 GNUNET_GETOPT_OPTION_END
703 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
706 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
709 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
710 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
712 &run, NULL)) ? 0 : 1;
717 /* end of gnunet-dns2gns.c */