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