Try to fix fs block plugin deps
[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_FS_PseudonymSignature))
109     {
110       GNUNET_break_op (0);
111       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
112     }
113     if (GNUNET_OK !=
114         GNUNET_FS_pseudonym_verify (&ub->purpose,
115                                     &ub->signature,
116                                     &ub->verification_key))
117     {
118       GNUNET_break_op (0);
119       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
120     }
121     if (NULL != bf)
122     {
123       GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
124       GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
125       if (NULL != *bf)
126       {
127         if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
128           return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
129       }
130       else
131       {
132         *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
133       }
134       GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
135     }
136     return GNUNET_BLOCK_EVALUATION_OK_MORE;
137   default:
138     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
139   }
140 }
141
142
143 /**
144  * Function called to obtain the key for a block.
145  *
146  * @param cls closure
147  * @param type block type
148  * @param block block to get the key for
149  * @param block_size number of bytes in block
150  * @param key set to the key (query) for the given block
151  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
152  *         (or if extracting a key from a block of this type does not work)
153  */
154 static int
155 block_plugin_fs_get_key (void *cls, enum GNUNET_BLOCK_Type type,
156                          const void *block, size_t block_size,
157                          struct GNUNET_HashCode *key)
158 {
159   const struct UBlock *ub;
160
161   switch (type)
162   {
163   case GNUNET_BLOCK_TYPE_FS_DBLOCK:
164   case GNUNET_BLOCK_TYPE_FS_IBLOCK:
165     GNUNET_CRYPTO_hash (block, block_size, key);
166     return GNUNET_OK;
167   case GNUNET_BLOCK_TYPE_FS_UBLOCK:
168     if (block_size < sizeof (struct UBlock))
169     {
170       GNUNET_break (0);
171       return GNUNET_SYSERR;
172     }
173     ub = block;
174     GNUNET_CRYPTO_hash (&ub->verification_key,
175                         sizeof (ub->verification_key),
176                         key);
177     return GNUNET_OK;
178   default:
179     GNUNET_break (0);
180     return GNUNET_SYSERR;
181   }
182 }
183
184
185 /**
186  * Entry point for the plugin.
187  */
188 void *
189 libgnunet_plugin_block_fs_init (void *cls)
190 {
191   static enum GNUNET_BLOCK_Type types[] =
192   {
193     GNUNET_BLOCK_TYPE_FS_DBLOCK,
194     GNUNET_BLOCK_TYPE_FS_IBLOCK,
195     GNUNET_BLOCK_TYPE_FS_UBLOCK,
196     GNUNET_BLOCK_TYPE_ANY       /* end of list */
197   };
198   struct GNUNET_BLOCK_PluginFunctions *api;
199
200   api = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_PluginFunctions));
201   api->evaluate = &block_plugin_fs_evaluate;
202   api->get_key = &block_plugin_fs_get_key;
203   api->types = types;
204   return api;
205 }
206
207
208 /**
209  * Exit point from the plugin.
210  */
211 void *
212 libgnunet_plugin_block_fs_done (void *cls)
213 {
214   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
215
216   GNUNET_free (api);
217   return NULL;
218 }
219
220 /* end of plugin_block_fs.c */