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 * - conversion of private to public records does NOT check if the
28 * records are actually public
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 "gns_common.h"
41 #include "gnunet-service-gns_resolver.h"
42 #include "gnunet-service-gns_interceptor.h"
43 #include "gnunet_protocols.h"
46 * The initial interval in milliseconds btween puts in
49 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
52 * The upper bound for the zone iteration interval in milliseconds
54 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
57 * The default put interval for the zone iteration. In case
60 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
63 * The factor the current zone iteration interval is divided by for each
64 * additional new record
66 #define LATE_ITERATION_SPEEDUP_FACTOR 2
69 * How long until a DHT PUT attempt should time out?
71 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
74 * What replication level do we use for DHT PUT operations?
76 #define DHT_GNS_REPLICATION_LEVEL 5
80 * Handle to a lookup operation from api
82 struct ClientLookupHandle
86 * We keep these in a DLL.
88 struct ClientLookupHandle *next;
91 * We keep these in a DLL.
93 struct ClientLookupHandle *prev;
96 * Handle to the requesting client
98 struct GNUNET_SERVER_Client *client;
101 * Active handle for the lookup.
103 struct GNS_ResolverHandle *lookup;
114 * Our handle to the DHT
116 static struct GNUNET_DHT_Handle *dht_handle;
119 * Active DHT put operation (or NULL)
121 static struct GNUNET_DHT_PutHandle *active_put;
124 * Our handle to the namestore service
126 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
129 * Handle to iterate over our authoritative zone in namestore
131 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
134 * Our notification context.
136 static struct GNUNET_SERVER_NotificationContext *nc;
141 static struct ClientLookupHandle *clh_head;
146 static struct ClientLookupHandle *clh_tail;
149 * Useful for zone update for DHT put
151 static unsigned long long num_public_records;
154 * Last seen record count
156 static unsigned long long last_num_public_records;
159 * Zone iteration PUT interval.
161 static struct GNUNET_TIME_Relative put_interval;
164 * Time window for zone iteration
166 static struct GNUNET_TIME_Relative zone_publish_time_window;
171 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
174 * #GNUNET_YES if zone has never been published before
176 static int first_zone_iteration;
181 static struct GNUNET_TIME_Relative default_lookup_timeout;
184 * #GNUNET_YES if ipv6 is supported
186 static int v6_enabled;
189 * #GNUNET_YES if ipv4 is supported
191 static int v4_enabled;
194 * Handle to the statistics service
196 static struct GNUNET_STATISTICS_Handle *statistics;
200 * Task run during shutdown.
206 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 struct ClientLookupHandle *clh;
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 GNUNET_SERVER_notification_context_destroy (nc);
213 while (NULL != (clh = clh_head))
215 GNS_resolver_lookup_cancel (clh->lookup);
216 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
220 GNS_interceptor_done ();
221 GNS_resolver_done ();
222 if (NULL != statistics)
224 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
227 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
229 GNUNET_SCHEDULER_cancel (zone_publish_task);
230 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
232 if (NULL != namestore_iter)
234 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
235 namestore_iter = NULL;
237 if (NULL != namestore_handle)
239 GNUNET_NAMESTORE_disconnect (namestore_handle);
240 namestore_handle = NULL;
242 if (NULL != active_put)
244 GNUNET_DHT_put_cancel (active_put);
247 if (NULL != dht_handle)
249 GNUNET_DHT_disconnect (dht_handle);
256 * Method called periodically that triggers iteration over authoritative records
259 * @param tc task context
262 publish_zone_dht_next (void *cls,
263 const struct GNUNET_SCHEDULER_TaskContext *tc)
265 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
266 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
271 * Periodically iterate over our zone and store everything in dht
274 * @param tc task context
277 publish_zone_dht_start (void *cls,
278 const struct GNUNET_SCHEDULER_TaskContext *tc);
282 * Continuation called from DHT once the PUT operation is done.
284 * @param cls closure, NULL
285 * @param success #GNUNET_OK on success
288 dht_put_continuation (void *cls,
291 struct GNUNET_TIME_Relative next_put_interval;
293 num_public_records++;
294 if ( (num_public_records > last_num_public_records) &&
295 (GNUNET_NO == first_zone_iteration) )
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Last record count was lower than current record count. Reducing interval.\n");
299 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
301 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
302 LATE_ITERATION_SPEEDUP_FACTOR);
305 next_put_interval = put_interval;
307 GNUNET_STATISTICS_set (statistics,
308 "Current zone iteration interval (ms)",
309 next_put_interval.rel_value_us / 1000LL,
311 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
312 &publish_zone_dht_next,
318 * Function used to put all records successively into the DHT.
320 * @param cls the closure (NULL)
321 * @param key the private key of the authority (ours)
322 * @param name the name of the records, NULL once the iteration is done
323 * @param rd_count the number of records in @a rd
324 * @param rd the record data
327 put_gns_record (void *cls,
328 const struct GNUNET_CRYPTO_EccPrivateKey *key,
330 unsigned int rd_count,
331 const struct GNUNET_NAMESTORE_RecordData *rd)
333 struct GNUNET_NAMESTORE_Block *block;
334 struct GNUNET_HashCode query;
335 struct GNUNET_TIME_Absolute expire;
340 /* we're done with one iteration, calculate when to do the next one */
341 namestore_iter = NULL;
342 last_num_public_records = num_public_records;
343 first_zone_iteration = GNUNET_NO;
344 if (0 == num_public_records)
347 * If no records are known (startup) or none present
348 * we can safely set the interval to the value for a single
351 put_interval = zone_publish_time_window;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
353 "No records in namestore database.\n");
357 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
360 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
365 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
366 GNUNET_STATISTICS_set (statistics,
367 "Current zone iteration interval (in ms)",
368 put_interval.rel_value_us / 1000LL,
370 GNUNET_STATISTICS_update (statistics,
371 "Number of zone iterations",
374 GNUNET_STATISTICS_set (statistics,
375 "Number of public records in DHT",
376 last_num_public_records,
378 if (0 == num_public_records)
379 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
380 &publish_zone_dht_start,
383 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
388 /* FIXME: filter out records that are not public! */
390 /* We got a set of records to publish */
393 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
397 expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
399 block = GNUNET_NAMESTORE_block_create (key,
404 block_size = ntohl (block->purpose.size)
405 + sizeof (struct GNUNET_CRYPTO_EccSignature)
406 + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
407 GNUNET_NAMESTORE_query_from_private_key (key,
411 active_put = GNUNET_DHT_put (dht_handle, &query,
412 DHT_GNS_REPLICATION_LEVEL,
413 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
414 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
418 DHT_OPERATION_TIMEOUT,
419 &dht_put_continuation,
421 if (NULL == active_put)
424 dht_put_continuation (NULL, GNUNET_NO);
431 * Periodically iterate over our zone and store everything in dht
434 * @param tc task context
437 publish_zone_dht_start (void *cls,
438 const struct GNUNET_SCHEDULER_TaskContext *tc)
440 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Scheduling DHT zone update!\n");
444 /* start counting again */
445 num_public_records = 0;
446 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
447 NULL, /* All zones */
453 /* END DHT ZONE PROPAGATION */
457 * Reply to client with the result from our lookup.
459 * @param cls the closure (our client lookup handle)
460 * @param rd_count the number of records
461 * @param rd the record data
464 send_lookup_response (void* cls,
466 const struct GNUNET_NAMESTORE_RecordData *rd)
468 struct ClientLookupHandle *clh = cls;
469 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Sending `%s' message with %d results\n",
477 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
478 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
479 rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
480 rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
481 rmsg->id = clh->request_id;
482 rmsg->rd_count = htonl (rd_count);
483 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len,
485 GNUNET_SERVER_notification_context_unicast (nc,
490 GNUNET_SERVER_receive_done (clh->client,
492 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
494 GNUNET_STATISTICS_update (statistics,
495 "Completed lookups", 1,
497 GNUNET_STATISTICS_update (statistics,
505 * Handle lookup requests from client
507 * @param cls the closure
508 * @param client the client
509 * @param message the message
512 handle_lookup (void *cls,
513 struct GNUNET_SERVER_Client *client,
514 const struct GNUNET_MessageHeader *message)
516 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
517 struct ClientLookupHandle *clh;
518 char *nameptr = name;
520 const struct GNUNET_CRYPTO_EccPrivateKey *key;
522 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Received `%s' message\n",
527 msg_size = ntohs (message->size);
528 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
534 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
535 GNUNET_SERVER_notification_context_add (nc, client);
536 if (GNUNET_YES == ntohl (sh_msg->have_key))
537 key = &sh_msg->shorten_key;
540 utf_in = (const char *) &sh_msg[1];
541 if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
542 (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
550 clh = GNUNET_new (struct ClientLookupHandle);
551 GNUNET_SERVER_client_set_user_context (client, clh);
552 GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
553 clh->client = client;
554 clh->request_id = sh_msg->id;
555 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
556 (GNUNET_OK != v4_enabled) )
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "LOOKUP: Query for A record but AF_INET not supported!");
560 send_lookup_response (clh, 0, NULL);
563 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
564 (GNUNET_OK != v6_enabled) )
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
567 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
568 send_lookup_response (clh, 0, NULL);
571 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
572 ntohl (sh_msg->type),
575 ntohl (sh_msg->only_cached),
576 &send_lookup_response, clh);
577 GNUNET_STATISTICS_update (statistics,
584 * One of our clients disconnected, clean up after it.
587 * @param client the client that disconnected
590 notify_client_disconnect (void *cls,
591 struct GNUNET_SERVER_Client *client)
593 struct ClientLookupHandle *clh;
597 clh = GNUNET_SERVER_client_get_user_context (client, struct ClientLookupHandle);
600 GNS_resolver_lookup_cancel (clh->lookup);
601 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
607 * Process GNS requests.
610 * @param server the initialized server
611 * @param c configuration to use
614 run (void *cls, struct GNUNET_SERVER_Handle *server,
615 const struct GNUNET_CONFIGURATION_Handle *c)
617 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
618 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
621 struct GNUNET_CRYPTO_EccPublicKey dns_root;
622 unsigned long long max_parallel_bg_queries = 0;
625 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
626 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
628 namestore_handle = GNUNET_NAMESTORE_connect (c);
629 if (NULL == namestore_handle)
631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
632 _("Failed to connect to the namestore!\n"));
633 GNUNET_SCHEDULER_shutdown ();
637 put_interval = INITIAL_PUT_INTERVAL;
638 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
641 GNUNET_CONFIGURATION_get_value_time (c, "gns",
642 "ZONE_PUBLISH_TIME_WINDOW",
643 &zone_publish_time_window))
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Time window for zone iteration: %s\n",
647 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
650 GNUNET_CONFIGURATION_get_value_number (c, "gns",
651 "MAX_PARALLEL_BACKGROUND_QUERIES",
652 &max_parallel_bg_queries))
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Number of allowed parallel background queries: %llu\n",
656 max_parallel_bg_queries);
660 GNUNET_CONFIGURATION_get_value_time (c, "gns",
661 "DEFAULT_LOOKUP_TIMEOUT",
662 &default_lookup_timeout))
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Default lookup timeout: %s\n",
666 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
670 dht_handle = GNUNET_DHT_connect (c,
671 (unsigned int) max_parallel_bg_queries);
672 if (NULL == dht_handle)
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("Could not connect to DHT!\n"));
676 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
681 GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
685 GNUNET_CRYPTO_ecc_public_key_from_string (dns_root_name,
686 strlen (dns_root_name),
689 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
691 _("valid public key required"));
692 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
693 GNUNET_free (dns_root_name);
696 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
697 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
699 GNUNET_free (dns_root_name);
701 GNS_interceptor_init (&dns_root, c))
703 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
707 /* FIXME: install client disconnect handle to clean up pending
708 lookups on client disconnect! */
709 GNS_resolver_init (namestore_handle, dht_handle,
711 max_parallel_bg_queries);
712 GNUNET_SERVER_disconnect_notify (server,
713 ¬ify_client_disconnect,
715 /* Schedule periodic put for our records. */
716 first_zone_iteration = GNUNET_YES;
717 GNUNET_SERVER_add_handlers (server, handlers);
718 statistics = GNUNET_STATISTICS_create ("gns", c);
719 nc = GNUNET_SERVER_notification_context_create (server, 1);
720 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
722 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
723 &shutdown_task, NULL);
728 * The main function for the GNS service.
730 * @param argc number of arguments from the command line
731 * @param argv command line arguments
732 * @return 0 ok, 1 on error
735 main (int argc, char *const *argv)
741 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
746 /* end of gnunet-service-gns.c */