ensure all plugins properly use BF, move shared logic to shared library
[oweals/gnunet.git] / src / regex / plugin_block_regex.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #include "platform.h"
27 #include "gnunet_block_plugin.h"
28 #include "gnunet_block_group_lib.h"
29 #include "block_regex.h"
30 #include "regex_block_lib.h"
31 #include "gnunet_signatures.h"
32
33
34 /**
35  * Number of bits we set per entry in the bloomfilter.
36  * Do not change!
37  */
38 #define BLOOMFILTER_K 16
39
40
41 /**
42  * How big is the BF we use for REGEX blocks?
43  */
44 #define REGEX_BF_SIZE 8
45
46
47 /**
48  * Create a new block group.
49  *
50  * @param ctx block context in which the block group is created
51  * @param type type of the block for which we are creating the group
52  * @param nonce random value used to seed the group creation
53  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
54  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
55  * @param va variable arguments specific to @a type
56  * @return block group handle, NULL if block groups are not supported
57  *         by this @a type of block (this is not an error)
58  */
59 static struct GNUNET_BLOCK_Group *
60 block_plugin_regex_create_group (void *cls,
61                                  enum GNUNET_BLOCK_Type type,
62                                  uint32_t nonce,
63                                  const void *raw_data,
64                                  size_t raw_data_size,
65                                  va_list va)
66 {
67   unsigned int bf_size;
68   const char *guard;
69
70   guard = va_arg (va, const char *);
71   if (0 == strcmp (guard,
72                    "seen-set-size"))
73     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
74                                                            BLOOMFILTER_K);
75   else if (0 == strcmp (guard,
76                         "filter-size"))
77     bf_size = va_arg (va, unsigned int);
78   else
79   {
80     GNUNET_break (0);
81     bf_size = REGEX_BF_SIZE;
82   }
83   GNUNET_break (NULL == va_arg (va, const char *));
84   return GNUNET_BLOCK_GROUP_bf_create (cls,
85                                        bf_size,
86                                        BLOOMFILTER_K,
87                                        type,
88                                        nonce,
89                                        raw_data,
90                                        raw_data_size);
91 }
92
93
94 /**
95  * Function called to validate a reply or a request of type
96  * #GNUNET_BLOCK_TYPE_REGEX.
97  * For request evaluation, pass "NULL" for the reply_block.
98  * Note that it is assumed that the reply has already been
99  * matched to the key (and signatures checked) as it would
100  * be done with the #GNUNET_BLOCK_get_key() function.
101  *
102  * @param cls closure
103  * @param type block type
104  * @param bg block group to evaluate against
105  * @param eo control flags
106  * @param query original query (hash)
107  * @param xquery extrended query data (can be NULL, depending on type)
108  * @param xquery_size number of bytes in @a xquery
109  * @param reply_block response to validate
110  * @param reply_block_size number of bytes in @a reply_block
111  * @return characterization of result
112  */
113 static enum GNUNET_BLOCK_EvaluationResult
114 evaluate_block_regex (void *cls,
115                       enum GNUNET_BLOCK_Type type,
116                       struct GNUNET_BLOCK_Group *bg,
117                       enum GNUNET_BLOCK_EvaluationOptions eo,
118                       const struct GNUNET_HashCode *query,
119                       const void *xquery,
120                       size_t xquery_size,
121                       const void *reply_block,
122                       size_t reply_block_size)
123 {
124   struct GNUNET_HashCode chash;
125
126   if (NULL == reply_block)
127   {
128     if (0 != xquery_size)
129       {
130         const char *s;
131
132         s = (const char *) xquery;
133         if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
134           {
135             GNUNET_break_op (0);
136             return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137           }
138       }
139     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
140   }
141   if (0 != xquery_size)
142   {
143     const char *s;
144
145     s = (const char *) xquery;
146     if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
147     {
148       GNUNET_break_op (0);
149       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150     }
151   }
152   else if (NULL != query)
153   {
154     /* xquery is required for regex GETs, at least an empty string */
155     GNUNET_break_op (0);
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
157                 type, query, xquery);
158     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159   }
160   switch (REGEX_BLOCK_check (reply_block,
161                              reply_block_size,
162                              query,
163                              xquery))
164   {
165     case GNUNET_SYSERR:
166       GNUNET_break_op(0);
167       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
168     case GNUNET_NO:
169       /* xquery missmatch, can happen */
170       return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
171     default:
172       break;
173   }
174   GNUNET_CRYPTO_hash (reply_block,
175                       reply_block_size,
176                       &chash);
177   if (GNUNET_YES ==
178       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
179                                           &chash))
180     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
181   return GNUNET_BLOCK_EVALUATION_OK_MORE;
182 }
183
184
185 /**
186  * Function called to validate a reply or a request of type
187  * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
188  * For request evaluation, pass "NULL" for the reply_block.
189  * Note that it is assumed that the reply has already been
190  * matched to the key (and signatures checked) as it would
191  * be done with the #GNUNET_BLOCK_get_key() function.
192  *
193  * @param cls closure
194  * @param type block type
195  * @param bg block group to evaluate against
196  * @param eo control flags
197  * @param query original query (hash)
198  * @param xquery extrended query data (can be NULL, depending on type)
199  * @param xquery_size number of bytes in @a xquery
200  * @param reply_block response to validate
201  * @param reply_block_size number of bytes in @a reply_block
202  * @return characterization of result
203  */
204 static enum GNUNET_BLOCK_EvaluationResult
205 evaluate_block_regex_accept (void *cls,
206                              enum GNUNET_BLOCK_Type type,
207                              struct GNUNET_BLOCK_Group *bg,
208                              enum GNUNET_BLOCK_EvaluationOptions eo,
209                              const struct GNUNET_HashCode *query,
210                              const void *xquery,
211                              size_t xquery_size, const void *reply_block,
212                              size_t reply_block_size)
213 {
214   const struct RegexAcceptBlock *rba;
215   struct GNUNET_HashCode chash;
216
217   if (0 != xquery_size)
218   {
219     GNUNET_break_op (0);
220     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
221   }
222   if (NULL == reply_block)
223     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
224   if (sizeof (struct RegexAcceptBlock) != reply_block_size)
225   {
226     GNUNET_break_op(0);
227     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
228   }
229   rba = reply_block;
230   if (ntohl (rba->purpose.size) !=
231       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
232       sizeof (struct GNUNET_TIME_AbsoluteNBO) +
233       sizeof (struct GNUNET_HashCode))
234   {
235     GNUNET_break_op(0);
236     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
237   }
238   if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
239   {
240     /* technically invalid, but can happen without an error, so
241        we're nice by reporting it as a 'duplicate' */
242     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
243   }
244   if (GNUNET_OK !=
245       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
246                                 &rba->purpose,
247                                 &rba->signature,
248                                 &rba->peer.public_key))
249   {
250     GNUNET_break_op(0);
251     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
252   }
253   GNUNET_CRYPTO_hash (reply_block,
254                       reply_block_size,
255                       &chash);
256   if (GNUNET_YES ==
257       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
258                                           &chash))
259     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
260   return GNUNET_BLOCK_EVALUATION_OK_MORE;
261 }
262
263
264 /**
265  * Function called to validate a reply or a request.  For
266  * request evaluation, simply pass "NULL" for the reply_block.
267  * Note that it is assumed that the reply has already been
268  * matched to the key (and signatures checked) as it would
269  * be done with the #GNUNET_BLOCK_get_key() function.
270  *
271  * @param cls closure
272  * @param type block type
273  * @param bg group to evaluate against
274  * @param eo control flags
275  * @param query original query (hash)
276  * @param xquery extrended query data (can be NULL, depending on type)
277  * @param xquery_size number of bytes in xquery
278  * @param reply_block response to validate
279  * @param reply_block_size number of bytes in reply block
280  * @return characterization of result
281  */
282 static enum GNUNET_BLOCK_EvaluationResult
283 block_plugin_regex_evaluate (void *cls,
284                              enum GNUNET_BLOCK_Type type,
285                              struct GNUNET_BLOCK_Group *bg,
286                              enum GNUNET_BLOCK_EvaluationOptions eo,
287                              const struct GNUNET_HashCode *query,
288                              const void *xquery,
289                              size_t xquery_size,
290                              const void *reply_block,
291                              size_t reply_block_size)
292 {
293   enum GNUNET_BLOCK_EvaluationResult result;
294
295   switch (type)
296   {
297     case GNUNET_BLOCK_TYPE_REGEX:
298       result = evaluate_block_regex (cls,
299                                      type,
300                                      bg,
301                                      eo,
302                                      query,
303                                      xquery, xquery_size,
304                                      reply_block, reply_block_size);
305       break;
306     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
307       result = evaluate_block_regex_accept (cls,
308                                             type,
309                                             bg,
310                                             eo,
311                                             query,
312                                             xquery, xquery_size,
313                                             reply_block, reply_block_size);
314       break;
315
316     default:
317       result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
318   }
319   return result;
320 }
321
322
323 /**
324  * Function called to obtain the key for a block.
325  *
326  * @param cls closure
327  * @param type block type
328  * @param block block to get the key for
329  * @param block_size number of bytes in @a block
330  * @param key set to the key (query) for the given block
331  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
332  *         (or if extracting a key from a block of this type does not work)
333  */
334 static int
335 block_plugin_regex_get_key (void *cls,
336                             enum GNUNET_BLOCK_Type type,
337                             const void *block,
338                             size_t block_size,
339                             struct GNUNET_HashCode *key)
340 {
341   switch (type)
342   {
343     case GNUNET_BLOCK_TYPE_REGEX:
344       if (GNUNET_OK !=
345           REGEX_BLOCK_get_key (block, block_size,
346                                key))
347       {
348         GNUNET_break_op (0);
349         return GNUNET_NO;
350       }
351       return GNUNET_OK;
352     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
353       if (sizeof (struct RegexAcceptBlock) != block_size)
354       {
355         GNUNET_break_op (0);
356         return GNUNET_NO;
357       }
358       *key = ((struct RegexAcceptBlock *) block)->key;
359       return GNUNET_OK;
360     default:
361       GNUNET_break (0);
362       return GNUNET_SYSERR;
363   }
364 }
365
366
367 /**
368  * Entry point for the plugin.
369  */
370 void *
371 libgnunet_plugin_block_regex_init (void *cls)
372 {
373   static enum GNUNET_BLOCK_Type types[] =
374   {
375     GNUNET_BLOCK_TYPE_REGEX,
376     GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
377     GNUNET_BLOCK_TYPE_ANY       /* end of list */
378   };
379   struct GNUNET_BLOCK_PluginFunctions *api;
380
381   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
382   api->evaluate = &block_plugin_regex_evaluate;
383   api->get_key = &block_plugin_regex_get_key;
384   api->create_group = &block_plugin_regex_create_group;
385   api->types = types;
386   return api;
387 }
388
389
390 /**
391  * Exit point from the plugin.
392  */
393 void *
394 libgnunet_plugin_block_regex_done (void *cls)
395 {
396   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
397
398   GNUNET_free (api);
399   return NULL;
400 }
401
402 /* end of plugin_block_regex.c */