aboutsummaryrefslogtreecommitdiff
path: root/src/zonemaster/gnunet-service-zonemaster-monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/zonemaster/gnunet-service-zonemaster-monitor.c')
-rw-r--r--src/zonemaster/gnunet-service-zonemaster-monitor.c460
1 files changed, 0 insertions, 460 deletions
diff --git a/src/zonemaster/gnunet-service-zonemaster-monitor.c b/src/zonemaster/gnunet-service-zonemaster-monitor.c
deleted file mode 100644
index 748a0f342..000000000
--- a/src/zonemaster/gnunet-service-zonemaster-monitor.c
+++ /dev/null
@@ -1,460 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2014, 2017, 2018 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
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
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_dht_service.h"
29#include "gnunet_namestore_service.h"
30#include "gnunet_statistics_service.h"
31
32#define LOG_STRERROR_FILE(kind, syscall, \
33 filename) GNUNET_log_from_strerror_file (kind, "util", \
34 syscall, \
35 filename)
36
37
38/**
39 * How often should we (re)publish each record before
40 * it expires?
41 */
42#define PUBLISH_OPS_PER_EXPIRATION 4
43
44/**
45 * How many pending DHT operations do we allow at most?
46 */
47#define DHT_QUEUE_LIMIT 2000
48
49/**
50 * How many events may the namestore give us before it has to wait
51 * for us to keep up?
52 */
53#define NAMESTORE_QUEUE_LIMIT 5
54
55/**
56 * What replication level do we use for DHT PUT operations?
57 */
58#define DHT_GNS_REPLICATION_LEVEL 5
59
60/**
61 * Handle for DHT PUT activity triggered from the namestore monitor.
62 */
63struct DhtPutActivity
64{
65 /**
66 * Kept in a DLL.
67 */
68 struct DhtPutActivity *next;
69
70 /**
71 * Kept in a DLL.
72 */
73 struct DhtPutActivity *prev;
74
75 /**
76 * Handle for the DHT PUT operation.
77 */
78 struct GNUNET_DHT_PutHandle *ph;
79
80 /**
81 * When was this PUT initiated?
82 */
83 struct GNUNET_TIME_Absolute start_date;
84};
85
86
87/**
88 * Handle to the statistics service
89 */
90static struct GNUNET_STATISTICS_Handle *statistics;
91
92/**
93 * Our handle to the DHT
94 */
95static struct GNUNET_DHT_Handle *dht_handle;
96
97/**
98 * Our handle to the namestore service
99 */
100static struct GNUNET_NAMESTORE_Handle *namestore_handle;
101
102/**
103 * Handle to monitor namestore changes to instant propagation.
104 */
105static struct GNUNET_NAMESTORE_ZoneMonitor *zmon;
106
107/**
108 * Head of monitor activities; kept in a DLL.
109 */
110static struct DhtPutActivity *ma_head;
111
112/**
113 * Tail of monitor activities; kept in a DLL.
114 */
115static struct DhtPutActivity *ma_tail;
116
117/**
118 * Number of entries in the DHT queue #ma_head.
119 */
120static unsigned int ma_queue_length;
121
122/**
123 * Optimize block insertion by caching map of private keys to
124 * public keys in memory?
125 */
126static int cache_keys;
127
128
129/**
130 * Task run during shutdown.
131 *
132 * @param cls unused
133 * @param tc unused
134 */
135static void
136shutdown_task (void *cls)
137{
138 struct DhtPutActivity *ma;
139
140 (void) cls;
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Shutting down!\n");
143 while (NULL != (ma = ma_head))
144 {
145 GNUNET_DHT_put_cancel (ma->ph);
146 ma_queue_length--;
147 GNUNET_CONTAINER_DLL_remove (ma_head,
148 ma_tail,
149 ma);
150 GNUNET_free (ma);
151 }
152 if (NULL != statistics)
153 {
154 GNUNET_STATISTICS_destroy (statistics,
155 GNUNET_NO);
156 statistics = NULL;
157 }
158 if (NULL != zmon)
159 {
160 GNUNET_NAMESTORE_zone_monitor_stop (zmon);
161 zmon = NULL;
162 }
163 if (NULL != namestore_handle)
164 {
165 GNUNET_NAMESTORE_disconnect (namestore_handle);
166 namestore_handle = NULL;
167 }
168 if (NULL != dht_handle)
169 {
170 GNUNET_DHT_disconnect (dht_handle);
171 dht_handle = NULL;
172 }
173}
174
175
176/**
177 * Continuation called from DHT once the PUT operation triggered
178 * by a monitor is done.
179 *
180 * @param cls a `struct DhtPutActivity`
181 */
182static void
183dht_put_monitor_continuation (void *cls)
184{
185 struct DhtPutActivity *ma = cls;
186
187 GNUNET_NAMESTORE_zone_monitor_next (zmon,
188 1);
189 ma_queue_length--;
190 GNUNET_CONTAINER_DLL_remove (ma_head,
191 ma_tail,
192 ma);
193 GNUNET_free (ma);
194}
195
196
197/**
198 * Store GNS records in the DHT.
199 *
200 * @param key key of the zone
201 * @param label label to store under
202 * @param rd_public public record data
203 * @param rd_public_count number of records in @a rd_public
204 * @param ma handle for the PUT operation
205 * @return DHT PUT handle, NULL on error
206 */
207static struct GNUNET_DHT_PutHandle *
208perform_dht_put (const struct GNUNET_IDENTITY_PrivateKey *key,
209 const char *label,
210 const struct GNUNET_GNSRECORD_Data *rd_public,
211 unsigned int rd_public_count,
212 struct GNUNET_TIME_Absolute expire,
213 struct DhtPutActivity *ma)
214{
215 struct GNUNET_GNSRECORD_Block *block;
216 struct GNUNET_HashCode query;
217 size_t block_size;
218 struct GNUNET_DHT_PutHandle *ret;
219
220 if (cache_keys)
221 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create2 (key,
222 expire,
223 label,
224 rd_public,
225 rd_public_count,
226 &block));
227 else
228 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (key,
229 expire,
230 label,
231 rd_public,
232 rd_public_count,
233 &block));
234 if (NULL == block)
235 {
236 GNUNET_break (0);
237 return NULL; /* whoops */
238 }
239 block_size = GNUNET_GNSRECORD_block_get_size (block);
240 GNUNET_GNSRECORD_query_from_private_key (key,
241 label,
242 &query);
243 GNUNET_STATISTICS_update (statistics,
244 "DHT put operations initiated",
245 1,
246 GNUNET_NO);
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
249 rd_public_count,
250 label,
251 GNUNET_STRINGS_absolute_time_to_string (expire),
252 GNUNET_h2s (&query));
253 ret = GNUNET_DHT_put (dht_handle,
254 &query,
255 DHT_GNS_REPLICATION_LEVEL,
256 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
257 GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
258 block_size,
259 block,
260 expire,
261 &dht_put_monitor_continuation,
262 ma);
263 GNUNET_free (block);
264 return ret;
265}
266
267/**
268 * Process a record that was stored in the namestore
269 * (invoked by the monitor).
270 *
271 * @param cls closure, NULL
272 * @param zone private key of the zone; NULL on disconnect
273 * @param label label of the records; NULL on disconnect
274 * @param rd_count number of entries in @a rd array, 0 if label was deleted
275 * @param rd array of records with data to store
276 */
277static void
278handle_monitor_event (void *cls,
279 const struct GNUNET_IDENTITY_PrivateKey *zone,
280 const char *label,
281 unsigned int rd_count,
282 const struct GNUNET_GNSRECORD_Data *rd)
283{
284 struct GNUNET_GNSRECORD_Data rd_public[rd_count];
285 unsigned int rd_public_count;
286 struct DhtPutActivity *ma;
287 struct GNUNET_TIME_Absolute expire;
288 char *emsg;
289
290 (void) cls;
291 GNUNET_STATISTICS_update (statistics,
292 "Namestore monitor events received",
293 1,
294 GNUNET_NO);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Received %u records for label `%s' via namestore monitor\n",
297 rd_count,
298 label);
299 /* filter out records that are not public, and convert to
300 absolute expiration time. */
301 if (GNUNET_OK != GNUNET_GNSRECORD_convert_records_for_export (label,
302 rd,
303 rd_count,
304 rd_public,
305 &rd_public_count,
306 &expire,
307 &emsg))
308 {
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "Zonemaster-monitor failed: %s\n", emsg);
311 GNUNET_free (emsg);
312 GNUNET_NAMESTORE_zone_monitor_next (zmon,
313 1);
314 return; /* nothing to do */
315 }
316 if (0 == rd_public_count)
317 {
318 GNUNET_NAMESTORE_zone_monitor_next (zmon,
319 1);
320 return; /* nothing to do */
321 }
322 ma = GNUNET_new (struct DhtPutActivity);
323 ma->start_date = GNUNET_TIME_absolute_get ();
324 ma->ph = perform_dht_put (zone,
325 label,
326 rd_public,
327 rd_public_count,
328 expire,
329 ma);
330 if (NULL == ma->ph)
331 {
332 /* PUT failed, do not remember operation */
333 GNUNET_free (ma);
334 GNUNET_NAMESTORE_zone_monitor_next (zmon,
335 1);
336 return;
337 }
338 GNUNET_CONTAINER_DLL_insert_tail (ma_head,
339 ma_tail,
340 ma);
341 ma_queue_length++;
342 if (ma_queue_length > DHT_QUEUE_LIMIT)
343 {
344 ma = ma_head;
345 GNUNET_CONTAINER_DLL_remove (ma_head,
346 ma_tail,
347 ma);
348 GNUNET_DHT_put_cancel (ma->ph);
349 ma_queue_length--;
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 "DHT PUT unconfirmed after %s, aborting PUT\n",
352 GNUNET_STRINGS_relative_time_to_string (
353 GNUNET_TIME_absolute_get_duration (ma->start_date),
354 GNUNET_YES));
355 GNUNET_free (ma);
356 }
357}
358
359
360/**
361 * The zone monitor encountered an IPC error trying to to get in
362 * sync. Restart from the beginning.
363 *
364 * @param cls NULL
365 */
366static void
367handle_monitor_error (void *cls)
368{
369 (void) cls;
370 GNUNET_STATISTICS_update (statistics,
371 "Namestore monitor errors encountered",
372 1,
373 GNUNET_NO);
374}
375
376
377/**
378 * Perform zonemaster duties: watch namestore, publish records.
379 *
380 * @param cls closure
381 * @param server the initialized server
382 * @param c configuration to use
383 */
384static void
385run (void *cls,
386 const struct GNUNET_CONFIGURATION_Handle *c,
387 struct GNUNET_SERVICE_Handle *service)
388{
389 unsigned long long max_parallel_bg_queries = 128;
390
391 (void) cls;
392 (void) service;
393 namestore_handle = GNUNET_NAMESTORE_connect (c);
394 if (NULL == namestore_handle)
395 {
396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397 _ ("Failed to connect to the namestore!\n"));
398 GNUNET_SCHEDULER_shutdown ();
399 return;
400 }
401 cache_keys = GNUNET_CONFIGURATION_get_value_yesno (c,
402 "namestore",
403 "CACHE_KEYS");
404 if (GNUNET_OK ==
405 GNUNET_CONFIGURATION_get_value_number (c,
406 "zonemaster",
407 "MAX_PARALLEL_BACKGROUND_QUERIES",
408 &max_parallel_bg_queries))
409 {
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Number of allowed parallel background queries: %llu\n",
412 max_parallel_bg_queries);
413 }
414 if (0 == max_parallel_bg_queries)
415 max_parallel_bg_queries = 1;
416 dht_handle = GNUNET_DHT_connect (c,
417 (unsigned int) max_parallel_bg_queries);
418 if (NULL == dht_handle)
419 {
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
421 _ ("Could not connect to DHT!\n"));
422 GNUNET_SCHEDULER_add_now (&shutdown_task,
423 NULL);
424 return;
425 }
426
427 /* Schedule periodic put for our records. */
428 statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
429 c);
430 zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
431 NULL,
432 GNUNET_NO,
433 &handle_monitor_error,
434 NULL,
435 &handle_monitor_event,
436 NULL,
437 NULL /* sync_cb */,
438 NULL);
439 GNUNET_NAMESTORE_zone_monitor_next (zmon,
440 NAMESTORE_QUEUE_LIMIT - 1);
441 GNUNET_break (NULL != zmon);
442 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
443 NULL);
444}
445
446
447/**
448 * Define "main" method using service macro.
449 */
450GNUNET_SERVICE_MAIN
451 ("zonemaster-monitor",
452 GNUNET_SERVICE_OPTION_NONE,
453 &run,
454 NULL,
455 NULL,
456 NULL,
457 GNUNET_MQ_handler_end ());
458
459
460/* end of gnunet-service-zonemaster-monitor.c */