bdef55aa3ab296c4193fa38c45b102861e35983c
[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_signatures.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_block_plugin.h"
31
32
33 /**
34  * Handle for a plugin.
35  */
36 struct Plugin
37 {
38   /**
39    * Name of the shared library.
40    */
41   char *library_name;
42
43   /**
44    * Plugin API.
45    */
46   struct GNUNET_BLOCK_PluginFunctions *api;
47 };
48
49
50 /**
51  * Handle to an initialized block library.
52  */
53 struct GNUNET_BLOCK_Context
54 {
55   /**
56    * NULL-terminated array of our plugins.
57    */
58   struct Plugin **plugins;
59
60   /**
61    * Our configuration.
62    */
63   const struct GNUNET_CONFIGURATION_Handle *cfg;
64 };
65
66
67 /**
68  * Mingle hash with the mingle_number to produce different bits.
69  * 
70  * @param in original hash code
71  * @param mingle_number number for hash permutation
72  * @param hc where to store the result.
73  */
74 void
75 GNUNET_BLOCK_mingle_hash (const GNUNET_HashCode * in,
76                           uint32_t mingle_number, GNUNET_HashCode * hc)
77 {
78   GNUNET_HashCode m;
79
80   GNUNET_CRYPTO_hash (&mingle_number, sizeof (uint32_t), &m);
81   GNUNET_CRYPTO_hash_xor (&m, in, hc);
82 }
83
84
85 /**
86  * Create a block context.  Loads the block plugins.
87  *
88  * @param cfg configuration to use
89  * @return NULL on error
90  */
91 struct GNUNET_BLOCK_Context *
92 GNUNET_BLOCK_context_create (const struct GNUNET_CONFIGURATION_Handle *cfg)
93 {
94   struct GNUNET_BLOCK_Context *ctx;
95   struct GNUNET_BLOCK_PluginFunctions *api;
96   struct Plugin *plugin;
97   unsigned int num_plugins;
98   char *plugs;
99   char *pos;
100   char *libname;
101
102   ctx = GNUNET_malloc (sizeof (struct GNUNET_BLOCK_Context));
103   ctx->cfg = cfg;
104   num_plugins = 0;
105   if (GNUNET_OK ==
106       GNUNET_CONFIGURATION_get_value_string (cfg, "block", "PLUGINS", &plugs))
107   {
108     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
109                 _("Loading block plugins `%s'\n"), plugs);
110     pos = strtok (plugs, " ");
111     while (pos != NULL)
112     {
113       GNUNET_asprintf (&libname, "libgnunet_plugin_block_%s", pos);
114       api = GNUNET_PLUGIN_load (libname, NULL);
115       if (api == NULL)
116       {
117         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118                     _("Failed to load block plugin `%s'\n"), pos);
119         GNUNET_free (libname);
120       }
121       else
122       {
123         plugin = GNUNET_malloc (sizeof (struct Plugin));
124         plugin->api = api;
125         plugin->library_name = libname;
126         GNUNET_array_append (ctx->plugins, num_plugins, plugin);
127       }
128       pos = strtok (NULL, " ");
129     }
130     GNUNET_free (plugs);
131   }
132   GNUNET_array_append (ctx->plugins, num_plugins, NULL);
133   return ctx;
134 }
135
136
137 /**
138  * Destroy the block context.
139  *
140  * @param ctx context to destroy
141  */
142 void
143 GNUNET_BLOCK_context_destroy (struct GNUNET_BLOCK_Context *ctx)
144 {
145   unsigned int i;
146   struct Plugin *plugin;
147
148   i = 0;
149   while (NULL != (plugin = ctx->plugins[i]))
150   {
151     GNUNET_break (NULL ==
152                   GNUNET_PLUGIN_unload (plugin->library_name, plugin->api));
153     GNUNET_free (plugin->library_name);
154     GNUNET_free (plugin);
155     i++;
156   }
157   GNUNET_free (ctx->plugins);
158   GNUNET_free (ctx);
159 }
160
161
162 /**
163  * Find a plugin for the given type.
164  *
165  * @param ctx context to search
166  * @param type type to look for
167  * @return NULL if no matching plugin exists
168  */
169 static struct GNUNET_BLOCK_PluginFunctions *
170 find_plugin (struct GNUNET_BLOCK_Context *ctx, enum GNUNET_BLOCK_Type type)
171 {
172   struct Plugin *plugin;
173   unsigned int i;
174   unsigned int j;
175
176   i = 0;
177   while (NULL != (plugin = ctx->plugins[i]))
178   {
179     j = 0;
180     while (0 != (plugin->api->types[j]))
181     {
182       if (type == plugin->api->types[j])
183         return plugin->api;
184       j++;
185     }
186     i++;
187   }
188   return NULL;
189 }
190
191
192 /**
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.
198  *
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
209  */
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,
216                        const void *xquery,
217                        size_t xquery_size,
218                        const void *reply_block, size_t reply_block_size)
219 {
220   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
221
222   if (plugin == NULL)
223     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
224   return plugin->evaluate (plugin->cls,
225                            type, query, bf, bf_mutator,
226                            xquery, xquery_size, reply_block, reply_block_size);
227 }
228
229
230 /**
231  * Function called to obtain the key for a block.
232  *
233  * @param ctx block context
234  * @param type block type
235  * @param block block to get the key for
236  * @param block_size number of bytes in block
237  * @param key set to the key (query) for the given block
238  * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
239  *         (or if extracting a key from a block of this type does not work)
240  */
241 int
242 GNUNET_BLOCK_get_key (struct GNUNET_BLOCK_Context *ctx,
243                       enum GNUNET_BLOCK_Type type,
244                       const void *block,
245                       size_t block_size, GNUNET_HashCode * key)
246 {
247   struct GNUNET_BLOCK_PluginFunctions *plugin = find_plugin (ctx, type);
248
249   if (plugin == NULL)
250     return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
251   return plugin->get_key (plugin->cls, type, block, block_size, key);
252 }
253
254
255 /* end of block.c */