plugin_block_test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  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
  21. */
  22. #include "platform.h"
  23. #include "gnunet_block_plugin.h"
  24. #include "gnunet_block_group_lib.h"
  25. /**
  26. * Number of bits we set per entry in the bloomfilter.
  27. * Do not change!
  28. */
  29. #define BLOOMFILTER_K 16
  30. /**
  31. * How big is the BF we use for DHT blocks?
  32. */
  33. #define TEST_BF_SIZE 8
  34. /**
  35. * Create a new block group.
  36. *
  37. * @param ctx block context in which the block group is created
  38. * @param type type of the block for which we are creating the group
  39. * @param nonce random value used to seed the group creation
  40. * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
  41. * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
  42. * @param va variable arguments specific to @a type
  43. * @return block group handle, NULL if block groups are not supported
  44. * by this @a type of block (this is not an error)
  45. */
  46. static struct GNUNET_BLOCK_Group *
  47. block_plugin_test_create_group (void *cls,
  48. enum GNUNET_BLOCK_Type type,
  49. uint32_t nonce,
  50. const void *raw_data,
  51. size_t raw_data_size,
  52. va_list va)
  53. {
  54. unsigned int bf_size;
  55. const char *guard;
  56. guard = va_arg (va, const char *);
  57. if (0 == strcmp (guard,
  58. "seen-set-size"))
  59. bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned
  60. int),
  61. BLOOMFILTER_K);
  62. else if (0 == strcmp (guard,
  63. "filter-size"))
  64. bf_size = va_arg (va, unsigned int);
  65. else
  66. {
  67. GNUNET_break (0);
  68. bf_size = TEST_BF_SIZE;
  69. }
  70. GNUNET_break (NULL == va_arg (va, const char *));
  71. return GNUNET_BLOCK_GROUP_bf_create (cls,
  72. bf_size,
  73. BLOOMFILTER_K,
  74. type,
  75. nonce,
  76. raw_data,
  77. raw_data_size);
  78. }
  79. /**
  80. * Function called to validate a reply or a request. For
  81. * request evaluation, simply pass "NULL" for the reply_block.
  82. *
  83. * @param cls closure
  84. * @param ctx block context
  85. * @param type block type
  86. * @param group group to check against
  87. * @param eo control flags
  88. * @param query original query (hash)
  89. * @param xquery extrended query data (can be NULL, depending on type)
  90. * @param xquery_size number of bytes in @a xquery
  91. * @param reply_block response to validate
  92. * @param reply_block_size number of bytes in @a reply_block
  93. * @return characterization of result
  94. */
  95. static enum GNUNET_BLOCK_EvaluationResult
  96. block_plugin_test_evaluate (void *cls,
  97. struct GNUNET_BLOCK_Context *ctx,
  98. enum GNUNET_BLOCK_Type type,
  99. struct GNUNET_BLOCK_Group *group,
  100. enum GNUNET_BLOCK_EvaluationOptions eo,
  101. const struct GNUNET_HashCode *query,
  102. const void *xquery,
  103. size_t xquery_size,
  104. const void *reply_block,
  105. size_t reply_block_size)
  106. {
  107. struct GNUNET_HashCode chash;
  108. if (GNUNET_BLOCK_TYPE_TEST != type)
  109. {
  110. GNUNET_break (0);
  111. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  112. }
  113. if (0 != xquery_size)
  114. {
  115. GNUNET_break_op (0);
  116. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  117. }
  118. if (NULL == reply_block)
  119. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  120. GNUNET_CRYPTO_hash (reply_block,
  121. reply_block_size,
  122. &chash);
  123. if (GNUNET_YES ==
  124. GNUNET_BLOCK_GROUP_bf_test_and_set (group,
  125. &chash))
  126. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  127. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  128. }
  129. /**
  130. * Function called to obtain the key for a block.
  131. *
  132. * @param cls closure
  133. * @param type block type
  134. * @param block block to get the key for
  135. * @param block_size number of bytes in @a block
  136. * @param key set to the key (query) for the given block
  137. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  138. * (or if extracting a key from a block of this type does not work)
  139. */
  140. static int
  141. block_plugin_test_get_key (void *cls,
  142. enum GNUNET_BLOCK_Type type,
  143. const void *block,
  144. size_t block_size,
  145. struct GNUNET_HashCode *key)
  146. {
  147. /* always fails since there is no fixed relationship between
  148. * keys and values for test values */
  149. return GNUNET_SYSERR;
  150. }
  151. /**
  152. * Entry point for the plugin.
  153. *
  154. * @param cls NULL
  155. * @return the exported block API
  156. */
  157. void *
  158. libgnunet_plugin_block_test_init (void *cls)
  159. {
  160. static enum GNUNET_BLOCK_Type types[] = {
  161. GNUNET_BLOCK_TYPE_TEST,
  162. GNUNET_BLOCK_TYPE_ANY /* end of list */
  163. };
  164. struct GNUNET_BLOCK_PluginFunctions *api;
  165. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  166. api->evaluate = &block_plugin_test_evaluate;
  167. api->get_key = &block_plugin_test_get_key;
  168. api->create_group = &block_plugin_test_create_group;
  169. api->types = types;
  170. return api;
  171. }
  172. /**
  173. * Exit point from the plugin.
  174. *
  175. * @param cls the return value from #libgnunet_plugin_block_test_init
  176. * @return NULL
  177. */
  178. void *
  179. libgnunet_plugin_block_test_done (void *cls)
  180. {
  181. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  182. GNUNET_free (api);
  183. return NULL;
  184. }
  185. /* end of plugin_block_test.c */