aboutsummaryrefslogtreecommitdiff
path: root/src/plugin/fs/plugin_block_fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugin/fs/plugin_block_fs.c')
-rw-r--r--src/plugin/fs/plugin_block_fs.c337
1 files changed, 337 insertions, 0 deletions
diff --git a/src/plugin/fs/plugin_block_fs.c b/src/plugin/fs/plugin_block_fs.c
new file mode 100644
index 000000000..bbd0ff57b
--- /dev/null
+++ b/src/plugin/fs/plugin_block_fs.c
@@ -0,0 +1,337 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2010, 2013, 2021 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 fs/plugin_block_fs.c
23 * @brief blocks used for file-sharing
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_block_plugin.h"
28
29#include "gnunet_fs_service.h"
30#include "block_fs.h"
31#include "gnunet_signatures.h"
32#include "gnunet_block_group_lib.h"
33
34
35/**
36 * Number of bits we set per entry in the bloomfilter.
37 * Do not change!
38 */
39#define BLOOMFILTER_K 16
40
41
42/**
43 * Create a new block group.
44 *
45 * @param ctx block context in which the block group is created
46 * @param type type of the block for which we are creating the group
47 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
48 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
49 * @param va variable arguments specific to @a type
50 * @return block group handle, NULL if block groups are not supported
51 * by this @a type of block (this is not an error)
52 */
53static struct GNUNET_BLOCK_Group *
54block_plugin_fs_create_group (void *cls,
55 enum GNUNET_BLOCK_Type type,
56 const void *raw_data,
57 size_t raw_data_size,
58 va_list va)
59{
60 unsigned int size;
61 const char *guard;
62
63 switch (type)
64 {
65 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
66 GNUNET_break (NULL == va_arg (va, const char *));
67 return NULL;
68 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
69 GNUNET_break (NULL == va_arg (va, const char *));
70 return NULL;
71 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
72 guard = va_arg (va, const char *);
73 if (0 == strcmp (guard,
74 "seen-set-size"))
75 {
76 size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned
77 int),
78 BLOOMFILTER_K);
79 }
80 else if (0 == strcmp (guard,
81 "filter-size"))
82 {
83 size = va_arg (va, unsigned int);
84 }
85 else
86 {
87 /* va-args invalid! bad bug, complain! */
88 GNUNET_break (0);
89 size = 8;
90 }
91 if (0 == size)
92 size = raw_data_size; /* not for us to determine, use what we got! */
93 GNUNET_break (NULL == va_arg (va, const char *));
94 return GNUNET_BLOCK_GROUP_bf_create (cls,
95 size,
96 BLOOMFILTER_K,
97 type,
98 raw_data,
99 raw_data_size);
100
101 default:
102 GNUNET_break (NULL == va_arg (va, const char *));
103 GNUNET_break (0);
104 return NULL;
105 }
106}
107
108
109/**
110 * Function called to obtain the key for a block.
111 *
112 * @param cls closure
113 * @param type block type
114 * @param block block to get the key for
115 * @param block_size number of bytes in @a block
116 * @param key set to the key (query) for the given block
117 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
118 * (or if extracting a key from a block of this type does not work)
119 */
120static enum GNUNET_GenericReturnValue
121block_plugin_fs_get_key (void *cls,
122 enum GNUNET_BLOCK_Type type,
123 const void *block,
124 size_t block_size,
125 struct GNUNET_HashCode *key)
126{
127 const struct UBlock *ub;
128
129 switch (type)
130 {
131 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
132 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
133 GNUNET_CRYPTO_hash (block,
134 block_size,
135 key);
136 return GNUNET_OK;
137 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
138 if (block_size < sizeof(struct UBlock))
139 {
140 GNUNET_break_op (0);
141 memset (key,
142 0,
143 sizeof (*key));
144 return GNUNET_OK;
145 }
146 ub = block;
147 GNUNET_CRYPTO_hash (&ub->verification_key,
148 sizeof(ub->verification_key),
149 key);
150 return GNUNET_OK;
151 default:
152 GNUNET_break (0);
153 return GNUNET_SYSERR;
154 }
155}
156
157
158/**
159 * Function called to validate a query.
160 *
161 * @param cls closure
162 * @param type block type
163 * @param query original query (hash)
164 * @param xquery extended query data (can be NULL, depending on type)
165 * @param xquery_size number of bytes in @a xquery
166 * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not
167 */
168static enum GNUNET_GenericReturnValue
169block_plugin_fs_check_query (void *cls,
170 enum GNUNET_BLOCK_Type type,
171 const struct GNUNET_HashCode *query,
172 const void *xquery,
173 size_t xquery_size)
174{
175 switch (type)
176 {
177 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
178 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
179 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
180 if (0 != xquery_size)
181 {
182 GNUNET_break_op (0);
183 return GNUNET_NO;
184 }
185 return GNUNET_OK;
186 default:
187 GNUNET_break (0);
188 return GNUNET_SYSERR;
189 }
190}
191
192
193/**
194 * Function called to validate a block for storage.
195 *
196 * @param cls closure
197 * @param type block type
198 * @param block block data to validate
199 * @param block_size number of bytes in @a block
200 * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not
201 */
202static enum GNUNET_GenericReturnValue
203block_plugin_fs_check_block (void *cls,
204 enum GNUNET_BLOCK_Type type,
205 const void *block,
206 size_t block_size)
207{
208 switch (type)
209 {
210 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
211 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
212 return GNUNET_OK;
213 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
214 {
215 const struct UBlock *ub;
216
217 if (block_size < sizeof(struct UBlock))
218 {
219 GNUNET_break_op (0);
220 return GNUNET_NO;
221 }
222 ub = block;
223 if (block_size !=
224 ntohl (ub->purpose.size)
225 + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
226 {
227 GNUNET_break_op (0);
228 return GNUNET_NO;
229 }
230 if (GNUNET_OK !=
231 GNUNET_CRYPTO_ecdsa_verify_ (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
232 &ub->purpose,
233 &ub->signature,
234 &ub->verification_key))
235 {
236 GNUNET_break_op (0);
237 return GNUNET_NO;
238 }
239 return GNUNET_OK;
240 }
241 default:
242 GNUNET_break (0);
243 return GNUNET_SYSERR;
244 }
245}
246
247
248/**
249 * Function called to validate a reply to a request. Note that it is assumed
250 * that the reply has already been matched to the key (and signatures checked)
251 * as it would be done with the GetKeyFunction and the
252 * BlockEvaluationFunction.
253 *
254 * @param cls closure
255 * @param type block type
256 * @param group which block group to use for evaluation
257 * @param query original query (hash)
258 * @param xquery extrended query data (can be NULL, depending on type)
259 * @param xquery_size number of bytes in @a xquery
260 * @param reply_block response to validate
261 * @param reply_block_size number of bytes in @a reply_block
262 * @return characterization of result
263 */
264static enum GNUNET_BLOCK_ReplyEvaluationResult
265block_plugin_fs_check_reply (void *cls,
266 enum GNUNET_BLOCK_Type type,
267 struct GNUNET_BLOCK_Group *group,
268 const struct GNUNET_HashCode *query,
269 const void *xquery,
270 size_t xquery_size,
271 const void *reply_block,
272 size_t reply_block_size)
273{
274 switch (type)
275 {
276 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
277 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
278 return GNUNET_BLOCK_REPLY_OK_LAST;
279 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
280 {
281 struct GNUNET_HashCode chash;
282
283 GNUNET_CRYPTO_hash (reply_block,
284 reply_block_size,
285 &chash);
286 if (GNUNET_YES ==
287 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
288 &chash))
289 return GNUNET_BLOCK_REPLY_OK_DUPLICATE;
290 return GNUNET_BLOCK_REPLY_OK_MORE;
291 }
292 default:
293 GNUNET_break (0);
294 return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED;
295 }
296}
297
298
299/**
300 * Entry point for the plugin.
301 */
302void *
303libgnunet_plugin_block_fs_init (void *cls)
304{
305 static const enum GNUNET_BLOCK_Type types[] = {
306 GNUNET_BLOCK_TYPE_FS_DBLOCK,
307 GNUNET_BLOCK_TYPE_FS_IBLOCK,
308 GNUNET_BLOCK_TYPE_FS_UBLOCK,
309 GNUNET_BLOCK_TYPE_ANY /* end of list */
310 };
311 struct GNUNET_BLOCK_PluginFunctions *api;
312
313 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
314 api->get_key = &block_plugin_fs_get_key;
315 api->create_group = &block_plugin_fs_create_group;
316 api->check_query = &block_plugin_fs_check_query;
317 api->check_block = &block_plugin_fs_check_block;
318 api->check_reply = &block_plugin_fs_check_reply;
319 api->types = types;
320 return api;
321}
322
323
324/**
325 * Exit point from the plugin.
326 */
327void *
328libgnunet_plugin_block_fs_done (void *cls)
329{
330 struct GNUNET_BLOCK_PluginFunctions *api = cls;
331
332 GNUNET_free (api);
333 return NULL;
334}
335
336
337/* end of plugin_block_fs.c */