transport_api_blacklist.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2014, 2016 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 transport/transport_api_blacklist.c
  18. * @brief library to access the blacklisting functions of the transport service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_hello_lib.h"
  25. #include "gnunet_protocols.h"
  26. #include "gnunet_transport_service.h"
  27. #include "transport.h"
  28. /**
  29. * Handle for blacklisting requests.
  30. */
  31. struct GNUNET_TRANSPORT_Blacklist
  32. {
  33. /**
  34. * Connection to transport service.
  35. */
  36. struct GNUNET_MQ_Handle *mq;
  37. /**
  38. * Configuration to use.
  39. */
  40. const struct GNUNET_CONFIGURATION_Handle *cfg;
  41. /**
  42. * Function to call for determining if a peer is allowed
  43. * to communicate with us.
  44. */
  45. GNUNET_TRANSPORT_BlacklistCallback cb;
  46. /**
  47. * Closure for @e cb.
  48. */
  49. void *cb_cls;
  50. };
  51. /**
  52. * Establish blacklist connection to transport service.
  53. *
  54. * @param br overall handle
  55. */
  56. static void
  57. reconnect (struct GNUNET_TRANSPORT_Blacklist *br);
  58. /**
  59. * Handle blacklist queries.
  60. *
  61. * @param cls our overall handle
  62. * @param bm query
  63. */
  64. static void
  65. handle_query (void *cls,
  66. const struct BlacklistMessage *bm)
  67. {
  68. struct GNUNET_TRANSPORT_Blacklist *br = cls;
  69. struct GNUNET_MQ_Envelope *env;
  70. struct BlacklistMessage *res;
  71. GNUNET_break (0 == ntohl (bm->is_allowed));
  72. env = GNUNET_MQ_msg (res,
  73. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY);
  74. res->is_allowed = htonl (br->cb (br->cb_cls,
  75. &bm->peer));
  76. res->peer = bm->peer;
  77. GNUNET_MQ_send (br->mq,
  78. env);
  79. }
  80. /**
  81. * Generic error handler, called with the appropriate error code and
  82. * the same closure specified at the creation of the message queue.
  83. * Not every message queue implementation supports an error handler.
  84. *
  85. * @param cls closure with the `struct GNUNET_TRANSPORT_Blacklist *`
  86. * @param error error code
  87. */
  88. static void
  89. mq_error_handler (void *cls,
  90. enum GNUNET_MQ_Error error)
  91. {
  92. struct GNUNET_TRANSPORT_Blacklist *br = cls;
  93. reconnect (br);
  94. }
  95. /**
  96. * Establish blacklist connection to transport service.
  97. *
  98. * @param br overall handle
  99. */
  100. static void
  101. reconnect (struct GNUNET_TRANSPORT_Blacklist *br)
  102. {
  103. struct GNUNET_MQ_MessageHandler handlers[] = {
  104. GNUNET_MQ_hd_fixed_size (query,
  105. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY,
  106. struct BlacklistMessage,
  107. br),
  108. GNUNET_MQ_handler_end ()
  109. };
  110. struct GNUNET_MQ_Envelope *env;
  111. struct GNUNET_MessageHeader *req;
  112. if (NULL != br->mq)
  113. GNUNET_MQ_destroy (br->mq);
  114. br->mq = GNUNET_CLIENT_connect (br->cfg,
  115. "transport",
  116. handlers,
  117. &mq_error_handler,
  118. br);
  119. if (NULL == br->mq)
  120. return;
  121. env = GNUNET_MQ_msg (req,
  122. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT);
  123. GNUNET_MQ_send (br->mq,
  124. env);
  125. }
  126. /**
  127. * Install a blacklist callback. The service will be queried for all
  128. * existing connections as well as any fresh connections to check if
  129. * they are permitted. If the blacklisting callback is unregistered,
  130. * all hosts that were denied in the past will automatically be
  131. * whitelisted again. Cancelling the blacklist handle is also the
  132. * only way to re-enable connections from peers that were previously
  133. * blacklisted.
  134. *
  135. * @param cfg configuration to use
  136. * @param cb callback to invoke to check if connections are allowed
  137. * @param cb_cls closure for @a cb
  138. * @return NULL on error, otherwise handle for cancellation
  139. */
  140. struct GNUNET_TRANSPORT_Blacklist *
  141. GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
  142. GNUNET_TRANSPORT_BlacklistCallback cb,
  143. void *cb_cls)
  144. {
  145. struct GNUNET_TRANSPORT_Blacklist *br;
  146. br = GNUNET_new (struct GNUNET_TRANSPORT_Blacklist);
  147. br->cfg = cfg;
  148. br->cb = cb;
  149. br->cb_cls = cb_cls;
  150. reconnect (br);
  151. if (NULL == br->mq)
  152. {
  153. GNUNET_free (br);
  154. return NULL;
  155. }
  156. return br;
  157. }
  158. /**
  159. * Abort the blacklist. Note that this function is the only way for
  160. * removing a peer from the blacklist.
  161. *
  162. * @param br handle of the request that is to be cancelled
  163. */
  164. void
  165. GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br)
  166. {
  167. GNUNET_MQ_destroy (br->mq);
  168. GNUNET_free (br);
  169. }
  170. /* end of transport_api_blacklist.c */