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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, \
34 filename) GNUNET_log_from_strerror_file (kind, "util", \
40 * How often should we (re)publish each record before
43 #define PUBLISH_OPS_PER_EXPIRATION 4
46 * How many pending DHT operations do we allow at most?
48 #define DHT_QUEUE_LIMIT 2000
51 * How many events may the namestore give us before it has to wait
54 #define NAMESTORE_QUEUE_LIMIT 5
57 * What replication level do we use for DHT PUT operations?
59 #define DHT_GNS_REPLICATION_LEVEL 5
63 * Handle for DHT PUT activity triggered from the namestore monitor.
70 struct DhtPutActivity *next;
75 struct DhtPutActivity *prev;
78 * Handle for the DHT PUT operation.
80 struct GNUNET_DHT_PutHandle *ph;
83 * When was this PUT initiated?
85 struct GNUNET_TIME_Absolute start_date;
90 * Handle to the statistics service
92 static struct GNUNET_STATISTICS_Handle *statistics;
95 * Our handle to the DHT
97 static struct GNUNET_DHT_Handle *dht_handle;
100 * Our handle to the namestore service
102 static struct GNUNET_NAMESTORE_Handle *namestore_handle;
105 * Handle to monitor namestore changes to instant propagation.
107 static struct GNUNET_NAMESTORE_ZoneMonitor *zmon;
110 * Head of monitor activities; kept in a DLL.
112 static struct DhtPutActivity *ma_head;
115 * Tail of monitor activities; kept in a DLL.
117 static struct DhtPutActivity *ma_tail;
120 * Number of entries in the DHT queue #ma_head.
122 static unsigned int ma_queue_length;
125 * Optimize block insertion by caching map of private keys to
126 * public keys in memory?
128 static int cache_keys;
132 * Task run during shutdown.
138 shutdown_task (void *cls)
140 struct DhtPutActivity *ma;
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 while (NULL != (ma = ma_head))
147 GNUNET_DHT_put_cancel (ma->ph);
149 GNUNET_CONTAINER_DLL_remove (ma_head,
154 if (NULL != statistics)
156 GNUNET_STATISTICS_destroy (statistics,
162 GNUNET_NAMESTORE_zone_monitor_stop (zmon);
165 if (NULL != namestore_handle)
167 GNUNET_NAMESTORE_disconnect (namestore_handle);
168 namestore_handle = NULL;
170 if (NULL != dht_handle)
172 GNUNET_DHT_disconnect (dht_handle);
179 * Continuation called from DHT once the PUT operation triggered
180 * by a monitor is done.
182 * @param cls a `struct DhtPutActivity`
185 dht_put_monitor_continuation (void *cls)
187 struct DhtPutActivity *ma = cls;
189 GNUNET_NAMESTORE_zone_monitor_next (zmon,
192 GNUNET_CONTAINER_DLL_remove (ma_head,
200 * Convert namestore records from the internal format to that
201 * suitable for publication (removes private records, converts
202 * to absolute expiration time).
204 * @param rd input records
205 * @param rd_count size of the @a rd and @a rd_public arrays
206 * @param rd_public where to write the converted records
207 * @return number of records written to @a rd_public
210 convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
211 unsigned int rd_count,
212 struct GNUNET_GNSRECORD_Data *rd_public)
214 struct GNUNET_TIME_Absolute now;
215 unsigned int rd_public_count;
218 now = GNUNET_TIME_absolute_get ();
219 for (unsigned int i = 0; i < rd_count; i++)
221 if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
223 if ((0 == (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
224 (rd[i].expiration_time < now.abs_value_us))
225 continue; /* record already expired, skip it */
226 rd_public[rd_public_count++] = rd[i];
228 return rd_public_count;
233 * Store GNS records in the DHT.
235 * @param key key of the zone
236 * @param label label to store under
237 * @param rd_public public record data
238 * @param rd_public_count number of records in @a rd_public
239 * @param ma handle for the PUT operation
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 struct DhtPutActivity *ma)
249 struct GNUNET_GNSRECORD_Block *block;
250 struct GNUNET_HashCode query;
251 struct GNUNET_TIME_Absolute expire;
253 struct GNUNET_DHT_PutHandle *ret;
255 expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
258 block = GNUNET_GNSRECORD_block_create2 (key,
264 block = GNUNET_GNSRECORD_block_create (key,
272 return NULL; /* whoops */
274 block_size = ntohl (block->purpose.size)
275 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)
276 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
277 GNUNET_GNSRECORD_query_from_private_key (key,
280 GNUNET_STATISTICS_update (statistics,
281 "DHT put operations initiated",
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
288 GNUNET_STRINGS_absolute_time_to_string (expire),
289 GNUNET_h2s (&query));
290 ret = GNUNET_DHT_put (dht_handle,
292 DHT_GNS_REPLICATION_LEVEL,
293 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
294 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
298 &dht_put_monitor_continuation,
306 * Process a record that was stored in the namestore
307 * (invoked by the monitor).
309 * @param cls closure, NULL
310 * @param zone private key of the zone; NULL on disconnect
311 * @param label label of the records; NULL on disconnect
312 * @param rd_count number of entries in @a rd array, 0 if label was deleted
313 * @param rd array of records with data to store
316 handle_monitor_event (void *cls,
317 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
319 unsigned int rd_count,
320 const struct GNUNET_GNSRECORD_Data *rd)
322 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
323 unsigned int rd_public_count;
324 struct DhtPutActivity *ma;
327 GNUNET_STATISTICS_update (statistics,
328 "Namestore monitor events received",
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Received %u records for label `%s' via namestore monitor\n",
335 /* filter out records that are not public, and convert to
336 absolute expiration time. */
337 rd_public_count = convert_records_for_export (rd,
340 if (0 == rd_public_count)
342 GNUNET_NAMESTORE_zone_monitor_next (zmon,
344 return; /* nothing to do */
346 ma = GNUNET_new (struct DhtPutActivity);
347 ma->start_date = GNUNET_TIME_absolute_get ();
348 ma->ph = perform_dht_put (zone,
355 /* PUT failed, do not remember operation */
357 GNUNET_NAMESTORE_zone_monitor_next (zmon,
361 GNUNET_CONTAINER_DLL_insert_tail (ma_head,
365 if (ma_queue_length > DHT_QUEUE_LIMIT)
368 GNUNET_CONTAINER_DLL_remove (ma_head,
371 GNUNET_DHT_put_cancel (ma->ph);
373 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
374 "DHT PUT unconfirmed after %s, aborting PUT\n",
375 GNUNET_STRINGS_relative_time_to_string (
376 GNUNET_TIME_absolute_get_duration (ma->start_date),
384 * The zone monitor encountered an IPC error trying to to get in
385 * sync. Restart from the beginning.
390 handle_monitor_error (void *cls)
393 GNUNET_STATISTICS_update (statistics,
394 "Namestore monitor errors encountered",
401 * Performe zonemaster duties: watch namestore, publish records.
404 * @param server the initialized server
405 * @param c configuration to use
409 const struct GNUNET_CONFIGURATION_Handle *c,
410 struct GNUNET_SERVICE_Handle *service)
412 unsigned long long max_parallel_bg_queries = 128;
416 namestore_handle = GNUNET_NAMESTORE_connect (c);
417 if (NULL == namestore_handle)
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 _ ("Failed to connect to the namestore!\n"));
421 GNUNET_SCHEDULER_shutdown ();
424 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (c,
428 GNUNET_CONFIGURATION_get_value_number (c,
430 "MAX_PARALLEL_BACKGROUND_QUERIES",
431 &max_parallel_bg_queries))
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Number of allowed parallel background queries: %llu\n",
435 max_parallel_bg_queries);
437 if (0 == max_parallel_bg_queries)
438 max_parallel_bg_queries = 1;
439 dht_handle = GNUNET_DHT_connect (c,
440 (unsigned int) max_parallel_bg_queries);
441 if (NULL == dht_handle)
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444 _ ("Could not connect to DHT!\n"));
445 GNUNET_SCHEDULER_add_now (&shutdown_task,
450 /* Schedule periodic put for our records. */
451 statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
453 zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
456 &handle_monitor_error,
458 &handle_monitor_event,
462 GNUNET_NAMESTORE_zone_monitor_next (zmon,
463 NAMESTORE_QUEUE_LIMIT - 1);
464 GNUNET_break (NULL != zmon);
465 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
471 * Define "main" method using service macro.
474 ("zonemaster-monitor",
475 GNUNET_SERVICE_OPTION_NONE,
480 GNUNET_MQ_handler_end ());
483 /* end of gnunet-service-zonemaster-monitor.c */