plugin_block_gns.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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
  62. int),
  63. BLOOMFILTER_K);
  64. else if (0 == strcmp (guard,
  65. "filter-size"))
  66. bf_size = va_arg (va, unsigned int);
  67. else
  68. {
  69. GNUNET_break (0);
  70. bf_size = GNS_BF_SIZE;
  71. }
  72. GNUNET_break (NULL == va_arg (va, const char *));
  73. return GNUNET_BLOCK_GROUP_bf_create (cls,
  74. bf_size,
  75. BLOOMFILTER_K,
  76. type,
  77. nonce,
  78. raw_data,
  79. raw_data_size);
  80. }
  81. /**
  82. * Function called to validate a reply or a request. For
  83. * request evaluation, simply pass "NULL" for the reply_block.
  84. * Note that it is assumed that the reply has already been
  85. * matched to the key (and signatures checked) as it would
  86. * be done with the "get_key" function.
  87. *
  88. * @param cls closure
  89. * @param ctx block context
  90. * @param type block type
  91. * @param bg block group to use for evaluation
  92. * @param eo control flags
  93. * @param query original query (hash)
  94. * @param xquery extrended query data (can be NULL, depending on @a type)
  95. * @param xquery_size number of bytes in @a xquery
  96. * @param reply_block response to validate
  97. * @param reply_block_size number of bytes in @a reply_block
  98. * @return characterization of result
  99. */
  100. static enum GNUNET_BLOCK_EvaluationResult
  101. block_plugin_gns_evaluate (void *cls,
  102. struct GNUNET_BLOCK_Context *ctx,
  103. enum GNUNET_BLOCK_Type type,
  104. struct GNUNET_BLOCK_Group *bg,
  105. enum GNUNET_BLOCK_EvaluationOptions eo,
  106. const struct GNUNET_HashCode *query,
  107. const void *xquery,
  108. size_t xquery_size,
  109. const void *reply_block,
  110. size_t reply_block_size)
  111. {
  112. const struct GNUNET_GNSRECORD_Block *block;
  113. struct GNUNET_HashCode h;
  114. struct GNUNET_HashCode chash;
  115. if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
  116. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  117. if (NULL == reply_block)
  118. {
  119. if (0 != xquery_size)
  120. {
  121. GNUNET_break_op (0);
  122. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  123. }
  124. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  125. }
  126. /* this is a reply */
  127. if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block))
  128. {
  129. GNUNET_break_op (0);
  130. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  131. }
  132. block = reply_block;
  133. if (ntohl (block->purpose.size) + sizeof(struct
  134. GNUNET_CRYPTO_EcdsaSignature)
  135. + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) !=
  136. reply_block_size)
  137. {
  138. GNUNET_break_op (0);
  139. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  140. }
  141. GNUNET_CRYPTO_hash (&block->derived_key,
  142. sizeof(block->derived_key),
  143. &h);
  144. if (0 != GNUNET_memcmp (&h, query))
  145. {
  146. GNUNET_break_op (0);
  147. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  148. }
  149. if (GNUNET_OK !=
  150. GNUNET_GNSRECORD_block_verify (block))
  151. {
  152. GNUNET_break_op (0);
  153. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  154. }
  155. GNUNET_CRYPTO_hash (reply_block,
  156. reply_block_size,
  157. &chash);
  158. if (GNUNET_YES ==
  159. GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
  160. &chash))
  161. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  162. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  163. }
  164. /**
  165. * Function called to obtain the key for a block.
  166. *
  167. * @param cls closure
  168. * @param type block type
  169. * @param reply_block block to get the key for
  170. * @param reply_block_size number of bytes in @a reply_block
  171. * @param key set to the key (query) for the given block
  172. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  173. * (or if extracting a key from a block of this type does not work)
  174. */
  175. static int
  176. block_plugin_gns_get_key (void *cls,
  177. enum GNUNET_BLOCK_Type type,
  178. const void *reply_block,
  179. size_t reply_block_size,
  180. struct GNUNET_HashCode *key)
  181. {
  182. const struct GNUNET_GNSRECORD_Block *block;
  183. if (type != GNUNET_BLOCK_TYPE_GNS_NAMERECORD)
  184. return GNUNET_SYSERR;
  185. if (reply_block_size < sizeof(struct GNUNET_GNSRECORD_Block))
  186. {
  187. GNUNET_break_op (0);
  188. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  189. }
  190. block = reply_block;
  191. GNUNET_CRYPTO_hash (&block->derived_key,
  192. sizeof(block->derived_key),
  193. key);
  194. return GNUNET_OK;
  195. }
  196. /**
  197. * Entry point for the plugin.
  198. */
  199. void *
  200. libgnunet_plugin_block_gns_init (void *cls)
  201. {
  202. static enum GNUNET_BLOCK_Type types[] = {
  203. GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
  204. GNUNET_BLOCK_TYPE_ANY /* end of list */
  205. };
  206. struct GNUNET_BLOCK_PluginFunctions *api;
  207. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  208. api->evaluate = &block_plugin_gns_evaluate;
  209. api->get_key = &block_plugin_gns_get_key;
  210. api->create_group = &block_plugin_gns_create_group;
  211. api->types = types;
  212. return api;
  213. }
  214. /**
  215. * Exit point from the plugin.
  216. */
  217. void *
  218. libgnunet_plugin_block_gns_done (void *cls)
  219. {
  220. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  221. GNUNET_free (api);
  222. return NULL;
  223. }
  224. /* end of plugin_block_gns.c */