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_signatures.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_block_plugin.h"
34 * Handle for a plugin.
39 * Name of the shared library.
46 struct GNUNET_BLOCK_PluginFunctions *api;
51 * Handle to an initialized block library.
53 struct GNUNET_BLOCK_Context
56 * NULL-terminated array of our plugins.
58 struct Plugin **plugins;
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Mingle hash with the mingle_number to produce different bits.
70 * @param in original hash code
71 * @param mingle_number number for hash permutation
72 * @param hc where to store the result.
75 GNUNET_BLOCK_mingle_hash (const GNUNET_HashCode * in, uint32_t mingle_number,
80 GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
81 GNUNET_CRYPTO_hash_xor (&m, in, hc);
86 * Create a block context. Loads the block plugins.
88 * @param cfg configuration to use
89 * @return NULL on error
91 struct GNUNET_BLOCK_Context *
92 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
94 struct GNUNET_BLOCK_Context *ctx;
95 struct GNUNET_BLOCK_PluginFunctions *api;
96 struct Plugin *plugin;
97 unsigned int num_plugins;
102 ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
106 GNUNET_CONFIGURATION_get_value_string (cfg, "block", "PLUGINS", &plugs))
108 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading block plugins `%s'\n"),
110 pos = strtok (plugs, " ");
113 GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
114 api = GNUNET_PLUGIN_load (libname, NULL);
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 _("Failed to load block plugin `%s'\n"), pos);
119 GNUNET_free (libname);
123 plugin = GNUNET_malloc (sizeof (struct Plugin));
125 plugin->library_name = libname;
126 GNUNET_array_append (ctx->plugins, num_plugins, plugin);
128 pos = strtok (NULL, " ");
132 GNUNET_array_append (ctx->plugins, num_plugins, NULL);
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;
149 while (NULL != (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);
157 GNUNET_free (ctx->plugins);
163 * Find a plugin for the given type.
165 * @param ctx context to search
166 * @param type type to look for
167 * @return NULL if no matching plugin exists
169 static struct GNUNET_BLOCK_PluginFunctions *
170 find_plugin (struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
172 struct Plugin *plugin;
177 while (NULL != (plugin = ctx->plugins[i]))
180 while (0 != (plugin->api->types[j]))
182 if (type == plugin->api->types[j])
193 * Function called to validate a reply or a request. For
194 * request evaluation, simply pass "NULL" for the reply_block.
195 * Note that it is assumed that the reply has already been
196 * matched to the key (and signatures checked) as it would
197 * be done with the "get_key" function.
199 * @param ctx block contxt
200 * @param type block type
201 * @param query original query (hash)
202 * @param bf pointer to bloom filter associated with query; possibly updated (!)
203 * @param bf_mutator mutation value for bf
204 * @param xquery extended query data (can be NULL, depending on type)
205 * @param xquery_size number of bytes in xquery
206 * @param reply_block response to validate
207 * @param reply_block_size number of bytes in reply block
208 * @return characterization of result
210 enum GNUNET_BLOCK_EvaluationResult
211 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
212 enum GNUNET_BLOCK_Type type,
213 const GNUNET_HashCode * query,
214 struct GNUNET_CONTAINER_BloomFilter **bf,
215 int32_t bf_mutator, const void *xquery,
216 size_t xquery_size, const void *reply_block,
217 size_t reply_block_size)
219 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
222 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
223 return plugin->evaluate (plugin->cls, type, query, bf, bf_mutator, xquery,
224 xquery_size, reply_block, reply_block_size);
229 * Function called to obtain the key for a block.
231 * @param ctx block context
232 * @param type block type
233 * @param block block to get the key for
234 * @param block_size number of bytes in block
235 * @param key set to the key (query) for the given block
236 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
237 * (or if extracting a key from a block of this type does not work)
240 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
241 enum GNUNET_BLOCK_Type type, const void *block,
242 size_t block_size, GNUNET_HashCode * key)
244 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
247 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
248 return plugin->get_key (plugin->cls, type, block, block_size, key);
254 * Construct a bloom filter that would filter out the given
257 * @param bf_mutator mutation value to use
258 * @param seen_results results already seen
259 * @param seen_results_count number of entries in 'seen_results'
260 * @return NULL if seen_results_count is 0, otherwise a BF
261 * that would match the given results.
263 struct GNUNET_CONTAINER_BloomFilter *
264 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
265 const GNUNET_HashCode *seen_results,
266 unsigned int seen_results_count)