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