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