diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-09-12 13:08:54 +0000 |
commit | a42b4e0ed22b65631caea4bb456f3d8fc21f11b1 (patch) | |
tree | a3a1e30a7f8e7f8b30c75c96923249831a7e8b2e /src/block/block.c | |
parent | dd253b7f7591f0869f8ea14ee111b7d3b3e480b6 (diff) | |
download | gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.tar.gz gnunet-a42b4e0ed22b65631caea4bb456f3d8fc21f11b1.zip |
towards pluggable block library
Diffstat (limited to 'src/block/block.c')
-rw-r--r-- | src/block/block.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/block/block.c b/src/block/block.c index 72dedd7be..cb6837499 100644 --- a/src/block/block.c +++ b/src/block/block.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "gnunet_util_lib.h" | 27 | #include "gnunet_util_lib.h" |
28 | #include "gnunet_signatures.h" | 28 | #include "gnunet_signatures.h" |
29 | #include "gnunet_block_lib.h" | 29 | #include "gnunet_block_lib.h" |
30 | #include "plugin_block.h" | ||
30 | 31 | ||
31 | /** | 32 | /** |
32 | * Check if the given KBlock is well-formed. | 33 | * Check if the given KBlock is well-formed. |
@@ -226,5 +227,185 @@ GNUNET_BLOCK_check_block (enum GNUNET_BLOCK_Type type, | |||
226 | return GNUNET_OK; | 227 | return GNUNET_OK; |
227 | } | 228 | } |
228 | 229 | ||
230 | /* ***************** NEW API ******************* */ | ||
231 | |||
232 | /** | ||
233 | * Handle for a plugin. | ||
234 | */ | ||
235 | struct Plugin | ||
236 | { | ||
237 | /** | ||
238 | * Name of the shared library. | ||
239 | */ | ||
240 | char *library_name; | ||
241 | |||
242 | /** | ||
243 | * Plugin API. | ||
244 | */ | ||
245 | struct GNUNET_BLOCK_PluginFunctions *api; | ||
246 | }; | ||
247 | |||
248 | /** | ||
249 | * Handle to an initialized block library. | ||
250 | */ | ||
251 | struct GNUNET_BLOCK_Context | ||
252 | { | ||
253 | /** | ||
254 | * NULL-terminated array of our plugins. | ||
255 | */ | ||
256 | struct Plugin **plugins; | ||
257 | |||
258 | /** | ||
259 | * Our configuration. | ||
260 | */ | ||
261 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
262 | }; | ||
263 | |||
264 | |||
265 | /** | ||
266 | * Create a block context. Loads the block plugins. | ||
267 | * | ||
268 | * @param cfg configuration to use | ||
269 | * @return NULL on error | ||
270 | */ | ||
271 | struct GNUNET_BLOCK_Context * | ||
272 | GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
273 | { | ||
274 | struct GNUNET_BLOCK_Context *ctx; | ||
275 | unsigned int num_plugins; | ||
276 | |||
277 | ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context)); | ||
278 | ctx->cfg = cfg; | ||
279 | num_plugins = 0; | ||
280 | /* FIXME: actually load plugins... */ | ||
281 | GNUNET_array_append (ctx->plugins, | ||
282 | num_plugins, | ||
283 | NULL); | ||
284 | return ctx; | ||
285 | } | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Destroy the block context. | ||
290 | * | ||
291 | * @param ctx context to destroy | ||
292 | */ | ||
293 | void | ||
294 | GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx) | ||
295 | { | ||
296 | unsigned int i; | ||
297 | struct Plugin *plugin; | ||
298 | |||
299 | i = 0; | ||
300 | while (NULL != (plugin = ctx->plugins[i])) | ||
301 | { | ||
302 | GNUNET_break (NULL == | ||
303 | GNUNET_PLUGIN_unload (plugin->library_name, | ||
304 | plugin->api)); | ||
305 | GNUNET_free (plugin->library_name); | ||
306 | GNUNET_free (plugin); | ||
307 | i++; | ||
308 | } | ||
309 | GNUNET_free (ctx->plugins); | ||
310 | GNUNET_free (ctx); | ||
311 | } | ||
312 | |||
313 | |||
314 | /** | ||
315 | * Find a plugin for the given type. | ||
316 | * | ||
317 | * @param ctx context to search | ||
318 | * @param type type to look for | ||
319 | * @return NULL if no matching plugin exists | ||
320 | */ | ||
321 | static struct GNUNET_BLOCK_PluginFunctions * | ||
322 | find_plugin (struct GNUNET_BLOCK_Context *ctx, | ||
323 | enum GNUNET_BLOCK_Type type) | ||
324 | { | ||
325 | struct Plugin *plugin; | ||
326 | unsigned int i; | ||
327 | unsigned int j; | ||
328 | |||
329 | i = 0; | ||
330 | while (NULL != (plugin = ctx->plugins[i])) | ||
331 | { | ||
332 | j = 0; | ||
333 | while (0 != (plugin->api->types[j])) | ||
334 | { | ||
335 | if (type == plugin->api->types[j]) | ||
336 | return plugin->api; | ||
337 | j++; | ||
338 | } | ||
339 | i++; | ||
340 | } | ||
341 | return NULL; | ||
342 | } | ||
343 | |||
344 | |||
345 | /** | ||
346 | * Function called to validate a reply or a request. For | ||
347 | * request evaluation, simply pass "NULL" for the reply_block. | ||
348 | * Note that it is assumed that the reply has already been | ||
349 | * matched to the key (and signatures checked) as it would | ||
350 | * be done with the "get_key" function. | ||
351 | * | ||
352 | * @param ctx block contxt | ||
353 | * @param type block type | ||
354 | * @param query original query (hash) | ||
355 | * @param bf pointer to bloom filter associated with query; possibly updated (!) | ||
356 | * @param bf_mutator mutation value for bf | ||
357 | * @param xquery extrended query data (can be NULL, depending on type) | ||
358 | * @param xquery_size number of bytes in xquery | ||
359 | * @param reply_block response to validate | ||
360 | * @param reply_block_size number of bytes in reply block | ||
361 | * @return characterization of result | ||
362 | */ | ||
363 | enum GNUNET_BLOCK_EvaluationResult | ||
364 | GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx, | ||
365 | enum GNUNET_BLOCK_Type type, | ||
366 | const GNUNET_HashCode *query, | ||
367 | struct GNUNET_CONTAINER_BloomFilter **bf, | ||
368 | int32_t bf_mutator, | ||
369 | const void *xquery, | ||
370 | size_t xquery_size, | ||
371 | const void *reply_block, | ||
372 | size_t reply_block_size) | ||
373 | { | ||
374 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type); | ||
375 | |||
376 | if (plugin == NULL) | ||
377 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
378 | return plugin->evaluate (plugin->cls, | ||
379 | type, query, bf, bf_mutator, | ||
380 | xquery, xquery_size, reply_block, reply_block_size); | ||
381 | } | ||
382 | |||
383 | |||
384 | /** | ||
385 | * Function called to obtain the key for a block. | ||
386 | * | ||
387 | * @param ctx block context | ||
388 | * @param type block type | ||
389 | * @param block block to get the key for | ||
390 | * @param block_size number of bytes in block | ||
391 | * @param key set to the key (query) for the given block | ||
392 | * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported | ||
393 | * (or if extracting a key from a block of this type does not work) | ||
394 | */ | ||
395 | int | ||
396 | GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx, | ||
397 | enum GNUNET_BLOCK_Type type, | ||
398 | const void *block, | ||
399 | size_t block_size, | ||
400 | GNUNET_HashCode *key) | ||
401 | { | ||
402 | struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type); | ||
403 | |||
404 | if (plugin == NULL) | ||
405 | return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED; | ||
406 | return plugin->get_key (plugin->cls, | ||
407 | type, block, block_size, key); | ||
408 | } | ||
409 | |||
229 | 410 | ||
230 | /* end of block.c */ | 411 | /* end of block.c */ |