documentation for new parameters
[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 "plugin_block.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  * Mingle hash with the mingle_number to produce different bits.
42  */
43 static void
44 mingle_hash (const GNUNET_HashCode * in,
45              int32_t mingle_number, 
46              GNUNET_HashCode * hc)
47 {
48   GNUNET_HashCode m;
49
50   GNUNET_CRYPTO_hash (&mingle_number, 
51                       sizeof (int32_t), 
52                       &m);
53   GNUNET_CRYPTO_hash_xor (&m, in, hc);
54 }
55
56
57 /**
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.
63  *
64  * @param cls closure
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
74  */
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,
80                           int32_t bf_mutator,
81                           const void *xquery,
82                           size_t xquery_size,
83                           const void *reply_block,
84                           size_t reply_block_size)
85 {
86   const struct SBlock *sb;
87   GNUNET_HashCode chash;
88   GNUNET_HashCode mhash;
89   const GNUNET_HashCode *nsid;
90   GNUNET_HashCode sh;
91
92   switch (type)
93     {
94     case GNUNET_BLOCK_TYPE_DBLOCK:
95     case GNUNET_BLOCK_TYPE_IBLOCK:
96       if (xquery_size != 0) 
97         {
98           GNUNET_break_op (0);
99           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
100         }
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) 
107         {
108           GNUNET_break_op (0);
109           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
110         }
111       if (reply_block == NULL)
112         return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
113       GNUNET_CRYPTO_hash (reply_block,
114                           reply_block_size,
115                           &chash);
116       mingle_hash (&chash, bf_mutator, &mhash);
117       if (NULL != *bf)
118         {
119           if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
120                                                                &mhash))
121             return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
122         }
123       else
124         {
125           *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 
126                                                    8,
127                                                    BLOOMFILTER_K);
128         }
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)) 
133         {
134           GNUNET_break_op (0);
135           return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
136         }
137       if (reply_block == NULL)
138         return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
139       nsid = xquery;
140       if (reply_block_size < sizeof (struct NBlock))
141         return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
142       sb = reply_block;
143       GNUNET_CRYPTO_hash (&sb->subspace,
144                           sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
145                           &sh);
146       if (0 != memcmp (nsid,
147                        &sh,
148                        sizeof (GNUNET_HashCode)))
149         {
150           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
151                       _("Reply mismatched in terms of namespace.  Discarded.\n"));
152           return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
153         }
154       GNUNET_CRYPTO_hash (reply_block,
155                           reply_block_size,
156                           &chash);
157       mingle_hash (&chash, bf_mutator, &mhash);
158       if (NULL != *bf)
159         {
160           if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf,
161                                                                &mhash))
162             return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
163         }
164       else
165         {
166           *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 
167                                                    8,
168                                                    BLOOMFILTER_K);
169         }
170       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
171       return GNUNET_BLOCK_EVALUATION_OK_MORE;
172     default:
173       return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
174     }
175 }
176
177
178 /**
179  * Function called to obtain the key for a block.
180  *
181  * @param cls closure
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)
188  */
189 static int
190 block_plugin_fs_get_key (void *cls,
191                          enum GNUNET_BLOCK_Type type,
192                          const void *block,
193                          size_t block_size,
194                          GNUNET_HashCode *key)
195 {
196   const struct KBlock *kb;
197   const struct SBlock *sb;
198   const struct NBlock *nb;
199
200   switch (type)
201     {
202     case GNUNET_BLOCK_TYPE_DBLOCK:
203     case GNUNET_BLOCK_TYPE_IBLOCK:
204       GNUNET_CRYPTO_hash (block, block_size, key);
205       return GNUNET_OK;
206     case GNUNET_BLOCK_TYPE_KBLOCK:
207       if (block_size < sizeof (struct KBlock))
208         {
209           GNUNET_break_op (0);
210           return GNUNET_SYSERR;
211         }
212       kb = block;
213       if (block_size - sizeof (struct KBlock) !=
214           ntohl (kb->purpose.size) 
215           - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) 
216           - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) ) 
217         {
218           GNUNET_break_op (0);
219           return GNUNET_SYSERR;
220         }
221       if (GNUNET_OK !=
222           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
223                                     &kb->purpose,
224                                     &kb->signature,
225                                     &kb->keyspace)) 
226         {
227           GNUNET_break_op (0);
228           return GNUNET_SYSERR;
229         }
230       if (key != NULL)
231         GNUNET_CRYPTO_hash (&kb->keyspace,
232                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
233                             key);
234       return GNUNET_OK;
235     case GNUNET_BLOCK_TYPE_SBLOCK:
236       if (block_size < sizeof (struct SBlock))
237         {
238           GNUNET_break_op (0);
239           return GNUNET_SYSERR;
240         }
241       sb = block;
242       if (block_size !=
243           ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
244         {
245           GNUNET_break_op (0);
246           return GNUNET_SYSERR;
247         }
248       if (GNUNET_OK !=
249           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
250                                     &sb->purpose,
251                                     &sb->signature,
252                                     &sb->subspace)) 
253         {
254           GNUNET_break_op (0);
255           return GNUNET_SYSERR;
256         }
257       if (key != NULL)
258         *key = sb->identifier;
259       return GNUNET_OK;
260     case GNUNET_BLOCK_TYPE_NBLOCK:
261       if (block_size < sizeof (struct NBlock))
262         {
263           GNUNET_break_op (0);
264           return GNUNET_SYSERR;
265         }
266       nb = block;
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) ) 
271         {
272           GNUNET_break_op (0);
273           return GNUNET_SYSERR;
274         }
275       if (block_size !=
276           ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
277         {
278           GNUNET_break_op (0);
279           return GNUNET_SYSERR;
280         }
281       if (GNUNET_OK !=
282           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
283                                     &nb->ksk_purpose,
284                                     &nb->ksk_signature,
285                                     &nb->keyspace)) 
286         {
287           GNUNET_break_op (0);
288           return GNUNET_SYSERR;
289         }
290       if (GNUNET_OK !=
291           GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
292                                     &nb->ns_purpose,
293                                     &nb->ns_signature,
294                                     &nb->subspace)) 
295         {
296           GNUNET_break_op (0);
297           return GNUNET_SYSERR;
298         }
299       /* FIXME: we used to xor ID with NSID,
300          why not here? */
301       if (key != NULL)
302         GNUNET_CRYPTO_hash (&nb->keyspace,
303                             sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
304                             key);
305       return GNUNET_OK;
306     default:
307       return GNUNET_SYSERR;
308     }
309 }
310                                   
311
312 /**
313  * Entry point for the plugin.
314  */
315 void *
316 libgnunet_plugin_block_fs_init (void *cls)
317 {
318   static enum GNUNET_BLOCK_Type types[] = 
319     {
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 */
326     };
327   struct GNUNET_BLOCK_PluginFunctions *api;
328
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;
332   api->types = types;
333   return api;
334 }
335
336
337 /**
338  * Exit point from the plugin.
339  */
340 void *
341 libgnunet_plugin_block_fs_done (void *cls)
342 {
343   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
344
345   GNUNET_free (api);
346   return NULL;
347 }
348
349 /* end of plugin_block_fs.c */