2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @brief library for data block manipulation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_block_lib.h"
31 #include "gnunet_block_plugin.h"
35 * Handle for a plugin.
40 * Name of the shared library.
47 struct GNUNET_BLOCK_PluginFunctions *api;
52 * Handle to an initialized block library.
54 struct GNUNET_BLOCK_Context
57 * Array of our plugins.
59 struct Plugin **plugins;
62 * Size of the 'plugins' array.
64 unsigned int num_plugins;
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 * Mingle hash with the mingle_number to produce different bits.
76 * @param in original hash code
77 * @param mingle_number number for hash permutation
78 * @param hc where to store the result.
81 GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode *in,
82 uint32_t mingle_number,
83 struct GNUNET_HashCode *hc)
85 struct GNUNET_HashCode m;
87 GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
88 GNUNET_CRYPTO_hash_xor (&m, in, hc);
93 * Add a plugin to the list managed by the block library.
95 * @param cls the block context
96 * @param library_name name of the plugin
97 * @param lib_ret the plugin API
100 add_plugin (void *cls,
101 const char *library_name,
104 struct GNUNET_BLOCK_Context *ctx = cls;
105 struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
106 struct Plugin *plugin;
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109 "Loading block plugin `%s'\n",
111 plugin = GNUNET_new (struct Plugin);
113 plugin->library_name = GNUNET_strdup (library_name);
114 GNUNET_array_append (ctx->plugins, ctx->num_plugins, plugin);
120 * Create a block context. Loads the block plugins.
122 * @param cfg configuration to use
123 * @return NULL on error
125 struct GNUNET_BLOCK_Context *
126 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
128 struct GNUNET_BLOCK_Context *ctx;
130 ctx = GNUNET_new (struct GNUNET_BLOCK_Context);
132 GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_", NULL, &add_plugin, ctx);
138 * Destroy the block context.
140 * @param ctx context to destroy
143 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
146 struct Plugin *plugin;
148 for (i = 0; i < ctx->num_plugins; i++)
150 plugin = ctx->plugins[i];
151 GNUNET_break (NULL ==
152 GNUNET_PLUGIN_unload (plugin->library_name, plugin->api));
153 GNUNET_free (plugin->library_name);
154 GNUNET_free (plugin);
156 GNUNET_free (ctx->plugins);
162 * Find a plugin for the given type.
164 * @param ctx context to search
165 * @param type type to look for
166 * @return NULL if no matching plugin exists
168 static struct GNUNET_BLOCK_PluginFunctions *
169 find_plugin (struct GNUNET_BLOCK_Context *ctx,
170 enum GNUNET_BLOCK_Type type)
172 struct Plugin *plugin;
176 for (i = 0; i < ctx->num_plugins; i++)
178 plugin = ctx->plugins[i];
180 while (0 != (plugin->api->types[j]))
182 if (type == plugin->api->types[j])
192 * Function called to validate a reply or a request. For
193 * request evaluation, simply pass "NULL" for the reply_block.
194 * Note that it is assumed that the reply has already been
195 * matched to the key (and signatures checked) as it would
196 * be done with the "get_key" function.
198 * @param ctx block contxt
199 * @param type block type
200 * @param query original query (hash)
201 * @param bf pointer to bloom filter associated with query; possibly updated (!)
202 * @param bf_mutator mutation value for @a bf
203 * @param xquery extended query data (can be NULL, depending on type)
204 * @param xquery_size number of bytes in @a xquery
205 * @param reply_block response to validate
206 * @param reply_block_size number of bytes in @a reply_block
207 * @return characterization of result
209 enum GNUNET_BLOCK_EvaluationResult
210 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
211 enum GNUNET_BLOCK_Type type,
212 const struct GNUNET_HashCode * query,
213 struct GNUNET_CONTAINER_BloomFilter **bf,
214 int32_t bf_mutator, const void *xquery,
215 size_t xquery_size, const void *reply_block,
216 size_t reply_block_size)
218 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
221 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
222 return plugin->evaluate (plugin->cls, type, query, bf, bf_mutator, xquery,
223 xquery_size, reply_block, reply_block_size);
228 * Function called to obtain the key for a block.
230 * @param ctx block context
231 * @param type block type
232 * @param block block to get the key for
233 * @param block_size number of bytes in @a block
234 * @param key set to the key (query) for the given block
235 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
236 * (or if extracting a key from a block of this type does not work)
239 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
240 enum GNUNET_BLOCK_Type type, const void *block,
241 size_t block_size, struct GNUNET_HashCode * key)
243 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
246 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
247 return plugin->get_key (plugin->cls, type, block, block_size, key);
252 * How many bytes should a bloomfilter be if we have already seen
253 * entry_count responses? Note that GNUNET_CONSTANTS_BLOOMFILTER_K gives us the number
254 * of bits set per entry. Furthermore, we should not re-size the
255 * filter too often (to keep it cheap).
257 * Since other peers will also add entries but not resize the filter,
258 * we should generally pick a slightly larger size than what the
259 * strict math would suggest.
261 * @param entry_count expected number of entries in the Bloom filter
262 * @return must be a power of two and smaller or equal to 2^15.
265 compute_bloomfilter_size (unsigned int entry_count)
268 unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
269 uint16_t max = 1 << 15;
271 if (entry_count > max)
274 while ((size < max) && (size < ideal))
283 * Construct a bloom filter that would filter out the given
286 * @param bf_mutator mutation value to use
287 * @param seen_results results already seen
288 * @param seen_results_count number of entries in @a seen_results
289 * @return NULL if seen_results_count is 0, otherwise a BF
290 * that would match the given results.
292 struct GNUNET_CONTAINER_BloomFilter *
293 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
294 const struct GNUNET_HashCode * seen_results,
295 unsigned int seen_results_count)
297 struct GNUNET_CONTAINER_BloomFilter *bf;
298 struct GNUNET_HashCode mhash;
302 nsize = compute_bloomfilter_size (seen_results_count);
303 bf = GNUNET_CONTAINER_bloomfilter_init (NULL, nsize,
304 GNUNET_CONSTANTS_BLOOMFILTER_K);
305 for (i = 0; i < seen_results_count; i++)
307 GNUNET_BLOCK_mingle_hash (&seen_results[i], bf_mutator, &mhash);
308 GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);