plugin_block_dns.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2013, 2017 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 dns/plugin_block_dns.c
  18. * @brief block plugin for advertising a DNS exit service
  19. * @author Christian Grothoff
  20. *
  21. * Note that this plugin might more belong with EXIT and PT
  22. * as those two are using this type of block. Still, this
  23. * might be a natural enough place for people to find the code...
  24. */
  25. #include "platform.h"
  26. #include "gnunet_block_plugin.h"
  27. #include "block_dns.h"
  28. #include "gnunet_signatures.h"
  29. #include "gnunet_block_group_lib.h"
  30. /**
  31. * Number of bits we set per entry in the bloomfilter.
  32. * Do not change!
  33. */
  34. #define BLOOMFILTER_K 16
  35. /**
  36. * Create a new block group.
  37. *
  38. * @param ctx block context in which the block group is created
  39. * @param type type of the block for which we are creating the group
  40. * @param nonce random value used to seed the group creation
  41. * @param raw_data optional serialized prior state of the group, NULL if unavailable/fresh
  42. * @param raw_data_size number of bytes in @a raw_data, 0 if unavailable/fresh
  43. * @param va variable arguments specific to @a type
  44. * @return block group handle, NULL if block groups are not supported
  45. * by this @a type of block (this is not an error)
  46. */
  47. static struct GNUNET_BLOCK_Group *
  48. block_plugin_dns_create_group (void *cls,
  49. enum GNUNET_BLOCK_Type type,
  50. uint32_t nonce,
  51. const void *raw_data,
  52. size_t raw_data_size,
  53. va_list va)
  54. {
  55. unsigned int bf_size;
  56. const char *guard;
  57. guard = va_arg (va, const char *);
  58. if (0 == strcmp (guard,
  59. "seen-set-size"))
  60. bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int),
  61. BLOOMFILTER_K);
  62. else if (0 == strcmp (guard,
  63. "filter-size"))
  64. bf_size = va_arg (va, unsigned int);
  65. else
  66. {
  67. GNUNET_break (0);
  68. bf_size = 8;
  69. }
  70. GNUNET_break (NULL == va_arg (va, const char *));
  71. return GNUNET_BLOCK_GROUP_bf_create (cls,
  72. bf_size,
  73. BLOOMFILTER_K,
  74. type,
  75. nonce,
  76. raw_data,
  77. raw_data_size);
  78. }
  79. /**
  80. * Function called to validate a reply or a request. For
  81. * request evaluation, simply pass "NULL" for the reply_block.
  82. *
  83. * @param cls closure
  84. * @param ctx block context
  85. * @param type block type
  86. * @param bg group to evaluate against
  87. * @param eo control flags
  88. * @param query original query (hash)
  89. * @param xquery extended query data (can be NULL, depending on type)
  90. * @param xquery_size number of bytes in @a xquery
  91. * @param reply_block response to validate
  92. * @param reply_block_size number of bytes in @a reply_block
  93. * @return characterization of result
  94. */
  95. static enum GNUNET_BLOCK_EvaluationResult
  96. block_plugin_dns_evaluate (void *cls,
  97. struct GNUNET_BLOCK_Context *ctx,
  98. enum GNUNET_BLOCK_Type type,
  99. struct GNUNET_BLOCK_Group *bg,
  100. enum GNUNET_BLOCK_EvaluationOptions eo,
  101. const struct GNUNET_HashCode * query,
  102. const void *xquery,
  103. size_t xquery_size,
  104. const void *reply_block,
  105. size_t reply_block_size)
  106. {
  107. const struct GNUNET_DNS_Advertisement *ad;
  108. struct GNUNET_HashCode phash;
  109. switch (type)
  110. {
  111. case GNUNET_BLOCK_TYPE_DNS:
  112. if (0 != xquery_size)
  113. return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
  114. if (NULL == reply_block)
  115. return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
  116. if (sizeof (struct GNUNET_DNS_Advertisement) != reply_block_size)
  117. {
  118. GNUNET_break_op (0);
  119. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  120. }
  121. ad = reply_block;
  122. if (ntohl (ad->purpose.size) !=
  123. sizeof (struct GNUNET_DNS_Advertisement) -
  124. sizeof (struct GNUNET_CRYPTO_EddsaSignature))
  125. {
  126. GNUNET_break_op (0);
  127. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  128. }
  129. if (0 ==
  130. GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh
  131. (ad->expiration_time)).rel_value_us)
  132. {
  133. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  134. "DNS advertisement has expired\n");
  135. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  136. }
  137. if (GNUNET_OK !=
  138. GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD,
  139. &ad->purpose,
  140. &ad->signature,
  141. &ad->peer.public_key))
  142. {
  143. GNUNET_break_op (0);
  144. return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
  145. }
  146. GNUNET_CRYPTO_hash (reply_block,
  147. reply_block_size,
  148. &phash);
  149. if (GNUNET_YES ==
  150. GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
  151. &phash))
  152. return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
  153. return GNUNET_BLOCK_EVALUATION_OK_MORE;
  154. default:
  155. return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
  156. }
  157. }
  158. /**
  159. * Function called to obtain the key for a block.
  160. *
  161. * @param cls closure
  162. * @param type block type
  163. * @param block block to get the key for
  164. * @param block_size number of bytes in @a block
  165. * @param key set to the key (query) for the given block
  166. * @return #GNUNET_OK on success, #GNUNET_SYSERR if type not supported
  167. * (or if extracting a key from a block of this type does not work)
  168. */
  169. static int
  170. block_plugin_dns_get_key (void *cls,
  171. enum GNUNET_BLOCK_Type type,
  172. const void *block,
  173. size_t block_size,
  174. struct GNUNET_HashCode *key)
  175. {
  176. /* we cannot extract a key from a block of this type */
  177. return GNUNET_SYSERR;
  178. }
  179. /**
  180. * Entry point for the plugin.
  181. */
  182. void *
  183. libgnunet_plugin_block_dns_init (void *cls)
  184. {
  185. static enum GNUNET_BLOCK_Type types[] =
  186. {
  187. GNUNET_BLOCK_TYPE_DNS,
  188. GNUNET_BLOCK_TYPE_ANY /* end of list */
  189. };
  190. struct GNUNET_BLOCK_PluginFunctions *api;
  191. api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions);
  192. api->evaluate = &block_plugin_dns_evaluate;
  193. api->get_key = &block_plugin_dns_get_key;
  194. api->create_group = &block_plugin_dns_create_group;
  195. api->types = types;
  196. return api;
  197. }
  198. /**
  199. * Exit point from the plugin.
  200. */
  201. void *
  202. libgnunet_plugin_block_dns_done (void *cls)
  203. {
  204. struct GNUNET_BLOCK_PluginFunctions *api = cls;
  205. GNUNET_free (api);
  206. return NULL;
  207. }
  208. /* end of plugin_block_dns.c */