2 This file is part of GNUnet
3 (C) 2010, 2013 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"
29 #include "gnunet_fs_service.h"
31 #include "gnunet_signatures.h"
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 struct 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 UBlock *ub;
67 struct GNUNET_HashCode hc;
68 struct GNUNET_HashCode chash;
69 struct GNUNET_HashCode mhash;
73 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
74 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
78 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
80 if (NULL == reply_block)
81 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
82 return GNUNET_BLOCK_EVALUATION_OK_LAST;
83 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
87 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
89 if (NULL == reply_block)
90 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
92 if (reply_block_size < sizeof (struct UBlock))
95 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
98 GNUNET_CRYPTO_hash (&ub->verification_key,
99 sizeof (ub->verification_key),
101 if (0 != memcmp (&hc,
103 sizeof (struct GNUNET_HashCode)))
106 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
108 if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
111 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
114 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
117 &ub->verification_key))
120 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
124 GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
125 GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
128 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
129 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
133 *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
135 GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
137 return GNUNET_BLOCK_EVALUATION_OK_MORE;
139 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
145 * Function called to obtain the key for a block.
148 * @param type block type
149 * @param block block to get the key for
150 * @param block_size number of bytes in block
151 * @param key set to the key (query) for the given block
152 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
153 * (or if extracting a key from a block of this type does not work)
156 block_plugin_fs_get_key (void *cls, enum GNUNET_BLOCK_Type type,
157 const void *block, size_t block_size,
158 struct GNUNET_HashCode *key)
160 const struct UBlock *ub;
164 case GNUNET_BLOCK_TYPE_FS_DBLOCK:
165 case GNUNET_BLOCK_TYPE_FS_IBLOCK:
166 GNUNET_CRYPTO_hash (block, block_size, key);
168 case GNUNET_BLOCK_TYPE_FS_UBLOCK:
169 if (block_size < sizeof (struct UBlock))
172 return GNUNET_SYSERR;
175 GNUNET_CRYPTO_hash (&ub->verification_key,
176 sizeof (ub->verification_key),
181 return GNUNET_SYSERR;
187 * Entry point for the plugin.
190 libgnunet_plugin_block_fs_init (void *cls)
192 static enum GNUNET_BLOCK_Type types[] =
194 GNUNET_BLOCK_TYPE_FS_DBLOCK,
195 GNUNET_BLOCK_TYPE_FS_IBLOCK,
196 GNUNET_BLOCK_TYPE_FS_UBLOCK,
197 GNUNET_BLOCK_TYPE_ANY /* end of list */
199 struct GNUNET_BLOCK_PluginFunctions *api;
201 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
202 api->evaluate = &block_plugin_fs_evaluate;
203 api->get_key = &block_plugin_fs_get_key;
210 * Exit point from the plugin.
213 libgnunet_plugin_block_fs_done (void *cls)
215 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
221 /* end of plugin_block_fs.c */