9eeb13190d3cd81bf00190206f40c38713f778b9
[oweals/gnunet.git] / src / regex / plugin_block_regex.c
1 /*
2      This file is part of GNUnet
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file regex/plugin_block_regex.c
23  * @brief blocks used for regex storage and search
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_block_plugin.h"
29 #include "block_regex.h"
30 #include "regex_block_lib.h"
31
32 /**
33  * Number of bits we set per entry in the bloomfilter.
34  * Do not change!
35  */
36 #define BLOOMFILTER_K 16
37
38
39 /**
40  * Show debug info about outgoing edges from a block.
41  * 
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.
46  * 
47  * @return GNUNET_YES to keep iterating.
48  */
49 static int
50 rdebug (void *cls,
51         const char *token,
52         size_t len,
53         const struct GNUNET_HashCode *key)
54 {
55   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
56               "%s: %.*s\n",
57               GNUNET_h2s (key), len, token);
58   return GNUNET_YES;
59 }
60
61
62 /**
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.
69  *
70  * @param cls closure
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
80  */
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)
88 {
89   if (NULL == reply_block) /* queries (GET) are always valid */
90     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
91   if (0 != xquery_size)
92   {
93     const char *query;
94
95     query = (const char *) xquery;
96     if ('\0' != query[xquery_size - 1]) /* must be valid 0-terminated string */
97     {
98       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99                   "Block xquery not a valid string\n");
100       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
101     }
102   }
103   else if (NULL != query) /* PUTs don't need xquery */
104   {
105     const struct RegexBlock *rblock = reply_block;
106
107     GNUNET_break_op (0);
108     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
109                 "Block with no xquery, query: %s, %u edges\n",
110                 GNUNET_h2s (query), 
111                 ntohl (rblock->n_edges));
112     REGEX_BLOCK_iterate (rblock, reply_block_size, &rdebug, NULL);
113     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
114   }
115   switch (REGEX_BLOCK_check (reply_block,
116                              reply_block_size,
117                              query,
118                              xquery))
119   {
120     case GNUNET_SYSERR:
121       GNUNET_break_op(0);
122       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
123     case GNUNET_NO:
124       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125                   "BLOCK XQUERY %s not accepted\n", xquery);
126       return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
127     default:
128       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129                   "BLOCK XQUERY %s accepted\n", xquery);
130       break;
131   }
132   if (NULL != bf)
133   {
134     struct GNUNET_HashCode chash;
135     struct GNUNET_HashCode mhash;
136
137     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
138     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
139     if (NULL != *bf)
140     {
141       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
142         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
143     }
144     else
145     {
146       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
147     }
148     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
149   }
150   return GNUNET_BLOCK_EVALUATION_OK_MORE;
151 }
152
153
154 /**
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.
161  *
162  * @param cls closure
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
172  */
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)
180 {
181   if (0 != xquery_size)
182   {
183     GNUNET_break_op (0);
184     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
185   }
186   if (NULL == reply_block)
187     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
188   if (sizeof (struct RegexAccept) != reply_block_size)
189   {
190     GNUNET_break_op(0);
191     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
192   }
193   if (NULL != bf)
194   {
195     struct GNUNET_HashCode chash;
196     struct GNUNET_HashCode mhash;
197
198     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
199     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
200     if (NULL != *bf)
201     {
202       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
203         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
204     }
205     else
206     {
207       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
208     }
209     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
210   }
211   return GNUNET_BLOCK_EVALUATION_OK_MORE;
212 }
213
214
215 /**
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.
221  *
222  * @param cls closure
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
232  */
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)
240 {
241   enum GNUNET_BLOCK_EvaluationResult result;
242
243   switch (type)
244   {
245     case GNUNET_BLOCK_TYPE_REGEX:
246       result = evaluate_block_regex (cls, type, query, bf, bf_mutator,
247                                      xquery, xquery_size,
248                                      reply_block, reply_block_size);
249       break;
250
251     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
252       result = evaluate_block_regex_accept (cls, type, query, bf, bf_mutator,
253                                             xquery, xquery_size,
254                                             reply_block, reply_block_size);
255       break;
256
257     default:
258       result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
259   }
260   return result;
261 }
262
263
264 /**
265  * Function called to obtain the key for a block.
266  *
267  * @param cls closure
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)
274  */
275 static int
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)
279 {
280   int ret;
281
282   switch (type)
283   {
284     case GNUNET_BLOCK_TYPE_REGEX:
285       ret = REGEX_BLOCK_get_key (block, block_size,
286                                  key);
287       GNUNET_break_op (GNUNET_OK == ret);
288       return ret;
289     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
290       GNUNET_assert (sizeof (struct RegexAccept) <= block_size);
291       *key = ((struct RegexAccept *) block)->key;
292       return GNUNET_OK;
293     default:
294       GNUNET_break (0);
295       return GNUNET_SYSERR;
296   }
297 }
298
299
300 /**
301  * Entry point for the plugin.
302  */
303 void *
304 libgnunet_plugin_block_regex_init (void *cls)
305 {
306   static enum GNUNET_BLOCK_Type types[] =
307   {
308     GNUNET_BLOCK_TYPE_REGEX,
309     GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
310     GNUNET_BLOCK_TYPE_ANY       /* end of list */
311   };
312   struct GNUNET_BLOCK_PluginFunctions *api;
313
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;
317   api->types = types;
318   return api;
319 }
320
321
322 /**
323  * Exit point from the plugin.
324  */
325 void *
326 libgnunet_plugin_block_regex_done (void *cls)
327 {
328   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
329
330   GNUNET_free (api);
331   return NULL;
332 }
333
334 /* end of plugin_block_regex.c */