GNUnet  0.10.x
gnunet-service-dht_datacache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2010, 2011, 2015, 2017 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  */
26 #include "platform.h"
27 #include "gnunet_datacache_lib.h"
31 #include "gnunet-service-dht.h"
32 
33 #define LOG(kind, ...) GNUNET_log_from(kind, "dht-dhtcache", __VA_ARGS__)
34 
39 #define NUM_CLOSEST 42
40 
45 
46 
59 void
61  const struct GNUNET_HashCode *key,
62  unsigned int put_path_length,
63  const struct GNUNET_PeerIdentity *put_path,
65  size_t data_size,
66  const void *data)
67 {
68  int r;
69 
70  if (NULL == datacache)
71  {
73  _("%s request received, but have no datacache!\n"), "PUT");
74  return;
75  }
76  if (data_size >= GNUNET_MAX_MESSAGE_SIZE)
77  {
78  GNUNET_break(0);
79  return;
80  }
81  /* Put size is actual data size plus struct overhead plus path length (if any) */
83  gettext_noop("# ITEMS stored in datacache"),
84  1,
85  GNUNET_NO);
86  r = GNUNET_DATACACHE_put(datacache,
87  key,
90  data_size,
91  data,
92  type,
93  expiration,
94  put_path_length,
95  put_path);
97  "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n",
98  GNUNET_h2s(key),
99  data_size,
100  r,
101  put_path_length);
102 }
103 
104 
112  const void *xquery;
113 
118 
123 
128 
132  void *gc_cls;
133 
137  size_t xquery_size;
138 
143 };
144 
145 
160 static int
162  const struct GNUNET_HashCode *key,
163  size_t data_size,
164  const char *data,
165  enum GNUNET_BLOCK_Type type,
166  struct GNUNET_TIME_Absolute exp,
167  unsigned int put_path_length,
168  const struct GNUNET_PeerIdentity *put_path)
169 {
170  static char non_null;
171  struct GetRequestContext *ctx = cls;
173 
174  if (0 == GNUNET_TIME_absolute_get_remaining(exp).rel_value_us)
175  {
176  GNUNET_break(0); /* why does datacache return expired values? */
177  return GNUNET_OK; /* skip expired record */
178  }
179  if ((NULL == data) &&
180  (0 == data_size))
181  data = &non_null; /* point anywhere, but not to NULL */
182 
183  eval
185  type,
186  ctx->bg,
188  key,
189  ctx->xquery,
190  ctx->xquery_size,
191  data,
192  data_size);
194  "Found reply for query %s in datacache, evaluation result is %d\n",
195  GNUNET_h2s(key),
196  (int)eval);
197  ctx->eval = eval;
198  switch (eval)
199  {
202  /* forward to local clients */
205  ("# Good RESULTS found in datacache"), 1,
206  GNUNET_NO);
207  ctx->gc(ctx->gc_cls,
208  type,
209  exp,
210  key,
211  put_path_length, put_path,
212  0, NULL,
213  data, data_size);
214  break;
215 
218  gettext_noop("# Duplicate RESULTS found in datacache"),
219  1,
220  GNUNET_NO);
221  break;
222 
225  gettext_noop("# Invalid RESULTS found in datacache"),
226  1,
227  GNUNET_NO);
228  break;
229 
232  gettext_noop("# Irrelevant RESULTS found in datacache"),
233  1,
234  GNUNET_NO);
235  break;
236 
238  GNUNET_break(0);
239  break;
240 
242  GNUNET_break_op(0);
243  return GNUNET_SYSERR;
244 
247  gettext_noop("# Unsupported RESULTS found in datacache"),
248  1,
249  GNUNET_NO);
251  _("Unsupported block type (%u) in local response!\n"),
252  type);
253  break;
254  }
256 }
257 
258 
273  enum GNUNET_BLOCK_Type type,
274  const void *xquery,
275  size_t xquery_size,
276  struct GNUNET_BLOCK_Group *bg,
278  void *gc_cls)
279 {
280  struct GetRequestContext ctx;
281  unsigned int r;
282 
283  if (NULL == datacache)
286  gettext_noop("# GET requests given to datacache"),
287  1,
288  GNUNET_NO);
290  ctx.key = *key;
291  ctx.xquery = xquery;
292  ctx.xquery_size = xquery_size;
293  ctx.bg = bg;
294  ctx.gc = gc;
295  ctx.gc_cls = gc_cls;
296  r = GNUNET_DATACACHE_get(datacache,
297  key,
298  type,
300  &ctx);
302  "DATACACHE GET for key %s completed (%d). %u results found.\n",
303  GNUNET_h2s(key),
304  ctx.eval,
305  r);
306  return ctx.eval;
307 }
308 
309 
324 static int
326  const struct GNUNET_HashCode *key,
327  size_t data_size,
328  const char *data,
329  enum GNUNET_BLOCK_Type type,
330  struct GNUNET_TIME_Absolute exp,
331  unsigned int path_info_len,
332  const struct GNUNET_PeerIdentity *path_info)
333 {
334  struct GNUNET_HashCode *dest = cls;
335 
336  *dest = *key;
337  return GNUNET_OK; /* should actually not matter which we return */
338 }
339 
340 
349 int
351 {
352  if (0 ==
353  GNUNET_DATACACHE_get_random(datacache,
355  key))
356  {
357  /* randomize key in this case */
359  key);
360  return GNUNET_SYSERR;
361  }
362  return GNUNET_OK;
363 }
364 
365 
369 struct SuccContext {
374 
378  void *cb_cls;
379 };
380 
381 
396 static int
398  const struct GNUNET_HashCode *key,
399  size_t size,
400  const char *data,
401  enum GNUNET_BLOCK_Type type,
402  struct GNUNET_TIME_Absolute exp,
403  unsigned int put_path_length,
404  const struct GNUNET_PeerIdentity *put_path)
405 {
406  const struct SuccContext *sc = cls;
407 
408  /* NOTE: The datacache currently does not store the RO from
409  the original 'put', so we don't know the 'correct' option
410  at this point anymore. Thus, we conservatively assume
411  that recording is desired (for now). */
412  sc->cb(sc->cb_cls,
414  key,
415  type,
416  put_path_length, put_path,
417  exp,
418  data,
419  size);
420  return GNUNET_OK;
421 }
422 
423 
432 void
435  void *cb_cls)
436 {
437  struct SuccContext sc;
438 
439  sc.cb = cb;
440  sc.cb_cls = cb_cls;
441  (void)GNUNET_DATACACHE_get_closest(datacache,
442  key,
443  NUM_CLOSEST,
445  &sc);
446 }
447 
448 
452 void
454 {
455  datacache = GNUNET_DATACACHE_create(GDS_cfg, "dhtcache");
456 }
457 
458 
462 void
464 {
465  if (NULL != datacache)
466  {
467  GNUNET_DATACACHE_destroy(datacache);
468  datacache = NULL;
469  }
470 }
471 
472 
473 /* end of gnunet-service-dht_datacache.c */
Block does not match query (invalid result)
#define NUM_CLOSEST
How many "closest" results to we return for migration when asked (at most)?
enum GNUNET_BLOCK_EvaluationResult eval
Return value to give back.
GNUnet DHT globals.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
We should keep track of the route that the message took in the P2P network.
size_t xquery_size
Number of bytes in xquery.
static char * expiration
Credential TTL.
void GDS_DATACACHE_init()
Initialize datacache subsystem.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
void(* GDS_DATACACHE_GetCallback)(void *cls, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute expiration_time, const struct GNUNET_HashCode *key, unsigned int put_path_length, const struct GNUNET_PeerIdentity *put_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *get_path, const void *data, size_t data_size)
Handle a result for a GET operation.
unsigned int GNUNET_CRYPTO_hash_matching_bits(const struct GNUNET_HashCode *first, const struct GNUNET_HashCode *second)
Determine how many low order bits match in two struct GNUNET_HashCodes.
Definition: crypto_hash.c:256
void GDS_DATACACHE_done()
Shutdown datacache subsystem.
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition: datacache.c:211
GDS_DATACACHE_GetCallback gc
Function to call on results.
void(* GDS_DATACACHE_SuccessorCallback)(void *cls, enum GNUNET_DHT_RouteOption options, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int put_path_length, const struct GNUNET_PeerIdentity *put_path, struct GNUNET_TIME_Absolute expiration, const void *data, size_t data_size)
Send the get result to requesting client.
Block does not match xquery (valid result, not relevant for the request)
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
static int datacache_get_successors_iterator(void *cls, const struct GNUNET_HashCode *key, size_t size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute exp, unsigned int put_path_length, const struct GNUNET_PeerIdentity *put_path)
Iterator for local get request results,.
#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).
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
struct GNUNET_HashCode my_identity_hash
Hash of the identity of this peer.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
struct GNUNET_HashCode key
The key this request was about.
int GDS_DATACACHE_get_random_key(struct GNUNET_HashCode *key)
Obtain a random key from the datacache.
void * cb_cls
Closure for cb.
void GNUNET_CRYPTO_hash_create_random(enum GNUNET_CRYPTO_Quality mode, struct GNUNET_HashCode *result)
Create a random hash code.
Definition: crypto_hash.c:138
#define LOG(kind,...)
Valid result, but suppressed because it is a duplicate.
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
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
enum GNUNET_BLOCK_EvaluationResult GNUNET_BLOCK_evaluate(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, enum GNUNET_BLOCK_EvaluationOptions eo, const struct GNUNET_HashCode *query, const void *xquery, size_t xquery_size, const void *reply_block, size_t reply_block_size)
Function called to validate a reply or a request.
Definition: block.c:337
void GDS_DATACACHE_handle_put(struct GNUNET_TIME_Absolute expiration, const struct GNUNET_HashCode *key, unsigned int put_path_length, const struct GNUNET_PeerIdentity *put_path, enum GNUNET_BLOCK_Type type, size_t data_size, const void *data)
Handle a datum we&#39;ve received from another peer.
struct GNUNET_DATACACHE_Handle * GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a data cache.
Definition: datacache.c:132
Last possible valid result.
Randomness for IVs etc.
GNUnet DHT tracking of requests for routing replies.
A 512-bit hashcode.
Context containing information about a GET request.
struct GNUNET_BLOCK_Group * bg
Block group to use to evaluate replies (updated)
The block is obtained from the local database, skip cryptographic checks.
Valid result, and there may be more.
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
GNUNET_BLOCK_EvaluationResult
Possible ways for how a block may relate to a query.
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
GNUnet DHT routing code.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
GDS_DATACACHE_SuccessorCallback cb
Function to call on the result.
Specified block type not supported by this plugin.
Closure for datacache_get_successors_iterator().
void GDS_DATACACHE_get_successors(const struct GNUNET_HashCode *key, GDS_DATACACHE_SuccessorCallback cb, void *cb_cls)
Handle a request for data close to a key that we have received from another peer. ...
GNUnet DHT service&#39;s datacache integration.
The identity of the host (wraps the signing key of the peer).
Internal state of the datacache library.
Definition: datacache.c:40
Query format does not match block type (invalid query).
static int datacache_get_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 put_path_length, const struct GNUNET_PeerIdentity *put_path)
Iterator for local get request results,.
#define GNUNET_log(kind,...)
Block group data.
void * gc_cls
Closure for gc.
enum GNUNET_BLOCK_EvaluationResult GDS_DATACACHE_handle_get(const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, const void *xquery, size_t xquery_size, struct GNUNET_BLOCK_Group *bg, GDS_DATACACHE_GetCallback gc, void *gc_cls)
Handle a GET request we&#39;ve received from another peer.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:331
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
static int datacache_random_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)
Function called with a random element from the datacache.
const struct GNUNET_CONFIGURATION_Handle * GDS_cfg
Configuration we use.
static struct GNUNET_DATACACHE_Handle * datacache
Handle to the datacache service (for inserting/retrieving data)
uint32_t data
The data value.
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
Query is valid, no reply given.
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
const void * xquery
extended query (see gnunet_block_lib.h).
#define gettext_noop(String)
Definition: gettext.h:69