diff options
Diffstat (limited to 'src/plugin/regex/plugin_block_regex.c')
-rw-r--r-- | src/plugin/regex/plugin_block_regex.c | 380 |
1 files changed, 380 insertions, 0 deletions
diff --git a/src/plugin/regex/plugin_block_regex.c b/src/plugin/regex/plugin_block_regex.c new file mode 100644 index 000000000..5f23a32df --- /dev/null +++ b/src/plugin/regex/plugin_block_regex.c | |||
@@ -0,0 +1,380 @@ | |||
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 raw_data optional serialized prior state of the group, NULL if unavailable/fresh | ||
53 | * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh | ||
54 | * @param va variable arguments specific to @a type | ||
55 | * @return block group handle, NULL if block groups are not supported | ||
56 | * by this @a type of block (this is not an error) | ||
57 | */ | ||
58 | static struct GNUNET_BLOCK_Group * | ||
59 | block_plugin_regex_create_group (void *cls, | ||
60 | enum GNUNET_BLOCK_Type type, | ||
61 | const void *raw_data, | ||
62 | size_t raw_data_size, | ||
63 | va_list va) | ||
64 | { | ||
65 | unsigned int bf_size; | ||
66 | const char *guard; | ||
67 | |||
68 | guard = va_arg (va, const char *); | ||
69 | if (0 == strcmp (guard, | ||
70 | "seen-set-size")) | ||
71 | bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned | ||
72 | int), | ||
73 | BLOOMFILTER_K); | ||
74 | else if (0 == strcmp (guard, | ||
75 | "filter-size")) | ||
76 | bf_size = va_arg (va, unsigned int); | ||
77 | else | ||
78 | { | ||
79 | GNUNET_break (0); | ||
80 | bf_size = REGEX_BF_SIZE; | ||
81 | } | ||
82 | GNUNET_break (NULL == va_arg (va, const char *)); | ||
83 | return GNUNET_BLOCK_GROUP_bf_create (cls, | ||
84 | bf_size, | ||
85 | BLOOMFILTER_K, | ||
86 | type, | ||
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 ctx block context | ||
97 | * @param type block type | ||
98 | * @param query original query (hash) | ||
99 | * @param xquery extrended query data (can be NULL, depending on type) | ||
100 | * @param xquery_size number of bytes in @a xquery | ||
101 | * @return #GNUNET_OK if the query is fine, #GNUNET_NO if not | ||
102 | */ | ||
103 | static enum GNUNET_GenericReturnValue | ||
104 | block_plugin_regex_check_query (void *cls, | ||
105 | enum GNUNET_BLOCK_Type type, | ||
106 | const struct GNUNET_HashCode *query, | ||
107 | const void *xquery, | ||
108 | size_t xquery_size) | ||
109 | { | ||
110 | switch (type) | ||
111 | { | ||
112 | case GNUNET_BLOCK_TYPE_REGEX: | ||
113 | if (0 != xquery_size) | ||
114 | { | ||
115 | const char *s; | ||
116 | |||
117 | s = (const char *) xquery; | ||
118 | if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */ | ||
119 | { | ||
120 | GNUNET_break_op (0); | ||
121 | return GNUNET_NO; | ||
122 | } | ||
123 | } | ||
124 | return GNUNET_OK; | ||
125 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: | ||
126 | if (0 != xquery_size) | ||
127 | { | ||
128 | GNUNET_break_op (0); | ||
129 | return GNUNET_NO; | ||
130 | } | ||
131 | return GNUNET_OK; | ||
132 | default: | ||
133 | GNUNET_break (0); | ||
134 | return GNUNET_SYSERR; | ||
135 | } | ||
136 | } | ||
137 | |||
138 | |||
139 | /** | ||
140 | * Function called to validate a block for storage. | ||
141 | * | ||
142 | * @param cls closure | ||
143 | * @param type block type | ||
144 | * @param block block data to validate | ||
145 | * @param block_size number of bytes in @a block | ||
146 | * @return #GNUNET_OK if the block is fine, #GNUNET_NO if not | ||
147 | */ | ||
148 | static enum GNUNET_GenericReturnValue | ||
149 | block_plugin_regex_check_block (void *cls, | ||
150 | enum GNUNET_BLOCK_Type type, | ||
151 | const void *block, | ||
152 | size_t block_size) | ||
153 | { | ||
154 | switch (type) | ||
155 | { | ||
156 | case GNUNET_BLOCK_TYPE_REGEX: | ||
157 | if (GNUNET_SYSERR == | ||
158 | REGEX_BLOCK_check (block, | ||
159 | block_size, | ||
160 | NULL, | ||
161 | NULL)) | ||
162 | return GNUNET_NO; | ||
163 | return GNUNET_OK; | ||
164 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: | ||
165 | { | ||
166 | const struct RegexAcceptBlock *rba; | ||
167 | |||
168 | if (sizeof(struct RegexAcceptBlock) != block_size) | ||
169 | { | ||
170 | GNUNET_break_op (0); | ||
171 | return GNUNET_NO; | ||
172 | } | ||
173 | rba = block; | ||
174 | if (ntohl (rba->purpose.size) != | ||
175 | sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) | ||
176 | + sizeof(struct GNUNET_TIME_AbsoluteNBO) | ||
177 | + sizeof(struct GNUNET_HashCode)) | ||
178 | { | ||
179 | GNUNET_break_op (0); | ||
180 | return GNUNET_NO; | ||
181 | } | ||
182 | if (GNUNET_TIME_absolute_is_past (GNUNET_TIME_absolute_ntoh ( | ||
183 | rba->expiration_time))) | ||
184 | { | ||
185 | return GNUNET_NO; | ||
186 | } | ||
187 | if (GNUNET_OK != | ||
188 | GNUNET_CRYPTO_eddsa_verify_ (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT, | ||
189 | &rba->purpose, | ||
190 | &rba->signature, | ||
191 | &rba->peer.public_key)) | ||
192 | { | ||
193 | GNUNET_break_op (0); | ||
194 | return GNUNET_NO; | ||
195 | } | ||
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 | */ | ||
221 | static enum GNUNET_BLOCK_ReplyEvaluationResult | ||
222 | block_plugin_regex_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 | struct GNUNET_HashCode chash; | ||
233 | |||
234 | switch (type) | ||
235 | { | ||
236 | case GNUNET_BLOCK_TYPE_REGEX: | ||
237 | if (0 != xquery_size) | ||
238 | { | ||
239 | const char *s; | ||
240 | |||
241 | s = (const char *) xquery; | ||
242 | GNUNET_assert ('\0' == s[xquery_size - 1]); | ||
243 | } | ||
244 | switch (REGEX_BLOCK_check (reply_block, | ||
245 | reply_block_size, | ||
246 | query, | ||
247 | xquery)) | ||
248 | { | ||
249 | case GNUNET_SYSERR: | ||
250 | GNUNET_assert (0); | ||
251 | case GNUNET_NO: | ||
252 | /* xquery mismatch, can happen */ | ||
253 | return GNUNET_BLOCK_REPLY_IRRELEVANT; | ||
254 | default: | ||
255 | break; | ||
256 | } | ||
257 | GNUNET_CRYPTO_hash (reply_block, | ||
258 | reply_block_size, | ||
259 | &chash); | ||
260 | if (GNUNET_YES == | ||
261 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, | ||
262 | &chash)) | ||
263 | return GNUNET_BLOCK_REPLY_OK_DUPLICATE; | ||
264 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
265 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: | ||
266 | { | ||
267 | const struct RegexAcceptBlock *rba; | ||
268 | |||
269 | GNUNET_assert (sizeof(struct RegexAcceptBlock) == reply_block_size); | ||
270 | rba = reply_block; | ||
271 | GNUNET_assert (ntohl (rba->purpose.size) == | ||
272 | sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) | ||
273 | + sizeof(struct GNUNET_TIME_AbsoluteNBO) | ||
274 | + sizeof(struct GNUNET_HashCode)); | ||
275 | GNUNET_CRYPTO_hash (reply_block, | ||
276 | reply_block_size, | ||
277 | &chash); | ||
278 | if (GNUNET_YES == | ||
279 | GNUNET_BLOCK_GROUP_bf_test_and_set (group, | ||
280 | &chash)) | ||
281 | return GNUNET_BLOCK_REPLY_OK_DUPLICATE; | ||
282 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
283 | } | ||
284 | default: | ||
285 | GNUNET_break (0); | ||
286 | return GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED; | ||
287 | } | ||
288 | return GNUNET_BLOCK_REPLY_OK_MORE; | ||
289 | } | ||
290 | |||
291 | |||
292 | /** | ||
293 | * Function called to obtain the key for a block. | ||
294 | * | ||
295 | * @param cls closure | ||
296 | * @param type block type | ||
297 | * @param block block to get the key for | ||
298 | * @param block_size number of bytes in @a block | ||
299 | * @param key set to the key (query) for the given block | ||
300 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported, | ||
301 | * #GNUNET_NO if extracting a key from a block of this type does not work | ||
302 | */ | ||
303 | static enum GNUNET_GenericReturnValue | ||
304 | block_plugin_regex_get_key (void *cls, | ||
305 | enum GNUNET_BLOCK_Type type, | ||
306 | const void *block, | ||
307 | size_t block_size, | ||
308 | struct GNUNET_HashCode *key) | ||
309 | { | ||
310 | switch (type) | ||
311 | { | ||
312 | case GNUNET_BLOCK_TYPE_REGEX: | ||
313 | if (GNUNET_OK != | ||
314 | REGEX_BLOCK_get_key (block, | ||
315 | block_size, | ||
316 | key)) | ||
317 | { | ||
318 | GNUNET_break_op (0); | ||
319 | memset (key, | ||
320 | 0, | ||
321 | sizeof (*key)); | ||
322 | return GNUNET_OK; | ||
323 | } | ||
324 | return GNUNET_OK; | ||
325 | case GNUNET_BLOCK_TYPE_REGEX_ACCEPT: | ||
326 | if (sizeof(struct RegexAcceptBlock) != block_size) | ||
327 | { | ||
328 | GNUNET_break_op (0); | ||
329 | memset (key, | ||
330 | 0, | ||
331 | sizeof (*key)); | ||
332 | return GNUNET_OK; | ||
333 | } | ||
334 | *key = ((struct RegexAcceptBlock *) block)->key; | ||
335 | return GNUNET_OK; | ||
336 | default: | ||
337 | GNUNET_break (0); | ||
338 | return GNUNET_SYSERR; | ||
339 | } | ||
340 | } | ||
341 | |||
342 | |||
343 | /** | ||
344 | * Entry point for the plugin. | ||
345 | */ | ||
346 | void * | ||
347 | libgnunet_plugin_block_regex_init (void *cls) | ||
348 | { | ||
349 | static const enum GNUNET_BLOCK_Type types[] = { | ||
350 | GNUNET_BLOCK_TYPE_REGEX, | ||
351 | GNUNET_BLOCK_TYPE_REGEX_ACCEPT, | ||
352 | GNUNET_BLOCK_TYPE_ANY /* end of list */ | ||
353 | }; | ||
354 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
355 | |||
356 | api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); | ||
357 | api->get_key = &block_plugin_regex_get_key; | ||
358 | api->check_query = &block_plugin_regex_check_query; | ||
359 | api->check_block = &block_plugin_regex_check_block; | ||
360 | api->check_reply = &block_plugin_regex_check_reply; | ||
361 | api->create_group = &block_plugin_regex_create_group; | ||
362 | api->types = types; | ||
363 | return api; | ||
364 | } | ||
365 | |||
366 | |||
367 | /** | ||
368 | * Exit point from the plugin. | ||
369 | */ | ||
370 | void * | ||
371 | libgnunet_plugin_block_regex_done (void *cls) | ||
372 | { | ||
373 | struct GNUNET_BLOCK_PluginFunctions *api = cls; | ||
374 | |||
375 | GNUNET_free (api); | ||
376 | return NULL; | ||
377 | } | ||
378 | |||
379 | |||
380 | /* end of plugin_block_regex.c */ | ||