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