2 This file is part of GNUnet
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
33 * Number of bits we set per entry in the bloomfilter.
36 #define BLOOMFILTER_K 16
40 * Show debug info about outgoing edges from a block.
42 * @param cls Closure (uunsed).
43 * @param token Edge label.
44 * @param len Length of @c token.
45 * @param key Block the edge point to.
47 * @return GNUNET_YES to keep iterating.
53 const struct GNUNET_HashCode *key)
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
57 GNUNET_h2s (key), len, token);
63 * Function called to validate a reply or a request of type
64 * GNUNET_BLOCK_TYPE_REGEX.
65 * For request evaluation, pass "NULL" for the reply_block.
66 * Note that it is assumed that the reply has already been
67 * matched to the key (and signatures checked) as it would
68 * be done with the "get_key" function.
71 * @param type block type
72 * @param query original query (hash)
73 * @param bf pointer to bloom filter associated with query; possibly updated (!)
74 * @param bf_mutator mutation value for bf
75 * @param xquery extrended query data (can be NULL, depending on type)
76 * @param xquery_size number of bytes in xquery
77 * @param reply_block response to validate
78 * @param reply_block_size number of bytes in reply block
79 * @return characterization of result
81 static enum GNUNET_BLOCK_EvaluationResult
82 evaluate_block_regex (void *cls, enum GNUNET_BLOCK_Type type,
83 const struct GNUNET_HashCode *query,
84 struct GNUNET_CONTAINER_BloomFilter **bf,
85 int32_t bf_mutator, const void *xquery,
86 size_t xquery_size, const void *reply_block,
87 size_t reply_block_size)
89 if (NULL == reply_block) /* queries (GET) are always valid */
90 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
95 query = (const char *) xquery;
96 if ('\0' != query[xquery_size - 1]) /* must be valid 0-terminated string */
98 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 "Block xquery not a valid string\n");
100 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
103 else if (NULL != query) /* PUTs don't need xquery */
105 const struct RegexBlock *rblock = reply_block;
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Block with no xquery, query: %s, %u edges\n",
111 ntohl (rblock->n_edges));
112 REGEX_BLOCK_iterate (rblock, reply_block_size, &rdebug, NULL);
113 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
115 switch (REGEX_BLOCK_check (reply_block,
122 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "BLOCK XQUERY %s not accepted\n", xquery);
126 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "BLOCK XQUERY %s accepted\n", xquery);
134 struct GNUNET_HashCode chash;
135 struct GNUNET_HashCode mhash;
137 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
138 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
141 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
142 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
146 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
148 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
150 return GNUNET_BLOCK_EVALUATION_OK_MORE;
155 * Function called to validate a reply or a request of type
156 * GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
157 * For request evaluation, pass "NULL" for the reply_block.
158 * Note that it is assumed that the reply has already been
159 * matched to the key (and signatures checked) as it would
160 * be done with the "get_key" function.
163 * @param type block type
164 * @param query original query (hash)
165 * @param bf pointer to bloom filter associated with query; possibly updated (!)
166 * @param bf_mutator mutation value for bf
167 * @param xquery extrended query data (can be NULL, depending on type)
168 * @param xquery_size number of bytes in xquery
169 * @param reply_block response to validate
170 * @param reply_block_size number of bytes in reply block
171 * @return characterization of result
173 static enum GNUNET_BLOCK_EvaluationResult
174 evaluate_block_regex_accept (void *cls, enum GNUNET_BLOCK_Type type,
175 const struct GNUNET_HashCode * query,
176 struct GNUNET_CONTAINER_BloomFilter **bf,
177 int32_t bf_mutator, const void *xquery,
178 size_t xquery_size, const void *reply_block,
179 size_t reply_block_size)
181 if (0 != xquery_size)
184 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
186 if (NULL == reply_block)
187 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
188 if (sizeof (struct RegexAccept) != reply_block_size)
191 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
195 struct GNUNET_HashCode chash;
196 struct GNUNET_HashCode mhash;
198 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
199 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
202 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
203 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
207 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
209 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
211 return GNUNET_BLOCK_EVALUATION_OK_MORE;
216 * Function called to validate a reply or a request. For
217 * request evaluation, simply pass "NULL" for the reply_block.
218 * Note that it is assumed that the reply has already been
219 * matched to the key (and signatures checked) as it would
220 * be done with the "get_key" function.
223 * @param type block type
224 * @param query original query (hash)
225 * @param bf pointer to bloom filter associated with query; possibly updated (!)
226 * @param bf_mutator mutation value for bf
227 * @param xquery extrended query data (can be NULL, depending on type)
228 * @param xquery_size number of bytes in xquery
229 * @param reply_block response to validate
230 * @param reply_block_size number of bytes in reply block
231 * @return characterization of result
233 static enum GNUNET_BLOCK_EvaluationResult
234 block_plugin_regex_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
235 const struct GNUNET_HashCode * query,
236 struct GNUNET_CONTAINER_BloomFilter **bf,
237 int32_t bf_mutator, const void *xquery,
238 size_t xquery_size, const void *reply_block,
239 size_t reply_block_size)
241 enum GNUNET_BLOCK_EvaluationResult result;
245 case GNUNET_BLOCK_TYPE_REGEX:
246 result = evaluate_block_regex (cls, type, query, bf, bf_mutator,
248 reply_block, reply_block_size);
251 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
252 result = evaluate_block_regex_accept (cls, type, query, bf, bf_mutator,
254 reply_block, reply_block_size);
258 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
265 * Function called to obtain the key for a block.
268 * @param type block type
269 * @param block block to get the key for
270 * @param block_size number of bytes in block
271 * @param key set to the key (query) for the given block
272 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
273 * (or if extracting a key from a block of this type does not work)
276 block_plugin_regex_get_key (void *cls, enum GNUNET_BLOCK_Type type,
277 const void *block, size_t block_size,
278 struct GNUNET_HashCode * key)
284 case GNUNET_BLOCK_TYPE_REGEX:
285 ret = REGEX_BLOCK_get_key (block, block_size,
287 GNUNET_break_op (GNUNET_OK == ret);
289 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
290 GNUNET_assert (sizeof (struct RegexAccept) <= block_size);
291 *key = ((struct RegexAccept *) block)->key;
295 return GNUNET_SYSERR;
301 * Entry point for the plugin.
304 libgnunet_plugin_block_regex_init (void *cls)
306 static enum GNUNET_BLOCK_Type types[] =
308 GNUNET_BLOCK_TYPE_REGEX,
309 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
310 GNUNET_BLOCK_TYPE_ANY /* end of list */
312 struct GNUNET_BLOCK_PluginFunctions *api;
314 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
315 api->evaluate = &block_plugin_regex_evaluate;
316 api->get_key = &block_plugin_regex_get_key;
323 * Exit point from the plugin.
326 libgnunet_plugin_block_regex_done (void *cls)
328 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
334 /* end of plugin_block_regex.c */