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 * NULL-terminated array of our plugins.
59 struct Plugin **plugins;
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Mingle hash with the mingle_number to produce different bits.
71 * @param in original hash code
72 * @param mingle_number number for hash permutation
73 * @param hc where to store the result.
76 GNUNET_BLOCK_mingle_hash (const GNUNET_HashCode * in, uint32_t mingle_number,
81 GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
82 GNUNET_CRYPTO_hash_xor (&m, in, hc);
87 * Create a block context. Loads the block plugins.
89 * @param cfg configuration to use
90 * @return NULL on error
92 struct GNUNET_BLOCK_Context *
93 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
95 struct GNUNET_BLOCK_Context *ctx;
96 struct GNUNET_BLOCK_PluginFunctions *api;
97 struct Plugin *plugin;
98 unsigned int num_plugins;
103 ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
107 GNUNET_CONFIGURATION_get_value_string (cfg, "block", "PLUGINS", &plugs))
109 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading block plugins `%s'\n"),
111 pos = strtok (plugs, " ");
114 GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
115 api = GNUNET_PLUGIN_load (libname, NULL);
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 _("Failed to load block plugin `%s'\n"), pos);
120 GNUNET_free (libname);
124 plugin = GNUNET_malloc (sizeof (struct Plugin));
126 plugin->library_name = libname;
127 GNUNET_array_append (ctx->plugins, num_plugins, plugin);
129 pos = strtok (NULL, " ");
133 GNUNET_array_append (ctx->plugins, num_plugins, NULL);
139 * Destroy the block context.
141 * @param ctx context to destroy
144 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
147 struct Plugin *plugin;
150 while (NULL != (plugin = ctx->plugins[i]))
152 GNUNET_break (NULL ==
153 GNUNET_PLUGIN_unload (plugin->library_name, plugin->api));
154 GNUNET_free (plugin->library_name);
155 GNUNET_free (plugin);
158 GNUNET_free (ctx->plugins);
164 * Find a plugin for the given type.
166 * @param ctx context to search
167 * @param type type to look for
168 * @return NULL if no matching plugin exists
170 static struct GNUNET_BLOCK_PluginFunctions *
171 find_plugin (struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
173 struct Plugin *plugin;
178 while (NULL != (plugin = ctx->plugins[i]))
181 while (0 != (plugin->api->types[j]))
183 if (type == plugin->api->types[j])
194 * Function called to validate a reply or a request. For
195 * request evaluation, simply pass "NULL" for the reply_block.
196 * Note that it is assumed that the reply has already been
197 * matched to the key (and signatures checked) as it would
198 * be done with the "get_key" function.
200 * @param ctx block contxt
201 * @param type block type
202 * @param query original query (hash)
203 * @param bf pointer to bloom filter associated with query; possibly updated (!)
204 * @param bf_mutator mutation value for bf
205 * @param xquery extended query data (can be NULL, depending on type)
206 * @param xquery_size number of bytes in xquery
207 * @param reply_block response to validate
208 * @param reply_block_size number of bytes in reply block
209 * @return characterization of result
211 enum GNUNET_BLOCK_EvaluationResult
212 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
213 enum GNUNET_BLOCK_Type type,
214 const GNUNET_HashCode * query,
215 struct GNUNET_CONTAINER_BloomFilter **bf,
216 int32_t bf_mutator, const void *xquery,
217 size_t xquery_size, const void *reply_block,
218 size_t reply_block_size)
220 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
223 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
224 return plugin->evaluate (plugin->cls, type, query, bf, bf_mutator, xquery,
225 xquery_size, reply_block, reply_block_size);
230 * Function called to obtain the key for a block.
232 * @param ctx block context
233 * @param type block type
234 * @param block block to get the key for
235 * @param block_size number of bytes in block
236 * @param key set to the key (query) for the given block
237 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
238 * (or if extracting a key from a block of this type does not work)
241 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
242 enum GNUNET_BLOCK_Type type, const void *block,
243 size_t block_size, GNUNET_HashCode * key)
245 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
248 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
249 return plugin->get_key (plugin->cls, type, block, block_size, key);
254 * How many bytes should a bloomfilter be if we have already seen
255 * entry_count responses? Note that GNUNET_CONSTANTS_BLOOMFILTER_K gives us the number
256 * of bits set per entry. Furthermore, we should not re-size the
257 * filter too often (to keep it cheap).
259 * Since other peers will also add entries but not resize the filter,
260 * we should generally pick a slightly larger size than what the
261 * strict math would suggest.
263 * @return must be a power of two and smaller or equal to 2^15.
266 compute_bloomfilter_size (unsigned int entry_count)
269 unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
270 uint16_t max = 1 << 15;
272 if (entry_count > max)
275 while ((size < max) && (size < ideal))
284 * Construct a bloom filter that would filter out the given
287 * @param bf_mutator mutation value to use
288 * @param seen_results results already seen
289 * @param seen_results_count number of entries in 'seen_results'
290 * @return NULL if seen_results_count is 0, otherwise a BF
291 * that would match the given results.
293 struct GNUNET_CONTAINER_BloomFilter *
294 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
295 const GNUNET_HashCode *seen_results,
296 unsigned int seen_results_count)
298 struct GNUNET_CONTAINER_BloomFilter *bf;
299 GNUNET_HashCode mhash;
303 nsize = compute_bloomfilter_size (seen_results_count);
304 bf = GNUNET_CONTAINER_bloomfilter_init (NULL, nsize, 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);