plugin_block_dht.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. This file is part of GNUnet
  3. (C) 2010 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 dht/plugin_block_dht.c
  19. * @brief block plugin for DHT internals (right now, find-peer requests only);
  20. * other plugins should be used to store "useful" data in the
  21. * DHT (see fs block plugin)
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_constants.h"
  26. #include "gnunet_hello_lib.h"
  27. #include "gnunet_block_plugin.h"
  28. #define DEBUG_DHT GNUNET_EXTRA_LOGGING
  29. /**
  30. * Function called to validate a reply or a request. For
  31. * request evaluation, simply pass "NULL" for the reply_block.
  32. *
  33. * @param cls closure
  34. * @param type block type
  35. * @param query original query (hash)
  36. * @param bf pointer to bloom filter associated with query; possibly updated (!)
  37. * @param bf_mutator mutation value for @a bf
  38. * @param xquery extended query data (can be NULL, depending on type)
  39. * @param xquery_size number of bytes in @a xquery
  40. * @param reply_block response to validate
  41. * @param reply_block_size number of bytes in @a reply_block
  42. * @return characterization of result
  43. */
  44. static enum GNUNET_BLOCK_EvaluationResult
  45. block_plugin_dht_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
  46. const struct GNUNET_HashCode * query,
  47. struct GNUNET_CONTAINER_BloomFilter **bf,
  48. int32_t bf_mutator, const void *xquery,
  49. size_t xquery_size, const void *reply_block,
  50. size_t reply_block_size)
  51. {
  52. struct GNUNET_HashCode mhash;
  53. const struct GNUNET_HELLO_Message *hello;
  54. struct GNUNET_PeerIdentity pid;
  55. const struct GNUNET_MessageHeader *msg;
  56. struct GNUNET_HashCode phash;
  57. if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
  58. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  59. if (xquery_size != 0)
  60. {
  61. GNUNET_break_op (0);
  62. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  63. }
  64. if (NULL == reply_block)
  65. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  66. if (reply_block_size < sizeof (struct GNUNET_MessageHeader))
  67. {
  68. GNUNET_break_op (0);
  69. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  70. }
  71. msg = reply_block;
  72. if (reply_block_size != ntohs (msg->size))
  73. {
  74. GNUNET_break_op (0);
  75. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  76. }
  77. hello = reply_block;
  78. if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
  79. {
  80. GNUNET_break_op (0);
  81. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  82. }
  83. if (NULL != bf)
  84. {
  85. GNUNET_CRYPTO_hash (&pid, sizeof (pid), &phash);
  86. GNUNET_BLOCK_mingle_hash (&phash, bf_mutator, &mhash);
  87. if (NULL != *bf)
  88. {
  89. if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
  90. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  91. }
  92. else
  93. {
  94. *bf =
  95. GNUNET_CONTAINER_bloomfilter_init (NULL, 8,
  96. GNUNET_CONSTANTS_BLOOMFILTER_K);
  97. }
  98. GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
  99. }
  100. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  101. }
  102. /**
  103. * Function called to obtain the key for a block.
  104. *
  105. * @param cls closure
  106. * @param type block type
  107. * @param block block to get the key for
  108. * @param block_size number of bytes @a block
  109. * @param key set to the key (query) for the given block
  110. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  111. * (or if extracting a key from a block of this type does not work)
  112. */
  113. static int
  114. block_plugin_dht_get_key (void *cls, enum GNUNET_BLOCK_Type type,
  115. const void *block, size_t block_size,
  116. struct GNUNET_HashCode * key)
  117. {
  118. const struct GNUNET_MessageHeader *msg;
  119. const struct GNUNET_HELLO_Message *hello;
  120. struct GNUNET_PeerIdentity *pid;
  121. if (type != GNUNET_BLOCK_TYPE_DHT_HELLO)
  122. return GNUNET_SYSERR;
  123. if (block_size < sizeof (struct GNUNET_MessageHeader))
  124. {
  125. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
  126. _("Block not of type %u\n"), GNUNET_BLOCK_TYPE_DHT_HELLO);
  127. return GNUNET_NO;
  128. }
  129. msg = block;
  130. if (block_size != ntohs (msg->size))
  131. {
  132. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
  133. _("Size mismatch for block\n"),
  134. GNUNET_BLOCK_TYPE_DHT_HELLO);
  135. return GNUNET_NO;
  136. }
  137. hello = block;
  138. memset (key, 0, sizeof (*key));
  139. pid = (struct GNUNET_PeerIdentity *) key;
  140. if (GNUNET_OK != GNUNET_HELLO_get_id (hello, pid))
  141. {
  142. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "block-dht",
  143. _("Block of type %u is malformed\n"),
  144. GNUNET_BLOCK_TYPE_DHT_HELLO);
  145. return GNUNET_NO;
  146. }
  147. return GNUNET_OK;
  148. }
  149. /**
  150. * Entry point for the plugin.
  151. */
  152. void *
  153. libgnunet_plugin_block_dht_init (void *cls)
  154. {
  155. static enum GNUNET_BLOCK_Type types[] =
  156. {
  157. GNUNET_BLOCK_TYPE_DHT_HELLO,
  158. GNUNET_BLOCK_TYPE_ANY /* end of list */
  159. };
  160. struct GNUNET_BLOCK_PluginFunctions *api;
  161. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  162. api->evaluate = &block_plugin_dht_evaluate;
  163. api->get_key = &block_plugin_dht_get_key;
  164. api->types = types;
  165. return api;
  166. }
  167. /**
  168. * Exit point from the plugin.
  169. */
  170. void *
  171. libgnunet_plugin_block_dht_done (void *cls)
  172. {
  173. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  174. GNUNET_free (api);
  175. return NULL;
  176. }
  177. /* end of plugin_block_dht.c */