fix bad free
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file regex/plugin_block_regex.c
21  * @brief blocks used for regex storage and search
22  * @author Bartlomiej Polot
23  */
24 #include "platform.h"
25 #include "gnunet_block_plugin.h"
26 #include "gnunet_block_group_lib.h"
27 #include "block_regex.h"
28 #include "regex_block_lib.h"
29 #include "gnunet_signatures.h"
30
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  * How big is the BF we use for REGEX blocks?
41  */
42 #define REGEX_BF_SIZE 8
43
44
45 /**
46  * Create a new block group.
47  *
48  * @param ctx block context in which the block group is created
49  * @param type type of the block for which we are creating the group
50  * @param nonce random value used to seed the group creation
51  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
52  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
53  * @param va variable arguments specific to @a type
54  * @return block group handle, NULL if block groups are not supported
55  *         by this @a type of block (this is not an error)
56  */
57 static struct GNUNET_BLOCK_Group *
58 block_plugin_regex_create_group (void *cls,
59                                  enum GNUNET_BLOCK_Type type,
60                                  uint32_t nonce,
61                                  const void *raw_data,
62                                  size_t raw_data_size,
63                                  va_list va)
64 {
65   unsigned int bf_size;
66   const char *guard;
67
68   guard = va_arg (va, const char *);
69   if (0 == strcmp (guard,
70                    "seen-set-size"))
71     bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
72                                                            BLOOMFILTER_K);
73   else if (0 == strcmp (guard,
74                         "filter-size"))
75     bf_size = va_arg (va, unsigned int);
76   else
77   {
78     GNUNET_break (0);
79     bf_size = REGEX_BF_SIZE;
80   }
81   GNUNET_break (NULL == va_arg (va, const char *));
82   return GNUNET_BLOCK_GROUP_bf_create (cls,
83                                        bf_size,
84                                        BLOOMFILTER_K,
85                                        type,
86                                        nonce,
87                                        raw_data,
88                                        raw_data_size);
89 }
90
91
92 /**
93  * Function called to validate a reply or a request of type
94  * #GNUNET_BLOCK_TYPE_REGEX.
95  * For request evaluation, pass "NULL" for the reply_block.
96  * Note that it is assumed that the reply has already been
97  * matched to the key (and signatures checked) as it would
98  * be done with the #GNUNET_BLOCK_get_key() function.
99  *
100  * @param cls closure
101  * @param type block type
102  * @param bg block group to evaluate against
103  * @param eo control flags
104  * @param query original query (hash)
105  * @param xquery extrended query data (can be NULL, depending on type)
106  * @param xquery_size number of bytes in @a xquery
107  * @param reply_block response to validate
108  * @param reply_block_size number of bytes in @a reply_block
109  * @return characterization of result
110  */
111 static enum GNUNET_BLOCK_EvaluationResult
112 evaluate_block_regex (void *cls,
113                       enum GNUNET_BLOCK_Type type,
114                       struct GNUNET_BLOCK_Group *bg,
115                       enum GNUNET_BLOCK_EvaluationOptions eo,
116                       const struct GNUNET_HashCode *query,
117                       const void *xquery,
118                       size_t xquery_size,
119                       const void *reply_block,
120                       size_t reply_block_size)
121 {
122   struct GNUNET_HashCode chash;
123
124   if (NULL == reply_block)
125   {
126     if (0 != xquery_size)
127       {
128         const char *s;
129
130         s = (const char *) xquery;
131         if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
132           {
133             GNUNET_break_op (0);
134             return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
135           }
136       }
137     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
138   }
139   if (0 != xquery_size)
140   {
141     const char *s;
142
143     s = (const char *) xquery;
144     if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
145     {
146       GNUNET_break_op (0);
147       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
148     }
149   }
150   else if (NULL != query)
151   {
152     /* xquery is required for regex GETs, at least an empty string */
153     GNUNET_break_op (0);
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
155                 type, query, xquery);
156     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
157   }
158   switch (REGEX_BLOCK_check (reply_block,
159                              reply_block_size,
160                              query,
161                              xquery))
162   {
163     case GNUNET_SYSERR:
164       GNUNET_break_op(0);
165       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
166     case GNUNET_NO:
167       /* xquery missmatch, can happen */
168       return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
169     default:
170       break;
171   }
172   GNUNET_CRYPTO_hash (reply_block,
173                       reply_block_size,
174                       &chash);
175   if (GNUNET_YES ==
176       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
177                                           &chash))
178     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
179   return GNUNET_BLOCK_EVALUATION_OK_MORE;
180 }
181
182
183 /**
184  * Function called to validate a reply or a request of type
185  * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
186  * For request evaluation, pass "NULL" for the reply_block.
187  * Note that it is assumed that the reply has already been
188  * matched to the key (and signatures checked) as it would
189  * be done with the #GNUNET_BLOCK_get_key() function.
190  *
191  * @param cls closure
192  * @param type block type
193  * @param bg block group to evaluate against
194  * @param eo control flags
195  * @param query original query (hash)
196  * @param xquery extrended query data (can be NULL, depending on type)
197  * @param xquery_size number of bytes in @a xquery
198  * @param reply_block response to validate
199  * @param reply_block_size number of bytes in @a reply_block
200  * @return characterization of result
201  */
202 static enum GNUNET_BLOCK_EvaluationResult
203 evaluate_block_regex_accept (void *cls,
204                              enum GNUNET_BLOCK_Type type,
205                              struct GNUNET_BLOCK_Group *bg,
206                              enum GNUNET_BLOCK_EvaluationOptions eo,
207                              const struct GNUNET_HashCode *query,
208                              const void *xquery,
209                              size_t xquery_size, const void *reply_block,
210                              size_t reply_block_size)
211 {
212   const struct RegexAcceptBlock *rba;
213   struct GNUNET_HashCode chash;
214
215   if (0 != xquery_size)
216   {
217     GNUNET_break_op (0);
218     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
219   }
220   if (NULL == reply_block)
221     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
222   if (sizeof (struct RegexAcceptBlock) != reply_block_size)
223   {
224     GNUNET_break_op(0);
225     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
226   }
227   rba = reply_block;
228   if (ntohl (rba->purpose.size) !=
229       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
230       sizeof (struct GNUNET_TIME_AbsoluteNBO) +
231       sizeof (struct GNUNET_HashCode))
232   {
233     GNUNET_break_op(0);
234     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
235   }
236   if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
237   {
238     /* technically invalid, but can happen without an error, so
239        we're nice by reporting it as a 'duplicate' */
240     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
241   }
242   if (GNUNET_OK !=
243       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
244                                 &rba->purpose,
245                                 &rba->signature,
246                                 &rba->peer.public_key))
247   {
248     GNUNET_break_op(0);
249     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
250   }
251   GNUNET_CRYPTO_hash (reply_block,
252                       reply_block_size,
253                       &chash);
254   if (GNUNET_YES ==
255       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
256                                           &chash))
257     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
258   return GNUNET_BLOCK_EVALUATION_OK_MORE;
259 }
260
261
262 /**
263  * Function called to validate a reply or a request.  For
264  * request evaluation, simply pass "NULL" for the reply_block.
265  * Note that it is assumed that the reply has already been
266  * matched to the key (and signatures checked) as it would
267  * be done with the #GNUNET_BLOCK_get_key() function.
268  *
269  * @param cls closure
270  * @param ctx block context
271  * @param type block type
272  * @param bg group to evaluate against
273  * @param eo control flags
274  * @param query original query (hash)
275  * @param xquery extrended query data (can be NULL, depending on type)
276  * @param xquery_size number of bytes in xquery
277  * @param reply_block response to validate
278  * @param reply_block_size number of bytes in reply block
279  * @return characterization of result
280  */
281 static enum GNUNET_BLOCK_EvaluationResult
282 block_plugin_regex_evaluate (void *cls,
283                              struct GNUNET_BLOCK_Context *ctx,
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_BLOCK_PluginFunctions *api = cls;
397
398   GNUNET_free (api);
399   return NULL;
400 }
401
402 /* end of plugin_block_regex.c */