fix
[oweals/gnunet.git] / src / gns / gnunet-dns2gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file gnunet-dns2gns.c
22  * @brief DNS server that translates DNS requests to GNS
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include <gnunet_util_lib.h>
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_gns_service.h>
29 #include <gnunet_dnsstub_lib.h>
30 #include "gns.h"
31
32 /**
33  * Timeout for DNS requests.
34  */
35 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
36
37 /**
38  * Default suffix
39  */
40 #define DNS_SUFFIX ".zkey.eu"
41
42 /**
43  * FCFS suffix
44  */
45 #define FCFS_SUFFIX "fcfs.zkey.eu"
46
47 /**
48  * Data kept per request.
49  */
50 struct Request
51 {
52   /**
53    * Socket to use for sending the reply.
54    */
55   struct GNUNET_NETWORK_Handle *lsock;
56
57   /**
58    * Destination address to use.
59    */
60   const void *addr;
61
62   /**
63    * Initially, this is the DNS request, it will then be
64    * converted to the DNS response.
65    */
66   struct GNUNET_DNSPARSER_Packet *packet;
67   
68   /**
69    * Our GNS request handle.
70    */
71   struct GNUNET_GNS_LookupRequest *lookup;
72
73   /**
74    * Our DNS request handle
75    */
76   struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
77
78   /**
79    * Task run on timeout or shutdown to clean up without
80    * response.
81    */
82   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
83
84   /**
85    * Number of bytes in 'addr'.
86    */ 
87   size_t addr_len;
88
89 };
90
91
92 /**
93  * Handle to GNS resolver.
94  */
95 struct GNUNET_GNS_Handle *gns;
96
97 /**
98  * Stub resolver
99  */
100 struct GNUNET_DNSSTUB_Context *dns_stub;
101
102 /**
103  * Listen socket for IPv4.
104  */
105 static struct GNUNET_NETWORK_Handle *listen_socket4;
106
107 /**
108  * Listen socket for IPv6.
109  */
110 static struct GNUNET_NETWORK_Handle *listen_socket6;
111
112 /**
113  * Task for IPv4 socket.
114  */
115 static GNUNET_SCHEDULER_TaskIdentifier t4;
116
117 /**
118  * Task for IPv6 socket.
119  */
120 static GNUNET_SCHEDULER_TaskIdentifier t6;
121
122 /**
123  * DNS suffix
124  */
125 static char *dns_suffix;
126
127 /**
128  * FCFS suffix
129  */
130 static char *fcfs_suffix;
131
132 /**
133  * IP of DNS server
134  */
135 static char *dns_ip;
136
137 /**
138  * Task run on shutdown.  Cleans up everything.
139  *
140  * @param cls unused
141  * @param tc scheduler context
142  */
143 static void
144 do_shutdown (void *cls,
145              const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
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)
152   {
153     GNUNET_NETWORK_socket_close (listen_socket4);
154     listen_socket4 = NULL;
155   }
156   if (NULL != listen_socket6)
157   {
158     GNUNET_NETWORK_socket_close (listen_socket6);
159     listen_socket6 = NULL;
160   }
161   GNUNET_GNS_disconnect (gns);
162   GNUNET_DNSSTUB_stop (dns_stub);
163   gns = NULL;
164 }
165
166
167 /**
168  * Send the response for the given request and clean up.
169  *
170  * @param request context for the request.
171  */
172 static void
173 send_response (struct Request *request)
174 {
175   char *buf;
176   size_t size;
177   
178   if (GNUNET_SYSERR ==
179       GNUNET_DNSPARSER_pack (request->packet,
180                              UINT16_MAX /* is this not too much? */,
181                              &buf,
182                              &size))
183     {
184       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
185                   _("Failed to pack DNS response into UDP packet!\n"));
186     }
187   else
188     {
189       if (size !=
190           GNUNET_NETWORK_socket_sendto (request->lsock,
191                                         buf, size,
192                                         request->addr,
193                                         request->addr_len))
194         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
195       GNUNET_free (buf);
196     }
197   GNUNET_SCHEDULER_cancel (request->timeout_task);
198   GNUNET_DNSPARSER_free_packet (request->packet);
199   GNUNET_free (request);
200 }
201
202
203 /**
204  * Task run on timeout.  Cleans up request.
205  *
206  * @param cls 'struct Request' of the request to clean up
207  * @param tc scheduler context
208  */
209 static void
210 do_timeout (void *cls,
211             const struct GNUNET_SCHEDULER_TaskContext *tc)
212 {
213   struct Request *request = cls;
214
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);
220 }
221
222 /**
223  * Iterator called on obtained result for a DNS
224  * lookup
225  *
226  * @param cls closure
227  * @param rs the request socket
228  * @param dns the DNS udp payload
229  * @param r size of the DNS payload
230  */
231 static void
232 dns_result_processor (void *cls,
233                   struct GNUNET_DNSSTUB_RequestSocket *rs,
234                   const struct GNUNET_TUN_DnsHeader *dns,
235                   size_t r)
236 {
237   struct Request *request = cls;
238   request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
239   send_response (request);
240 }
241
242 /**
243  * Iterator called on obtained result for a GNS
244  * lookup
245  *
246  * @param cls closure
247  * @param rd_count number of records
248  * @param rd the records in reply
249  */
250 static void
251 result_processor (void *cls,
252                   uint32_t rd_count,
253                   const struct GNUNET_NAMESTORE_RecordData *rd)
254 {
255   struct Request *request = cls;
256   struct GNUNET_DNSPARSER_Packet *packet;
257   uint32_t i;
258   struct GNUNET_DNSPARSER_Record rec;
259
260   request->lookup = NULL;
261   packet = request->packet;
262   packet->flags.query_or_response = 1;
263   packet->flags.return_code = GNUNET_DNSPARSER_RETURN_CODE_NO_ERROR;
264   packet->flags.checking_disabled = 0;
265   packet->flags.authenticated_data = 1;
266   packet->flags.zero = 0;
267   packet->flags.recursion_available = 1;
268   packet->flags.message_truncated = 0;
269   packet->flags.authoritative_answer = 0;
270   //packet->flags.opcode = GNUNET_DNSPARSER_OPCODE_STATUS; // ???
271   for (i=0;i<rd_count;i++)
272     {
273       switch (rd[i].record_type)
274         {
275         case GNUNET_DNSPARSER_TYPE_A:
276           GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
277           rec.name = GNUNET_strdup (packet->queries[0].name);
278           rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
279           rec.type = GNUNET_DNSPARSER_TYPE_A;
280           rec.data.raw.data = GNUNET_malloc (sizeof (struct in_addr));
281           memcpy (rec.data.raw.data,
282                   rd[i].data,
283                   rd[i].data_size);
284           rec.data.raw.data_len = sizeof (struct in_addr);
285           GNUNET_array_append (packet->answers,
286                                packet->num_answers,
287                                rec);
288           break;
289         case GNUNET_DNSPARSER_TYPE_AAAA:
290           GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
291           rec.name = GNUNET_strdup (packet->queries[0].name);
292           rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
293           rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
294           rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
295           memcpy (rec.data.raw.data,
296                   rd[i].data,
297                   rd[i].data_size);
298           rec.data.raw.data_len = sizeof (struct in6_addr);
299           GNUNET_array_append (packet->answers,
300                                packet->num_answers,
301                                rec);
302           break;
303         case GNUNET_DNSPARSER_TYPE_CNAME:
304           rec.name = GNUNET_strdup (packet->queries[0].name);
305           rec.data.hostname = strdup (rd[i].data);
306           rec.class = GNUNET_DNSPARSER_CLASS_INTERNET;
307           rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
308           memcpy (rec.data.hostname,
309                   rd[i].data,
310                   rd[i].data_size);
311           GNUNET_array_append (packet->answers,
312                                packet->num_answers,
313                                rec);
314           break;
315         default:
316           /* skip */
317           break;
318         }
319     }
320   send_response (request);
321 }
322
323
324 /**
325  * Handle DNS request.
326  *
327  * @param lsock socket to use for sending the reply
328  * @param addr address to use for sending the reply
329  * @param addr_len number of bytes in addr
330  * @param udp_msg DNS request payload
331  * @param udp_msg_size number of bytes in udp_msg 
332  */
333 static void
334 handle_request (struct GNUNET_NETWORK_Handle *lsock,
335                 const void *addr,
336                 size_t addr_len,
337                 const char *udp_msg,
338                 size_t udp_msg_size)
339 {
340   struct Request *request;
341   struct GNUNET_DNSPARSER_Packet *packet;
342   char *name;
343   size_t name_len;
344   enum GNUNET_GNS_RecordType type;
345   int use_gns;
346
347   packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
348   if (NULL == packet)
349     {
350       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351                   _("Cannot parse DNS request from %s\n"),
352                   GNUNET_a2s (addr, addr_len));
353       return;
354     }
355   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
356                   "%d, %d, %d, %d\n",
357                   packet->flags.query_or_response,
358                   packet->num_answers,
359                   packet->num_authority_records,
360                   packet->num_additional_records);
361   if ( (0 != packet->flags.query_or_response) || 
362        (0 != packet->num_answers) ||
363        (0 != packet->num_authority_records))
364     {
365       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366                   _("Received malformed DNS request from %s\n"),
367                   GNUNET_a2s (addr, addr_len));
368       GNUNET_DNSPARSER_free_packet (packet);
369       return;
370     }
371   if ( (1 != packet->num_queries) )
372     {
373       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374                   _("Received unsupported DNS request from %s\n"),
375                   GNUNET_a2s (addr, addr_len));
376       GNUNET_DNSPARSER_free_packet (packet);
377       return;
378     }
379   request = GNUNET_malloc (sizeof (struct Request) + addr_len);
380   request->lsock = lsock;
381   request->packet = packet;
382   request->addr = &request[1];
383   request->addr_len = addr_len;
384   memcpy (&request[1], addr, addr_len);
385   request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
386                                                         &do_timeout,
387                                                         request);
388   name = GNUNET_strdup (packet->queries[0].name);
389   name_len = strlen (name);
390   use_gns = GNUNET_NO;
391   if ( (name_len > strlen (dns_suffix)) &&
392        (0 == strcasecmp (dns_suffix,
393                          &name[name_len - strlen (dns_suffix)])) )
394     {
395       if (0 == strcasecmp (fcfs_suffix,
396                            &name[name_len - strlen (fcfs_suffix)]))
397       {
398         name[name_len - strlen (dns_suffix) + 1] = '\0';
399         strcat (name, GNUNET_GNS_TLD);
400       }
401       else
402       {
403         name[name_len - strlen (dns_suffix) + 1] = '\0';
404         strcat (name, GNUNET_GNS_TLD_ZKEY);
405       }
406       name_len = strlen (name);
407     }
408   if ( (name_len > strlen ((GNUNET_GNS_TLD) + 1)) &&
409        (0 == strcasecmp (GNUNET_GNS_TLD,
410                          &name[name_len - strlen (GNUNET_GNS_TLD)])) )
411     use_gns = GNUNET_YES;
412
413   if ( (name_len > strlen (GNUNET_GNS_TLD_ZKEY)) &&
414        (0 == strcasecmp (GNUNET_GNS_TLD_ZKEY,
415                          &name[name_len - strlen (GNUNET_GNS_TLD_ZKEY)])) )
416     use_gns = GNUNET_YES;
417
418   if (GNUNET_YES == use_gns)
419   {
420       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421                    "Calling GNS\n");
422       type = packet->queries[0].type;
423       request->lookup = GNUNET_GNS_lookup (gns,
424                                            name,
425                                            type,
426                                            GNUNET_NO,
427                                            NULL,
428                                            &result_processor,
429                                            request);
430     }
431   else
432     {
433       /* FIXME: do traditional *DNS* lookup; note that
434          gnunet-service-dns already has code to do this;
435          factor into library to share! Why not use GNUNET_RESOLVER here?*/
436       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437                    "Calling DNS at %s\n", dns_ip);
438       GNUNET_DNSPARSER_free_packet (request->packet);
439       request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
440                                                      udp_msg,
441                                                      udp_msg_size,
442                                                      &dns_result_processor,
443                                                      request);
444
445
446
447     }
448   GNUNET_free (name);
449 }
450
451
452 /**
453  * Task to read IPv4 DNS packets.
454  *
455  * @param cls the 'listen_socket4'
456  * @param tc scheduler context
457  */ 
458 static void
459 read_dns4 (void *cls,
460            const struct GNUNET_SCHEDULER_TaskContext *tc)
461 {
462   struct sockaddr_in v4;
463   socklen_t addrlen;
464   ssize_t size;
465
466   GNUNET_assert (listen_socket4 == cls);
467   t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
468                                       listen_socket4,
469                                       &read_dns4,
470                                       listen_socket4);
471   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
472     return; /* shutdown? */
473   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
474   if (0 > size)
475     {
476       GNUNET_break (0);
477       return; /* read error!? */
478     }
479   {
480     char buf[size];
481     
482     addrlen = sizeof (v4);
483     GNUNET_break (size == 
484                   GNUNET_NETWORK_socket_recvfrom (listen_socket4,
485                                                   buf,
486                                                   size,
487                                                   (struct sockaddr *) &v4,
488                                                   &addrlen));
489     handle_request (listen_socket4, &v4, addrlen,
490                     buf, size);
491   }
492 }
493
494
495 /**
496  * Task to read IPv6 DNS packets.
497  *
498  * @param cls the 'listen_socket6'
499  * @param tc scheduler context
500  */ 
501 static void
502 read_dns6 (void *cls,
503            const struct GNUNET_SCHEDULER_TaskContext *tc)
504 {
505   struct sockaddr_in6 v6;
506   socklen_t addrlen;
507   ssize_t size;
508
509   GNUNET_assert (listen_socket6 == cls);
510   t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
511                                       listen_socket6,
512                                       &read_dns6,
513                                       listen_socket6);
514   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
515     return; /* shutdown? */
516   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
517   if (0 > size)
518     {
519       GNUNET_break (0);
520       return; /* read error!? */
521     }
522   {
523     char buf[size];
524     
525     addrlen = sizeof (v6);
526     GNUNET_break (size == 
527                   GNUNET_NETWORK_socket_recvfrom (listen_socket6,
528                                                   buf,
529                                                   size,
530                                                   (struct sockaddr *) &v6,
531                                                   &addrlen));
532     handle_request (listen_socket6, &v6, addrlen,
533                     buf, size);
534   }
535 }
536
537
538 /**
539  * Main function that will be run.
540  *
541  * @param cls closure
542  * @param args remaining command-line arguments
543  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
544  * @param cfg configuration
545  */
546 static void
547 run (void *cls, char *const *args, const char *cfgfile,
548      const struct GNUNET_CONFIGURATION_Handle *cfg)
549 {
550   if (NULL == dns_ip)
551   {
552     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
553                 "No DNS server specified!\n");
554     return;
555   }
556
557   if (NULL == dns_suffix)
558     dns_suffix = DNS_SUFFIX;
559
560   if (NULL == fcfs_suffix)
561     fcfs_suffix = FCFS_SUFFIX;
562
563   gns = GNUNET_GNS_connect (cfg);
564
565   dns_stub = GNUNET_DNSSTUB_start (dns_ip);
566
567   if (NULL == dns_stub)
568     return;
569
570   if (NULL == gns)
571     return;
572   listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
573                                                  SOCK_DGRAM, 
574                                                  IPPROTO_UDP);
575   if (NULL != listen_socket4)
576     {
577       struct sockaddr_in v4;
578
579       memset (&v4, 0, sizeof (v4));
580       v4.sin_family = AF_INET;
581 #if HAVE_SOCKADDR_IN_SIN_LEN
582       v4.sin_len = sizeof (v4);
583 #endif
584       v4.sin_port = htons (53);
585       if (GNUNET_OK !=
586           GNUNET_NETWORK_socket_bind (listen_socket4,
587                                       (struct sockaddr *) &v4,
588                                       sizeof (v4)))
589         {
590           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
591           GNUNET_NETWORK_socket_close (listen_socket4);
592           listen_socket4 = NULL;
593         }
594     }
595   listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
596                                                 SOCK_DGRAM, 
597                                                 IPPROTO_UDP);
598   if (NULL != listen_socket6)
599     {
600       struct sockaddr_in6 v6;
601
602       memset (&v6, 0, sizeof (v6));
603       v6.sin6_family = AF_INET6;
604 #if HAVE_SOCKADDR_IN_SIN_LEN
605       v6.sin6_len = sizeof (v6);
606 #endif
607       v6.sin6_port = htons (53);
608       if (GNUNET_OK !=
609           GNUNET_NETWORK_socket_bind (listen_socket6,
610                                       (struct sockaddr *) &v6,
611                                       sizeof (v6)))
612         {
613           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
614           GNUNET_NETWORK_socket_close (listen_socket6);
615           listen_socket6 = NULL;
616         }
617     }
618   if ( (NULL == listen_socket4) &&
619        (NULL == listen_socket6) )
620     return;
621   if (NULL != listen_socket4)
622     t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
623                                         listen_socket4,
624                                         &read_dns4,
625                                         listen_socket4);
626   if (NULL != listen_socket6)
627     t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
628                                         listen_socket6,
629                                         &read_dns6,
630                                         listen_socket6);
631
632   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
633                                 &do_shutdown, NULL);
634 }
635
636
637 /**
638  * The main function for the fcfs daemon.
639  *
640  * @param argc number of arguments from the command line
641  * @param argv command line arguments
642  * @return 0 ok, 1 on error
643  */
644 int
645 main (int argc, 
646       char *const *argv)
647 {
648   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
649     {'d', "dns", NULL,
650       gettext_noop ("IP of recursive dns resolver to use (required)"), 1,
651       &GNUNET_GETOPT_set_string, &dns_ip},
652     {'s', "suffix", NULL,
653       gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
654       &GNUNET_GETOPT_set_string, &dns_suffix},
655     {'f', "fcfs", NULL,
656       gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
657       &GNUNET_GETOPT_set_string, &fcfs_suffix},
658     GNUNET_GETOPT_OPTION_END
659   };
660   int ret;
661
662   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
663                                                  &argc, &argv))
664     return 2;
665   GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
666   ret =
667       (GNUNET_OK ==
668        GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
669                            _("GNUnet DNS-to-GNS proxy (a DNS server)"), 
670                            options,
671                            &run, NULL)) ? 0 : 1;
672
673   return ret;
674 }
675
676 /* end of gnunet-dns2gns.c */