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