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 * Mingle hash with the mingle_number to produce different bits.
44 mingle_hash (const GNUNET_HashCode * in,
45 int32_t mingle_number,
50 GNUNET_CRYPTO_hash (&mingle_number,
53 GNUNET_CRYPTO_hash_xor (&m, in, hc);
58 * Function called to validate a reply or a request. For
59 * request evaluation, simply pass "NULL" for the reply_block.
60 * Note that it is assumed that the reply has already been
61 * matched to the key (and signatures checked) as it would
62 * be done with the "get_key" function.
65 * @param type block type
66 * @param query original query (hash)
67 * @param bf pointer to bloom filter associated with query; possibly updated (!)
68 * @param bf_mutator mutation value for bf
69 * @param xquery extrended query data (can be NULL, depending on type)
70 * @param xquery_size number of bytes in xquery
71 * @param reply_block response to validate
72 * @param reply_block_size number of bytes in reply block
73 * @return characterization of result
75 static enum GNUNET_BLOCK_EvaluationResult
76 block_plugin_fs_evaluate (void *cls,
77 enum GNUNET_BLOCK_Type type,
78 const GNUNET_HashCode *query,
79 struct GNUNET_CONTAINER_BloomFilter **bf,
83 const void *reply_block,
84 size_t reply_block_size)
86 const struct SBlock *sb;
87 GNUNET_HashCode chash;
88 GNUNET_HashCode mhash;
89 const GNUNET_HashCode *nsid;
94 case GNUNET_BLOCK_TYPE_DBLOCK:
95 case GNUNET_BLOCK_TYPE_IBLOCK:
99 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
101 if (reply_block == NULL)
102 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
103 return GNUNET_BLOCK_EVALUATION_OK_LAST;
104 case GNUNET_BLOCK_TYPE_KBLOCK:
105 case GNUNET_BLOCK_TYPE_NBLOCK:
106 if (xquery_size != 0)
109 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
111 if (reply_block == NULL)
112 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
113 GNUNET_CRYPTO_hash (reply_block,
116 mingle_hash (&chash, bf_mutator, &mhash);
119 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
121 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
125 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
129 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
130 return GNUNET_BLOCK_EVALUATION_OK_MORE;
131 case GNUNET_BLOCK_TYPE_SBLOCK:
132 if (xquery_size != sizeof (GNUNET_HashCode))
135 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
137 if (reply_block == NULL)
138 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
140 if (reply_block_size < sizeof (struct NBlock))
141 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
143 GNUNET_CRYPTO_hash (&sb->subspace,
144 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
146 if (0 != memcmp (nsid,
148 sizeof (GNUNET_HashCode)))
150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
151 _("Reply mismatched in terms of namespace. Discarded.\n"));
152 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
154 GNUNET_CRYPTO_hash (reply_block,
157 mingle_hash (&chash, bf_mutator, &mhash);
160 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
162 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
166 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
170 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
171 return GNUNET_BLOCK_EVALUATION_OK_MORE;
173 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
179 * Function called to obtain the key for a block.
182 * @param type block type
183 * @param block block to get the key for
184 * @param block_size number of bytes in block
185 * @param key set to the key (query) for the given block
186 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
187 * (or if extracting a key from a block of this type does not work)
190 block_plugin_fs_get_key (void *cls,
191 enum GNUNET_BLOCK_Type type,
194 GNUNET_HashCode *key)
196 const struct KBlock *kb;
197 const struct SBlock *sb;
198 const struct NBlock *nb;
202 case GNUNET_BLOCK_TYPE_DBLOCK:
203 case GNUNET_BLOCK_TYPE_IBLOCK:
204 GNUNET_CRYPTO_hash (block, block_size, key);
206 case GNUNET_BLOCK_TYPE_KBLOCK:
207 if (block_size < sizeof (struct KBlock))
210 return GNUNET_SYSERR;
213 if (block_size - sizeof (struct KBlock) !=
214 ntohl (kb->purpose.size)
215 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
216 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
219 return GNUNET_SYSERR;
222 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
228 return GNUNET_SYSERR;
231 GNUNET_CRYPTO_hash (&kb->keyspace,
232 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
235 case GNUNET_BLOCK_TYPE_SBLOCK:
236 if (block_size < sizeof (struct SBlock))
239 return GNUNET_SYSERR;
243 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
246 return GNUNET_SYSERR;
249 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
255 return GNUNET_SYSERR;
258 *key = sb->identifier;
260 case GNUNET_BLOCK_TYPE_NBLOCK:
261 if (block_size < sizeof (struct NBlock))
264 return GNUNET_SYSERR;
267 if (block_size - sizeof (struct NBlock) !=
268 ntohl (nb->ns_purpose.size)
269 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
270 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
273 return GNUNET_SYSERR;
276 ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
279 return GNUNET_SYSERR;
282 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
288 return GNUNET_SYSERR;
291 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
297 return GNUNET_SYSERR;
299 /* FIXME: we used to xor ID with NSID,
302 GNUNET_CRYPTO_hash (&nb->keyspace,
303 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
307 return GNUNET_SYSERR;
313 * Entry point for the plugin.
316 libgnunet_plugin_block_fs_init (void *cls)
318 static enum GNUNET_BLOCK_Type types[] =
320 GNUNET_BLOCK_TYPE_DBLOCK,
321 GNUNET_BLOCK_TYPE_IBLOCK,
322 GNUNET_BLOCK_TYPE_KBLOCK,
323 GNUNET_BLOCK_TYPE_SBLOCK,
324 GNUNET_BLOCK_TYPE_NBLOCK,
325 GNUNET_BLOCK_TYPE_ANY /* end of list */
327 struct GNUNET_BLOCK_PluginFunctions *api;
329 api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
330 api->evaluate = &block_plugin_fs_evaluate;
331 api->get_key = &block_plugin_fs_get_key;
338 * Exit point from the plugin.
341 libgnunet_plugin_block_fs_done (void *cls)
343 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
349 /* end of plugin_block_fs.c */