2
0

plugin_block_template.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_template.c
  18. * @brief template for a block plugin
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_block_plugin.h"
  23. #include "gnunet_block_group_lib.h"
  24. #define DEBUG_TEMPLATE GNUNET_EXTRA_LOGGING
  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 TEMPLATE_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_template_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 = TEMPLATE_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 context
  85. * @param type block type
  86. * @param group block group to use
  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 xquery
  91. * @param reply_block response to validate
  92. * @param reply_block_size number of bytes in reply block
  93. * @return characterization of result
  94. */
  95. static enum GNUNET_BLOCK_EvaluationResult
  96. block_plugin_template_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 (NULL == reply_block)
  109. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  110. GNUNET_CRYPTO_hash (reply_block,
  111. reply_block_size,
  112. &chash);
  113. if (GNUNET_YES ==
  114. GNUNET_BLOCK_GROUP_bf_test_and_set (group,
  115. &chash))
  116. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  117. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  118. }
  119. /**
  120. * Function called to obtain the key for a block.
  121. *
  122. * @param cls closure
  123. * @param type block type
  124. * @param block block to get the key for
  125. * @param block_size number of bytes in block
  126. * @param key set to the key (query) for the given block
  127. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  128. * (or if extracting a key from a block of this type does not work)
  129. */
  130. static int
  131. block_plugin_template_get_key (void *cls,
  132. enum GNUNET_BLOCK_Type type,
  133. const void *block,
  134. size_t block_size,
  135. struct GNUNET_HashCode *key)
  136. {
  137. return GNUNET_SYSERR;
  138. }
  139. /**
  140. * Entry point for the plugin.
  141. *
  142. * @param cls a `const struct GNUNET_CONFIGURATION_Handle`
  143. */
  144. void *
  145. libgnunet_plugin_block_template_init (void *cls)
  146. {
  147. static enum GNUNET_BLOCK_Type types[] = {
  148. /* FIXME: insert supported block types here */
  149. GNUNET_BLOCK_TYPE_ANY /* end of list */
  150. };
  151. struct GNUNET_BLOCK_PluginFunctions *api;
  152. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  153. api->evaluate = &block_plugin_template_evaluate;
  154. api->get_key = &block_plugin_template_get_key;
  155. api->create_group = &block_plugin_template_create_group;
  156. api->types = types;
  157. return api;
  158. }
  159. /**
  160. * Exit point from the plugin.
  161. */
  162. void *
  163. libgnunet_plugin_block_template_done (void *cls)
  164. {
  165. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  166. GNUNET_free (api);
  167. return NULL;
  168. }
  169. /* end of plugin_block_template.c */