aboutsummaryrefslogtreecommitdiff
path: root/src/plugin/dht/plugin_block_dht.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin/dht/plugin_block_dht.c')
-rw-r--r--src/plugin/dht/plugin_block_dht.c308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/plugin/dht/plugin_block_dht.c b/src/plugin/dht/plugin_block_dht.c
new file mode 100644
index 000000000..ede23ea57
--- /dev/null
+++ b/src/plugin/dht/plugin_block_dht.c
@@ -0,0 +1,308 @@
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 const 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 pid = GNUNET_HELLO_builder_iterate (b,
152 NULL, NULL);
153 GNUNET_CRYPTO_hash (pid,
154 sizeof (*pid),
155 &h_pid);
156 GNUNET_HELLO_builder_free (b);
157 return GNUNET_OK;
158 }
159 default:
160 GNUNET_break (0);
161 return GNUNET_SYSERR;
162 }
163}
164
165
166/**
167 * Function called to validate a reply to a request. Note that it is assumed
168 * that the reply has already been matched to the key (and signatures checked)
169 * as it would be done with the GetKeyFunction and the
170 * BlockEvaluationFunction.
171 *
172 * @param cls closure
173 * @param type block type
174 * @param group which block group to use for evaluation
175 * @param query original query (hash)
176 * @param xquery extrended query data (can be NULL, depending on type)
177 * @param xquery_size number of bytes in @a xquery
178 * @param reply_block response to validate
179 * @param reply_block_size number of bytes in @a reply_block
180 * @return characterization of result
181 */
182static enum GNUNET_BLOCK_ReplyEvaluationResult
183block_plugin_dht_check_reply (
184 void *cls,
185 enum GNUNET_BLOCK_Type type,
186 struct GNUNET_BLOCK_Group *group,
187 const struct GNUNET_HashCode *query,
188 const void *xquery,
189 size_t xquery_size,
190 const void *reply_block,
191 size_t reply_block_size)
192{
193 switch (type)
194 {
195 case GNUNET_BLOCK_TYPE_DHT_HELLO:
196 {
197 struct GNUNET_HELLO_Builder *b;
198 const struct GNUNET_PeerIdentity *pid;
199 struct GNUNET_HashCode h_pid;
200
201 b = GNUNET_HELLO_builder_from_block (reply_block,
202 reply_block_size);
203 GNUNET_assert (NULL != b);
204 pid = GNUNET_HELLO_builder_iterate (b,
205 NULL, NULL);
206 GNUNET_CRYPTO_hash (pid,
207 sizeof (*pid),
208 &h_pid);
209 GNUNET_HELLO_builder_free (b);
210 if (GNUNET_YES ==
211 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
212 &h_pid))
213 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
214 return GNUNET_BLOCK_REPLY_OK_MORE;
215 }
216 default:
217 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
218 }
219}
220
221
222/**
223 * Function called to obtain the key for a block.
224 *
225 * @param cls closure
226 * @param type block type
227 * @param block block to get the key for
228 * @param block_size number of bytes @a block
229 * @param[out] key set to the key (query) for the given block
230 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
231 * (or if extracting a key from a block of this type does not work)
232 */
233static enum GNUNET_GenericReturnValue
234block_plugin_dht_get_key (void *cls,
235 enum GNUNET_BLOCK_Type type,
236 const void *block,
237 size_t block_size,
238 struct GNUNET_HashCode *key)
239{
240 switch (type)
241 {
242 case GNUNET_BLOCK_TYPE_DHT_HELLO:
243 {
244 struct GNUNET_HELLO_Builder *b;
245 const struct GNUNET_PeerIdentity *pid;
246
247 b = GNUNET_HELLO_builder_from_block (block,
248 block_size);
249 if (NULL == b)
250 {
251 GNUNET_break (0);
252 memset (key,
253 0,
254 sizeof (*key));
255 return GNUNET_OK;
256 }
257 pid = GNUNET_HELLO_builder_iterate (b,
258 NULL, NULL);
259 GNUNET_CRYPTO_hash (pid,
260 sizeof (*pid),
261 key);
262 GNUNET_HELLO_builder_free (b);
263 return GNUNET_OK;
264 }
265 default:
266 GNUNET_break (0);
267 return GNUNET_SYSERR;
268 }
269}
270
271
272/**
273 * Entry point for the plugin.
274 */
275void *
276libgnunet_plugin_block_dht_init (void *cls)
277{
278 static enum GNUNET_BLOCK_Type types[] = {
279 GNUNET_BLOCK_TYPE_DHT_HELLO,
280 GNUNET_BLOCK_TYPE_ANY /* end of list */
281 };
282 struct GNUNET_BLOCK_PluginFunctions *api;
283
284 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
285 api->get_key = &block_plugin_dht_get_key;
286 api->check_query = &block_plugin_dht_check_query;
287 api->check_block = &block_plugin_dht_check_block;
288 api->check_reply = &block_plugin_dht_check_reply;
289 api->create_group = &block_plugin_dht_create_group;
290 api->types = types;
291 return api;
292}
293
294
295/**
296 * Exit point from the plugin.
297 */
298void *
299libgnunet_plugin_block_dht_done (void *cls)
300{
301 struct GNUNET_BLOCK_PluginFunctions *api = cls;
302
303 GNUNET_free (api);
304 return NULL;
305}
306
307
308/* end of plugin_block_dht.c */