plugin_block_test.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 int),
  60. BLOOMFILTER_K);
  61. else if (0 == strcmp (guard,
  62. "filter-size"))
  63. bf_size = va_arg (va, unsigned int);
  64. else
  65. {
  66. GNUNET_break (0);
  67. bf_size = TEST_BF_SIZE;
  68. }
  69. GNUNET_break (NULL == va_arg (va, const char *));
  70. return GNUNET_BLOCK_GROUP_bf_create (cls,
  71. bf_size,
  72. BLOOMFILTER_K,
  73. type,
  74. nonce,
  75. raw_data,
  76. raw_data_size);
  77. }
  78. /**
  79. * Function called to validate a reply or a request. For
  80. * request evaluation, simply pass "NULL" for the reply_block.
  81. *
  82. * @param cls closure
  83. * @param ctx block context
  84. * @param type block type
  85. * @param group group to check against
  86. * @param eo control flags
  87. * @param query original query (hash)
  88. * @param xquery extrended query data (can be NULL, depending on type)
  89. * @param xquery_size number of bytes in @a xquery
  90. * @param reply_block response to validate
  91. * @param reply_block_size number of bytes in @a reply_block
  92. * @return characterization of result
  93. */
  94. static enum GNUNET_BLOCK_EvaluationResult
  95. block_plugin_test_evaluate (void *cls,
  96. struct GNUNET_BLOCK_Context *ctx,
  97. enum GNUNET_BLOCK_Type type,
  98. struct GNUNET_BLOCK_Group *group,
  99. enum GNUNET_BLOCK_EvaluationOptions eo,
  100. const struct GNUNET_HashCode *query,
  101. const void *xquery,
  102. size_t xquery_size,
  103. const void *reply_block,
  104. size_t reply_block_size)
  105. {
  106. struct GNUNET_HashCode chash;
  107. if ( GNUNET_BLOCK_TYPE_TEST != type)
  108. {
  109. GNUNET_break (0);
  110. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  111. }
  112. if (0 != xquery_size)
  113. {
  114. GNUNET_break_op (0);
  115. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  116. }
  117. if (NULL == reply_block)
  118. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  119. GNUNET_CRYPTO_hash (reply_block,
  120. reply_block_size,
  121. &chash);
  122. if (GNUNET_YES ==
  123. GNUNET_BLOCK_GROUP_bf_test_and_set (group,
  124. &chash))
  125. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  126. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  127. }
  128. /**
  129. * Function called to obtain the key for a block.
  130. *
  131. * @param cls closure
  132. * @param type block type
  133. * @param block block to get the key for
  134. * @param block_size number of bytes in @a block
  135. * @param key set to the key (query) for the given block
  136. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  137. * (or if extracting a key from a block of this type does not work)
  138. */
  139. static int
  140. block_plugin_test_get_key (void *cls,
  141. enum GNUNET_BLOCK_Type type,
  142. const void *block,
  143. size_t block_size,
  144. struct GNUNET_HashCode *key)
  145. {
  146. /* always fails since there is no fixed relationship between
  147. * keys and values for test values */
  148. return GNUNET_SYSERR;
  149. }
  150. /**
  151. * Entry point for the plugin.
  152. *
  153. * @param cls NULL
  154. * @return the exported block API
  155. */
  156. void *
  157. libgnunet_plugin_block_test_init (void *cls)
  158. {
  159. static enum GNUNET_BLOCK_Type types[] =
  160. {
  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 */