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