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