2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2017, 2018 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-monitor.c
23 * @brief monitor namestore changes and publish them immediately to GNUnet name system
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dht_service.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_statistics_service.h"
33 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37 * How often should we (re)publish each record before
40 #define PUBLISH_OPS_PER_EXPIRATION 4
43 * How many pending DHT operations do we allow at most?
45 #define DHT_QUEUE_LIMIT 2000
48 * How many events may the namestore give us before it has to wait
51 #define NAMESTORE_QUEUE_LIMIT 50
54 * What replication level do we use for DHT PUT operations?
56 #define DHT_GNS_REPLICATION_LEVEL 5
60 * Handle for DHT PUT activity triggered from the namestore monitor.
67 struct DhtPutActivity *next;
72 struct DhtPutActivity *prev;
75 * Handle for the DHT PUT operation.
77 struct GNUNET_DHT_PutHandle *ph;
80 * When was this PUT initiated?
82 struct GNUNET_TIME_Absolute start_date;
87 * Handle to the statistics service
89 static struct GNUNET_STATISTICS_Handle *statistics;
92 * Our handle to the DHT
94 static struct GNUNET_DHT_Handle *dht_handle;
97 * Our handle to the namestore service
99 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
102 * Handle to monitor namestore changes to instant propagation.
104 static struct GNUNET_NAMESTORE_ZoneMonitor *zmon;
107 * Head of monitor activities; kept in a DLL.
109 static struct DhtPutActivity *ma_head;
112 * Tail of monitor activities; kept in a DLL.
114 static struct DhtPutActivity *ma_tail;
117 * Number of entries in the DHT queue #ma_head.
119 static unsigned int ma_queue_length;
122 * Optimize block insertion by caching map of private keys to
123 * public keys in memory?
125 static int cache_keys;
129 * Task run during shutdown.
135 shutdown_task (void *cls)
137 struct DhtPutActivity *ma;
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 while (NULL != (ma = ma_head))
144 GNUNET_DHT_put_cancel (ma->ph);
146 GNUNET_CONTAINER_DLL_remove (ma_head,
151 if (NULL != statistics)
153 GNUNET_STATISTICS_destroy (statistics,
159 GNUNET_NAMESTORE_zone_monitor_stop (zmon);
162 if (NULL != namestore_handle)
164 GNUNET_NAMESTORE_disconnect (namestore_handle);
165 namestore_handle = NULL;
167 if (NULL != dht_handle)
169 GNUNET_DHT_disconnect (dht_handle);
176 * Continuation called from DHT once the PUT operation triggered
177 * by a monitor is done.
179 * @param cls a `struct DhtPutActivity`
182 dht_put_monitor_continuation (void *cls)
184 struct DhtPutActivity *ma = cls;
186 GNUNET_NAMESTORE_zone_monitor_next (zmon,
189 GNUNET_CONTAINER_DLL_remove (ma_head,
197 * Convert namestore records from the internal format to that
198 * suitable for publication (removes private records, converts
199 * to absolute expiration time).
201 * @param rd input records
202 * @param rd_count size of the @a rd and @a rd_public arrays
203 * @param rd_public where to write the converted records
204 * @return number of records written to @a rd_public
207 convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
208 unsigned int rd_count,
209 struct GNUNET_GNSRECORD_Data *rd_public)
211 struct GNUNET_TIME_Absolute now;
212 unsigned int rd_public_count;
215 now = GNUNET_TIME_absolute_get ();
216 for (unsigned int i=0;i<rd_count;i++)
217 if (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
219 rd_public[rd_public_count] = rd[i];
220 if (rd_public[rd_public_count].expiration_time < now.abs_value_us)
222 /* record already expired, skip it */
227 return rd_public_count;
232 * Store GNS records in the DHT.
234 * @param key key of the zone
235 * @param label label to store under
236 * @param rd_public public record data
237 * @param rd_public_count number of records in @a rd_public
238 * @param cont function to call with PUT result
239 * @param cont_cls closure for @a cont
240 * @return DHT PUT handle, NULL on error
242 static struct GNUNET_DHT_PutHandle *
243 perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
245 const struct GNUNET_GNSRECORD_Data *rd_public,
246 unsigned int rd_public_count,
247 GNUNET_SCHEDULER_TaskCallback cont,
250 struct GNUNET_GNSRECORD_Block *block;
251 struct GNUNET_HashCode query;
252 struct GNUNET_TIME_Absolute expire;
254 struct GNUNET_DHT_PutHandle *ret;
256 expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
259 block = GNUNET_GNSRECORD_block_create2 (key,
265 block = GNUNET_GNSRECORD_block_create (key,
273 return NULL; /* whoops */
275 block_size = ntohl (block->purpose.size)
276 + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
277 + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
278 GNUNET_GNSRECORD_query_from_private_key (key,
281 GNUNET_STATISTICS_update (statistics,
282 "DHT put operations initiated",
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
289 GNUNET_STRINGS_absolute_time_to_string (expire),
290 GNUNET_h2s (&query));
291 ret = GNUNET_DHT_put (dht_handle,
293 DHT_GNS_REPLICATION_LEVEL,
294 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
295 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
307 * Process a record that was stored in the namestore
308 * (invoked by the monitor).
310 * @param cls closure, NULL
311 * @param zone private key of the zone; NULL on disconnect
312 * @param label label of the records; NULL on disconnect
313 * @param rd_count number of entries in @a rd array, 0 if label was deleted
314 * @param rd array of records with data to store
317 handle_monitor_event (void *cls,
318 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
320 unsigned int rd_count,
321 const struct GNUNET_GNSRECORD_Data *rd)
323 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
324 unsigned int rd_public_count;
325 struct DhtPutActivity *ma;
328 GNUNET_STATISTICS_update (statistics,
329 "Namestore monitor events received",
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Received %u records for label `%s' via namestore monitor\n",
336 /* filter out records that are not public, and convert to
337 absolute expiration time. */
338 rd_public_count = convert_records_for_export (rd,
341 if (0 == rd_public_count)
343 GNUNET_NAMESTORE_zone_monitor_next (zmon,
345 return; /* nothing to do */
347 ma = GNUNET_new (struct DhtPutActivity);
348 ma->start_date = GNUNET_TIME_absolute_get ();
349 ma->ph = perform_dht_put (zone,
353 &dht_put_monitor_continuation,
357 /* PUT failed, do not remember operation */
359 GNUNET_NAMESTORE_zone_monitor_next (zmon,
363 GNUNET_CONTAINER_DLL_insert_tail (ma_head,
367 if (ma_queue_length > DHT_QUEUE_LIMIT)
370 GNUNET_CONTAINER_DLL_remove (ma_head,
373 GNUNET_DHT_put_cancel (ma->ph);
375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
376 "DHT PUT unconfirmed after %s, aborting PUT\n",
377 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (ma->start_date),
385 * The zone monitor encountered an IPC error trying to to get in
386 * sync. Restart from the beginning.
391 handle_monitor_error (void *cls)
394 GNUNET_STATISTICS_update (statistics,
395 "Namestore monitor errors encountered",
402 * Performe zonemaster duties: watch namestore, publish records.
405 * @param server the initialized server
406 * @param c configuration to use
410 const struct GNUNET_CONFIGURATION_Handle *c,
411 struct GNUNET_SERVICE_Handle *service)
413 unsigned long long max_parallel_bg_queries = 128;
417 namestore_handle = GNUNET_NAMESTORE_connect (c);
418 if (NULL == namestore_handle)
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
421 _("Failed to connect to the namestore!\n"));
422 GNUNET_SCHEDULER_shutdown ();
425 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (c,
429 GNUNET_CONFIGURATION_get_value_number (c,
431 "MAX_PARALLEL_BACKGROUND_QUERIES",
432 &max_parallel_bg_queries))
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Number of allowed parallel background queries: %llu\n",
436 max_parallel_bg_queries);
438 if (0 == max_parallel_bg_queries)
439 max_parallel_bg_queries = 1;
440 dht_handle = GNUNET_DHT_connect (c,
441 (unsigned int) max_parallel_bg_queries);
442 if (NULL == dht_handle)
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
445 _("Could not connect to DHT!\n"));
446 GNUNET_SCHEDULER_add_now (&shutdown_task,
451 /* Schedule periodic put for our records. */
452 statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
454 zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
457 &handle_monitor_error,
459 &handle_monitor_event,
463 GNUNET_NAMESTORE_zone_monitor_next (zmon,
464 NAMESTORE_QUEUE_LIMIT - 1);
465 GNUNET_break (NULL != zmon);
466 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
472 * Define "main" method using service macro.
475 ("zonemaster-monitor",
476 GNUNET_SERVICE_OPTION_NONE,
481 GNUNET_MQ_handler_end());
484 /* end of gnunet-service-zonemaster-monitor.c */