GNUnet  0.10.x
plugin_peerstore_flat.c
Go to the documentation of this file.
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2015 Christian Grothoff (and other contributing authors)
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 
27 #include "platform.h"
30 #include "peerstore.h"
31 
35 struct Plugin
36 {
37 
41  const struct GNUNET_CONFIGURATION_Handle *cfg;
42 
47 
52 
56  void *iter_cls;
57 
61  const char *iter_key;
62 
67 
71  const char *iter_sub_system;
72 
77 
81  uint64_t deleted_entries;
82 
86  uint64_t exp_changes;
87 
91  char *fn;
92 
97 
98 };
99 
100 
101 static int
102 delete_entries (void *cls,
103  const struct GNUNET_HashCode *key,
104  void *value)
105 {
106  struct Plugin *plugin = cls;
107  struct GNUNET_PEERSTORE_Record *entry = value;
108  if (0 != strcmp (plugin->iter_key, entry->key))
109  return GNUNET_YES;
110  if (0 != memcmp (plugin->iter_peer,
111  &entry->peer,
112  sizeof (struct GNUNET_PeerIdentity)))
113  return GNUNET_YES;
114  if (0 != strcmp (plugin->iter_sub_system, entry->sub_system))
115  return GNUNET_YES;
116 
117  GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value);
118  plugin->deleted_entries++;
119  return GNUNET_YES;
120 }
121 
122 
132 static int
134  const struct GNUNET_PeerIdentity *peer,
135  const char *key)
136 {
137  struct Plugin *plugin = cls;
138 
139  plugin->iter_sub_system = sub_system;
140  plugin->iter_peer = peer;
141  plugin->iter_key = key;
142  plugin->deleted_entries = 0;
143 
146  plugin);
147  return plugin->deleted_entries;
148 }
149 
150 static int
151 expire_entries (void *cls,
152  const struct GNUNET_HashCode *key,
153  void *value)
154 {
155  struct Plugin *plugin = cls;
156  struct GNUNET_PEERSTORE_Record *entry = value;
157 
158  if (entry->expiry.abs_value_us < plugin->iter_now.abs_value_us)
159  {
160  GNUNET_CONTAINER_multihashmap_remove (plugin->hm, key, value);
161  plugin->exp_changes++;
162  }
163  return GNUNET_YES;
164 }
165 
166 
167 
178 static int
181  void *cont_cls)
182 {
183  struct Plugin *plugin = cls;
184  plugin->exp_changes = 0;
185  plugin->iter_now = now;
186 
189  plugin);
190  if (NULL != cont)
191  {
192  cont (cont_cls, plugin->exp_changes);
193  }
194  return GNUNET_OK;
195 
196 }
197 
198 
199 static int
200 iterate_entries (void *cls,
201  const struct GNUNET_HashCode *key,
202  void *value)
203 {
204  struct Plugin *plugin = cls;
205  struct GNUNET_PEERSTORE_Record *entry = value;
206 
207  if ((NULL != plugin->iter_peer) &&
208  (0 != memcmp (plugin->iter_peer,
209  &entry->peer,
210  sizeof (struct GNUNET_PeerIdentity))))
211  {
212  return GNUNET_YES;
213  }
214  if ((NULL != plugin->iter_key) &&
215  (0 != strcmp (plugin->iter_key,
216  entry->key)))
217  {
218  return GNUNET_YES;
219  }
220  if (NULL != plugin->iter)
221  plugin->iter (plugin->iter_cls, entry, NULL);
222  plugin->iter_result_found = GNUNET_YES;
223  return GNUNET_YES;
224 }
225 
240 static int
242  const struct GNUNET_PeerIdentity *peer,
243  const char *key,
245  void *iter_cls)
246 {
247  struct Plugin *plugin = cls;
248  plugin->iter = iter;
249  plugin->iter_cls = iter_cls;
250  plugin->iter_peer = peer;
251  plugin->iter_sub_system = sub_system;
252  plugin->iter_key = key;
253 
256  plugin);
257  if (NULL != iter)
258  iter (iter_cls, NULL, NULL);
259  return GNUNET_OK;
260 }
261 
262 
280 static int
281 peerstore_flat_store_record (void *cls, const char *sub_system,
282  const struct GNUNET_PeerIdentity *peer,
283  const char *key, const void *value, size_t size,
284  struct GNUNET_TIME_Absolute expiry,
287  void *cont_cls)
288 {
289  struct Plugin *plugin = cls;
290  struct GNUNET_HashCode hkey;
291  struct GNUNET_PEERSTORE_Record *entry;
292  const char *peer_id;
293 
294 
295  entry = GNUNET_new (struct GNUNET_PEERSTORE_Record);
296  entry->sub_system = GNUNET_strdup (sub_system);
297  entry->key = GNUNET_strdup (key);
298  entry->value = GNUNET_malloc (size);
299  GNUNET_memcpy (entry->value, value, size);
300  entry->value_size = size;
301  entry->peer = *peer;
302  entry->expiry = expiry;
303 
304  peer_id = GNUNET_i2s (peer);
305  GNUNET_CRYPTO_hash (peer_id,
306  strlen (peer_id),
307  &hkey);
308 
310  {
311  peerstore_flat_delete_records (cls, sub_system, peer, key);
312  }
313 
315  &hkey,
316  entry,
318  if (NULL != cont)
319  {
320  cont (cont_cls, GNUNET_OK);
321  }
322  return GNUNET_OK;
323 }
324 
325 
334 static int
336 {
337  char *afsdir;
338  char *key;
339  char *sub_system;
340  const char *peer_id;
341  char *peer;
342  char *value;
343  char *expiry;
344  struct GNUNET_DISK_FileHandle *fh;
345  struct GNUNET_PEERSTORE_Record *entry;
346  struct GNUNET_HashCode hkey;
347  size_t size;
348  char *buffer;
349  char *line;
350 
351  if (GNUNET_OK !=
352  GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-flat",
353  "FILENAME", &afsdir))
354  {
356  "FILENAME");
357  return GNUNET_SYSERR;
358  }
359  if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
360  {
362  {
363  GNUNET_break (0);
364  GNUNET_free (afsdir);
365  return GNUNET_SYSERR;
366  }
367  }
368  /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
369  plugin->fn = afsdir;
370 
371  fh = GNUNET_DISK_file_open (afsdir,
376  if (NULL == fh)
377  {
379  _("Unable to initialize file: %s.\n"),
380  afsdir);
381  return GNUNET_SYSERR;
382  }
383 
384  /* Load data from file into hashmap */
386  GNUNET_NO);
387 
388  if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
389  &size,
390  GNUNET_YES,
391  GNUNET_YES))
392  {
394  _("Unable to get filesize: %s.\n"),
395  afsdir);
396  return GNUNET_SYSERR;
397  }
398 
399  buffer = GNUNET_malloc (size + 1);
400 
402  buffer,
403  size))
404  {
406  _("Unable to read file: %s.\n"),
407  afsdir);
409  GNUNET_free (buffer);
410  return GNUNET_SYSERR;
411  }
412 
413  buffer[size] = '\0';
415  if (0 < size) {
416  line = strtok (buffer, "\n");
417  while (line != NULL) {
418  sub_system = strtok (line, ",");
419  if (NULL == sub_system)
420  break;
421  peer = strtok (NULL, ",");
422  if (NULL == peer)
423  break;
424  key = strtok (NULL, ",");
425  if (NULL == key)
426  break;
427  value = strtok (NULL, ",");
428  if (NULL == value)
429  break;
430  expiry = strtok (NULL, ",");
431  if (NULL == expiry)
432  break;
433  entry = GNUNET_new (struct GNUNET_PEERSTORE_Record);
434  entry->sub_system = GNUNET_strdup (sub_system);
435  entry->key = GNUNET_strdup (key);
436  {
437  size_t s;
438  char *o;
439 
440  o = NULL;
442  strlen (peer),
443  (void**)&o);
444  if (sizeof (struct GNUNET_PeerIdentity) == s)
445  GNUNET_memcpy (&entry->peer,
446  o,
447  s);
448  else
449  GNUNET_break (0);
451  }
453  strlen (value),
454  (void**)&entry->value);
455  if (GNUNET_SYSERR ==
457  &entry->expiry))
458  {
459  GNUNET_free (entry->sub_system);
460  GNUNET_free (entry->key);
461  GNUNET_free (entry);
462  break;
463  }
464  peer_id = GNUNET_i2s (&entry->peer);
465  GNUNET_CRYPTO_hash (peer_id,
466  strlen (peer_id),
467  &hkey);
468 
470  &hkey,
471  entry,
473 
474  }
475  }
476  GNUNET_free (buffer);
477  return GNUNET_OK;
478 }
479 
480 static int
482  const struct GNUNET_HashCode *key,
483  void *value)
484 {
485  struct GNUNET_DISK_FileHandle *fh = cls;
486  struct GNUNET_PEERSTORE_Record *entry = value;
487  char *line;
488  char *peer;
489  const char *expiry;
490  char *val;
491 
493  entry->value_size,
494  &val);
496  GNUNET_STRINGS_base64_encode ((char*)&entry->peer,
497  sizeof (struct GNUNET_PeerIdentity),
498  &peer);
499  GNUNET_asprintf (&line,
500  "%s,%s,%s,%s,%s",
501  entry->sub_system,
502  peer,
503  entry->key,
504  val,
505  expiry);
506  GNUNET_free (val);
507  GNUNET_free (peer);
509  line,
510  strlen (line));
511  GNUNET_free (entry->sub_system);
512  GNUNET_free (entry->key);
513  GNUNET_free (entry->value);
514  GNUNET_free (entry);
515  GNUNET_free (line);
516  return GNUNET_YES;
517 
518 }
519 
525 static void
527 {
528  struct GNUNET_DISK_FileHandle *fh;
529  fh = GNUNET_DISK_file_open (plugin->fn,
535  if (NULL == fh)
536  {
538  _("Unable to initialize file: %s.\n"),
539  plugin->fn);
540  return;
541  }
544  fh);
547 }
548 
549 
556 void *
558 {
559  static struct Plugin plugin;
560  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
562 
563  if (NULL != plugin.cfg)
564  return NULL; /* can only initialize once! */
565  memset (&plugin, 0, sizeof (struct Plugin));
566  plugin.cfg = cfg;
567  if (GNUNET_OK != database_setup (&plugin))
568  {
569  database_shutdown (&plugin);
570  return NULL;
571  }
573  api->cls = &plugin;
577  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is running\n");
578  return api;
579 }
580 
581 
588 void *
590 {
592  struct Plugin *plugin = api->cls;
593 
594  database_shutdown (plugin);
595  plugin->cfg = NULL;
596  GNUNET_free (api);
597  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flat plugin is finished\n");
598  return NULL;
599 }
600 
601 /* end of plugin_peerstore_sqlite.c */
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:669
Create file if it doesn&#39;t exist.
void(* GNUNET_PEERSTORE_Processor)(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Function called by PEERSTORE for each matching record.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
struct GNUNET_PeerIdentity peer
Peer Identity.
void * libgnunet_plugin_peerstore_flat_done(void *cls)
Exit point from the plugin.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:881
Delete any previous values for the given key before storing the given value.
size_t value_size
Size of value BLOB.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_TIME_Absolute iter_now
Iterator time.
uint32_t options
Options for addresses.
char * key
TLS key.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
int GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, struct GNUNET_TIME_Absolute *atime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:408
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:833
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_HashMapIterator it, void *it_cls)
Iterate over all entries in the map.
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
uint64_t abs_value_us
The actual value.
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...
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
IPC messages.
static int iterate_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
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)
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
uint64_t deleted_entries
Deleted entries.
#define GNUNET_memcpy(dst, src, n)
void * value
Record value BLOB.
static char * line
Desired phone line (string to be converted to a hash).
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.
static int store_and_free_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
static int peerstore_flat_expire_records(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
Truncate file if it exists.
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:1049
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static int delete_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
const struct GNUNET_PeerIdentity * iter_peer
Iterator peer.
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.
GNUNET_PEERSTORE_StoreOption
Options for storing values in PEERSTORE.
static int fh
Handle to the unique file.
void * libgnunet_plugin_peerstore_flat_init(void *cls)
Entry point for the plugin.
static int expire_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
struct returned by the initialization function of the plugin
A 512-bit hashcode.
static int peerstore_flat_delete_records(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key)
Delete records with the given key.
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **output)
Decode from Base64.
Definition: strings.c:2067
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:2004
static int peerstore_flat_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.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
void(* GNUNET_PEERSTORE_Continuation)(void *cls, int success)
Continuation called with a status result.
struct GNUNET_CONTAINER_MultiHashMap * hm
HashMap.
void * iter_cls
Iterator cls.
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.
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.
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:289
char * fn
Filename used for the DB.
Allow multiple values with the same key.
uint64_t exp_changes
Expired entries.
The identity of the host (wraps the signing key of the peer).
unsigned long long size
Size of all values we&#39;re storing.
configuration data
Definition: configuration.c:85
struct GNUNET_TIME_Absolute expiry
Expiry time of entry.
char * sub_system
Responsible sub system string.
Handle for a plugin.
Definition: block.c:37
Single PEERSTORE record.
#define GNUNET_log(kind,...)
static int peerstore_flat_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.
Open the file for both reading and writing.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
const char * iter_sub_system
Iterator subsystem.
int iter_result_found
Result found bool.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
const char * iter_key
iterator key
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:792
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1673
GNUNET_PEERSTORE_Processor iter
Iterator.
Handle used to access files (and pipes).
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.