- implement mesh regex block plugin checks
[oweals/gnunet.git] / src / mesh / plugin_block_mesh.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 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 mesh/plugin_block_mesh.c
23  * @brief blocks used for mesh peer discovery
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_block_plugin.h"
29 #include "block_mesh.h"
30
31 /**
32  * Number of bits we set per entry in the bloomfilter.
33  * Do not change!
34  */
35 #define BLOOMFILTER_K 16
36
37
38 /**
39  * Check if the regex block is well formed, including all edges
40  * 
41  * @param block The start of the block.
42  * @param size The size of the block.
43  * 
44  * @return GNUNET_OK in case it's fine, GNUNET_SYSERR otherwise.
45  */
46 static int
47 check_mesh_regex_block (const struct MeshRegexBlock *block, size_t size)
48 {
49   struct MeshRegexEdge *edge;
50   unsigned int n;
51   unsigned int n_token;
52   unsigned int i;
53   size_t offset;
54   char *aux;
55
56   offset = sizeof (struct MeshRegexBlock);
57   if (offset > size) // Is it safe to access the regex block?
58     return GNUNET_SYSERR;
59   n = ntohl (block->n_proof);
60   offset =+ n;
61   if (offset > size) // Is it safe to access the regex proof?
62     return GNUNET_SYSERR;
63   aux = (char *) &block[1];  // Skip regex block
64   aux = &aux[n];             // Skip regex proof
65   n = ntohl (block->n_edges);
66   for (i = 0; i < n; n++) // aux always points at the end of the previous block
67   {
68     offset += sizeof (struct MeshRegexEdge);
69     if (offset > size) // Is it safe to access the next edge block?
70       return GNUNET_SYSERR;
71     edge = (struct MeshRegexEdge *) aux;
72     n_token = ntohl (edge->n_token);
73     offset += n_token;
74     if (offset > size) // Is it safe to access the edge token?
75       return GNUNET_SYSERR;
76     aux = (char *) &edge[1]; // Skip edge block
77     aux = &aux[n_token];     // Skip edge token
78   }
79   // The total size should be exactly the size of (regex + all edges) blocks
80   return (offset == size) ? GNUNET_OK : GNUNET_SYSERR;
81 }
82
83
84 /**
85  * Function called to validate a reply or a request.  For
86  * request evaluation, simply pass "NULL" for the reply_block.
87  * Note that it is assumed that the reply has already been
88  * matched to the key (and signatures checked) as it would
89  * be done with the "get_key" function.
90  *
91  * @param cls closure
92  * @param type block type
93  * @param query original query (hash)
94  * @param bf pointer to bloom filter associated with query; possibly updated (!)
95  * @param bf_mutator mutation value for bf
96  * @param xquery extrended query data (can be NULL, depending on type)
97  * @param xquery_size number of bytes in xquery
98  * @param reply_block response to validate
99  * @param reply_block_size number of bytes in reply block
100  * @return characterization of result
101  */
102 static enum GNUNET_BLOCK_EvaluationResult
103 block_plugin_mesh_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
104                             const struct GNUNET_HashCode * query,
105                             struct GNUNET_CONTAINER_BloomFilter **bf,
106                             int32_t bf_mutator, const void *xquery,
107                             size_t xquery_size, const void *reply_block,
108                             size_t reply_block_size)
109 {
110   struct GNUNET_HashCode chash;
111   struct GNUNET_HashCode mhash;
112
113   switch (type)
114   {
115   case GNUNET_BLOCK_TYPE_MESH_PEER:
116     if (0 != xquery_size)
117     {
118       GNUNET_break_op (0);
119       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
120     }
121     if (NULL == reply_block)
122       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
123     if (sizeof (struct PBlock) != reply_block_size)  
124       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;  
125     return GNUNET_BLOCK_EVALUATION_OK_LAST;
126   case GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE:
127     /* FIXME: have an xquery? not sure */
128     if (0 != xquery_size)
129     {
130       GNUNET_break_op (0);
131       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
132     }
133     if (NULL == reply_block)
134       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
135     if (sizeof (struct PBlock) != reply_block_size)  
136       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;  
137     if (NULL != bf)
138     {
139       GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
140       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
141       if (NULL != *bf)
142       {
143         if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
144           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
145       }
146       else
147       {
148         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
149       }
150       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
151     }
152     return GNUNET_BLOCK_EVALUATION_OK_MORE;
153   case GNUNET_BLOCK_TYPE_MESH_REGEX:
154     if (0 != xquery_size)
155     {
156       GNUNET_break_op (0);
157       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
158     }
159     if (NULL == reply_block)
160       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
161     if (GNUNET_OK != check_mesh_regex_block (reply_block, reply_block_size))
162       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
163     if (NULL != bf)
164     {
165       GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
166       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
167       if (NULL != *bf)
168       {
169         if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
170           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
171       }
172       else
173       {
174         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
175       }
176       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
177     }
178     return GNUNET_BLOCK_EVALUATION_OK_MORE;
179   case GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT:
180     if (0 != xquery_size)
181     {
182       GNUNET_break_op (0);
183       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
184     }
185     if (NULL == reply_block)
186       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
187     if (sizeof (struct MeshRegexAccept) != reply_block_size)  
188       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
189     if (NULL != bf)
190     {
191       GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
192       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
193       if (NULL != *bf)
194       {
195         if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
196           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
197       }
198       else
199       {
200         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
201       }
202       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
203     }
204     return GNUNET_BLOCK_EVALUATION_OK_MORE;
205   default:
206     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
207   }
208 }
209
210
211 /**
212  * Function called to obtain the key for a block.
213  *
214  * @param cls closure
215  * @param type block type
216  * @param block block to get the key for
217  * @param block_size number of bytes in block
218  * @param key set to the key (query) for the given block
219  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
220  *         (or if extracting a key from a block of this type does not work)
221  */
222 static int
223 block_plugin_mesh_get_key (void *cls, enum GNUNET_BLOCK_Type type,
224                            const void *block, size_t block_size,
225                            struct GNUNET_HashCode * key)
226 {
227   const struct PBlock *pb;
228   pb = block;
229
230   switch (type)
231   {
232   case GNUNET_BLOCK_TYPE_MESH_PEER:
233     if (sizeof (struct PBlock) != block_size)
234       return GNUNET_SYSERR;
235     *key = pb->id.hashPubKey;
236     return GNUNET_OK;
237   case GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE:
238     GNUNET_CRYPTO_hash (&pb->type, sizeof(GNUNET_MESH_ApplicationType), key);
239     return GNUNET_OK;
240   default:
241     return GNUNET_SYSERR;
242   }
243 }
244
245
246 /**
247  * Entry point for the plugin.
248  */
249 void *
250 libgnunet_plugin_block_mesh_init (void *cls)
251 {
252   static enum GNUNET_BLOCK_Type types[] =
253   {
254     GNUNET_BLOCK_TYPE_MESH_PEER,
255     GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
256     GNUNET_BLOCK_TYPE_ANY       /* end of list */
257   };
258   struct GNUNET_BLOCK_PluginFunctions *api;
259
260   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
261   api->evaluate = &block_plugin_mesh_evaluate;
262   api->get_key = &block_plugin_mesh_get_key;
263   api->types = types;
264   return api;
265 }
266
267
268 /**
269  * Exit point from the plugin.
270  */
271 void *
272 libgnunet_plugin_block_mesh_done (void *cls)
273 {
274   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
275
276   GNUNET_free (api);
277   return NULL;
278 }
279
280 /* end of plugin_block_mesh.c */