2de67020db39d96baa8aa37fb4fb454c5fda467b
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011-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 gns/gnunet-service-gns.c
22  * @brief GNUnet GNS service
23  * @author Martin Schanzenbach
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - perform conversion of relative expiration times to absolute time
28  *   when going to DHT!
29  */
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_namestore_service.h"
37 #include "gnunet_gns_service.h"
38 #include "gnunet_statistics_service.h"
39 #include "gns.h"
40 #include "gns_common.h"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet-service-gns_interceptor.h"
43 #include "gnunet_protocols.h"
44
45 /**
46  * The initial interval in milliseconds btween puts in
47  * a zone iteration
48  */
49 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
50
51 /**
52  * The upper bound for the zone iteration interval in milliseconds
53  */
54 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
55
56 /**
57  * The default put interval for the zone iteration. In case
58  * no option is found
59  */
60 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
61
62 /**
63  * The factor the current zone iteration interval is divided by for each
64  * additional new record
65  */
66 #define LATE_ITERATION_SPEEDUP_FACTOR 2
67
68 /**
69  * How long until a DHT PUT attempt should time out?
70  */
71 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
72
73 /**
74  * What replication level do we use for DHT PUT operations?
75  */
76 #define DHT_GNS_REPLICATION_LEVEL 5
77
78
79 /**
80  * Handle to a lookup operation from api
81  */
82 struct ClientLookupHandle
83 {
84
85   /**
86    * We keep these in a DLL.
87    */
88   struct ClientLookupHandle *next;
89
90   /**
91    * We keep these in a DLL.
92    */
93   struct ClientLookupHandle *prev;
94
95   /**
96    * Handle to the requesting client
97    */
98   struct GNUNET_SERVER_Client *client;
99
100   /**
101    * Active handle for the lookup.
102    */
103   struct GNS_ResolverHandle *lookup;
104
105   /**
106    * request id 
107    */
108   uint32_t request_id;
109
110 };
111
112
113 /**
114  * Our handle to the DHT
115  */
116 static struct GNUNET_DHT_Handle *dht_handle;
117
118 /**
119  * Active DHT put operation (or NULL)
120  */
121 static struct GNUNET_DHT_PutHandle *active_put;
122
123 /**
124  * Our handle to the namestore service
125  */
126 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
127
128 /**
129  * Handle to iterate over our authoritative zone in namestore
130  */
131 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
132
133 /**
134  * Our notification context.
135  */
136 static struct GNUNET_SERVER_NotificationContext *nc;
137
138 /**
139  * Head of the DLL.
140  */
141 static struct ClientLookupHandle *clh_head;
142
143 /**
144  * Tail of the DLL.
145  */
146 static struct ClientLookupHandle *clh_tail;
147
148 /**
149  * Useful for zone update for DHT put
150  */
151 static unsigned long long num_public_records;
152
153 /**
154  * Last seen record count
155  */
156 static unsigned long long last_num_public_records;
157
158 /**
159  * Zone iteration PUT interval.
160  */
161 static struct GNUNET_TIME_Relative put_interval;
162
163 /**
164  * Time window for zone iteration
165  */
166 static struct GNUNET_TIME_Relative zone_publish_time_window;
167
168 /**
169  * zone publish task
170  */
171 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
172
173 /**
174  * #GNUNET_YES if zone has never been published before
175  */
176 static int first_zone_iteration;
177
178 /**
179  * The lookup timeout
180  */
181 static struct GNUNET_TIME_Relative default_lookup_timeout;
182
183 /**
184  * #GNUNET_YES if ipv6 is supported
185  */
186 static int v6_enabled;
187
188 /**
189  * #GNUNET_YES if ipv4 is supported
190  */
191 static int v4_enabled;
192
193 /**
194  * Handle to the statistics service
195  */
196 static struct GNUNET_STATISTICS_Handle *statistics;
197
198
199 /**
200  * Task run during shutdown.
201  *
202  * @param cls unused
203  * @param tc unused
204  */
205 static void
206 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 {
208   struct ClientLookupHandle *clh;
209
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "Shutting down!\n");
212   GNUNET_SERVER_notification_context_destroy (nc);  
213   while (NULL != (clh = clh_head))
214   {
215     GNS_resolver_lookup_cancel (clh->lookup);
216     GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
217     GNUNET_free (clh);
218   }
219
220   GNS_interceptor_done ();
221   GNS_resolver_done ();
222   if (NULL != statistics)
223   {
224     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
225     statistics = NULL;
226   }
227   if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
228   {
229     GNUNET_SCHEDULER_cancel (zone_publish_task);
230     zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
231   }
232   if (NULL != namestore_iter)
233   {
234     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
235     namestore_iter = NULL;
236   }
237   if (NULL != namestore_handle)
238   {
239     GNUNET_NAMESTORE_disconnect (namestore_handle);
240     namestore_handle = NULL;
241   }
242   if (NULL != active_put)
243   {
244     GNUNET_DHT_put_cancel (active_put);
245     active_put = NULL;
246   }
247   if (NULL != dht_handle)
248   {
249     GNUNET_DHT_disconnect (dht_handle);
250     dht_handle = NULL;
251   }
252 }
253
254
255 /**
256  * Method called periodically that triggers iteration over authoritative records
257  *
258  * @param cls closure
259  * @param tc task context
260  */
261 static void
262 publish_zone_dht_next (void *cls,
263                        const struct GNUNET_SCHEDULER_TaskContext *tc)
264 {
265   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
266   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
267 }
268
269
270 /**
271  * Periodically iterate over our zone and store everything in dht
272  *
273  * @param cls NULL
274  * @param tc task context
275  */
276 static void
277 publish_zone_dht_start (void *cls, 
278                         const struct GNUNET_SCHEDULER_TaskContext *tc);
279
280
281 /**
282  * Continuation called from DHT once the PUT operation is done.
283  *
284  * @param cls closure, NULL
285  * @param success #GNUNET_OK on success
286  */
287 static void
288 dht_put_continuation (void *cls,
289                       int success)
290 {
291   struct GNUNET_TIME_Relative next_put_interval; 
292
293   num_public_records++;  
294   if ( (num_public_records > last_num_public_records) &&
295        (GNUNET_NO == first_zone_iteration) )
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298                 "Last record count was lower than current record count.  Reducing interval.\n");
299     put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
300                                                 num_public_records);
301     next_put_interval = GNUNET_TIME_relative_divide (put_interval,
302                                                      LATE_ITERATION_SPEEDUP_FACTOR);
303   }
304   else
305     next_put_interval = put_interval;
306
307   GNUNET_STATISTICS_set (statistics,
308                          "Current zone iteration interval (ms)",
309                          next_put_interval.rel_value_us / 1000LL,
310                          GNUNET_NO); 
311   zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
312                                                     &publish_zone_dht_next,
313                                                     NULL);
314 }
315
316
317 /**
318  * Function used to put all records successively into the DHT.
319  *
320  * @param cls the closure (NULL)
321  * @param key the private key of the authority (ours)
322  * @param name the name of the records, NULL once the iteration is done
323  * @param rd_count the number of records in @a rd
324  * @param rd the record data
325  */
326 static void
327 put_gns_record (void *cls,
328                 const struct GNUNET_CRYPTO_EccPrivateKey *key,
329                 const char *name,
330                 unsigned int rd_count,
331                 const struct GNUNET_NAMESTORE_RecordData *rd)
332 {  
333   struct GNUNET_NAMESTORE_Block *block;
334   struct GNUNET_HashCode query;
335   struct GNUNET_TIME_Absolute expire; 
336   size_t block_size;
337   struct GNUNET_NAMESTORE_RecordData rd_public[rd_count];
338   unsigned int rd_public_count;
339   unsigned int i;
340
341   if (NULL == name)
342   {
343     /* we're done with one iteration, calculate when to do the next one */
344     namestore_iter = NULL;
345     last_num_public_records = num_public_records;
346     first_zone_iteration = GNUNET_NO;
347     if (0 == num_public_records)
348     {
349       /**
350        * If no records are known (startup) or none present
351        * we can safely set the interval to the value for a single
352        * record
353        */
354       put_interval = zone_publish_time_window;
355       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
356                   "No records in namestore database.\n");
357     }
358     else
359     {
360       put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
361                                                   num_public_records);
362     }
363     put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
364                                              put_interval);
365
366     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367                 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
368                 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
369     GNUNET_STATISTICS_set (statistics,
370                            "Current zone iteration interval (in ms)",
371                            put_interval.rel_value_us / 1000LL,
372                            GNUNET_NO);
373     GNUNET_STATISTICS_update (statistics,
374                               "Number of zone iterations", 
375                               1, 
376                               GNUNET_NO);
377     GNUNET_STATISTICS_set (statistics,
378                            "Number of public records in DHT",
379                            last_num_public_records,
380                            GNUNET_NO);
381     if (0 == num_public_records)
382       zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
383                                                         &publish_zone_dht_start,
384                                                         NULL);
385     else
386       zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
387                                                     NULL);
388     return;
389   }
390
391   /* filter out records that are not public! */
392   rd_public_count = 0;
393   for (i=0;i<rd_count;i++)
394     if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
395                              GNUNET_NAMESTORE_RF_PENDING)))
396       rd_public[rd_public_count++] = rd[i];
397
398
399   /* We got a set of records to publish */
400   if (0 == rd_public_count)
401   {
402     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
403                                                    NULL);
404     return;
405   }
406   expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
407                                                         rd_public);
408   block = GNUNET_NAMESTORE_block_create (key,
409                                          expire,
410                                          name,
411                                          rd_public,
412                                          rd_public_count);
413   block_size = ntohl (block->purpose.size) 
414     + sizeof (struct GNUNET_CRYPTO_EccSignature) 
415     + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
416   GNUNET_NAMESTORE_query_from_private_key (key,
417                                            name,
418                                            &query);
419
420   active_put = GNUNET_DHT_put (dht_handle, &query,
421                                DHT_GNS_REPLICATION_LEVEL,
422                                GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
423                                GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
424                                block_size,
425                                block,
426                                expire,
427                                DHT_OPERATION_TIMEOUT,
428                                &dht_put_continuation,
429                                NULL); 
430   if (NULL == active_put)
431   {
432     GNUNET_break (0);
433     dht_put_continuation (NULL, GNUNET_NO);
434   }
435   GNUNET_free (block);
436 }
437
438
439 /**
440  * Periodically iterate over our zone and store everything in dht
441  *
442  * @param cls NULL
443  * @param tc task context
444  */
445 static void
446 publish_zone_dht_start (void *cls, 
447                         const struct GNUNET_SCHEDULER_TaskContext *tc)
448 {
449   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
450
451   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
452               "Scheduling DHT zone update!\n");  
453   /* start counting again */
454   num_public_records = 0;
455   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
456                                                           NULL, /* All zones */
457                                                           &put_gns_record,
458                                                           NULL);
459 }
460
461
462 /* END DHT ZONE PROPAGATION */
463
464
465 /**
466  * Reply to client with the result from our lookup.
467  *
468  * @param cls the closure (our client lookup handle)
469  * @param rd_count the number of records
470  * @param rd the record data
471  */
472 static void
473 send_lookup_response (void* cls,
474                       uint32_t rd_count,
475                       const struct GNUNET_NAMESTORE_RecordData *rd)
476 {
477   struct ClientLookupHandle *clh = cls;
478   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
479   size_t len;
480   
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
482               "Sending `%s' message with %d results\n",
483               "LOOKUP_RESULT", 
484               rd_count);
485   
486   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
487   rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage)); 
488   rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
489   rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
490   rmsg->id = clh->request_id;
491   rmsg->rd_count = htonl (rd_count); 
492   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, 
493                                       (char*) &rmsg[1]);
494   GNUNET_SERVER_notification_context_unicast (nc, 
495                                               clh->client,
496                                               &rmsg->header,
497                                               GNUNET_NO);
498   GNUNET_free (rmsg);
499   GNUNET_SERVER_receive_done (clh->client, 
500                               GNUNET_OK); 
501   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
502   GNUNET_free (clh);
503   GNUNET_STATISTICS_update (statistics,
504                             "Completed lookups", 1, 
505                             GNUNET_NO);
506   GNUNET_STATISTICS_update (statistics,
507                             "Records resolved", 
508                             rd_count, 
509                             GNUNET_NO);
510 }
511
512
513 /**
514  * Handle lookup requests from client
515  *
516  * @param cls the closure
517  * @param client the client
518  * @param message the message
519  */
520 static void
521 handle_lookup (void *cls,
522                struct GNUNET_SERVER_Client *client,
523                const struct GNUNET_MessageHeader *message)
524 {
525   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
526   struct ClientLookupHandle *clh;
527   char *nameptr = name;
528   const char *utf_in;
529   const struct GNUNET_CRYPTO_EccPrivateKey *key;
530   uint16_t msg_size;
531   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
532   
533   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
534               "Received `%s' message\n", 
535               "LOOKUP");
536   msg_size = ntohs (message->size);
537   if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
538   {
539     GNUNET_break (0);
540     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
541     return;
542   }
543   sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
544   GNUNET_SERVER_notification_context_add (nc, client);
545   if (GNUNET_YES == ntohl (sh_msg->have_key))
546     key = &sh_msg->shorten_key;
547   else
548     key = NULL;
549   utf_in = (const char *) &sh_msg[1];
550   if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
551        (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
552   {
553     GNUNET_break (0);
554     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
555     return;
556   }
557   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
558   
559   clh = GNUNET_new (struct ClientLookupHandle);
560   GNUNET_SERVER_client_set_user_context (client, clh);
561   GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
562   clh->client = client;
563   clh->request_id = sh_msg->id;
564   if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
565        (GNUNET_OK != v4_enabled) )
566   {
567     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568                 "LOOKUP: Query for A record but AF_INET not supported!");
569     send_lookup_response (clh, 0, NULL);
570     return;
571   }  
572   if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
573        (GNUNET_OK != v6_enabled) )
574   {
575     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576                 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
577     send_lookup_response (clh, 0, NULL);
578     return;
579   }
580   clh->lookup = GNS_resolver_lookup (&sh_msg->zone, 
581                                      ntohl (sh_msg->type),
582                                      name,
583                                      key,
584                                      ntohl (sh_msg->only_cached),
585                                      &send_lookup_response, clh);
586   GNUNET_STATISTICS_update (statistics,
587                             "Lookup attempts", 
588                             1, GNUNET_NO);
589 }
590
591
592 /**
593  * One of our clients disconnected, clean up after it.
594  *
595  * @param cls NULL
596  * @param client the client that disconnected
597  */
598 static void
599 notify_client_disconnect (void *cls,
600                           struct GNUNET_SERVER_Client *client)
601 {
602   struct ClientLookupHandle *clh;
603
604   if (NULL == client)
605     return;
606   clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
607   if (NULL == clh)
608     return;
609   GNS_resolver_lookup_cancel (clh->lookup);
610   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
611   GNUNET_free (clh);
612 }
613
614
615 /**
616  * Process GNS requests.
617  *
618  * @param cls closure
619  * @param server the initialized server
620  * @param c configuration to use
621  */
622 static void
623 run (void *cls, struct GNUNET_SERVER_Handle *server,
624      const struct GNUNET_CONFIGURATION_Handle *c)
625 {
626   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
627     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
628     {NULL, NULL, 0, 0}
629   };
630   struct GNUNET_CRYPTO_EccPublicKey dns_root;
631   unsigned long long max_parallel_bg_queries = 0;
632   char *dns_root_name;
633
634   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
635   v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
636
637   namestore_handle = GNUNET_NAMESTORE_connect (c);
638   if (NULL == namestore_handle)
639   {
640     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
641                 _("Failed to connect to the namestore!\n"));
642     GNUNET_SCHEDULER_shutdown ();
643     return;
644   }
645   
646   put_interval = INITIAL_PUT_INTERVAL;
647   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
648
649   if (GNUNET_OK ==
650       GNUNET_CONFIGURATION_get_value_time (c, "gns",
651                                            "ZONE_PUBLISH_TIME_WINDOW",
652                                            &zone_publish_time_window))
653   {
654     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655                 "Time window for zone iteration: %s\n",
656                 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
657   }
658   if (GNUNET_OK ==
659       GNUNET_CONFIGURATION_get_value_number (c, "gns",
660                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
661                                             &max_parallel_bg_queries))
662   {
663     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664                 "Number of allowed parallel background queries: %llu\n",
665                 max_parallel_bg_queries);
666   }
667
668   if (GNUNET_OK ==
669       GNUNET_CONFIGURATION_get_value_time (c, "gns",
670                                            "DEFAULT_LOOKUP_TIMEOUT",
671                                            &default_lookup_timeout))
672   {
673     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674                 "Default lookup timeout: %s\n",
675                 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
676                                                         GNUNET_YES));
677   }
678   
679   dht_handle = GNUNET_DHT_connect (c,
680                                    (unsigned int) max_parallel_bg_queries);
681   if (NULL == dht_handle)
682   {
683     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684                 _("Could not connect to DHT!\n"));
685     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
686     return;
687   }
688   
689   if (GNUNET_OK ==
690       GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
691                                              &dns_root_name))
692   {
693     if (GNUNET_OK !=
694         GNUNET_CRYPTO_ecc_public_key_from_string (dns_root_name,
695                                                   strlen (dns_root_name),
696                                                   &dns_root))
697     {
698       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
699                                  "gns", "DNS_ROOT", 
700                                  _("valid public key required"));
701       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
702       GNUNET_free (dns_root_name);
703       return;
704     }
705     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
706                 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
707                 dns_root_name);
708     GNUNET_free (dns_root_name);    
709     if (GNUNET_SYSERR ==
710         GNS_interceptor_init (&dns_root, c))
711     {
712       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
713       return;
714     }
715   }
716   /* FIXME: install client disconnect handle to clean up pending
717      lookups on client disconnect! */
718   GNS_resolver_init (namestore_handle, dht_handle, 
719                      c,
720                      max_parallel_bg_queries);
721   GNUNET_SERVER_disconnect_notify (server,
722                                    &notify_client_disconnect,
723                                    NULL);
724   /* Schedule periodic put for our records. */    
725   first_zone_iteration = GNUNET_YES;
726   GNUNET_SERVER_add_handlers (server, handlers);
727   statistics = GNUNET_STATISTICS_create ("gns", c);
728   nc = GNUNET_SERVER_notification_context_create (server, 1);
729   zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
730                                                 NULL);
731   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 
732                                 &shutdown_task, NULL);
733 }
734
735
736 /**
737  * The main function for the GNS service.
738  *
739  * @param argc number of arguments from the command line
740  * @param argv command line arguments
741  * @return 0 ok, 1 on error
742  */
743 int
744 main (int argc, char *const *argv)
745 {
746   int ret;
747
748   ret =
749       (GNUNET_OK ==
750        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
751                            NULL)) ? 0 : 1;
752   return ret;
753 }
754
755 /* end of gnunet-service-gns.c */