block.c 8.5 KB

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