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