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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file regex/plugin_block_regex.c
23 * @brief blocks used for regex storage and search
24 * @author Bartlomiej Polot
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"
36 * Number of bits we set per entry in the bloomfilter.
39 #define BLOOMFILTER_K 16
43 * How big is the BF we use for REGEX blocks?
45 #define REGEX_BF_SIZE 8
49 * How many bytes should a bloomfilter be if we have already seen
50 * entry_count responses? Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
51 * gives us the number of bits set per entry. Furthermore, we should
52 * not re-size the filter too often (to keep it cheap).
54 * Since other peers will also add entries but not resize the filter,
55 * we should generally pick a slightly larger size than what the
56 * strict math would suggest.
58 * @param entry_count expected number of entries in the Bloom filter
59 * @return must be a power of two and smaller or equal to 2^15.
62 compute_bloomfilter_size (unsigned int entry_count)
65 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
66 uint16_t max = 1 << 15;
68 if (entry_count > max)
71 while ((size < max) && (size < ideal))
80 * Create a new block group.
82 * @param ctx block context in which the block group is created
83 * @param type type of the block for which we are creating the group
84 * @param nonce random value used to seed the group creation
85 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
86 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
87 * @param va variable arguments specific to @a type
88 * @return block group handle, NULL if block groups are not supported
89 * by this @a type of block (this is not an error)
91 static struct GNUNET_BLOCK_Group *
92 block_plugin_regex_create_group (void *cls,
93 enum GNUNET_BLOCK_Type type,
102 guard = va_arg (va, const char *);
103 if (0 == strcmp (guard,
105 bf_size = compute_bloomfilter_size (va_arg (va, unsigned int));
106 else if (0 == strcmp (guard,
108 bf_size = va_arg (va, unsigned int);
112 bf_size = REGEX_BF_SIZE;
114 GNUNET_break (NULL == va_arg (va, const char *));
115 return GNUNET_BLOCK_GROUP_bf_create (cls,
126 * Function called to validate a reply or a request of type
127 * #GNUNET_BLOCK_TYPE_REGEX.
128 * For request evaluation, pass "NULL" for the reply_block.
129 * Note that it is assumed that the reply has already been
130 * matched to the key (and signatures checked) as it would
131 * be done with the #GNUNET_BLOCK_get_key() function.
134 * @param type block type
135 * @param bg block group to evaluate against
136 * @param eo control flags
137 * @param query original query (hash)
138 * @param xquery extrended query data (can be NULL, depending on type)
139 * @param xquery_size number of bytes in @a xquery
140 * @param reply_block response to validate
141 * @param reply_block_size number of bytes in @a reply_block
142 * @return characterization of result
144 static enum GNUNET_BLOCK_EvaluationResult
145 evaluate_block_regex (void *cls,
146 enum GNUNET_BLOCK_Type type,
147 struct GNUNET_BLOCK_Group *bg,
148 enum GNUNET_BLOCK_EvaluationOptions eo,
149 const struct GNUNET_HashCode *query,
152 const void *reply_block,
153 size_t reply_block_size)
155 struct GNUNET_HashCode chash;
157 if (NULL == reply_block)
159 if (0 != xquery_size)
163 s = (const char *) xquery;
164 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
167 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
170 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
172 if (0 != xquery_size)
176 s = (const char *) xquery;
177 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
180 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
183 else if (NULL != query)
185 /* xquery is required for regex GETs, at least an empty string */
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
188 type, query, xquery);
189 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
191 switch (REGEX_BLOCK_check (reply_block,
198 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
200 /* xquery missmatch, can happen */
201 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
205 GNUNET_CRYPTO_hash (reply_block,
209 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
211 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
212 return GNUNET_BLOCK_EVALUATION_OK_MORE;
217 * Function called to validate a reply or a request of type
218 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
219 * For request evaluation, pass "NULL" for the reply_block.
220 * Note that it is assumed that the reply has already been
221 * matched to the key (and signatures checked) as it would
222 * be done with the #GNUNET_BLOCK_get_key() function.
225 * @param type block type
226 * @param bg block group to evaluate against
227 * @param eo control flags
228 * @param query original query (hash)
229 * @param xquery extrended query data (can be NULL, depending on type)
230 * @param xquery_size number of bytes in @a xquery
231 * @param reply_block response to validate
232 * @param reply_block_size number of bytes in @a reply_block
233 * @return characterization of result
235 static enum GNUNET_BLOCK_EvaluationResult
236 evaluate_block_regex_accept (void *cls,
237 enum GNUNET_BLOCK_Type type,
238 struct GNUNET_BLOCK_Group *bg,
239 enum GNUNET_BLOCK_EvaluationOptions eo,
240 const struct GNUNET_HashCode *query,
242 size_t xquery_size, const void *reply_block,
243 size_t reply_block_size)
245 const struct RegexAcceptBlock *rba;
246 struct GNUNET_HashCode chash;
248 if (0 != xquery_size)
251 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
253 if (NULL == reply_block)
254 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
255 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
258 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
261 if (ntohl (rba->purpose.size) !=
262 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
263 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
264 sizeof (struct GNUNET_HashCode))
267 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
269 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
271 /* technically invalid, but can happen without an error, so
272 we're nice by reporting it as a 'duplicate' */
273 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
276 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
279 &rba->peer.public_key))
282 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
284 GNUNET_CRYPTO_hash (reply_block,
288 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
290 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
291 return GNUNET_BLOCK_EVALUATION_OK_MORE;
296 * Function called to validate a reply or a request. For
297 * request evaluation, simply pass "NULL" for the reply_block.
298 * Note that it is assumed that the reply has already been
299 * matched to the key (and signatures checked) as it would
300 * be done with the #GNUNET_BLOCK_get_key() function.
303 * @param type block type
304 * @param bg group to evaluate against
305 * @param eo control flags
306 * @param query original query (hash)
307 * @param xquery extrended query data (can be NULL, depending on type)
308 * @param xquery_size number of bytes in xquery
309 * @param reply_block response to validate
310 * @param reply_block_size number of bytes in reply block
311 * @return characterization of result
313 static enum GNUNET_BLOCK_EvaluationResult
314 block_plugin_regex_evaluate (void *cls,
315 enum GNUNET_BLOCK_Type type,
316 struct GNUNET_BLOCK_Group *bg,
317 enum GNUNET_BLOCK_EvaluationOptions eo,
318 const struct GNUNET_HashCode *query,
321 const void *reply_block,
322 size_t reply_block_size)
324 enum GNUNET_BLOCK_EvaluationResult result;
328 case GNUNET_BLOCK_TYPE_REGEX:
329 result = evaluate_block_regex (cls,
335 reply_block, reply_block_size);
337 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
338 result = evaluate_block_regex_accept (cls,
344 reply_block, reply_block_size);
348 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
355 * Function called to obtain the key for a block.
358 * @param type block type
359 * @param block block to get the key for
360 * @param block_size number of bytes in @a block
361 * @param key set to the key (query) for the given block
362 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
363 * (or if extracting a key from a block of this type does not work)
366 block_plugin_regex_get_key (void *cls,
367 enum GNUNET_BLOCK_Type type,
370 struct GNUNET_HashCode *key)
374 case GNUNET_BLOCK_TYPE_REGEX:
376 REGEX_BLOCK_get_key (block, block_size,
383 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
384 if (sizeof (struct RegexAcceptBlock) != block_size)
389 *key = ((struct RegexAcceptBlock *) block)->key;
393 return GNUNET_SYSERR;
399 * Entry point for the plugin.
402 libgnunet_plugin_block_regex_init (void *cls)
404 static enum GNUNET_BLOCK_Type types[] =
406 GNUNET_BLOCK_TYPE_REGEX,
407 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
408 GNUNET_BLOCK_TYPE_ANY /* end of list */
410 struct GNUNET_BLOCK_PluginFunctions *api;
412 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
413 api->evaluate = &block_plugin_regex_evaluate;
414 api->get_key = &block_plugin_regex_get_key;
415 api->create_group = &block_plugin_regex_create_group;
422 * Exit point from the plugin.
425 libgnunet_plugin_block_regex_done (void *cls)
427 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
433 /* end of plugin_block_regex.c */