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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file dht/plugin_block_dht.c
21 * @brief block plugin for DHT internals (right now, find-peer requests only);
22 * other plugins should be used to store "useful" data in the
23 * DHT (see fs block plugin)
24 * @author Christian Grothoff
27 #include "gnunet_constants.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_block_plugin.h"
30 #include "gnunet_block_group_lib.h"
32 #define DEBUG_DHT GNUNET_EXTRA_LOGGING
35 * Number of bits we set per entry in the bloomfilter.
38 #define BLOOMFILTER_K 16
42 * Create a new block group.
44 * @param ctx block context in which the block group is created
45 * @param type type of the block for which we are creating the group
46 * @param nonce random value used to seed the group creation
47 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
48 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
49 * @param va variable arguments specific to @a type
50 * @return block group handle, NULL if block groups are not supported
51 * by this @a type of block (this is not an error)
53 static struct GNUNET_BLOCK_Group *
54 block_plugin_dht_create_group (void *cls,
55 enum GNUNET_BLOCK_Type type,
64 guard = va_arg (va, const char *);
65 if (0 == strcmp (guard,
67 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va,
70 else if (0 == strcmp (guard,
72 bf_size = va_arg (va, unsigned int);
78 GNUNET_break (NULL == va_arg (va, const char *));
79 return GNUNET_BLOCK_GROUP_bf_create (cls,
90 * Function called to validate a reply or a request. For
91 * request evaluation, simply pass "NULL" for the @a reply_block.
95 * @param type block type
96 * @param group block group to check against
97 * @param eo control flags
98 * @param query original query (hash)
99 * @param xquery extended query data (can be NULL, depending on type)
100 * @param xquery_size number of bytes in @a xquery
101 * @param reply_block response to validate
102 * @param reply_block_size number of bytes in @a reply_block
103 * @return characterization of result
105 static enum GNUNET_BLOCK_EvaluationResult
106 block_plugin_dht_evaluate (void *cls,
107 struct GNUNET_BLOCK_Context *ctx,
108 enum GNUNET_BLOCK_Type type,
109 struct GNUNET_BLOCK_Group *group,
110 enum GNUNET_BLOCK_EvaluationOptions eo,
111 const struct GNUNET_HashCode *query,
114 const void *reply_block,
115 size_t reply_block_size)
117 const struct GNUNET_HELLO_Message *hello;
118 struct GNUNET_PeerIdentity pid;
119 const struct GNUNET_MessageHeader *msg;
120 struct GNUNET_HashCode phash;
122 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
123 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
124 if (0 != xquery_size)
127 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
129 if (NULL == reply_block)
130 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
131 if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
134 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
137 if (reply_block_size != ntohs (msg->size))
140 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
143 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
146 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
148 GNUNET_CRYPTO_hash (&pid,
152 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
154 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
155 return GNUNET_BLOCK_EVALUATION_OK_MORE;
160 * Function called to obtain the key for a block.
163 * @param type block type
164 * @param block block to get the key for
165 * @param block_size number of bytes @a block
166 * @param[out] key set to the key (query) for the given block
167 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
168 * (or if extracting a key from a block of this type does not work)
171 block_plugin_dht_get_key (void *cls,
172 enum GNUNET_BLOCK_Type type,
175 struct GNUNET_HashCode *key)
177 const struct GNUNET_MessageHeader *msg;
178 const struct GNUNET_HELLO_Message *hello;
179 struct GNUNET_PeerIdentity *pid;
181 if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
182 return GNUNET_SYSERR;
183 if (block_size < sizeof (struct GNUNET_MessageHeader))
185 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
187 _("Block not of type %u\n"),
188 GNUNET_BLOCK_TYPE_DHT_HELLO);
192 if (block_size != ntohs (msg->size))
194 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
196 _("Size mismatch for block\n"),
197 GNUNET_BLOCK_TYPE_DHT_HELLO);
201 memset (key, 0, sizeof (*key));
202 pid = (struct GNUNET_PeerIdentity *) key;
203 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
205 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
207 _("Block of type %u is malformed\n"),
208 GNUNET_BLOCK_TYPE_DHT_HELLO);
216 * Entry point for the plugin.
219 libgnunet_plugin_block_dht_init (void *cls)
221 static enum GNUNET_BLOCK_Type types[] =
223 GNUNET_BLOCK_TYPE_DHT_HELLO,
224 GNUNET_BLOCK_TYPE_ANY /* end of list */
226 struct GNUNET_BLOCK_PluginFunctions *api;
228 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
229 api->evaluate = &block_plugin_dht_evaluate;
230 api->get_key = &block_plugin_dht_get_key;
231 api->create_group = &block_plugin_dht_create_group;
238 * Exit point from the plugin.
241 libgnunet_plugin_block_dht_done (void *cls)
243 struct GNUNET_BLOCK_PluginFunctions *api = cls;
249 /* end of plugin_block_dht.c */