GNUnet  0.10.x
gnunet-service-zonemaster-monitor.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_dht_service.h"
31 
32 
33 #define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util", syscall, filename)
34 
35 
40 #define PUBLISH_OPS_PER_EXPIRATION 4
41 
45 #define DHT_QUEUE_LIMIT 2000
46 
51 #define NAMESTORE_QUEUE_LIMIT 5
52 
56 #define DHT_GNS_REPLICATION_LEVEL 5
57 
58 
67 
72 
77 
82 };
83 
84 
89 
94 
99 
104 
108 static struct DhtPutActivity *ma_head;
109 
113 static struct DhtPutActivity *ma_tail;
114 
118 static unsigned int ma_queue_length;
119 
124 static int cache_keys;
125 
126 
133 static void
134 shutdown_task(void *cls)
135 {
136  struct DhtPutActivity *ma;
137 
138  (void)cls;
140  "Shutting down!\n");
141  while (NULL != (ma = ma_head))
142  {
144  ma_queue_length--;
146  ma_tail,
147  ma);
148  GNUNET_free(ma);
149  }
150  if (NULL != statistics)
151  {
152  GNUNET_STATISTICS_destroy(statistics,
153  GNUNET_NO);
154  statistics = NULL;
155  }
156  if (NULL != zmon)
157  {
159  zmon = NULL;
160  }
161  if (NULL != namestore_handle)
162  {
163  GNUNET_NAMESTORE_disconnect(namestore_handle);
164  namestore_handle = NULL;
165  }
166  if (NULL != dht_handle)
167  {
168  GNUNET_DHT_disconnect(dht_handle);
169  dht_handle = NULL;
170  }
171 }
172 
173 
180 static void
182 {
183  struct DhtPutActivity *ma = cls;
184 
186  1);
187  ma_queue_length--;
189  ma_tail,
190  ma);
191  GNUNET_free(ma);
192 }
193 
194 
205 static unsigned int
207  unsigned int rd_count,
208  struct GNUNET_GNSRECORD_Data *rd_public)
209 {
210  struct GNUNET_TIME_Absolute now;
211  unsigned int rd_public_count;
212 
213  rd_public_count = 0;
214  now = GNUNET_TIME_absolute_get();
215  for (unsigned int i = 0; i < rd_count; i++)
216  {
217  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
218  continue;
219  if ((0 == (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
220  (rd[i].expiration_time < now.abs_value_us))
221  continue; /* record already expired, skip it */
222  rd_public[rd_public_count++] = rd[i];
223  }
224  return rd_public_count;
225 }
226 
227 
238 static struct GNUNET_DHT_PutHandle *
240  const char *label,
241  const struct GNUNET_GNSRECORD_Data *rd_public,
242  unsigned int rd_public_count,
243  struct DhtPutActivity *ma)
244 {
245  struct GNUNET_GNSRECORD_Block *block;
246  struct GNUNET_HashCode query;
247  struct GNUNET_TIME_Absolute expire;
248  size_t block_size;
249  struct GNUNET_DHT_PutHandle *ret;
250 
251  expire = GNUNET_GNSRECORD_record_get_expiration_time(rd_public_count,
252  rd_public);
253  if (cache_keys)
254  block = GNUNET_GNSRECORD_block_create2(key,
255  expire,
256  label,
257  rd_public,
258  rd_public_count);
259  else
260  block = GNUNET_GNSRECORD_block_create(key,
261  expire,
262  label,
263  rd_public,
264  rd_public_count);
265  if (NULL == block)
266  {
267  GNUNET_break(0);
268  return NULL; /* whoops */
269  }
270  block_size = ntohl(block->purpose.size)
271  + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)
272  + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
274  label,
275  &query);
276  GNUNET_STATISTICS_update(statistics,
277  "DHT put operations initiated",
278  1,
279  GNUNET_NO);
281  "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
282  rd_public_count,
283  label,
285  GNUNET_h2s(&query));
286  ret = GNUNET_DHT_put(dht_handle,
287  &query,
291  block_size,
292  block,
293  expire,
295  ma);
296  GNUNET_free(block);
297  return ret;
298 }
299 
300 
311 static void
313  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
314  const char *label,
315  unsigned int rd_count,
316  const struct GNUNET_GNSRECORD_Data *rd)
317 {
318  struct GNUNET_GNSRECORD_Data rd_public[rd_count];
319  unsigned int rd_public_count;
320  struct DhtPutActivity *ma;
321 
322  (void)cls;
323  GNUNET_STATISTICS_update(statistics,
324  "Namestore monitor events received",
325  1,
326  GNUNET_NO);
328  "Received %u records for label `%s' via namestore monitor\n",
329  rd_count,
330  label);
331  /* filter out records that are not public, and convert to
332  absolute expiration time. */
333  rd_public_count = convert_records_for_export(rd,
334  rd_count,
335  rd_public);
336  if (0 == rd_public_count)
337  {
339  1);
340  return; /* nothing to do */
341  }
342  ma = GNUNET_new(struct DhtPutActivity);
344  ma->ph = perform_dht_put(zone,
345  label,
346  rd,
347  rd_count,
348  ma);
349  if (NULL == ma->ph)
350  {
351  /* PUT failed, do not remember operation */
352  GNUNET_free(ma);
354  1);
355  return;
356  }
358  ma_tail,
359  ma);
360  ma_queue_length++;
362  {
363  ma = ma_head;
365  ma_tail,
366  ma);
368  ma_queue_length--;
370  "DHT PUT unconfirmed after %s, aborting PUT\n",
372  GNUNET_YES));
373  GNUNET_free(ma);
374  }
375 }
376 
377 
384 static void
386 {
387  (void)cls;
388  GNUNET_STATISTICS_update(statistics,
389  "Namestore monitor errors encountered",
390  1,
391  GNUNET_NO);
392 }
393 
394 
402 static void
403 run(void *cls,
404  const struct GNUNET_CONFIGURATION_Handle *c,
406 {
407  unsigned long long max_parallel_bg_queries = 128;
408 
409  (void)cls;
410  (void)service;
411  namestore_handle = GNUNET_NAMESTORE_connect(c);
412  if (NULL == namestore_handle)
413  {
415  _("Failed to connect to the namestore!\n"));
417  return;
418  }
420  "namestore",
421  "CACHE_KEYS");
422  if (GNUNET_OK ==
424  "zonemaster",
425  "MAX_PARALLEL_BACKGROUND_QUERIES",
426  &max_parallel_bg_queries))
427  {
429  "Number of allowed parallel background queries: %llu\n",
430  max_parallel_bg_queries);
431  }
432  if (0 == max_parallel_bg_queries)
433  max_parallel_bg_queries = 1;
434  dht_handle = GNUNET_DHT_connect(c,
435  (unsigned int)max_parallel_bg_queries);
436  if (NULL == dht_handle)
437  {
439  _("Could not connect to DHT!\n"));
441  NULL);
442  return;
443  }
444 
445  /* Schedule periodic put for our records. */
446  statistics = GNUNET_STATISTICS_create("zonemaster-mon",
447  c);
449  NULL,
450  GNUNET_NO,
452  NULL,
454  NULL,
455  NULL /* sync_cb */,
456  NULL);
459  GNUNET_break(NULL != zmon);
461  NULL);
462 }
463 
464 
469  ("zonemaster-monitor",
471  &run,
472  NULL,
473  NULL,
474  NULL,
476 
477 
478 /* end of gnunet-service-zonemaster-monitor.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
struct GNUNET_DHT_PutHandle * ph
Handle for the DHT PUT operation.
struct DhtPutActivity * prev
Kept in a DLL.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
static int cache_keys
Optimize block insertion by caching map of private keys to public keys in memory? ...
void GNUNET_DHT_put_cancel(struct GNUNET_DHT_PutHandle *ph)
Cancels a DHT PUT operation.
Definition: dht_api.c:1030
static void shutdown_task(void *cls)
Task run during shutdown.
static struct GNUNET_STATISTICS_Handle * statistics
Handle to the statistics service.
static struct GNUNET_NAMESTORE_ZoneMonitor * zmon
Handle to monitor namestore changes to instant propagation.
struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_NAMESTORE_zone_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, int iterate_first, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor monitor, void *monitor_cls, GNUNET_SCHEDULER_TaskCallback sync_cb, void *sync_cb_cls)
Begin monitoring a zone for changes.
struct GNUNET_GNSRECORD_Block * GNUNET_GNSRECORD_block_create(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Sign name and records.
Handle to a service.
Definition: service.c:114
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
struct GNUNET_GNSRECORD_Block * GNUNET_GNSRECORD_block_create2(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Sign name and records, cache derived public key (also keeps the private key in static memory...
struct GNUNET_TIME_Absolute start_date
When was this PUT initiated?
#define DHT_GNS_REPLICATION_LEVEL
What replication level do we use for DHT PUT operations?
void GNUNET_NAMESTORE_zone_monitor_stop(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Stop monitoring a zone for changes.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Private ECC key encoded for transmission.
Information we have in an encrypted block with record data (i.e.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
static int ret
Final status code.
Definition: gnunet-arm.c:89
Handle for the service.
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Handle for DHT PUT activity triggered from the namestore monitor.
Connection to the DHT service.
Definition: dht_api.c:198
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define DHT_QUEUE_LIMIT
How many pending DHT operations do we allow at most?
static char * zone
Name of the zone we manage.
void GNUNET_GNSRECORD_query_from_private_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
static struct GNUNET_NAMESTORE_Handle * namestore_handle
Our handle to the namestore service.
void GNUNET_NAMESTORE_disconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from the namestore service (and free associated resources).
Handle for a monitoring activity.
static void handle_monitor_error(void *cls)
The zone monitor encountered an IPC error trying to to get in sync.
GNUNET_SERVICE_MAIN("zonemaster-monitor", GNUNET_SERVICE_OPTION_NONE, &run, NULL, NULL, NULL, GNUNET_MQ_handler_end())
Define "main" method using service macro.
Connection to the NAMESTORE service.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:686
an ECC signature using ECDSA
This is a private record of this peer and it should thus not be handed out to other peers...
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Block for storing record data.
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Returns the expiration time of the given block of records.
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_DHT_PutHandle * GNUNET_DHT_put(struct GNUNET_DHT_Handle *handle, const struct GNUNET_HashCode *key, uint32_t desired_replication_level, enum GNUNET_DHT_RouteOption options, enum GNUNET_BLOCK_Type type, size_t size, const void *data, struct GNUNET_TIME_Absolute exp, GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
Perform a PUT operation storing data in the DHT.
Definition: dht_api.c:961
#define NAMESTORE_QUEUE_LIMIT
How many events may the namestore give us before it has to wait for us to keep up?
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition: dht_api.c:885
struct GNUNET_NAMESTORE_Handle * GNUNET_NAMESTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the namestore service.
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:913
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
static struct DhtPutActivity * ma_head
Head of monitor activities; kept in a DLL.
Handle to a PUT request.
Definition: dht_api.c:43
static struct DhtPutActivity * ma_tail
Tail of monitor activities; kept in a DLL.
configuration data
Definition: configuration.c:83
static void handle_monitor_event(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namestore (invoked by the monitor).
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:373
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Performe zonemaster duties: watch namestore, publish records.
#define GNUNET_log(kind,...)
static struct GNUNET_DHT_Handle * dht_handle
Our handle to the DHT.
static unsigned int ma_queue_length
Number of entries in the DHT queue ma_head.
static struct GNUNET_DHT_PutHandle * perform_dht_put(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const char *label, const struct GNUNET_GNSRECORD_Data *rd_public, unsigned int rd_public_count, struct DhtPutActivity *ma)
Store GNS records in the DHT.
static unsigned int convert_records_for_export(const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *rd_public)
Convert namestore records from the internal format to that suitable for publication (removes private ...
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
static void dht_put_monitor_continuation(void *cls)
Continuation called from DHT once the PUT operation triggered by a monitor is done.
This flag is currently unused; former RF_PENDING flag.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:741
int GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
struct DhtPutActivity * next
Kept in a DLL.
void GNUNET_NAMESTORE_zone_monitor_next(struct GNUNET_NAMESTORE_ZoneMonitor *zm, uint64_t limit)
Calls the monitor processor specified in GNUNET_NAMESTORE_zone_monitor_start for the next record(s)...
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
Each peer along the way should look at &#39;enc&#39; (otherwise only the k-peers closest to the key should lo...
#define GNUNET_free(ptr)
Wrapper around free.