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"
35 * Number of bits we set per entry in the bloomfilter.
38 #define BLOOMFILTER_K 16
42 * How big is the BF we use for REGEX blocks?
44 #define REGEX_BF_SIZE 8
48 * Create a new block group.
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 nonce random value used to seed the group creation
53 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
54 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
55 * @param va variable arguments specific to @a type
56 * @return block group handle, NULL if block groups are not supported
57 * by this @a type of block (this is not an error)
59 static struct GNUNET_BLOCK_Group *
60 block_plugin_regex_create_group (void *cls,
61 enum GNUNET_BLOCK_Type type,
70 guard = va_arg (va, const char *);
71 if (0 == strcmp (guard,
73 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
75 else if (0 == strcmp (guard,
77 bf_size = va_arg (va, unsigned int);
81 bf_size = REGEX_BF_SIZE;
83 GNUNET_break (NULL == va_arg (va, const char *));
84 return GNUNET_BLOCK_GROUP_bf_create (cls,
95 * Function called to validate a reply or a request of type
96 * #GNUNET_BLOCK_TYPE_REGEX.
97 * For request evaluation, pass "NULL" for the reply_block.
98 * Note that it is assumed that the reply has already been
99 * matched to the key (and signatures checked) as it would
100 * be done with the #GNUNET_BLOCK_get_key() function.
103 * @param type block type
104 * @param bg block group to evaluate against
105 * @param eo control flags
106 * @param query original query (hash)
107 * @param xquery extrended query data (can be NULL, depending on type)
108 * @param xquery_size number of bytes in @a xquery
109 * @param reply_block response to validate
110 * @param reply_block_size number of bytes in @a reply_block
111 * @return characterization of result
113 static enum GNUNET_BLOCK_EvaluationResult
114 evaluate_block_regex (void *cls,
115 enum GNUNET_BLOCK_Type type,
116 struct GNUNET_BLOCK_Group *bg,
117 enum GNUNET_BLOCK_EvaluationOptions eo,
118 const struct GNUNET_HashCode *query,
121 const void *reply_block,
122 size_t reply_block_size)
124 struct GNUNET_HashCode chash;
126 if (NULL == reply_block)
128 if (0 != xquery_size)
132 s = (const char *) xquery;
133 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
136 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
139 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
141 if (0 != xquery_size)
145 s = (const char *) xquery;
146 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
152 else if (NULL != query)
154 /* xquery is required for regex GETs, at least an empty string */
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
157 type, query, xquery);
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
160 switch (REGEX_BLOCK_check (reply_block,
167 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
169 /* xquery missmatch, can happen */
170 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
174 GNUNET_CRYPTO_hash (reply_block,
178 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
180 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
181 return GNUNET_BLOCK_EVALUATION_OK_MORE;
186 * Function called to validate a reply or a request of type
187 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
188 * For request evaluation, pass "NULL" for the reply_block.
189 * Note that it is assumed that the reply has already been
190 * matched to the key (and signatures checked) as it would
191 * be done with the #GNUNET_BLOCK_get_key() function.
194 * @param type block type
195 * @param bg block group to evaluate against
196 * @param eo control flags
197 * @param query original query (hash)
198 * @param xquery extrended query data (can be NULL, depending on type)
199 * @param xquery_size number of bytes in @a xquery
200 * @param reply_block response to validate
201 * @param reply_block_size number of bytes in @a reply_block
202 * @return characterization of result
204 static enum GNUNET_BLOCK_EvaluationResult
205 evaluate_block_regex_accept (void *cls,
206 enum GNUNET_BLOCK_Type type,
207 struct GNUNET_BLOCK_Group *bg,
208 enum GNUNET_BLOCK_EvaluationOptions eo,
209 const struct GNUNET_HashCode *query,
211 size_t xquery_size, const void *reply_block,
212 size_t reply_block_size)
214 const struct RegexAcceptBlock *rba;
215 struct GNUNET_HashCode chash;
217 if (0 != xquery_size)
220 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
222 if (NULL == reply_block)
223 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
224 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
227 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
230 if (ntohl (rba->purpose.size) !=
231 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
232 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
233 sizeof (struct GNUNET_HashCode))
236 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
238 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
240 /* technically invalid, but can happen without an error, so
241 we're nice by reporting it as a 'duplicate' */
242 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
245 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
248 &rba->peer.public_key))
251 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
253 GNUNET_CRYPTO_hash (reply_block,
257 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
259 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
260 return GNUNET_BLOCK_EVALUATION_OK_MORE;
265 * Function called to validate a reply or a request. For
266 * request evaluation, simply pass "NULL" for the reply_block.
267 * Note that it is assumed that the reply has already been
268 * matched to the key (and signatures checked) as it would
269 * be done with the #GNUNET_BLOCK_get_key() function.
272 * @param ctx block context
273 * @param type block type
274 * @param bg group to evaluate against
275 * @param eo control flags
276 * @param query original query (hash)
277 * @param xquery extrended query data (can be NULL, depending on type)
278 * @param xquery_size number of bytes in xquery
279 * @param reply_block response to validate
280 * @param reply_block_size number of bytes in reply block
281 * @return characterization of result
283 static enum GNUNET_BLOCK_EvaluationResult
284 block_plugin_regex_evaluate (void *cls,
285 struct GNUNET_BLOCK_Context *ctx,
286 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_BLOCK_Group *bg,
288 enum GNUNET_BLOCK_EvaluationOptions eo,
289 const struct GNUNET_HashCode *query,
292 const void *reply_block,
293 size_t reply_block_size)
295 enum GNUNET_BLOCK_EvaluationResult result;
299 case GNUNET_BLOCK_TYPE_REGEX:
300 result = evaluate_block_regex (cls,
306 reply_block, reply_block_size);
308 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
309 result = evaluate_block_regex_accept (cls,
315 reply_block, reply_block_size);
319 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
326 * Function called to obtain the key for a block.
329 * @param type block type
330 * @param block block to get the key for
331 * @param block_size number of bytes in @a block
332 * @param key set to the key (query) for the given block
333 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
334 * (or if extracting a key from a block of this type does not work)
337 block_plugin_regex_get_key (void *cls,
338 enum GNUNET_BLOCK_Type type,
341 struct GNUNET_HashCode *key)
345 case GNUNET_BLOCK_TYPE_REGEX:
347 REGEX_BLOCK_get_key (block, block_size,
354 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
355 if (sizeof (struct RegexAcceptBlock) != block_size)
360 *key = ((struct RegexAcceptBlock *) block)->key;
364 return GNUNET_SYSERR;
370 * Entry point for the plugin.
373 libgnunet_plugin_block_regex_init (void *cls)
375 static enum GNUNET_BLOCK_Type types[] =
377 GNUNET_BLOCK_TYPE_REGEX,
378 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
379 GNUNET_BLOCK_TYPE_ANY /* end of list */
381 struct GNUNET_BLOCK_PluginFunctions *api;
383 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
384 api->evaluate = &block_plugin_regex_evaluate;
385 api->get_key = &block_plugin_regex_get_key;
386 api->create_group = &block_plugin_regex_create_group;
393 * Exit point from the plugin.
396 libgnunet_plugin_block_regex_done (void *cls)
398 struct GNUNET_BLOCK_PluginFunctions *api = cls;
404 /* end of plugin_block_regex.c */