aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/gnunet-service-dht_datacache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/gnunet-service-dht_datacache.c')
-rw-r--r--src/service/dht/gnunet-service-dht_datacache.c291
1 files changed, 291 insertions, 0 deletions
diff --git a/src/service/dht/gnunet-service-dht_datacache.c b/src/service/dht/gnunet-service-dht_datacache.c
new file mode 100644
index 000000000..dcb6308a9
--- /dev/null
+++ b/src/service/dht/gnunet-service-dht_datacache.c
@@ -0,0 +1,291 @@
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 */
20/**
21 * @file dht/gnunet-service-dht_datacache.c
22 * @brief GNUnet DHT service's datacache integration
23 * @author Christian Grothoff
24 * @author Nathan Evans
25 */
26#include "platform.h"
27#include "gnunet_datacache_lib.h"
28#include "gnunet-service-dht_datacache.h"
29#include "gnunet-service-dht_neighbours.h"
30#include "gnunet-service-dht_routing.h"
31#include "gnunet-service-dht.h"
32
33#define LOG(kind, ...) GNUNET_log_from (kind, "dht-dhtcache", __VA_ARGS__)
34
35/**
36 * How many "closest" results to we return for migration when
37 * asked (at most)?
38 */
39#define NUM_CLOSEST 4
40
41
42/**
43 * Handle to the datacache service (for inserting/retrieving data)
44 */
45static struct GNUNET_DATACACHE_Handle *datacache;
46
47
48void
49GDS_DATACACHE_handle_put (const struct GNUNET_DATACACHE_Block *bd)
50{
51 struct GNUNET_HashCode xor;
52 enum GNUNET_GenericReturnValue r;
53
54 if (NULL == datacache)
55 {
56 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
57 "PUT request received, but have no datacache!\n");
58 return;
59 }
60 if (bd->data_size >= GNUNET_MAX_MESSAGE_SIZE)
61 {
62 GNUNET_break (0);
63 return;
64 }
65 /* Put size is actual data size plus struct overhead plus path length (if any) */
66 GNUNET_STATISTICS_update (GDS_stats,
67 "# ITEMS stored in datacache",
68 1,
69 GNUNET_NO);
70 GNUNET_CRYPTO_hash_xor (&bd->key,
71 &GDS_my_identity_hash,
72 &xor);
73 r = GNUNET_DATACACHE_put (datacache,
74 GNUNET_CRYPTO_hash_count_leading_zeros (&xor),
75 bd);
76 LOG (GNUNET_ERROR_TYPE_DEBUG,
77 "DATACACHE PUT for key %s [%lu] completed (%d) after %u hops\n",
78 GNUNET_h2s (&bd->key),
79 (unsigned long) bd->data_size,
80 r,
81 bd->put_path_length);
82}
83
84
85/**
86 * Context containing information about a GET request.
87 */
88struct GetRequestContext
89{
90 /**
91 * extended query (see gnunet_block_lib.h).
92 */
93 const void *xquery;
94
95 /**
96 * The key this request was about
97 */
98 struct GNUNET_HashCode key;
99
100 /**
101 * Block group to use to evaluate replies (updated)
102 */
103 struct GNUNET_BLOCK_Group *bg;
104
105 /**
106 * Function to call on results.
107 */
108 GDS_DATACACHE_GetCallback gc;
109
110 /**
111 * Closure for @e gc.
112 */
113 void *gc_cls;
114
115 /**
116 * Number of bytes in xquery.
117 */
118 size_t xquery_size;
119
120 /**
121 * Return value to give back.
122 */
123 enum GNUNET_BLOCK_ReplyEvaluationResult eval;
124};
125
126
127/**
128 * Iterator for local get request results,
129 *
130 * @param cls closure for iterator, a `struct GetRequestContext`
131 * @param bd block data
132 * @return #GNUNET_OK to continue iteration, anything else
133 * to stop iteration.
134 */
135static enum GNUNET_GenericReturnValue
136datacache_get_iterator (void *cls,
137 const struct GNUNET_DATACACHE_Block *bd)
138{
139 struct GetRequestContext *ctx = cls;
140 enum GNUNET_BLOCK_ReplyEvaluationResult eval;
141
142 if (GNUNET_TIME_absolute_is_past (bd->expiration_time))
143 {
144 GNUNET_break (0); /* why does datacache return expired values? */
145 return GNUNET_OK; /* skip expired record */
146 }
147 eval
148 = GNUNET_BLOCK_check_reply (GDS_block_context,
149 bd->type,
150 ctx->bg,
151 &bd->key,
152 ctx->xquery,
153 ctx->xquery_size,
154 bd->data,
155 bd->data_size);
156 LOG (GNUNET_ERROR_TYPE_DEBUG,
157 "Evaluated reply for query %s in datacache, result is %d\n",
158 GNUNET_h2s (&bd->key),
159 (int) eval);
160 ctx->eval = eval;
161 switch (eval)
162 {
163 case GNUNET_BLOCK_REPLY_OK_MORE:
164 case GNUNET_BLOCK_REPLY_OK_LAST:
165 case GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED:
166 /* forward to initiator */
167 GNUNET_STATISTICS_update (GDS_stats,
168 "# Good RESULTS found in datacache",
169 1,
170 GNUNET_NO);
171 ctx->gc (ctx->gc_cls,
172 bd);
173 break;
174 case GNUNET_BLOCK_REPLY_OK_DUPLICATE:
175 GNUNET_STATISTICS_update (GDS_stats,
176 "# Duplicate RESULTS found in datacache",
177 1,
178 GNUNET_NO);
179 break;
180 case GNUNET_BLOCK_REPLY_IRRELEVANT:
181 GNUNET_STATISTICS_update (GDS_stats,
182 "# Irrelevant RESULTS found in datacache",
183 1,
184 GNUNET_NO);
185 break;
186 }
187 return (eval == GNUNET_BLOCK_REPLY_OK_LAST) ? GNUNET_NO : GNUNET_OK;
188}
189
190
191enum GNUNET_BLOCK_ReplyEvaluationResult
192GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
193 enum GNUNET_BLOCK_Type type,
194 const void *xquery,
195 size_t xquery_size,
196 struct GNUNET_BLOCK_Group *bg,
197 GDS_DATACACHE_GetCallback gc,
198 void *gc_cls)
199{
200 struct GetRequestContext ctx = {
201 .eval = GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED,
202 .key = *key,
203 .xquery = xquery,
204 .xquery_size = xquery_size,
205 .bg = bg,
206 .gc = gc,
207 .gc_cls = gc_cls
208 };
209 unsigned int r;
210
211 if (NULL == datacache)
212 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
213 GNUNET_STATISTICS_update (GDS_stats,
214 "# GET requests given to datacache",
215 1,
216 GNUNET_NO);
217 r = GNUNET_DATACACHE_get (datacache,
218 key,
219 type,
220 &datacache_get_iterator,
221 &ctx);
222 LOG (GNUNET_ERROR_TYPE_DEBUG,
223 "DATACACHE GET for key %s completed (%d). %u results found.\n",
224 GNUNET_h2s (key),
225 ctx.eval,
226 r);
227 return ctx.eval;
228}
229
230
231enum GNUNET_BLOCK_ReplyEvaluationResult
232GDS_DATACACHE_get_closest (const struct GNUNET_HashCode *key,
233 enum GNUNET_BLOCK_Type type,
234 const void *xquery,
235 size_t xquery_size,
236 struct GNUNET_BLOCK_Group *bg,
237 GDS_DATACACHE_GetCallback cb,
238 void *cb_cls)
239{
240 struct GetRequestContext ctx = {
241 .eval = GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED,
242 .key = *key,
243 .xquery = xquery,
244 .xquery_size = xquery_size,
245 .bg = bg,
246 .gc = cb,
247 .gc_cls = cb_cls
248 };
249 unsigned int r;
250
251 if (NULL == datacache)
252 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
253 GNUNET_STATISTICS_update (GDS_stats,
254 "# GET closest requests given to datacache",
255 1,
256 GNUNET_NO);
257 r = GNUNET_DATACACHE_get_closest (datacache,
258 key,
259 type,
260 NUM_CLOSEST,
261 &datacache_get_iterator,
262 &ctx);
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "DATACACHE approximate GET for key %s completed (%d). %u results found.\n",
265 GNUNET_h2s (key),
266 ctx.eval,
267 r);
268 return ctx.eval;
269}
270
271
272void
273GDS_DATACACHE_init ()
274{
275 datacache = GNUNET_DATACACHE_create (GDS_cfg,
276 "dhtcache");
277}
278
279
280void
281GDS_DATACACHE_done ()
282{
283 if (NULL != datacache)
284 {
285 GNUNET_DATACACHE_destroy (datacache);
286 datacache = NULL;
287 }
288}
289
290
291/* end of gnunet-service-dht_datacache.c */