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 "plugin_block.h"
34 * Handle for a plugin.
39 * Name of the shared library.
46 struct GNUNET_BLOCK_PluginFunctions *api;
50 * Handle to an initialized block library.
52 struct GNUNET_BLOCK_Context
55 * NULL-terminated array of our plugins.
57 struct Plugin **plugins;
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
67 * Create a block context. Loads the block plugins.
69 * @param cfg configuration to use
70 * @return NULL on error
72 struct GNUNET_BLOCK_Context *
73 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
75 struct GNUNET_BLOCK_Context *ctx;
76 struct GNUNET_BLOCK_PluginFunctions *api;
77 struct Plugin *plugin;
78 unsigned int num_plugins;
83 ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
87 GNUNET_CONFIGURATION_get_value_string (cfg,
88 "block", "PLUGINS", &plugs))
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91 _("Loading block plugins `%s'\n"), plugs);
92 pos = strtok (plugs, " ");
95 GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
96 api = GNUNET_PLUGIN_load (libname, NULL);
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100 _("Failed to load block plugin `%s'\n"),
102 GNUNET_free (libname);
106 plugin = GNUNET_malloc (sizeof (struct Plugin));
108 plugin->library_name = libname;
109 GNUNET_array_append (ctx->plugins,
113 pos = strtok (NULL, " ");
117 GNUNET_array_append (ctx->plugins,
125 * Destroy the block context.
127 * @param ctx context to destroy
130 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
133 struct Plugin *plugin;
136 while (NULL != (plugin = ctx->plugins[i]))
138 GNUNET_break (NULL ==
139 GNUNET_PLUGIN_unload (plugin->library_name,
141 GNUNET_free (plugin->library_name);
142 GNUNET_free (plugin);
145 GNUNET_free (ctx->plugins);
151 * Find a plugin for the given type.
153 * @param ctx context to search
154 * @param type type to look for
155 * @return NULL if no matching plugin exists
157 static struct GNUNET_BLOCK_PluginFunctions *
158 find_plugin (struct GNUNET_BLOCK_Context *ctx,
159 enum GNUNET_BLOCK_Type type)
161 struct Plugin *plugin;
166 while (NULL != (plugin = ctx->plugins[i]))
169 while (0 != (plugin->api->types[j]))
171 if (type == plugin->api->types[j])
182 * Function called to validate a reply or a request. For
183 * request evaluation, simply pass "NULL" for the reply_block.
184 * Note that it is assumed that the reply has already been
185 * matched to the key (and signatures checked) as it would
186 * be done with the "get_key" function.
188 * @param ctx block contxt
189 * @param type block type
190 * @param query original query (hash)
191 * @param bf pointer to bloom filter associated with query; possibly updated (!)
192 * @param bf_mutator mutation value for bf
193 * @param xquery extrended query data (can be NULL, depending on type)
194 * @param xquery_size number of bytes in xquery
195 * @param reply_block response to validate
196 * @param reply_block_size number of bytes in reply block
197 * @return characterization of result
199 enum GNUNET_BLOCK_EvaluationResult
200 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
201 enum GNUNET_BLOCK_Type type,
202 const GNUNET_HashCode *query,
203 struct GNUNET_CONTAINER_BloomFilter **bf,
207 const void *reply_block,
208 size_t reply_block_size)
210 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
213 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
214 return plugin->evaluate (plugin->cls,
215 type, query, bf, bf_mutator,
216 xquery, xquery_size, reply_block, reply_block_size);
221 * Function called to obtain the key for a block.
223 * @param ctx block context
224 * @param type block type
225 * @param block block to get the key for
226 * @param block_size number of bytes in block
227 * @param key set to the key (query) for the given block
228 * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
229 * (or if extracting a key from a block of this type does not work)
232 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
233 enum GNUNET_BLOCK_Type type,
236 GNUNET_HashCode *key)
238 struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
241 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
242 return plugin->get_key (plugin->cls,
243 type, block, block_size, key);