ats_api_connectivity.c 9.5 KB

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