diff options
Diffstat (limited to 'src/dht/gnunet-service-dht_datacache.c')
-rw-r--r-- | src/dht/gnunet-service-dht_datacache.c | 318 |
1 files changed, 0 insertions, 318 deletions
diff --git a/src/dht/gnunet-service-dht_datacache.c b/src/dht/gnunet-service-dht_datacache.c deleted file mode 100644 index be0a6db81..000000000 --- a/src/dht/gnunet-service-dht_datacache.c +++ /dev/null | |||
@@ -1,318 +0,0 @@ | |||
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 | */ | ||
45 | static struct GNUNET_DATACACHE_Handle *datacache; | ||
46 | |||
47 | |||
48 | void | ||
49 | GDS_DATACACHE_handle_put (const struct GDS_DATACACHE_BlockData *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 | &bd->key, | ||
75 | GNUNET_CRYPTO_hash_count_leading_zeros (&xor), | ||
76 | bd->data_size, | ||
77 | bd->data, | ||
78 | bd->type, | ||
79 | bd->expiration_time, | ||
80 | bd->put_path_length, | ||
81 | bd->put_path); | ||
82 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
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 | |||
91 | /** | ||
92 | * Context containing information about a GET request. | ||
93 | */ | ||
94 | struct GetRequestContext | ||
95 | { | ||
96 | /** | ||
97 | * extended query (see gnunet_block_lib.h). | ||
98 | */ | ||
99 | const void *xquery; | ||
100 | |||
101 | /** | ||
102 | * The key this request was about | ||
103 | */ | ||
104 | struct GNUNET_HashCode key; | ||
105 | |||
106 | /** | ||
107 | * Block group to use to evaluate replies (updated) | ||
108 | */ | ||
109 | struct GNUNET_BLOCK_Group *bg; | ||
110 | |||
111 | /** | ||
112 | * Function to call on results. | ||
113 | */ | ||
114 | GDS_DATACACHE_GetCallback gc; | ||
115 | |||
116 | /** | ||
117 | * Closure for @e gc. | ||
118 | */ | ||
119 | void *gc_cls; | ||
120 | |||
121 | /** | ||
122 | * Number of bytes in xquery. | ||
123 | */ | ||
124 | size_t xquery_size; | ||
125 | |||
126 | /** | ||
127 | * Return value to give back. | ||
128 | */ | ||
129 | enum GNUNET_BLOCK_ReplyEvaluationResult eval; | ||
130 | }; | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Iterator for local get request results, | ||
135 | * | ||
136 | * @param cls closure for iterator, a `struct GetRequestContext` | ||
137 | * @param exp when does this value expire? | ||
138 | * @param key the key this data is stored under | ||
139 | * @param data_size the size of the data identified by key | ||
140 | * @param data the actual data | ||
141 | * @param type the type of the @a data | ||
142 | * @param put_path_length number of peers in @a put_path | ||
143 | * @param put_path path the reply took on put | ||
144 | * @return #GNUNET_OK to continue iteration, anything else | ||
145 | * to stop iteration. | ||
146 | */ | ||
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; | ||
158 | enum GNUNET_BLOCK_ReplyEvaluationResult eval; | ||
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 | |||
169 | if (GNUNET_TIME_absolute_is_past (exp)) | ||
170 | { | ||
171 | GNUNET_break (0); /* why does datacache return expired values? */ | ||
172 | return GNUNET_OK; /* skip expired record */ | ||
173 | } | ||
174 | eval | ||
175 | = GNUNET_BLOCK_check_reply (GDS_block_context, | ||
176 | bd.type, | ||
177 | ctx->bg, | ||
178 | &bd.key, | ||
179 | ctx->xquery, | ||
180 | ctx->xquery_size, | ||
181 | bd.data, | ||
182 | bd.data_size); | ||
183 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
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 | { | ||
190 | case GNUNET_BLOCK_REPLY_OK_MORE: | ||
191 | case GNUNET_BLOCK_REPLY_OK_LAST: | ||
192 | case GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED: | ||
193 | /* forward to initiator */ | ||
194 | GNUNET_STATISTICS_update (GDS_stats, | ||
195 | "# Good RESULTS found in datacache", | ||
196 | 1, | ||
197 | GNUNET_NO); | ||
198 | ctx->gc (ctx->gc_cls, | ||
199 | &bd); | ||
200 | break; | ||
201 | case GNUNET_BLOCK_REPLY_OK_DUPLICATE: | ||
202 | GNUNET_STATISTICS_update (GDS_stats, | ||
203 | "# Duplicate RESULTS found in datacache", | ||
204 | 1, | ||
205 | GNUNET_NO); | ||
206 | break; | ||
207 | case GNUNET_BLOCK_REPLY_IRRELEVANT: | ||
208 | GNUNET_STATISTICS_update (GDS_stats, | ||
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 | |||
218 | enum GNUNET_BLOCK_ReplyEvaluationResult | ||
219 | GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key, | ||
220 | enum GNUNET_BLOCK_Type type, | ||
221 | const void *xquery, | ||
222 | size_t xquery_size, | ||
223 | struct GNUNET_BLOCK_Group *bg, | ||
224 | GDS_DATACACHE_GetCallback gc, | ||
225 | void *gc_cls) | ||
226 | { | ||
227 | struct GetRequestContext ctx = { | ||
228 | .eval = GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED, | ||
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) | ||
239 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
240 | GNUNET_STATISTICS_update (GDS_stats, | ||
241 | "# GET requests given to datacache", | ||
242 | 1, | ||
243 | GNUNET_NO); | ||
244 | r = GNUNET_DATACACHE_get (datacache, | ||
245 | key, | ||
246 | type, | ||
247 | &datacache_get_iterator, | ||
248 | &ctx); | ||
249 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
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 | |||
258 | enum GNUNET_BLOCK_ReplyEvaluationResult | ||
259 | GDS_DATACACHE_get_closest (const struct GNUNET_HashCode *key, | ||
260 | enum GNUNET_BLOCK_Type type, | ||
261 | const void *xquery, | ||
262 | size_t xquery_size, | ||
263 | struct GNUNET_BLOCK_Group *bg, | ||
264 | GDS_DATACACHE_GetCallback cb, | ||
265 | void *cb_cls) | ||
266 | { | ||
267 | struct GetRequestContext ctx = { | ||
268 | .eval = GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED, | ||
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) | ||
279 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
280 | GNUNET_STATISTICS_update (GDS_stats, | ||
281 | "# GET closest requests given to datacache", | ||
282 | 1, | ||
283 | GNUNET_NO); | ||
284 | r = GNUNET_DATACACHE_get_closest (datacache, | ||
285 | key, | ||
286 | type, | ||
287 | NUM_CLOSEST, | ||
288 | &datacache_get_iterator, | ||
289 | &ctx); | ||
290 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
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 | ||
300 | GDS_DATACACHE_init () | ||
301 | { | ||
302 | datacache = GNUNET_DATACACHE_create (GDS_cfg, | ||
303 | "dhtcache"); | ||
304 | } | ||
305 | |||
306 | |||
307 | void | ||
308 | GDS_DATACACHE_done () | ||
309 | { | ||
310 | if (NULL != datacache) | ||
311 | { | ||
312 | GNUNET_DATACACHE_destroy (datacache); | ||
313 | datacache = NULL; | ||
314 | } | ||
315 | } | ||
316 | |||
317 | |||
318 | /* end of gnunet-service-dht_datacache.c */ | ||