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 return GNUNET_BLOCK_GROUP_bf_create (cls,
122 * Function called to validate a reply or a request. For
123 * request evaluation, simply pass "NULL" for the @a reply_block.
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
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,
144 const void *reply_block,
145 size_t reply_block_size)
147 const struct GNUNET_HELLO_Message *hello;
148 struct GNUNET_PeerIdentity pid;
149 const struct GNUNET_MessageHeader *msg;
150 struct GNUNET_HashCode phash;
152 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
153 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
154 if (0 != xquery_size)
157 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
159 if (NULL == reply_block)
160 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
161 if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
164 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
167 if (reply_block_size != ntohs (msg->size))
170 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
173 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
176 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
178 GNUNET_CRYPTO_hash (&pid,
182 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
184 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
185 return GNUNET_BLOCK_EVALUATION_OK_MORE;
190 * Function called to obtain the key for a block.
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)
201 block_plugin_dht_get_key (void *cls,
202 enum GNUNET_BLOCK_Type type,
205 struct GNUNET_HashCode *key)
207 const struct GNUNET_MessageHeader *msg;
208 const struct GNUNET_HELLO_Message *hello;
209 struct GNUNET_PeerIdentity *pid;
211 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
212 return GNUNET_SYSERR;
213 if (block_size < sizeof (struct GNUNET_MessageHeader))
215 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
216 _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
220 if (block_size != ntohs (msg->size))
222 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
223 _("Size mismatch for block\n"),
224 GNUNET_BLOCK_TYPE_DHT_HELLO);
228 memset (key, 0, sizeof (*key));
229 pid = (struct GNUNET_PeerIdentity *) key;
230 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
232 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
233 _("Block of type %u is malformed\n"),
234 GNUNET_BLOCK_TYPE_DHT_HELLO);
242 * Entry point for the plugin.
245 libgnunet_plugin_block_dht_init (void *cls)
247 static enum GNUNET_BLOCK_Type types[] =
249 GNUNET_BLOCK_TYPE_DHT_HELLO,
250 GNUNET_BLOCK_TYPE_ANY /* end of list */
252 struct GNUNET_BLOCK_PluginFunctions *api;
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;
264 * Exit point from the plugin.
267 libgnunet_plugin_block_dht_done (void *cls)
269 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
275 /* end of plugin_block_dht.c */