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