GNUnet 0.27.0
 
Loading...
Searching...
No Matches
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 "gnunet_common.h"
27#include "platform.h"
32#include "gnunet-service-dht.h"
33
34#define LOG(kind, ...) GNUNET_log_from (kind, "dht-dhtcache", __VA_ARGS__)
35
40#define NUM_CLOSEST 4
41
42
47
48
49void
51{
52 const struct GNUNET_HashCode *my_identity_hash;
53 struct GNUNET_HashCode xor;
55 my_identity_hash = GNUNET_PILS_get_identity_hash (GDS_pils);
56 if (NULL == my_identity_hash)
57 {
59 "PUT request received, but have no identity hash!\n");
60 return;
61 }
62 if (NULL == datacache)
63 {
65 "PUT request received, but have no datacache!\n");
66 return;
67 }
69 {
70 GNUNET_break (0);
71 return;
72 }
73 /* Put size is actual data size plus struct overhead plus path length (if any) */
75 "# ITEMS stored in datacache",
76 1,
77 GNUNET_NO);
79 my_identity_hash,
80 &xor);
83 bd);
85 "DATACACHE PUT for key %s [%lu] completed (%d) after %u hops\n",
86 GNUNET_h2s (&bd->key),
87 (unsigned long) bd->data_size,
88 r,
89 bd->put_path_length);
90}
91
92
133
134
145 const struct GNUNET_DATACACHE_Block *bd)
146{
147 struct GetRequestContext *ctx = cls;
149
151 {
152 GNUNET_break (0); /* why does datacache return expired values? */
153 return GNUNET_OK; /* skip expired record */
154 }
155 eval
157 bd->type,
158 ctx->bg,
159 &bd->key,
160 ctx->xquery,
161 ctx->xquery_size,
162 bd->data,
163 bd->data_size);
165 "Evaluated reply for query %s in datacache, result is %d\n",
166 GNUNET_h2s (&bd->key),
167 (int) eval);
168 ctx->eval = eval;
169 switch (eval)
170 {
174 /* forward to initiator */
176 "# Good RESULTS found in datacache",
177 1,
178 GNUNET_NO);
179 ctx->gc (ctx->gc_cls,
180 bd);
181 break;
184 "# Duplicate RESULTS found in datacache",
185 1,
186 GNUNET_NO);
187 break;
190 "# Irrelevant RESULTS found in datacache",
191 1,
192 GNUNET_NO);
193 break;
194 }
196}
197
198
202 const void *xquery,
203 size_t xquery_size,
204 struct GNUNET_BLOCK_Group *bg,
206 void *gc_cls)
207{
208 struct GetRequestContext ctx = {
210 .key = *key,
211 .xquery = xquery,
212 .xquery_size = xquery_size,
213 .bg = bg,
214 .gc = gc,
215 .gc_cls = gc_cls
216 };
217 unsigned int r;
218
219 if (NULL == datacache)
222 "# GET requests given to datacache",
223 1,
224 GNUNET_NO);
226 key,
227 type,
229 &ctx);
231 "DATACACHE GET for key %s completed (%d). %u results found.\n",
232 GNUNET_h2s (key),
233 ctx.eval,
234 r);
235 return ctx.eval;
236}
237
238
242 const void *xquery,
243 size_t xquery_size,
244 struct GNUNET_BLOCK_Group *bg,
246 void *cb_cls)
247{
248 struct GetRequestContext ctx = {
250 .key = *key,
251 .xquery = xquery,
252 .xquery_size = xquery_size,
253 .bg = bg,
254 .gc = cb,
255 .gc_cls = cb_cls
256 };
257 unsigned int r;
258
259 if (NULL == datacache)
262 "# GET closest requests given to datacache",
263 1,
264 GNUNET_NO);
266 key,
267 type,
270 &ctx);
272 "DATACACHE approximate GET for key %s completed (%d). %u results found.\n",
273 GNUNET_h2s (key),
274 ctx.eval,
275 r);
276 return ctx.eval;
277}
278
279
280void
282{
284 "dhtcache");
285}
286
287
288void
290{
291 if (NULL != datacache)
292 {
294 datacache = NULL;
295 }
296}
297
298
299/* end of gnunet-service-dht_datacache.c */
struct GNUNET_PILS_Handle * GDS_pils
Handle for the pils service.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_FS_Handle * ctx
static uint32_t type
Type string converted to DNS type value.
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.
void GDS_DATACACHE_handle_put(const struct GNUNET_DATACACHE_Block *bd)
Handle a datum we've received from another peer.
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)
static enum GNUNET_GenericReturnValue datacache_get_iterator(void *cls, const struct GNUNET_DATACACHE_Block *bd)
Iterator for local get request results,.
GNUnet DHT service's datacache integration.
void(* GDS_DATACACHE_GetCallback)(void *cls, const struct GNUNET_DATACACHE_Block *bd)
Handle a result for a GET operation.
GNUnet DHT routing code.
GNUnet DHT tracking of requests for routing replies.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
const struct GNUNET_HashCode * GNUNET_PILS_get_identity_hash(const struct GNUNET_PILS_Handle *handle)
Return the hash of the current peer identity from a given handle.
Definition pils_api.c:736
#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:339
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:292
enum GNUNET_GenericReturnValue GNUNET_DATACACHE_put(struct GNUNET_DATACACHE_Handle *h, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
Store an item in the datacache.
Definition datacache.c:250
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:224
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:327
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
unsigned int GNUNET_CRYPTO_hash_count_leading_zeros(const struct GNUNET_HashCode *h)
Count the number of leading 0 bits in h.
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_NO
#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:667
Information about a block stored in the datacache.
enum GNUNET_BLOCK_Type type
Type of the block.
const void * data
Actual block data.
struct GNUNET_HashCode key
Key of the block.
size_t data_size
Number of bytes in data.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
Internal state of the datacache library.
Definition datacache.c:41
A 512-bit hashcode.
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).
size_t xquery_size
Number of bytes in xquery.