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 GNUnet GNS service
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
27 * - perform conversion of relative expiration times to absolute time
31 #include "gnunet_util_lib.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_dns_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_namestore_service.h"
37 #include "gnunet_gns_service.h"
38 #include "gnunet_statistics_service.h"
40 #include "gnunet-service-gns_resolver.h"
41 #include "gnunet-service-gns_interceptor.h"
42 #include "gnunet_protocols.h"
45 * The initial interval in milliseconds btween puts in
48 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
51 * The upper bound for the zone iteration interval in milliseconds
53 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
56 * The default put interval for the zone iteration. In case
59 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
62 * The factor the current zone iteration interval is divided by for each
63 * additional new record
65 #define LATE_ITERATION_SPEEDUP_FACTOR 2
68 * How long until a DHT PUT attempt should time out?
70 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
73 * What replication level do we use for DHT PUT operations?
75 #define DHT_GNS_REPLICATION_LEVEL 5
79 * Handle to a lookup operation from api
81 struct ClientLookupHandle
85 * We keep these in a DLL.
87 struct ClientLookupHandle *next;
90 * We keep these in a DLL.
92 struct ClientLookupHandle *prev;
95 * Handle to the requesting client
97 struct GNUNET_SERVER_Client *client;
100 * Active handle for the lookup.
102 struct GNS_ResolverHandle *lookup;
113 * Our handle to the DHT
115 static struct GNUNET_DHT_Handle *dht_handle;
118 * Active DHT put operation (or NULL)
120 static struct GNUNET_DHT_PutHandle *active_put;
123 * Our handle to the namestore service
125 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
128 * Handle to iterate over our authoritative zone in namestore
130 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
133 * Our notification context.
135 static struct GNUNET_SERVER_NotificationContext *nc;
140 static struct ClientLookupHandle *clh_head;
145 static struct ClientLookupHandle *clh_tail;
148 * Useful for zone update for DHT put
150 static unsigned long long num_public_records;
153 * Last seen record count
155 static unsigned long long last_num_public_records;
158 * Zone iteration PUT interval.
160 static struct GNUNET_TIME_Relative put_interval;
163 * Time window for zone iteration
165 static struct GNUNET_TIME_Relative zone_publish_time_window;
170 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
173 * #GNUNET_YES if zone has never been published before
175 static int first_zone_iteration;
180 static struct GNUNET_TIME_Relative default_lookup_timeout;
183 * #GNUNET_YES if ipv6 is supported
185 static int v6_enabled;
188 * #GNUNET_YES if ipv4 is supported
190 static int v4_enabled;
193 * Handle to the statistics service
195 static struct GNUNET_STATISTICS_Handle *statistics;
199 * Task run during shutdown.
205 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 struct ClientLookupHandle *clh;
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 GNUNET_SERVER_notification_context_destroy (nc);
212 while (NULL != (clh = clh_head))
214 GNS_resolver_lookup_cancel (clh->lookup);
215 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
219 GNS_interceptor_done ();
220 GNS_resolver_done ();
221 if (NULL != statistics)
223 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
226 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
228 GNUNET_SCHEDULER_cancel (zone_publish_task);
229 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
231 if (NULL != namestore_iter)
233 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
234 namestore_iter = NULL;
236 if (NULL != namestore_handle)
238 GNUNET_NAMESTORE_disconnect (namestore_handle);
239 namestore_handle = NULL;
241 if (NULL != active_put)
243 GNUNET_DHT_put_cancel (active_put);
246 if (NULL != dht_handle)
248 GNUNET_DHT_disconnect (dht_handle);
255 * Method called periodically that triggers iteration over authoritative records
258 * @param tc task context
261 publish_zone_dht_next (void *cls,
262 const struct GNUNET_SCHEDULER_TaskContext *tc)
264 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
265 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
270 * Periodically iterate over our zone and store everything in dht
273 * @param tc task context
276 publish_zone_dht_start (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc);
281 * Continuation called from DHT once the PUT operation is done.
283 * @param cls closure, NULL
284 * @param success #GNUNET_OK on success
287 dht_put_continuation (void *cls,
290 struct GNUNET_TIME_Relative next_put_interval;
292 num_public_records++;
293 if ( (num_public_records > last_num_public_records) &&
294 (GNUNET_NO == first_zone_iteration) )
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Last record count was lower than current record count. Reducing interval.\n");
298 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
300 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
301 LATE_ITERATION_SPEEDUP_FACTOR);
304 next_put_interval = put_interval;
306 GNUNET_STATISTICS_set (statistics,
307 "Current zone iteration interval (ms)",
308 next_put_interval.rel_value_us / 1000LL,
310 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
311 &publish_zone_dht_next,
317 * Function used to put all records successively into the DHT.
319 * @param cls the closure (NULL)
320 * @param key the private key of the authority (ours)
321 * @param name the name of the records, NULL once the iteration is done
322 * @param rd_count the number of records in @a rd
323 * @param rd the record data
326 put_gns_record (void *cls,
327 const struct GNUNET_CRYPTO_EccPrivateKey *key,
329 unsigned int rd_count,
330 const struct GNUNET_NAMESTORE_RecordData *rd)
332 struct GNUNET_NAMESTORE_Block *block;
333 struct GNUNET_HashCode query;
334 struct GNUNET_TIME_Absolute expire;
336 struct GNUNET_NAMESTORE_RecordData rd_public[rd_count];
337 unsigned int rd_public_count;
342 /* we're done with one iteration, calculate when to do the next one */
343 namestore_iter = NULL;
344 last_num_public_records = num_public_records;
345 first_zone_iteration = GNUNET_NO;
346 if (0 == num_public_records)
349 * If no records are known (startup) or none present
350 * we can safely set the interval to the value for a single
353 put_interval = zone_publish_time_window;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
355 "No records in namestore database.\n");
359 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
362 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
367 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
368 GNUNET_STATISTICS_set (statistics,
369 "Current zone iteration interval (in ms)",
370 put_interval.rel_value_us / 1000LL,
372 GNUNET_STATISTICS_update (statistics,
373 "Number of zone iterations",
376 GNUNET_STATISTICS_set (statistics,
377 "Number of public records in DHT",
378 last_num_public_records,
380 if (0 == num_public_records)
381 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
382 &publish_zone_dht_start,
385 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
390 /* filter out records that are not public! */
392 for (i=0;i<rd_count;i++)
393 if (0 == (rd[i].flags & (GNUNET_NAMESTORE_RF_PRIVATE |
394 GNUNET_NAMESTORE_RF_PENDING)))
395 rd_public[rd_public_count++] = rd[i];
398 /* We got a set of records to publish */
399 if (0 == rd_public_count)
401 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
405 expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_public_count,
407 block = GNUNET_NAMESTORE_block_create (key,
412 block_size = ntohl (block->purpose.size)
413 + sizeof (struct GNUNET_CRYPTO_EccSignature)
414 + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
415 GNUNET_NAMESTORE_query_from_private_key (key,
419 active_put = GNUNET_DHT_put (dht_handle, &query,
420 DHT_GNS_REPLICATION_LEVEL,
421 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
422 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
426 DHT_OPERATION_TIMEOUT,
427 &dht_put_continuation,
429 if (NULL == active_put)
432 dht_put_continuation (NULL, GNUNET_NO);
439 * Periodically iterate over our zone and store everything in dht
442 * @param tc task context
445 publish_zone_dht_start (void *cls,
446 const struct GNUNET_SCHEDULER_TaskContext *tc)
448 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Scheduling DHT zone update!\n");
452 /* start counting again */
453 num_public_records = 0;
454 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
455 NULL, /* All zones */
461 /* END DHT ZONE PROPAGATION */
465 * Reply to client with the result from our lookup.
467 * @param cls the closure (our client lookup handle)
468 * @param rd_count the number of records
469 * @param rd the record data
472 send_lookup_response (void* cls,
474 const struct GNUNET_NAMESTORE_RecordData *rd)
476 struct ClientLookupHandle *clh = cls;
477 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Sending `%s' message with %d results\n",
485 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
486 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
487 rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
488 rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
489 rmsg->id = clh->request_id;
490 rmsg->rd_count = htonl (rd_count);
491 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len,
493 GNUNET_SERVER_notification_context_unicast (nc,
498 GNUNET_SERVER_receive_done (clh->client,
500 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
502 GNUNET_STATISTICS_update (statistics,
503 "Completed lookups", 1,
505 GNUNET_STATISTICS_update (statistics,
513 * Handle lookup requests from client
515 * @param cls the closure
516 * @param client the client
517 * @param message the message
520 handle_lookup (void *cls,
521 struct GNUNET_SERVER_Client *client,
522 const struct GNUNET_MessageHeader *message)
524 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
525 struct ClientLookupHandle *clh;
526 char *nameptr = name;
528 const struct GNUNET_CRYPTO_EccPrivateKey *key;
530 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Received `%s' message\n",
535 msg_size = ntohs (message->size);
536 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
542 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
543 GNUNET_SERVER_notification_context_add (nc, client);
544 if (GNUNET_YES == ntohl (sh_msg->have_key))
545 key = &sh_msg->shorten_key;
548 utf_in = (const char *) &sh_msg[1];
549 if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
550 (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
556 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
558 clh = GNUNET_new (struct ClientLookupHandle);
559 GNUNET_SERVER_client_set_user_context (client, clh);
560 GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
561 clh->client = client;
562 clh->request_id = sh_msg->id;
563 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
564 (GNUNET_OK != v4_enabled) )
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "LOOKUP: Query for A record but AF_INET not supported!");
568 send_lookup_response (clh, 0, NULL);
571 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
572 (GNUNET_OK != v6_enabled) )
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
576 send_lookup_response (clh, 0, NULL);
579 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
580 ntohl (sh_msg->type),
583 ntohl (sh_msg->only_cached),
584 &send_lookup_response, clh);
585 GNUNET_STATISTICS_update (statistics,
592 * One of our clients disconnected, clean up after it.
595 * @param client the client that disconnected
598 notify_client_disconnect (void *cls,
599 struct GNUNET_SERVER_Client *client)
601 struct ClientLookupHandle *clh;
605 clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
608 GNS_resolver_lookup_cancel (clh->lookup);
609 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
615 * Process GNS requests.
618 * @param server the initialized server
619 * @param c configuration to use
622 run (void *cls, struct GNUNET_SERVER_Handle *server,
623 const struct GNUNET_CONFIGURATION_Handle *c)
625 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
626 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
629 struct GNUNET_CRYPTO_EccPublicKey dns_root;
630 unsigned long long max_parallel_bg_queries = 0;
633 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
634 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
636 namestore_handle = GNUNET_NAMESTORE_connect (c);
637 if (NULL == namestore_handle)
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 _("Failed to connect to the namestore!\n"));
641 GNUNET_SCHEDULER_shutdown ();
645 put_interval = INITIAL_PUT_INTERVAL;
646 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
649 GNUNET_CONFIGURATION_get_value_time (c, "gns",
650 "ZONE_PUBLISH_TIME_WINDOW",
651 &zone_publish_time_window))
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Time window for zone iteration: %s\n",
655 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
658 GNUNET_CONFIGURATION_get_value_number (c, "gns",
659 "MAX_PARALLEL_BACKGROUND_QUERIES",
660 &max_parallel_bg_queries))
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Number of allowed parallel background queries: %llu\n",
664 max_parallel_bg_queries);
668 GNUNET_CONFIGURATION_get_value_time (c, "gns",
669 "DEFAULT_LOOKUP_TIMEOUT",
670 &default_lookup_timeout))
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Default lookup timeout: %s\n",
674 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
678 dht_handle = GNUNET_DHT_connect (c,
679 (unsigned int) max_parallel_bg_queries);
680 if (NULL == dht_handle)
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
683 _("Could not connect to DHT!\n"));
684 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
689 GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
693 GNUNET_CRYPTO_ecc_public_key_from_string (dns_root_name,
694 strlen (dns_root_name),
697 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
699 _("valid public key required"));
700 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
701 GNUNET_free (dns_root_name);
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
705 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
707 GNUNET_free (dns_root_name);
709 GNS_interceptor_init (&dns_root, c))
711 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
715 /* FIXME: install client disconnect handle to clean up pending
716 lookups on client disconnect! */
717 GNS_resolver_init (namestore_handle, dht_handle,
719 max_parallel_bg_queries);
720 GNUNET_SERVER_disconnect_notify (server,
721 ¬ify_client_disconnect,
723 /* Schedule periodic put for our records. */
724 first_zone_iteration = GNUNET_YES;
725 GNUNET_SERVER_add_handlers (server, handlers);
726 statistics = GNUNET_STATISTICS_create ("gns", c);
727 nc = GNUNET_SERVER_notification_context_create (server, 1);
728 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
730 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
731 &shutdown_task, NULL);
736 * The main function for the GNS service.
738 * @param argc number of arguments from the command line
739 * @param argv command line arguments
740 * @return 0 ok, 1 on error
743 main (int argc, char *const *argv)
749 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
754 /* end of gnunet-service-gns.c */