2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file zonemaster/gnunet-service-zonemaster.c
23 * @brief publish records from namestore to GNUnet name system
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dnsparser_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_namestore_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_namestore_plugin.h"
33 #include "gnunet_signatures.h"
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
41 * The initial interval in milliseconds btween puts in
44 #define INITIAL_PUT_INTERVAL GNUNET_TIME_UNIT_MILLISECONDS
47 * The lower bound for the zone iteration interval
49 #define MINIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_UNIT_SECONDS
52 * The upper bound for the zone iteration interval
54 #define MAXIMUM_ZONE_ITERATION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
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 for DHT PUT activity triggered from the namestore monitor.
82 struct MonitorActivity
87 struct MonitorActivity *next;
92 struct MonitorActivity *prev;
95 * Handle for the DHT PUT operation.
97 struct GNUNET_DHT_PutHandle *ph;
102 * Handle to the statistics service
104 static struct GNUNET_STATISTICS_Handle *statistics;
107 * Our handle to the DHT
109 static struct GNUNET_DHT_Handle *dht_handle;
112 * Active DHT put operation (or NULL)
114 static struct GNUNET_DHT_PutHandle *active_put;
117 * Our handle to the namestore service
119 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
122 * Handle to iterate over our authoritative zone in namestore
124 static struct GNUNET_NAMESTORE_ZoneIterator *namestore_iter;
127 * Handle to monitor namestore changes to instant propagation.
129 static struct GNUNET_NAMESTORE_ZoneMonitor *zmon;
132 * Head of monitor activities; kept in a DLL.
134 static struct MonitorActivity *ma_head;
137 * Tail of monitor activities; kept in a DLL.
139 static struct MonitorActivity *ma_tail;
142 * Useful for zone update for DHT put
144 static unsigned long long num_public_records;
147 * Last seen record count
149 static unsigned long long last_num_public_records;
152 * Minimum relative expiration time of records seem during the current
155 static struct GNUNET_TIME_Relative min_relative_record_time;
158 * Zone iteration PUT interval.
160 static struct GNUNET_TIME_Relative put_interval;
163 * Default time window for zone iteration
165 static struct GNUNET_TIME_Relative zone_publish_time_window_default;
168 * Time window for zone iteration, adjusted based on relative record
169 * expiration times in our zone.
171 static struct GNUNET_TIME_Relative zone_publish_time_window;
176 static struct GNUNET_SCHEDULER_Task *zone_publish_task;
179 * #GNUNET_YES if zone has never been published before
181 static int first_zone_iteration;
184 * Task run during shutdown.
190 shutdown_task (void *cls)
192 struct MonitorActivity *ma;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 while (NULL != (ma = ma_head))
198 GNUNET_DHT_put_cancel (ma->ph);
199 GNUNET_CONTAINER_DLL_remove (ma_head,
204 if (NULL != statistics)
206 GNUNET_STATISTICS_destroy (statistics,
210 if (NULL != zone_publish_task)
212 GNUNET_SCHEDULER_cancel (zone_publish_task);
213 zone_publish_task = NULL;
215 if (NULL != namestore_iter)
217 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
218 namestore_iter = NULL;
222 GNUNET_NAMESTORE_zone_monitor_stop (zmon);
225 if (NULL != namestore_handle)
227 GNUNET_NAMESTORE_disconnect (namestore_handle);
228 namestore_handle = NULL;
230 if (NULL != active_put)
232 GNUNET_DHT_put_cancel (active_put);
235 if (NULL != dht_handle)
237 GNUNET_DHT_disconnect (dht_handle);
244 * Method called periodically that triggers iteration over authoritative records
249 publish_zone_dht_next (void *cls)
251 zone_publish_task = NULL;
252 GNUNET_assert (NULL != namestore_iter);
253 GNUNET_NAMESTORE_zone_iterator_next (namestore_iter);
258 * Periodically iterate over our zone and store everything in dht
263 publish_zone_dht_start (void *cls);
267 * Continuation called from DHT once the PUT operation is done.
269 * @param cls closure, NULL if called from regular iteration,
270 * `struct MonitorActivity` if called from #handle_monitor_event.
271 * @param success #GNUNET_OK on success
274 dht_put_continuation (void *cls,
277 struct MonitorActivity *ma = cls;
278 struct GNUNET_TIME_Relative next_put_interval;
280 num_public_records++;
284 if ( (num_public_records > last_num_public_records) &&
285 (GNUNET_NO == first_zone_iteration) )
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Last record count was lower than current record count. Reducing interval.\n");
289 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
291 next_put_interval = GNUNET_TIME_relative_divide (put_interval,
292 LATE_ITERATION_SPEEDUP_FACTOR);
295 next_put_interval = put_interval;
296 next_put_interval = GNUNET_TIME_relative_min (next_put_interval,
297 MAXIMUM_ZONE_ITERATION_INTERVAL);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "PUT complete, next PUT in %s!\n",
300 GNUNET_STRINGS_relative_time_to_string (next_put_interval,
303 GNUNET_STATISTICS_set (statistics,
304 "Current zone iteration interval (ms)",
305 next_put_interval.rel_value_us / 1000LL,
307 GNUNET_assert (NULL == zone_publish_task);
308 zone_publish_task = GNUNET_SCHEDULER_add_delayed (next_put_interval,
309 &publish_zone_dht_next,
314 GNUNET_CONTAINER_DLL_remove (ma_head,
323 * Convert namestore records from the internal format to that
324 * suitable for publication (removes private records, converts
325 * to absolute expiration time).
327 * @param rd input records
328 * @param rd_count size of the @a rd and @a rd_public arrays
329 * @param rd_public where to write the converted records
330 * @return number of records written to @a rd_public
333 convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
334 unsigned int rd_count,
335 struct GNUNET_GNSRECORD_Data *rd_public)
337 struct GNUNET_TIME_Absolute now;
338 unsigned int rd_public_count;
342 now = GNUNET_TIME_absolute_get ();
343 for (i=0;i<rd_count;i++)
344 if (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
346 rd_public[rd_public_count] = rd[i];
347 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
349 /* GNUNET_GNSRECORD_block_create will convert to absolute time;
350 we just need to adjust our iteration frequency */
351 min_relative_record_time.rel_value_us =
352 GNUNET_MIN (rd_public[rd_public_count].expiration_time,
353 min_relative_record_time.rel_value_us);
355 else if (rd_public[rd_public_count].expiration_time < now.abs_value_us)
357 /* record already expired, skip it */
362 return rd_public_count;
367 * Store GNS records in the DHT.
369 * @param key key of the zone
370 * @param label label to store under
371 * @param rd_public public record data
372 * @param rd_public_count number of records in @a rd_public
373 * @param pc_arg closure argument to pass to the #dht_put_continuation
374 * @return DHT PUT handle, NULL on error
376 static struct GNUNET_DHT_PutHandle *
377 perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
379 const struct GNUNET_GNSRECORD_Data *rd_public,
380 unsigned int rd_public_count,
383 struct GNUNET_GNSRECORD_Block *block;
384 struct GNUNET_HashCode query;
385 struct GNUNET_TIME_Absolute expire;
387 struct GNUNET_DHT_PutHandle *ret;
389 expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
391 block = GNUNET_GNSRECORD_block_create (key,
397 return NULL; /* whoops */
398 block_size = ntohl (block->purpose.size)
399 + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
400 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
401 GNUNET_GNSRECORD_query_from_private_key (key,
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
408 GNUNET_STRINGS_absolute_time_to_string (expire),
409 GNUNET_h2s (&query));
410 ret = GNUNET_DHT_put (dht_handle,
412 DHT_GNS_REPLICATION_LEVEL,
413 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
414 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
418 &dht_put_continuation,
426 * We encountered an error in our zone iteration.
429 zone_iteration_error (void *cls)
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Got disconnected from namestore database, retrying.\n");
433 namestore_iter = NULL;
434 /* We end up here on error/disconnect/shutdown, so potentially
435 while a zone publish task or a DHT put is still running; hence
436 we need to cancel those. */
437 if (NULL != zone_publish_task)
439 GNUNET_SCHEDULER_cancel (zone_publish_task);
440 zone_publish_task = NULL;
442 if (NULL != active_put)
444 GNUNET_DHT_put_cancel (active_put);
447 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
453 * Zone iteration is completed.
456 zone_iteration_finished (void *cls)
458 /* we're done with one iteration, calculate when to do the next one */
459 namestore_iter = NULL;
460 last_num_public_records = num_public_records;
461 first_zone_iteration = GNUNET_NO;
462 if (0 == num_public_records)
465 * If no records are known (startup) or none present
466 * we can safely set the interval to the value for a single
469 put_interval = zone_publish_time_window;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
471 "No records in namestore database.\n");
475 /* If records are present, next publication is based on the minimum
476 * relative expiration time of the records published divided by 4
478 zone_publish_time_window
479 = GNUNET_TIME_relative_min (GNUNET_TIME_relative_divide (min_relative_record_time, 4),
480 zone_publish_time_window_default);
481 put_interval = GNUNET_TIME_relative_divide (zone_publish_time_window,
484 /* reset for next iteration */
485 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
486 put_interval = GNUNET_TIME_relative_max (MINIMUM_ZONE_ITERATION_INTERVAL,
488 put_interval = GNUNET_TIME_relative_min (put_interval,
489 MAXIMUM_ZONE_ITERATION_INTERVAL);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Zone iteration finished. Adjusted zone iteration interval to %s\n",
492 GNUNET_STRINGS_relative_time_to_string (put_interval,
494 GNUNET_STATISTICS_set (statistics,
495 "Current zone iteration interval (in ms)",
496 put_interval.rel_value_us / 1000LL,
498 GNUNET_STATISTICS_update (statistics,
499 "Number of zone iterations",
502 GNUNET_STATISTICS_set (statistics,
503 "Number of public records in DHT",
504 last_num_public_records,
506 GNUNET_assert (NULL == zone_publish_task);
507 if (0 == num_public_records)
508 zone_publish_task = GNUNET_SCHEDULER_add_delayed (put_interval,
509 &publish_zone_dht_start,
512 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
518 * Function used to put all records successively into the DHT.
520 * @param cls the closure (NULL)
521 * @param key the private key of the authority (ours)
522 * @param label the name of the records, NULL once the iteration is done
523 * @param rd_count the number of records in @a rd
524 * @param rd the record data
527 put_gns_record (void *cls,
528 const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
530 unsigned int rd_count,
531 const struct GNUNET_GNSRECORD_Data *rd)
533 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
534 unsigned int rd_public_count;
536 rd_public_count = convert_records_for_export (rd,
540 if (0 == rd_public_count)
542 GNUNET_assert (NULL == zone_publish_task);
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Record set empty, moving to next record set\n");
545 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_next,
549 /* We got a set of records to publish */
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
551 "Starting DHT PUT\n");
552 active_put = perform_dht_put (key,
557 if (NULL == active_put)
560 dht_put_continuation (NULL, GNUNET_NO);
566 * Periodically iterate over all zones and store everything in DHT
571 publish_zone_dht_start (void *cls)
573 zone_publish_task = NULL;
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Starting DHT zone update!\n");
577 /* start counting again */
578 num_public_records = 0;
579 GNUNET_assert (NULL == namestore_iter);
581 = GNUNET_NAMESTORE_zone_iteration_start (namestore_handle,
582 NULL, /* All zones */
583 &zone_iteration_error,
587 &zone_iteration_finished,
593 * Process a record that was stored in the namestore
594 * (invoked by the monitor).
596 * @param cls closure, NULL
597 * @param zone private key of the zone; NULL on disconnect
598 * @param label label of the records; NULL on disconnect
599 * @param rd_count number of entries in @a rd array, 0 if label was deleted
600 * @param rd array of records with data to store
603 handle_monitor_event (void *cls,
604 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
606 unsigned int rd_count,
607 const struct GNUNET_GNSRECORD_Data *rd)
609 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
610 unsigned int rd_public_count;
611 struct MonitorActivity *ma;
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Received %u records for label `%s' via namestore monitor\n",
617 /* filter out records that are not public, and convert to
618 absolute expiration time. */
619 rd_public_count = convert_records_for_export (rd,
622 if (0 == rd_public_count)
623 return; /* nothing to do */
624 ma = GNUNET_new (struct MonitorActivity);
625 ma->ph = perform_dht_put (zone,
632 /* PUT failed, do not remember operation */
636 GNUNET_CONTAINER_DLL_insert (ma_head,
643 * The zone monitor is now in SYNC with the current state of the
644 * name store. Start to perform periodic iterations.
649 monitor_sync_event (void *cls)
651 GNUNET_assert (NULL == zone_publish_task);
652 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
658 * The zone monitor is now in SYNC with the current state of the
659 * name store. Start to perform periodic iterations.
664 handle_monitor_error (void *cls)
666 if (NULL != zone_publish_task)
668 GNUNET_SCHEDULER_cancel (zone_publish_task);
669 zone_publish_task = NULL;
671 if (NULL != namestore_iter)
673 GNUNET_NAMESTORE_zone_iteration_stop (namestore_iter);
674 namestore_iter = NULL;
676 if (NULL != active_put)
678 GNUNET_DHT_put_cancel (active_put);
681 zone_publish_task = GNUNET_SCHEDULER_add_now (&publish_zone_dht_start,
687 * Performe zonemaster duties: watch namestore, publish records.
690 * @param server the initialized server
691 * @param c configuration to use
695 const struct GNUNET_CONFIGURATION_Handle *c,
696 struct GNUNET_SERVICE_Handle *service)
698 unsigned long long max_parallel_bg_queries = 128;
700 min_relative_record_time = GNUNET_TIME_UNIT_FOREVER_REL;
701 namestore_handle = GNUNET_NAMESTORE_connect (c);
702 if (NULL == namestore_handle)
704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
705 _("Failed to connect to the namestore!\n"));
706 GNUNET_SCHEDULER_shutdown ();
710 put_interval = INITIAL_PUT_INTERVAL;
711 zone_publish_time_window_default = DEFAULT_ZONE_PUBLISH_TIME_WINDOW;
713 GNUNET_CONFIGURATION_get_value_time (c,
715 "ZONE_PUBLISH_TIME_WINDOW",
716 &zone_publish_time_window_default))
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Time window for zone iteration: %s\n",
720 GNUNET_STRINGS_relative_time_to_string (zone_publish_time_window,
723 zone_publish_time_window = zone_publish_time_window_default;
725 GNUNET_CONFIGURATION_get_value_number (c,
727 "MAX_PARALLEL_BACKGROUND_QUERIES",
728 &max_parallel_bg_queries))
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Number of allowed parallel background queries: %llu\n",
732 max_parallel_bg_queries);
734 if (0 == max_parallel_bg_queries)
735 max_parallel_bg_queries = 1;
736 dht_handle = GNUNET_DHT_connect (c,
737 (unsigned int) max_parallel_bg_queries);
738 if (NULL == dht_handle)
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 _("Could not connect to DHT!\n"));
742 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
746 /* Schedule periodic put for our records. */
747 first_zone_iteration = GNUNET_YES;\
748 statistics = GNUNET_STATISTICS_create ("zonemaster", c);
749 zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
752 &handle_monitor_error,
754 &handle_monitor_event,
758 GNUNET_break (NULL != zmon);
759 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
764 * Define "main" method using service macro.
768 GNUNET_SERVICE_OPTION_NONE,
773 GNUNET_MQ_handler_end());
776 /* end of gnunet-service-zonemaster.c */