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, "Block with no xquery\n");
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " key: %s, %u edges\n",
110 GNUNET_h2s (&rblock->key), ntohl (rblock->n_edges));
111 REGEX_INTERNAL_block_iterate (rblock, reply_block_size, &rdebug, NULL);
112 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
114 switch (REGEX_INTERNAL_block_check (reply_block,
120 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "BLOCK XQUERY %s not accepted\n", xquery);
124 return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127 "BLOCK XQUERY %s accepted\n", xquery);
132 struct GNUNET_HashCode chash;
133 struct GNUNET_HashCode mhash;
135 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
136 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
139 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
140 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
144 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
146 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
148 return GNUNET_BLOCK_EVALUATION_OK_MORE;
153 * Function called to validate a reply or a request of type
154 * GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
155 * For request evaluation, pass "NULL" for the reply_block.
156 * Note that it is assumed that the reply has already been
157 * matched to the key (and signatures checked) as it would
158 * be done with the "get_key" function.
161 * @param type block type
162 * @param query original query (hash)
163 * @param bf pointer to bloom filter associated with query; possibly updated (!)
164 * @param bf_mutator mutation value for bf
165 * @param xquery extrended query data (can be NULL, depending on type)
166 * @param xquery_size number of bytes in xquery
167 * @param reply_block response to validate
168 * @param reply_block_size number of bytes in reply block
169 * @return characterization of result
171 static enum GNUNET_BLOCK_EvaluationResult
172 evaluate_block_regex_accept (void *cls, enum GNUNET_BLOCK_Type type,
173 const struct GNUNET_HashCode * query,
174 struct GNUNET_CONTAINER_BloomFilter **bf,
175 int32_t bf_mutator, const void *xquery,
176 size_t xquery_size, const void *reply_block,
177 size_t reply_block_size)
179 if (0 != xquery_size)
182 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
184 if (NULL == reply_block)
185 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
186 if (sizeof (struct RegexAccept) != reply_block_size)
189 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
193 struct GNUNET_HashCode chash;
194 struct GNUNET_HashCode mhash;
196 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
197 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
200 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
201 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
205 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
207 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
209 return GNUNET_BLOCK_EVALUATION_OK_MORE;
214 * Function called to validate a reply or a request. For
215 * request evaluation, simply pass "NULL" for the reply_block.
216 * Note that it is assumed that the reply has already been
217 * matched to the key (and signatures checked) as it would
218 * be done with the "get_key" function.
221 * @param type block type
222 * @param query original query (hash)
223 * @param bf pointer to bloom filter associated with query; possibly updated (!)
224 * @param bf_mutator mutation value for bf
225 * @param xquery extrended query data (can be NULL, depending on type)
226 * @param xquery_size number of bytes in xquery
227 * @param reply_block response to validate
228 * @param reply_block_size number of bytes in reply block
229 * @return characterization of result
231 static enum GNUNET_BLOCK_EvaluationResult
232 block_plugin_regex_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
233 const struct GNUNET_HashCode * query,
234 struct GNUNET_CONTAINER_BloomFilter **bf,
235 int32_t bf_mutator, const void *xquery,
236 size_t xquery_size, const void *reply_block,
237 size_t reply_block_size)
239 enum GNUNET_BLOCK_EvaluationResult result;
243 case GNUNET_BLOCK_TYPE_REGEX:
244 result = evaluate_block_regex (cls, type, query, bf, bf_mutator,
246 reply_block, reply_block_size);
249 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
250 result = evaluate_block_regex_accept (cls, type, query, bf, bf_mutator,
252 reply_block, reply_block_size);
256 result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
263 * Function called to obtain the key for a block.
266 * @param type block type
267 * @param block block to get the key for
268 * @param block_size number of bytes in block
269 * @param key set to the key (query) for the given block
270 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
271 * (or if extracting a key from a block of this type does not work)
274 block_plugin_regex_get_key (void *cls, enum GNUNET_BLOCK_Type type,
275 const void *block, size_t block_size,
276 struct GNUNET_HashCode * key)
280 case GNUNET_BLOCK_TYPE_REGEX:
281 GNUNET_assert (sizeof (struct RegexBlock) <= block_size);
282 *key = ((struct RegexBlock *) block)->key;
284 case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
285 GNUNET_assert (sizeof (struct RegexAccept) <= block_size);
286 *key = ((struct RegexAccept *) block)->key;
290 return GNUNET_SYSERR;
296 * Entry point for the plugin.
299 libgnunet_plugin_block_regex_init (void *cls)
301 static enum GNUNET_BLOCK_Type types[] =
303 GNUNET_BLOCK_TYPE_REGEX,
304 GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
305 GNUNET_BLOCK_TYPE_ANY /* end of list */
307 struct GNUNET_BLOCK_PluginFunctions *api;
309 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
310 api->evaluate = &block_plugin_regex_evaluate;
311 api->get_key = &block_plugin_regex_get_key;
318 * Exit point from the plugin.
321 libgnunet_plugin_block_regex_done (void *cls)
323 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
329 /* end of plugin_block_regex.c */