2 This file is part of GNUnet
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
20 * @file regex/plugin_block_regex.c
21 * @brief blocks used for regex storage and search
22 * @author Bartlomiej Polot
25 #include "gnunet_block_plugin.h"
26 #include "gnunet_block_group_lib.h"
27 #include "block_regex.h"
28 #include "regex_block_lib.h"
29 #include "gnunet_signatures.h"
33 * Number of bits we set per entry in the bloomfilter.
36 #define BLOOMFILTER_K 16
40 * How big is the BF we use for REGEX blocks?
42 #define REGEX_BF_SIZE 8
46 * Create a new block group.
48 * @param ctx block context in which the block group is created
49 * @param type type of the block for which we are creating the group
50 * @param nonce random value used to seed the group creation
51 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
52 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
53 * @param va variable arguments specific to @a type
54 * @return block group handle, NULL if block groups are not supported
55 * by this @a type of block (this is not an error)
57 static struct GNUNET_BLOCK_Group *
58 block_plugin_regex_create_group (void *cls,
59 enum GNUNET_BLOCK_Type type,
68 guard = va_arg (va, const char *);
69 if (0 == strcmp (guard,
71 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
73 else if (0 == strcmp (guard,
75 bf_size = va_arg (va, unsigned int);
79 bf_size = REGEX_BF_SIZE;
81 GNUNET_break (NULL == va_arg (va, const char *));
82 return GNUNET_BLOCK_GROUP_bf_create (cls,
93 * Function called to validate a reply or a request of type
94 * #GNUNET_BLOCK_TYPE_REGEX.
95 * For request evaluation, pass "NULL" for the reply_block.
96 * Note that it is assumed that the reply has already been
97 * matched to the key (and signatures checked) as it would
98 * be done with the #GNUNET_BLOCK_get_key() function.
101 * @param type block type
102 * @param bg block group to evaluate against
103 * @param eo control flags
104 * @param query original query (hash)
105 * @param xquery extrended query data (can be NULL, depending on type)
106 * @param xquery_size number of bytes in @a xquery
107 * @param reply_block response to validate
108 * @param reply_block_size number of bytes in @a reply_block
109 * @return characterization of result
111 static enum GNUNET_BLOCK_EvaluationResult
112 evaluate_block_regex (void *cls,
113 enum GNUNET_BLOCK_Type type,
114 struct GNUNET_BLOCK_Group *bg,
115 enum GNUNET_BLOCK_EvaluationOptions eo,
116 const struct GNUNET_HashCode *query,
119 const void *reply_block,
120 size_t reply_block_size)
122 struct GNUNET_HashCode chash;
124 if (NULL == reply_block)
126 if (0 != xquery_size)
130 s = (const char *) xquery;
131 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
134 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
139 if (0 != xquery_size)
143 s = (const char *) xquery;
144 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150 else if (NULL != query)
152 /* xquery is required for regex GETs, at least an empty string */
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
155 type, query, xquery);
156 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
158 switch (REGEX_BLOCK_check (reply_block,
165 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
167 /* xquery missmatch, can happen */
168 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
172 GNUNET_CRYPTO_hash (reply_block,
176 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
178 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
179 return GNUNET_BLOCK_EVALUATION_OK_MORE;
184 * Function called to validate a reply or a request of type
185 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
186 * For request evaluation, pass "NULL" for the reply_block.
187 * Note that it is assumed that the reply has already been
188 * matched to the key (and signatures checked) as it would
189 * be done with the #GNUNET_BLOCK_get_key() function.
192 * @param type block type
193 * @param bg block group to evaluate against
194 * @param eo control flags
195 * @param query original query (hash)
196 * @param xquery extrended query data (can be NULL, depending on type)
197 * @param xquery_size number of bytes in @a xquery
198 * @param reply_block response to validate
199 * @param reply_block_size number of bytes in @a reply_block
200 * @return characterization of result
202 static enum GNUNET_BLOCK_EvaluationResult
203 evaluate_block_regex_accept (void *cls,
204 enum GNUNET_BLOCK_Type type,
205 struct GNUNET_BLOCK_Group *bg,
206 enum GNUNET_BLOCK_EvaluationOptions eo,
207 const struct GNUNET_HashCode *query,
209 size_t xquery_size, const void *reply_block,
210 size_t reply_block_size)
212 const struct RegexAcceptBlock *rba;
213 struct GNUNET_HashCode chash;
215 if (0 != xquery_size)
218 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
220 if (NULL == reply_block)
221 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
222 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
225 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
228 if (ntohl (rba->purpose.size) !=
229 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
230 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
231 sizeof (struct GNUNET_HashCode))
234 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
236 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
238 /* technically invalid, but can happen without an error, so
239 we're nice by reporting it as a 'duplicate' */
240 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
243 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
246 &rba->peer.public_key))
249 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
251 GNUNET_CRYPTO_hash (reply_block,
255 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
257 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
258 return GNUNET_BLOCK_EVALUATION_OK_MORE;
263 * Function called to validate a reply or a request. For
264 * request evaluation, simply pass "NULL" for the reply_block.
265 * Note that it is assumed that the reply has already been
266 * matched to the key (and signatures checked) as it would
267 * be done with the #GNUNET_BLOCK_get_key() function.
270 * @param ctx block context
271 * @param type block type
272 * @param bg group to evaluate against
273 * @param eo control flags
274 * @param query original query (hash)
275 * @param xquery extrended query data (can be NULL, depending on type)
276 * @param xquery_size number of bytes in xquery
277 * @param reply_block response to validate
278 * @param reply_block_size number of bytes in reply block
279 * @return characterization of result
281 static enum GNUNET_BLOCK_EvaluationResult
282 block_plugin_regex_evaluate (void *cls,
283 struct GNUNET_BLOCK_Context *ctx,
284 enum GNUNET_BLOCK_Type type,
285 struct GNUNET_BLOCK_Group *bg,
286 enum GNUNET_BLOCK_EvaluationOptions eo,
287 const struct GNUNET_HashCode *query,
290 const void *reply_block,
291 size_t reply_block_size)
293 enum GNUNET_BLOCK_EvaluationResult result;
297 case GNUNET_BLOCK_TYPE_REGEX:
298 result = evaluate_block_regex (cls,
304 reply_block, reply_block_size);
306 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
307 result = evaluate_block_regex_accept (cls,
313 reply_block, reply_block_size);
317 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
324 * Function called to obtain the key for a block.
327 * @param type block type
328 * @param block block to get the key for
329 * @param block_size number of bytes in @a block
330 * @param key set to the key (query) for the given block
331 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
332 * (or if extracting a key from a block of this type does not work)
335 block_plugin_regex_get_key (void *cls,
336 enum GNUNET_BLOCK_Type type,
339 struct GNUNET_HashCode *key)
343 case GNUNET_BLOCK_TYPE_REGEX:
345 REGEX_BLOCK_get_key (block, block_size,
352 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
353 if (sizeof (struct RegexAcceptBlock) != block_size)
358 *key = ((struct RegexAcceptBlock *) block)->key;
362 return GNUNET_SYSERR;
368 * Entry point for the plugin.
371 libgnunet_plugin_block_regex_init (void *cls)
373 static enum GNUNET_BLOCK_Type types[] =
375 GNUNET_BLOCK_TYPE_REGEX,
376 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
377 GNUNET_BLOCK_TYPE_ANY /* end of list */
379 struct GNUNET_BLOCK_PluginFunctions *api;
381 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
382 api->evaluate = &block_plugin_regex_evaluate;
383 api->get_key = &block_plugin_regex_get_key;
384 api->create_group = &block_plugin_regex_create_group;
391 * Exit point from the plugin.
394 libgnunet_plugin_block_regex_done (void *cls)
396 struct GNUNET_BLOCK_PluginFunctions *api = cls;
402 /* end of plugin_block_regex.c */