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