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 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.
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.
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/>.
20 * @file fs/plugin_block_fs.c
21 * @brief blocks used for file-sharing
22 * @author Christian Grothoff
25 #include "gnunet_block_plugin.h"
26 #include "gnunet_fs_service.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_block_group_lib.h"
33 * Number of bits we set per entry in the bloomfilter.
36 #define BLOOMFILTER_K 16
40 * Create a new block group.
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)
51 static struct GNUNET_BLOCK_Group *
52 block_plugin_fs_create_group (void *cls,
53 enum GNUNET_BLOCK_Type type,
64 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
65 GNUNET_break (NULL == va_arg (va, const char *));
67 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
68 GNUNET_break (NULL == va_arg (va, const char *));
70 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
71 guard = va_arg (va, const char *);
72 if (0 == strcmp (guard,
75 size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
78 else if (0 == strcmp (guard,
81 size = va_arg (va, unsigned int);
85 /* va-args invalid! bad bug, complain! */
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,
100 GNUNET_break (NULL == va_arg (va, const char *));
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.
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
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,
135 const void *reply_block,
136 size_t reply_block_size)
138 const struct UBlock *ub;
139 struct GNUNET_HashCode hc;
140 struct GNUNET_HashCode chash;
144 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
145 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
146 if (0 != xquery_size)
149 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
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)
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
160 if (NULL == reply_block)
161 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
163 if (reply_block_size < sizeof (struct UBlock))
166 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
169 GNUNET_CRYPTO_hash (&ub->verification_key,
170 sizeof (ub->verification_key),
172 if (0 != memcmp (&hc,
174 sizeof (struct GNUNET_HashCode)))
177 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
179 if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
182 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
184 if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
186 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
189 &ub->verification_key)) )
192 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
194 GNUNET_CRYPTO_hash (reply_block,
198 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
200 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
201 return GNUNET_BLOCK_EVALUATION_OK_MORE;
203 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
209 * Function called to obtain the key for a block.
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)
220 block_plugin_fs_get_key (void *cls,
221 enum GNUNET_BLOCK_Type type,
224 struct GNUNET_HashCode *key)
226 const struct UBlock *ub;
230 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
231 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
232 GNUNET_CRYPTO_hash (block, block_size, key);
234 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
235 if (block_size < sizeof (struct UBlock))
238 return GNUNET_SYSERR;
241 GNUNET_CRYPTO_hash (&ub->verification_key,
242 sizeof (ub->verification_key),
247 return GNUNET_SYSERR;
253 * Entry point for the plugin.
256 libgnunet_plugin_block_fs_init (void *cls)
258 static enum GNUNET_BLOCK_Type types[] =
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 */
265 struct GNUNET_BLOCK_PluginFunctions *api;
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;
277 * Exit point from the plugin.
280 libgnunet_plugin_block_fs_done (void *cls)
282 struct GNUNET_BLOCK_PluginFunctions *api = cls;
288 /* end of plugin_block_fs.c */