- fix
[oweals/gnunet.git] / src / block / block.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file block/block.c
23  * @brief library for data block manipulation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
32
33
34 /**
35  * Handle for a plugin.
36  */
37 struct Plugin
38 {
39   /**
40    * Name of the shared library.
41    */
42   char *library_name;
43
44   /**
45    * Plugin API.
46    */
47   struct GNUNET_BLOCK_PluginFunctions *api;
48 };
49
50
51 /**
52  * Handle to an initialized block library.
53  */
54 struct GNUNET_BLOCK_Context
55 {
56   /**
57    * Array of our plugins.
58    */
59   struct Plugin **plugins;
60
61   /**
62    * Size of the 'plugins' array.
63    */
64   unsigned int num_plugins;
65
66   /**
67    * Our configuration.
68    */
69   const struct GNUNET_CONFIGURATION_Handle *cfg;
70 };
71
72
73 /**
74  * Mingle hash with the mingle_number to produce different bits.
75  *
76  * @param in original hash code
77  * @param mingle_number number for hash permutation
78  * @param hc where to store the result.
79  */
80 void
81 GNUNET_BLOCK_mingle_hash (const struct GNUNET_HashCode * in, uint32_t mingle_number,
82                           struct GNUNET_HashCode * hc)
83 {
84   struct GNUNET_HashCode m;
85
86   GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
87   GNUNET_CRYPTO_hash_xor (&m, in, hc);
88 }
89
90
91 /**
92  * Add a plugin to the list managed by the block library.
93  *
94  * @param cls the block context
95  * @param library_name name of the plugin
96  * @param lib_ret the plugin API
97  */
98 static void
99 add_plugin (void *cls, const char *library_name, void *lib_ret)
100 {
101   struct GNUNET_BLOCK_Context *ctx = cls;
102   struct GNUNET_BLOCK_PluginFunctions *api = lib_ret;
103   struct Plugin *plugin;
104
105   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Loading block plugin `%s'\n"),
106               library_name);
107   plugin = GNUNET_malloc (sizeof (struct Plugin));
108   plugin->api = api;
109   plugin->library_name = GNUNET_strdup (library_name);
110   GNUNET_array_append (ctx->plugins, ctx->num_plugins, plugin);
111 }
112
113
114
115 /**
116  * Create a block context.  Loads the block plugins.
117  *
118  * @param cfg configuration to use
119  * @return NULL on error
120  */
121 struct GNUNET_BLOCK_Context *
122 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
123 {
124   struct GNUNET_BLOCK_Context *ctx;
125
126   ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
127   ctx->cfg = cfg;
128   GNUNET_PLUGIN_load_all ("libgnunet_plugin_block_", NULL, &add_plugin, ctx);
129   return ctx;
130 }
131
132
133 /**
134  * Destroy the block context.
135  *
136  * @param ctx context to destroy
137  */
138 void
139 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
140 {
141   unsigned int i;
142   struct Plugin *plugin;
143
144   for (i = 0; i < ctx->num_plugins; i++)
145   {
146     plugin = ctx->plugins[i];
147     GNUNET_break (NULL ==
148                   GNUNET_PLUGIN_unload (plugin->library_name, plugin->api));
149     GNUNET_free (plugin->library_name);
150     GNUNET_free (plugin);
151   }
152   GNUNET_free (ctx->plugins);
153   GNUNET_free (ctx);
154 }
155
156
157 /**
158  * Find a plugin for the given type.
159  *
160  * @param ctx context to search
161  * @param type type to look for
162  * @return NULL if no matching plugin exists
163  */
164 static struct GNUNET_BLOCK_PluginFunctions *
165 find_plugin (struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
166 {
167   struct Plugin *plugin;
168   unsigned int i;
169   unsigned int j;
170
171   for (i = 0; i < ctx->num_plugins; i++)
172   {
173     plugin = ctx->plugins[i];
174     j = 0;
175     while (0 != (plugin->api->types[j]))
176     {
177       if (type == plugin->api->types[j])
178         return plugin->api;
179       j++;
180     }
181   }
182   return NULL;
183 }
184
185
186 /**
187  * Function called to validate a reply or a request.  For
188  * request evaluation, simply pass "NULL" for the reply_block.
189  * Note that it is assumed that the reply has already been
190  * matched to the key (and signatures checked) as it would
191  * be done with the "get_key" function.
192  *
193  * @param ctx block contxt
194  * @param type block type
195  * @param query original query (hash)
196  * @param bf pointer to bloom filter associated with query; possibly updated (!)
197  * @param bf_mutator mutation value for bf
198  * @param xquery extended query data (can be NULL, depending on type)
199  * @param xquery_size number of bytes in xquery
200  * @param reply_block response to validate
201  * @param reply_block_size number of bytes in reply block
202  * @return characterization of result
203  */
204 enum GNUNET_BLOCK_EvaluationResult
205 GNUNET_BLOCK_evaluate (struct GNUNET_BLOCK_Context *ctx,
206                        enum GNUNET_BLOCK_Type type,
207                        const struct GNUNET_HashCode * query,
208                        struct GNUNET_CONTAINER_BloomFilter **bf,
209                        int32_t bf_mutator, const void *xquery,
210                        size_t xquery_size, const void *reply_block,
211                        size_t reply_block_size)
212 {
213   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
214
215   if (plugin == NULL)
216     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
217   return plugin->evaluate (plugin->cls, type, query, bf, bf_mutator, xquery,
218                            xquery_size, reply_block, reply_block_size);
219 }
220
221
222 /**
223  * Function called to obtain the key for a block.
224  *
225  * @param ctx block context
226  * @param type block type
227  * @param block block to get the key for
228  * @param block_size number of bytes in block
229  * @param key set to the key (query) for the given block
230  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
231  *         (or if extracting a key from a block of this type does not work)
232  */
233 int
234 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
235                       enum GNUNET_BLOCK_Type type, const void *block,
236                       size_t block_size, struct GNUNET_HashCode * key)
237 {
238   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
239
240   if (plugin == NULL)
241     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
242   return plugin->get_key (plugin->cls, type, block, block_size, key);
243 }
244
245
246 /**
247  * How many bytes should a bloomfilter be if we have already seen
248  * entry_count responses?  Note that GNUNET_CONSTANTS_BLOOMFILTER_K gives us the number
249  * of bits set per entry.  Furthermore, we should not re-size the
250  * filter too often (to keep it cheap).
251  *
252  * Since other peers will also add entries but not resize the filter,
253  * we should generally pick a slightly larger size than what the
254  * strict math would suggest.
255  *
256  * @return must be a power of two and smaller or equal to 2^15.
257  */
258 static size_t
259 compute_bloomfilter_size (unsigned int entry_count)
260 {
261   size_t size;
262   unsigned int ideal = (entry_count * GNUNET_CONSTANTS_BLOOMFILTER_K) / 4;
263   uint16_t max = 1 << 15;
264
265   if (entry_count > max)
266     return max;
267   size = 8;
268   while ((size < max) && (size < ideal))
269     size *= 2;
270   if (size > max)
271     return max;
272   return size;
273 }
274
275
276 /**
277  * Construct a bloom filter that would filter out the given
278  * results.
279  *
280  * @param bf_mutator mutation value to use
281  * @param seen_results results already seen
282  * @param seen_results_count number of entries in 'seen_results'
283  * @return NULL if seen_results_count is 0, otherwise a BF
284  *         that would match the given results.
285  */
286 struct GNUNET_CONTAINER_BloomFilter *
287 GNUNET_BLOCK_construct_bloomfilter (int32_t bf_mutator,
288                                     const struct GNUNET_HashCode * seen_results,
289                                     unsigned int seen_results_count)
290 {
291   struct GNUNET_CONTAINER_BloomFilter *bf;
292   struct GNUNET_HashCode mhash;
293   unsigned int i;
294   size_t nsize;
295
296   nsize = compute_bloomfilter_size (seen_results_count);
297   bf = GNUNET_CONTAINER_bloomfilter_init (NULL, nsize,
298                                           GNUNET_CONSTANTS_BLOOMFILTER_K);
299   for (i = 0; i < seen_results_count; i++)
300   {
301     GNUNET_BLOCK_mingle_hash (&seen_results[i], bf_mutator, &mhash);
302     GNUNET_CONTAINER_bloomfilter_add (bf, &mhash);
303   }
304   return bf;
305 }
306
307
308 /* end of block.c */