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_constants.h"
32 #include "gnunet_signatures.h"
36 * How big is the BF we use for REGEX blocks?
38 #define REGEX_BF_SIZE 8
42 * Create a new block group.
44 * @param ctx block context in which the block group is created
45 * @param type type of the block for which we are creating the group
46 * @param nonce random value used to seed the group creation
47 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
48 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
49 * @param va variable arguments specific to @a type
50 * @return block group handle, NULL if block groups are not supported
51 * by this @a type of block (this is not an error)
53 static struct GNUNET_BLOCK_Group *
54 block_plugin_regex_create_group (void *cls,
55 enum GNUNET_BLOCK_Type type,
61 return GNUNET_BLOCK_GROUP_bf_create (cls,
63 GNUNET_CONSTANTS_BLOOMFILTER_K,
72 * Function called to validate a reply or a request of type
73 * #GNUNET_BLOCK_TYPE_REGEX.
74 * For request evaluation, pass "NULL" for the reply_block.
75 * Note that it is assumed that the reply has already been
76 * matched to the key (and signatures checked) as it would
77 * be done with the #GNUNET_BLOCK_get_key() function.
80 * @param type block type
81 * @param bg block group to evaluate against
82 * @param eo control flags
83 * @param query original query (hash)
84 * @param xquery extrended query data (can be NULL, depending on type)
85 * @param xquery_size number of bytes in @a xquery
86 * @param reply_block response to validate
87 * @param reply_block_size number of bytes in @a reply_block
88 * @return characterization of result
90 static enum GNUNET_BLOCK_EvaluationResult
91 evaluate_block_regex (void *cls,
92 enum GNUNET_BLOCK_Type type,
93 struct GNUNET_BLOCK_Group *bg,
94 enum GNUNET_BLOCK_EvaluationOptions eo,
95 const struct GNUNET_HashCode *query,
98 const void *reply_block,
99 size_t reply_block_size)
101 struct GNUNET_HashCode chash;
103 if (NULL == reply_block)
105 if (0 != xquery_size)
109 s = (const char *) xquery;
110 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
113 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
116 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
118 if (0 != xquery_size)
122 s = (const char *) xquery;
123 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
126 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
129 else if (NULL != query)
131 /* xquery is required for regex GETs, at least an empty string */
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
134 type, query, xquery);
135 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 switch (REGEX_BLOCK_check (reply_block,
144 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
146 /* xquery missmatch, can happen */
147 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
151 GNUNET_CRYPTO_hash (reply_block,
155 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
157 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
158 return GNUNET_BLOCK_EVALUATION_OK_MORE;
163 * Function called to validate a reply or a request of type
164 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
165 * For request evaluation, pass "NULL" for the reply_block.
166 * Note that it is assumed that the reply has already been
167 * matched to the key (and signatures checked) as it would
168 * be done with the #GNUNET_BLOCK_get_key() function.
171 * @param type block type
172 * @param bg block group to evaluate against
173 * @param eo control flags
174 * @param query original query (hash)
175 * @param xquery extrended query data (can be NULL, depending on type)
176 * @param xquery_size number of bytes in @a xquery
177 * @param reply_block response to validate
178 * @param reply_block_size number of bytes in @a reply_block
179 * @return characterization of result
181 static enum GNUNET_BLOCK_EvaluationResult
182 evaluate_block_regex_accept (void *cls,
183 enum GNUNET_BLOCK_Type type,
184 struct GNUNET_BLOCK_Group *bg,
185 enum GNUNET_BLOCK_EvaluationOptions eo,
186 const struct GNUNET_HashCode *query,
188 size_t xquery_size, const void *reply_block,
189 size_t reply_block_size)
191 const struct RegexAcceptBlock *rba;
192 struct GNUNET_HashCode chash;
194 if (0 != xquery_size)
197 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
199 if (NULL == reply_block)
200 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
201 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
204 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
207 if (ntohl (rba->purpose.size) !=
208 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
209 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
210 sizeof (struct GNUNET_HashCode))
213 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
215 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
217 /* technically invalid, but can happen without an error, so
218 we're nice by reporting it as a 'duplicate' */
219 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
222 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
225 &rba->peer.public_key))
228 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
230 GNUNET_CRYPTO_hash (reply_block,
234 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
236 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
237 return GNUNET_BLOCK_EVALUATION_OK_MORE;
242 * Function called to validate a reply or a request. For
243 * request evaluation, simply pass "NULL" for the reply_block.
244 * Note that it is assumed that the reply has already been
245 * matched to the key (and signatures checked) as it would
246 * be done with the #GNUNET_BLOCK_get_key() function.
249 * @param type block type
250 * @param bg group to evaluate against
251 * @param eo control flags
252 * @param query original query (hash)
253 * @param xquery extrended query data (can be NULL, depending on type)
254 * @param xquery_size number of bytes in xquery
255 * @param reply_block response to validate
256 * @param reply_block_size number of bytes in reply block
257 * @return characterization of result
259 static enum GNUNET_BLOCK_EvaluationResult
260 block_plugin_regex_evaluate (void *cls,
261 enum GNUNET_BLOCK_Type type,
262 struct GNUNET_BLOCK_Group *bg,
263 enum GNUNET_BLOCK_EvaluationOptions eo,
264 const struct GNUNET_HashCode *query,
267 const void *reply_block,
268 size_t reply_block_size)
270 enum GNUNET_BLOCK_EvaluationResult result;
274 case GNUNET_BLOCK_TYPE_REGEX:
275 result = evaluate_block_regex (cls,
281 reply_block, reply_block_size);
283 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
284 result = evaluate_block_regex_accept (cls,
290 reply_block, reply_block_size);
294 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
301 * Function called to obtain the key for a block.
304 * @param type block type
305 * @param block block to get the key for
306 * @param block_size number of bytes in @a block
307 * @param key set to the key (query) for the given block
308 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
309 * (or if extracting a key from a block of this type does not work)
312 block_plugin_regex_get_key (void *cls,
313 enum GNUNET_BLOCK_Type type,
316 struct GNUNET_HashCode *key)
320 case GNUNET_BLOCK_TYPE_REGEX:
322 REGEX_BLOCK_get_key (block, block_size,
329 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
330 if (sizeof (struct RegexAcceptBlock) != block_size)
335 *key = ((struct RegexAcceptBlock *) block)->key;
339 return GNUNET_SYSERR;
345 * Entry point for the plugin.
348 libgnunet_plugin_block_regex_init (void *cls)
350 static enum GNUNET_BLOCK_Type types[] =
352 GNUNET_BLOCK_TYPE_REGEX,
353 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
354 GNUNET_BLOCK_TYPE_ANY /* end of list */
356 struct GNUNET_BLOCK_PluginFunctions *api;
358 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
359 api->evaluate = &block_plugin_regex_evaluate;
360 api->get_key = &block_plugin_regex_get_key;
361 api->create_group = &block_plugin_regex_create_group;
368 * Exit point from the plugin.
371 libgnunet_plugin_block_regex_done (void *cls)
373 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
379 /* end of plugin_block_regex.c */