aboutsummaryrefslogtreecommitdiff
path: root/src/dht/plugin_block_dht.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2023-10-18 20:19:34 +0200
committerMartin Schanzenbach <schanzen@gnunet.org>2023-10-18 20:19:34 +0200
commit498cb28b16dbebbacb674fa166aeac3fe4ea9922 (patch)
treecd1f59025a55f263f297ca706d90b1a395f6e163 /src/dht/plugin_block_dht.c
parent11949e419fd7230431646703ac0cf8e34f615673 (diff)
downloadgnunet-498cb28b16dbebbacb674fa166aeac3fe4ea9922.tar.gz
gnunet-498cb28b16dbebbacb674fa166aeac3fe4ea9922.zip
BUILD: Move dht/datastore to service/cli
Diffstat (limited to 'src/dht/plugin_block_dht.c')
-rw-r--r--src/dht/plugin_block_dht.c311
1 files changed, 0 insertions, 311 deletions
diff --git a/src/dht/plugin_block_dht.c b/src/dht/plugin_block_dht.c
deleted file mode 100644
index aa5ffc719..000000000
--- a/src/dht/plugin_block_dht.c
+++ /dev/null
@@ -1,311 +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_uri_lib.h"
31#include "gnunet_block_plugin.h"
32#include "gnunet_block_group_lib.h"
33
34#define DEBUG_DHT GNUNET_EXTRA_LOGGING
35
36/**
37 * Number of bits we set per entry in the bloomfilter.
38 * Do not change!
39 */
40#define BLOOMFILTER_K 16
41
42
43/**
44 * Create a new block group.
45 *
46 * @param ctx block context in which the block group is created
47 * @param type type of the block for which we are creating the group
48 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
49 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
50 * @param va variable arguments specific to @a type
51 * @return block group handle, NULL if block groups are not supported
52 * by this @a type of block (this is not an error)
53 */
54static struct GNUNET_BLOCK_Group *
55block_plugin_dht_create_group (void *cls,
56 enum GNUNET_BLOCK_Type type,
57 const void *raw_data,
58 size_t raw_data_size,
59 va_list va)
60{
61 unsigned int bf_size;
62 const char *guard;
63
64 guard = va_arg (va, const char *);
65 if (0 == strcmp (guard,
66 "seen-set-size"))
67 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va,
68 unsigned int),
69 BLOOMFILTER_K);
70 else if (0 == strcmp (guard,
71 "filter-size"))
72 bf_size = va_arg (va, unsigned int);
73 else
74 {
75 GNUNET_break (0);
76 bf_size = 8;
77 }
78 GNUNET_break (NULL == va_arg (va, const char *));
79 return GNUNET_BLOCK_GROUP_bf_create (cls,
80 bf_size,
81 BLOOMFILTER_K,
82 type,
83 raw_data,
84 raw_data_size);
85}
86
87
88/**
89 * Function called to validate a query.
90 *
91 * @param cls closure
92 * @param type block type
93 * @param query original query (hash)
94 * @param xquery extrended query data (can be NULL, depending on type)
95 * @param xquery_size number of bytes in @a xquery
96 * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not
97 */
98static enum GNUNET_GenericReturnValue
99block_plugin_dht_check_query (void *cls,
100 enum GNUNET_BLOCK_Type type,
101 const struct GNUNET_HashCode *query,
102 const void *xquery,
103 size_t xquery_size)
104{
105 switch (type)
106 {
107 case GNUNET_BLOCK_TYPE_DHT_HELLO:
108 if (0 != xquery_size)
109 {
110 GNUNET_break_op (0);
111 return GNUNET_NO;
112 }
113 return GNUNET_OK;
114 default:
115 GNUNET_break (0);
116 return GNUNET_SYSERR;
117 }
118}
119
120
121/**
122 * Function called to validate a block for storage.
123 *
124 * @param cls closure
125 * @param type block type
126 * @param block block data to validate
127 * @param block_size number of bytes in @a block
128 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
129 */
130static enum GNUNET_GenericReturnValue
131block_plugin_dht_check_block (void *cls,
132 enum GNUNET_BLOCK_Type type,
133 const void *block,
134 size_t block_size)
135{
136 switch (type)
137 {
138 case GNUNET_BLOCK_TYPE_DHT_HELLO:
139 {
140 struct GNUNET_HELLO_Builder *b;
141 struct GNUNET_PeerIdentity pid;
142 struct GNUNET_HashCode h_pid;
143
144 b = GNUNET_HELLO_builder_from_block (block,
145 block_size);
146 if (NULL == b)
147 {
148 GNUNET_break (0);
149 return GNUNET_NO;
150 }
151 GNUNET_HELLO_builder_iterate (b,
152 &pid,
153 NULL, NULL);
154 GNUNET_CRYPTO_hash (&pid,
155 sizeof (pid),
156 &h_pid);
157 GNUNET_HELLO_builder_free (b);
158 return GNUNET_OK;
159 }
160 default:
161 GNUNET_break (0);
162 return GNUNET_SYSERR;
163 }
164}
165
166
167/**
168 * Function called to validate a reply to a request. Note that it is assumed
169 * that the reply has already been matched to the key (and signatures checked)
170 * as it would be done with the GetKeyFunction and the
171 * BlockEvaluationFunction.
172 *
173 * @param cls closure
174 * @param type block type
175 * @param group which block group to use for evaluation
176 * @param query original query (hash)
177 * @param xquery extrended query data (can be NULL, depending on type)
178 * @param xquery_size number of bytes in @a xquery
179 * @param reply_block response to validate
180 * @param reply_block_size number of bytes in @a reply_block
181 * @return characterization of result
182 */
183static enum GNUNET_BLOCK_ReplyEvaluationResult
184block_plugin_dht_check_reply (
185 void *cls,
186 enum GNUNET_BLOCK_Type type,
187 struct GNUNET_BLOCK_Group *group,
188 const struct GNUNET_HashCode *query,
189 const void *xquery,
190 size_t xquery_size,
191 const void *reply_block,
192 size_t reply_block_size)
193{
194 switch (type)
195 {
196 case GNUNET_BLOCK_TYPE_DHT_HELLO:
197 {
198 struct GNUNET_HELLO_Builder *b;
199 struct GNUNET_PeerIdentity pid;
200 struct GNUNET_HashCode h_pid;
201
202 b = GNUNET_HELLO_builder_from_block (reply_block,
203 reply_block_size);
204 GNUNET_assert (NULL != b);
205 GNUNET_HELLO_builder_iterate (b,
206 &pid,
207 NULL, NULL);
208 GNUNET_CRYPTO_hash (&pid,
209 sizeof (pid),
210 &h_pid);
211 GNUNET_HELLO_builder_free (b);
212 if (GNUNET_YES ==
213 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
214 &h_pid))
215 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
216 return GNUNET_BLOCK_REPLY_OK_MORE;
217 }
218 default:
219 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
220 }
221}
222
223
224/**
225 * Function called to obtain the key for a block.
226 *
227 * @param cls closure
228 * @param type block type
229 * @param block block to get the key for
230 * @param block_size number of bytes @a block
231 * @param[out] key set to the key (query) for the given block
232 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
233 * (or if extracting a key from a block of this type does not work)
234 */
235static enum GNUNET_GenericReturnValue
236block_plugin_dht_get_key (void *cls,
237 enum GNUNET_BLOCK_Type type,
238 const void *block,
239 size_t block_size,
240 struct GNUNET_HashCode *key)
241{
242 switch (type)
243 {
244 case GNUNET_BLOCK_TYPE_DHT_HELLO:
245 {
246 struct GNUNET_HELLO_Builder *b;
247 struct GNUNET_PeerIdentity pid;
248
249 b = GNUNET_HELLO_builder_from_block (block,
250 block_size);
251 if (NULL == b)
252 {
253 GNUNET_break (0);
254 memset (key,
255 0,
256 sizeof (*key));
257 return GNUNET_OK;
258 }
259 GNUNET_HELLO_builder_iterate (b,
260 &pid,
261 NULL, NULL);
262 GNUNET_CRYPTO_hash (&pid,
263 sizeof (pid),
264 key);
265 GNUNET_HELLO_builder_free (b);
266 return GNUNET_OK;
267 }
268 default:
269 GNUNET_break (0);
270 return GNUNET_SYSERR;
271 }
272}
273
274
275/**
276 * Entry point for the plugin.
277 */
278void *
279libgnunet_plugin_block_dht_init (void *cls)
280{
281 static enum GNUNET_BLOCK_Type types[] = {
282 GNUNET_BLOCK_TYPE_DHT_HELLO,
283 GNUNET_BLOCK_TYPE_ANY /* end of list */
284 };
285 struct GNUNET_BLOCK_PluginFunctions *api;
286
287 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
288 api->get_key = &block_plugin_dht_get_key;
289 api->check_query = &block_plugin_dht_check_query;
290 api->check_block = &block_plugin_dht_check_block;
291 api->check_reply = &block_plugin_dht_check_reply;
292 api->create_group = &block_plugin_dht_create_group;
293 api->types = types;
294 return api;
295}
296
297
298/**
299 * Exit point from the plugin.
300 */
301void *
302libgnunet_plugin_block_dht_done (void *cls)
303{
304 struct GNUNET_BLOCK_PluginFunctions *api = cls;
305
306 GNUNET_free (api);
307 return NULL;
308}
309
310
311/* end of plugin_block_dht.c */