4b5c66e576d174670fe6d5a4cfaa42e415da2787
[oweals/gnunet.git] / src / gns / gnunet-dns2gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012-2013 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_identity_service.h>
30 #include <gnunet_dnsstub_lib.h>
31 #include "gns.h"
32
33 /**
34  * Timeout for DNS requests.
35  */
36 #define TIMEOUT GNUNET_TIME_UNIT_MINUTES
37
38 /**
39  * Default suffix
40  */
41 #define DNS_SUFFIX ".zkey.eu"
42
43 /**
44  * FCFS suffix
45  */
46 #define FCFS_SUFFIX "fcfs.zkey.eu"
47
48 /**
49  * Data kept per request.
50  */
51 struct Request
52 {
53   /**
54    * Socket to use for sending the reply.
55    */
56   struct GNUNET_NETWORK_Handle *lsock;
57
58   /**
59    * Destination address to use.
60    */
61   const void *addr;
62
63   /**
64    * Initially, this is the DNS request, it will then be
65    * converted to the DNS response.
66    */
67   struct GNUNET_DNSPARSER_Packet *packet;
68   
69   /**
70    * Our GNS request handle.
71    */
72   struct GNUNET_GNS_LookupRequest *lookup;
73
74   /**
75    * Our DNS request handle
76    */
77   struct GNUNET_DNSSTUB_RequestSocket *dns_lookup;
78
79   /**
80    * Task run on timeout or shutdown to clean up without
81    * response.
82    */
83   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84
85   /**
86    * Number of bytes in 'addr'.
87    */ 
88   size_t addr_len;
89
90 };
91
92
93 /**
94  * Handle to GNS resolver.
95  */
96 struct GNUNET_GNS_Handle *gns;
97
98 /**
99  * Stub resolver
100  */
101 struct GNUNET_DNSSTUB_Context *dns_stub;
102
103 /**
104  * Listen socket for IPv4.
105  */
106 static struct GNUNET_NETWORK_Handle *listen_socket4;
107
108 /**
109  * Listen socket for IPv6.
110  */
111 static struct GNUNET_NETWORK_Handle *listen_socket6;
112
113 /**
114  * Task for IPv4 socket.
115  */
116 static GNUNET_SCHEDULER_TaskIdentifier t4;
117
118 /**
119  * Task for IPv6 socket.
120  */
121 static GNUNET_SCHEDULER_TaskIdentifier t6;
122
123 /**
124  * DNS suffix, suffix of this gateway in DNS; defaults to '.zkey.eu'
125  */
126 static char *dns_suffix;
127
128 /**
129  * FCFS suffix, suffix of FCFS-authority in DNS; defaults to 'fcfs.zkey.eu'.
130  */
131 static char *fcfs_suffix;
132
133 /**
134  * IP of DNS server
135  */
136 static char *dns_ip;
137
138 /**
139  * UDP Port we listen on for inbound DNS requests.
140  */
141 static unsigned int listen_port = 53;
142
143 /**
144  * Which GNS zone do we translate incoming DNS requests to?
145  */
146 static struct GNUNET_CRYPTO_EccPublicKey my_zone;
147
148 /**
149  * '-z' option with the main zone to use.
150  */
151 static char *gns_zone_str;
152
153 /**
154  * Configuration to use.
155  */
156 static const struct GNUNET_CONFIGURATION_Handle *cfg;
157
158 /**
159  * Connection to identity service.
160  */
161 static struct GNUNET_IDENTITY_Handle *identity;
162
163 /**
164  * Request for our ego.
165  */
166 static struct GNUNET_IDENTITY_Operation *id_op;
167
168
169
170 /**
171  * Task run on shutdown.  Cleans up everything.
172  *
173  * @param cls unused
174  * @param tc scheduler context
175  */
176 static void
177 do_shutdown (void *cls,
178              const struct GNUNET_SCHEDULER_TaskContext *tc)
179 {
180   if (GNUNET_SCHEDULER_NO_TASK != t4)
181     GNUNET_SCHEDULER_cancel (t4);
182   if (GNUNET_SCHEDULER_NO_TASK != t6)
183     GNUNET_SCHEDULER_cancel (t6);
184   if (NULL != listen_socket4)
185   {
186     GNUNET_NETWORK_socket_close (listen_socket4);
187     listen_socket4 = NULL;
188   }
189   if (NULL != listen_socket6)
190   {
191     GNUNET_NETWORK_socket_close (listen_socket6);
192     listen_socket6 = NULL;
193   }
194   GNUNET_GNS_disconnect (gns);
195   gns = NULL;
196   GNUNET_DNSSTUB_stop (dns_stub);
197   dns_stub = NULL;
198 }
199
200
201 /**
202  * Send the response for the given request and clean up.
203  *
204  * @param request context for the request.
205  */
206 static void
207 send_response (struct Request *request)
208 {
209   char *buf;
210   size_t size;
211   
212   if (GNUNET_SYSERR ==
213       GNUNET_DNSPARSER_pack (request->packet,
214                              UINT16_MAX /* is this not too much? */,
215                              &buf,
216                              &size))
217     {
218       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
219                   _("Failed to pack DNS response into UDP packet!\n"));
220     }
221   else
222     {
223       if (size !=
224           GNUNET_NETWORK_socket_sendto (request->lsock,
225                                         buf, size,
226                                         request->addr,
227                                         request->addr_len))
228         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
229       GNUNET_free (buf);
230     }
231   GNUNET_SCHEDULER_cancel (request->timeout_task);
232   GNUNET_DNSPARSER_free_packet (request->packet);
233   GNUNET_free (request);
234 }
235
236
237 /**
238  * Task run on timeout.  Cleans up request.
239  *
240  * @param cls 'struct Request' of the request to clean up
241  * @param tc scheduler context
242  */
243 static void
244 do_timeout (void *cls,
245             const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   struct Request *request = cls;
248
249   if (NULL != request->packet)
250     GNUNET_DNSPARSER_free_packet (request->packet);
251   if (NULL != request->lookup)
252     GNUNET_GNS_lookup_cancel (request->lookup);
253   if (NULL != request->dns_lookup)
254     GNUNET_DNSSTUB_resolve_cancel (request->dns_lookup);
255   GNUNET_free (request);
256 }
257
258
259 /**
260  * Iterator called on obtained result for a DNS lookup
261  *
262  * @param cls closure
263  * @param rs the request socket
264  * @param dns the DNS udp payload
265  * @param r size of the DNS payload
266  */
267 static void
268 dns_result_processor (void *cls,
269                       struct GNUNET_DNSSTUB_RequestSocket *rs,
270                       const struct GNUNET_TUN_DnsHeader *dns,
271                       size_t r)
272 {
273   struct Request *request = cls;
274
275   request->packet = GNUNET_DNSPARSER_parse ((char*)dns, r);
276   send_response (request);
277 }
278
279
280 /**
281  * Iterator called on obtained result for a GNS lookup.
282  *
283  * @param cls closure
284  * @param rd_count number of records in @a rd
285  * @param rd the records in reply
286  */
287 static void
288 result_processor (void *cls,
289                   uint32_t rd_count,
290                   const struct GNUNET_NAMESTORE_RecordData *rd)
291 {
292   struct Request *request = cls;
293   struct GNUNET_DNSPARSER_Packet *packet;
294   uint32_t i;
295   struct GNUNET_DNSPARSER_Record rec;
296
297   request->lookup = NULL;
298   packet = request->packet;
299   packet->flags.query_or_response = 1;
300   packet->flags.return_code = GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR;
301   packet->flags.checking_disabled = 0;
302   packet->flags.authenticated_data = 1;
303   packet->flags.zero = 0;
304   packet->flags.recursion_available = 1;
305   packet->flags.message_truncated = 0;
306   packet->flags.authoritative_answer = 0;
307   //packet->flags.opcode = GNUNET_TUN_DNS_OPCODE_STATUS; // ???
308   for (i=0;i<rd_count;i++)
309     {
310       // FIXME: do we need to hanlde #GNUNET_NAMESTORE_RF_SHADOW_RECORD
311       // here? Or should we do this in libgnunetgns?
312       rec.expiration_time.abs_value_us = rd[i].expiration_time;
313       switch (rd[i].record_type)
314         {
315         case GNUNET_DNSPARSER_TYPE_A:
316           GNUNET_assert (sizeof (struct in_addr) == rd[i].data_size);
317           rec.name = GNUNET_strdup (packet->queries[0].name);
318           rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
319           rec.type = GNUNET_DNSPARSER_TYPE_A;
320           rec.data.raw.data = GNUNET_new (struct in_addr);
321           memcpy (rec.data.raw.data,
322                   rd[i].data,
323                   rd[i].data_size);
324           rec.data.raw.data_len = sizeof (struct in_addr);
325           GNUNET_array_append (packet->answers,
326                                packet->num_answers,
327                                rec);
328           break;
329         case GNUNET_DNSPARSER_TYPE_AAAA:
330           GNUNET_assert (sizeof (struct in6_addr) == rd[i].data_size);
331           rec.name = GNUNET_strdup (packet->queries[0].name);
332           rec.data.raw.data = GNUNET_malloc (sizeof (struct in6_addr));
333           rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
334           rec.type = GNUNET_DNSPARSER_TYPE_AAAA;
335           memcpy (rec.data.raw.data,
336                   rd[i].data,
337                   rd[i].data_size);
338           rec.data.raw.data_len = sizeof (struct in6_addr);
339           GNUNET_array_append (packet->answers,
340                                packet->num_answers,
341                                rec);
342           break;
343         case GNUNET_DNSPARSER_TYPE_CNAME:
344           rec.name = GNUNET_strdup (packet->queries[0].name);
345           rec.data.hostname = strdup (rd[i].data);
346           rec.class = GNUNET_TUN_DNS_CLASS_INTERNET;
347           rec.type = GNUNET_DNSPARSER_TYPE_CNAME;
348           memcpy (rec.data.hostname,
349                   rd[i].data,
350                   rd[i].data_size);
351           GNUNET_array_append (packet->answers,
352                                packet->num_answers,
353                                rec);
354           break;
355         default:
356           /* skip */
357           break;
358         }
359     }
360   send_response (request);
361 }
362
363
364 /**
365  * Handle DNS request.
366  *
367  * @param lsock socket to use for sending the reply
368  * @param addr address to use for sending the reply
369  * @param addr_len number of bytes in @a addr
370  * @param udp_msg DNS request payload
371  * @param udp_msg_size number of bytes in @a udp_msg 
372  */
373 static void
374 handle_request (struct GNUNET_NETWORK_Handle *lsock,
375                 const void *addr,
376                 size_t addr_len,
377                 const char *udp_msg,
378                 size_t udp_msg_size)
379 {
380   struct Request *request;
381   struct GNUNET_DNSPARSER_Packet *packet;
382   char *name;
383   size_t name_len;
384   int type;
385   int use_gns;
386
387   packet = GNUNET_DNSPARSER_parse (udp_msg, udp_msg_size);
388   if (NULL == packet)
389     {
390       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
391                   _("Cannot parse DNS request from %s\n"),
392                   GNUNET_a2s (addr, addr_len));
393       return;
394     }
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396               "Received request for `%s' with flags %u, #answers %d, #auth %d, #additional %d\n",
397               packet->queries[0].name,
398               (unsigned int) packet->flags.query_or_response,
399               (int) packet->num_answers,
400               (int) packet->num_authority_records,
401               (int) packet->num_additional_records);
402   if ( (0 != packet->flags.query_or_response) || 
403        (0 != packet->num_answers) ||
404        (0 != packet->num_authority_records))
405     {
406       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
407                   _("Received malformed DNS request from %s\n"),
408                   GNUNET_a2s (addr, addr_len));
409       GNUNET_DNSPARSER_free_packet (packet);
410       return;
411     }
412   if ( (1 != packet->num_queries) )
413     {
414       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415                   _("Received unsupported DNS request from %s\n"),
416                   GNUNET_a2s (addr, addr_len));
417       GNUNET_DNSPARSER_free_packet (packet);
418       return;
419     }
420   request = GNUNET_malloc (sizeof (struct Request) + addr_len);
421   request->lsock = lsock;
422   request->packet = packet;
423   request->addr = &request[1];
424   request->addr_len = addr_len;
425   memcpy (&request[1], addr, addr_len);
426   request->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
427                                                         &do_timeout,
428                                                         request);
429   name = GNUNET_strdup (packet->queries[0].name);
430   name_len = strlen (name);
431   use_gns = GNUNET_NO;
432
433   
434   if ( (name_len > strlen (fcfs_suffix)) &&
435        (0 == strcasecmp (fcfs_suffix,
436                          &name[name_len - strlen (fcfs_suffix)])) )
437   {
438     /* replace ".fcfs.zkey.eu" with ".gnu" */
439     strcpy (&name[name_len - strlen (fcfs_suffix)],
440             ".gnu");
441     use_gns = GNUNET_YES;
442   } else if ( (name_len > strlen (dns_suffix)) &&
443        (0 == strcasecmp (dns_suffix,
444                          &name[name_len - strlen (dns_suffix)])) )
445   {
446     /* replace ".zkey.eu" with ".zkey" */
447     strcpy (&name[name_len - strlen (dns_suffix)],
448             ".zkey");
449     use_gns = GNUNET_YES;
450   } else if ( (name_len > strlen (".gnu")) &&
451        (0 == strcasecmp (".gnu",
452                          &name[name_len - strlen (".gnu")])) )
453   {
454     /* name is in GNS */
455     use_gns = GNUNET_YES;
456   }
457   if (GNUNET_YES == use_gns)
458   {
459     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460                 "Calling GNS on `%s'\n",
461                 name);
462     type = packet->queries[0].type;
463     request->lookup = GNUNET_GNS_lookup (gns,
464                                          name,
465                                          &my_zone,
466                                          type,
467                                          GNUNET_NO,
468                                          NULL /* no shorten */,
469                                          &result_processor,
470                                          request);
471   }
472   else
473   {
474     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475                 "Using DNS resolver IP `%s' to resolve `%s'\n", 
476                 dns_ip,
477                 name);
478     GNUNET_DNSPARSER_free_packet (request->packet);
479     request->packet = NULL;
480     request->dns_lookup = GNUNET_DNSSTUB_resolve2 (dns_stub,
481                                                    udp_msg,
482                                                    udp_msg_size,
483                                                    &dns_result_processor,
484                                                    request);
485   }
486   GNUNET_free (name);
487 }
488
489
490 /**
491  * Task to read IPv4 DNS packets.
492  *
493  * @param cls the 'listen_socket4'
494  * @param tc scheduler context
495  */ 
496 static void
497 read_dns4 (void *cls,
498            const struct GNUNET_SCHEDULER_TaskContext *tc)
499 {
500   struct sockaddr_in v4;
501   socklen_t addrlen;
502   ssize_t size;
503
504   GNUNET_assert (listen_socket4 == cls);
505   t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
506                                       listen_socket4,
507                                       &read_dns4,
508                                       listen_socket4);
509   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
510     return; /* shutdown? */
511   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket4);
512   if (0 > size)
513     {
514       GNUNET_break (0);
515       return; /* read error!? */
516     }
517   {
518     char buf[size];
519     
520     addrlen = sizeof (v4);
521     GNUNET_break (size == 
522                   GNUNET_NETWORK_socket_recvfrom (listen_socket4,
523                                                   buf,
524                                                   size,
525                                                   (struct sockaddr *) &v4,
526                                                   &addrlen));
527     handle_request (listen_socket4, &v4, addrlen,
528                     buf, size);
529   }
530 }
531
532
533 /**
534  * Task to read IPv6 DNS packets.
535  *
536  * @param cls the 'listen_socket6'
537  * @param tc scheduler context
538  */ 
539 static void
540 read_dns6 (void *cls,
541            const struct GNUNET_SCHEDULER_TaskContext *tc)
542 {
543   struct sockaddr_in6 v6;
544   socklen_t addrlen;
545   ssize_t size;
546
547   GNUNET_assert (listen_socket6 == cls);
548   t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
549                                       listen_socket6,
550                                       &read_dns6,
551                                       listen_socket6);
552   if (0 == (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason))
553     return; /* shutdown? */
554   size = GNUNET_NETWORK_socket_recvfrom_amount (listen_socket6);
555   if (0 > size)
556     {
557       GNUNET_break (0);
558       return; /* read error!? */
559     }
560   {
561     char buf[size];
562     
563     addrlen = sizeof (v6);
564     GNUNET_break (size == 
565                   GNUNET_NETWORK_socket_recvfrom (listen_socket6,
566                                                   buf,
567                                                   size,
568                                                   (struct sockaddr *) &v6,
569                                                   &addrlen));
570     handle_request (listen_socket6, &v6, addrlen,
571                     buf, size);
572   }
573 }
574
575
576 /**
577  * Start DNS daemon.
578  */
579 static void
580 run_dnsd ()
581 {
582   if (NULL == dns_suffix)
583     dns_suffix = DNS_SUFFIX;
584   if (NULL == fcfs_suffix)
585     fcfs_suffix = FCFS_SUFFIX;
586   if (NULL == (gns = GNUNET_GNS_connect (cfg)))
587     return;
588   if (NULL == (dns_stub = GNUNET_DNSSTUB_start (dns_ip)))
589   {
590     GNUNET_GNS_disconnect (gns);
591     gns = NULL;
592     return;
593   }
594   listen_socket4 = GNUNET_NETWORK_socket_create (PF_INET,
595                                                  SOCK_DGRAM, 
596                                                  IPPROTO_UDP);
597   if (NULL != listen_socket4)
598     {
599       struct sockaddr_in v4;
600
601       memset (&v4, 0, sizeof (v4));
602       v4.sin_family = AF_INET;
603 #if HAVE_SOCKADDR_IN_SIN_LEN
604       v4.sin_len = sizeof (v4);
605 #endif
606       v4.sin_port = htons (listen_port);
607       if (GNUNET_OK !=
608           GNUNET_NETWORK_socket_bind (listen_socket4,
609                                       (struct sockaddr *) &v4,
610                                       sizeof (v4),
611                                       0))
612         {
613           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
614           GNUNET_NETWORK_socket_close (listen_socket4);
615           listen_socket4 = NULL;
616         }
617     }
618   listen_socket6 = GNUNET_NETWORK_socket_create (PF_INET6,
619                                                 SOCK_DGRAM, 
620                                                 IPPROTO_UDP);
621   if (NULL != listen_socket6)
622     {
623       struct sockaddr_in6 v6;
624
625       memset (&v6, 0, sizeof (v6));
626       v6.sin6_family = AF_INET6;
627 #if HAVE_SOCKADDR_IN_SIN_LEN
628       v6.sin6_len = sizeof (v6);
629 #endif
630       v6.sin6_port = htons (listen_port);
631       if (GNUNET_OK !=
632           GNUNET_NETWORK_socket_bind (listen_socket6,
633                                       (struct sockaddr *) &v6,
634                                       sizeof (v6),
635                                       0))
636         {
637           GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
638           GNUNET_NETWORK_socket_close (listen_socket6);
639           listen_socket6 = NULL;
640         }
641     }
642   if ( (NULL == listen_socket4) &&
643        (NULL == listen_socket6) )
644     {
645       GNUNET_GNS_disconnect (gns);
646       gns = NULL;
647       GNUNET_DNSSTUB_stop (dns_stub);
648       dns_stub = NULL;
649       return;
650     }
651   if (NULL != listen_socket4)
652     t4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
653                                         listen_socket4,
654                                         &read_dns4,
655                                         listen_socket4);
656   if (NULL != listen_socket6)
657     t6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
658                                         listen_socket6,
659                                         &read_dns6,
660                                         listen_socket6);
661
662 }
663
664
665 /** 
666  * Method called to inform about the egos of this peer.
667  *
668  * When used with #GNUNET_IDENTITY_create or #GNUNET_IDENTITY_get,
669  * this function is only called ONCE, and 'NULL' being passed in
670  * @a ego does indicate an error (i.e. name is taken or no default
671  * value is known).  If @a ego is non-NULL and if '*ctx'
672  * is set in those callbacks, the value WILL be passed to a subsequent
673  * call to the identity callback of #GNUNET_IDENTITY_connect (if 
674  * that one was not NULL).
675  *
676  * @param cls closure, NULL
677  * @param ego ego handle
678  * @param ctx context for application to store data for this ego
679  *                 (during the lifetime of this process, initially NULL)
680  * @param name name assigned by the user for this ego,
681  *                   NULL if the user just deleted the ego and it
682  *                   must thus no longer be used
683  */
684 static void
685 identity_cb (void *cls,
686              struct GNUNET_IDENTITY_Ego *ego,
687              void **ctx,
688              const char *name)
689 {
690   id_op = NULL;
691   if (NULL == ego)
692   {
693     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
694                 _("No ego configured for `dns2gns` subsystem\n"));
695     return;
696   }
697   GNUNET_IDENTITY_ego_get_public_key (ego,
698                                       &my_zone);
699   run_dnsd ();
700 }
701
702
703 /**
704  * Main function that will be run.
705  *
706  * @param cls closure
707  * @param args remaining command-line arguments
708  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
709  * @param c configuration
710  */
711 static void
712 run (void *cls, char *const *args, const char *cfgfile,
713      const struct GNUNET_CONFIGURATION_Handle *c)
714 {
715   cfg = c;
716
717   if (NULL == dns_ip)
718   {
719     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
720                 _("No DNS server specified!\n"));
721     return;
722   }
723   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
724                                 &do_shutdown, NULL);
725   if (NULL == gns_zone_str)
726     {
727       identity = GNUNET_IDENTITY_connect (cfg,
728                                           NULL, NULL);
729       id_op = GNUNET_IDENTITY_get (identity,
730                                    "dns2gns",
731                                    &identity_cb,
732                                    NULL);
733       return;
734     }
735   if ( (NULL == gns_zone_str) ||
736        (GNUNET_OK !=
737         GNUNET_CRYPTO_ecc_public_key_from_string (gns_zone_str,
738                                                   strlen (gns_zone_str),
739                                                   &my_zone)) )
740   {
741     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
742                 _("No valid GNS zone specified!\n"));
743     GNUNET_SCHEDULER_shutdown ();
744     return;
745   }
746   run_dnsd ();
747 }
748
749
750 /**
751  * The main function for the dns2gns daemon.
752  *
753  * @param argc number of arguments from the command line
754  * @param argv command line arguments
755  * @return 0 ok, 1 on error
756  */
757 int
758 main (int argc, 
759       char *const *argv)
760 {
761   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
762     {'d', "dns", "IP",
763       gettext_noop ("IP of recursive DNS resolver to use (required)"), 1,
764       &GNUNET_GETOPT_set_string, &dns_ip},
765     {'f', "fcfs", "NAME",
766       gettext_noop ("Authoritative FCFS suffix to use (optional); default: fcfs.zkey.eu"), 1,
767       &GNUNET_GETOPT_set_string, &fcfs_suffix},
768     {'s', "suffix", "SUFFIX",
769       gettext_noop ("Authoritative DNS suffix to use (optional); default: zkey.eu"), 1,
770       &GNUNET_GETOPT_set_string, &dns_suffix},
771     {'p', "port", "UDPPORT",
772       gettext_noop ("UDP port to listen on for inbound DNS requests; default: 53"), 1,
773       &GNUNET_GETOPT_set_uint, &listen_port},
774     {'z', "zone", "PUBLICKEY",
775       gettext_noop ("Public key of the GNS zone to use (overrides default)"), 1,
776       &GNUNET_GETOPT_set_string, &gns_zone_str},
777     GNUNET_GETOPT_OPTION_END
778   };
779   int ret;
780
781   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
782                                                  &argc, &argv))
783     return 2;
784   GNUNET_log_setup ("gnunet-dns2gns", "WARNING", NULL);
785   ret =
786       (GNUNET_OK ==
787        GNUNET_PROGRAM_run (argc, argv, "gnunet-dns2gns",
788                            _("GNUnet DNS-to-GNS proxy (a DNS server)"), 
789                            options,
790                            &run, NULL)) ? 0 : 1;
791   GNUNET_free ((void*) argv);
792   return ret;
793 }
794
795 /* end of gnunet-dns2gns.c */