822cc7d12c4e2adf72aed7e06f2d2d7defa733a3
[oweals/gnunet.git] / src / gns / gnunet-service-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011, 2012 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  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.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 "gns_common.h"
36 #include "gnunet-service-gns_resolver.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 /**
65  * Handle to a lookup operation from api
66  */
67 struct ClientLookupHandle
68 {
69
70   /**
71    * Handle to the requesting client
72    */
73   struct GNUNET_SERVER_Client *client;
74
75   /**
76    * optional zone private key used for shorten
77    */
78   struct GNUNET_CRYPTO_EccPrivateKey *shorten_key;
79
80   /**
81    * the name to look up
82    */
83   char *name; 
84
85   /**
86    * The zone we look up in
87    */
88   struct GNUNET_CRYPTO_ShortHashCode zone;
89
90   /**
91    * request id 
92    */
93   uint32_t request_id;
94
95   /**
96    * GNUNET_YES if we only want to lookup from local cache
97    */
98   int only_cached;
99
100   /**
101    * request type
102    */
103   int type;
104 };
105
106
107 /**
108  * Our handle to the DHT
109  */
110 static struct GNUNET_DHT_Handle *dht_handle;
111
112 /**
113  * Our zone's private key
114  */
115 static struct GNUNET_CRYPTO_EccPrivateKey *zone_key;
116
117 /**
118  * Our handle to the namestore service
119  */
120 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
121
122 /**
123  * Handle to iterate over our authoritative zone in namestore
124  */
125 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
126
127 /**
128  * Our notification context.
129  */
130 static struct GNUNET_SERVER_NotificationContext *nc;
131
132 /**
133  * Our zone hash
134  */
135 static struct GNUNET_CRYPTO_ShortHashCode zone_hash;
136
137 /**
138  * Useful for zone update for DHT put
139  */
140 static unsigned long long num_public_records;
141
142 /**
143  * Last seen record count
144  */
145 static unsigned long long last_num_public_records;
146
147 /**
148  * Zone iteration PUT interval.
149  */
150 static struct GNUNET_TIME_Relative put_interval;
151
152 /**
153  * Time window for zone iteration
154  */
155 static struct GNUNET_TIME_Relative zone_publish_time_window;
156
157 /**
158  * zone publish task
159  */
160 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
161
162 /**
163  * GNUNET_YES if automatic pkey import for name shortening
164  * is enabled
165  */
166 static int auto_import_pkey;
167
168 /**
169  * GNUNET_YES if zone has never been published before
170  */
171 static int first_zone_iteration;
172
173 /**
174  * The lookup timeout
175  */
176 static struct GNUNET_TIME_Relative default_lookup_timeout;
177
178 /**
179  * GNUNET_YES if ipv6 is supported
180  */
181 static int v6_enabled;
182
183 /**
184  * GNUNET_YES if ipv4 is supported
185  */
186 static int v4_enabled;
187
188 /**
189  * Handle to the statistics service
190  */
191 static struct GNUNET_STATISTICS_Handle *statistics;
192
193
194 /**
195  * Task run during shutdown.
196  *
197  * @param cls unused
198  * @param tc unused
199  */
200 static void
201 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 {
203   struct ClientShortenHandle *csh_tmp;
204
205   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
206              "Shutting down!\n");
207   GNUNET_SERVER_notification_context_destroy (nc);  
208   gns_interceptor_stop ();
209   gns_resolver_cleanup ();
210   if (NULL != statistics)
211   {
212     GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
213     statistics = NULL;
214   }
215   if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
216   {
217     GNUNET_SCHEDULER_cancel (zone_publish_task);
218     zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
219   }
220   if (NULL != namestore_iter)
221   {
222     GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
223     namestore_iter = NULL;
224   }
225   if (NULL != namestore_handle)
226   {
227     GNUNET_NAMESTORE_disconnect (namestore_handle);
228     namestore_handle = NULL;
229   }
230   if (NULL != dht_handle)
231   {
232     GNUNET_DHT_disconnect (dht_handle);
233     dht_handle = NULL;
234   }
235 }
236
237
238 /**
239  * Method called periodically that triggers iteration over authoritative records
240  *
241  * @param cls closure
242  * @param tc task context
243  */
244 static void
245 publish_zone_dht_next (void *cls,
246                        const struct GNUNET_SCHEDULER_TaskContext *tc)
247 {
248   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
249   GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
250 }
251
252
253 /**
254  * Periodically iterate over our zone and store everything in dht
255  *
256  * @param cls NULL
257  * @param tc task context
258  */
259 static void
260 publish_zone_dht_start (void *cls, 
261                         const struct GNUNET_SCHEDULER_TaskContext *tc);
262
263
264 /**
265  * Function used to put all records successively into the DHT.
266  *
267  * @param cls the closure (NULL)
268  * @param key the public key of the authority (ours)
269  * @param expiration lifetime of the namestore entry
270  * @param name the name of the records
271  * @param rd_count the number of records in data
272  * @param rd the record data
273  * @param signature the signature for the record data
274  */
275 static void
276 put_gns_record (void *cls,
277                 const struct GNUNET_CRYPTO_EccPublicKey *key,
278                 struct GNUNET_TIME_Absolute expiration,
279                 const char *name,
280                 unsigned int rd_count,
281                 const struct GNUNET_NAMESTORE_RecordData *rd,
282                 const struct GNUNET_CRYPTO_EccSignature *signature)
283 {  
284   struct GNSNameRecordBlock *nrb;
285   struct GNUNET_CRYPTO_ShortHashCode zhash;
286   struct GNUNET_HashCode dht_key;
287   uint32_t rd_payload_length;
288   char* nrb_data = NULL;
289   size_t namelen;
290   struct GNUNET_TIME_Relative next_put_interval; 
291
292   if (NULL == name)
293   {
294     /* we're done */
295     namestore_iter = NULL;
296     last_num_public_records = num_public_records;
297     first_zone_iteration = GNUNET_NO;
298     if (0 == num_public_records)
299     {
300       /**
301        * If no records are known (startup) or none present
302        * we can safely set the interval to the value for a single
303        * record
304        */
305       put_interval = zone_publish_time_window;
306       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
307                   "No records in db.\n");
308     }
309     else
310     {
311       put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
312                                                   num_public_records);
313     }
314     put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
315                                              put_interval);
316
317     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318                 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
319                 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
320     GNUNET_STATISTICS_set (statistics,
321                            "Current zone iteration interval (in ms)",
322                            put_interval.rel_value_us / 1000LL,
323                            GNUNET_NO);
324     GNUNET_STATISTICS_update (statistics,
325                               "Number of zone iterations", 1, GNUNET_NO);
326     GNUNET_STATISTICS_set (statistics,
327                            "Number of public records in DHT",
328                            last_num_public_records,
329                            GNUNET_NO);
330     if (0 == num_public_records)
331       zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
332                                                          &publish_zone_dht_start,
333                                                          NULL);
334     else
335       zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
336     return;
337   }
338   
339   namelen = strlen (name) + 1;
340   if (0 == rd_count)
341   {
342     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343                 "No records for name `%s'! Skipping.\n",
344                 name);
345     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
346                                                    NULL);
347     return;
348   }
349   if (NULL == signature)
350   {
351     GNUNET_break (0);
352     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
353                                                    NULL);
354     return;
355   }
356   
357   /* TODO 2) AB: New publishing
358    *
359    * - Use new signature S_d
360    * - Obtain new derived public key V = H(H(i,Q) * Q)
361    * - Obtain HKDF(i,Q)
362    * - Compute encrypte record block E with HKDF(i,Q) (rd, rd_count)
363    * - Create block B = |V,E,S_d|
364    * - Compute new DHT key H(V) in TODO 3)
365    *
366    * -> Put (H(V), B)
367    */
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369               "Putting records for `%s' into the DHT\n", name); 
370   rd_payload_length = GNUNET_NAMESTORE_records_get_size (rd_count, rd); 
371   nrb = GNUNET_malloc (rd_payload_length + namelen
372                        + sizeof (struct GNSNameRecordBlock));
373   nrb->signature = *signature;
374   nrb->public_key = *key;
375   nrb->rd_count = htonl (rd_count);
376   memcpy (&nrb[1], name, namelen);
377   nrb_data = (char *) &nrb[1];
378   nrb_data += namelen;
379   rd_payload_length += sizeof(struct GNSNameRecordBlock) + namelen;
380   GNUNET_CRYPTO_short_hash (key,
381                             sizeof (struct GNUNET_CRYPTO_EccPublicKey),
382                             &zhash);
383   if (-1 == GNUNET_NAMESTORE_records_serialize (rd_count,
384                                                 rd,
385                                                 rd_payload_length,
386                                                 nrb_data))
387   {
388     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389                 _("Records for name `%s' in zone %s too large to fit into DHT"),
390                 name,
391                 GNUNET_short_h2s (&zhash));
392     GNUNET_free (nrb);
393     zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
394                                                    NULL);
395     return;
396   }
397   /* TODO AB: Here records are put in the DHT: modify dht_key to H(key) = H(H(name,zone) * zone) */
398   GNUNET_GNS_get_key_for_record (name, &zhash, &dht_key);
399   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400               "putting %u records from zone %s for `%s' under key: %s with size %u and timeout %s\n",
401               rd_count,
402               GNUNET_short_h2s (&zhash),
403               name, 
404               GNUNET_h2s (&dht_key), 
405               (unsigned int) rd_payload_length,
406               GNUNET_STRINGS_relative_time_to_string (DHT_OPERATION_TIMEOUT, GNUNET_YES));
407   
408   GNUNET_STATISTICS_update (statistics,
409                             "Record bytes put into DHT", 
410                             rd_payload_length, GNUNET_NO);
411
412   (void) GNUNET_DHT_put (dht_handle, &dht_key,
413                          DHT_GNS_REPLICATION_LEVEL,
414                          GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
415                          GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
416                          rd_payload_length,
417                          (char*)nrb,
418                          expiration,
419                          DHT_OPERATION_TIMEOUT,
420                          NULL,
421                          NULL); 
422   GNUNET_free (nrb);
423
424   num_public_records++;  
425   if ( (num_public_records > last_num_public_records)
426        && (GNUNET_NO == first_zone_iteration) )
427   {
428     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429                 "Last record count was lower than current record count.  Reducing interval.\n");
430     put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
431                                                 num_public_records);
432     next_put_interval = GNUNET_TIME_relative_divide (put_interval,
433                                                      LATE_ITERATION_SPEEDUP_FACTOR);
434   }
435   else
436     next_put_interval = put_interval;
437
438   GNUNET_STATISTICS_set (statistics,
439                          "Current zone iteration interval (ms)",
440                          next_put_interval.rel_value_us / 1000LL,
441                          GNUNET_NO); 
442   zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
443                                                     &publish_zone_dht_next,
444                                                     NULL);
445 }
446
447
448 /**
449  * Periodically iterate over our zone and store everything in dht
450  *
451  * @param cls NULL
452  * @param tc task context
453  */
454 static void
455 publish_zone_dht_start (void *cls, 
456                         const struct GNUNET_SCHEDULER_TaskContext *tc)
457 {
458   zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
459
460   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
461               "Scheduling DHT zone update!\n");  
462   /* start counting again */
463   num_public_records = 0;
464   namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
465                                                           NULL, /* All zones */
466                                                           GNUNET_NAMESTORE_RF_AUTHORITY,
467                                                           GNUNET_NAMESTORE_RF_PRIVATE,
468                                                           &put_gns_record,
469                                                           NULL);
470 }
471
472
473 /* END DHT ZONE PROPAGATION */
474
475
476 /**
477  * Reply to client with the result from our lookup.
478  *
479  * @param cls the closure (our client lookup handle)
480  * @param rd_count the number of records
481  * @param rd the record data
482  */
483 static void
484 send_lookup_response (void* cls,
485                       uint32_t rd_count,
486                       const struct GNUNET_NAMESTORE_RecordData *rd)
487 {
488   struct ClientLookupHandle* clh = cls;
489   struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
490   size_t len;
491   
492   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message with %d results\n",
493               "LOOKUP_RESULT", rd_count);
494   
495   len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
496   rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
497   
498   rmsg->id = clh->request_id;
499   rmsg->rd_count = htonl(rd_count);
500   rmsg->header.type = htons(GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
501   rmsg->header.size = 
502     htons(len+sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
503   
504   GNUNET_NAMESTORE_records_serialize (rd_count, rd, len, (char*)&rmsg[1]);
505   
506   GNUNET_SERVER_notification_context_unicast (nc, clh->client,
507                                 (const struct GNUNET_MessageHeader *) rmsg,
508                                 GNUNET_NO);
509   GNUNET_SERVER_receive_done (clh->client, GNUNET_OK);
510   
511   GNUNET_free(rmsg);
512   GNUNET_free(clh->name);
513   
514   if (NULL != clh->shorten_key)
515     GNUNET_CRYPTO_ecc_key_free (clh->shorten_key);
516   GNUNET_free (clh);
517   GNUNET_STATISTICS_update (statistics,
518                             "Completed lookups", 1, GNUNET_NO);
519   if (NULL != rd)
520     GNUNET_STATISTICS_update (statistics,
521                               "Records resolved", rd_count, GNUNET_NO);
522 }
523
524
525 /**
526  * Handle lookup requests from client
527  *
528  * @param cls the closure
529  * @param client the client
530  * @param message the message
531  */
532 static void
533 handle_lookup (void *cls,
534                struct GNUNET_SERVER_Client * client,
535                const struct GNUNET_MessageHeader * message)
536 {
537   size_t namelen;
538   char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
539   struct ClientLookupHandle *clh;
540   char* nameptr = name;
541   const char *utf_in;
542   int only_cached;
543   const struct GNUNET_CRYPTO_EccPrivateKey *key;
544   uint16_t msg_size;
545   const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
546   
547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
548               "Received `%s' message\n", "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 == ntohl (sh_msg->have_key))
559   {
560     key = &sh_msg->shorten_key;
561   }
562   else
563   {
564     key = NULL;
565   }
566   utf_in = (const char *) &sh_msg[1];
567   if ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1])
568   {
569     GNUNET_break (0);
570     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
571     return;
572   }  
573   GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
574   
575   namelen = strlen (name) + 1;
576   clh = GNUNET_malloc (sizeof (struct ClientLookupHandle));
577   memset (clh, 0, sizeof (struct ClientLookupHandle));
578   clh->client = client;
579   clh->name = GNUNET_malloc (namelen);
580   strcpy (clh->name, name);
581   clh->request_id = sh_msg->id;
582   clh->type = ntohl (sh_msg->type);
583   if (NULL != key)
584   {
585     clh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EccPrivateKey);
586     *clh->shorten_key = *key;
587   }
588   only_cached = ntohl (sh_msg->only_cached);
589   
590   if (strlen (name) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) {
591     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592                 "LOOKUP: %s is too long", name);
593     clh->name = NULL;
594     send_lookup_response (clh, 0, NULL);
595     return;
596   }
597
598   if ((GNUNET_DNSPARSER_TYPE_A == clh->type) &&
599       (GNUNET_OK != v4_enabled))
600   {
601     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
602                "LOOKUP: Query for A record but AF_INET not supported!");
603     clh->name = NULL;
604     send_lookup_response (clh, 0, NULL);
605     return;
606   }
607   
608   if ((GNUNET_DNSPARSER_TYPE_AAAA == clh->type) &&
609       (GNUNET_OK != v6_enabled))
610   {
611     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612                 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
613     clh->name = NULL;
614     send_lookup_response (clh, 0, NULL);
615     return;
616   }
617   
618   if (GNUNET_NO == ntohl (sh_msg->have_zone))
619     clh->zone = zone_hash;  /* Default zone */
620   else
621     clh->zone = sh_msg->zone;
622   
623   if (GNUNET_YES == auto_import_pkey)
624   {
625     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, clh->name,
626                                 clh->shorten_key,
627                                 default_lookup_timeout,
628                                 clh->only_cached,
629                                 &send_lookup_response, clh);  
630   }
631   else
632   {
633     gns_resolver_lookup_record (clh->zone, clh->zone, clh->type, name,
634                                 NULL,
635                                 default_lookup_timeout,
636                                 only_cached,
637                                 &send_lookup_response, clh);
638   }
639   GNUNET_STATISTICS_update (statistics,
640                             "Record lookup attempts", 1, GNUNET_NO);
641 }
642
643
644 /**
645  * Process GNS requests.
646  *
647  * @param cls closure
648  * @param server the initialized server
649  * @param c configuration to use
650  */
651 static void
652 run (void *cls, struct GNUNET_SERVER_Handle *server,
653      const struct GNUNET_CONFIGURATION_Handle *c)
654 {
655   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
656     { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
657     {NULL, NULL, 0, 0}
658   };
659   struct GNUNET_CRYPTO_EccPublicKey pkey;
660   unsigned long long max_parallel_bg_queries = 0;
661   int ignore_pending = GNUNET_NO;
662
663   v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
664   v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
665
666   namestore_handle = GNUNET_NAMESTORE_connect (c);
667   if (NULL == namestore_handle)
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670                 _("Failed to connect to the namestore!\n"));
671     GNUNET_SCHEDULER_shutdown ();
672     return;
673   }
674   
675   auto_import_pkey = GNUNET_NO;
676   if (GNUNET_YES ==
677       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
678                                             "AUTO_IMPORT_PKEY"))
679   {
680     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681                 "Automatic PKEY import is enabled.\n");
682     auto_import_pkey = GNUNET_YES;
683   }
684   put_interval = INITIAL_PUT_INTERVAL;
685   zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
686
687   if (GNUNET_OK ==
688       GNUNET_CONFIGURATION_get_value_time (c, "gns",
689                                            "ZONE_PUBLISH_TIME_WINDOW",
690                                            &zone_publish_time_window))
691   {
692     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693                 "Time window for zone iteration: %s\n",
694                 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
695   }
696   if (GNUNET_OK ==
697       GNUNET_CONFIGURATION_get_value_number (c, "gns",
698                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
699                                             &max_parallel_bg_queries))
700   {
701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702                 "Number of allowed parallel background queries: %llu\n",
703                 max_parallel_bg_queries);
704   }
705
706   if (GNUNET_YES ==
707       GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
708                                             "AUTO_IMPORT_CONFIRMATION_REQ"))
709   {
710     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711                 "Auto import requires user confirmation\n");
712     ignore_pending = GNUNET_YES;
713   }
714
715   if (GNUNET_OK ==
716       GNUNET_CONFIGURATION_get_value_time (c, "gns",
717                                            "DEFAULT_LOOKUP_TIMEOUT",
718                                            &default_lookup_timeout))
719   {
720     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721                 "Default lookup timeout: %s\n",
722                 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
723                                                         GNUNET_YES));
724   }
725   
726   dht_handle = GNUNET_DHT_connect (c,
727                                    (unsigned int) max_parallel_bg_queries);
728   if (NULL == dht_handle)
729   {
730     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731                 _("Could not connect to DHT!\n"));
732     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
733     return;
734   }
735   
736   if (GNUNET_SYSERR ==
737       gns_resolver_init (namestore_handle, dht_handle, zone_hash, c,
738                          max_parallel_bg_queries,
739                          ignore_pending))
740   {
741     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742                 _("Unable to initialize resolver!\n"));
743     GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
744     return;
745   }
746
747   if (GNUNET_YES ==
748       GNUNET_CONFIGURATION_get_value_yesno (c, "gns", "HIJACK_DNS"))
749   {
750     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
751                 "DNS hijacking enabled. Connecting to DNS service.\n");
752
753     if (GNUNET_SYSERR ==
754         gns_interceptor_init (zone_hash, zone_key, c))
755     {
756       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
757                "Failed to enable the DNS interceptor!\n");
758     }
759   }
760   
761   /**
762    * Schedule periodic put for our records We have roughly an hour for
763    * all records;
764    */
765   first_zone_iteration = GNUNET_YES;
766   zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start, NULL);
767   GNUNET_SERVER_add_handlers (server, handlers);
768   statistics = GNUNET_STATISTICS_create ("gns", c);
769   nc = GNUNET_SERVER_notification_context_create (server, 1);
770   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
771                                 NULL);
772 }
773
774
775 /**
776  * The main function for the GNS service.
777  *
778  * @param argc number of arguments from the command line
779  * @param argv command line arguments
780  * @return 0 ok, 1 on error
781  */
782 int
783 main (int argc, char *const *argv)
784 {
785   int ret;
786
787   ret =
788       (GNUNET_OK ==
789        GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
790                            NULL)) ? 0 : 1;
791   return ret;
792 }
793
794 /* end of gnunet-service-gns.c */