2 This file is part of GNUnet
3 (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file block/plugin_block_fs.c
23 * @brief blocks used for file-sharing
24 * @author Christian Grothoff
28 #include "plugin_block.h"
30 #include "gnunet_signatures.h"
32 #define DEBUG_FS_BLOCK GNUNET_NO
35 * Number of bits we set per entry in the bloomfilter.
38 #define BLOOMFILTER_K 16
41 * Function called to validate a reply or a request. For
42 * request evaluation, simply pass "NULL" for the reply_block.
43 * Note that it is assumed that the reply has already been
44 * matched to the key (and signatures checked) as it would
45 * be done with the "get_key" function.
48 * @param type block type
49 * @param query original query (hash)
50 * @param bf pointer to bloom filter associated with query; possibly updated (!)
51 * @param bf_mutator mutation value for bf
52 * @param xquery extrended query data (can be NULL, depending on type)
53 * @param xquery_size number of bytes in xquery
54 * @param reply_block response to validate
55 * @param reply_block_size number of bytes in reply block
56 * @return characterization of result
58 static enum GNUNET_BLOCK_EvaluationResult
59 block_plugin_fs_evaluate (void *cls,
60 enum GNUNET_BLOCK_Type type,
61 const GNUNET_HashCode *query,
62 struct GNUNET_CONTAINER_BloomFilter **bf,
66 const void *reply_block,
67 size_t reply_block_size)
69 const struct SBlock *sb;
70 GNUNET_HashCode chash;
71 GNUNET_HashCode mhash;
72 const GNUNET_HashCode *nsid;
77 case GNUNET_BLOCK_TYPE_DBLOCK:
78 case GNUNET_BLOCK_TYPE_IBLOCK:
82 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
84 if (reply_block == NULL)
85 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
86 return GNUNET_BLOCK_EVALUATION_OK_LAST;
87 case GNUNET_BLOCK_TYPE_KBLOCK:
88 case GNUNET_BLOCK_TYPE_NBLOCK:
92 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
94 if (reply_block == NULL)
95 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
96 GNUNET_CRYPTO_hash (reply_block,
99 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
102 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
104 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
108 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
112 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
113 return GNUNET_BLOCK_EVALUATION_OK_MORE;
114 case GNUNET_BLOCK_TYPE_SBLOCK:
115 if (xquery_size != sizeof (GNUNET_HashCode))
118 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
120 if (reply_block == NULL)
121 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
123 if (reply_block_size < sizeof (struct SBlock))
126 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
129 GNUNET_CRYPTO_hash (&sb->subspace,
130 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
132 if (0 != memcmp (nsid,
134 sizeof (GNUNET_HashCode)))
136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
137 _("Reply mismatched in terms of namespace. Discarded.\n"));
138 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
140 GNUNET_CRYPTO_hash (reply_block,
143 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
146 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
148 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
152 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
156 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
157 return GNUNET_BLOCK_EVALUATION_OK_MORE;
159 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
165 * Function called to obtain the key for a block.
168 * @param type block type
169 * @param block block to get the key for
170 * @param block_size number of bytes in block
171 * @param key set to the key (query) for the given block
172 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
173 * (or if extracting a key from a block of this type does not work)
176 block_plugin_fs_get_key (void *cls,
177 enum GNUNET_BLOCK_Type type,
180 GNUNET_HashCode *key)
182 const struct KBlock *kb;
183 const struct SBlock *sb;
184 const struct NBlock *nb;
188 case GNUNET_BLOCK_TYPE_DBLOCK:
189 case GNUNET_BLOCK_TYPE_IBLOCK:
190 GNUNET_CRYPTO_hash (block, block_size, key);
192 case GNUNET_BLOCK_TYPE_KBLOCK:
193 if (block_size < sizeof (struct KBlock))
196 return GNUNET_SYSERR;
199 if (block_size - sizeof (struct KBlock) !=
200 ntohl (kb->purpose.size)
201 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
202 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
205 return GNUNET_SYSERR;
208 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
214 return GNUNET_SYSERR;
217 GNUNET_CRYPTO_hash (&kb->keyspace,
218 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
221 case GNUNET_BLOCK_TYPE_SBLOCK:
222 if (block_size < sizeof (struct SBlock))
225 return GNUNET_SYSERR;
229 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
232 return GNUNET_SYSERR;
235 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
241 return GNUNET_SYSERR;
244 *key = sb->identifier;
246 case GNUNET_BLOCK_TYPE_NBLOCK:
247 if (block_size < sizeof (struct NBlock))
250 return GNUNET_SYSERR;
253 if (block_size - sizeof (struct NBlock) !=
254 ntohl (nb->ns_purpose.size)
255 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
256 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
259 return GNUNET_SYSERR;
262 ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
265 return GNUNET_SYSERR;
268 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
274 return GNUNET_SYSERR;
277 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
283 return GNUNET_SYSERR;
285 /* FIXME: we used to xor ID with NSID,
288 GNUNET_CRYPTO_hash (&nb->keyspace,
289 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
293 return GNUNET_SYSERR;
299 * Entry point for the plugin.
302 libgnunet_plugin_block_fs_init (void *cls)
304 static enum GNUNET_BLOCK_Type types[] =
306 GNUNET_BLOCK_TYPE_DBLOCK,
307 GNUNET_BLOCK_TYPE_IBLOCK,
308 GNUNET_BLOCK_TYPE_KBLOCK,
309 GNUNET_BLOCK_TYPE_SBLOCK,
310 GNUNET_BLOCK_TYPE_NBLOCK,
311 GNUNET_BLOCK_TYPE_ANY /* end of list */
313 struct GNUNET_BLOCK_PluginFunctions *api;
315 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
316 api->evaluate = &block_plugin_fs_evaluate;
317 api->get_key = &block_plugin_fs_get_key;
324 * Exit point from the plugin.
327 libgnunet_plugin_block_fs_done (void *cls)
329 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
335 /* end of plugin_block_fs.c */