ats_api_connectivity.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-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 ats/ats_api_connectivity.c
  18. * @brief enable clients to ask ATS about establishing connections to peers
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. */
  22. #include "platform.h"
  23. #include "gnunet_ats_service.h"
  24. #include "ats.h"
  25. #define LOG(kind, ...) GNUNET_log_from (kind, "ats-connectivity-api", \
  26. __VA_ARGS__)
  27. /**
  28. * Handle for ATS address suggestion requests.
  29. */
  30. struct GNUNET_ATS_ConnectivitySuggestHandle
  31. {
  32. /**
  33. * ID of the peer for which address suggestion was requested.
  34. */
  35. struct GNUNET_PeerIdentity id;
  36. /**
  37. * Connecitivity handle this suggestion handle belongs to.
  38. */
  39. struct GNUNET_ATS_ConnectivityHandle *ch;
  40. /**
  41. * How urgent is the request.
  42. */
  43. uint32_t strength;
  44. };
  45. /**
  46. * Handle to the ATS subsystem for connectivity management.
  47. */
  48. struct GNUNET_ATS_ConnectivityHandle
  49. {
  50. /**
  51. * Our configuration.
  52. */
  53. const struct GNUNET_CONFIGURATION_Handle *cfg;
  54. /**
  55. * Map with the identities of all the peers for which we would
  56. * like to have address suggestions. The key is the PID, the
  57. * value is currently the `struct GNUNET_ATS_ConnectivitySuggestHandle`
  58. */
  59. struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
  60. /**
  61. * Message queue for sending requests to the ATS service.
  62. */
  63. struct GNUNET_MQ_Handle *mq;
  64. /**
  65. * Task to trigger reconnect.
  66. */
  67. struct GNUNET_SCHEDULER_Task *task;
  68. /**
  69. * Reconnect backoff delay.
  70. */
  71. struct GNUNET_TIME_Relative backoff;
  72. };
  73. /**
  74. * Re-establish the connection to the ATS service.
  75. *
  76. * @param ch handle to use to re-connect.
  77. */
  78. static void
  79. reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
  80. /**
  81. * Re-establish the connection to the ATS service.
  82. *
  83. * @param cls handle to use to re-connect.
  84. */
  85. static void
  86. reconnect_task (void *cls)
  87. {
  88. struct GNUNET_ATS_ConnectivityHandle *ch = cls;
  89. ch->task = NULL;
  90. reconnect (ch);
  91. }
  92. /**
  93. * Disconnect from ATS and then reconnect.
  94. *
  95. * @param ch our handle
  96. */
  97. static void
  98. force_reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
  99. {
  100. if (NULL != ch->mq)
  101. {
  102. GNUNET_MQ_destroy (ch->mq);
  103. ch->mq = NULL;
  104. }
  105. ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
  106. ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff,
  107. &reconnect_task,
  108. ch);
  109. }
  110. /**
  111. * We encountered an error handling the MQ to the
  112. * ATS service. Reconnect.
  113. *
  114. * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
  115. * @param error details about the error
  116. */
  117. static void
  118. error_handler (void *cls,
  119. enum GNUNET_MQ_Error error)
  120. {
  121. struct GNUNET_ATS_ConnectivityHandle *ch = cls;
  122. LOG (GNUNET_ERROR_TYPE_DEBUG,
  123. "ATS connection died (code %d), reconnecting\n",
  124. (int) error);
  125. force_reconnect (ch);
  126. }
  127. /**
  128. * Transmit request for an address suggestion.
  129. *
  130. * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
  131. * @param peer peer to ask for an address suggestion for
  132. * @param value the `struct GNUNET_ATS_SuggestHandle`
  133. * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
  134. * failure (message queue no longer exists)
  135. */
  136. static int
  137. transmit_suggestion (void *cls,
  138. const struct GNUNET_PeerIdentity *peer,
  139. void *value)
  140. {
  141. struct GNUNET_ATS_ConnectivityHandle *ch = cls;
  142. struct GNUNET_ATS_ConnectivitySuggestHandle *sh = value;
  143. struct GNUNET_MQ_Envelope *ev;
  144. struct RequestAddressMessage *m;
  145. if (NULL == ch->mq)
  146. return GNUNET_SYSERR;
  147. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
  148. m->strength = htonl (sh->strength);
  149. m->peer = *peer;
  150. GNUNET_MQ_send (ch->mq, ev);
  151. return GNUNET_OK;
  152. }
  153. /**
  154. * Re-establish the connection to the ATS service.
  155. *
  156. * @param ch handle to use to re-connect.
  157. */
  158. static void
  159. reconnect (struct GNUNET_ATS_ConnectivityHandle *ch)
  160. {
  161. static const struct GNUNET_MQ_MessageHandler handlers[] =
  162. { { NULL, 0, 0 } };
  163. struct GNUNET_MQ_Envelope *ev;
  164. struct ClientStartMessage *init;
  165. GNUNET_assert (NULL == ch->mq);
  166. ch->mq = GNUNET_CLIENT_connect (ch->cfg,
  167. "ats",
  168. handlers,
  169. &error_handler,
  170. ch);
  171. if (NULL == ch->mq)
  172. {
  173. force_reconnect (ch);
  174. return;
  175. }
  176. ev = GNUNET_MQ_msg (init,
  177. GNUNET_MESSAGE_TYPE_ATS_START);
  178. init->start_flag = htonl (START_FLAG_CONNECTION_SUGGESTION);
  179. GNUNET_MQ_send (ch->mq, ev);
  180. if (NULL == ch->mq)
  181. return;
  182. GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
  183. &transmit_suggestion,
  184. ch);
  185. }
  186. /**
  187. * Initialize the ATS connectivity suggestion client handle.
  188. *
  189. * @param cfg configuration to use
  190. * @return ats connectivity handle, NULL on error
  191. */
  192. struct GNUNET_ATS_ConnectivityHandle *
  193. GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
  194. {
  195. struct GNUNET_ATS_ConnectivityHandle *ch;
  196. ch = GNUNET_new (struct GNUNET_ATS_ConnectivityHandle);
  197. ch->cfg = cfg;
  198. ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32,
  199. GNUNET_YES);
  200. reconnect (ch);
  201. return ch;
  202. }
  203. /**
  204. * Function called to free all `struct GNUNET_ATS_ConnectivitySuggestHandle`s
  205. * in the map.
  206. *
  207. * @param cls NULL
  208. * @param key the key
  209. * @param value the value to free
  210. * @return #GNUNET_OK (continue to iterate)
  211. */
  212. static int
  213. free_sug_handle (void *cls,
  214. const struct GNUNET_PeerIdentity *key,
  215. void *value)
  216. {
  217. struct GNUNET_ATS_ConnectivitySuggestHandle *cur = value;
  218. GNUNET_free (cur);
  219. return GNUNET_OK;
  220. }
  221. /**
  222. * Client is done with ATS connectivity management, release resources.
  223. *
  224. * @param ch handle to release
  225. */
  226. void
  227. GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch)
  228. {
  229. if (NULL != ch->mq)
  230. {
  231. GNUNET_MQ_destroy (ch->mq);
  232. ch->mq = NULL;
  233. }
  234. if (NULL != ch->task)
  235. {
  236. GNUNET_SCHEDULER_cancel (ch->task);
  237. ch->task = NULL;
  238. }
  239. GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
  240. &free_sug_handle,
  241. NULL);
  242. GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
  243. GNUNET_free (ch);
  244. }
  245. /**
  246. * We would like to receive address suggestions for a peer. ATS will
  247. * respond with a call to the continuation immediately containing an address or
  248. * no address if none is available. ATS can suggest more addresses until we call
  249. * #GNUNET_ATS_connectivity_suggest_cancel().
  250. *
  251. * @param ch handle
  252. * @param peer identity of the peer we need an address for
  253. * @param strength how urgent is the need for such a suggestion
  254. * @return suggest handle, NULL if a request is already pending
  255. */
  256. struct GNUNET_ATS_ConnectivitySuggestHandle *
  257. GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
  258. const struct GNUNET_PeerIdentity *peer,
  259. uint32_t strength)
  260. {
  261. struct GNUNET_ATS_ConnectivitySuggestHandle *s;
  262. s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
  263. s->ch = ch;
  264. s->id = *peer;
  265. s->strength = strength;
  266. if (GNUNET_OK !=
  267. GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
  268. &s->id,
  269. s,
  270. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  271. {
  272. LOG (GNUNET_ERROR_TYPE_DEBUG,
  273. "Not requesting ATS to suggest address for `%s', request already pending\n",
  274. GNUNET_i2s (peer));
  275. GNUNET_free (s);
  276. return NULL;
  277. }
  278. LOG (GNUNET_ERROR_TYPE_DEBUG,
  279. "Requesting ATS to suggest address for `%s'\n",
  280. GNUNET_i2s (peer));
  281. if (NULL == ch->mq)
  282. return s;
  283. (void) transmit_suggestion (ch,
  284. &s->id,
  285. s);
  286. return s;
  287. }
  288. /**
  289. * We no longer care about being connected to a peer.
  290. *
  291. * @param sh handle to stop
  292. */
  293. void
  294. GNUNET_ATS_connectivity_suggest_cancel (struct
  295. GNUNET_ATS_ConnectivitySuggestHandle *sh)
  296. {
  297. struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
  298. struct GNUNET_MQ_Envelope *ev;
  299. struct RequestAddressMessage *m;
  300. LOG (GNUNET_ERROR_TYPE_DEBUG,
  301. "Telling ATS we no longer care for an address for `%s'\n",
  302. GNUNET_i2s (&sh->id));
  303. GNUNET_assert (GNUNET_OK ==
  304. GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
  305. &sh->id,
  306. sh));
  307. if (NULL == ch->mq)
  308. {
  309. GNUNET_free (sh);
  310. return;
  311. }
  312. ev = GNUNET_MQ_msg (m,
  313. GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
  314. m->strength = htonl (0);
  315. m->peer = sh->id;
  316. GNUNET_MQ_send (ch->mq, ev);
  317. GNUNET_free (sh);
  318. }
  319. /* end of ats_api_connectivity.c */