565356786f16f8c085735483675ebbbd6c7a2c3c
[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, "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;
113   }
114   switch (REGEX_INTERNAL_block_check (reply_block,
115                                       reply_block_size,
116                                       xquery))
117   {
118     case GNUNET_SYSERR:
119       GNUNET_break_op(0);
120       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
121     case GNUNET_NO:
122       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123                   "BLOCK XQUERY %s not accepted\n", xquery);
124       return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
125     default:
126       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127                   "BLOCK XQUERY %s accepted\n", xquery);
128       break;
129   }
130   if (NULL != bf)
131   {
132     struct GNUNET_HashCode chash;
133     struct GNUNET_HashCode mhash;
134
135     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
136     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
137     if (NULL != *bf)
138     {
139       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
140         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
141     }
142     else
143     {
144       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
145     }
146     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
147   }
148   return GNUNET_BLOCK_EVALUATION_OK_MORE;
149 }
150
151
152 /**
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.
159  *
160  * @param cls closure
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
170  */
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)
178 {
179   if (0 != xquery_size)
180   {
181     GNUNET_break_op (0);
182     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
183   }
184   if (NULL == reply_block)
185     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
186   if (sizeof (struct RegexAccept) != reply_block_size)
187   {
188     GNUNET_break_op(0);
189     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
190   }
191   if (NULL != bf)
192   {
193     struct GNUNET_HashCode chash;
194     struct GNUNET_HashCode mhash;
195
196     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
197     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
198     if (NULL != *bf)
199     {
200       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
201         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
202     }
203     else
204     {
205       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
206     }
207     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
208   }
209   return GNUNET_BLOCK_EVALUATION_OK_MORE;
210 }
211
212
213 /**
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.
219  *
220  * @param cls closure
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
230  */
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)
238 {
239   enum GNUNET_BLOCK_EvaluationResult result;
240
241   switch (type)
242   {
243     case GNUNET_BLOCK_TYPE_REGEX:
244       result = evaluate_block_regex (cls, type, query, bf, bf_mutator,
245                                      xquery, xquery_size,
246                                      reply_block, reply_block_size);
247       break;
248
249     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
250       result = evaluate_block_regex_accept (cls, type, query, bf, bf_mutator,
251                                             xquery, xquery_size,
252                                             reply_block, reply_block_size);
253       break;
254
255     default:
256       result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
257   }
258   return result;
259 }
260
261
262 /**
263  * Function called to obtain the key for a block.
264  *
265  * @param cls closure
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)
272  */
273 static int
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)
277 {
278   switch (type)
279   {
280     case GNUNET_BLOCK_TYPE_REGEX:
281       GNUNET_assert (sizeof (struct RegexBlock) <= block_size);
282       *key = ((struct RegexBlock *) block)->key;
283       return GNUNET_OK;
284     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
285       GNUNET_assert (sizeof (struct RegexAccept) <= block_size);
286       *key = ((struct RegexAccept *) block)->key;
287       return GNUNET_OK;
288     default:
289       GNUNET_break (0);
290       return GNUNET_SYSERR;
291   }
292 }
293
294
295 /**
296  * Entry point for the plugin.
297  */
298 void *
299 libgnunet_plugin_block_regex_init (void *cls)
300 {
301   static enum GNUNET_BLOCK_Type types[] =
302   {
303     GNUNET_BLOCK_TYPE_REGEX,
304     GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
305     GNUNET_BLOCK_TYPE_ANY       /* end of list */
306   };
307   struct GNUNET_BLOCK_PluginFunctions *api;
308
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;
312   api->types = types;
313   return api;
314 }
315
316
317 /**
318  * Exit point from the plugin.
319  */
320 void *
321 libgnunet_plugin_block_regex_done (void *cls)
322 {
323   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
324
325   GNUNET_free (api);
326   return NULL;
327 }
328
329 /* end of plugin_block_regex.c */