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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file block/plugin_block_test.c
18 * @brief block plugin to test the DHT as a simple key-value store;
19 * this plugin simply accepts any (new) response for any key
20 * @author Christian Grothoff
24 #include "gnunet_block_plugin.h"
25 #include "gnunet_block_group_lib.h"
28 * Number of bits we set per entry in the bloomfilter.
31 #define BLOOMFILTER_K 16
34 * How big is the BF we use for DHT blocks?
36 #define TEST_BF_SIZE 8
40 * Create a new block group.
42 * @param ctx block context in which the block group is created
43 * @param type type of the block for which we are creating the group
44 * @param nonce random value used to seed the group creation
45 * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
46 * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
47 * @param va variable arguments specific to @a type
48 * @return block group handle, NULL if block groups are not supported
49 * by this @a type of block (this is not an error)
51 static struct GNUNET_BLOCK_Group *
52 block_plugin_test_create_group (void *cls,
53 enum GNUNET_BLOCK_Type type,
62 guard = va_arg (va, const char *);
63 if (0 == strcmp (guard,
65 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
67 else if (0 == strcmp (guard,
69 bf_size = va_arg (va, unsigned int);
73 bf_size = TEST_BF_SIZE;
75 GNUNET_break (NULL == va_arg (va, const char *));
76 return GNUNET_BLOCK_GROUP_bf_create (cls,
87 * Function called to validate a reply or a request. For
88 * request evaluation, simply pass "NULL" for the reply_block.
91 * @param ctx block context
92 * @param type block type
93 * @param group group to check against
94 * @param eo control flags
95 * @param query original query (hash)
96 * @param xquery extrended query data (can be NULL, depending on type)
97 * @param xquery_size number of bytes in @a xquery
98 * @param reply_block response to validate
99 * @param reply_block_size number of bytes in @a reply_block
100 * @return characterization of result
102 static enum GNUNET_BLOCK_EvaluationResult
103 block_plugin_test_evaluate (void *cls,
104 struct GNUNET_BLOCK_Context *ctx,
105 enum GNUNET_BLOCK_Type type,
106 struct GNUNET_BLOCK_Group *group,
107 enum GNUNET_BLOCK_EvaluationOptions eo,
108 const struct GNUNET_HashCode *query,
111 const void *reply_block,
112 size_t reply_block_size)
114 struct GNUNET_HashCode chash;
116 if ( GNUNET_BLOCK_TYPE_TEST != type)
119 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
121 if (0 != xquery_size)
124 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
126 if (NULL == reply_block)
127 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
128 GNUNET_CRYPTO_hash (reply_block,
132 GNUNET_BLOCK_GROUP_bf_test_and_set (group,
134 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
135 return GNUNET_BLOCK_EVALUATION_OK_MORE;
140 * Function called to obtain the key for a block.
143 * @param type block type
144 * @param block block to get the key for
145 * @param block_size number of bytes in @a block
146 * @param key set to the key (query) for the given block
147 * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
148 * (or if extracting a key from a block of this type does not work)
151 block_plugin_test_get_key (void *cls,
152 enum GNUNET_BLOCK_Type type,
155 struct GNUNET_HashCode *key)
157 /* always fails since there is no fixed relationship between
158 * keys and values for test values */
159 return GNUNET_SYSERR;
164 * Entry point for the plugin.
167 * @return the exported block API
170 libgnunet_plugin_block_test_init (void *cls)
172 static enum GNUNET_BLOCK_Type types[] =
174 GNUNET_BLOCK_TYPE_TEST,
175 GNUNET_BLOCK_TYPE_ANY /* end of list */
177 struct GNUNET_BLOCK_PluginFunctions *api;
179 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
180 api->evaluate = &block_plugin_test_evaluate;
181 api->get_key = &block_plugin_test_get_key;
182 api->create_group = &block_plugin_test_create_group;
189 * Exit point from the plugin.
191 * @param cls the return value from #libgnunet_plugin_block_test_init
195 libgnunet_plugin_block_test_done (void *cls)
197 struct GNUNET_BLOCK_PluginFunctions *api = cls;
203 /* end of plugin_block_test.c */