2 This file is part of GNUnet
3 Copyright (C) 2010, 2017 GNUnet e.V.
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.
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.
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.
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
29 #include "gnunet_constants.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_block_plugin.h"
32 #include "gnunet_block_group_lib.h"
34 #define DEBUG_DHT GNUNET_EXTRA_LOGGING
37 * Number of bits we set per entry in the bloomfilter.
40 #define BLOOMFILTER_K 16
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).
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.
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.
57 compute_bloomfilter_size (unsigned int entry_count)
60 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
61 uint16_t max = 1 << 15;
63 if (entry_count > max)
66 while ((size < max) && (size < ideal))
75 * Create a new block group.
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)
86 static struct GNUNET_BLOCK_Group *
87 block_plugin_dht_create_group (void *cls,
88 enum GNUNET_BLOCK_Type type,
97 guard = va_arg (va, const char *);
98 if (0 == memcmp (guard,
100 strlen ("seen-set-size")))
101 bf_size = compute_bloomfilter_size (va_arg (va, unsigned int));
102 else if (0 == memcmp (guard,
104 strlen ("filter-size")))
105 bf_size = va_arg (va, unsigned int);
111 GNUNET_break (NULL == va_arg (va, const char *));
112 return GNUNET_BLOCK_GROUP_bf_create (cls,
123 * Function called to validate a reply or a request. For
124 * request evaluation, simply pass "NULL" for the @a reply_block.
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
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,
145 const void *reply_block,
146 size_t reply_block_size)
148 const struct GNUNET_HELLO_Message *hello;
149 struct GNUNET_PeerIdentity pid;
150 const struct GNUNET_MessageHeader *msg;
151 struct GNUNET_HashCode phash;
153 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
154 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
155 if (0 != xquery_size)
158 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
160 if (NULL == reply_block)
161 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
162 if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
165 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
168 if (reply_block_size != ntohs (msg->size))
171 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
174 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
177 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
179 GNUNET_CRYPTO_hash (&pid,
183 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
185 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
186 return GNUNET_BLOCK_EVALUATION_OK_MORE;
191 * Function called to obtain the key for a block.
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)
202 block_plugin_dht_get_key (void *cls,
203 enum GNUNET_BLOCK_Type type,
206 struct GNUNET_HashCode *key)
208 const struct GNUNET_MessageHeader *msg;
209 const struct GNUNET_HELLO_Message *hello;
210 struct GNUNET_PeerIdentity *pid;
212 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
213 return GNUNET_SYSERR;
214 if (block_size < sizeof (struct GNUNET_MessageHeader))
216 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
217 _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
221 if (block_size != ntohs (msg->size))
223 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
224 _("Size mismatch for block\n"),
225 GNUNET_BLOCK_TYPE_DHT_HELLO);
229 memset (key, 0, sizeof (*key));
230 pid = (struct GNUNET_PeerIdentity *) key;
231 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
233 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
234 _("Block of type %u is malformed\n"),
235 GNUNET_BLOCK_TYPE_DHT_HELLO);
243 * Entry point for the plugin.
246 libgnunet_plugin_block_dht_init (void *cls)
248 static enum GNUNET_BLOCK_Type types[] =
250 GNUNET_BLOCK_TYPE_DHT_HELLO,
251 GNUNET_BLOCK_TYPE_ANY /* end of list */
253 struct GNUNET_BLOCK_PluginFunctions *api;
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;
265 * Exit point from the plugin.
268 libgnunet_plugin_block_dht_done (void *cls)
270 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
276 /* end of plugin_block_dht.c */