aboutsummaryrefslogtreecommitdiff
path: root/src/dht/plugin_block_dht.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dht/plugin_block_dht.c')
-rw-r--r--src/dht/plugin_block_dht.c411
1 files changed, 0 insertions, 411 deletions
diff --git a/src/dht/plugin_block_dht.c b/src/dht/plugin_block_dht.c
deleted file mode 100644
index 3dd3dd792..000000000
--- a/src/dht/plugin_block_dht.c
+++ /dev/null
@@ -1,411 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2010, 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/**
22 * @file dht/plugin_block_dht.c
23 * @brief block plugin for DHT internals (right now, find-peer requests only);
24 * other plugins should be used to store "useful" data in the
25 * DHT (see fs block plugin)
26 * @author Christian Grothoff
27 */
28#include "platform.h"
29#include "gnunet_constants.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_hello_uri_lib.h"
32#include "gnunet_block_plugin.h"
33#include "gnunet_block_group_lib.h"
34
35#define DEBUG_DHT GNUNET_EXTRA_LOGGING
36
37/**
38 * Number of bits we set per entry in the bloomfilter.
39 * Do not change!
40 */
41#define BLOOMFILTER_K 16
42
43
44/**
45 * Create a new block group.
46 *
47 * @param ctx block context in which the block group is created
48 * @param type type of the block for which we are creating the group
49 * @param nonce random value used to seed the group creation
50 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
51 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
52 * @param va variable arguments specific to @a type
53 * @return block group handle, NULL if block groups are not supported
54 * by this @a type of block (this is not an error)
55 */
56static struct GNUNET_BLOCK_Group *
57block_plugin_dht_create_group (void *cls,
58 enum GNUNET_BLOCK_Type type,
59 uint32_t nonce,
60 const void *raw_data,
61 size_t raw_data_size,
62 va_list va)
63{
64 unsigned int bf_size;
65 const char *guard;
66
67 guard = va_arg (va, const char *);
68 if (0 == strcmp (guard,
69 "seen-set-size"))
70 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va,
71 unsigned int),
72 BLOOMFILTER_K);
73 else if (0 == strcmp (guard,
74 "filter-size"))
75 bf_size = va_arg (va, unsigned int);
76 else
77 {
78 GNUNET_break (0);
79 bf_size = 8;
80 }
81 GNUNET_break (NULL == va_arg (va, const char *));
82 return GNUNET_BLOCK_GROUP_bf_create (cls,
83 bf_size,
84 BLOOMFILTER_K,
85 type,
86 nonce,
87 raw_data,
88 raw_data_size);
89}
90
91
92/**
93 * Function called to validate a query.
94 *
95 * @param cls closure
96 * @param type block type
97 * @param query original query (hash)
98 * @param xquery extrended query data (can be NULL, depending on type)
99 * @param xquery_size number of bytes in @a xquery
100 * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not
101 */
102static enum GNUNET_GenericReturnValue
103block_plugin_dht_check_query (void *cls,
104 enum GNUNET_BLOCK_Type type,
105 const struct GNUNET_HashCode *query,
106 const void *xquery,
107 size_t xquery_size)
108{
109 switch (type)
110 {
111 case GNUNET_BLOCK_TYPE_DHT_HELLO:
112 if (0 != xquery_size)
113 {
114 GNUNET_break_op (0);
115 return GNUNET_NO;
116 }
117 return GNUNET_OK;
118 case GNUNET_BLOCK_TYPE_DHT_URL_HELLO:
119 if (0 != xquery_size)
120 {
121 GNUNET_break_op (0);
122 return GNUNET_NO;
123 }
124 return GNUNET_OK;
125 default:
126 GNUNET_break (0);
127 return GNUNET_SYSERR;
128 }
129}
130
131
132/**
133 * Function called to validate a block for storage.
134 *
135 * @param cls closure
136 * @param type block type
137 * @param block block data to validate
138 * @param block_size number of bytes in @a block
139 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
140 */
141static enum GNUNET_GenericReturnValue
142block_plugin_dht_check_block (void *cls,
143 enum GNUNET_BLOCK_Type type,
144 const void *block,
145 size_t block_size)
146{
147 switch (type)
148 {
149 case GNUNET_BLOCK_TYPE_DHT_HELLO:
150 {
151 const struct GNUNET_HELLO_Message *hello;
152 struct GNUNET_PeerIdentity pid;
153 const struct GNUNET_MessageHeader *msg;
154
155 if (block_size < sizeof(struct GNUNET_MessageHeader))
156 {
157 GNUNET_break_op (0);
158 return GNUNET_NO;
159 }
160 msg = block;
161 if (block_size != ntohs (msg->size))
162 {
163 GNUNET_break_op (0);
164 return GNUNET_NO;
165 }
166 hello = block;
167 if (GNUNET_OK !=
168 GNUNET_HELLO_get_id (hello,
169 &pid))
170 {
171 GNUNET_break_op (0);
172 return GNUNET_NO;
173 }
174 return GNUNET_OK;
175 }
176 case GNUNET_BLOCK_TYPE_DHT_URL_HELLO:
177 {
178 struct GNUNET_HELLO_Builder *b;
179 struct GNUNET_PeerIdentity pid;
180 struct GNUNET_HashCode h_pid;
181
182 b = GNUNET_HELLO_builder_from_block (block,
183 block_size);
184 if (NULL == b)
185 {
186 GNUNET_break (0);
187 return GNUNET_NO;
188 }
189 GNUNET_HELLO_builder_iterate (b,
190 &pid,
191 NULL, NULL);
192 GNUNET_CRYPTO_hash (&pid,
193 sizeof (pid),
194 &h_pid);
195 GNUNET_HELLO_builder_free (b);
196 return GNUNET_OK;
197 }
198 default:
199 GNUNET_break (0);
200 return GNUNET_SYSERR;
201 }
202}
203
204
205/**
206 * Function called to validate a reply to a request. Note that it is assumed
207 * that the reply has already been matched to the key (and signatures checked)
208 * as it would be done with the GetKeyFunction and the
209 * BlockEvaluationFunction.
210 *
211 * @param cls closure
212 * @param type block type
213 * @param group which block group to use for evaluation
214 * @param query original query (hash)
215 * @param xquery extrended query data (can be NULL, depending on type)
216 * @param xquery_size number of bytes in @a xquery
217 * @param reply_block response to validate
218 * @param reply_block_size number of bytes in @a reply_block
219 * @return characterization of result
220 */
221static enum GNUNET_BLOCK_ReplyEvaluationResult
222block_plugin_dht_check_reply (
223 void *cls,
224 enum GNUNET_BLOCK_Type type,
225 struct GNUNET_BLOCK_Group *group,
226 const struct GNUNET_HashCode *query,
227 const void *xquery,
228 size_t xquery_size,
229 const void *reply_block,
230 size_t reply_block_size)
231{
232 switch (type)
233 {
234 case GNUNET_BLOCK_TYPE_DHT_HELLO:
235 {
236 const struct GNUNET_MessageHeader *msg = reply_block;
237 const struct GNUNET_HELLO_Message *hello = reply_block;
238 struct GNUNET_PeerIdentity pid;
239 struct GNUNET_HashCode phash;
240
241 GNUNET_assert (reply_block_size >= sizeof(struct GNUNET_MessageHeader));
242 GNUNET_assert (reply_block_size == ntohs (msg->size));
243 GNUNET_assert (GNUNET_OK ==
244 GNUNET_HELLO_get_id (hello,
245 &pid));
246 GNUNET_CRYPTO_hash (&pid,
247 sizeof(pid),
248 &phash);
249 if (GNUNET_YES ==
250 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
251 &phash))
252 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
253 return GNUNET_BLOCK_REPLY_OK_MORE;
254 }
255 case GNUNET_BLOCK_TYPE_DHT_URL_HELLO:
256 {
257 struct GNUNET_HELLO_Builder *b;
258 struct GNUNET_PeerIdentity pid;
259 struct GNUNET_HashCode h_pid;
260
261 b = GNUNET_HELLO_builder_from_block (reply_block,
262 reply_block_size);
263 GNUNET_assert (NULL != b);
264 GNUNET_HELLO_builder_iterate (b,
265 &pid,
266 NULL, NULL);
267 GNUNET_CRYPTO_hash (&pid,
268 sizeof (pid),
269 &h_pid);
270 GNUNET_HELLO_builder_free (b);
271 if (GNUNET_YES ==
272 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
273 &h_pid))
274 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
275 return GNUNET_BLOCK_REPLY_OK_MORE;
276 }
277 default:
278 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
279 }
280}
281
282
283/**
284 * Function called to obtain the key for a block.
285 *
286 * @param cls closure
287 * @param type block type
288 * @param block block to get the key for
289 * @param block_size number of bytes @a block
290 * @param[out] key set to the key (query) for the given block
291 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
292 * (or if extracting a key from a block of this type does not work)
293 */
294static enum GNUNET_GenericReturnValue
295block_plugin_dht_get_key (void *cls,
296 enum GNUNET_BLOCK_Type type,
297 const void *block,
298 size_t block_size,
299 struct GNUNET_HashCode *key)
300{
301 switch (type)
302 {
303 case GNUNET_BLOCK_TYPE_DHT_HELLO:
304 {
305 const struct GNUNET_MessageHeader *msg;
306 const struct GNUNET_HELLO_Message *hello;
307 struct GNUNET_PeerIdentity *pid;
308
309 if (block_size < sizeof(struct GNUNET_MessageHeader))
310 {
311 GNUNET_break_op (0);
312 memset (key,
313 0,
314 sizeof (*key));
315 return GNUNET_OK;
316 }
317 msg = block;
318 if (block_size != ntohs (msg->size))
319 {
320 GNUNET_break_op (0);
321 memset (key,
322 0,
323 sizeof (*key));
324 return GNUNET_OK;
325 }
326 hello = block;
327 memset (key,
328 0,
329 sizeof(*key));
330 pid = (struct GNUNET_PeerIdentity *) key;
331 if (GNUNET_OK !=
332 GNUNET_HELLO_get_id (hello,
333 pid))
334 {
335 GNUNET_break_op (0);
336 memset (key,
337 0,
338 sizeof (*key));
339 return GNUNET_OK;
340 }
341 return GNUNET_OK;
342 }
343 case GNUNET_BLOCK_TYPE_DHT_URL_HELLO:
344 {
345 struct GNUNET_HELLO_Builder *b;
346 struct GNUNET_PeerIdentity pid;
347
348 b = GNUNET_HELLO_builder_from_block (block,
349 block_size);
350 if (NULL == b)
351 {
352 GNUNET_break (0);
353 memset (key,
354 0,
355 sizeof (*key));
356 return GNUNET_OK;
357 }
358 GNUNET_HELLO_builder_iterate (b,
359 &pid,
360 NULL, NULL);
361 GNUNET_CRYPTO_hash (&pid,
362 sizeof (pid),
363 key);
364 GNUNET_HELLO_builder_free (b);
365 return GNUNET_OK;
366 }
367 default:
368 GNUNET_break (0);
369 return GNUNET_SYSERR;
370 }
371}
372
373
374/**
375 * Entry point for the plugin.
376 */
377void *
378libgnunet_plugin_block_dht_init (void *cls)
379{
380 static enum GNUNET_BLOCK_Type types[] = {
381 GNUNET_BLOCK_TYPE_DHT_HELLO,
382 GNUNET_BLOCK_TYPE_DHT_URL_HELLO,
383 GNUNET_BLOCK_TYPE_ANY /* end of list */
384 };
385 struct GNUNET_BLOCK_PluginFunctions *api;
386
387 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
388 api->get_key = &block_plugin_dht_get_key;
389 api->check_query = &block_plugin_dht_check_query;
390 api->check_block = &block_plugin_dht_check_block;
391 api->check_reply = &block_plugin_dht_check_reply;
392 api->create_group = &block_plugin_dht_create_group;
393 api->types = types;
394 return api;
395}
396
397
398/**
399 * Exit point from the plugin.
400 */
401void *
402libgnunet_plugin_block_dht_done (void *cls)
403{
404 struct GNUNET_BLOCK_PluginFunctions *api = cls;
405
406 GNUNET_free (api);
407 return NULL;
408}
409
410
411/* end of plugin_block_dht.c */