2 This file is part of GNUnet
3 Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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
28 #include "gnunet_block_plugin.h"
29 #include "block_regex.h"
30 #include "regex_block_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_signatures.h"
36 * Function called to validate a reply or a request of type
37 * #GNUNET_BLOCK_TYPE_REGEX.
38 * For request evaluation, pass "NULL" for the reply_block.
39 * Note that it is assumed that the reply has already been
40 * matched to the key (and signatures checked) as it would
41 * be done with the #GNUNET_BLOCK_get_key() function.
44 * @param type block type
45 * @param eo control flags
46 * @param query original query (hash)
47 * @param bf pointer to bloom filter associated with query; possibly updated (!)
48 * @param bf_mutator mutation value for bf
49 * @param xquery extrended query data (can be NULL, depending on type)
50 * @param xquery_size number of bytes in @a xquery
51 * @param reply_block response to validate
52 * @param reply_block_size number of bytes in @a reply_block
53 * @return characterization of result
55 static enum GNUNET_BLOCK_EvaluationResult
56 evaluate_block_regex (void *cls,
57 enum GNUNET_BLOCK_Type type,
58 enum GNUNET_BLOCK_EvaluationOptions eo,
59 const struct GNUNET_HashCode *query,
60 struct GNUNET_CONTAINER_BloomFilter **bf,
64 const void *reply_block,
65 size_t reply_block_size)
67 if (NULL == reply_block)
73 s = (const char *) xquery;
74 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
77 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
80 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
86 s = (const char *) xquery;
87 if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
90 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
93 else if (NULL != query)
95 /* xquery is required for regex GETs, at least an empty string */
97 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
99 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
101 switch (REGEX_BLOCK_check (reply_block,
108 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
110 /* xquery missmatch, can happen */
111 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
117 struct GNUNET_HashCode chash;
118 struct GNUNET_HashCode mhash;
120 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
121 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
124 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
125 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
129 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K);
131 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
133 return GNUNET_BLOCK_EVALUATION_OK_MORE;
138 * Function called to validate a reply or a request of type
139 * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
140 * For request evaluation, pass "NULL" for the reply_block.
141 * Note that it is assumed that the reply has already been
142 * matched to the key (and signatures checked) as it would
143 * be done with the #GNUNET_BLOCK_get_key() function.
146 * @param type block type
147 * @param eo control flags
148 * @param query original query (hash)
149 * @param bf pointer to bloom filter associated with query; possibly updated (!)
150 * @param bf_mutator mutation value for bf
151 * @param xquery extrended query data (can be NULL, depending on type)
152 * @param xquery_size number of bytes in @a xquery
153 * @param reply_block response to validate
154 * @param reply_block_size number of bytes in @a reply_block
155 * @return characterization of result
157 static enum GNUNET_BLOCK_EvaluationResult
158 evaluate_block_regex_accept (void *cls,
159 enum GNUNET_BLOCK_Type type,
160 enum GNUNET_BLOCK_EvaluationOptions eo,
161 const struct GNUNET_HashCode * query,
162 struct GNUNET_CONTAINER_BloomFilter **bf,
163 int32_t bf_mutator, const void *xquery,
164 size_t xquery_size, const void *reply_block,
165 size_t reply_block_size)
167 const struct RegexAcceptBlock *rba;
169 if (0 != xquery_size)
172 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
174 if (NULL == reply_block)
175 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
176 if (sizeof (struct RegexAcceptBlock) != reply_block_size)
179 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
182 if (ntohl (rba->purpose.size) !=
183 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
184 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
185 sizeof (struct GNUNET_HashCode))
188 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
190 if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
192 /* technically invalid, but can happen without an error, so
193 we're nice by reporting it as a 'duplicate' */
194 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
197 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
200 &rba->peer.public_key))
203 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
207 struct GNUNET_HashCode chash;
208 struct GNUNET_HashCode mhash;
210 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
211 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
214 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
215 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
219 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K);
221 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
223 return GNUNET_BLOCK_EVALUATION_OK_MORE;
228 * Function called to validate a reply or a request. For
229 * request evaluation, simply pass "NULL" for the reply_block.
230 * Note that it is assumed that the reply has already been
231 * matched to the key (and signatures checked) as it would
232 * be done with the #GNUNET_BLOCK_get_key() function.
235 * @param type block type
236 * @param eo control flags
237 * @param query original query (hash)
238 * @param bf pointer to bloom filter associated with query; possibly updated (!)
239 * @param bf_mutator mutation value for bf
240 * @param xquery extrended query data (can be NULL, depending on type)
241 * @param xquery_size number of bytes in xquery
242 * @param reply_block response to validate
243 * @param reply_block_size number of bytes in reply block
244 * @return characterization of result
246 static enum GNUNET_BLOCK_EvaluationResult
247 block_plugin_regex_evaluate (void *cls,
248 enum GNUNET_BLOCK_Type type,
249 enum GNUNET_BLOCK_EvaluationOptions eo,
250 const struct GNUNET_HashCode *query,
251 struct GNUNET_CONTAINER_BloomFilter **bf,
255 const void *reply_block,
256 size_t reply_block_size)
258 enum GNUNET_BLOCK_EvaluationResult result;
262 case GNUNET_BLOCK_TYPE_REGEX:
263 result = evaluate_block_regex (cls,
269 reply_block, reply_block_size);
271 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
272 result = evaluate_block_regex_accept (cls,
278 reply_block, reply_block_size);
282 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
289 * Function called to obtain the key for a block.
292 * @param type block type
293 * @param block block to get the key for
294 * @param block_size number of bytes in @a block
295 * @param key set to the key (query) for the given block
296 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
297 * (or if extracting a key from a block of this type does not work)
300 block_plugin_regex_get_key (void *cls,
301 enum GNUNET_BLOCK_Type type,
304 struct GNUNET_HashCode *key)
308 case GNUNET_BLOCK_TYPE_REGEX:
310 REGEX_BLOCK_get_key (block, block_size,
317 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
318 if (sizeof (struct RegexAcceptBlock) != block_size)
323 *key = ((struct RegexAcceptBlock *) block)->key;
327 return GNUNET_SYSERR;
333 * Entry point for the plugin.
336 libgnunet_plugin_block_regex_init (void *cls)
338 static enum GNUNET_BLOCK_Type types[] =
340 GNUNET_BLOCK_TYPE_REGEX,
341 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
342 GNUNET_BLOCK_TYPE_ANY /* end of list */
344 struct GNUNET_BLOCK_PluginFunctions *api;
346 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
347 api->evaluate = &block_plugin_regex_evaluate;
348 api->get_key = &block_plugin_regex_get_key;
355 * Exit point from the plugin.
358 libgnunet_plugin_block_regex_done (void *cls)
360 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
366 /* end of plugin_block_regex.c */