glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / gns / gnunet-dns2gns.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
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      Affero General Public License for more details.
14 */
15 /**
16  * @file gnunet-dns2gns.c
17  * @brief DNS server that translates DNS requests to GNS
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include <gnunet_util_lib.h>
22 #include <gnunet_dnsparser_lib.h>
23 #include <gnunet_gns_service.h>
24 #include <gnunet_dnsstub_lib.h>
25 #include "gns.h"
26
27 /**
28  * Timeout for DNS requests.
29  */
30 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
31
32 /**
33  * Data kept per request.
34  */
35 struct Request
36 {
37   /**
38    * Socket to use for sending the reply.
39    */
40   struct GNUNET_NETWORK_Handle *lsock;
41
42   /**
43    * Destination address to use.
44    */
45   const void *addr;
46
47   /**
48    * Initially, this is the DNS request, it will then be
49    * converted to the DNS response.
50    */
51   struct GNUNET_DNSPARSER_Packet *packet;
52
53   /**
54    * Our GNS request handle.
55    */
56   struct GNUNET_GNS_LookupWithTldRequest *lookup;
57
58   /**
59    * Our DNS request handle
60    */
61   struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
62
63   /**
64    * Task run on timeout or shutdown to clean up without
65    * response.
66    */
67   struct GNUNET_SCHEDULER_Task *timeout_task;
68
69   /**
70    * Original UDP request message.
71    */
72   char *udp_msg;
73
74   /**
75    * Number of bytes in @e addr.
76    */
77   size_t addr_len;
78
79   /**
80    * Number of bytes in @e udp_msg.
81    */
82   size_t udp_msg_size;
83
84   /**
85    * ID of the original request.
86    */
87   uint16_t original_request_id;
88 };
89
90
91 /**
92  * Handle to GNS resolver.
93  */
94 struct GNUNET_GNS_Handle *gns;
95
96 /**
97  * Stub resolver
98  */
99 struct GNUNET_DNSSTUB_Context *dns_stub;
100
101 /**
102  * Listen socket for IPv4.
103  */
104 static struct GNUNET_NETWORK_Handle *listen_socket4;
105
106 /**
107  * Listen socket for IPv6.
108  */
109 static struct GNUNET_NETWORK_Handle *listen_socket6;
110
111 /**
112  * Task for IPv4 socket.
113  */
114 static struct GNUNET_SCHEDULER_Task *t4;
115
116 /**
117  * Task for IPv6 socket.
118  */
119 static struct GNUNET_SCHEDULER_Task *t6;
120
121 /**
122  * IP of DNS server
123  */
124 static char *dns_ip;
125
126 /**
127  * UDP Port we listen on for inbound DNS requests.
128  */
129 static unsigned int listen_port = 53;
130
131 /**
132  * Configuration to use.
133  */
134 static const struct GNUNET_CONFIGURATION_Handle *cfg;
135
136
137 /**
138  * Task run on shutdown.  Cleans up everything.
139  *
140  * @param cls unused
141  */
142 static void
143 do_shutdown (void *cls)
144 {
145   (void) cls;
146   if (NULL != t4)
147   {
148     GNUNET_SCHEDULER_cancel (t4);
149     t4 = NULL;
150   }
151   if (NULL != t6)
152   {
153     GNUNET_SCHEDULER_cancel (t6);
154     t6 = NULL;
155   }
156   if (NULL != listen_socket4)
157   {
158     GNUNET_NETWORK_socket_close (listen_socket4);
159     listen_socket4 = NULL;
160   }
161   if (NULL != listen_socket6)
162   {
163     GNUNET_NETWORK_socket_close (listen_socket6);
164     listen_socket6 = NULL;
165   }
166   if (NULL != gns)
167   {
168     GNUNET_GNS_disconnect (gns);
169     gns = NULL;
170   }
171   if (NULL != dns_stub)
172   {
173     GNUNET_DNSSTUB_stop (dns_stub);
174     dns_stub = NULL;
175   }
176 }
177
178
179 /**
180  * Send the response for the given request and clean up.
181  *
182  * @param request context for the request.
183  */
184 static void
185 send_response (struct Request *request)
186 {
187   char *buf;
188   size_t size;
189   ssize_t sret;
190
191   if (GNUNET_SYSERR ==
192       GNUNET_DNSPARSER_pack (request->packet,
193                              UINT16_MAX /* is this not too much? */,
194                              &buf,
195                              &size))
196     {
197       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
198                   _("Failed to pack DNS response into UDP packet!\n"));
199     }
200   else
201     {
202       sret = GNUNET_NETWORK_socket_sendto (request->lsock,
203                                            buf,
204                                            size,
205                                            request->addr,
206                                            request->addr_len);
207       if ( (sret < 0) ||
208            (size != (size_t) sret) )
209         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
210                              "sendto");
211       GNUNET_free (buf);
212     }
213   GNUNET_SCHEDULER_cancel (request->timeout_task);
214   GNUNET_DNSPARSER_free_packet (request->packet);
215   GNUNET_free (request->udp_msg);
216   GNUNET_free (request);
217 }
218
219
220 /**
221  * Task run on timeout.  Cleans up request.
222  *
223  * @param cls `struct Request *` of the request to clean up
224  */
225 static void
226 do_timeout (void *cls)
227 {
228   struct Request *request = cls;
229
230   if (NULL != request->packet)
231     GNUNET_DNSPARSER_free_packet (request->packet);
232   if (NULL != request->lookup)
233     GNUNET_GNS_lookup_with_tld_cancel (request->lookup);
234   if (NULL != request->dns_lookup)
235     GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
236   GNUNET_free (request->udp_msg);
237   GNUNET_free (request);
238 }
239
240
241 /**
242  * Iterator called on obtained result for a DNS lookup
243  *
244  * @param cls closure
245  * @param dns the DNS udp payload
246  * @param r size of the DNS payload
247  */
248 static void
249 dns_result_processor (void *cls,
250                       const struct GNUNET_TUN_DnsHeader *dns,
251                       size_t r)
252 {
253   struct Request *request = cls;
254
255   if (NULL == dns)
256   {
257     /* DNSSTUB gave up, so we trigger timeout early */
258     GNUNET_SCHEDULER_cancel (request->timeout_task);
259     do_timeout (request);
260     return;
261   }
262   if (request->original_request_id != dns->id)
263   {
264     /* for a another query, ignore */
265     return;
266   }
267   request->packet = GNUNET_DNSPARSER_parse ((char*)dns,
268                                             r);
269   send_response (request);
270 }
271
272
273 /**
274  * Iterator called on obtained result for a GNS lookup.
275  *
276  * @param cls closure
277  * @param was_gns #GNUNET_NO if the TLD is not configured for GNS
278  * @param rd_count number of records in @a rd
279  * @param rd the records in reply
280  */
281 static void
282 result_processor (void *cls,
283                   int was_gns,
284                   uint32_t rd_count,
285                   const struct GNUNET_GNSRECORD_Data *rd)
286 {
287   struct Request *request = cls;
288   struct GNUNET_DNSPARSER_Packet *packet;
289   struct GNUNET_DNSPARSER_Record rec;
290
291   request->lookup = NULL;
292   if (GNUNET_NO == was_gns)
293   {
294     /* TLD not configured for GNS, fall back to DNS */
295     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296                 "Using DNS resolver IP `%s' to resolve `%s'\n",
297                 dns_ip,
298                 request->packet->queries[0].name);
299     request->original_request_id = request->packet->id;
300     GNUNET_DNSPARSER_free_packet (request->packet);
301     request->packet = NULL;
302     request->dns_lookup = GNUNET_DNSSTUB_resolve (dns_stub,
303                                                   request->udp_msg,
304                                                   request->udp_msg_size,
305                                                   &dns_result_processor,
306                                                   request);
307     return;
308   }
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 (uint32_t i=0;i<rd_count;i++)
320     {
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)
325         {
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           GNUNET_memcpy (rec.data.raw.data,
333                   rd[i].data,
334                   rd[i].data_size);
335           rec.data.raw.data_len = sizeof (struct in_addr);
336           GNUNET_array_append (packet->answers,
337                                packet->num_answers,
338                                rec);
339           break;
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           GNUNET_memcpy (rec.data.raw.data,
347                   rd[i].data,
348                   rd[i].data_size);
349           rec.data.raw.data_len = sizeof (struct in6_addr);
350           GNUNET_array_append (packet->answers,
351                                packet->num_answers,
352                                rec);
353           break;
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           GNUNET_memcpy (rec.data.hostname,
360                   rd[i].data,
361                   rd[i].data_size);
362           GNUNET_array_append (packet->answers,
363                                packet->num_answers,
364                                rec);
365           break;
366         default:
367           /* skip */
368           break;
369         }
370     }
371   send_response (request);
372 }
373
374
375 /**
376  * Handle DNS request.
377  *
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
383  */
384 static void
385 handle_request (struct GNUNET_NETWORK_Handle *lsock,
386                 const void *addr,
387                 size_t addr_len,
388                 const char *udp_msg,
389                 size_t udp_msg_size)
390 {
391   struct Request *request;
392   struct GNUNET_DNSPARSER_Packet *packet;
393
394   packet = GNUNET_DNSPARSER_parse (udp_msg,
395                                    udp_msg_size);
396   if (NULL == packet)
397   {
398     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
399                 _("Cannot parse DNS request from %s\n"),
400                 GNUNET_a2s (addr, addr_len));
401     return;
402   }
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404               "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
405               packet->queries[0].name,
406               (unsigned int) packet->flags.query_or_response,
407               (int) packet->num_answers,
408               (int) packet->num_authority_records,
409               (int) packet->num_additional_records);
410   if ( (0 != packet->flags.query_or_response) ||
411        (0 != packet->num_answers) ||
412        (0 != packet->num_authority_records))
413   {
414     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415                 _("Received malformed DNS request from %s\n"),
416                 GNUNET_a2s (addr, addr_len));
417     GNUNET_DNSPARSER_free_packet (packet);
418     return;
419   }
420   if ( (1 != packet->num_queries) )
421   {
422     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
423                 _("Received unsupported DNS request from %s\n"),
424                 GNUNET_a2s (addr,
425                             addr_len));
426     GNUNET_DNSPARSER_free_packet (packet);
427     return;
428   }
429   request = GNUNET_malloc (sizeof (struct Request) + addr_len);
430   request->lsock = lsock;
431   request->packet = packet;
432   request->addr = &request[1];
433   request->addr_len = addr_len;
434   GNUNET_memcpy (&request[1],
435                  addr,
436                  addr_len);
437   request->udp_msg_size = udp_msg_size;
438   request->udp_msg = GNUNET_memdup (udp_msg,
439                                     udp_msg_size);
440   request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
441                                                         &do_timeout,
442                                                         request);
443   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444               "Calling GNS on `%s'\n",
445               packet->queries[0].name);
446   request->lookup = GNUNET_GNS_lookup_with_tld (gns,
447                                                 packet->queries[0].name,
448                                                 packet->queries[0].type,
449                                                 GNUNET_NO,
450                                                 &result_processor,
451                                                 request);
452 }
453
454
455 /**
456  * Task to read IPv4 DNS packets.
457  *
458  * @param cls the 'listen_socket4'
459  */
460 static void
461 read_dns4 (void *cls)
462 {
463   struct sockaddr_in v4;
464   socklen_t addrlen;
465   ssize_t size;
466   const struct GNUNET_SCHEDULER_TaskContext *tc;
467
468   GNUNET_assert (listen_socket4 == cls);
469   t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
470                                       listen_socket4,
471                                       &read_dns4,
472                                       listen_socket4);
473   tc = GNUNET_SCHEDULER_get_task_context ();
474   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
475     return; /* shutdown? */
476   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
477   if (0 > size)
478     {
479       GNUNET_break (0);
480       return; /* read error!? */
481     }
482   {
483     char buf[size + 1];
484     ssize_t sret;
485
486     addrlen = sizeof (v4);
487     sret = GNUNET_NETWORK_socket_recvfrom (listen_socket4,
488                                            buf,
489                                            size + 1,
490                                            (struct sockaddr *) &v4,
491                                            &addrlen);
492     if (0 > sret)
493     {
494       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
495                            "recvfrom");
496       return;
497     }
498     GNUNET_break (size == sret);
499     handle_request (listen_socket4,
500                     &v4,
501                     addrlen,
502                     buf,
503                     size);
504   }
505 }
506
507
508 /**
509  * Task to read IPv6 DNS packets.
510  *
511  * @param cls the 'listen_socket6'
512  */
513 static void
514 read_dns6 (void *cls)
515 {
516   struct sockaddr_in6 v6;
517   socklen_t addrlen;
518   ssize_t size;
519   const struct GNUNET_SCHEDULER_TaskContext *tc;
520
521   GNUNET_assert (listen_socket6 == cls);
522   t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
523                                       listen_socket6,
524                                       &read_dns6,
525                                       listen_socket6);
526   tc = GNUNET_SCHEDULER_get_task_context ();
527   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
528     return; /* shutdown? */
529   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
530   if (0 > size)
531     {
532       GNUNET_break (0);
533       return; /* read error!? */
534     }
535   {
536     char buf[size];
537     ssize_t sret;
538
539     addrlen = sizeof (v6);
540     sret = GNUNET_NETWORK_socket_recvfrom (listen_socket6,
541                                            buf,
542                                            size,
543                                            (struct sockaddr *) &v6,
544                                            &addrlen);
545     if (0 > sret)
546     {
547       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
548                            "recvfrom");
549       return;
550     }
551     GNUNET_break (size == sret);
552     handle_request (listen_socket6,
553                     &v6,
554                     addrlen,
555                     buf,
556                     size);
557   }
558 }
559
560
561 /**
562  * Main function that will be run.
563  *
564  * @param cls closure
565  * @param args remaining command-line arguments
566  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
567  * @param c configuration
568  */
569 static void
570 run (void *cls,
571      char *const *args,
572      const char *cfgfile,
573      const struct GNUNET_CONFIGURATION_Handle *c)
574 {
575   (void) cls;
576   (void) args;
577   (void) cfgfile;
578   cfg = c;
579   if (NULL == dns_ip)
580   {
581     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
582                 _("No DNS server specified!\n"));
583     return;
584   }
585   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
586                                  NULL);
587   if (NULL == (gns = GNUNET_GNS_connect (cfg)))
588     return;
589   GNUNET_assert (NULL != (dns_stub = GNUNET_DNSSTUB_start (128)));
590   if (GNUNET_OK !=
591       GNUNET_DNSSTUB_add_dns_ip (dns_stub,
592                                  dns_ip))
593   {
594     GNUNET_DNSSTUB_stop (dns_stub);
595     GNUNET_GNS_disconnect (gns);
596     gns = NULL;
597     return;
598   }
599   listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
600                                                  SOCK_DGRAM,
601                                                  IPPROTO_UDP);
602   if (NULL != listen_socket4)
603   {
604     struct sockaddr_in v4;
605
606     memset (&v4, 0, sizeof (v4));
607     v4.sin_family = AF_INET;
608 #if HAVE_SOCKADDR_IN_SIN_LEN
609     v4.sin_len = sizeof (v4);
610 #endif
611     v4.sin_port = htons (listen_port);
612     if (GNUNET_OK !=
613         GNUNET_NETWORK_socket_bind (listen_socket4,
614                                     (struct sockaddr *) &v4,
615                                     sizeof (v4)))
616     {
617       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
618       GNUNET_NETWORK_socket_close (listen_socket4);
619       listen_socket4 = NULL;
620     }
621   }
622   listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
623                                                 SOCK_DGRAM,
624                                                 IPPROTO_UDP);
625   if (NULL != listen_socket6)
626   {
627     struct sockaddr_in6 v6;
628
629     memset (&v6, 0, sizeof (v6));
630     v6.sin6_family = AF_INET6;
631 #if HAVE_SOCKADDR_IN_SIN_LEN
632     v6.sin6_len = sizeof (v6);
633 #endif
634     v6.sin6_port = htons (listen_port);
635     if (GNUNET_OK !=
636         GNUNET_NETWORK_socket_bind (listen_socket6,
637                                     (struct sockaddr *) &v6,
638                                     sizeof (v6)))
639     {
640       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
641       GNUNET_NETWORK_socket_close (listen_socket6);
642       listen_socket6 = NULL;
643     }
644   }
645   if ( (NULL == listen_socket4) &&
646        (NULL == listen_socket6) )
647   {
648     GNUNET_GNS_disconnect (gns);
649     gns = NULL;
650     GNUNET_DNSSTUB_stop (dns_stub);
651     dns_stub = NULL;
652     return;
653   }
654   if (NULL != listen_socket4)
655     t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
656                                         listen_socket4,
657                                         &read_dns4,
658                                         listen_socket4);
659   if (NULL != listen_socket6)
660     t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
661                                         listen_socket6,
662                                         &read_dns6,
663                                         listen_socket6);
664 }
665
666
667 /**
668  * The main function for the dns2gns daemon.
669  *
670  * @param argc number of arguments from the command line
671  * @param argv command line arguments
672  * @return 0 ok, 1 on error
673  */
674 int
675 main (int argc,
676       char *const *argv)
677 {
678   struct GNUNET_GETOPT_CommandLineOption options[] = {
679     GNUNET_GETOPT_option_string ('d',
680                                  "dns",
681                                  "IP",
682                                  gettext_noop ("IP of recursive DNS resolver to use (required)"),
683                                  &dns_ip),
684     GNUNET_GETOPT_option_uint ('p',
685                                "port",
686                                "UDPPORT",
687                                gettext_noop ("UDP port to listen on for inbound DNS requests; default: 2853"),
688                                &listen_port),
689     GNUNET_GETOPT_OPTION_END
690   };
691   int ret;
692
693   if (GNUNET_OK !=
694       GNUNET_STRINGS_get_utf8_args (argc, argv,
695                                     &argc, &argv))
696     return 2;
697   GNUNET_log_setup ("gnunet-dns2gns",
698                     "WARNING",
699                     NULL);
700   ret =
701       (GNUNET_OK ==
702        GNUNET_PROGRAM_run (argc, argv,
703                            "gnunet-dns2gns",
704                            _("GNUnet DNS-to-GNS proxy (a DNS server)"),
705                            options,
706                            &run, NULL)) ? 0 : 1;
707   GNUNET_free ((void*) argv);
708   return ret;
709 }
710
711 /* end of gnunet-dns2gns.c */