2 This file is part of GNUnet
3 Copyright (C) 2010, 2013 GNUnet e.V.
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.
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.
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.
22 * @file fs/plugin_block_fs.c
23 * @brief blocks used for file-sharing
24 * @author Christian Grothoff
27 #include "gnunet_block_plugin.h"
28 #include "gnunet_fs_service.h"
30 #include "gnunet_signatures.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_block_group_lib.h"
36 * Number of bits we set per entry in the bloomfilter.
39 #define BLOOMFILTER_K 16
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).
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.
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.
56 compute_bloomfilter_size (unsigned int entry_count)
59 unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
60 uint16_t max = 1 << 15;
62 if (entry_count > max)
65 while ((size < max) && (size < ideal))
74 * Create a new block group.
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)
85 static struct GNUNET_BLOCK_Group *
86 block_plugin_fs_create_group (void *cls,
87 enum GNUNET_BLOCK_Type type,
98 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
100 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
102 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
103 guard = va_arg (va, const char *);
104 if (0 != memcmp (guard,
106 strlen ("fs-seen-set-size")))
108 /* va-args invalid! bad bug, complain! */
114 size = compute_bloomfilter_size (va_arg (va, unsigned int));
117 size = raw_data_size; /* not for us to determine, use what we got! */
118 return GNUNET_BLOCK_GROUP_bf_create (cls,
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.
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
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,
158 const void *reply_block,
159 size_t reply_block_size)
161 const struct UBlock *ub;
162 struct GNUNET_HashCode hc;
163 struct GNUNET_HashCode chash;
167 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
168 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
169 if (0 != xquery_size)
172 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
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)
181 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
183 if (NULL == reply_block)
184 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
186 if (reply_block_size < sizeof (struct UBlock))
189 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
192 GNUNET_CRYPTO_hash (&ub->verification_key,
193 sizeof (ub->verification_key),
195 if (0 != memcmp (&hc,
197 sizeof (struct GNUNET_HashCode)))
200 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
202 if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
205 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
207 if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
209 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
212 &ub->verification_key)) )
215 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
217 GNUNET_CRYPTO_hash (reply_block,
221 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
223 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
224 return GNUNET_BLOCK_EVALUATION_OK_MORE;
226 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
232 * Function called to obtain the key for a block.
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)
243 block_plugin_fs_get_key (void *cls,
244 enum GNUNET_BLOCK_Type type,
247 struct GNUNET_HashCode *key)
249 const struct UBlock *ub;
253 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
254 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
255 GNUNET_CRYPTO_hash (block, block_size, key);
257 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
258 if (block_size < sizeof (struct UBlock))
261 return GNUNET_SYSERR;
264 GNUNET_CRYPTO_hash (&ub->verification_key,
265 sizeof (ub->verification_key),
270 return GNUNET_SYSERR;
276 * Entry point for the plugin.
279 libgnunet_plugin_block_fs_init (void *cls)
281 static enum GNUNET_BLOCK_Type types[] =
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 */
288 struct GNUNET_BLOCK_PluginFunctions *api;
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;
300 * Exit point from the plugin.
303 libgnunet_plugin_block_fs_done (void *cls)
305 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
311 /* end of plugin_block_fs.c */