error handling
[oweals/gnunet.git] / src / zonemaster / gnunet-service-zonemaster-monitor.c
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_CRYPTO_EcdsaPrivateKey *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 = ntohl (block->purpose.size)
275                + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)
276                + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
277   GNUNET_GNSRECORD_query_from_private_key (key,
278                                            label,
279                                            &query);
280   GNUNET_STATISTICS_update (statistics,
281                             "DHT put operations initiated",
282                             1,
283                             GNUNET_NO);
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285               "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
286               rd_public_count,
287               label,
288               GNUNET_STRINGS_absolute_time_to_string (expire),
289               GNUNET_h2s (&query));
290   ret = GNUNET_DHT_put (dht_handle,
291                         &query,
292                         DHT_GNS_REPLICATION_LEVEL,
293                         GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
294                         GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
295                         block_size,
296                         block,
297                         expire,
298                         &dht_put_monitor_continuation,
299                         ma);
300   GNUNET_free (block);
301   return ret;
302 }
303
304
305 /**
306  * Process a record that was stored in the namestore
307  * (invoked by the monitor).
308  *
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
314  */
315 static void
316 handle_monitor_event (void *cls,
317                       const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
318                       const char *label,
319                       unsigned int rd_count,
320                       const struct GNUNET_GNSRECORD_Data *rd)
321 {
322   struct GNUNET_GNSRECORD_Data rd_public[rd_count];
323   unsigned int rd_public_count;
324   struct DhtPutActivity *ma;
325
326   (void) cls;
327   GNUNET_STATISTICS_update (statistics,
328                             "Namestore monitor events received",
329                             1,
330                             GNUNET_NO);
331   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332               "Received %u records for label `%s' via namestore monitor\n",
333               rd_count,
334               label);
335   /* filter out records that are not public, and convert to
336      absolute expiration time. */
337   rd_public_count = convert_records_for_export (rd,
338                                                 rd_count,
339                                                 rd_public);
340   if (0 == rd_public_count)
341   {
342     GNUNET_NAMESTORE_zone_monitor_next (zmon,
343                                         1);
344     return;   /* nothing to do */
345   }
346   ma = GNUNET_new (struct DhtPutActivity);
347   ma->start_date = GNUNET_TIME_absolute_get ();
348   ma->ph = perform_dht_put (zone,
349                             label,
350                             rd,
351                             rd_count,
352                             ma);
353   if (NULL == ma->ph)
354   {
355     /* PUT failed, do not remember operation */
356     GNUNET_free (ma);
357     GNUNET_NAMESTORE_zone_monitor_next (zmon,
358                                         1);
359     return;
360   }
361   GNUNET_CONTAINER_DLL_insert_tail (ma_head,
362                                     ma_tail,
363                                     ma);
364   ma_queue_length++;
365   if (ma_queue_length > DHT_QUEUE_LIMIT)
366   {
367     ma = ma_head;
368     GNUNET_CONTAINER_DLL_remove (ma_head,
369                                  ma_tail,
370                                  ma);
371     GNUNET_DHT_put_cancel (ma->ph);
372     ma_queue_length--;
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),
377                   GNUNET_YES));
378     GNUNET_free (ma);
379   }
380 }
381
382
383 /**
384  * The zone monitor encountered an IPC error trying to to get in
385  * sync. Restart from the beginning.
386  *
387  * @param cls NULL
388  */
389 static void
390 handle_monitor_error (void *cls)
391 {
392   (void) cls;
393   GNUNET_STATISTICS_update (statistics,
394                             "Namestore monitor errors encountered",
395                             1,
396                             GNUNET_NO);
397 }
398
399
400 /**
401  * Performe zonemaster duties: watch namestore, publish records.
402  *
403  * @param cls closure
404  * @param server the initialized server
405  * @param c configuration to use
406  */
407 static void
408 run (void *cls,
409      const struct GNUNET_CONFIGURATION_Handle *c,
410      struct GNUNET_SERVICE_Handle *service)
411 {
412   unsigned long long max_parallel_bg_queries = 128;
413
414   (void) cls;
415   (void) service;
416   namestore_handle = GNUNET_NAMESTORE_connect (c);
417   if (NULL == namestore_handle)
418   {
419     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420                 _ ("Failed to connect to the namestore!\n"));
421     GNUNET_SCHEDULER_shutdown ();
422     return;
423   }
424   cache_keys = GNUNET_CONFIGURATION_get_value_yesno (c,
425                                                      "namestore",
426                                                      "CACHE_KEYS");
427   if (GNUNET_OK ==
428       GNUNET_CONFIGURATION_get_value_number (c,
429                                              "zonemaster",
430                                              "MAX_PARALLEL_BACKGROUND_QUERIES",
431                                              &max_parallel_bg_queries))
432   {
433     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434                 "Number of allowed parallel background queries: %llu\n",
435                 max_parallel_bg_queries);
436   }
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)
442   {
443     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444                 _ ("Could not connect to DHT!\n"));
445     GNUNET_SCHEDULER_add_now (&shutdown_task,
446                               NULL);
447     return;
448   }
449
450   /* Schedule periodic put for our records. */
451   statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
452                                          c);
453   zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
454                                               NULL,
455                                               GNUNET_NO,
456                                               &handle_monitor_error,
457                                               NULL,
458                                               &handle_monitor_event,
459                                               NULL,
460                                               NULL /* sync_cb */,
461                                               NULL);
462   GNUNET_NAMESTORE_zone_monitor_next (zmon,
463                                       NAMESTORE_QUEUE_LIMIT - 1);
464   GNUNET_break (NULL != zmon);
465   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
466                                  NULL);
467 }
468
469
470 /**
471  * Define "main" method using service macro.
472  */
473 GNUNET_SERVICE_MAIN
474   ("zonemaster-monitor",
475   GNUNET_SERVICE_OPTION_NONE,
476   &run,
477   NULL,
478   NULL,
479   NULL,
480   GNUNET_MQ_handler_end ());
481
482
483 /* end of gnunet-service-zonemaster-monitor.c */