GNUnet  0.17.5
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 #define LOG_STRERROR_FILE(kind, syscall, \
33  filename) GNUNET_log_from_strerror_file (kind, "util", \
34  syscall, \
35  filename)
36 
37 
42 #define PUBLISH_OPS_PER_EXPIRATION 4
43 
47 #define DHT_QUEUE_LIMIT 2000
48 
53 #define NAMESTORE_QUEUE_LIMIT 5
54 
58 #define DHT_GNS_REPLICATION_LEVEL 5
59 
64 {
69 
74 
79 
84 };
85 
86 
91 
96 
101 
106 
110 static struct DhtPutActivity *ma_head;
111 
115 static struct DhtPutActivity *ma_tail;
116 
120 static unsigned int ma_queue_length;
121 
126 static int cache_keys;
127 
128 
135 static void
136 shutdown_task (void *cls)
137 {
138  struct DhtPutActivity *ma;
139 
140  (void) cls;
142  "Shutting down!\n");
143  while (NULL != (ma = ma_head))
144  {
146  ma_queue_length--;
148  ma_tail,
149  ma);
150  GNUNET_free (ma);
151  }
152  if (NULL != statistics)
153  {
155  GNUNET_NO);
156  statistics = NULL;
157  }
158  if (NULL != zmon)
159  {
161  zmon = NULL;
162  }
163  if (NULL != namestore_handle)
164  {
166  namestore_handle = NULL;
167  }
168  if (NULL != dht_handle)
169  {
171  dht_handle = NULL;
172  }
173 }
174 
175 
182 static void
184 {
185  struct DhtPutActivity *ma = cls;
186 
188  1);
189  ma_queue_length--;
191  ma_tail,
192  ma);
193  GNUNET_free (ma);
194 }
195 
196 
207 static struct GNUNET_DHT_PutHandle *
209  const char *label,
210  const struct GNUNET_GNSRECORD_Data *rd_public,
211  unsigned int rd_public_count,
213  struct DhtPutActivity *ma)
214 {
215  struct GNUNET_GNSRECORD_Block *block;
216  struct GNUNET_HashCode query;
217  size_t block_size;
218  struct GNUNET_DHT_PutHandle *ret;
219 
220  if (cache_keys)
222  expire,
223  label,
224  rd_public,
225  rd_public_count,
226  &block));
227  else
229  expire,
230  label,
231  rd_public,
232  rd_public_count,
233  &block));
234  if (NULL == block)
235  {
236  GNUNET_break (0);
237  return NULL; /* whoops */
238  }
239  block_size = GNUNET_GNSRECORD_block_get_size (block);
241  label,
242  &query);
244  "DHT put operations initiated",
245  1,
246  GNUNET_NO);
248  "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
249  rd_public_count,
250  label,
252  GNUNET_h2s (&query));
254  &query,
258  block_size,
259  block,
260  expire,
262  ma);
263  GNUNET_free (block);
264  return ret;
265 }
266 
277 static void
279  const struct GNUNET_IDENTITY_PrivateKey *zone,
280  const char *label,
281  unsigned int rd_count,
282  const struct GNUNET_GNSRECORD_Data *rd)
283 {
284  struct GNUNET_GNSRECORD_Data rd_public[rd_count];
285  unsigned int rd_public_count;
286  struct DhtPutActivity *ma;
288  char *emsg;
289 
290  (void) cls;
292  "Namestore monitor events received",
293  1,
294  GNUNET_NO);
296  "Received %u records for label `%s' via namestore monitor\n",
297  rd_count,
298  label);
299  /* filter out records that are not public, and convert to
300  absolute expiration time. */
302  rd,
303  rd_count,
304  rd_public,
305  &rd_public_count,
306  &expire,
307  &emsg))
308  {
310  "Zonemaster-monitor failed: %s\n", emsg);
311  GNUNET_free (emsg);
313  1);
314  return; /* nothing to do */
315  }
316  if (0 == rd_public_count)
317  {
319  1);
320  return; /* nothing to do */
321  }
322  ma = GNUNET_new (struct DhtPutActivity);
324  ma->ph = perform_dht_put (zone,
325  label,
326  rd_public,
327  rd_public_count,
328  expire,
329  ma);
330  if (NULL == ma->ph)
331  {
332  /* PUT failed, do not remember operation */
333  GNUNET_free (ma);
335  1);
336  return;
337  }
339  ma_tail,
340  ma);
341  ma_queue_length++;
343  {
344  ma = ma_head;
346  ma_tail,
347  ma);
349  ma_queue_length--;
351  "DHT PUT unconfirmed after %s, aborting PUT\n",
354  GNUNET_YES));
355  GNUNET_free (ma);
356  }
357 }
358 
359 
366 static void
368 {
369  (void) cls;
371  "Namestore monitor errors encountered",
372  1,
373  GNUNET_NO);
374 }
375 
376 
384 static void
385 run (void *cls,
386  const struct GNUNET_CONFIGURATION_Handle *c,
388 {
389  unsigned long long max_parallel_bg_queries = 128;
390 
391  (void) cls;
392  (void) service;
394  if (NULL == namestore_handle)
395  {
397  _ ("Failed to connect to the namestore!\n"));
399  return;
400  }
402  "namestore",
403  "CACHE_KEYS");
404  if (GNUNET_OK ==
406  "zonemaster",
407  "MAX_PARALLEL_BACKGROUND_QUERIES",
408  &max_parallel_bg_queries))
409  {
411  "Number of allowed parallel background queries: %llu\n",
412  max_parallel_bg_queries);
413  }
414  if (0 == max_parallel_bg_queries)
415  max_parallel_bg_queries = 1;
417  (unsigned int) max_parallel_bg_queries);
418  if (NULL == dht_handle)
419  {
421  _ ("Could not connect to DHT!\n"));
423  NULL);
424  return;
425  }
426 
427  /* Schedule periodic put for our records. */
428  statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
429  c);
431  NULL,
432  GNUNET_NO,
434  NULL,
436  NULL,
437  NULL /* sync_cb */,
438  NULL);
441  GNUNET_break (NULL != zmon);
443  NULL);
444 }
445 
446 
451  ("zonemaster-monitor",
453  &run,
454  NULL,
455  NULL,
456  NULL,
458 
459 
460 /* end of gnunet-service-zonemaster-monitor.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_HashCode key
The key used in the DHT.
static char * expire
DID Document expiration Date Attribut String.
Definition: gnunet-did.c:101
static char * zone
Name of the zone being managed.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static void handle_monitor_event(void *cls, const struct GNUNET_IDENTITY_PrivateKey *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).
GNUNET_SERVICE_MAIN("zonemaster-monitor", GNUNET_SERVICE_OPTION_NONE, &run, NULL, NULL, NULL, GNUNET_MQ_handler_end())
Define "main" method using service macro.
static struct GNUNET_DHT_PutHandle * perform_dht_put(const struct GNUNET_IDENTITY_PrivateKey *key, const char *label, const struct GNUNET_GNSRECORD_Data *rd_public, unsigned int rd_public_count, struct GNUNET_TIME_Absolute expire, struct DhtPutActivity *ma)
Store GNS records in the DHT.
#define DHT_QUEUE_LIMIT
How many pending DHT operations do we allow at most?
static void handle_monitor_error(void *cls)
The zone monitor encountered an IPC error trying to to get in sync.
static struct GNUNET_NAMESTORE_Handle * namestore_handle
Our handle to the namestore service.
static void shutdown_task(void *cls)
Task run during shutdown.
static struct GNUNET_STATISTICS_Handle * statistics
Handle to the statistics service.
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 void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Perform zonemaster duties: watch namestore, publish records.
static int cache_keys
Optimize block insertion by caching map of private keys to public keys in memory?
#define DHT_GNS_REPLICATION_LEVEL
What replication level do we use for DHT PUT operations?
static void dht_put_monitor_continuation(void *cls)
Continuation called from DHT once the PUT operation triggered by a monitor is done.
static struct DhtPutActivity * ma_tail
Tail of monitor activities; kept in a DLL.
static struct DhtPutActivity * ma_head
Head of monitor activities; kept in a DLL.
#define NAMESTORE_QUEUE_LIMIT
How many events may the namestore give us before it has to wait for us to keep up?
static struct GNUNET_NAMESTORE_ZoneMonitor * zmon
Handle to monitor namestore changes to instant propagation.
@ GNUNET_BLOCK_TYPE_GNS_NAMERECORD
Block for storing GNS record data.
API to the DHT service.
API that can be used to store naming information on a GNUnet node;.
API to create, modify and access statistics.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue 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".
void GNUNET_DHT_put_cancel(struct GNUNET_DHT_PutHandle *ph)
Cancels a DHT PUT operation.
Definition: dht_api.c:1148
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:1039
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:1090
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:1062
@ GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE
Each peer along the way should process the request (otherwise only peers locally closest to the key w...
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
void GNUNET_GNSRECORD_query_from_private_key(const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_create2(const struct GNUNET_IDENTITY_PrivateKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count, struct GNUNET_GNSRECORD_Block **result)
Sign name and records, cache derived public key (also keeps the private key in static memory,...
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_create(const struct GNUNET_IDENTITY_PrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count, struct GNUNET_GNSRECORD_Block **result)
Sign name and records.
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_convert_records_for_export(const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *rd_public, unsigned int *rd_count_public, struct GNUNET_TIME_Absolute *expiry, char **emsg)
Convert namestore records from the internal format to that suitable for publication (removes private ...
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_NO
Definition: gnunet_common.h:98
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
void GNUNET_NAMESTORE_disconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from the namestore service (and free associated resources).
void GNUNET_NAMESTORE_zone_monitor_stop(struct GNUNET_NAMESTORE_ZoneMonitor *zm)
Stop monitoring a zone for changes.
struct GNUNET_NAMESTORE_Handle * GNUNET_NAMESTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the namestore service.
struct GNUNET_NAMESTORE_ZoneMonitor * GNUNET_NAMESTORE_zone_monitor_start(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_IDENTITY_PrivateKey *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.
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).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:533
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:1281
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,...
Definition: scheduler.c:1316
@ GNUNET_SERVICE_OPTION_NONE
Use defaults.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
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_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
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:435
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:110
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:617
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:570
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Handle for DHT PUT activity triggered from the namestore monitor.
struct DhtPutActivity * prev
Kept in a DLL.
struct DhtPutActivity * next
Kept in a DLL.
struct GNUNET_DHT_PutHandle * ph
Handle for the DHT PUT operation.
struct GNUNET_TIME_Absolute start_date
When was this PUT initiated?
Connection to the DHT service.
Definition: dht_api.c:237
Handle to a PUT request.
Definition: dht_api.c:45
A 512-bit hashcode.
A private key for an identity as per LSD0001.
Connection to the NAMESTORE service.
Handle for a monitoring activity.
Handle to a service.
Definition: service.c:117
Handle for the service.
Time for absolute times used by GNUnet, in microseconds.