-fix (C) notices
[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
27 #include "platform.h"
28 #include "gnunet_block_plugin.h"
29 #include "block_regex.h"
30 #include "regex_block_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_signatures.h"
33
34
35 /**
36  * Function called to validate a reply or a request of type
37  * #GNUNET_BLOCK_TYPE_REGEX.
38  * For request evaluation, pass "NULL" for the reply_block.
39  * Note that it is assumed that the reply has already been
40  * matched to the key (and signatures checked) as it would
41  * be done with the #GNUNET_BLOCK_get_key() function.
42  *
43  * @param cls closure
44  * @param type block type
45  * @param eo control flags
46  * @param query original query (hash)
47  * @param bf pointer to bloom filter associated with query; possibly updated (!)
48  * @param bf_mutator mutation value for bf
49  * @param xquery extrended query data (can be NULL, depending on type)
50  * @param xquery_size number of bytes in @a xquery
51  * @param reply_block response to validate
52  * @param reply_block_size number of bytes in @a reply_block
53  * @return characterization of result
54  */
55 static enum GNUNET_BLOCK_EvaluationResult
56 evaluate_block_regex (void *cls,
57                       enum GNUNET_BLOCK_Type type,
58                       enum GNUNET_BLOCK_EvaluationOptions eo,
59                       const struct GNUNET_HashCode *query,
60                       struct GNUNET_CONTAINER_BloomFilter **bf,
61                       int32_t bf_mutator,
62                       const void *xquery,
63                       size_t xquery_size,
64                       const void *reply_block,
65                       size_t reply_block_size)
66 {
67   if (NULL == reply_block)
68   {
69     if (0 != xquery_size)
70       {
71         const char *s;
72
73         s = (const char *) xquery;
74         if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
75           {
76             GNUNET_break_op (0);
77             return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
78           }
79       }
80     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
81   }
82   if (0 != xquery_size)
83   {
84     const char *s;
85
86     s = (const char *) xquery;
87     if ('\0' != s[xquery_size - 1]) /* must be valid 0-terminated string */
88     {
89       GNUNET_break_op (0);
90       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
91     }
92   }
93   else if (NULL != query)
94   {
95     /* xquery is required for regex GETs, at least an empty string */
96     GNUNET_break_op (0);
97     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "type %d, query %p, xquery %p\n",
98                 type, query, xquery);
99     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
100   }
101   switch (REGEX_BLOCK_check (reply_block,
102                              reply_block_size,
103                              query,
104                              xquery))
105   {
106     case GNUNET_SYSERR:
107       GNUNET_break_op(0);
108       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
109     case GNUNET_NO:
110       /* xquery missmatch, can happen */
111       return GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT;
112     default:
113       break;
114   }
115   if (NULL != bf)
116   {
117     struct GNUNET_HashCode chash;
118     struct GNUNET_HashCode mhash;
119
120     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
121     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
122     if (NULL != *bf)
123     {
124       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
125         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
126     }
127     else
128     {
129       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K);
130     }
131     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
132   }
133   return GNUNET_BLOCK_EVALUATION_OK_MORE;
134 }
135
136
137 /**
138  * Function called to validate a reply or a request of type
139  * #GNUNET_BLOCK_TYPE_REGEX_ACCEPT.
140  * For request evaluation, pass "NULL" for the reply_block.
141  * Note that it is assumed that the reply has already been
142  * matched to the key (and signatures checked) as it would
143  * be done with the #GNUNET_BLOCK_get_key() function.
144  *
145  * @param cls closure
146  * @param type block type
147  * @param eo control flags
148  * @param query original query (hash)
149  * @param bf pointer to bloom filter associated with query; possibly updated (!)
150  * @param bf_mutator mutation value for bf
151  * @param xquery extrended query data (can be NULL, depending on type)
152  * @param xquery_size number of bytes in @a xquery
153  * @param reply_block response to validate
154  * @param reply_block_size number of bytes in @a reply_block
155  * @return characterization of result
156  */
157 static enum GNUNET_BLOCK_EvaluationResult
158 evaluate_block_regex_accept (void *cls,
159                              enum GNUNET_BLOCK_Type type,
160                              enum GNUNET_BLOCK_EvaluationOptions eo,
161                              const struct GNUNET_HashCode * query,
162                              struct GNUNET_CONTAINER_BloomFilter **bf,
163                              int32_t bf_mutator, const void *xquery,
164                              size_t xquery_size, const void *reply_block,
165                              size_t reply_block_size)
166 {
167   const struct RegexAcceptBlock *rba;
168
169   if (0 != xquery_size)
170   {
171     GNUNET_break_op (0);
172     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
173   }
174   if (NULL == reply_block)
175     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
176   if (sizeof (struct RegexAcceptBlock) != reply_block_size)
177   {
178     GNUNET_break_op(0);
179     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
180   }
181   rba = reply_block;
182   if (ntohl (rba->purpose.size) !=
183       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
184       sizeof (struct GNUNET_TIME_AbsoluteNBO) +
185       sizeof (struct GNUNET_HashCode))
186   {
187     GNUNET_break_op(0);
188     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
189   }
190   if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (rba->expiration_time)).rel_value_us)
191   {
192     /* technically invalid, but can happen without an error, so
193        we're nice by reporting it as a 'duplicate' */
194     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
195   }
196   if (GNUNET_OK !=
197       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_REGEX_ACCEPT,
198                                 &rba->purpose,
199                                 &rba->signature,
200                                 &rba->peer.public_key))
201   {
202     GNUNET_break_op(0);
203     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
204   }
205   if (NULL != bf)
206   {
207     struct GNUNET_HashCode chash;
208     struct GNUNET_HashCode mhash;
209
210     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
211     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
212     if (NULL != *bf)
213     {
214       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
215         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
216     }
217     else
218     {
219       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, GNUNET_CONSTANTS_BLOOMFILTER_K);
220     }
221     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
222   }
223   return GNUNET_BLOCK_EVALUATION_OK_MORE;
224 }
225
226
227 /**
228  * Function called to validate a reply or a request.  For
229  * request evaluation, simply pass "NULL" for the reply_block.
230  * Note that it is assumed that the reply has already been
231  * matched to the key (and signatures checked) as it would
232  * be done with the #GNUNET_BLOCK_get_key() function.
233  *
234  * @param cls closure
235  * @param type block type
236  * @param eo control flags
237  * @param query original query (hash)
238  * @param bf pointer to bloom filter associated with query; possibly updated (!)
239  * @param bf_mutator mutation value for bf
240  * @param xquery extrended query data (can be NULL, depending on type)
241  * @param xquery_size number of bytes in xquery
242  * @param reply_block response to validate
243  * @param reply_block_size number of bytes in reply block
244  * @return characterization of result
245  */
246 static enum GNUNET_BLOCK_EvaluationResult
247 block_plugin_regex_evaluate (void *cls,
248                              enum GNUNET_BLOCK_Type type,
249                              enum GNUNET_BLOCK_EvaluationOptions eo,
250                              const struct GNUNET_HashCode *query,
251                              struct GNUNET_CONTAINER_BloomFilter **bf,
252                              int32_t bf_mutator,
253                              const void *xquery,
254                              size_t xquery_size,
255                              const void *reply_block,
256                              size_t reply_block_size)
257 {
258   enum GNUNET_BLOCK_EvaluationResult result;
259
260   switch (type)
261   {
262     case GNUNET_BLOCK_TYPE_REGEX:
263       result = evaluate_block_regex (cls,
264                                      type,
265                                      eo,
266                                      query,
267                                      bf, bf_mutator,
268                                      xquery, xquery_size,
269                                      reply_block, reply_block_size);
270       break;
271     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
272       result = evaluate_block_regex_accept (cls,
273                                             type,
274                                             eo,
275                                             query,
276                                             bf, bf_mutator,
277                                             xquery, xquery_size,
278                                             reply_block, reply_block_size);
279       break;
280
281     default:
282       result = GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
283   }
284   return result;
285 }
286
287
288 /**
289  * Function called to obtain the key for a block.
290  *
291  * @param cls closure
292  * @param type block type
293  * @param block block to get the key for
294  * @param block_size number of bytes in @a block
295  * @param key set to the key (query) for the given block
296  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
297  *         (or if extracting a key from a block of this type does not work)
298  */
299 static int
300 block_plugin_regex_get_key (void *cls,
301                             enum GNUNET_BLOCK_Type type,
302                             const void *block,
303                             size_t block_size,
304                             struct GNUNET_HashCode *key)
305 {
306   switch (type)
307   {
308     case GNUNET_BLOCK_TYPE_REGEX:
309       if (GNUNET_OK !=
310           REGEX_BLOCK_get_key (block, block_size,
311                                key))
312       {
313         GNUNET_break_op (0);
314         return GNUNET_NO;
315       }
316       return GNUNET_OK;
317     case GNUNET_BLOCK_TYPE_REGEX_ACCEPT:
318       if (sizeof (struct RegexAcceptBlock) != block_size)
319       {
320         GNUNET_break_op (0);
321         return GNUNET_NO;
322       }
323       *key = ((struct RegexAcceptBlock *) block)->key;
324       return GNUNET_OK;
325     default:
326       GNUNET_break (0);
327       return GNUNET_SYSERR;
328   }
329 }
330
331
332 /**
333  * Entry point for the plugin.
334  */
335 void *
336 libgnunet_plugin_block_regex_init (void *cls)
337 {
338   static enum GNUNET_BLOCK_Type types[] =
339   {
340     GNUNET_BLOCK_TYPE_REGEX,
341     GNUNET_BLOCK_TYPE_REGEX_ACCEPT,
342     GNUNET_BLOCK_TYPE_ANY       /* end of list */
343   };
344   struct GNUNET_BLOCK_PluginFunctions *api;
345
346   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
347   api->evaluate = &block_plugin_regex_evaluate;
348   api->get_key = &block_plugin_regex_get_key;
349   api->types = types;
350   return api;
351 }
352
353
354 /**
355  * Exit point from the plugin.
356  */
357 void *
358 libgnunet_plugin_block_regex_done (void *cls)
359 {
360   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
361
362   GNUNET_free (api);
363   return NULL;
364 }
365
366 /* end of plugin_block_regex.c */