Added debug print of tree structure
[oweals/gnunet.git] / src / block / plugin_block_fs.c
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 "gnunet_block_plugin.h"
29 #include "block_fs.h"
30 #include "gnunet_signatures.h"
31
32 #define DEBUG_FS_BLOCK GNUNET_NO
33
34 /**
35  * Number of bits we set per entry in the bloomfilter.
36  * Do not change!
37  */
38 #define BLOOMFILTER_K 16
39
40 /**
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.
46  *
47  * @param cls closure
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
57  */
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)
65 {
66   const struct SBlock *sb;
67   GNUNET_HashCode chash;
68   GNUNET_HashCode mhash;
69   const GNUNET_HashCode *nsid;
70   GNUNET_HashCode sh;
71
72   switch (type)
73   {
74   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
75   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
76     if (xquery_size != 0)
77     {
78       GNUNET_break_op (0);
79       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
80     }
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:
86     if (xquery_size != 0)
87     {
88       GNUNET_break_op (0);
89       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
90     }
91     if (reply_block == NULL)
92       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
93     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
94     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
95     if (NULL != *bf)
96     {
97       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
98         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
99     }
100     else
101     {
102       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
103     }
104     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
105     return GNUNET_BLOCK_EVALUATION_OK_MORE;
106   case GNUNET_BLOCK_TYPE_FS_SBLOCK:
107     if (xquery_size != sizeof (GNUNET_HashCode))
108     {
109       GNUNET_break_op (0);
110       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
111     }
112     if (reply_block == NULL)
113       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
114     nsid = xquery;
115     if (reply_block_size < sizeof (struct SBlock))
116     {
117       GNUNET_break_op (0);
118       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
119     }
120     sb = reply_block;
121     GNUNET_CRYPTO_hash (&sb->subspace,
122                         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
123                         &sh);
124     if (0 != memcmp (nsid, &sh, sizeof (GNUNET_HashCode)))
125     {
126       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "block-fs",
127                        _
128                        ("Reply mismatched in terms of namespace.  Discarded.\n"));
129       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
130     }
131     GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
132     GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
133     if (NULL != *bf)
134     {
135       if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
136         return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
137     }
138     else
139     {
140       *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
141     }
142     GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
143     return GNUNET_BLOCK_EVALUATION_OK_MORE;
144   default:
145     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
146   }
147 }
148
149
150 /**
151  * Function called to obtain the key for a block.
152  *
153  * @param cls closure
154  * @param type block type
155  * @param block block to get the key for
156  * @param block_size number of bytes in block
157  * @param key set to the key (query) for the given block
158  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
159  *         (or if extracting a key from a block of this type does not work)
160  */
161 static int
162 block_plugin_fs_get_key (void *cls, enum GNUNET_BLOCK_Type type,
163                          const void *block, size_t block_size,
164                          GNUNET_HashCode * key)
165 {
166   const struct KBlock *kb;
167   const struct SBlock *sb;
168   const struct NBlock *nb;
169
170   switch (type)
171   {
172   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
173   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
174     GNUNET_CRYPTO_hash (block, block_size, key);
175     return GNUNET_OK;
176   case GNUNET_BLOCK_TYPE_FS_KBLOCK:
177     if (block_size < sizeof (struct KBlock))
178     {
179       GNUNET_break_op (0);
180       return GNUNET_NO;
181     }
182     kb = block;
183     if (block_size - sizeof (struct KBlock) !=
184         ntohl (kb->purpose.size) -
185         sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) -
186         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))
187     {
188       GNUNET_break_op (0);
189       return GNUNET_NO;
190     }
191     if (GNUNET_OK !=
192         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
193                                   &kb->purpose, &kb->signature, &kb->keyspace))
194     {
195       GNUNET_break_op (0);
196       return GNUNET_NO;
197     }
198     if (key != NULL)
199       GNUNET_CRYPTO_hash (&kb->keyspace,
200                           sizeof (struct
201                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
202                           key);
203     return GNUNET_OK;
204   case GNUNET_BLOCK_TYPE_FS_SBLOCK:
205     if (block_size < sizeof (struct SBlock))
206     {
207       GNUNET_break_op (0);
208       return GNUNET_NO;
209     }
210     sb = block;
211     if (block_size !=
212         ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
213     {
214       GNUNET_break_op (0);
215       return GNUNET_NO;
216     }
217     if (GNUNET_OK !=
218         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
219                                   &sb->purpose, &sb->signature, &sb->subspace))
220     {
221       GNUNET_break_op (0);
222       return GNUNET_NO;
223     }
224     if (key != NULL)
225       *key = sb->identifier;
226     return GNUNET_OK;
227   case GNUNET_BLOCK_TYPE_FS_NBLOCK:
228     if (block_size < sizeof (struct NBlock))
229     {
230       GNUNET_break_op (0);
231       return GNUNET_NO;
232     }
233     nb = block;
234     if (block_size - sizeof (struct NBlock) !=
235         ntohl (nb->ns_purpose.size) -
236         sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) -
237         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))
238     {
239       GNUNET_break_op (0);
240       return GNUNET_NO;
241     }
242     if (block_size !=
243         ntohl (nb->ksk_purpose.size) +
244         sizeof (struct GNUNET_CRYPTO_RsaSignature))
245     {
246       GNUNET_break_op (0);
247       return GNUNET_NO;
248     }
249     if (GNUNET_OK !=
250         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
251                                   &nb->ksk_purpose, &nb->ksk_signature,
252                                   &nb->keyspace))
253     {
254       GNUNET_break_op (0);
255       return GNUNET_NO;
256     }
257     if (GNUNET_OK !=
258         GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
259                                   &nb->ns_purpose, &nb->ns_signature,
260                                   &nb->subspace))
261     {
262       GNUNET_break_op (0);
263       return GNUNET_NO;
264     }
265     /* FIXME: we used to xor ID with NSID,
266      * why not here? */
267     if (key != NULL)
268       GNUNET_CRYPTO_hash (&nb->keyspace,
269                           sizeof (struct
270                                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
271                           key);
272     return GNUNET_OK;
273   default:
274     return GNUNET_SYSERR;
275   }
276 }
277
278
279 /**
280  * Entry point for the plugin.
281  */
282 void *
283 libgnunet_plugin_block_fs_init (void *cls)
284 {
285   static enum GNUNET_BLOCK_Type types[] =
286   {
287     GNUNET_BLOCK_TYPE_FS_DBLOCK,
288     GNUNET_BLOCK_TYPE_FS_IBLOCK,
289     GNUNET_BLOCK_TYPE_FS_KBLOCK,
290     GNUNET_BLOCK_TYPE_FS_SBLOCK,
291     GNUNET_BLOCK_TYPE_FS_NBLOCK,
292     GNUNET_BLOCK_TYPE_ANY       /* end of list */
293   };
294   struct GNUNET_BLOCK_PluginFunctions *api;
295
296   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
297   api->evaluate = &block_plugin_fs_evaluate;
298   api->get_key = &block_plugin_fs_get_key;
299   api->types = types;
300   return api;
301 }
302
303
304 /**
305  * Exit point from the plugin.
306  */
307 void *
308 libgnunet_plugin_block_fs_done (void *cls)
309 {
310   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
311
312   GNUNET_free (api);
313   return NULL;
314 }
315
316 /* end of plugin_block_fs.c */