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 fs/plugin_block_fs.c
23 * @brief blocks used for file-sharing
24 * @author Christian Grothoff
28 #include "gnunet_block_plugin.h"
30 #include "gnunet_signatures.h"
32 #define DEBUG_FS_BLOCK GNUNET_EXTRA_LOGGING
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, enum GNUNET_BLOCK_Type type,
60 const GNUNET_HashCode * query,
61 struct GNUNET_CONTAINER_BloomFilter **bf,
62 int32_t bf_mutator, const void *xquery,
63 size_t xquery_size, const void *reply_block,
64 size_t reply_block_size)
66 const struct SBlock *sb;
67 GNUNET_HashCode chash;
68 GNUNET_HashCode mhash;
69 const GNUNET_HashCode *nsid;
74 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
75 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
79 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
81 if (reply_block == NULL)
82 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
83 return GNUNET_BLOCK_EVALUATION_OK_LAST;
84 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
85 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
89 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
91 if (reply_block == NULL)
92 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
95 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
96 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
99 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
100 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
104 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
106 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
108 return GNUNET_BLOCK_EVALUATION_OK_MORE;
109 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
110 if (xquery_size != sizeof (GNUNET_HashCode))
113 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
115 if (reply_block == NULL)
116 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
118 if (reply_block_size < sizeof (struct SBlock))
121 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
124 GNUNET_CRYPTO_hash (&sb->subspace,
125 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
127 if (0 != memcmp (nsid, &sh, sizeof (GNUNET_HashCode)))
129 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "block-fs",
131 ("Reply mismatched in terms of namespace. Discarded.\n"));
132 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
136 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
137 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
140 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
141 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
145 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
147 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
149 return GNUNET_BLOCK_EVALUATION_OK_MORE;
151 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
157 * Function called to obtain the key for a block.
160 * @param type block type
161 * @param block block to get the key for
162 * @param block_size number of bytes in block
163 * @param key set to the key (query) for the given block
164 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
165 * (or if extracting a key from a block of this type does not work)
168 block_plugin_fs_get_key (void *cls, enum GNUNET_BLOCK_Type type,
169 const void *block, size_t block_size,
170 GNUNET_HashCode * key)
172 const struct KBlock *kb;
173 const struct SBlock *sb;
174 const struct NBlock *nb;
178 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
179 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
180 GNUNET_CRYPTO_hash (block, block_size, key);
182 case GNUNET_BLOCK_TYPE_FS_KBLOCK:
183 if (block_size < sizeof (struct KBlock))
189 if (block_size - sizeof (struct KBlock) !=
190 ntohl (kb->purpose.size) -
191 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) -
192 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))
198 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
199 &kb->purpose, &kb->signature, &kb->keyspace))
205 GNUNET_CRYPTO_hash (&kb->keyspace,
207 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
210 case GNUNET_BLOCK_TYPE_FS_SBLOCK:
211 if (block_size < sizeof (struct SBlock))
218 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
224 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
225 &sb->purpose, &sb->signature, &sb->subspace))
231 *key = sb->identifier;
233 case GNUNET_BLOCK_TYPE_FS_NBLOCK:
234 if (block_size < sizeof (struct NBlock))
240 if (block_size - sizeof (struct NBlock) !=
241 ntohl (nb->ns_purpose.size) -
242 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) -
243 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))
249 ntohl (nb->ksk_purpose.size) +
250 sizeof (struct GNUNET_CRYPTO_RsaSignature))
256 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
257 &nb->ksk_purpose, &nb->ksk_signature,
264 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
265 &nb->ns_purpose, &nb->ns_signature,
271 /* FIXME: we used to xor ID with NSID,
274 GNUNET_CRYPTO_hash (&nb->keyspace,
276 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
280 return GNUNET_SYSERR;
286 * Entry point for the plugin.
289 libgnunet_plugin_block_fs_init (void *cls)
291 static enum GNUNET_BLOCK_Type types[] =
293 GNUNET_BLOCK_TYPE_FS_DBLOCK,
294 GNUNET_BLOCK_TYPE_FS_IBLOCK,
295 GNUNET_BLOCK_TYPE_FS_KBLOCK,
296 GNUNET_BLOCK_TYPE_FS_SBLOCK,
297 GNUNET_BLOCK_TYPE_FS_NBLOCK,
298 GNUNET_BLOCK_TYPE_ANY /* end of list */
300 struct GNUNET_BLOCK_PluginFunctions *api;
302 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
303 api->evaluate = &block_plugin_fs_evaluate;
304 api->get_key = &block_plugin_fs_get_key;
311 * Exit point from the plugin.
314 libgnunet_plugin_block_fs_done (void *cls)
316 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
322 /* end of plugin_block_fs.c */