- update fs
[oweals/gnunet.git] / src / fs / plugin_block_fs.c
1 /*
2      This file is part of GNUnet
3      (C) 2010, 2013 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 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 "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 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)
65 {
66   const struct UBlock *ub;
67   struct GNUNET_HashCode hc;
68   struct GNUNET_HashCode chash;
69   struct GNUNET_HashCode mhash;
70
71   switch (type)
72   {
73   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
74   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
75     if (0 != xquery_size)
76     {
77       GNUNET_break_op (0);
78       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
79     }
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:
84     if (0 != xquery_size)
85     {
86       GNUNET_break_op (0);
87       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
88     }
89     if (NULL == reply_block)
90       return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
91
92     if (reply_block_size < sizeof (struct UBlock))
93     {
94       GNUNET_break_op (0);
95       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
96     }
97     ub = reply_block;
98     GNUNET_CRYPTO_hash (&ub->verification_key,
99                         sizeof (ub->verification_key),
100                         &hc);
101     if (0 != memcmp (&hc,
102                      query,
103                      sizeof (struct GNUNET_HashCode)))
104     {
105       GNUNET_break_op (0);
106       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
107     }
108     if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
109     {
110       GNUNET_break_op (0);
111       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
112     }
113     if (GNUNET_OK !=
114         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
115                                   &ub->purpose,
116                                   &ub->signature,
117                                   &ub->verification_key))
118     {
119       GNUNET_break_op (0);
120       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
121     }
122     if (NULL != bf)
123     {
124       GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
125       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
126       if (NULL != *bf)
127       {
128         if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
129           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
130       }
131       else
132       {
133         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
134       }
135       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
136     }
137     return GNUNET_BLOCK_EVALUATION_OK_MORE;
138   default:
139     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
140   }
141 }
142
143
144 /**
145  * Function called to obtain the key for a block.
146  *
147  * @param cls closure
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)
154  */
155 static int
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)
159 {
160   const struct UBlock *ub;
161
162   switch (type)
163   {
164   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
165   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
166     GNUNET_CRYPTO_hash (block, block_size, key);
167     return GNUNET_OK;
168   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
169     if (block_size < sizeof (struct UBlock))
170     {
171       GNUNET_break (0);
172       return GNUNET_SYSERR;
173     }
174     ub = block;
175     GNUNET_CRYPTO_hash (&ub->verification_key,
176                         sizeof (ub->verification_key),
177                         key);
178     return GNUNET_OK;
179   default:
180     GNUNET_break (0);
181     return GNUNET_SYSERR;
182   }
183 }
184
185
186 /**
187  * Entry point for the plugin.
188  */
189 void *
190 libgnunet_plugin_block_fs_init (void *cls)
191 {
192   static enum GNUNET_BLOCK_Type types[] =
193   {
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 */
198   };
199   struct GNUNET_BLOCK_PluginFunctions *api;
200
201   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
202   api->evaluate = &block_plugin_fs_evaluate;
203   api->get_key = &block_plugin_fs_get_key;
204   api->types = types;
205   return api;
206 }
207
208
209 /**
210  * Exit point from the plugin.
211  */
212 void *
213 libgnunet_plugin_block_fs_done (void *cls)
214 {
215   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
216
217   GNUNET_free (api);
218   return NULL;
219 }
220
221 /* end of plugin_block_fs.c */