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);
162 GNUNET_DNSSTUB_stop (dns_stub);
168 * Send the response for the given request and clean up.
170 * @param request context for the request.
173 send_response (struct Request *request)
179 GNUNET_DNSPARSER_pack (request->packet,
180 UINT16_MAX /* is this not too much? */,
184 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
185 _("Failed to pack DNS response into UDP packet!\n"));
190 GNUNET_NETWORK_socket_sendto (request->lsock,
194 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
197 GNUNET_SCHEDULER_cancel (request->timeout_task);
198 GNUNET_DNSPARSER_free_packet (request->packet);
199 GNUNET_free (request);
204 * Task run on timeout. Cleans up request.
206 * @param cls 'struct Request' of the request to clean up
207 * @param tc scheduler context
210 do_timeout (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext *tc)
213 struct Request *request = cls;
215 if (NULL != request->packet)
216 GNUNET_DNSPARSER_free_packet (request->packet);
217 if (NULL != request->lookup)
218 GNUNET_GNS_cancel_lookup_request (request->lookup);
219 GNUNET_free (request);
223 * Iterator called on obtained result for a DNS
227 * @param rd_count number of records
228 * @param rd the records in reply
231 dns_result_processor (void *cls,
232 struct GNUNET_DNSSTUB_RequestSocket *rs,
233 const struct GNUNET_TUN_DnsHeader *dns,
236 struct Request *request = cls;
237 request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
238 send_response (request);
242 * Iterator called on obtained result for a GNS
246 * @param rd_count number of records
247 * @param rd the records in reply
250 result_processor (void *cls,
252 const struct GNUNET_NAMESTORE_RecordData *rd)
254 struct Request *request = cls;
255 struct GNUNET_DNSPARSER_Packet *packet;
257 struct GNUNET_DNSPARSER_Record rec;
259 request->lookup = NULL;
260 packet = request->packet;
261 packet->flags.query_or_response = 1;
262 packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
263 packet->flags.checking_disabled = 0;
264 packet->flags.authenticated_data = 1;
265 packet->flags.zero = 0;
266 packet->flags.recursion_available = 1;
267 packet->flags.message_truncated = 0;
268 packet->flags.authoritative_answer = 0;
269 //packet->flags.opcode = GNUNET_DNSPARSER_OPCODE_STATUS; // ???
270 for (i=0;i<rd_count;i++)
272 switch (rd[i].record_type)
274 case GNUNET_DNSPARSER_TYPE_A:
275 GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
276 rec.name = GNUNET_strdup (packet->queries[0].name);
277 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
278 rec.type = GNUNET_DNSPARSER_TYPE_A;
279 rec.data.raw.data = GNUNET_malloc (sizeof (struct in_addr));
280 memcpy (rec.data.raw.data,
283 rec.data.raw.data_len = sizeof (struct in_addr);
284 GNUNET_array_append (packet->answers,
288 case GNUNET_DNSPARSER_TYPE_AAAA:
289 GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
290 rec.name = GNUNET_strdup (packet->queries[0].name);
291 rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
292 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
293 rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
294 memcpy (rec.data.raw.data,
297 rec.data.raw.data_len = sizeof (struct in6_addr);
298 GNUNET_array_append (packet->answers,
302 case GNUNET_DNSPARSER_TYPE_CNAME:
303 rec.name = GNUNET_strdup (packet->queries[0].name);
304 rec.data.hostname = strdup (rd[i].data);
305 rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
306 rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
307 memcpy (rec.data.hostname,
310 GNUNET_array_append (packet->answers,
319 send_response (request);
324 * Handle DNS request.
326 * @param lsock socket to use for sending the reply
327 * @param addr address to use for sending the reply
328 * @param addr_len number of bytes in addr
329 * @param udp_msg DNS request payload
330 * @param udp_msg_size number of bytes in udp_msg
333 handle_request (struct GNUNET_NETWORK_Handle *lsock,
339 struct Request *request;
340 struct GNUNET_DNSPARSER_Packet *packet;
343 enum GNUNET_GNS_RecordType type;
346 packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
350 _("Cannot parse DNS request from %s\n"),
351 GNUNET_a2s (addr, addr_len));
354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
356 packet->flags.query_or_response,
358 packet->num_authority_records,
359 packet->num_additional_records);
360 if ( (0 != packet->flags.query_or_response) ||
361 (0 != packet->num_answers) ||
362 (0 != packet->num_authority_records))
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365 _("Received malformed DNS request from %s\n"),
366 GNUNET_a2s (addr, addr_len));
367 GNUNET_DNSPARSER_free_packet (packet);
370 if ( (1 != packet->num_queries) )
372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
373 _("Received unsupported DNS request from %s\n"),
374 GNUNET_a2s (addr, addr_len));
375 GNUNET_DNSPARSER_free_packet (packet);
378 request = GNUNET_malloc (sizeof (struct Request) + addr_len);
379 request->lsock = lsock;
380 request->packet = packet;
381 request->addr = &request[1];
382 request->addr_len = addr_len;
383 memcpy (&request[1], addr, addr_len);
384 request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
387 name = GNUNET_strdup (packet->queries[0].name);
388 name_len = strlen (name);
390 if ( (name_len > strlen (dns_suffix)) &&
391 (0 == strcasecmp (dns_suffix,
392 &name[name_len - strlen (dns_suffix)])) )
394 if (0 == strcasecmp (fcfs_suffix,
395 &name[name_len - strlen (fcfs_suffix)]))
397 name[name_len - strlen (dns_suffix) + 1] = '\0';
398 strcat (name, GNUNET_GNS_TLD);
402 name[name_len - strlen (dns_suffix) + 1] = '\0';
403 strcat (name, GNUNET_GNS_TLD_ZKEY);
405 name_len = strlen (name);
407 if ( (name_len > strlen ((GNUNET_GNS_TLD) + 1)) &&
408 (0 == strcasecmp (GNUNET_GNS_TLD,
409 &name[name_len - strlen (GNUNET_GNS_TLD)])) )
410 use_gns = GNUNET_YES;
412 if ( (name_len > strlen (GNUNET_GNS_TLD_ZKEY)) &&
413 (0 == strcasecmp (GNUNET_GNS_TLD_ZKEY,
414 &name[name_len - strlen (GNUNET_GNS_TLD_ZKEY)])) )
415 use_gns = GNUNET_YES;
417 if (GNUNET_YES == use_gns)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 type = packet->queries[0].type;
422 request->lookup = GNUNET_GNS_lookup (gns,
432 /* FIXME: do traditional *DNS* lookup; note that
433 gnunet-service-dns already has code to do this;
434 factor into library to share! Why not use GNUNET_RESOLVER here?*/
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Calling DNS at %s\n", dns_ip);
437 GNUNET_DNSPARSER_free_packet (request->packet);
438 request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
441 &dns_result_processor,
452 * Task to read IPv4 DNS packets.
454 * @param cls the 'listen_socket4'
455 * @param tc scheduler context
458 read_dns4 (void *cls,
459 const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct sockaddr_in v4;
465 GNUNET_assert (listen_socket4 == cls);
466 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
470 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
471 return; /* shutdown? */
472 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
476 return; /* read error!? */
481 addrlen = sizeof (v4);
482 GNUNET_break (size ==
483 GNUNET_NETWORK_socket_recvfrom (listen_socket4,
486 (struct sockaddr *) &v4,
488 handle_request (listen_socket4, &v4, addrlen,
495 * Task to read IPv6 DNS packets.
497 * @param cls the 'listen_socket6'
498 * @param tc scheduler context
501 read_dns6 (void *cls,
502 const struct GNUNET_SCHEDULER_TaskContext *tc)
504 struct sockaddr_in6 v6;
508 GNUNET_assert (listen_socket6 == cls);
509 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
513 if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
514 return; /* shutdown? */
515 size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
519 return; /* read error!? */
524 addrlen = sizeof (v6);
525 GNUNET_break (size ==
526 GNUNET_NETWORK_socket_recvfrom (listen_socket6,
529 (struct sockaddr *) &v6,
531 handle_request (listen_socket6, &v6, addrlen,
538 * Main function that will be run.
541 * @param args remaining command-line arguments
542 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
543 * @param cfg configuration
546 run (void *cls, char *const *args, const char *cfgfile,
547 const struct GNUNET_CONFIGURATION_Handle *cfg)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
552 "No DNS server specified!\n");
556 if (NULL == dns_suffix)
557 dns_suffix = DNS_SUFFIX;
559 if (NULL == fcfs_suffix)
560 fcfs_suffix = FCFS_SUFFIX;
562 gns = GNUNET_GNS_connect (cfg);
564 dns_stub = GNUNET_DNSSTUB_start (dns_ip);
566 if (NULL == dns_stub)
571 listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
574 if (NULL != listen_socket4)
576 struct sockaddr_in v4;
578 memset (&v4, 0, sizeof (v4));
579 v4.sin_family = AF_INET;
580 #if HAVE_SOCKADDR_IN_SIN_LEN
581 v4.sin_len = sizeof (v4);
583 v4.sin_port = htons (53);
585 GNUNET_NETWORK_socket_bind (listen_socket4,
586 (struct sockaddr *) &v4,
589 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
590 GNUNET_NETWORK_socket_close (listen_socket4);
591 listen_socket4 = NULL;
594 listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
597 if (NULL != listen_socket6)
599 struct sockaddr_in6 v6;
601 memset (&v6, 0, sizeof (v6));
602 v6.sin6_family = AF_INET6;
603 #if HAVE_SOCKADDR_IN_SIN_LEN
604 v6.sin6_len = sizeof (v6);
606 v6.sin6_port = htons (53);
608 GNUNET_NETWORK_socket_bind (listen_socket6,
609 (struct sockaddr *) &v6,
612 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
613 GNUNET_NETWORK_socket_close (listen_socket6);
614 listen_socket6 = NULL;
617 if ( (NULL == listen_socket4) &&
618 (NULL == listen_socket6) )
620 if (NULL != listen_socket4)
621 t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
625 if (NULL != listen_socket6)
626 t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
631 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
637 * The main function for the fcfs daemon.
639 * @param argc number of arguments from the command line
640 * @param argv command line arguments
641 * @return 0 ok, 1 on error
647 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
649 gettext_noop ("IP of recursive dns resolver to use (required)"), 1,
650 &GNUNET_GETOPT_set_string, &dns_ip},
651 {'s', "suffix", NULL,
652 gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
653 &GNUNET_GETOPT_set_string, &dns_suffix},
655 gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
656 &GNUNET_GETOPT_set_string, &fcfs_suffix},
657 GNUNET_GETOPT_OPTION_END
661 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
664 GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
667 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
668 _("GNUnet DNS-to-GNS proxy (a DNS server)"),
670 &run, NULL)) ? 0 : 1;
675 /* end of gnunet-dns2gns.c */