2 This file is part of GNUnet.
3 (C) 2011-2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file gns/gnunet-service-gns.c
22 * @brief GNU Name System (main service)
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
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"
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"
42 * The initial interval in milliseconds btween puts in
45 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
48 * The upper bound for the zone iteration interval in milliseconds
50 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
53 * The default put interval for the zone iteration. In case
56 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
59 * The factor the current zone iteration interval is divided by for each
60 * additional new record
62 #define LATE_ITERATION_SPEEDUP_FACTOR 2
65 * How long until a DHT PUT attempt should time out?
67 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
70 * What replication level do we use for DHT PUT operations?
72 #define DHT_GNS_REPLICATION_LEVEL 5
76 * Handle to a lookup operation from api
78 struct ClientLookupHandle
82 * We keep these in a DLL.
84 struct ClientLookupHandle *next;
87 * We keep these in a DLL.
89 struct ClientLookupHandle *prev;
92 * Handle to the requesting client
94 struct GNUNET_SERVER_Client *client;
97 * Active handle for the lookup.
99 struct GNS_ResolverHandle *lookup;
110 * Our handle to the DHT
112 static struct GNUNET_DHT_Handle *dht_handle;
115 * Active DHT put operation (or NULL)
117 static struct GNUNET_DHT_PutHandle *active_put;
120 * Our handle to the namestore service
122 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
125 * Our handle to the namecache service
127 static struct GNUNET_NAMECACHE_Handle *namecache_handle;
130 * Handle to iterate over our authoritative zone in namestore
132 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
135 * Our notification context.
137 static struct GNUNET_SERVER_NotificationContext *nc;
142 static struct ClientLookupHandle *clh_head;
147 static struct ClientLookupHandle *clh_tail;
150 * Useful for zone update for DHT put
152 static unsigned long long num_public_records;
155 * Last seen record count
157 static unsigned long long last_num_public_records;
160 * Minimum relative expiration time of records seem during the current
163 static struct GNUNET_TIME_Relative min_relative_record_time;
166 * Zone iteration PUT interval.
168 static struct GNUNET_TIME_Relative put_interval;
171 * Time window for zone iteration
173 static struct GNUNET_TIME_Relative zone_publish_time_window;
178 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
181 * #GNUNET_YES if zone has never been published before
183 static int first_zone_iteration;
186 * #GNUNET_YES if ipv6 is supported
188 static int v6_enabled;
191 * #GNUNET_YES if ipv4 is supported
193 static int v4_enabled;
196 * Handle to the statistics service
198 static struct GNUNET_STATISTICS_Handle *statistics;
202 * Task run during shutdown.
208 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 struct ClientLookupHandle *clh;
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 GNUNET_SERVER_notification_context_destroy (nc);
215 while (NULL != (clh = clh_head))
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);
223 GNS_interceptor_done ();
224 GNS_resolver_done ();
226 if (NULL != statistics)
228 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
231 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
233 GNUNET_SCHEDULER_cancel (zone_publish_task);
234 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
236 if (NULL != namestore_iter)
238 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
239 namestore_iter = NULL;
241 if (NULL != namestore_handle)
243 GNUNET_NAMESTORE_disconnect (namestore_handle);
244 namestore_handle = NULL;
246 if (NULL != namecache_handle)
248 GNUNET_NAMECACHE_disconnect (namecache_handle);
249 namecache_handle = NULL;
251 if (NULL != active_put)
253 GNUNET_DHT_put_cancel (active_put);
256 if (NULL != dht_handle)
258 GNUNET_DHT_disconnect (dht_handle);
265 * Method called periodically that triggers iteration over authoritative records
268 * @param tc task context
271 publish_zone_dht_next (void *cls,
272 const struct GNUNET_SCHEDULER_TaskContext *tc)
274 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
275 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
280 * Periodically iterate over our zone and store everything in dht
283 * @param tc task context
286 publish_zone_dht_start (void *cls,
287 const struct GNUNET_SCHEDULER_TaskContext *tc);
291 * Continuation called from DHT once the PUT operation is done.
293 * @param cls closure, NULL
294 * @param success #GNUNET_OK on success
297 dht_put_continuation (void *cls,
300 struct GNUNET_TIME_Relative next_put_interval;
303 num_public_records++;
304 if ( (num_public_records > last_num_public_records) &&
305 (GNUNET_NO == first_zone_iteration) )
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,
311 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
312 LATE_ITERATION_SPEEDUP_FACTOR);
315 next_put_interval = put_interval;
317 GNUNET_STATISTICS_set (statistics,
318 "Current zone iteration interval (ms)",
319 next_put_interval.rel_value_us / 1000LL,
321 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
322 &publish_zone_dht_next,
328 * Function used to put all records successively into the DHT.
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
337 put_gns_record (void *cls,
338 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
340 unsigned int rd_count,
341 const struct GNUNET_GNSRECORD_Data *rd)
343 struct GNUNET_GNSRECORD_Block *block;
344 struct GNUNET_HashCode query;
345 struct GNUNET_TIME_Absolute expire;
346 struct GNUNET_TIME_Absolute now;
348 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
349 unsigned int rd_public_count;
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)
361 * If no records are known (startup) or none present
362 * we can safely set the interval to the value for a single
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");
371 zone_publish_time_window
372 = GNUNET_TIME_relative_min (min_relative_record_time,
373 DEFAULT_ZONE_PUBLISH_TIME_WINDOW);
374 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
377 /* reset for next iteration */
378 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
379 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
384 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
385 GNUNET_STATISTICS_set (statistics,
386 "Current zone iteration interval (in ms)",
387 put_interval.rel_value_us / 1000LL,
389 GNUNET_STATISTICS_update (statistics,
390 "Number of zone iterations",
393 GNUNET_STATISTICS_set (statistics,
394 "Number of public records in DHT",
395 last_num_public_records,
397 if (0 == num_public_records)
398 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
399 &publish_zone_dht_start,
402 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
407 /* filter out records that are not public, and convert to
408 absolute expiration time. */
410 now = GNUNET_TIME_absolute_get ();
411 for (i=0;i<rd_count;i++)
412 if (0 == (rd[i].flags & (GNUNET_GNSRECORD_RF_PRIVATE |
413 GNUNET_GNSRECORD_RF_PENDING)))
415 rd_public[rd_public_count] = rd[i];
416 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
418 /* GNUNET_GNSRECORD_block_create will convert to absolute time;
419 we just need to adjust our iteration frequency */
420 min_relative_record_time.rel_value_us =
421 GNUNET_MIN (rd_public[rd_public_count].expiration_time,
422 min_relative_record_time.rel_value_us);
424 else if (rd_public[rd_public_count].expiration_time < now.abs_value_us)
426 /* record already expired, skip it */
432 /* We got a set of records to publish */
433 if (0 == rd_public_count)
435 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
439 expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
441 block = GNUNET_GNSRECORD_block_create (key,
446 block_size = ntohl (block->purpose.size)
447 + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
448 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
449 GNUNET_GNSRECORD_query_from_private_key (key,
452 active_put = GNUNET_DHT_put (dht_handle, &query,
453 DHT_GNS_REPLICATION_LEVEL,
454 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
455 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
459 DHT_OPERATION_TIMEOUT,
460 &dht_put_continuation,
462 if (NULL == active_put)
465 dht_put_continuation (NULL, GNUNET_NO);
472 * Periodically iterate over our zone and store everything in dht
475 * @param tc task context
478 publish_zone_dht_start (void *cls,
479 const struct GNUNET_SCHEDULER_TaskContext *tc)
481 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Starting DHT zone update!\n");
485 /* start counting again */
486 num_public_records = 0;
487 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
488 NULL, /* All zones */
494 /* END DHT ZONE PROPAGATION */
498 * Reply to client with the result from our lookup.
500 * @param cls the closure (our client lookup handle)
501 * @param rd_count the number of records in @a rd
502 * @param rd the record data
505 send_lookup_response (void* cls,
507 const struct GNUNET_GNSRECORD_Data *rd)
509 struct ClientLookupHandle *clh = cls;
510 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Sending `%s' message with %d results\n",
518 len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
519 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
520 rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
521 rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
522 rmsg->id = clh->request_id;
523 rmsg->rd_count = htonl (rd_count);
524 GNUNET_GNSRECORD_records_serialize (rd_count, rd, len,
526 GNUNET_SERVER_notification_context_unicast (nc,
531 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
532 GNUNET_SERVER_client_set_user_context (clh->client, NULL);
534 GNUNET_STATISTICS_update (statistics,
535 "Completed lookups", 1,
537 GNUNET_STATISTICS_update (statistics,
545 * Handle lookup requests from client
547 * @param cls the closure
548 * @param client the client
549 * @param message the message
552 handle_lookup (void *cls,
553 struct GNUNET_SERVER_Client *client,
554 const struct GNUNET_MessageHeader *message)
556 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
557 struct ClientLookupHandle *clh;
558 char *nameptr = name;
560 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key;
562 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Received `%s' message\n",
567 msg_size = ntohs (message->size);
568 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
575 GNUNET_SERVER_notification_context_add (nc, client);
576 if (GNUNET_YES == ntohs (sh_msg->have_key))
577 key = &sh_msg->shorten_key;
580 utf_in = (const char *) &sh_msg[1];
581 if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
582 (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
588 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
591 clh = GNUNET_new (struct ClientLookupHandle);
592 GNUNET_SERVER_client_set_user_context (client, clh);
593 GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
594 clh->client = client;
595 clh->request_id = sh_msg->id;
596 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
597 (GNUNET_OK != v4_enabled) )
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "LOOKUP: Query for A record but AF_INET not supported!");
601 send_lookup_response (clh, 0, NULL);
604 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
605 (GNUNET_OK != v6_enabled) )
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
609 send_lookup_response (clh, 0, NULL);
612 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
613 ntohl (sh_msg->type),
616 ntohl (sh_msg->only_cached),
617 &send_lookup_response, clh);
618 GNUNET_STATISTICS_update (statistics,
625 * One of our clients disconnected, clean up after it.
628 * @param client the client that disconnected
631 notify_client_disconnect (void *cls,
632 struct GNUNET_SERVER_Client *client)
634 struct ClientLookupHandle *clh;
638 clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
641 GNS_resolver_lookup_cancel (clh->lookup);
642 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
648 * Process GNS requests.
651 * @param server the initialized server
652 * @param c configuration to use
655 run (void *cls, struct GNUNET_SERVER_Handle *server,
656 const struct GNUNET_CONFIGURATION_Handle *c)
658 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
659 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
662 struct GNUNET_CRYPTO_EcdsaPublicKey dns_root;
663 unsigned long long max_parallel_bg_queries = 0;
666 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
667 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
668 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
669 namestore_handle = GNUNET_NAMESTORE_connect (c);
670 if (NULL == namestore_handle)
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 _("Failed to connect to the namestore!\n"));
674 GNUNET_SCHEDULER_shutdown ();
677 namecache_handle = GNUNET_NAMECACHE_connect (c);
678 if (NULL == namecache_handle)
680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
681 _("Failed to connect to the namecache!\n"));
682 GNUNET_SCHEDULER_shutdown ();
686 put_interval = INITIAL_PUT_INTERVAL;
687 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
690 GNUNET_CONFIGURATION_get_value_time (c, "gns",
691 "ZONE_PUBLISH_TIME_WINDOW",
692 &zone_publish_time_window))
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Time window for zone iteration: %s\n",
696 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
699 GNUNET_CONFIGURATION_get_value_number (c, "gns",
700 "MAX_PARALLEL_BACKGROUND_QUERIES",
701 &max_parallel_bg_queries))
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Number of allowed parallel background queries: %llu\n",
705 max_parallel_bg_queries);
708 dht_handle = GNUNET_DHT_connect (c,
709 (unsigned int) max_parallel_bg_queries);
710 if (NULL == dht_handle)
712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
713 _("Could not connect to DHT!\n"));
714 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
719 GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
723 GNUNET_CRYPTO_ecdsa_public_key_from_string (dns_root_name,
724 strlen (dns_root_name),
727 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
729 _("valid public key required"));
730 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
731 GNUNET_free (dns_root_name);
734 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
735 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
737 GNUNET_free (dns_root_name);
739 GNS_interceptor_init (&dns_root, c))
741 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
745 GNS_resolver_init (namestore_handle,
749 max_parallel_bg_queries);
750 GNS_shorten_init (namestore_handle,
753 GNUNET_SERVER_disconnect_notify (server,
754 ¬ify_client_disconnect,
756 /* Schedule periodic put for our records. */
757 first_zone_iteration = GNUNET_YES;
758 GNUNET_SERVER_add_handlers (server, handlers);
759 statistics = GNUNET_STATISTICS_create ("gns", c);
760 nc = GNUNET_SERVER_notification_context_create (server, 1);
761 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
763 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
764 &shutdown_task, NULL);
769 * The main function for the GNS service.
771 * @param argc number of arguments from the command line
772 * @param argv command line arguments
773 * @return 0 ok, 1 on error
776 main (int argc, char *const *argv)
782 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
787 /* end of gnunet-service-gns.c */