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.
17 * @file fs/plugin_block_fs.c
18 * @brief blocks used for file-sharing
19 * @author Christian Grothoff
22 #include "gnunet_block_plugin.h"
23 #include "gnunet_fs_service.h"
25 #include "gnunet_signatures.h"
26 #include "gnunet_block_group_lib.h"
30 * Number of bits we set per entry in the bloomfilter.
33 #define BLOOMFILTER_K 16
37 * Create a new block group.
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)
48 static struct GNUNET_BLOCK_Group *
49 block_plugin_fs_create_group (void *cls,
50 enum GNUNET_BLOCK_Type type,
61 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
62 GNUNET_break (NULL == va_arg (va, const char *));
64 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
65 GNUNET_break (NULL == va_arg (va, const char *));
67 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
68 guard = va_arg (va, const char *);
69 if (0 == strcmp (guard,
72 size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
75 else if (0 == strcmp (guard,
78 size = va_arg (va, unsigned int);
82 /* va-args invalid! bad bug, complain! */
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,
97 GNUNET_break (NULL == va_arg (va, const char *));
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.
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
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,
132 const void *reply_block,
133 size_t reply_block_size)
135 const struct UBlock *ub;
136 struct GNUNET_HashCode hc;
137 struct GNUNET_HashCode chash;
141 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
142 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
143 if (0 != xquery_size)
146 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
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)
155 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
157 if (NULL == reply_block)
158 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
160 if (reply_block_size < sizeof (struct UBlock))
163 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
166 GNUNET_CRYPTO_hash (&ub->verification_key,
167 sizeof (ub->verification_key),
169 if (0 != memcmp (&hc,
171 sizeof (struct GNUNET_HashCode)))
174 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
176 if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
179 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
181 if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
183 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
186 &ub->verification_key)) )
189 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
191 GNUNET_CRYPTO_hash (reply_block,
195 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
197 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
198 return GNUNET_BLOCK_EVALUATION_OK_MORE;
200 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
206 * Function called to obtain the key for a block.
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)
217 block_plugin_fs_get_key (void *cls,
218 enum GNUNET_BLOCK_Type type,
221 struct GNUNET_HashCode *key)
223 const struct UBlock *ub;
227 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
228 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
229 GNUNET_CRYPTO_hash (block, block_size, key);
231 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
232 if (block_size < sizeof (struct UBlock))
235 return GNUNET_SYSERR;
238 GNUNET_CRYPTO_hash (&ub->verification_key,
239 sizeof (ub->verification_key),
244 return GNUNET_SYSERR;
250 * Entry point for the plugin.
253 libgnunet_plugin_block_fs_init (void *cls)
255 static enum GNUNET_BLOCK_Type types[] =
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 */
262 struct GNUNET_BLOCK_PluginFunctions *api;
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;
274 * Exit point from the plugin.
277 libgnunet_plugin_block_fs_done (void *cls)
279 struct GNUNET_BLOCK_PluginFunctions *api = cls;
285 /* end of plugin_block_fs.c */