GNUnet  0.10.x
datacache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 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  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_datacache_lib.h"
30 
31 
32 #define LOG(kind, ...) GNUNET_log_from(kind, "datacache", __VA_ARGS__)
33 
34 #define LOG_STRERROR_FILE(kind, op, fn) \
35  GNUNET_log_from_strerror_file(kind, "datacache", op, fn)
36 
45 
50 
55 
59  char *section;
60 
66 
70  char *short_name;
71 
75  char *lib_name;
76 
80  char *bloom_name;
81 
86 
90  unsigned long long utilization;
91 };
92 
93 
102 static void
103 env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size)
104 {
105  struct GNUNET_DATACACHE_Handle *h = cls;
106 
108  "Content under key `%s' discarded\n",
109  GNUNET_h2s(key));
110  GNUNET_assert(h->utilization >= size);
111  h->utilization -= size;
114  gettext_noop("# bytes stored"),
115  -(long long)size,
116  GNUNET_NO);
118  gettext_noop("# items stored"),
119  -1,
120  GNUNET_NO);
121 }
122 
123 
133  const char *section)
134 {
135  unsigned int bf_size;
136  unsigned long long quota;
138  char *libname;
139  char *name;
140 
141  if (GNUNET_OK !=
142  GNUNET_CONFIGURATION_get_value_size(cfg, section, "QUOTA", &quota))
143  {
145  return NULL;
146  }
147  if (GNUNET_OK !=
148  GNUNET_CONFIGURATION_get_value_string(cfg, section, "DATABASE", &name))
149  {
151  return NULL;
152  }
153  bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
154 
155  ret = GNUNET_new(struct GNUNET_DATACACHE_Handle);
156 
157  if (GNUNET_YES !=
158  GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF"))
159  {
160  if (GNUNET_YES !=
161  GNUNET_CONFIGURATION_get_value_yesno(cfg, section, "DISABLE_BF_RC"))
162  {
163  ret->bloom_name = GNUNET_DISK_mktemp("gnunet-datacachebloom");
164  }
165  if (NULL != ret->bloom_name)
166  {
168  ret->bloom_name,
169  quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
170  5);
171  }
172  if (NULL == ret->filter)
173  {
174  ret->filter =
176  bf_size,
177  5); /* approx. 3% false positives at max use */
178  }
179  }
180  ret->stats = GNUNET_STATISTICS_create("datacache", cfg);
181  ret->section = GNUNET_strdup(section);
182  ret->env.cfg = cfg;
184  ret->env.section = ret->section;
185  ret->env.cls = ret;
187  ret->env.quota = quota;
188  LOG(GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
189  GNUNET_asprintf(&libname, "libgnunet_plugin_datacache_%s", name);
190  ret->short_name = name;
191  ret->lib_name = libname;
192  ret->api = GNUNET_PLUGIN_load(libname, &ret->env);
193  if (ret->api == NULL)
194  {
196  _("Failed to load datacache plugin for `%s'\n"),
197  name);
199  return NULL;
200  }
201  return ret;
202 }
203 
204 
210 void
212 {
213  if (NULL != h->filter)
215  if (NULL != h->api)
217  GNUNET_free(h->lib_name);
219  GNUNET_free(h->section);
220  if (NULL != h->bloom_name)
221  {
222  if (0 != unlink(h->bloom_name))
224  "datacache",
225  "unlink",
226  h->bloom_name);
228  }
230  GNUNET_free(h);
231 }
232 
233 
248 int
250  const struct GNUNET_HashCode *key,
251  uint32_t xor_distance,
252  size_t data_size,
253  const char *data,
254  enum GNUNET_BLOCK_Type type,
255  struct GNUNET_TIME_Absolute discard_time,
256  unsigned int path_info_len,
257  const struct GNUNET_PeerIdentity *path_info)
258 {
259  ssize_t used;
260 
261  used = h->api->put(h->api->cls,
262  key,
263  xor_distance,
264  data_size,
265  data,
266  type,
267  discard_time,
268  path_info_len,
269  path_info);
270  if (-1 == used)
271  {
272  GNUNET_break(0);
273  return GNUNET_SYSERR;
274  }
275  if (0 == used)
276  {
277  /* duplicate */
278  return GNUNET_NO;
279  }
281  "Stored data under key `%s' in cache\n",
282  GNUNET_h2s(key));
283  if (NULL != h->filter)
286  gettext_noop("# bytes stored"),
287  used,
288  GNUNET_NO);
290  gettext_noop("# items stored"),
291  1,
292  GNUNET_NO);
293  while (h->utilization + used > h->env.quota)
294  GNUNET_assert(GNUNET_OK == h->api->del(h->api->cls));
295  h->utilization += used;
296  return GNUNET_OK;
297 }
298 
299 
311 unsigned int
313  const struct GNUNET_HashCode *key,
314  enum GNUNET_BLOCK_Type type,
316  void *iter_cls)
317 {
319  gettext_noop("# requests received"),
320  1,
321  GNUNET_NO);
323  "Processing request for key `%s'\n",
324  GNUNET_h2s(key));
325  if ((NULL != h->filter) &&
327  {
329  gettext_noop(
330  "# requests filtered by bloom filter"),
331  1,
332  GNUNET_NO);
334  "Bloomfilter filters request for key `%s'\n",
335  GNUNET_h2s(key));
336  return 0; /* can not be present */
337  }
338  return h->api->get(h->api->cls, key, type, iter, iter_cls);
339 }
340 
341 
350 unsigned int
353  void *iter_cls)
354 {
356  gettext_noop(
357  "# requests for random value received"),
358  1,
359  GNUNET_NO);
360  LOG(GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
361  return h->api->get_random(h->api->cls, iter, iter_cls);
362 }
363 
364 
378 unsigned int
380  const struct GNUNET_HashCode *key,
381  unsigned int num_results,
383  void *iter_cls)
384 {
386  gettext_noop(
387  "# proximity search requests received"),
388  1,
389  GNUNET_NO);
391  "Processing proximity search at `%s'\n",
392  GNUNET_h2s(key));
393  return h->api->get_closest(h->api->cls, key, num_results, iter, iter_cls);
394 }
395 
396 
397 /* end of datacache.c */
const char * section
Configuration section to use.
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
void * cls
Closure to pass to all plugin functions.
struct GNUNET_DATACACHE_PluginEnvironment env
Environment provided to our plugin.
Definition: datacache.c:85
ssize_t(* put)(void *cls, const struct GNUNET_HashCode *key, uint32_t xor_distance, size_t size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
int(* del)(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition: datacache.c:211
int GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, uint32_t xor_distance, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
Definition: datacache.c:249
#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.
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.
static int ret
Final status code.
Definition: gnunet-arm.c:89
Handle for the service.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
GNUNET_DATACACHE_DeleteNotifyCallback delete_notify
Function to call whenever the plugin needs to discard content that it was asked to store...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
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
struct GNUNET_STATISTICS_Handle * stats
Opaque handle for the statistics service.
Definition: datacache.c:54
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
char * short_name
Short name for the plugin (i.e.
Definition: datacache.c:70
unsigned long long utilization
How much space is in use right now?
Definition: datacache.c:90
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition: disk.c:465
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
void GNUNET_CONTAINER_bloomfilter_remove(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Remove an element from the filter.
int GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
unsigned int GNUNET_DATACACHE_get(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datacache.
Definition: datacache.c:312
unsigned int(* get_closest)(void *cls, const struct GNUNET_HashCode *key, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
struct GNUNET_DATACACHE_Handle * GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a data cache.
Definition: datacache.c:132
struct GNUNET_CONTAINER_BloomFilter * filter
Bloomfilter to quickly tell if we don&#39;t have the content.
Definition: datacache.c:44
char * lib_name
Name of the library (i.e.
Definition: datacache.c:75
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_load(const char *filename, size_t size, unsigned int k)
Load a Bloom filter from a file.
struct returned by the initialization function of the plugin
A 512-bit hashcode.
int GNUNET_CONFIGURATION_get_value_size(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *size)
Get a configuration value that should be a size in bytes.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: datacache.c:49
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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
const char * name
#define LOG(kind,...)
Definition: datacache.c:32
static unsigned long long quota
How much space are we allowed to use?
The identity of the host (wraps the signing key of the peer).
struct GNUNET_DATACACHE_PluginFunctions * api
API of the transport as returned by the plugin&#39;s initialization function.
Definition: datacache.c:65
Internal state of the datacache library.
Definition: datacache.c:40
static void env_delete_notify(void *cls, const struct GNUNET_HashCode *key, size_t size)
Function called by plugins to notify the datacache about content deletions.
Definition: datacache.c:103
configuration data
Definition: configuration.c:83
int(* GNUNET_DATACACHE_Iterator)(void *cls, const struct GNUNET_HashCode *key, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute exp, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
An iterator over a set of items stored in the datacache.
void * cls
Closure to use for callbacks.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#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
unsigned int(* get_random)(void *cls, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Return a random value from the datastore.
unsigned long long quota
How much space are we allowed to use?
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".
uint32_t data
The data value.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
static size_t data_size
Number of bytes in data.
unsigned int GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
Definition: datacache.c:379
char * section
Configuration section to use.
Definition: datacache.c:59
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associcated with a filter in memory, flush to drive if needed (do not free the space o...
char * bloom_name
Name for the bloom filter file.
Definition: datacache.c:80
unsigned int GNUNET_DATACACHE_get_random(struct GNUNET_DATACACHE_Handle *h, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Obtain a random element from the datacache.
Definition: datacache.c:351
unsigned int(* get)(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69