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