869ec155440ff2dd8adbc456c6327a43b4c5365c
[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 (va_arg (va, const char *),
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   return GNUNET_BLOCK_GROUP_bf_create (cls,
112                                        bf_size,
113                                        BLOOMFILTER_K,
114                                        type,
115                                        nonce,
116                                        raw_data,
117                                        raw_data_size);
118 }
119
120
121 /**
122  * Function called to validate a reply or a request.  For
123  * request evaluation, simply pass "NULL" for the @a reply_block.
124  *
125  * @param cls closure
126  * @param type block type
127  * @param group block group to check against
128  * @param eo control flags
129  * @param query original query (hash)
130  * @param xquery extended query data (can be NULL, depending on type)
131  * @param xquery_size number of bytes in @a xquery
132  * @param reply_block response to validate
133  * @param reply_block_size number of bytes in @a reply_block
134  * @return characterization of result
135  */
136 static enum GNUNET_BLOCK_EvaluationResult
137 block_plugin_dht_evaluate (void *cls,
138                            enum GNUNET_BLOCK_Type type,
139                            struct GNUNET_BLOCK_Group *group,
140                            enum GNUNET_BLOCK_EvaluationOptions eo,
141                            const struct GNUNET_HashCode *query,
142                            const void *xquery,
143                            size_t xquery_size,
144                            const void *reply_block,
145                            size_t reply_block_size)
146 {
147   const struct GNUNET_HELLO_Message *hello;
148   struct GNUNET_PeerIdentity pid;
149   const struct GNUNET_MessageHeader *msg;
150   struct GNUNET_HashCode phash;
151
152   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
153     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
154   if (0 != xquery_size)
155   {
156     GNUNET_break_op (0);
157     return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
158   }
159   if (NULL == reply_block)
160     return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
161   if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
162   {
163     GNUNET_break_op (0);
164     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
165   }
166   msg = reply_block;
167   if (reply_block_size != ntohs (msg->size))
168   {
169     GNUNET_break_op (0);
170     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
171   }
172   hello = reply_block;
173   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
174   {
175     GNUNET_break_op (0);
176     return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
177   }
178   GNUNET_CRYPTO_hash (&pid,
179                       sizeof (pid),
180                       &phash);
181   if (GNUNET_YES ==
182       GNUNET_BLOCK_GROUP_bf_test_and_set (group,
183                                           &phash))
184     return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
185   return GNUNET_BLOCK_EVALUATION_OK_MORE;
186 }
187
188
189 /**
190  * Function called to obtain the key for a block.
191  *
192  * @param cls closure
193  * @param type block type
194  * @param block block to get the key for
195  * @param block_size number of bytes @a block
196  * @param[out] key set to the key (query) for the given block
197  * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
198  *         (or if extracting a key from a block of this type does not work)
199  */
200 static int
201 block_plugin_dht_get_key (void *cls,
202                           enum GNUNET_BLOCK_Type type,
203                           const void *block,
204                           size_t block_size,
205                           struct GNUNET_HashCode *key)
206 {
207   const struct GNUNET_MessageHeader *msg;
208   const struct GNUNET_HELLO_Message *hello;
209   struct GNUNET_PeerIdentity *pid;
210
211   if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
212     return GNUNET_SYSERR;
213   if (block_size < sizeof (struct GNUNET_MessageHeader))
214   {
215     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
216                      _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
217     return GNUNET_NO;
218   }
219   msg = block;
220   if (block_size != ntohs (msg->size))
221   {
222     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
223                      _("Size mismatch for block\n"),
224                      GNUNET_BLOCK_TYPE_DHT_HELLO);
225     return GNUNET_NO;
226   }
227   hello = block;
228   memset (key, 0, sizeof (*key));
229   pid = (struct GNUNET_PeerIdentity *) key;
230   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
231   {
232     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
233                      _("Block of type %u is malformed\n"),
234                      GNUNET_BLOCK_TYPE_DHT_HELLO);
235     return GNUNET_NO;
236   }
237   return GNUNET_OK;
238 }
239
240
241 /**
242  * Entry point for the plugin.
243  */
244 void *
245 libgnunet_plugin_block_dht_init (void *cls)
246 {
247   static enum GNUNET_BLOCK_Type types[] =
248   {
249     GNUNET_BLOCK_TYPE_DHT_HELLO,
250     GNUNET_BLOCK_TYPE_ANY       /* end of list */
251   };
252   struct GNUNET_BLOCK_PluginFunctions *api;
253
254   api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
255   api->evaluate = &block_plugin_dht_evaluate;
256   api->get_key = &block_plugin_dht_get_key;
257   api->create_group = &block_plugin_dht_create_group;
258   api->types = types;
259   return api;
260 }
261
262
263 /**
264  * Exit point from the plugin.
265  */
266 void *
267 libgnunet_plugin_block_dht_done (void *cls)
268 {
269   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
270
271   GNUNET_free (api);
272   return NULL;
273 }
274
275 /* end of plugin_block_dht.c */