transport_api2_address.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 2016, 2018, 2019 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_api2_address.c
  18. * @brief library to inform the transport service about addresses to be validated
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_transport_address_service.h"
  26. #include "gnunet_ats_transport_service.h"
  27. #include "transport.h"
  28. #define LOG(kind, ...) \
  29. GNUNET_log_from (kind, "transport-api-address", __VA_ARGS__)
  30. /**
  31. * Handle for the transport service (includes all of the
  32. * state for the transport service).
  33. */
  34. struct GNUNET_TRANSPORT_AddressHandle
  35. {
  36. /**
  37. * My client connection to the transport service.
  38. */
  39. struct GNUNET_MQ_Handle *mq;
  40. /**
  41. * My configuration.
  42. */
  43. const struct GNUNET_CONFIGURATION_Handle *cfg;
  44. /**
  45. * ID of the task trying to reconnect to the service.
  46. */
  47. struct GNUNET_SCHEDULER_Task *reconnect_task;
  48. /**
  49. * Delay until we try to reconnect.
  50. */
  51. struct GNUNET_TIME_Relative reconnect_delay;
  52. };
  53. /**
  54. * Function that will schedule the job that will try
  55. * to connect us again to the client.
  56. *
  57. * @param h transport service to reconnect
  58. */
  59. static void
  60. disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_AddressHandle *h);
  61. /**
  62. * Generic error handler, called with the appropriate
  63. * error code and the same closure specified at the creation of
  64. * the message queue.
  65. * Not every message queue implementation supports an error handler.
  66. *
  67. * @param cls closure with the `struct GNUNET_TRANSPORT_AddressHandle *`
  68. * @param error error code
  69. */
  70. static void
  71. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  72. {
  73. struct GNUNET_TRANSPORT_AddressHandle *h = cls;
  74. LOG (GNUNET_ERROR_TYPE_DEBUG,
  75. "Error receiving from transport service, disconnecting temporarily.\n");
  76. disconnect_and_schedule_reconnect (h);
  77. }
  78. /**
  79. * Try again to connect to transport service.
  80. *
  81. * @param cls the handle to the transport service
  82. */
  83. static void
  84. reconnect (void *cls)
  85. {
  86. struct GNUNET_TRANSPORT_AddressHandle *h = cls;
  87. struct GNUNET_MQ_MessageHandler handlers[] = {GNUNET_MQ_handler_end ()};
  88. h->reconnect_task = NULL;
  89. LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
  90. GNUNET_assert (NULL == h->mq);
  91. h->mq =
  92. GNUNET_CLIENT_connect (h->cfg, "transport", handlers, &mq_error_handler, h);
  93. }
  94. /**
  95. * Disconnect from the transport service.
  96. *
  97. * @param h transport service to disconnect
  98. */
  99. static void
  100. disconnect (struct GNUNET_TRANSPORT_AddressHandle *h)
  101. {
  102. if (NULL != h->mq)
  103. {
  104. GNUNET_MQ_destroy (h->mq);
  105. h->mq = NULL;
  106. }
  107. }
  108. /**
  109. * Function that will schedule the job that will try
  110. * to connect us again to the client.
  111. *
  112. * @param h transport service to reconnect
  113. */
  114. static void
  115. disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_AddressHandle *h)
  116. {
  117. GNUNET_assert (NULL == h->reconnect_task);
  118. disconnect (h);
  119. LOG (GNUNET_ERROR_TYPE_DEBUG,
  120. "Scheduling task to reconnect to transport service in %s.\n",
  121. GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
  122. h->reconnect_task =
  123. GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
  124. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  125. }
  126. /**
  127. * Connect to the transport service.
  128. *
  129. * @param cfg configuration to use
  130. * @return NULL on error
  131. */
  132. struct GNUNET_TRANSPORT_AddressHandle *
  133. GNUNET_TRANSPORT_address_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
  134. {
  135. struct GNUNET_TRANSPORT_AddressHandle *h;
  136. h = GNUNET_new (struct GNUNET_TRANSPORT_AddressHandle);
  137. h->cfg = cfg;
  138. h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
  139. LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service\n");
  140. reconnect (h);
  141. if (NULL == h->mq)
  142. {
  143. GNUNET_free (h);
  144. return NULL;
  145. }
  146. return h;
  147. }
  148. /**
  149. * Disconnect from the transport service.
  150. *
  151. * @param handle handle to the service as returned from #GNUNET_TRANSPORT_address_connect()
  152. */
  153. void
  154. GNUNET_TRANSPORT_address_disconnect (
  155. struct GNUNET_TRANSPORT_AddressHandle *handle)
  156. {
  157. LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
  158. /* this disconnects all neighbours... */
  159. disconnect (handle);
  160. /* and now we stop trying to connect again... */
  161. if (NULL != handle->reconnect_task)
  162. {
  163. GNUNET_SCHEDULER_cancel (handle->reconnect_task);
  164. handle->reconnect_task = NULL;
  165. }
  166. GNUNET_free (handle);
  167. }
  168. /**
  169. * The client has learned about a possible address for peer @a pid
  170. * (i.e. via broadcast, multicast, DHT, ...). The transport service
  171. * should consider validating it. Note that the plugin is NOT expected
  172. * to have verified the signature, the transport service must decide
  173. * whether to check the signature.
  174. *
  175. * While the notification is sent to @a ch asynchronously, this API
  176. * does not return a handle as the delivery of addresses is simply
  177. * unreliable, and if @a ch is down, the data provided will simply be
  178. * lost.
  179. *
  180. * @param ch communicator handle
  181. * @param pid peer the address is for
  182. * @param raw raw address data
  183. * @param raw_size number of bytes in @a raw
  184. */
  185. void
  186. GNUNET_TRANSPORT_address_try (struct GNUNET_TRANSPORT_AddressHandle *ch,
  187. const struct GNUNET_PeerIdentity *pid,
  188. const void *raw,
  189. const size_t raw_size)
  190. {
  191. struct GNUNET_MQ_Envelope *env;
  192. struct GNUNET_TRANSPORT_AddressToVerify *hdr;
  193. env =
  194. GNUNET_MQ_msg_extra (hdr,
  195. raw_size,
  196. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_CONSIDER_VERIFY);
  197. hdr->peer = *pid;
  198. memcpy (&hdr[1], raw, raw_size);
  199. GNUNET_MQ_send (ch->mq, env);
  200. }
  201. /* end of transport_api2_address.c */