092bba7055f10230370360969b357593064ad4bd
[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 GNU Name System (main service)
23  * @author Martin Schanzenbach
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dns_service.h"
29 #include "gnunet_dnsparser_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_gns_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gns.h"
35 #include "gnunet-service-gns_resolver.h"
36 #include "gnunet-service-gns_shorten.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     GNUNET_SERVER_client_set_user_context (clh->client, NULL);
211     GNS_resolver_lookup_cancel (clh->lookup);
212     GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
213     GNUNET_free (clh);
214   }
215
216   GNS_interceptor_done ();
217   GNS_resolver_done ();
218   GNS_shorten_done ();
219   if (NULL != statistics)
220   {
221     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
222     statistics = NULL;
223   }
224   if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
225   {
226     GNUNET_SCHEDULER_cancel (zone_publish_task);
227     zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
228   }
229   if (NULL != namestore_iter)
230   {
231     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
232     namestore_iter = NULL;
233   }
234   if (NULL != namestore_handle)
235   {
236     GNUNET_NAMESTORE_disconnect (namestore_handle);
237     namestore_handle = NULL;
238   }
239   if (NULL != active_put)
240   {
241     GNUNET_DHT_put_cancel (active_put);
242     active_put = NULL;
243   }
244   if (NULL != dht_handle)
245   {
246     GNUNET_DHT_disconnect (dht_handle);
247     dht_handle = NULL;
248   }
249 }
250
251
252 /**
253  * Method called periodically that triggers iteration over authoritative records
254  *
255  * @param cls closure
256  * @param tc task context
257  */
258 static void
259 publish_zone_dht_next (void *cls,
260                        const struct GNUNET_SCHEDULER_TaskContext *tc)
261 {
262   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
263   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
264 }
265
266
267 /**
268  * Periodically iterate over our zone and store everything in dht
269  *
270  * @param cls NULL
271  * @param tc task context
272  */
273 static void
274 publish_zone_dht_start (void *cls, 
275                         const struct GNUNET_SCHEDULER_TaskContext *tc);
276
277
278 /**
279  * Continuation called from DHT once the PUT operation is done.
280  *
281  * @param cls closure, NULL
282  * @param success #GNUNET_OK on success
283  */
284 static void
285 dht_put_continuation (void *cls,
286                       int success)
287 {
288   struct GNUNET_TIME_Relative next_put_interval; 
289
290   active_put = NULL;
291   num_public_records++;  
292   if ( (num_public_records > last_num_public_records) &&
293        (GNUNET_NO == first_zone_iteration) )
294   {
295     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296                 "Last record count was lower than current record count.  Reducing interval.\n");
297     put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
298                                                 num_public_records);
299     next_put_interval = GNUNET_TIME_relative_divide (put_interval,
300                                                      LATE_ITERATION_SPEEDUP_FACTOR);
301   }
302   else
303     next_put_interval = put_interval;
304
305   GNUNET_STATISTICS_set (statistics,
306                          "Current zone iteration interval (ms)",
307                          next_put_interval.rel_value_us / 1000LL,
308                          GNUNET_NO); 
309   zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
310                                                     &publish_zone_dht_next,
311                                                     NULL);
312 }
313
314
315 /**
316  * Function used to put all records successively into the DHT.
317  *
318  * @param cls the closure (NULL)
319  * @param key the private key of the authority (ours)
320  * @param name the name of the records, NULL once the iteration is done
321  * @param rd_count the number of records in @a rd
322  * @param rd the record data
323  */
324 static void
325 put_gns_record (void *cls,
326                 const struct GNUNET_CRYPTO_EccPrivateKey *key,
327                 const char *name,
328                 unsigned int rd_count,
329                 const struct GNUNET_NAMESTORE_RecordData *rd)
330 {  
331   struct GNUNET_NAMESTORE_Block *block;
332   struct GNUNET_HashCode query;
333   struct GNUNET_TIME_Absolute expire; 
334   struct GNUNET_TIME_Absolute now;
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, and convert to
391      absolute expiration time. */
392   rd_public_count = 0;
393   now = GNUNET_TIME_absolute_get ();
394   for (i=0;i<rd_count;i++)
395     if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
396                              GNUNET_NAMESTORE_RF_PENDING)))
397     {
398       rd_public[rd_public_count] = rd[i];
399       if (0 != (rd[i].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
400       {
401         rd_public[rd_public_count].flags &= ~GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION;
402         rd_public[rd_public_count].expiration_time += now.abs_value_us;
403       }
404       rd_public_count++;
405     }
406
407   /* We got a set of records to publish */
408   if (0 == rd_public_count)
409   {
410     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
411                                                    NULL);
412     return;
413   }
414   expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
415                                                         rd_public);
416   block = GNUNET_NAMESTORE_block_create (key,
417                                          expire,
418                                          name,
419                                          rd_public,
420                                          rd_public_count);
421   block_size = ntohl (block->purpose.size) 
422     + sizeof (struct GNUNET_CRYPTO_EccSignature) 
423     + sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
424   GNUNET_NAMESTORE_query_from_private_key (key,
425                                            name,
426                                            &query);
427
428   active_put = GNUNET_DHT_put (dht_handle, &query,
429                                DHT_GNS_REPLICATION_LEVEL,
430                                GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
431                                GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
432                                block_size,
433                                block,
434                                expire,
435                                DHT_OPERATION_TIMEOUT,
436                                &dht_put_continuation,
437                                NULL); 
438   if (NULL == active_put)
439   {
440     GNUNET_break (0);
441     dht_put_continuation (NULL, GNUNET_NO);
442   }
443   GNUNET_free (block);
444 }
445
446
447 /**
448  * Periodically iterate over our zone and store everything in dht
449  *
450  * @param cls NULL
451  * @param tc task context
452  */
453 static void
454 publish_zone_dht_start (void *cls, 
455                         const struct GNUNET_SCHEDULER_TaskContext *tc)
456 {
457   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
458
459   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
460               "Scheduling DHT zone update!\n");  
461   /* start counting again */
462   num_public_records = 0;
463   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
464                                                           NULL, /* All zones */
465                                                           &put_gns_record,
466                                                           NULL);
467 }
468
469
470 /* END DHT ZONE PROPAGATION */
471
472
473 /**
474  * Reply to client with the result from our lookup.
475  *
476  * @param cls the closure (our client lookup handle)
477  * @param rd_count the number of records in @a rd
478  * @param rd the record data
479  */
480 static void
481 send_lookup_response (void* cls,
482                       uint32_t rd_count,
483                       const struct GNUNET_NAMESTORE_RecordData *rd)
484 {
485   struct ClientLookupHandle *clh = cls;
486   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
487   size_t len;
488   
489   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
490               "Sending `%s' message with %d results\n",
491               "LOOKUP_RESULT", 
492               rd_count);
493   
494   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
495   rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage)); 
496   rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
497   rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
498   rmsg->id = clh->request_id;
499   rmsg->rd_count = htonl (rd_count); 
500   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, 
501                                       (char*) &rmsg[1]);
502   GNUNET_SERVER_notification_context_unicast (nc, 
503                                               clh->client,
504                                               &rmsg->header,
505                                               GNUNET_NO);
506   GNUNET_free (rmsg);
507   GNUNET_SERVER_receive_done (clh->client, 
508                               GNUNET_OK); 
509   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
510   GNUNET_SERVER_client_set_user_context (clh->client, NULL);
511   GNUNET_free (clh);
512   GNUNET_STATISTICS_update (statistics,
513                             "Completed lookups", 1, 
514                             GNUNET_NO);
515   GNUNET_STATISTICS_update (statistics,
516                             "Records resolved", 
517                             rd_count, 
518                             GNUNET_NO);
519 }
520
521
522 /**
523  * Handle lookup requests from client
524  *
525  * @param cls the closure
526  * @param client the client
527  * @param message the message
528  */
529 static void
530 handle_lookup (void *cls,
531                struct GNUNET_SERVER_Client *client,
532                const struct GNUNET_MessageHeader *message)
533 {
534   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
535   struct ClientLookupHandle *clh;
536   char *nameptr = name;
537   const char *utf_in;
538   const struct GNUNET_CRYPTO_EccPrivateKey *key;
539   uint16_t msg_size;
540   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
541   
542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
543               "Received `%s' message\n", 
544               "LOOKUP");
545   msg_size = ntohs (message->size);
546   if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
547   {
548     GNUNET_break (0);
549     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550     return;
551   }
552   sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
553   GNUNET_SERVER_notification_context_add (nc, client);
554   if (GNUNET_YES == ntohl (sh_msg->have_key))
555     key = &sh_msg->shorten_key;
556   else
557     key = NULL;
558   utf_in = (const char *) &sh_msg[1];
559   if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
560        (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
561   {
562     GNUNET_break (0);
563     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
564     return;
565   }
566   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
567   
568   clh = GNUNET_new (struct ClientLookupHandle);
569   GNUNET_SERVER_client_set_user_context (client, clh);
570   GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
571   clh->client = client;
572   clh->request_id = sh_msg->id;
573   if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
574        (GNUNET_OK != v4_enabled) )
575   {
576     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577                 "LOOKUP: Query for A record but AF_INET not supported!");
578     send_lookup_response (clh, 0, NULL);
579     return;
580   }  
581   if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
582        (GNUNET_OK != v6_enabled) )
583   {
584     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585                 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
586     send_lookup_response (clh, 0, NULL);
587     return;
588   }
589   clh->lookup = GNS_resolver_lookup (&sh_msg->zone, 
590                                      ntohl (sh_msg->type),
591                                      name,
592                                      key,
593                                      ntohl (sh_msg->only_cached),
594                                      &send_lookup_response, clh);
595   GNUNET_STATISTICS_update (statistics,
596                             "Lookup attempts", 
597                             1, GNUNET_NO);
598 }
599
600
601 /**
602  * One of our clients disconnected, clean up after it.
603  *
604  * @param cls NULL
605  * @param client the client that disconnected
606  */
607 static void
608 notify_client_disconnect (void *cls,
609                           struct GNUNET_SERVER_Client *client)
610 {
611   struct ClientLookupHandle *clh;
612
613   if (NULL == client)
614     return;
615   clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
616   if (NULL == clh)
617     return;
618   GNS_resolver_lookup_cancel (clh->lookup);
619   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
620   GNUNET_free (clh);
621 }
622
623
624 /**
625  * Process GNS requests.
626  *
627  * @param cls closure
628  * @param server the initialized server
629  * @param c configuration to use
630  */
631 static void
632 run (void *cls, struct GNUNET_SERVER_Handle *server,
633      const struct GNUNET_CONFIGURATION_Handle *c)
634 {
635   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
636     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
637     {NULL, NULL, 0, 0}
638   };
639   struct GNUNET_CRYPTO_EccPublicSignKey dns_root;
640   unsigned long long max_parallel_bg_queries = 0;
641   char *dns_root_name;
642
643   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
644   v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
645
646   namestore_handle = GNUNET_NAMESTORE_connect (c);
647   if (NULL == namestore_handle)
648   {
649     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
650                 _("Failed to connect to the namestore!\n"));
651     GNUNET_SCHEDULER_shutdown ();
652     return;
653   }
654   
655   put_interval = INITIAL_PUT_INTERVAL;
656   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
657
658   if (GNUNET_OK ==
659       GNUNET_CONFIGURATION_get_value_time (c, "gns",
660                                            "ZONE_PUBLISH_TIME_WINDOW",
661                                            &zone_publish_time_window))
662   {
663     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664                 "Time window for zone iteration: %s\n",
665                 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
666   }
667   if (GNUNET_OK ==
668       GNUNET_CONFIGURATION_get_value_number (c, "gns",
669                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
670                                             &max_parallel_bg_queries))
671   {
672     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673                 "Number of allowed parallel background queries: %llu\n",
674                 max_parallel_bg_queries);
675   }
676
677   if (GNUNET_OK ==
678       GNUNET_CONFIGURATION_get_value_time (c, "gns",
679                                            "DEFAULT_LOOKUP_TIMEOUT",
680                                            &default_lookup_timeout))
681   {
682     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683                 "Default lookup timeout: %s\n",
684                 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
685                                                         GNUNET_YES));
686   }
687   
688   dht_handle = GNUNET_DHT_connect (c,
689                                    (unsigned int) max_parallel_bg_queries);
690   if (NULL == dht_handle)
691   {
692     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
693                 _("Could not connect to DHT!\n"));
694     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
695     return;
696   }
697   
698   if (GNUNET_OK ==
699       GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
700                                              &dns_root_name))
701   {
702     if (GNUNET_OK !=
703         GNUNET_CRYPTO_ecc_public_sign_key_from_string (dns_root_name,
704                                                   strlen (dns_root_name),
705                                                   &dns_root))
706     {
707       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
708                                  "gns", "DNS_ROOT", 
709                                  _("valid public key required"));
710       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
711       GNUNET_free (dns_root_name);
712       return;
713     }
714     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
715                 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
716                 dns_root_name);
717     GNUNET_free (dns_root_name);    
718     if (GNUNET_SYSERR ==
719         GNS_interceptor_init (&dns_root, c))
720     {
721       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
722       return;
723     }
724   }
725   GNS_resolver_init (namestore_handle, dht_handle, 
726                      c,
727                      max_parallel_bg_queries);
728   GNS_shorten_init (namestore_handle, dht_handle);
729   GNUNET_SERVER_disconnect_notify (server,
730                                    &notify_client_disconnect,
731                                    NULL);
732   /* Schedule periodic put for our records. */    
733   first_zone_iteration = GNUNET_YES;
734   GNUNET_SERVER_add_handlers (server, handlers);
735   statistics = GNUNET_STATISTICS_create ("gns", c);
736   nc = GNUNET_SERVER_notification_context_create (server, 1);
737   zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
738                                                 NULL);
739   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 
740                                 &shutdown_task, NULL);
741 }
742
743
744 /**
745  * The main function for the GNS service.
746  *
747  * @param argc number of arguments from the command line
748  * @param argv command line arguments
749  * @return 0 ok, 1 on error
750  */
751 int
752 main (int argc, char *const *argv)
753 {
754   int ret;
755
756   ret =
757       (GNUNET_OK ==
758        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
759                            NULL)) ? 0 : 1;
760   return ret;
761 }
762
763 /* end of gnunet-service-gns.c */