diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
commit | a42b4e0ed22b65631caea4bb456f3d8fc21f11b1 (patch) | |
tree | a3a1e30a7f8e7f8b30c75c96923249831a7e8b2e /src/block/plugin_block_fs.c | |
parent | dd253b7f7591f0869f8ea14ee111b7d3b3e480b6 (diff) | |
download | gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.tar.gz gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.zip |
towards pluggable block library
Diffstat (limited to 'src/block/plugin_block_fs.c')
-rw-r--r-- | src/block/plugin_block_fs.c | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/src/block/plugin_block_fs.c b/src/block/plugin_block_fs.c new file mode 100644 index 000000000..362932991 --- /dev/null +++ b/src/block/plugin_block_fs.c | |||
@@ -0,0 +1,346 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file block/plugin_block_fs.c | ||
23 | * @brief blocks used for file-sharing | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "plugin_block.h" | ||
29 | #include "gnunet_signatures.h" | ||
30 | |||
31 | #define DEBUG_FS_BLOCK GNUNET_NO | ||
32 | |||
33 | /** | ||
34 | * Number of bits we set per entry in the bloomfilter. | ||
35 | * Do not change! | ||
36 | */ | ||
37 | #define BLOOMFILTER_K 16 | ||
38 | |||
39 | /** | ||
40 | * Mingle hash with the mingle_number to produce different bits. | ||
41 | */ | ||
42 | static void | ||
43 | mingle_hash (const GNUNET_HashCode * in, | ||
44 | int32_t mingle_number, | ||
45 | GNUNET_HashCode * hc) | ||
46 | { | ||
47 | GNUNET_HashCode m; | ||
48 | |||
49 | GNUNET_CRYPTO_hash (&mingle_number, | ||
50 | sizeof (int32_t), | ||
51 | &m); | ||
52 | GNUNET_CRYPTO_hash_xor (&m, in, hc); | ||
53 | } | ||
54 | |||
55 | |||
56 | /** | ||
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. | ||
62 | * | ||
63 | * @param cls closure | ||
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 | ||
73 | */ | ||
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, | ||
79 | int32_t bf_mutator, | ||
80 | const void *xquery, | ||
81 | size_t xquery_size, | ||
82 | const void *reply_block, | ||
83 | size_t reply_block_size) | ||
84 | { | ||
85 | const struct SBlock *sb; | ||
86 | GNUNET_HashCode chash; | ||
87 | GNUNET_HashCode mhash; | ||
88 | const GNUNET_HashCode *nsid; | ||
89 | GNUNET_HashCode sh; | ||
90 | |||
91 | switch (type) | ||
92 | { | ||
93 | case GNUNET_BLOCK_TYPE_DBLOCK: | ||
94 | case GNUNET_BLOCK_TYPE_IBLOCK: | ||
95 | if (xquery_size != 0) | ||
96 | { | ||
97 | GNUNET_break_op (0); | ||
98 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
99 | } | ||
100 | return GNUNET_BLOCK_EVALUATION_OK_LAST; | ||
101 | case GNUNET_BLOCK_TYPE_KBLOCK: | ||
102 | case GNUNET_BLOCK_TYPE_NBLOCK: | ||
103 | if (xquery_size != 0) | ||
104 | { | ||
105 | GNUNET_break_op (0); | ||
106 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
107 | } | ||
108 | if (reply_block == NULL) | ||
109 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
110 | GNUNET_CRYPTO_hash (reply_block, | ||
111 | reply_block_size, | ||
112 | &chash); | ||
113 | mingle_hash (&chash, bf_mutator, &mhash); | ||
114 | if (NULL != *bf) | ||
115 | { | ||
116 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, | ||
117 | &mhash)) | ||
118 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
119 | } | ||
120 | else | ||
121 | { | ||
122 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
123 | 8, | ||
124 | BLOOMFILTER_K); | ||
125 | } | ||
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)) | ||
130 | { | ||
131 | GNUNET_break_op (0); | ||
132 | return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID; | ||
133 | } | ||
134 | if (reply_block == NULL) | ||
135 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
136 | nsid = xquery; | ||
137 | if (reply_block_size < sizeof (struct NBlock)) | ||
138 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | ||
139 | sb = reply_block; | ||
140 | GNUNET_CRYPTO_hash (&sb->subspace, | ||
141 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
142 | &sh); | ||
143 | if (0 != memcmp (nsid, | ||
144 | &sh, | ||
145 | sizeof (GNUNET_HashCode))) | ||
146 | { | ||
147 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
148 | _("Reply mismatched in terms of namespace. Discarded.\n")); | ||
149 | return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; | ||
150 | } | ||
151 | GNUNET_CRYPTO_hash (reply_block, | ||
152 | reply_block_size, | ||
153 | &chash); | ||
154 | mingle_hash (&chash, bf_mutator, &mhash); | ||
155 | if (NULL != *bf) | ||
156 | { | ||
157 | if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, | ||
158 | &mhash)) | ||
159 | return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE; | ||
160 | } | ||
161 | else | ||
162 | { | ||
163 | *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, | ||
164 | 8, | ||
165 | BLOOMFILTER_K); | ||
166 | } | ||
167 | GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash); | ||
168 | return GNUNET_BLOCK_EVALUATION_OK_MORE; | ||
169 | default: | ||
170 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
171 | } | ||
172 | } | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Function called to obtain the key for a block. | ||
177 | * | ||
178 | * @param cls closure | ||
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) | ||
185 | */ | ||
186 | static int | ||
187 | block_plugin_fs_get_key (void *cls, | ||
188 | enum GNUNET_BLOCK_Type type, | ||
189 | const void *block, | ||
190 | size_t block_size, | ||
191 | GNUNET_HashCode *key) | ||
192 | { | ||
193 | const struct KBlock *kb; | ||
194 | const struct SBlock *sb; | ||
195 | const struct NBlock *nb; | ||
196 | |||
197 | switch (type) | ||
198 | { | ||
199 | case GNUNET_BLOCK_TYPE_DBLOCK: | ||
200 | case GNUNET_BLOCK_TYPE_IBLOCK: | ||
201 | GNUNET_CRYPTO_hash (block, block_size, key); | ||
202 | return GNUNET_OK; | ||
203 | case GNUNET_BLOCK_TYPE_KBLOCK: | ||
204 | if (block_size < sizeof (struct KBlock)) | ||
205 | { | ||
206 | GNUNET_break_op (0); | ||
207 | return GNUNET_SYSERR; | ||
208 | } | ||
209 | kb = block; | ||
210 | if (block_size - sizeof (struct KBlock) != | ||
211 | ntohl (kb->purpose.size) | ||
212 | - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) | ||
213 | - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) | ||
214 | { | ||
215 | GNUNET_break_op (0); | ||
216 | return GNUNET_SYSERR; | ||
217 | } | ||
218 | if (GNUNET_OK != | ||
219 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK, | ||
220 | &kb->purpose, | ||
221 | &kb->signature, | ||
222 | &kb->keyspace)) | ||
223 | { | ||
224 | GNUNET_break_op (0); | ||
225 | return GNUNET_SYSERR; | ||
226 | } | ||
227 | if (key != NULL) | ||
228 | GNUNET_CRYPTO_hash (&kb->keyspace, | ||
229 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
230 | key); | ||
231 | return GNUNET_OK; | ||
232 | case GNUNET_BLOCK_TYPE_SBLOCK: | ||
233 | if (block_size < sizeof (struct SBlock)) | ||
234 | { | ||
235 | GNUNET_break_op (0); | ||
236 | return GNUNET_SYSERR; | ||
237 | } | ||
238 | sb = block; | ||
239 | if (block_size != | ||
240 | ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
241 | { | ||
242 | GNUNET_break_op (0); | ||
243 | return GNUNET_SYSERR; | ||
244 | } | ||
245 | if (GNUNET_OK != | ||
246 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK, | ||
247 | &sb->purpose, | ||
248 | &sb->signature, | ||
249 | &sb->subspace)) | ||
250 | { | ||
251 | GNUNET_break_op (0); | ||
252 | return GNUNET_SYSERR; | ||
253 | } | ||
254 | if (key != NULL) | ||
255 | *key = sb->identifier; | ||
256 | return GNUNET_OK; | ||
257 | case GNUNET_BLOCK_TYPE_NBLOCK: | ||
258 | if (block_size < sizeof (struct NBlock)) | ||
259 | { | ||
260 | GNUNET_break_op (0); | ||
261 | return GNUNET_SYSERR; | ||
262 | } | ||
263 | nb = block; | ||
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) ) | ||
268 | { | ||
269 | GNUNET_break_op (0); | ||
270 | return GNUNET_SYSERR; | ||
271 | } | ||
272 | if (block_size != | ||
273 | ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature)) | ||
274 | { | ||
275 | GNUNET_break_op (0); | ||
276 | return GNUNET_SYSERR; | ||
277 | } | ||
278 | if (GNUNET_OK != | ||
279 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG, | ||
280 | &nb->ksk_purpose, | ||
281 | &nb->ksk_signature, | ||
282 | &nb->keyspace)) | ||
283 | { | ||
284 | GNUNET_break_op (0); | ||
285 | return GNUNET_SYSERR; | ||
286 | } | ||
287 | if (GNUNET_OK != | ||
288 | GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK, | ||
289 | &nb->ns_purpose, | ||
290 | &nb->ns_signature, | ||
291 | &nb->subspace)) | ||
292 | { | ||
293 | GNUNET_break_op (0); | ||
294 | return GNUNET_SYSERR; | ||
295 | } | ||
296 | /* FIXME: we used to xor ID with NSID, | ||
297 | why not here? */ | ||
298 | if (key != NULL) | ||
299 | GNUNET_CRYPTO_hash (&nb->keyspace, | ||
300 | sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), | ||
301 | key); | ||
302 | return GNUNET_OK; | ||
303 | default: | ||
304 | return GNUNET_SYSERR; | ||
305 | } | ||
306 | } | ||
307 | |||
308 | |||
309 | /** | ||
310 | * Entry point for the plugin. | ||
311 | */ | ||
312 | void * | ||
313 | gnunet_plugin_block_fs_init (void *cls) | ||
314 | { | ||
315 | static enum GNUNET_BLOCK_Type types[] = | ||
316 | { | ||
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 */ | ||
323 | }; | ||
324 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
325 | |||
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; | ||
329 | api->types = types; | ||
330 | return api; | ||
331 | } | ||
332 | |||
333 | |||
334 | /** | ||
335 | * Exit point from the plugin. | ||
336 | */ | ||
337 | void * | ||
338 | gnunet_plugin_block_fs_done (void *cls) | ||
339 | { | ||
340 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
341 | |||
342 | GNUNET_free (api); | ||
343 | return NULL; | ||
344 | } | ||
345 | |||
346 | /* end of plugin_block_fs.c */ | ||