-fixing #2985
[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 "gns_common.h"
37 #include "gnunet-service-gns_resolver.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   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   size_t block_size;
333   struct GNUNET_NAMESTORE_RecordData rd_public[rd_count];
334   unsigned int rd_public_count;
335   unsigned int i;
336
337   if (NULL == name)
338   {
339     /* we're done with one iteration, calculate when to do the next one */
340     namestore_iter = NULL;
341     last_num_public_records = num_public_records;
342     first_zone_iteration = GNUNET_NO;
343     if (0 == num_public_records)
344     {
345       /**
346        * If no records are known (startup) or none present
347        * we can safely set the interval to the value for a single
348        * record
349        */
350       put_interval = zone_publish_time_window;
351       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
352                   "No records in namestore database.\n");
353     }
354     else
355     {
356       put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
357                                                   num_public_records);
358     }
359     put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
360                                              put_interval);
361
362     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363                 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
364                 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
365     GNUNET_STATISTICS_set (statistics,
366                            "Current zone iteration interval (in ms)",
367                            put_interval.rel_value_us / 1000LL,
368                            GNUNET_NO);
369     GNUNET_STATISTICS_update (statistics,
370                               "Number of zone iterations", 
371                               1, 
372                               GNUNET_NO);
373     GNUNET_STATISTICS_set (statistics,
374                            "Number of public records in DHT",
375                            last_num_public_records,
376                            GNUNET_NO);
377     if (0 == num_public_records)
378       zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
379                                                         &publish_zone_dht_start,
380                                                         NULL);
381     else
382       zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
383                                                     NULL);
384     return;
385   }
386
387   /* filter out records that are not public! */
388   rd_public_count = 0;
389   for (i=0;i<rd_count;i++)
390     if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
391                              GNUNET_NAMESTORE_RF_PENDING)))
392       rd_public[rd_public_count++] = rd[i];
393
394
395   /* We got a set of records to publish */
396   if (0 == rd_public_count)
397   {
398     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
399                                                    NULL);
400     return;
401   }
402   expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
403                                                         rd_public);
404   block = GNUNET_NAMESTORE_block_create (key,
405                                          expire,
406                                          name,
407                                          rd_public,
408                                          rd_public_count);
409   block_size = ntohl (block->purpose.size) 
410     + sizeof (struct GNUNET_CRYPTO_EccSignature) 
411     + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
412   GNUNET_NAMESTORE_query_from_private_key (key,
413                                            name,
414                                            &query);
415
416   active_put = GNUNET_DHT_put (dht_handle, &query,
417                                DHT_GNS_REPLICATION_LEVEL,
418                                GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
419                                GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
420                                block_size,
421                                block,
422                                expire,
423                                DHT_OPERATION_TIMEOUT,
424                                &dht_put_continuation,
425                                NULL); 
426   if (NULL == active_put)
427   {
428     GNUNET_break (0);
429     dht_put_continuation (NULL, GNUNET_NO);
430   }
431   GNUNET_free (block);
432 }
433
434
435 /**
436  * Periodically iterate over our zone and store everything in dht
437  *
438  * @param cls NULL
439  * @param tc task context
440  */
441 static void
442 publish_zone_dht_start (void *cls, 
443                         const struct GNUNET_SCHEDULER_TaskContext *tc)
444 {
445   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
446
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
448               "Scheduling DHT zone update!\n");  
449   /* start counting again */
450   num_public_records = 0;
451   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
452                                                           NULL, /* All zones */
453                                                           &put_gns_record,
454                                                           NULL);
455 }
456
457
458 /* END DHT ZONE PROPAGATION */
459
460
461 /**
462  * Reply to client with the result from our lookup.
463  *
464  * @param cls the closure (our client lookup handle)
465  * @param rd_count the number of records
466  * @param rd the record data
467  */
468 static void
469 send_lookup_response (void* cls,
470                       uint32_t rd_count,
471                       const struct GNUNET_NAMESTORE_RecordData *rd)
472 {
473   struct ClientLookupHandle *clh = cls;
474   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
475   size_t len;
476   
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
478               "Sending `%s' message with %d results\n",
479               "LOOKUP_RESULT", 
480               rd_count);
481   
482   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
483   rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage)); 
484   rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
485   rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
486   rmsg->id = clh->request_id;
487   rmsg->rd_count = htonl (rd_count); 
488   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, 
489                                       (char*) &rmsg[1]);
490   GNUNET_SERVER_notification_context_unicast (nc, 
491                                               clh->client,
492                                               &rmsg->header,
493                                               GNUNET_NO);
494   GNUNET_free (rmsg);
495   GNUNET_SERVER_receive_done (clh->client, 
496                               GNUNET_OK); 
497   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
498   GNUNET_free (clh);
499   GNUNET_STATISTICS_update (statistics,
500                             "Completed lookups", 1, 
501                             GNUNET_NO);
502   GNUNET_STATISTICS_update (statistics,
503                             "Records resolved", 
504                             rd_count, 
505                             GNUNET_NO);
506 }
507
508
509 /**
510  * Handle lookup requests from client
511  *
512  * @param cls the closure
513  * @param client the client
514  * @param message the message
515  */
516 static void
517 handle_lookup (void *cls,
518                struct GNUNET_SERVER_Client *client,
519                const struct GNUNET_MessageHeader *message)
520 {
521   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
522   struct ClientLookupHandle *clh;
523   char *nameptr = name;
524   const char *utf_in;
525   const struct GNUNET_CRYPTO_EccPrivateKey *key;
526   uint16_t msg_size;
527   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
528   
529   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
530               "Received `%s' message\n", 
531               "LOOKUP");
532   msg_size = ntohs (message->size);
533   if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
534   {
535     GNUNET_break (0);
536     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
537     return;
538   }
539   sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
540   GNUNET_SERVER_notification_context_add (nc, client);
541   if (GNUNET_YES == ntohl (sh_msg->have_key))
542     key = &sh_msg->shorten_key;
543   else
544     key = NULL;
545   utf_in = (const char *) &sh_msg[1];
546   if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
547        (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
548   {
549     GNUNET_break (0);
550     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
551     return;
552   }
553   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
554   
555   clh = GNUNET_new (struct ClientLookupHandle);
556   GNUNET_SERVER_client_set_user_context (client, clh);
557   GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
558   clh->client = client;
559   clh->request_id = sh_msg->id;
560   if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
561        (GNUNET_OK != v4_enabled) )
562   {
563     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564                 "LOOKUP: Query for A record but AF_INET not supported!");
565     send_lookup_response (clh, 0, NULL);
566     return;
567   }  
568   if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
569        (GNUNET_OK != v6_enabled) )
570   {
571     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572                 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
573     send_lookup_response (clh, 0, NULL);
574     return;
575   }
576   clh->lookup = GNS_resolver_lookup (&sh_msg->zone, 
577                                      ntohl (sh_msg->type),
578                                      name,
579                                      key,
580                                      ntohl (sh_msg->only_cached),
581                                      &send_lookup_response, clh);
582   GNUNET_STATISTICS_update (statistics,
583                             "Lookup attempts", 
584                             1, GNUNET_NO);
585 }
586
587
588 /**
589  * One of our clients disconnected, clean up after it.
590  *
591  * @param cls NULL
592  * @param client the client that disconnected
593  */
594 static void
595 notify_client_disconnect (void *cls,
596                           struct GNUNET_SERVER_Client *client)
597 {
598   struct ClientLookupHandle *clh;
599
600   if (NULL == client)
601     return;
602   clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
603   if (NULL == clh)
604     return;
605   GNS_resolver_lookup_cancel (clh->lookup);
606   GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
607   GNUNET_free (clh);
608 }
609
610
611 /**
612  * Process GNS requests.
613  *
614  * @param cls closure
615  * @param server the initialized server
616  * @param c configuration to use
617  */
618 static void
619 run (void *cls, struct GNUNET_SERVER_Handle *server,
620      const struct GNUNET_CONFIGURATION_Handle *c)
621 {
622   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
623     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
624     {NULL, NULL, 0, 0}
625   };
626   struct GNUNET_CRYPTO_EccPublicKey dns_root;
627   unsigned long long max_parallel_bg_queries = 0;
628   char *dns_root_name;
629
630   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
631   v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
632
633   namestore_handle = GNUNET_NAMESTORE_connect (c);
634   if (NULL == namestore_handle)
635   {
636     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
637                 _("Failed to connect to the namestore!\n"));
638     GNUNET_SCHEDULER_shutdown ();
639     return;
640   }
641   
642   put_interval = INITIAL_PUT_INTERVAL;
643   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
644
645   if (GNUNET_OK ==
646       GNUNET_CONFIGURATION_get_value_time (c, "gns",
647                                            "ZONE_PUBLISH_TIME_WINDOW",
648                                            &zone_publish_time_window))
649   {
650     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651                 "Time window for zone iteration: %s\n",
652                 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
653   }
654   if (GNUNET_OK ==
655       GNUNET_CONFIGURATION_get_value_number (c, "gns",
656                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
657                                             &max_parallel_bg_queries))
658   {
659     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660                 "Number of allowed parallel background queries: %llu\n",
661                 max_parallel_bg_queries);
662   }
663
664   if (GNUNET_OK ==
665       GNUNET_CONFIGURATION_get_value_time (c, "gns",
666                                            "DEFAULT_LOOKUP_TIMEOUT",
667                                            &default_lookup_timeout))
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670                 "Default lookup timeout: %s\n",
671                 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
672                                                         GNUNET_YES));
673   }
674   
675   dht_handle = GNUNET_DHT_connect (c,
676                                    (unsigned int) max_parallel_bg_queries);
677   if (NULL == dht_handle)
678   {
679     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
680                 _("Could not connect to DHT!\n"));
681     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
682     return;
683   }
684   
685   if (GNUNET_OK ==
686       GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
687                                              &dns_root_name))
688   {
689     if (GNUNET_OK !=
690         GNUNET_CRYPTO_ecc_public_key_from_string (dns_root_name,
691                                                   strlen (dns_root_name),
692                                                   &dns_root))
693     {
694       GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
695                                  "gns", "DNS_ROOT", 
696                                  _("valid public key required"));
697       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
698       GNUNET_free (dns_root_name);
699       return;
700     }
701     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
702                 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
703                 dns_root_name);
704     GNUNET_free (dns_root_name);    
705     if (GNUNET_SYSERR ==
706         GNS_interceptor_init (&dns_root, c))
707     {
708       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
709       return;
710     }
711   }
712   /* FIXME: install client disconnect handle to clean up pending
713      lookups on client disconnect! */
714   GNS_resolver_init (namestore_handle, dht_handle, 
715                      c,
716                      max_parallel_bg_queries);
717   GNUNET_SERVER_disconnect_notify (server,
718                                    &notify_client_disconnect,
719                                    NULL);
720   /* Schedule periodic put for our records. */    
721   first_zone_iteration = GNUNET_YES;
722   GNUNET_SERVER_add_handlers (server, handlers);
723   statistics = GNUNET_STATISTICS_create ("gns", c);
724   nc = GNUNET_SERVER_notification_context_create (server, 1);
725   zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, 
726                                                 NULL);
727   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, 
728                                 &shutdown_task, NULL);
729 }
730
731
732 /**
733  * The main function for the GNS service.
734  *
735  * @param argc number of arguments from the command line
736  * @param argv command line arguments
737  * @return 0 ok, 1 on error
738  */
739 int
740 main (int argc, char *const *argv)
741 {
742   int ret;
743
744   ret =
745       (GNUNET_OK ==
746        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
747                            NULL)) ? 0 : 1;
748   return ret;
749 }
750
751 /* end of gnunet-service-gns.c */