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