plugin_block_fs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. This file is part of GNUnet
  3. (C) 2010, 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/plugin_block_fs.c
  19. * @brief blocks used for file-sharing
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_block_plugin.h"
  24. #include "gnunet_fs_service.h"
  25. #include "block_fs.h"
  26. #include "gnunet_signatures.h"
  27. /**
  28. * Number of bits we set per entry in the bloomfilter.
  29. * Do not change!
  30. */
  31. #define BLOOMFILTER_K 16
  32. /**
  33. * Function called to validate a reply or a request. For
  34. * request evaluation, simply pass "NULL" for the reply_block.
  35. * Note that it is assumed that the reply has already been
  36. * matched to the key (and signatures checked) as it would
  37. * be done with the "get_key" function.
  38. *
  39. * @param cls closure
  40. * @param type block type
  41. * @param query original query (hash)
  42. * @param bf pointer to bloom filter associated with query; possibly updated (!)
  43. * @param bf_mutator mutation value for bf
  44. * @param xquery extrended query data (can be NULL, depending on type)
  45. * @param xquery_size number of bytes in xquery
  46. * @param reply_block response to validate
  47. * @param reply_block_size number of bytes in reply block
  48. * @return characterization of result
  49. */
  50. static enum GNUNET_BLOCK_EvaluationResult
  51. block_plugin_fs_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
  52. const struct GNUNET_HashCode *query,
  53. struct GNUNET_CONTAINER_BloomFilter **bf,
  54. int32_t bf_mutator, const void *xquery,
  55. size_t xquery_size, const void *reply_block,
  56. size_t reply_block_size)
  57. {
  58. const struct UBlock *ub;
  59. struct GNUNET_HashCode hc;
  60. struct GNUNET_HashCode chash;
  61. struct GNUNET_HashCode mhash;
  62. switch (type)
  63. {
  64. case GNUNET_BLOCK_TYPE_FS_DBLOCK:
  65. case GNUNET_BLOCK_TYPE_FS_IBLOCK:
  66. if (0 != xquery_size)
  67. {
  68. GNUNET_break_op (0);
  69. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  70. }
  71. if (NULL == reply_block)
  72. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  73. return GNUNET_BLOCK_EVALUATION_OK_LAST;
  74. case GNUNET_BLOCK_TYPE_FS_UBLOCK:
  75. if (0 != xquery_size)
  76. {
  77. GNUNET_break_op (0);
  78. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  79. }
  80. if (NULL == reply_block)
  81. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  82. if (reply_block_size < sizeof (struct UBlock))
  83. {
  84. GNUNET_break_op (0);
  85. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  86. }
  87. ub = reply_block;
  88. GNUNET_CRYPTO_hash (&ub->verification_key,
  89. sizeof (ub->verification_key),
  90. &hc);
  91. if (0 != memcmp (&hc,
  92. query,
  93. sizeof (struct GNUNET_HashCode)))
  94. {
  95. GNUNET_break_op (0);
  96. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  97. }
  98. if (reply_block_size != ntohl (ub->purpose.size) + sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
  99. {
  100. GNUNET_break_op (0);
  101. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  102. }
  103. if (GNUNET_OK !=
  104. GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK,
  105. &ub->purpose,
  106. &ub->signature,
  107. &ub->verification_key))
  108. {
  109. GNUNET_break_op (0);
  110. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  111. }
  112. if (NULL != bf)
  113. {
  114. GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
  115. GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
  116. if (NULL != *bf)
  117. {
  118. if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
  119. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  120. }
  121. else
  122. {
  123. *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
  124. }
  125. GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
  126. }
  127. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  128. default:
  129. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  130. }
  131. }
  132. /**
  133. * Function called to obtain the key for a block.
  134. *
  135. * @param cls closure
  136. * @param type block type
  137. * @param block block to get the key for
  138. * @param block_size number of bytes in block
  139. * @param key set to the key (query) for the given block
  140. * @return GNUNET_OK on success, GNUNET_SYSERR if type not supported
  141. * (or if extracting a key from a block of this type does not work)
  142. */
  143. static int
  144. block_plugin_fs_get_key (void *cls, enum GNUNET_BLOCK_Type type,
  145. const void *block, size_t block_size,
  146. struct GNUNET_HashCode *key)
  147. {
  148. const struct UBlock *ub;
  149. switch (type)
  150. {
  151. case GNUNET_BLOCK_TYPE_FS_DBLOCK:
  152. case GNUNET_BLOCK_TYPE_FS_IBLOCK:
  153. GNUNET_CRYPTO_hash (block, block_size, key);
  154. return GNUNET_OK;
  155. case GNUNET_BLOCK_TYPE_FS_UBLOCK:
  156. if (block_size < sizeof (struct UBlock))
  157. {
  158. GNUNET_break (0);
  159. return GNUNET_SYSERR;
  160. }
  161. ub = block;
  162. GNUNET_CRYPTO_hash (&ub->verification_key,
  163. sizeof (ub->verification_key),
  164. key);
  165. return GNUNET_OK;
  166. default:
  167. GNUNET_break (0);
  168. return GNUNET_SYSERR;
  169. }
  170. }
  171. /**
  172. * Entry point for the plugin.
  173. */
  174. void *
  175. libgnunet_plugin_block_fs_init (void *cls)
  176. {
  177. static enum GNUNET_BLOCK_Type types[] =
  178. {
  179. GNUNET_BLOCK_TYPE_FS_DBLOCK,
  180. GNUNET_BLOCK_TYPE_FS_IBLOCK,
  181. GNUNET_BLOCK_TYPE_FS_UBLOCK,
  182. GNUNET_BLOCK_TYPE_ANY /* end of list */
  183. };
  184. struct GNUNET_BLOCK_PluginFunctions *api;
  185. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  186. api->evaluate = &block_plugin_fs_evaluate;
  187. api->get_key = &block_plugin_fs_get_key;
  188. api->types = types;
  189. return api;
  190. }
  191. /**
  192. * Exit point from the plugin.
  193. */
  194. void *
  195. libgnunet_plugin_block_fs_done (void *cls)
  196. {
  197. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  198. GNUNET_free (api);
  199. return NULL;
  200. }
  201. /* end of plugin_block_fs.c */