-fix (C) notices
[oweals/gnunet.git] / src / fs / plugin_block_fs.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2010, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/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 "gnunet_fs_service.h"
30 #include "block_fs.h"
31 #include "gnunet_signatures.h"
32
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 #GNUNET_BLOCK_get_key() function.
46  *
47  * @param cls closure
48  * @param type block type
49  * @param eo control flags
50  * @param query original query (hash)
51  * @param bf pointer to bloom filter associated with query; possibly updated (!)
52  * @param bf_mutator mutation value for @a bf
53  * @param xquery extrended query data (can be NULL, depending on type)
54  * @param xquery_size number of bytes in @a xquery
55  * @param reply_block response to validate
56  * @param reply_block_size number of bytes in @a reply_block
57  * @return characterization of result
58  */
59 static enum GNUNET_BLOCK_EvaluationResult
60 block_plugin_fs_evaluate (void *cls,
61                           enum GNUNET_BLOCK_Type type,
62                           enum GNUNET_BLOCK_EvaluationOptions eo,
63                           const struct GNUNET_HashCode *query,
64                           struct GNUNET_CONTAINER_BloomFilter **bf,
65                           int32_t bf_mutator,
66                           const void *xquery,
67                           size_t xquery_size,
68                           const void *reply_block,
69                           size_t reply_block_size)
70 {
71   const struct UBlock *ub;
72   struct GNUNET_HashCode hc;
73   struct GNUNET_HashCode chash;
74   struct GNUNET_HashCode mhash;
75
76   switch (type)
77   {
78   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
79   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
80     if (0 != xquery_size)
81     {
82       GNUNET_break_op (0);
83       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
84     }
85     if (NULL == reply_block)
86       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
87     return GNUNET_BLOCK_EVALUATION_OK_LAST;
88   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
89     if (0 != xquery_size)
90     {
91       GNUNET_break_op (0);
92       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
93     }
94     if (NULL == reply_block)
95       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
96
97     if (reply_block_size < sizeof (struct UBlock))
98     {
99       GNUNET_break_op (0);
100       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
101     }
102     ub = reply_block;
103     GNUNET_CRYPTO_hash (&ub->verification_key,
104                         sizeof (ub->verification_key),
105                         &hc);
106     if (0 != memcmp (&hc,
107                      query,
108                      sizeof (struct GNUNET_HashCode)))
109     {
110       GNUNET_break_op (0);
111       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
112     }
113     if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
114     {
115       GNUNET_break_op (0);
116       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
117     }
118     if ( (0 == (eo & GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO)) &&
119          (GNUNET_OK !=
120           GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
121                                       &ub->purpose,
122                                       &ub->signature,
123                                       &ub->verification_key)) )
124     {
125       GNUNET_break_op (0);
126       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
127     }
128     if (NULL != bf)
129     {
130       GNUNET_CRYPTO_hash (reply_block,
131                           reply_block_size,
132                           &chash);
133       GNUNET_BLOCK_mingle_hash (&chash,
134                                 bf_mutator,
135                                 &mhash);
136       if (NULL != *bf)
137       {
138         if (GNUNET_YES ==
139             GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
140           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
141       }
142       else
143       {
144         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
145       }
146       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
147     }
148     return GNUNET_BLOCK_EVALUATION_OK_MORE;
149   default:
150     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
151   }
152 }
153
154
155 /**
156  * Function called to obtain the key for a block.
157  *
158  * @param cls closure
159  * @param type block type
160  * @param block block to get the key for
161  * @param block_size number of bytes in @a block
162  * @param key set to the key (query) for the given block
163  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
164  *         (or if extracting a key from a block of this type does not work)
165  */
166 static int
167 block_plugin_fs_get_key (void *cls,
168                          enum GNUNET_BLOCK_Type type,
169                          const void *block,
170                          size_t block_size,
171                          struct GNUNET_HashCode *key)
172 {
173   const struct UBlock *ub;
174
175   switch (type)
176   {
177   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
178   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
179     GNUNET_CRYPTO_hash (block, block_size, key);
180     return GNUNET_OK;
181   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
182     if (block_size < sizeof (struct UBlock))
183     {
184       GNUNET_break (0);
185       return GNUNET_SYSERR;
186     }
187     ub = block;
188     GNUNET_CRYPTO_hash (&ub->verification_key,
189                         sizeof (ub->verification_key),
190                         key);
191     return GNUNET_OK;
192   default:
193     GNUNET_break (0);
194     return GNUNET_SYSERR;
195   }
196 }
197
198
199 /**
200  * Entry point for the plugin.
201  */
202 void *
203 libgnunet_plugin_block_fs_init (void *cls)
204 {
205   static enum GNUNET_BLOCK_Type types[] =
206   {
207     GNUNET_BLOCK_TYPE_FS_DBLOCK,
208     GNUNET_BLOCK_TYPE_FS_IBLOCK,
209     GNUNET_BLOCK_TYPE_FS_UBLOCK,
210     GNUNET_BLOCK_TYPE_ANY       /* end of list */
211   };
212   struct GNUNET_BLOCK_PluginFunctions *api;
213
214   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
215   api->evaluate = &block_plugin_fs_evaluate;
216   api->get_key = &block_plugin_fs_get_key;
217   api->types = types;
218   return api;
219 }
220
221
222 /**
223  * Exit point from the plugin.
224  */
225 void *
226 libgnunet_plugin_block_fs_done (void *cls)
227 {
228   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
229
230   GNUNET_free (api);
231   return NULL;
232 }
233
234 /* end of plugin_block_fs.c */