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.
17 * @file regex/plugin_block_regex.c
18 * @brief blocks used for regex storage and search
19 * @author Bartlomiej Polot
22 #include "gnunet_block_plugin.h"
23 #include "gnunet_block_group_lib.h"
24 #include "block_regex.h"
25 #include "regex_block_lib.h"
26 #include "gnunet_signatures.h"
30 * Number of bits we set per entry in the bloomfilter.
33 #define BLOOMFILTER_K 16
37 * How big is the BF we use for REGEX blocks?
39 #define REGEX_BF_SIZE 8
43 * Create a new block group.
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 nonce random value used to seed the group creation
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)
54 static struct GNUNET_BLOCK_Group *
55 block_plugin_regex_create_group (void *cls,
56 enum GNUNET_BLOCK_Type type,
65 guard = va_arg (va, const char *);
66 if (0 == strcmp (guard,
68 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
70 else if (0 == strcmp (guard,
72 bf_size = va_arg (va, unsigned int);
76 bf_size = REGEX_BF_SIZE;
78 GNUNET_break (NULL == va_arg (va, const char *));
79 return GNUNET_BLOCK_GROUP_bf_create (cls,
90 * Function called to validate a reply or a request of type
91 * #GNUNET_BLOCK_TYPE_REGEX.
92 * For request evaluation, pass "NULL" for the reply_block.
93 * Note that it is assumed that the reply has already been
94 * matched to the key (and signatures checked) as it would
95 * be done with the #GNUNET_BLOCK_get_key() function.
98 * @param type block type
99 * @param bg block group to evaluate against
100 * @param eo control flags
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 * @param reply_block response to validate
105 * @param reply_block_size number of bytes in @a reply_block
106 * @return characterization of result
108 static enum GNUNET_BLOCK_EvaluationResult
109 evaluate_block_regex (void *cls,
110 enum GNUNET_BLOCK_Type type,
111 struct GNUNET_BLOCK_Group *bg,
112 enum GNUNET_BLOCK_EvaluationOptions eo,
113 const struct GNUNET_HashCode *query,
116 const void *reply_block,
117 size_t reply_block_size)
119 struct GNUNET_HashCode chash;
121 if (NULL == reply_block)
123 if (0 != xquery_size)
127 s = (const char *) xquery;
128 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
131 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
134 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
136 if (0 != xquery_size)
140 s = (const char *) xquery;
141 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
144 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
147 else if (NULL != query)
149 /* xquery is required for regex GETs, at least an empty string */
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
152 type, query, xquery);
153 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
155 switch (REGEX_BLOCK_check (reply_block,
162 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
164 /* xquery missmatch, can happen */
165 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
169 GNUNET_CRYPTO_hash (reply_block,
173 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
175 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
176 return GNUNET_BLOCK_EVALUATION_OK_MORE;
181 * Function called to validate a reply or a request of type
182 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
183 * For request evaluation, pass "NULL" for the reply_block.
184 * Note that it is assumed that the reply has already been
185 * matched to the key (and signatures checked) as it would
186 * be done with the #GNUNET_BLOCK_get_key() function.
189 * @param type block type
190 * @param bg block group to evaluate against
191 * @param eo control flags
192 * @param query original query (hash)
193 * @param xquery extrended query data (can be NULL, depending on type)
194 * @param xquery_size number of bytes in @a xquery
195 * @param reply_block response to validate
196 * @param reply_block_size number of bytes in @a reply_block
197 * @return characterization of result
199 static enum GNUNET_BLOCK_EvaluationResult
200 evaluate_block_regex_accept (void *cls,
201 enum GNUNET_BLOCK_Type type,
202 struct GNUNET_BLOCK_Group *bg,
203 enum GNUNET_BLOCK_EvaluationOptions eo,
204 const struct GNUNET_HashCode *query,
206 size_t xquery_size, const void *reply_block,
207 size_t reply_block_size)
209 const struct RegexAcceptBlock *rba;
210 struct GNUNET_HashCode chash;
212 if (0 != xquery_size)
215 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
217 if (NULL == reply_block)
218 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
219 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
222 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
225 if (ntohl (rba->purpose.size) !=
226 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
227 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
228 sizeof (struct GNUNET_HashCode))
231 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
233 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
235 /* technically invalid, but can happen without an error, so
236 we're nice by reporting it as a 'duplicate' */
237 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
240 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
243 &rba->peer.public_key))
246 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
248 GNUNET_CRYPTO_hash (reply_block,
252 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
254 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
255 return GNUNET_BLOCK_EVALUATION_OK_MORE;
260 * Function called to validate a reply or a request. For
261 * request evaluation, simply pass "NULL" for the reply_block.
262 * Note that it is assumed that the reply has already been
263 * matched to the key (and signatures checked) as it would
264 * be done with the #GNUNET_BLOCK_get_key() function.
267 * @param ctx block context
268 * @param type block type
269 * @param bg group to evaluate against
270 * @param eo control flags
271 * @param query original query (hash)
272 * @param xquery extrended query data (can be NULL, depending on type)
273 * @param xquery_size number of bytes in xquery
274 * @param reply_block response to validate
275 * @param reply_block_size number of bytes in reply block
276 * @return characterization of result
278 static enum GNUNET_BLOCK_EvaluationResult
279 block_plugin_regex_evaluate (void *cls,
280 struct GNUNET_BLOCK_Context *ctx,
281 enum GNUNET_BLOCK_Type type,
282 struct GNUNET_BLOCK_Group *bg,
283 enum GNUNET_BLOCK_EvaluationOptions eo,
284 const struct GNUNET_HashCode *query,
287 const void *reply_block,
288 size_t reply_block_size)
290 enum GNUNET_BLOCK_EvaluationResult result;
294 case GNUNET_BLOCK_TYPE_REGEX:
295 result = evaluate_block_regex (cls,
301 reply_block, reply_block_size);
303 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
304 result = evaluate_block_regex_accept (cls,
310 reply_block, reply_block_size);
314 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
321 * Function called to obtain the key for a block.
324 * @param type block type
325 * @param block block to get the key for
326 * @param block_size number of bytes in @a block
327 * @param key set to the key (query) for the given block
328 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
329 * (or if extracting a key from a block of this type does not work)
332 block_plugin_regex_get_key (void *cls,
333 enum GNUNET_BLOCK_Type type,
336 struct GNUNET_HashCode *key)
340 case GNUNET_BLOCK_TYPE_REGEX:
342 REGEX_BLOCK_get_key (block, block_size,
349 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
350 if (sizeof (struct RegexAcceptBlock) != block_size)
355 *key = ((struct RegexAcceptBlock *) block)->key;
359 return GNUNET_SYSERR;
365 * Entry point for the plugin.
368 libgnunet_plugin_block_regex_init (void *cls)
370 static enum GNUNET_BLOCK_Type types[] =
372 GNUNET_BLOCK_TYPE_REGEX,
373 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
374 GNUNET_BLOCK_TYPE_ANY /* end of list */
376 struct GNUNET_BLOCK_PluginFunctions *api;
378 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
379 api->evaluate = &block_plugin_regex_evaluate;
380 api->get_key = &block_plugin_regex_get_key;
381 api->create_group = &block_plugin_regex_create_group;
388 * Exit point from the plugin.
391 libgnunet_plugin_block_regex_done (void *cls)
393 struct GNUNET_BLOCK_PluginFunctions *api = cls;
399 /* end of plugin_block_regex.c */