plugin_block_test.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. This file is part of GNUnet
  3. Copyright (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 block/plugin_block_test.c
  19. * @brief block plugin to test the DHT as a simple key-value store;
  20. * this plugin simply accepts any (new) response for any key
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_block_plugin.h"
  25. /**
  26. * Number of bits we set per entry in the bloomfilter.
  27. * Do not change!
  28. */
  29. #define BLOOMFILTER_K 16
  30. /**
  31. * Function called to validate a reply or a request. For
  32. * request evaluation, simply pass "NULL" for the reply_block.
  33. *
  34. * @param cls closure
  35. * @param type block type
  36. * @param query original query (hash)
  37. * @param bf pointer to bloom filter associated with query; possibly updated (!)
  38. * @param bf_mutator mutation value for @a bf
  39. * @param xquery extrended query data (can be NULL, depending on type)
  40. * @param xquery_size number of bytes in @a xquery
  41. * @param reply_block response to validate
  42. * @param reply_block_size number of bytes in @a reply_block
  43. * @return characterization of result
  44. */
  45. static enum GNUNET_BLOCK_EvaluationResult
  46. block_plugin_test_evaluate (void *cls, enum GNUNET_BLOCK_Type type,
  47. const struct GNUNET_HashCode * query,
  48. struct GNUNET_CONTAINER_BloomFilter **bf,
  49. int32_t bf_mutator, const void *xquery,
  50. size_t xquery_size, const void *reply_block,
  51. size_t reply_block_size)
  52. {
  53. struct GNUNET_HashCode chash;
  54. struct GNUNET_HashCode mhash;
  55. if ( GNUNET_BLOCK_TYPE_TEST != type)
  56. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  57. if (0 != xquery_size)
  58. {
  59. GNUNET_break_op (0);
  60. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  61. }
  62. if (NULL == reply_block)
  63. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  64. if (NULL != bf)
  65. {
  66. GNUNET_CRYPTO_hash (reply_block, reply_block_size, &chash);
  67. GNUNET_BLOCK_mingle_hash (&chash, bf_mutator, &mhash);
  68. if (NULL != *bf)
  69. {
  70. if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (*bf, &mhash))
  71. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  72. }
  73. else
  74. {
  75. *bf = GNUNET_CONTAINER_bloomfilter_init (NULL, 8, BLOOMFILTER_K);
  76. }
  77. GNUNET_CONTAINER_bloomfilter_add (*bf, &mhash);
  78. }
  79. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  80. }
  81. /**
  82. * Function called to obtain the key for a block.
  83. *
  84. * @param cls closure
  85. * @param type block type
  86. * @param block block to get the key for
  87. * @param block_size number of bytes in @a block
  88. * @param key set to the key (query) for the given block
  89. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  90. * (or if extracting a key from a block of this type does not work)
  91. */
  92. static int
  93. block_plugin_test_get_key (void *cls, enum GNUNET_BLOCK_Type type,
  94. const void *block, size_t block_size,
  95. struct GNUNET_HashCode * key)
  96. {
  97. /* always fails since there is no fixed relationship between
  98. * keys and values for test values */
  99. return GNUNET_SYSERR;
  100. }
  101. /**
  102. * Entry point for the plugin.
  103. *
  104. * @param cls NULL
  105. * @return the exported block API
  106. */
  107. void *
  108. libgnunet_plugin_block_test_init (void *cls)
  109. {
  110. static enum GNUNET_BLOCK_Type types[] =
  111. {
  112. GNUNET_BLOCK_TYPE_TEST,
  113. GNUNET_BLOCK_TYPE_ANY /* end of list */
  114. };
  115. struct GNUNET_BLOCK_PluginFunctions *api;
  116. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  117. api->evaluate = &block_plugin_test_evaluate;
  118. api->get_key = &block_plugin_test_get_key;
  119. api->types = types;
  120. return api;
  121. }
  122. /**
  123. * Exit point from the plugin.
  124. *
  125. * @param cls the return value from #libgnunet_plugin_block_test_init
  126. * @return NULL
  127. */
  128. void *
  129. libgnunet_plugin_block_test_done (void *cls)
  130. {
  131. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  132. GNUNET_free (api);
  133. return NULL;
  134. }
  135. /* end of plugin_block_test.c */