transport_api_manipulation.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 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_manipulation.c
  18. * @brief library to access the low-level P2P IO service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_transport_service.h"
  28. #include "transport.h"
  29. #define LOG(kind, ...) GNUNET_log_from (kind, "transport-api", __VA_ARGS__)
  30. /**
  31. * Handle for the transport service (includes all of the
  32. * state for the transport service).
  33. */
  34. struct GNUNET_TRANSPORT_ManipulationHandle
  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. * Reconnect in progress
  54. */
  55. int reconnecting;
  56. };
  57. /**
  58. * Function that will schedule the job that will try
  59. * to connect us again to the client.
  60. *
  61. * @param h transport service to reconnect
  62. */
  63. static void
  64. disconnect_and_schedule_reconnect (struct
  65. GNUNET_TRANSPORT_ManipulationHandle *h);
  66. /**
  67. * Generic error handler, called with the appropriate
  68. * error code and the same closure specified at the creation of
  69. * the message queue.
  70. * Not every message queue implementation supports an error handler.
  71. *
  72. * @param cls closure with the `struct GNUNET_TRANSPORT_ManipulationHandle *`
  73. * @param error error code
  74. */
  75. static void
  76. mq_error_handler (void *cls,
  77. enum GNUNET_MQ_Error error)
  78. {
  79. struct GNUNET_TRANSPORT_ManipulationHandle *h = cls;
  80. LOG (GNUNET_ERROR_TYPE_DEBUG,
  81. "Error receiving from transport service, disconnecting temporarily.\n");
  82. h->reconnecting = GNUNET_YES;
  83. disconnect_and_schedule_reconnect (h);
  84. }
  85. /**
  86. * Try again to connect to transport service.
  87. *
  88. * @param cls the handle to the transport service
  89. */
  90. static void
  91. reconnect (void *cls)
  92. {
  93. struct GNUNET_TRANSPORT_ManipulationHandle *h = cls;
  94. struct GNUNET_MQ_MessageHandler handlers[] = {
  95. GNUNET_MQ_handler_end ()
  96. };
  97. struct GNUNET_MQ_Envelope *env;
  98. struct StartMessage *s;
  99. h->reconnect_task = NULL;
  100. LOG (GNUNET_ERROR_TYPE_DEBUG,
  101. "Connecting to transport service.\n");
  102. GNUNET_assert (NULL == h->mq);
  103. h->reconnecting = GNUNET_NO;
  104. h->mq = GNUNET_CLIENT_connect (h->cfg,
  105. "transport",
  106. handlers,
  107. &mq_error_handler,
  108. h);
  109. if (NULL == h->mq)
  110. return;
  111. env = GNUNET_MQ_msg (s,
  112. GNUNET_MESSAGE_TYPE_TRANSPORT_START);
  113. GNUNET_MQ_send (h->mq,
  114. env);
  115. }
  116. /**
  117. * Function that will schedule the job that will try
  118. * to connect us again to the client.
  119. *
  120. * @param h transport service to reconnect
  121. */
  122. static void
  123. disconnect_and_schedule_reconnect (struct
  124. GNUNET_TRANSPORT_ManipulationHandle *h)
  125. {
  126. GNUNET_assert (NULL == h->reconnect_task);
  127. if (NULL != h->mq)
  128. {
  129. GNUNET_MQ_destroy (h->mq);
  130. h->mq = NULL;
  131. }
  132. h->reconnect_task =
  133. GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
  134. &reconnect,
  135. h);
  136. h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
  137. }
  138. /**
  139. * Set transport metrics for a peer and a direction.
  140. *
  141. * @param handle transport handle
  142. * @param peer the peer to set the metric for
  143. * @param prop the performance metrics to set
  144. * @param delay_in inbound delay to introduce
  145. * @param delay_out outbound delay to introduce
  146. *
  147. * Note: Delay restrictions in receiving direction will be enforced
  148. * with one message delay.
  149. */
  150. void
  151. GNUNET_TRANSPORT_manipulation_set (struct
  152. GNUNET_TRANSPORT_ManipulationHandle *handle,
  153. const struct GNUNET_PeerIdentity *peer,
  154. const struct GNUNET_ATS_Properties *prop,
  155. struct GNUNET_TIME_Relative delay_in,
  156. struct GNUNET_TIME_Relative delay_out)
  157. {
  158. struct GNUNET_MQ_Envelope *env;
  159. struct TrafficMetricMessage *msg;
  160. if (NULL == handle->mq)
  161. return;
  162. env = GNUNET_MQ_msg (msg,
  163. GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
  164. msg->reserved = htonl (0);
  165. msg->peer = *peer;
  166. GNUNET_ATS_properties_hton (&msg->properties,
  167. prop);
  168. msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
  169. msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
  170. GNUNET_MQ_send (handle->mq,
  171. env);
  172. }
  173. /**
  174. * Connect to the transport service. Note that the connection may
  175. * complete (or fail) asynchronously.
  176. *
  177. * @param cfg configuration to use
  178. * @return NULL on error
  179. */
  180. struct GNUNET_TRANSPORT_ManipulationHandle *
  181. GNUNET_TRANSPORT_manipulation_connect (const struct
  182. GNUNET_CONFIGURATION_Handle *cfg)
  183. {
  184. struct GNUNET_TRANSPORT_ManipulationHandle *h;
  185. h = GNUNET_new (struct GNUNET_TRANSPORT_ManipulationHandle);
  186. h->cfg = cfg;
  187. LOG (GNUNET_ERROR_TYPE_DEBUG,
  188. "Connecting to transport service.\n");
  189. reconnect (h);
  190. if (NULL == h->mq)
  191. {
  192. GNUNET_free (h);
  193. return NULL;
  194. }
  195. return h;
  196. }
  197. /**
  198. * Disconnect from the transport service.
  199. *
  200. * @param handle handle to the service as returned from #GNUNET_TRANSPORT_manipulation_connect()
  201. */
  202. void
  203. GNUNET_TRANSPORT_manipulation_disconnect (struct
  204. GNUNET_TRANSPORT_ManipulationHandle *
  205. handle)
  206. {
  207. if (NULL == handle->reconnect_task)
  208. disconnect_and_schedule_reconnect (handle);
  209. /* and now we stop trying to connect again... */
  210. if (NULL != handle->reconnect_task)
  211. {
  212. GNUNET_SCHEDULER_cancel (handle->reconnect_task);
  213. handle->reconnect_task = NULL;
  214. }
  215. GNUNET_free (handle);
  216. }
  217. /* end of transport_api_manipulation.c */