GNUnet  0.16.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, 2022 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 4
40 
41 
46 
47 
48 void
50 {
51  struct GNUNET_HashCode xor;
53 
54  if (NULL == datacache)
55  {
57  "PUT request received, but have no datacache!\n");
58  return;
59  }
61  {
62  GNUNET_break (0);
63  return;
64  }
65  /* Put size is actual data size plus struct overhead plus path length (if any) */
67  "# ITEMS stored in datacache",
68  1,
69  GNUNET_NO);
72  &xor);
74  &bd->key,
76  bd->data_size,
77  bd->data,
78  bd->type,
79  bd->expiration_time,
80  bd->put_path_length,
81  bd->put_path);
83  "DATACACHE PUT for key %s [%lu] completed (%d) after %u hops\n",
84  GNUNET_h2s (&bd->key),
85  (unsigned long) bd->data_size,
86  r,
87  bd->put_path_length);
88 }
89 
90 
95 {
99  const void *xquery;
100 
104  struct GNUNET_HashCode key;
105 
110 
115 
119  void *gc_cls;
120 
124  size_t xquery_size;
125 
130 };
131 
132 
147 static enum GNUNET_GenericReturnValue
148 datacache_get_iterator (void *cls,
149  const struct GNUNET_HashCode *key,
150  size_t data_size,
151  const char *data,
152  enum GNUNET_BLOCK_Type type,
153  struct GNUNET_TIME_Absolute exp,
154  unsigned int put_path_length,
155  const struct GNUNET_DHT_PathElement *put_path)
156 {
157  struct GetRequestContext *ctx = cls;
159  struct GDS_DATACACHE_BlockData bd = {
160  .key = *key,
161  .expiration_time = exp,
162  .put_path = put_path,
163  .data = data,
164  .data_size = data_size,
165  .put_path_length = put_path_length,
166  .type = type
167  };
168 
170  {
171  GNUNET_break (0); /* why does datacache return expired values? */
172  return GNUNET_OK; /* skip expired record */
173  }
174  eval
176  bd.type,
177  ctx->bg,
178  &bd.key,
179  ctx->xquery,
180  ctx->xquery_size,
181  bd.data,
182  bd.data_size);
184  "Evaluated reply for query %s in datacache, result is %d\n",
185  GNUNET_h2s (key),
186  (int) eval);
187  ctx->eval = eval;
188  switch (eval)
189  {
193  /* forward to initiator */
195  "# Good RESULTS found in datacache",
196  1,
197  GNUNET_NO);
198  ctx->gc (ctx->gc_cls,
199  &bd);
200  break;
203  "# Duplicate RESULTS found in datacache",
204  1,
205  GNUNET_NO);
206  break;
209  "# Irrelevant RESULTS found in datacache",
210  1,
211  GNUNET_NO);
212  break;
213  }
214  return (eval == GNUNET_BLOCK_REPLY_OK_LAST) ? GNUNET_NO : GNUNET_OK;
215 }
216 
217 
220  enum GNUNET_BLOCK_Type type,
221  const void *xquery,
222  size_t xquery_size,
223  struct GNUNET_BLOCK_Group *bg,
225  void *gc_cls)
226 {
227  struct GetRequestContext ctx = {
229  .key = *key,
230  .xquery = xquery,
231  .xquery_size = xquery_size,
232  .bg = bg,
233  .gc = gc,
234  .gc_cls = gc_cls
235  };
236  unsigned int r;
237 
238  if (NULL == datacache)
241  "# GET requests given to datacache",
242  1,
243  GNUNET_NO);
245  key,
246  type,
248  &ctx);
250  "DATACACHE GET for key %s completed (%d). %u results found.\n",
251  GNUNET_h2s (key),
252  ctx.eval,
253  r);
254  return ctx.eval;
255 }
256 
257 
260  enum GNUNET_BLOCK_Type type,
261  const void *xquery,
262  size_t xquery_size,
263  struct GNUNET_BLOCK_Group *bg,
265  void *cb_cls)
266 {
267  struct GetRequestContext ctx = {
269  .key = *key,
270  .xquery = xquery,
271  .xquery_size = xquery_size,
272  .bg = bg,
273  .gc = cb,
274  .gc_cls = cb_cls
275  };
276  unsigned int r;
277 
278  if (NULL == datacache)
281  "# GET closest requests given to datacache",
282  1,
283  GNUNET_NO);
285  key,
286  type,
287  NUM_CLOSEST,
289  &ctx);
291  "DATACACHE approximate GET for key %s completed (%d). %u results found.\n",
292  GNUNET_h2s (key),
293  ctx.eval,
294  r);
295  return ctx.eval;
296 }
297 
298 
299 void
301 {
303  "dhtcache");
304 }
305 
306 
307 void
309 {
310  if (NULL != datacache)
311  {
313  datacache = NULL;
314  }
315 }
316 
317 
318 /* end of gnunet-service-dht_datacache.c */
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
struct GNUNET_HashCode GDS_my_identity_hash
Hash of the identity of this peer.
GNUnet DHT globals.
struct GNUNET_STATISTICS_Handle * GDS_stats
Handle for the statistics service.
struct GNUNET_BLOCK_Context * GDS_block_context
Our handle to the BLOCK library.
const struct GNUNET_CONFIGURATION_Handle * GDS_cfg
Configuration we use.
#define NUM_CLOSEST
How many "closest" results to we return for migration when asked (at most)?
void GDS_DATACACHE_init()
Initialize datacache subsystem.
enum GNUNET_BLOCK_ReplyEvaluationResult GDS_DATACACHE_get_closest(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 cb, void *cb_cls)
Handle a request for data close to a key that we have received from another peer.
enum GNUNET_BLOCK_ReplyEvaluationResult 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've received from another peer.
static enum GNUNET_GenericReturnValue 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_DHT_PathElement *put_path)
Iterator for local get request results,.
void GDS_DATACACHE_done()
Shutdown datacache subsystem.
#define LOG(kind,...)
static struct GNUNET_DATACACHE_Handle * datacache
Handle to the datacache service (for inserting/retrieving data)
void GDS_DATACACHE_handle_put(const struct GDS_DATACACHE_BlockData *bd)
Handle a datum we've received from another peer.
GNUnet DHT service's datacache integration.
void(* GDS_DATACACHE_GetCallback)(void *cls, const struct GDS_DATACACHE_BlockData *bd)
Handle a result for a GET operation.
GNUnet DHT routing code.
GNUnet DHT tracking of requests for routing replies.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:92
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_NO
Definition: gnunet_common.h:94
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
Definition: crypto_hash.c:176
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
enum GNUNET_BLOCK_ReplyEvaluationResult GNUNET_BLOCK_check_reply(struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type, struct GNUNET_BLOCK_Group *group, 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 if a reply is good for a particular query.
Definition: block.c:338
GNUNET_BLOCK_ReplyEvaluationResult
Possible ways for how a block may relate to a query.
@ GNUNET_BLOCK_REPLY_OK_MORE
Valid result, and there may be more.
@ GNUNET_BLOCK_REPLY_OK_DUPLICATE
Valid result, but suppressed because it is a duplicate.
@ GNUNET_BLOCK_REPLY_OK_LAST
Last possible valid result.
@ GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED
Specified block type not supported by any plugin.
@ GNUNET_BLOCK_REPLY_IRRELEVANT
Block does not match xquery (valid result, not relevant for the request)
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:315
enum GNUNET_GenericReturnValue 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_DHT_PathElement *path_info)
Store an item in the datacache.
Definition: datacache.c:261
struct GNUNET_DATACACHE_Handle * GNUNET_DATACACHE_create(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Create a data cache.
Definition: datacache.c:128
void GNUNET_DATACACHE_destroy(struct GNUNET_DATACACHE_Handle *h)
Destroy a data cache (and free associated resources).
Definition: datacache.c:235
unsigned int GNUNET_DATACACHE_get_closest(struct GNUNET_DATACACHE_Handle *h, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, 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:349
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
Definition: crypto_hash.c:135
#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_DEBUG
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
bool GNUNET_TIME_absolute_is_past(struct GNUNET_TIME_Absolute abs)
Test if abs is truly in the past (excluding now).
Definition: time.c:668
Information about a block stored in the datacache.
const void * data
Actual block data.
size_t data_size
Number of bytes in data.
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_BLOCK_Type type
Type of the block.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_HashCode key
Key of the block.
Block group data.
Internal state of the datacache library.
Definition: datacache.c:41
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
A 512-bit hashcode.
Time for absolute times used by GNUnet, in microseconds.
Context containing information about a GET request.
enum GNUNET_BLOCK_ReplyEvaluationResult eval
Return value to give back.
struct GNUNET_BLOCK_Group * bg
Block group to use to evaluate replies (updated)
GDS_DATACACHE_GetCallback gc
Function to call on results.
struct GNUNET_HashCode key
The key this request was about.
const void * xquery
extended query (see gnunet_block_lib.h).
void * gc_cls
Closure for gc.
size_t xquery_size
Number of bytes in xquery.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model