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