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
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"
35 #include "gns_common.h"
36 #include "gnunet-service-gns_resolver.h"
37 #include "gnunet-service-gns_interceptor.h"
38 #include "gnunet_protocols.h"
41 * The initial interval in milliseconds btween puts in
44 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
47 * The upper bound for the zone iteration interval in milliseconds
49 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
52 * The default put interval for the zone iteration. In case
55 #define DEFAULT_ZONE_PUBLISH_TIME_WINDOW GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
58 * The factor the current zone iteration interval is divided by for each
59 * additional new record
61 #define LATE_ITERATION_SPEEDUP_FACTOR 2
64 * How long until a DHT PUT attempt should time out?
66 #define DHT_OPERATION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
69 * What replication level do we use for DHT PUT operations?
71 #define DHT_GNS_REPLICATION_LEVEL 5
75 * Handle to a lookup operation from api
77 struct ClientLookupHandle
81 * We keep these in a DLL.
83 struct ClientLookupHandle *next;
86 * We keep these in a DLL.
88 struct ClientLookupHandle *prev;
91 * Handle to the requesting client
93 struct GNUNET_SERVER_Client *client;
96 * Active handle for the lookup.
98 struct GNS_ResolverHandle *lookup;
109 * Our handle to the DHT
111 static struct GNUNET_DHT_Handle *dht_handle;
114 * Active DHT put operation (or NULL)
116 static struct GNUNET_DHT_PutHandle *active_put;
119 * Our handle to the namestore service
121 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
124 * Handle to iterate over our authoritative zone in namestore
126 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
129 * Our notification context.
131 static struct GNUNET_SERVER_NotificationContext *nc;
136 static struct ClientLookupHandle *clh_head;
141 static struct ClientLookupHandle *clh_tail;
144 * Useful for zone update for DHT put
146 static unsigned long long num_public_records;
149 * Last seen record count
151 static unsigned long long last_num_public_records;
154 * Zone iteration PUT interval.
156 static struct GNUNET_TIME_Relative put_interval;
159 * Time window for zone iteration
161 static struct GNUNET_TIME_Relative zone_publish_time_window;
166 static GNUNET_SCHEDULER_TaskIdentifier zone_publish_task;
169 * #GNUNET_YES if zone has never been published before
171 static int first_zone_iteration;
176 static struct GNUNET_TIME_Relative default_lookup_timeout;
179 * #GNUNET_YES if ipv6 is supported
181 static int v6_enabled;
184 * #GNUNET_YES if ipv4 is supported
186 static int v4_enabled;
189 * Handle to the statistics service
191 static struct GNUNET_STATISTICS_Handle *statistics;
195 * Task run during shutdown.
201 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 struct ClientLookupHandle *clh;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 GNUNET_SERVER_notification_context_destroy (nc);
208 while (NULL != (clh = clh_head))
210 GNS_resolver_lookup_cancel (clh->lookup);
211 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
215 GNS_interceptor_done ();
216 GNS_resolver_done ();
217 if (NULL != statistics)
219 GNUNET_STATISTICS_destroy (statistics, GNUNET_NO);
222 if (GNUNET_SCHEDULER_NO_TASK != zone_publish_task)
224 GNUNET_SCHEDULER_cancel (zone_publish_task);
225 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
227 if (NULL != namestore_iter)
229 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
230 namestore_iter = NULL;
232 if (NULL != namestore_handle)
234 GNUNET_NAMESTORE_disconnect (namestore_handle);
235 namestore_handle = NULL;
237 if (NULL != active_put)
239 GNUNET_DHT_put_cancel (active_put);
242 if (NULL != dht_handle)
244 GNUNET_DHT_disconnect (dht_handle);
251 * Method called periodically that triggers iteration over authoritative records
254 * @param tc task context
257 publish_zone_dht_next (void *cls,
258 const struct GNUNET_SCHEDULER_TaskContext *tc)
260 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
261 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
266 * Periodically iterate over our zone and store everything in dht
269 * @param tc task context
272 publish_zone_dht_start (void *cls,
273 const struct GNUNET_SCHEDULER_TaskContext *tc);
277 * Continuation called from DHT once the PUT operation is done.
279 * @param cls closure, NULL
280 * @param success #GNUNET_OK on success
283 dht_put_continuation (void *cls,
286 struct GNUNET_TIME_Relative next_put_interval;
288 num_public_records++;
289 if ( (num_public_records > last_num_public_records) &&
290 (GNUNET_NO == first_zone_iteration) )
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Last record count was lower than current record count. Reducing interval.\n");
294 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
296 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
297 LATE_ITERATION_SPEEDUP_FACTOR);
300 next_put_interval = put_interval;
302 GNUNET_STATISTICS_set (statistics,
303 "Current zone iteration interval (ms)",
304 next_put_interval.rel_value_us / 1000LL,
306 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
307 &publish_zone_dht_next,
313 * Function used to put all records successively into the DHT.
315 * @param cls the closure (NULL)
316 * @param key the private key of the authority (ours)
317 * @param name the name of the records, NULL once the iteration is done
318 * @param rd_count the number of records in @a rd
319 * @param rd the record data
322 put_gns_record (void *cls,
323 const struct GNUNET_CRYPTO_EccPrivateKey *key,
325 unsigned int rd_count,
326 const struct GNUNET_NAMESTORE_RecordData *rd)
328 struct GNUNET_NAMESTORE_Block *block;
329 struct GNUNET_HashCode query;
330 struct GNUNET_TIME_Absolute expire;
335 /* we're done with one iteration, calculate when to do the next one */
336 namestore_iter = NULL;
337 last_num_public_records = num_public_records;
338 first_zone_iteration = GNUNET_NO;
339 if (0 == num_public_records)
342 * If no records are known (startup) or none present
343 * we can safely set the interval to the value for a single
346 put_interval = zone_publish_time_window;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
348 "No records in namestore database.\n");
352 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
355 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
360 GNUNET_STRINGS_relative_time_to_string (put_interval, GNUNET_YES));
361 GNUNET_STATISTICS_set (statistics,
362 "Current zone iteration interval (in ms)",
363 put_interval.rel_value_us / 1000LL,
365 GNUNET_STATISTICS_update (statistics,
366 "Number of zone iterations",
369 GNUNET_STATISTICS_set (statistics,
370 "Number of public records in DHT",
371 last_num_public_records,
373 if (0 == num_public_records)
374 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
375 &publish_zone_dht_start,
378 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
383 /* FIXME: filter out records that are not public! */
385 /* We got a set of records to publish */
388 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
392 expire = GNUNET_NAMESTORE_record_get_expiration_time (rd_count,
394 block = GNUNET_NAMESTORE_block_create (key,
399 block_size = ntohl (block->purpose.size)
400 + sizeof (struct GNUNET_CRYPTO_EccSignature)
401 + sizeof (struct GNUNET_CRYPTO_EccPublicKey);
402 GNUNET_NAMESTORE_query_from_private_key (key,
406 active_put = GNUNET_DHT_put (dht_handle, &query,
407 DHT_GNS_REPLICATION_LEVEL,
408 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
409 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
413 DHT_OPERATION_TIMEOUT,
414 &dht_put_continuation,
416 if (NULL == active_put)
419 dht_put_continuation (NULL, GNUNET_NO);
426 * Periodically iterate over our zone and store everything in dht
429 * @param tc task context
432 publish_zone_dht_start (void *cls,
433 const struct GNUNET_SCHEDULER_TaskContext *tc)
435 zone_publish_task = GNUNET_SCHEDULER_NO_TASK;
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Scheduling DHT zone update!\n");
439 /* start counting again */
440 num_public_records = 0;
441 namestore_iter = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
442 NULL, /* All zones */
448 /* END DHT ZONE PROPAGATION */
452 * Reply to client with the result from our lookup.
454 * @param cls the closure (our client lookup handle)
455 * @param rd_count the number of records
456 * @param rd the record data
459 send_lookup_response (void* cls,
461 const struct GNUNET_NAMESTORE_RecordData *rd)
463 struct ClientLookupHandle *clh = cls;
464 struct GNUNET_GNS_ClientLookupResultMessage *rmsg;
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Sending `%s' message with %d results\n",
472 len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
473 rmsg = GNUNET_malloc (len + sizeof (struct GNUNET_GNS_ClientLookupResultMessage));
474 rmsg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT);
475 rmsg->header.size = htons (len + sizeof(struct GNUNET_GNS_ClientLookupResultMessage));
476 rmsg->id = clh->request_id;
477 rmsg->rd_count = htonl (rd_count);
478 GNUNET_NAMESTORE_records_serialize (rd_count, rd, len,
480 GNUNET_SERVER_notification_context_unicast (nc,
485 GNUNET_SERVER_receive_done (clh->client,
487 GNUNET_CONTAINER_DLL_remove (clh_head, clh_tail, clh);
489 GNUNET_STATISTICS_update (statistics,
490 "Completed lookups", 1,
492 GNUNET_STATISTICS_update (statistics,
500 * Handle lookup requests from client
502 * @param cls the closure
503 * @param client the client
504 * @param message the message
507 handle_lookup (void *cls,
508 struct GNUNET_SERVER_Client *client,
509 const struct GNUNET_MessageHeader *message)
511 char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
512 struct ClientLookupHandle *clh;
513 char *nameptr = name;
515 const struct GNUNET_CRYPTO_EccPrivateKey *key;
517 const struct GNUNET_GNS_ClientLookupMessage *sh_msg;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Received `%s' message\n",
522 msg_size = ntohs (message->size);
523 if (msg_size < sizeof (struct GNUNET_GNS_ClientLookupMessage))
526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
529 sh_msg = (const struct GNUNET_GNS_ClientLookupMessage *) message;
530 GNUNET_SERVER_notification_context_add (nc, client);
531 if (GNUNET_YES == ntohl (sh_msg->have_key))
532 key = &sh_msg->shorten_key;
535 utf_in = (const char *) &sh_msg[1];
536 if ( ('\0' != utf_in[msg_size - sizeof (struct GNUNET_GNS_ClientLookupMessage) - 1]) ||
537 (strlen (utf_in) > GNUNET_DNSPARSER_MAX_NAME_LENGTH) )
540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 GNUNET_STRINGS_utf8_tolower (utf_in, &nameptr);
545 clh = GNUNET_new (struct ClientLookupHandle);
546 GNUNET_CONTAINER_DLL_insert (clh_head, clh_tail, clh);
547 clh->client = client;
548 clh->request_id = sh_msg->id;
549 if ( (GNUNET_DNSPARSER_TYPE_A == ntohl (sh_msg->type)) &&
550 (GNUNET_OK != v4_enabled) )
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "LOOKUP: Query for A record but AF_INET not supported!");
554 send_lookup_response (clh, 0, NULL);
557 if ( (GNUNET_DNSPARSER_TYPE_AAAA == ntohl (sh_msg->type)) &&
558 (GNUNET_OK != v6_enabled) )
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "LOOKUP: Query for AAAA record but AF_INET6 not supported!");
562 send_lookup_response (clh, 0, NULL);
565 clh->lookup = GNS_resolver_lookup (&sh_msg->zone,
566 ntohl (sh_msg->type),
569 ntohl (sh_msg->only_cached),
570 &send_lookup_response, clh);
571 GNUNET_STATISTICS_update (statistics,
578 * Process GNS requests.
581 * @param server the initialized server
582 * @param c configuration to use
585 run (void *cls, struct GNUNET_SERVER_Handle *server,
586 const struct GNUNET_CONFIGURATION_Handle *c)
588 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
589 { &handle_lookup, NULL, GNUNET_MESSAGE_TYPE_GNS_LOOKUP, 0},
592 struct GNUNET_CRYPTO_EccPublicKey dns_root;
593 unsigned long long max_parallel_bg_queries = 0;
594 int ignore_pending = GNUNET_NO;
597 v6_enabled = GNUNET_NETWORK_test_pf (PF_INET6);
598 v4_enabled = GNUNET_NETWORK_test_pf (PF_INET);
600 namestore_handle = GNUNET_NAMESTORE_connect (c);
601 if (NULL == namestore_handle)
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 _("Failed to connect to the namestore!\n"));
605 GNUNET_SCHEDULER_shutdown ();
609 put_interval = INITIAL_PUT_INTERVAL;
610 zone_publish_time_window = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
613 GNUNET_CONFIGURATION_get_value_time (c, "gns",
614 "ZONE_PUBLISH_TIME_WINDOW",
615 &zone_publish_time_window))
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Time window for zone iteration: %s\n",
619 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window, GNUNET_YES));
622 GNUNET_CONFIGURATION_get_value_number (c, "gns",
623 "MAX_PARALLEL_BACKGROUND_QUERIES",
624 &max_parallel_bg_queries))
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Number of allowed parallel background queries: %llu\n",
628 max_parallel_bg_queries);
632 GNUNET_CONFIGURATION_get_value_yesno (c, "gns",
633 "AUTO_IMPORT_CONFIRMATION_REQ"))
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Auto import requires user confirmation\n");
637 ignore_pending = GNUNET_YES;
641 GNUNET_CONFIGURATION_get_value_time (c, "gns",
642 "DEFAULT_LOOKUP_TIMEOUT",
643 &default_lookup_timeout))
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Default lookup timeout: %s\n",
647 GNUNET_STRINGS_relative_time_to_string (default_lookup_timeout,
651 dht_handle = GNUNET_DHT_connect (c,
652 (unsigned int) max_parallel_bg_queries);
653 if (NULL == dht_handle)
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 _("Could not connect to DHT!\n"));
657 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
662 GNS_resolver_init (namestore_handle, dht_handle,
664 max_parallel_bg_queries,
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668 _("Unable to initialize resolver!\n"));
669 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
674 GNUNET_CONFIGURATION_get_value_string (c, "gns", "DNS_ROOT",
678 GNUNET_CRYPTO_ecc_public_key_from_string (dns_root_name,
679 strlen (dns_root_name),
682 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
684 _("valid public key required"));
685 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
686 GNUNET_free (dns_root_name);
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
690 "DNS hijacking with root `%s' enabled. Connecting to DNS service.\n",
692 GNUNET_free (dns_root_name);
694 GNS_interceptor_init (&dns_root, c))
696 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
700 /* FIXME: install client disconnect handle to clean up pending
701 lookups on client disconnect! */
703 /* Schedule periodic put for our records. */
704 first_zone_iteration = GNUNET_YES;
705 GNUNET_SERVER_add_handlers (server, handlers);
706 statistics = GNUNET_STATISTICS_create ("gns", c);
707 nc = GNUNET_SERVER_notification_context_create (server, 1);
708 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
710 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
711 &shutdown_task, NULL);
716 * The main function for the GNS service.
718 * @param argc number of arguments from the command line
719 * @param argv command line arguments
720 * @return 0 ok, 1 on error
723 main (int argc, char *const *argv)
729 GNUNET_SERVICE_run (argc, argv, "gns", GNUNET_SERVICE_OPTION_NONE, &run,
734 /* end of gnunet-service-gns.c */