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