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