2 This file is part of GNUnet.
3 Copyright (C) 2010 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.
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 * Serialize state of a block group.
164 * @param bg group to serialize
165 * @param[out] nonce set to the nonce of the @a bg
166 * @param[out] raw_data set to the serialized state
167 * @param[out] raw_data_size set to the number of bytes in @a raw_data
168 * @return #GNUNET_OK on success, #GNUNET_NO if serialization is not
169 * supported, #GNUNET_SYSERR on error
172 GNUNET_BLOCK_group_serialize (struct GNUNET_BLOCK_Group *bg,
175 size_t *raw_data_size)
182 if (NULL == bg->serialize_cb)
184 return bg->serialize_cb (bg,
192 * Destroy resources used by a block group.
194 * @param bg group to destroy, NULL is allowed
197 GNUNET_BLOCK_group_destroy (struct GNUNET_BLOCK_Group *bg)
206 * Try merging two block groups. Afterwards, @a bg1 should remain
207 * valid and contain the rules from both @a bg1 and @bg2, and
208 * @a bg2 should be destroyed (as part of this call). The latter
209 * should happen even if merging is not supported.
211 * @param[in,out] bg1 first group to merge, is updated
212 * @param bg2 second group to merge, is destroyed
213 * @return #GNUNET_OK on success,
214 * #GNUNET_NO if merge failed due to different nonce
215 * #GNUNET_SYSERR if merging is not supported
218 GNUNET_BLOCK_group_merge (struct GNUNET_BLOCK_Group *bg1,
219 struct GNUNET_BLOCK_Group *bg2)
227 bg2->destroy_cb (bg2);
230 if (NULL == bg1->merge_cb)
231 return GNUNET_SYSERR;
232 GNUNET_assert (bg1->merge_cb == bg1->merge_cb);
233 ret = bg1->merge_cb (bg1,
235 bg2->destroy_cb (bg2);
241 * Find a plugin for the given type.
243 * @param ctx context to search
244 * @param type type to look for
245 * @return NULL if no matching plugin exists
247 static struct GNUNET_BLOCK_PluginFunctions *
248 find_plugin (struct GNUNET_BLOCK_Context *ctx,
249 enum GNUNET_BLOCK_Type type)
251 struct Plugin *plugin;
255 for (i = 0; i < ctx->num_plugins; i++)
257 plugin = ctx->plugins[i];
259 while (0 != (plugin->api->types[j]))
261 if (type == plugin->api->types[j])
271 * Create a new block group.
273 * @param ctx block context in which the block group is created
274 * @param type type of the block for which we are creating the group
275 * @param nonce random value used to seed the group creation
276 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
277 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
278 * @return block group handle, NULL if block groups are not supported
279 * by this @a type of block (this is not an error)
281 struct GNUNET_BLOCK_Group *
282 GNUNET_BLOCK_group_create (struct GNUNET_BLOCK_Context *ctx,
283 enum GNUNET_BLOCK_Type type,
285 const void *raw_data,
286 size_t raw_data_size,
289 struct GNUNET_BLOCK_PluginFunctions *plugin;
290 struct GNUNET_BLOCK_Group *bg;
293 plugin = find_plugin (ctx,
295 if (NULL == plugin->create_group)
297 va_start (ap, raw_data_size);
298 bg = plugin->create_group (plugin->cls,
310 * Function called to validate a reply or a request. For
311 * request evaluation, simply pass "NULL" for the reply_block.
312 * Note that it is assumed that the reply has already been
313 * matched to the key (and signatures checked) as it would
314 * be done with the "get_key" function.
316 * @param ctx block contxt
317 * @param type block type
318 * @param block block group to use
319 * @param eo control flags
320 * @param query original query (hash)
321 * @param xquery extended query data (can be NULL, depending on type)
322 * @param xquery_size number of bytes in @a xquery
323 * @param reply_block response to validate
324 * @param reply_block_size number of bytes in @a reply_block
325 * @return characterization of result
327 enum GNUNET_BLOCK_EvaluationResult
328 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
329 enum GNUNET_BLOCK_Type type,
330 struct GNUNET_BLOCK_Group *group,
331 enum GNUNET_BLOCK_EvaluationOptions eo,
332 const struct GNUNET_HashCode *query,
335 const void *reply_block,
336 size_t reply_block_size)
338 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
342 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
343 return plugin->evaluate (plugin->cls,
356 * Function called to obtain the key for a block.
358 * @param ctx block context
359 * @param type block type
360 * @param block block to get the key for
361 * @param block_size number of bytes in @a block
362 * @param key set to the key (query) for the given block
363 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
364 * (or if extracting a key from a block of this type does not work)
367 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
368 enum GNUNET_BLOCK_Type type,
371 struct GNUNET_HashCode *key)
373 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx,
377 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
378 return plugin->get_key (plugin->cls, type, block, block_size, key);
383 * Update block group to filter out the given results. Note that the
384 * use of a hash for seen results implies that the caller magically
385 * knows how the specific block engine hashes for filtering
386 * duplicates, so this API may not always apply.
388 * @param bf_mutator mutation value to use
389 * @param seen_results results already seen
390 * @param seen_results_count number of entries in @a seen_results
391 * @return #GNUNET_SYSERR if not supported, #GNUNET_OK on success
394 GNUNET_BLOCK_group_set_seen (struct GNUNET_BLOCK_Group *bg,
395 const struct GNUNET_HashCode *seen_results,
396 unsigned int seen_results_count)
400 if (NULL == bg->mark_seen_cb)
401 return GNUNET_SYSERR;
402 bg->mark_seen_cb (bg,