plugin_block_gns.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-2013 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 gns/plugin_block_gns.c
  18. * @brief blocks used for GNS records
  19. * @author Martin Schanzenbach
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_block_group_lib.h"
  24. #include "gnunet_block_plugin.h"
  25. #include "gnunet_namestore_service.h"
  26. #include "gnunet_signatures.h"
  27. /**
  28. * Number of bits we set per entry in the bloomfilter.
  29. * Do not change! -from fs
  30. */
  31. #define BLOOMFILTER_K 16
  32. /**
  33. * How big is the BF we use for GNS blocks?
  34. */
  35. #define GNS_BF_SIZE 8
  36. /**
  37. * Create a new block group.
  38. *
  39. * @param ctx block context in which the block group is created
  40. * @param type type of the block for which we are creating the group
  41. * @param nonce random value used to seed the group creation
  42. * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
  43. * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
  44. * @param va variable arguments specific to @a type
  45. * @return block group handle, NULL if block groups are not supported
  46. * by this @a type of block (this is not an error)
  47. */
  48. static struct GNUNET_BLOCK_Group *
  49. block_plugin_gns_create_group (void *cls,
  50. enum GNUNET_BLOCK_Type type,
  51. uint32_t nonce,
  52. const void *raw_data,
  53. size_t raw_data_size,
  54. va_list va)
  55. {
  56. unsigned int bf_size;
  57. const char *guard;
  58. guard = va_arg (va, const char *);
  59. if (0 == strcmp (guard,
  60. "seen-set-size"))
  61. bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
  62. BLOOMFILTER_K);
  63. else if (0 == strcmp (guard,
  64. "filter-size"))
  65. bf_size = va_arg (va, unsigned int);
  66. else
  67. {
  68. GNUNET_break (0);
  69. bf_size = GNS_BF_SIZE;
  70. }
  71. GNUNET_break (NULL == va_arg (va, const char *));
  72. return GNUNET_BLOCK_GROUP_bf_create (cls,
  73. bf_size,
  74. BLOOMFILTER_K,
  75. type,
  76. nonce,
  77. raw_data,
  78. raw_data_size);
  79. }
  80. /**
  81. * Function called to validate a reply or a request. For
  82. * request evaluation, simply pass "NULL" for the reply_block.
  83. * Note that it is assumed that the reply has already been
  84. * matched to the key (and signatures checked) as it would
  85. * be done with the "get_key" function.
  86. *
  87. * @param cls closure
  88. * @param ctx block context
  89. * @param type block type
  90. * @param bg block group to use for evaluation
  91. * @param eo control flags
  92. * @param query original query (hash)
  93. * @param xquery extrended query data (can be NULL, depending on @a type)
  94. * @param xquery_size number of bytes in @a xquery
  95. * @param reply_block response to validate
  96. * @param reply_block_size number of bytes in @a reply_block
  97. * @return characterization of result
  98. */
  99. static enum GNUNET_BLOCK_EvaluationResult
  100. block_plugin_gns_evaluate (void *cls,
  101. struct GNUNET_BLOCK_Context *ctx,
  102. enum GNUNET_BLOCK_Type type,
  103. struct GNUNET_BLOCK_Group *bg,
  104. enum GNUNET_BLOCK_EvaluationOptions eo,
  105. const struct GNUNET_HashCode *query,
  106. const void *xquery,
  107. size_t xquery_size,
  108. const void *reply_block,
  109. size_t reply_block_size)
  110. {
  111. const struct GNUNET_GNSRECORD_Block *block;
  112. struct GNUNET_HashCode h;
  113. struct GNUNET_HashCode chash;
  114. if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
  115. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  116. if (NULL == reply_block)
  117. {
  118. if (0 != xquery_size)
  119. {
  120. GNUNET_break_op (0);
  121. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  122. }
  123. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  124. }
  125. /* this is a reply */
  126. if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
  127. {
  128. GNUNET_break_op (0);
  129. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  130. }
  131. block = reply_block;
  132. if (ntohl (block->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature) + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
  133. reply_block_size)
  134. {
  135. GNUNET_break_op (0);
  136. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  137. }
  138. GNUNET_CRYPTO_hash (&block->derived_key,
  139. sizeof (block->derived_key),
  140. &h);
  141. if (0 != memcmp (&h, query, sizeof (struct GNUNET_HashCode)))
  142. {
  143. GNUNET_break_op (0);
  144. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  145. }
  146. if (GNUNET_OK !=
  147. GNUNET_GNSRECORD_block_verify (block))
  148. {
  149. GNUNET_break_op (0);
  150. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  151. }
  152. GNUNET_CRYPTO_hash (reply_block,
  153. reply_block_size,
  154. &chash);
  155. if (GNUNET_YES ==
  156. GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
  157. &chash))
  158. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  159. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  160. }
  161. /**
  162. * Function called to obtain the key for a block.
  163. *
  164. * @param cls closure
  165. * @param type block type
  166. * @param reply_block block to get the key for
  167. * @param reply_block_size number of bytes in @a reply_block
  168. * @param key set to the key (query) for the given block
  169. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  170. * (or if extracting a key from a block of this type does not work)
  171. */
  172. static int
  173. block_plugin_gns_get_key (void *cls,
  174. enum GNUNET_BLOCK_Type type,
  175. const void *reply_block,
  176. size_t reply_block_size,
  177. struct GNUNET_HashCode *key)
  178. {
  179. const struct GNUNET_GNSRECORD_Block *block;
  180. if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
  181. return GNUNET_SYSERR;
  182. if (reply_block_size < sizeof (struct GNUNET_GNSRECORD_Block))
  183. {
  184. GNUNET_break_op (0);
  185. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  186. }
  187. block = reply_block;
  188. GNUNET_CRYPTO_hash (&block->derived_key,
  189. sizeof (block->derived_key),
  190. key);
  191. return GNUNET_OK;
  192. }
  193. /**
  194. * Entry point for the plugin.
  195. */
  196. void *
  197. libgnunet_plugin_block_gns_init (void *cls)
  198. {
  199. static enum GNUNET_BLOCK_Type types[] =
  200. {
  201. GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
  202. GNUNET_BLOCK_TYPE_ANY /* end of list */
  203. };
  204. struct GNUNET_BLOCK_PluginFunctions *api;
  205. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  206. api->evaluate = &block_plugin_gns_evaluate;
  207. api->get_key = &block_plugin_gns_get_key;
  208. api->create_group = &block_plugin_gns_create_group;
  209. api->types = types;
  210. return api;
  211. }
  212. /**
  213. * Exit point from the plugin.
  214. */
  215. void *
  216. libgnunet_plugin_block_gns_done (void *cls)
  217. {
  218. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  219. GNUNET_free (api);
  220. return NULL;
  221. }
  222. /* end of plugin_block_gns.c */