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