GNUnet  0.10.x
gnunet-service-peerstore.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2014, 2015, 2016 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 "peerstore.h"
30 #include "peerstore_common.h"
31 
32 
36 #define EXPIRED_RECORDS_CLEANUP_INTERVAL 300 /* 5mins */
37 
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
42 
46 static char *db_lib_name;
47 
52 
57 
62 
66 static int in_shutdown;
67 
71 static unsigned int num_clients;
72 
73 
77 static void
79 {
80  if (NULL != db_lib_name)
81  {
84  db_lib_name = NULL;
85  }
86  if (NULL != watchers)
87  {
89  watchers = NULL;
90  }
91  if (NULL != expire_task)
92  {
93  GNUNET_SCHEDULER_cancel(expire_task);
94  expire_task = NULL;
95  }
97 }
98 
99 
105 static void
106 shutdown_task(void *cls)
107 {
109  if (0 == num_clients) /* Only when no connected clients. */
110  do_shutdown();
111 }
112 
113 
114 /* Forward declaration */
115 static void
116 expire_records_continuation(void *cls, int success);
117 
118 
122 static void
124 {
125  int ret;
126 
127  expire_task = NULL;
128  GNUNET_assert(NULL != db);
129  ret = db->expire_records(db->cls,
132  NULL);
133  if (GNUNET_OK != ret)
134  {
135  GNUNET_assert(NULL == expire_task);
136  expire_task = GNUNET_SCHEDULER_add_delayed(
140  NULL);
141  }
142 }
143 
144 
151 static void
152 expire_records_continuation(void *cls, int success)
153 {
154  if (success > 0)
155  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", success);
156  GNUNET_assert(NULL == expire_task);
157  expire_task = GNUNET_SCHEDULER_add_delayed(
161  NULL);
162 }
163 
164 
173 static void *
175  struct GNUNET_SERVICE_Client *client,
176  struct GNUNET_MQ_Handle *mq)
177 {
178  num_clients++;
179  return client;
180 }
181 
182 
191 static int
192 client_disconnect_it(void *cls, const struct GNUNET_HashCode *key, void *value)
193 {
194  if (value == cls)
195  {
197  GNUNET_CONTAINER_multihashmap_remove(watchers, key, value));
198  num_clients++;
199  }
200  return GNUNET_OK;
201 }
202 
203 
210 static void
212  struct GNUNET_SERVICE_Client *client,
213  void *app_cls)
214 {
215  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A client disconnected, cleaning up.\n");
216  if (NULL != watchers)
219  client);
220  num_clients--;
221  if ((0 == num_clients) && in_shutdown)
222  do_shutdown();
223 }
224 
225 
234 static void
235 record_iterator(void *cls,
236  const struct GNUNET_PEERSTORE_Record *record,
237  const char *emsg)
238 {
239  struct GNUNET_PEERSTORE_Record *cls_record = cls;
240  struct GNUNET_MQ_Envelope *env;
241 
242  if (NULL == record)
243  {
244  /* No more records */
245  struct GNUNET_MessageHeader *endmsg;
246 
249  if (NULL == emsg)
250  {
252  }
253  else
254  {
255  GNUNET_break(0);
256  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to iterate: %s\n", emsg);
257  GNUNET_SERVICE_client_drop(cls_record->client);
258  }
259  PEERSTORE_destroy_record(cls_record);
260  return;
261  }
262 
264  record->sub_system,
265  &record->peer,
266  record->key,
267  record->value,
268  record->value_size,
269  record->expiry,
270  0,
273 }
274 
275 
285 static int
286 watch_notifier_it(void *cls, const struct GNUNET_HashCode *key, void *value)
287 {
288  struct GNUNET_PEERSTORE_Record *record = cls;
289  struct GNUNET_SERVICE_Client *client = value;
290  struct GNUNET_MQ_Envelope *env;
291 
292  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found a watcher to update.\n");
294  record->sub_system,
295  &record->peer,
296  record->key,
297  record->value,
298  record->value_size,
299  record->expiry,
300  0,
303  return GNUNET_YES;
304 }
305 
306 
312 static void
314 {
315  struct GNUNET_HashCode keyhash;
316 
317  PEERSTORE_hash_key(record->sub_system, &record->peer, record->key, &keyhash);
319  &keyhash,
321  record);
322 }
323 
324 
331 static void
332 handle_watch_cancel(void *cls, const struct StoreKeyHashMessage *hm)
333 {
334  struct GNUNET_SERVICE_Client *client = cls;
335 
336  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch cancel request.\n");
337  if (GNUNET_OK !=
338  GNUNET_CONTAINER_multihashmap_remove(watchers, &hm->keyhash, client))
339  {
340  GNUNET_break(0);
342  return;
343  }
344  num_clients++;
346 }
347 
348 
355 static void
356 handle_watch(void *cls, const struct StoreKeyHashMessage *hm)
357 {
358  struct GNUNET_SERVICE_Client *client = cls;
359 
360  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received a watch request.\n");
361  num_clients--; /* do not count watchers */
364  &hm->keyhash,
365  client,
368 }
369 
370 
378 static int
379 check_iterate(void *cls, const struct StoreRecordMessage *srm)
380 {
382 
383  record = PEERSTORE_parse_record_message(srm);
384  if (NULL == record)
385  {
386  GNUNET_break(0);
387  return GNUNET_SYSERR;
388  }
389  if (NULL == record->sub_system)
390  {
391  GNUNET_break(0);
392  PEERSTORE_destroy_record(record);
393  return GNUNET_SYSERR;
394  }
395  PEERSTORE_destroy_record(record);
396  return GNUNET_OK;
397 }
398 
399 
406 static void
407 handle_iterate(void *cls, const struct StoreRecordMessage *srm)
408 {
409  struct GNUNET_SERVICE_Client *client = cls;
411 
412  record = PEERSTORE_parse_record_message(srm);
414  "Iterate request: ss `%s', peer `%s', key `%s'\n",
415  record->sub_system,
416  GNUNET_i2s(&record->peer),
417  (NULL == record->key) ? "NULL" : record->key);
418  record->client = client;
419  if (GNUNET_OK !=
420  db->iterate_records(db->cls,
421  record->sub_system,
422  (ntohs(srm->peer_set)) ? &record->peer : NULL,
423  record->key,
425  record))
426  {
427  GNUNET_break(0);
429  PEERSTORE_destroy_record(record);
430  }
431 }
432 
433 
440 static void
441 store_record_continuation(void *cls, int success)
442 {
443  struct GNUNET_PEERSTORE_Record *record = cls;
444 
445  if (GNUNET_OK == success)
446  {
447  watch_notifier(record);
449  }
450  else
451  {
452  GNUNET_break(0);
454  }
455  PEERSTORE_destroy_record(record);
456 }
457 
458 
466 static int
467 check_store(void *cls, const struct StoreRecordMessage *srm)
468 {
470 
471  record = PEERSTORE_parse_record_message(srm);
472  if (NULL == record)
473  {
474  GNUNET_break(0);
475  return GNUNET_SYSERR;
476  }
477  if ((NULL == record->sub_system) || (NULL == record->key))
478  {
479  GNUNET_break(0);
480  PEERSTORE_destroy_record(record);
481  return GNUNET_SYSERR;
482  }
483  PEERSTORE_destroy_record(record);
484  return GNUNET_OK;
485 }
486 
487 
494 static void
495 handle_store(void *cls, const struct StoreRecordMessage *srm)
496 {
497  struct GNUNET_SERVICE_Client *client = cls;
499 
500  record = PEERSTORE_parse_record_message(srm);
501  GNUNET_log(
503  "Received a store request. Sub system `%s' Peer `%s Key `%s' Options: %u.\n",
504  record->sub_system,
505  GNUNET_i2s(&record->peer),
506  record->key,
507  (uint32_t)ntohl(srm->options));
508  record->client = client;
509  if (GNUNET_OK != db->store_record(db->cls,
510  record->sub_system,
511  &record->peer,
512  record->key,
513  record->value,
514  record->value_size,
515  record->expiry,
516  ntohl(srm->options),
518  record))
519  {
520  GNUNET_break(0);
521  PEERSTORE_destroy_record(record);
523  return;
524  }
525 }
526 
527 
535 static void
536 run(void *cls,
537  const struct GNUNET_CONFIGURATION_Handle *c,
539 {
540  char *database;
541 
543  cfg = c;
545  "peerstore",
546  "DATABASE",
547  &database))
548  {
550  "peerstore",
551  "DATABASE");
553  return;
554  }
555  GNUNET_asprintf(&db_lib_name, "libgnunet_plugin_peerstore_%s", database);
556  db = GNUNET_PLUGIN_load(db_lib_name, (void *)cfg);
557  GNUNET_free(database);
558  if (NULL == db)
559  {
561  _("Could not load database backend `%s'\n"),
562  db_lib_name);
564  return;
565  }
569 }
570 
571 
576  "peerstore",
578  &run,
581  NULL,
582  GNUNET_MQ_hd_var_size(store,
584  struct StoreRecordMessage,
585  NULL),
586  GNUNET_MQ_hd_var_size(iterate,
588  struct StoreRecordMessage,
589  NULL),
592  struct StoreKeyHashMessage,
593  NULL),
594  GNUNET_MQ_hd_fixed_size(watch_cancel,
596  struct StoreKeyHashMessage,
597  NULL),
599 
600 
601 /* end of gnunet-service-peerstore.c */
static unsigned int num_clients
Number of connected clients.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_STORE
Store request message.
Helper peerstore functions.
static void record_iterator(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Function called by for each matching record.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
struct GNUNET_PEERSTORE_Record * PEERSTORE_parse_record_message(const struct StoreRecordMessage *srm)
Parses a message carrying a record.
static void cleanup_expired_records(void *cls)
Deletes any expired records from storage.
struct GNUNET_PeerIdentity peer
Peer Identity.
void PEERSTORE_destroy_record(struct GNUNET_PEERSTORE_Record *record)
Free any memory allocated for this record.
static void handle_watch(void *cls, const struct StoreKeyHashMessage *hm)
Handle a watch request from client.
Handle to a service.
Definition: service.c:114
static int client_disconnect_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Search for a disconnected client and remove it.
struct GNUNET_MQ_Handle * GNUNET_SERVICE_client_get_mq(struct GNUNET_SERVICE_Client *c)
Obtain the message queue of c.
Definition: service.c:2424
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
#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL
Watch cancel request.
size_t value_size
Size of value BLOB.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_SERVICE_Client * client
Client from which this record originated.
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE
Iteration request.
int(* store_record)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Store a record in the peerstore.
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
void * cls
Closure to pass to all plugin functions.
Internal representation of the hash map.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *client, struct GNUNET_MQ_Handle *mq)
A client disconnected.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_RECORD
Watch response.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
IPC messages.
void * GNUNET_PLUGIN_unload(const char *library_name, void *arg)
Unload plugin (runs the "done" callback and returns whatever "done" returned).
Definition: plugin.c:255
Handle to a client that is connected to a service.
Definition: service.c:246
#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_RECORD
Iteration record message.
char * key
Record key string.
int(* expire_records)(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
static struct GNUNET_SCHEDULER_Task * expire_task
Task run to clean up expired records.
static void handle_watch_cancel(void *cls, const struct StoreKeyHashMessage *hm)
Handle a watch cancel request from client.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
GNUNET_SERVICE_MAIN("peerstore", GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_var_size(store, GNUNET_MESSAGE_TYPE_PEERSTORE_STORE, struct StoreRecordMessage, NULL), GNUNET_MQ_hd_var_size(iterate, GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE, struct StoreRecordMessage, NULL), GNUNET_MQ_hd_fixed_size(watch, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH, struct StoreKeyHashMessage, NULL), GNUNET_MQ_hd_fixed_size(watch_cancel, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH_CANCEL, struct StoreKeyHashMessage, NULL), GNUNET_MQ_handler_end())
Define "main" method using service macro.
void * value
Record value BLOB.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *client, void *app_cls)
A client disconnected.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static char * value
Value of the record to add/remove.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
uint16_t peer_set
GNUNET_YES if peer id value set, GNUNET_NO otherwise
Definition: peerstore.h:45
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static void shutdown_task(void *cls)
Task run during shutdown.
Trigger a SOFT server shutdown on signals, allowing active non-monitor clients to complete their tran...
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
int GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
struct GNUNET_HashCode keyhash
Hash of a record key.
Definition: peerstore.h:102
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:440
static struct GNUNET_CONTAINER_MultiHashMap * watchers
Hashmap with all watch requests.
void GNUNET_SERVICE_client_mark_monitor(struct GNUNET_SERVICE_Client *c)
Set the &#39;monitor&#39; flag on this client.
Definition: service.c:2394
struct returned by the initialization function of the plugin
A 512-bit hashcode.
void PEERSTORE_hash_key(const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, struct GNUNET_HashCode *ret)
Creates a hash of the given key combination.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
#define GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH
Watch request.
static void do_shutdown()
Perform the actual shutdown operations.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_MQ_Envelope * PEERSTORE_create_record_mq_envelope(const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t value_size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, uint16_t msg_type)
Creates a MQ envelope for a single record.
static int watch
Watch value continuously.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int(* iterate_records)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor iter, void *iter_cls)
Iterate over the records given an optional peer id and/or key.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void handle_store(void *cls, const struct StoreRecordMessage *srm)
Handle a store request from client.
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Peerstore service runner.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
uint32_t options
Options, needed only in case of a store operation.
Definition: peerstore.h:79
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
#define GNUNET_MESSAGE_TYPE_PEERSTORE_ITERATE_END
Iteration end message.
static struct GNUNET_PEERSTORE_PluginFunctions * db
Database handle.
static int check_store(void *cls, const struct StoreRecordMessage *srm)
Check a store request from client.
Allow multiple values with the same key.
Handle to a message queue.
Definition: mq.c:84
static void handle_iterate(void *cls, const struct StoreRecordMessage *srm)
Handle an iterate request from client.
int GNUNET_CONTAINER_multihashmap_get_multiple(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map that match a particular key.
#define EXPIRED_RECORDS_CLEANUP_INTERVAL
Interval for expired records cleanup (in seconds)
configuration data
Definition: configuration.c:83
static void store_record_continuation(void *cls, int success)
Continuation of store_record called by the peerstore plugin.
struct GNUNET_TIME_Absolute expiry
Expiry time of entry.
char * sub_system
Responsible sub system string.
static int check_iterate(void *cls, const struct StoreRecordMessage *srm)
Check an iterate request from client.
static int watch_notifier_it(void *cls, const struct GNUNET_HashCode *key, void *value)
Iterator over all watcher clients to notify them of a new record.
Single PEERSTORE record.
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
static void watch_notifier(struct GNUNET_PEERSTORE_Record *record)
Given a new record, notifies watchers.
Message carrying record key hash.
Definition: peerstore.h:88
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
Message carrying a PEERSTORE record message.
Definition: peerstore.h:36
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:77
void * GNUNET_PLUGIN_load(const char *library_name, void *arg)
Setup plugin (runs the "init" callback and returns whatever "init" returned).
Definition: plugin.c:206
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
static int in_shutdown
Are we in the process of shutting down the service? GNUNET_YES / GNUNET_NO.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
static void expire_records_continuation(void *cls, int success)
Continuation to expire_records called by the peerstore plugin.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
static char * db_lib_name
Database plugin library name.