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 == memcmp (guard,
105 strlen ("seen-set-size")))
106 bf_size = compute_bloomfilter_size (va_arg (va, unsigned int));
107 else if (0 == memcmp (guard,
109 strlen ("filter-size")))
110 bf_size = va_arg (va, unsigned int);
114 bf_size = REGEX_BF_SIZE;
116 GNUNET_break (NULL == va_arg (va, const char *));
117 return GNUNET_BLOCK_GROUP_bf_create (cls,
128 * Function called to validate a reply or a request of type
129 * #GNUNET_BLOCK_TYPE_REGEX.
130 * For request evaluation, pass "NULL" for the reply_block.
131 * Note that it is assumed that the reply has already been
132 * matched to the key (and signatures checked) as it would
133 * be done with the #GNUNET_BLOCK_get_key() function.
136 * @param type block type
137 * @param bg block group to evaluate against
138 * @param eo control flags
139 * @param query original query (hash)
140 * @param xquery extrended query data (can be NULL, depending on type)
141 * @param xquery_size number of bytes in @a xquery
142 * @param reply_block response to validate
143 * @param reply_block_size number of bytes in @a reply_block
144 * @return characterization of result
146 static enum GNUNET_BLOCK_EvaluationResult
147 evaluate_block_regex (void *cls,
148 enum GNUNET_BLOCK_Type type,
149 struct GNUNET_BLOCK_Group *bg,
150 enum GNUNET_BLOCK_EvaluationOptions eo,
151 const struct GNUNET_HashCode *query,
154 const void *reply_block,
155 size_t reply_block_size)
157 struct GNUNET_HashCode chash;
159 if (NULL == reply_block)
161 if (0 != xquery_size)
165 s = (const char *) xquery;
166 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
169 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
172 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
174 if (0 != xquery_size)
178 s = (const char *) xquery;
179 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
182 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
185 else if (NULL != query)
187 /* xquery is required for regex GETs, at least an empty string */
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
190 type, query, xquery);
191 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
193 switch (REGEX_BLOCK_check (reply_block,
200 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
202 /* xquery missmatch, can happen */
203 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
207 GNUNET_CRYPTO_hash (reply_block,
211 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
213 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
214 return GNUNET_BLOCK_EVALUATION_OK_MORE;
219 * Function called to validate a reply or a request of type
220 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
221 * For request evaluation, pass "NULL" for the reply_block.
222 * Note that it is assumed that the reply has already been
223 * matched to the key (and signatures checked) as it would
224 * be done with the #GNUNET_BLOCK_get_key() function.
227 * @param type block type
228 * @param bg block group to evaluate against
229 * @param eo control flags
230 * @param query original query (hash)
231 * @param xquery extrended query data (can be NULL, depending on type)
232 * @param xquery_size number of bytes in @a xquery
233 * @param reply_block response to validate
234 * @param reply_block_size number of bytes in @a reply_block
235 * @return characterization of result
237 static enum GNUNET_BLOCK_EvaluationResult
238 evaluate_block_regex_accept (void *cls,
239 enum GNUNET_BLOCK_Type type,
240 struct GNUNET_BLOCK_Group *bg,
241 enum GNUNET_BLOCK_EvaluationOptions eo,
242 const struct GNUNET_HashCode *query,
244 size_t xquery_size, const void *reply_block,
245 size_t reply_block_size)
247 const struct RegexAcceptBlock *rba;
248 struct GNUNET_HashCode chash;
250 if (0 != xquery_size)
253 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
255 if (NULL == reply_block)
256 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
257 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
260 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
263 if (ntohl (rba->purpose.size) !=
264 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
265 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
266 sizeof (struct GNUNET_HashCode))
269 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
271 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
273 /* technically invalid, but can happen without an error, so
274 we're nice by reporting it as a 'duplicate' */
275 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
278 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
281 &rba->peer.public_key))
284 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
286 GNUNET_CRYPTO_hash (reply_block,
290 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
292 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
293 return GNUNET_BLOCK_EVALUATION_OK_MORE;
298 * Function called to validate a reply or a request. For
299 * request evaluation, simply pass "NULL" for the reply_block.
300 * Note that it is assumed that the reply has already been
301 * matched to the key (and signatures checked) as it would
302 * be done with the #GNUNET_BLOCK_get_key() function.
305 * @param type block type
306 * @param bg group to evaluate against
307 * @param eo control flags
308 * @param query original query (hash)
309 * @param xquery extrended query data (can be NULL, depending on type)
310 * @param xquery_size number of bytes in xquery
311 * @param reply_block response to validate
312 * @param reply_block_size number of bytes in reply block
313 * @return characterization of result
315 static enum GNUNET_BLOCK_EvaluationResult
316 block_plugin_regex_evaluate (void *cls,
317 enum GNUNET_BLOCK_Type type,
318 struct GNUNET_BLOCK_Group *bg,
319 enum GNUNET_BLOCK_EvaluationOptions eo,
320 const struct GNUNET_HashCode *query,
323 const void *reply_block,
324 size_t reply_block_size)
326 enum GNUNET_BLOCK_EvaluationResult result;
330 case GNUNET_BLOCK_TYPE_REGEX:
331 result = evaluate_block_regex (cls,
337 reply_block, reply_block_size);
339 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
340 result = evaluate_block_regex_accept (cls,
346 reply_block, reply_block_size);
350 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
357 * Function called to obtain the key for a block.
360 * @param type block type
361 * @param block block to get the key for
362 * @param block_size number of bytes in @a block
363 * @param key set to the key (query) for the given block
364 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
365 * (or if extracting a key from a block of this type does not work)
368 block_plugin_regex_get_key (void *cls,
369 enum GNUNET_BLOCK_Type type,
372 struct GNUNET_HashCode *key)
376 case GNUNET_BLOCK_TYPE_REGEX:
378 REGEX_BLOCK_get_key (block, block_size,
385 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
386 if (sizeof (struct RegexAcceptBlock) != block_size)
391 *key = ((struct RegexAcceptBlock *) block)->key;
395 return GNUNET_SYSERR;
401 * Entry point for the plugin.
404 libgnunet_plugin_block_regex_init (void *cls)
406 static enum GNUNET_BLOCK_Type types[] =
408 GNUNET_BLOCK_TYPE_REGEX,
409 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
410 GNUNET_BLOCK_TYPE_ANY /* end of list */
412 struct GNUNET_BLOCK_PluginFunctions *api;
414 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
415 api->evaluate = &block_plugin_regex_evaluate;
416 api->get_key = &block_plugin_regex_get_key;
417 api->create_group = &block_plugin_regex_create_group;
424 * Exit point from the plugin.
427 libgnunet_plugin_block_regex_done (void *cls)
429 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
435 /* end of plugin_block_regex.c */