support BF size adjustments in other plugins
[oweals/gnunet.git] / src / gns / plugin_block_gns.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 gns/plugin_block_gns.c
23  * @brief blocks used for GNS records
24  * @author Martin Schanzenbach
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_block_group_lib.h"
30 #include "gnunet_block_plugin.h"
31 #include "gnunet_namestore_service.h"
32 #include "gnunet_signatures.h"
33
34 /**
35  * Number of bits we set per entry in the bloomfilter.
36  * Do not change! -from fs
37  */
38 #define BLOOMFILTER_K 16
39
40 /**
41  * How big is the BF we use for GNS blocks?
42  */
43 #define GNS_BF_SIZE 8
44
45
46 /**
47  * How many bytes should a bloomfilter be if we have already seen
48  * entry_count responses?  Note that #GNUNET_CONSTANTS_BLOOMFILTER_K
49  * gives us the number of bits set per entry.  Furthermore, we should
50  * not re-size the filter too often (to keep it cheap).
51  *
52  * Since other peers will also add entries but not resize the filter,
53  * we should generally pick a slightly larger size than what the
54  * strict math would suggest.
55  *
56  * @param entry_count expected number of entries in the Bloom filter
57  * @return must be a power of two and smaller or equal to 2^15.
58  */
59 static size_t
60 compute_bloomfilter_size (unsigned int entry_count)
61 {
62   size_t size;
63   unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
64   uint16_t max = 1 << 15;
65
66   if (entry_count > max)
67     return max;
68   size = 8;
69   while ((size < max) && (size < ideal))
70     size *= 2;
71   if (size > max)
72     return max;
73   return size;
74 }
75
76
77 /**
78  * Create a new block group.
79  *
80  * @param ctx block context in which the block group is created
81  * @param type type of the block for which we are creating the group
82  * @param nonce random value used to seed the group creation
83  * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
84  * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
85  * @param va variable arguments specific to @a type
86  * @return block group handle, NULL if block groups are not supported
87  *         by this @a type of block (this is not an error)
88  */
89 static struct GNUNET_BLOCK_Group *
90 block_plugin_gns_create_group (void *cls,
91                                enum GNUNET_BLOCK_Type type,
92                                uint32_t nonce,
93                                const void *raw_data,
94                                size_t raw_data_size,
95                                va_list va)
96 {
97   unsigned int bf_size;
98   const char *guard;
99
100   guard = va_arg (va, const char *);
101   if (0 == memcmp (guard,
102                    "seen-set-size",
103                    strlen ("seen-set-size")))
104     bf_size = compute_bloomfilter_size (va_arg (va, unsigned int));
105   else if (0 == memcmp (guard,
106                         "filter-size",
107                         strlen ("filter-size")))
108     bf_size = va_arg (va, unsigned int);
109   else
110   {
111     GNUNET_break (0);
112     bf_size = GNS_BF_SIZE;
113   }
114   GNUNET_break (NULL == va_arg (va, const char *));
115   return GNUNET_BLOCK_GROUP_bf_create (cls,
116                                        bf_size,
117                                        BLOOMFILTER_K,
118                                        type,
119                                        nonce,
120                                        raw_data,
121                                        raw_data_size);
122 }
123
124
125 /**
126  * Function called to validate a reply or a request.  For
127  * request evaluation, simply pass "NULL" for the reply_block.
128  * Note that it is assumed that the reply has already been
129  * matched to the key (and signatures checked) as it would
130  * be done with the "get_key" function.
131  *
132  * @param cls closure
133  * @param type block type
134  * @param bg block group to use for evaluation
135  * @param eo control flags
136  * @param query original query (hash)
137  * @param xquery extrended query data (can be NULL, depending on @a type)
138  * @param xquery_size number of bytes in @a xquery
139  * @param reply_block response to validate
140  * @param reply_block_size number of bytes in @a reply_block
141  * @return characterization of result
142  */
143 static enum GNUNET_BLOCK_EvaluationResult
144 block_plugin_gns_evaluate (void *cls,
145                            enum GNUNET_BLOCK_Type type,
146                            struct GNUNET_BLOCK_Group *bg,
147                            enum GNUNET_BLOCK_EvaluationOptions eo,
148                            const struct GNUNET_HashCode *query,
149                            const void *xquery,
150                            size_t xquery_size,
151                            const void *reply_block,
152                            size_t reply_block_size)
153 {
154   const struct GNUNET_GNSRECORD_Block *block;
155   struct GNUNET_HashCode h;
156   struct GNUNET_HashCode chash;
157
158   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
159     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
160   if (NULL == reply_block)
161   {
162     if (0 != xquery_size)
163     {
164       GNUNET_break_op (0);
165       return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
166     }
167     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
168   }
169
170   /* this is a reply */
171   if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
172     {
173       GNUNET_break_op (0);
174       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
175     }
176   block = reply_block;
177   if (ntohl (block->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature) + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
178       reply_block_size)
179     {
180       GNUNET_break_op (0);
181       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
182     }
183   GNUNET_CRYPTO_hash (&block->derived_key,
184                       sizeof (block->derived_key),
185                       &h);
186   if (0 != memcmp (&h, query, sizeof (struct GNUNET_HashCode)))
187     {
188       GNUNET_break_op (0);
189       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
190     }
191   if (GNUNET_OK !=
192       GNUNET_GNSRECORD_block_verify (block))
193     {
194       GNUNET_break_op (0);
195       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
196     }
197   GNUNET_CRYPTO_hash (reply_block,
198                       reply_block_size,
199                       &chash);
200   if (GNUNET_YES ==
201       GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
202                                           &chash))
203     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
204   return GNUNET_BLOCK_EVALUATION_OK_MORE;
205 }
206
207
208 /**
209  * Function called to obtain the key for a block.
210  *
211  * @param cls closure
212  * @param type block type
213  * @param reply_block block to get the key for
214  * @param reply_block_size number of bytes in @a reply_block
215  * @param key set to the key (query) for the given block
216  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
217  *         (or if extracting a key from a block of this type does not work)
218  */
219 static int
220 block_plugin_gns_get_key (void *cls,
221                           enum GNUNET_BLOCK_Type type,
222                           const void *reply_block,
223                           size_t reply_block_size,
224                           struct GNUNET_HashCode *key)
225 {
226   const struct GNUNET_GNSRECORD_Block *block;
227
228   if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
229     return GNUNET_SYSERR;
230   if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
231     {
232       GNUNET_break_op (0);
233       return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
234     }
235   block = reply_block;
236   GNUNET_CRYPTO_hash (&block->derived_key,
237                       sizeof (block->derived_key),
238                       key);
239   return GNUNET_OK;
240 }
241
242
243 /**
244  * Entry point for the plugin.
245  */
246 void *
247 libgnunet_plugin_block_gns_init (void *cls)
248 {
249   static enum GNUNET_BLOCK_Type types[] =
250   {
251     GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
252     GNUNET_BLOCK_TYPE_ANY       /* end of list */
253   };
254   struct GNUNET_BLOCK_PluginFunctions *api;
255
256   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
257   api->evaluate = &block_plugin_gns_evaluate;
258   api->get_key = &block_plugin_gns_get_key;
259   api->create_group = &block_plugin_gns_create_group;
260   api->types = types;
261   return api;
262 }
263
264
265 /**
266  * Exit point from the plugin.
267  */
268 void *
269 libgnunet_plugin_block_gns_done (void *cls)
270 {
271   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
272
273   GNUNET_free (api);
274   return NULL;
275 }
276
277 /* end of plugin_block_gns.c */