-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / fs / plugin_block_fs.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010, 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 fs/plugin_block_fs.c
21  * @brief blocks used for file-sharing
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_block_plugin.h"
26 #include "gnunet_fs_service.h"
27 #include "block_fs.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_block_group_lib.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  * Create a new block group.
41  *
42  * @param ctx block context in which the block group is created
43  * @param type type of the block for which we are creating the group
44  * @param nonce random value used to seed the group creation
45  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
46  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
47  * @param va variable arguments specific to @a type
48  * @return block group handle, NULL if block groups are not supported
49  *         by this @a type of block (this is not an error)
50  */
51 static struct GNUNET_BLOCK_Group *
52 block_plugin_fs_create_group (void *cls,
53                               enum GNUNET_BLOCK_Type type,
54                               uint32_t nonce,
55                               const void *raw_data,
56                               size_t raw_data_size,
57                               va_list va)
58 {
59   unsigned int size;
60   const char *guard;
61
62   switch (type)
63   {
64   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
65     GNUNET_break (NULL == va_arg (va, const char *));
66     return NULL;
67   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
68     GNUNET_break (NULL == va_arg (va, const char *));
69     return NULL;
70   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
71     guard = va_arg (va, const char *);
72     if (0 == strcmp (guard,
73                      "seen-set-size"))
74     {
75       size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
76                                                           BLOOMFILTER_K);
77     }
78     else if (0 == strcmp (guard,
79                           "filter-size"))
80     {
81       size = va_arg (va, unsigned int);
82     }
83     else
84     {
85       /* va-args invalid! bad bug, complain! */
86       GNUNET_break (0);
87       size = 8;
88     }
89     if (0 == size)
90       size = raw_data_size; /* not for us to determine, use what we got! */
91     GNUNET_break (NULL == va_arg (va, const char *));
92     return GNUNET_BLOCK_GROUP_bf_create (cls,
93                                          size,
94                                          BLOOMFILTER_K,
95                                          type,
96                                          nonce,
97                                          raw_data,
98                                          raw_data_size);
99   default:
100     GNUNET_break (NULL == va_arg (va, const char *));
101     GNUNET_break (0);
102     return NULL;
103   }
104 }
105
106
107 /**
108  * Function called to validate a reply or a request.  For
109  * request evaluation, simply pass "NULL" for the reply_block.
110  * Note that it is assumed that the reply has already been
111  * matched to the key (and signatures checked) as it would
112  * be done with the #GNUNET_BLOCK_get_key() function.
113  *
114  * @param cls closure
115  * @param ctx block context
116  * @param type block type
117  * @param bg group to use for evaluation
118  * @param eo control flags
119  * @param query original query (hash)
120  * @param xquery extrended query data (can be NULL, depending on type)
121  * @param xquery_size number of bytes in @a xquery
122  * @param reply_block response to validate
123  * @param reply_block_size number of bytes in @a reply_block
124  * @return characterization of result
125  */
126 static enum GNUNET_BLOCK_EvaluationResult
127 block_plugin_fs_evaluate (void *cls,
128                           struct GNUNET_BLOCK_Context *ctx,
129                           enum GNUNET_BLOCK_Type type,
130                           struct GNUNET_BLOCK_Group *bg,
131                           enum GNUNET_BLOCK_EvaluationOptions eo,
132                           const struct GNUNET_HashCode *query,
133                           const void *xquery,
134                           size_t xquery_size,
135                           const void *reply_block,
136                           size_t reply_block_size)
137 {
138   const struct UBlock *ub;
139   struct GNUNET_HashCode hc;
140   struct GNUNET_HashCode chash;
141
142   switch (type)
143   {
144   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
145   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
146     if (0 != xquery_size)
147     {
148       GNUNET_break_op (0);
149       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
150     }
151     if (NULL == reply_block)
152       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
153     return GNUNET_BLOCK_EVALUATION_OK_LAST;
154   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
155     if (0 != xquery_size)
156     {
157       GNUNET_break_op (0);
158       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159     }
160     if (NULL == reply_block)
161       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
162
163     if (reply_block_size < sizeof (struct UBlock))
164     {
165       GNUNET_break_op (0);
166       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
167     }
168     ub = reply_block;
169     GNUNET_CRYPTO_hash (&ub->verification_key,
170                         sizeof (ub->verification_key),
171                         &hc);
172     if (0 != memcmp (&hc,
173                      query,
174                      sizeof (struct GNUNET_HashCode)))
175     {
176       GNUNET_break_op (0);
177       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
178     }
179     if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
180     {
181       GNUNET_break_op (0);
182       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
183     }
184     if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
185          (GNUNET_OK !=
186           GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
187                                       &ub->purpose,
188                                       &ub->signature,
189                                       &ub->verification_key)) )
190     {
191       GNUNET_break_op (0);
192       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
193     }
194     GNUNET_CRYPTO_hash (reply_block,
195                         reply_block_size,
196                         &chash);
197     if (GNUNET_YES ==
198         GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
199                                             &chash))
200       return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
201     return GNUNET_BLOCK_EVALUATION_OK_MORE;
202   default:
203     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
204   }
205 }
206
207
208 /**
209  * Function called to obtain the key for a block.
210  *
211  * @param cls closure
212  * @param type block type
213  * @param block block to get the key for
214  * @param block_size number of bytes in @a block
215  * @param key set to the key (query) for the given block
216  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
217  *         (or if extracting a key from a block of this type does not work)
218  */
219 static int
220 block_plugin_fs_get_key (void *cls,
221                          enum GNUNET_BLOCK_Type type,
222                          const void *block,
223                          size_t block_size,
224                          struct GNUNET_HashCode *key)
225 {
226   const struct UBlock *ub;
227
228   switch (type)
229   {
230   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
231   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
232     GNUNET_CRYPTO_hash (block, block_size, key);
233     return GNUNET_OK;
234   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
235     if (block_size < sizeof (struct UBlock))
236     {
237       GNUNET_break (0);
238       return GNUNET_SYSERR;
239     }
240     ub = block;
241     GNUNET_CRYPTO_hash (&ub->verification_key,
242                         sizeof (ub->verification_key),
243                         key);
244     return GNUNET_OK;
245   default:
246     GNUNET_break (0);
247     return GNUNET_SYSERR;
248   }
249 }
250
251
252 /**
253  * Entry point for the plugin.
254  */
255 void *
256 libgnunet_plugin_block_fs_init (void *cls)
257 {
258   static enum GNUNET_BLOCK_Type types[] =
259   {
260     GNUNET_BLOCK_TYPE_FS_DBLOCK,
261     GNUNET_BLOCK_TYPE_FS_IBLOCK,
262     GNUNET_BLOCK_TYPE_FS_UBLOCK,
263     GNUNET_BLOCK_TYPE_ANY       /* end of list */
264   };
265   struct GNUNET_BLOCK_PluginFunctions *api;
266
267   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
268   api->evaluate = &block_plugin_fs_evaluate;
269   api->get_key = &block_plugin_fs_get_key;
270   api->create_group = &block_plugin_fs_create_group;
271   api->types = types;
272   return api;
273 }
274
275
276 /**
277  * Exit point from the plugin.
278  */
279 void *
280 libgnunet_plugin_block_fs_done (void *cls)
281 {
282   struct GNUNET_BLOCK_PluginFunctions *api = cls;
283
284   GNUNET_free (api);
285   return NULL;
286 }
287
288 /* end of plugin_block_fs.c */