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"
29 #include "gnunet_signatures.h"
31 #define DEBUG_FS_BLOCK GNUNET_NO
34 * Number of bits we set per entry in the bloomfilter.
37 #define BLOOMFILTER_K 16
40 * Mingle hash with the mingle_number to produce different bits.
43 mingle_hash (const GNUNET_HashCode * in,
44 int32_t mingle_number,
49 GNUNET_CRYPTO_hash (&mingle_number,
52 GNUNET_CRYPTO_hash_xor (&m, in, hc);
57 * Function called to validate a reply or a request. For
58 * request evaluation, simply pass "NULL" for the reply_block.
59 * Note that it is assumed that the reply has already been
60 * matched to the key (and signatures checked) as it would
61 * be done with the "get_key" function.
64 * @param type block type
65 * @param query original query (hash)
66 * @param bf pointer to bloom filter associated with query; possibly updated (!)
67 * @param bf_mutator mutation value for bf
68 * @param xquery extrended query data (can be NULL, depending on type)
69 * @param xquery_size number of bytes in xquery
70 * @param reply_block response to validate
71 * @param reply_block_size number of bytes in reply block
72 * @return characterization of result
74 static enum GNUNET_BLOCK_EvaluationResult
75 block_plugin_fs_evaluate (void *cls,
76 enum GNUNET_BLOCK_Type type,
77 const GNUNET_HashCode *query,
78 struct GNUNET_CONTAINER_BloomFilter **bf,
82 const void *reply_block,
83 size_t reply_block_size)
85 const struct SBlock *sb;
86 GNUNET_HashCode chash;
87 GNUNET_HashCode mhash;
88 const GNUNET_HashCode *nsid;
93 case GNUNET_BLOCK_TYPE_DBLOCK:
94 case GNUNET_BLOCK_TYPE_IBLOCK:
98 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
100 return GNUNET_BLOCK_EVALUATION_OK_LAST;
101 case GNUNET_BLOCK_TYPE_KBLOCK:
102 case GNUNET_BLOCK_TYPE_NBLOCK:
103 if (xquery_size != 0)
106 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
108 if (reply_block == NULL)
109 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
110 GNUNET_CRYPTO_hash (reply_block,
113 mingle_hash (&chash, bf_mutator, &mhash);
116 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
118 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
122 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
126 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
127 return GNUNET_BLOCK_EVALUATION_OK_MORE;
128 case GNUNET_BLOCK_TYPE_SBLOCK:
129 if (xquery_size != sizeof (GNUNET_HashCode))
132 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
134 if (reply_block == NULL)
135 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
137 if (reply_block_size < sizeof (struct NBlock))
138 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
140 GNUNET_CRYPTO_hash (&sb->subspace,
141 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
143 if (0 != memcmp (nsid,
145 sizeof (GNUNET_HashCode)))
147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
148 _("Reply mismatched in terms of namespace. Discarded.\n"));
149 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
151 GNUNET_CRYPTO_hash (reply_block,
154 mingle_hash (&chash, bf_mutator, &mhash);
157 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
159 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
163 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
167 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
168 return GNUNET_BLOCK_EVALUATION_OK_MORE;
170 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
176 * Function called to obtain the key for a block.
179 * @param type block type
180 * @param block block to get the key for
181 * @param block_size number of bytes in block
182 * @param key set to the key (query) for the given block
183 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
184 * (or if extracting a key from a block of this type does not work)
187 block_plugin_fs_get_key (void *cls,
188 enum GNUNET_BLOCK_Type type,
191 GNUNET_HashCode *key)
193 const struct KBlock *kb;
194 const struct SBlock *sb;
195 const struct NBlock *nb;
199 case GNUNET_BLOCK_TYPE_DBLOCK:
200 case GNUNET_BLOCK_TYPE_IBLOCK:
201 GNUNET_CRYPTO_hash (block, block_size, key);
203 case GNUNET_BLOCK_TYPE_KBLOCK:
204 if (block_size < sizeof (struct KBlock))
207 return GNUNET_SYSERR;
210 if (block_size - sizeof (struct KBlock) !=
211 ntohl (kb->purpose.size)
212 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
213 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
216 return GNUNET_SYSERR;
219 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
225 return GNUNET_SYSERR;
228 GNUNET_CRYPTO_hash (&kb->keyspace,
229 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
232 case GNUNET_BLOCK_TYPE_SBLOCK:
233 if (block_size < sizeof (struct SBlock))
236 return GNUNET_SYSERR;
240 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
243 return GNUNET_SYSERR;
246 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
252 return GNUNET_SYSERR;
255 *key = sb->identifier;
257 case GNUNET_BLOCK_TYPE_NBLOCK:
258 if (block_size < sizeof (struct NBlock))
261 return GNUNET_SYSERR;
264 if (block_size - sizeof (struct NBlock) !=
265 ntohl (nb->ns_purpose.size)
266 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
267 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
270 return GNUNET_SYSERR;
273 ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
276 return GNUNET_SYSERR;
279 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
285 return GNUNET_SYSERR;
288 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
294 return GNUNET_SYSERR;
296 /* FIXME: we used to xor ID with NSID,
299 GNUNET_CRYPTO_hash (&nb->keyspace,
300 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
304 return GNUNET_SYSERR;
310 * Entry point for the plugin.
313 gnunet_plugin_block_fs_init (void *cls)
315 static enum GNUNET_BLOCK_Type types[] =
317 GNUNET_BLOCK_TYPE_DBLOCK,
318 GNUNET_BLOCK_TYPE_IBLOCK,
319 GNUNET_BLOCK_TYPE_KBLOCK,
320 GNUNET_BLOCK_TYPE_SBLOCK,
321 GNUNET_BLOCK_TYPE_NBLOCK,
322 GNUNET_BLOCK_TYPE_ANY /* end of list */
324 struct GNUNET_BLOCK_PluginFunctions *api;
326 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
327 api->evaluate = &block_plugin_fs_evaluate;
328 api->get_key = &block_plugin_fs_get_key;
335 * Exit point from the plugin.
338 gnunet_plugin_block_fs_done (void *cls)
340 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
346 /* end of plugin_block_fs.c */