ats_api_connectivity.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file ats/ats_api_connectivity.c
  19. * @brief enable clients to ask ATS about establishing connections to peers
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_ats_service.h"
  25. #include "ats.h"
  26. /**
  27. * Handle for ATS address suggestion requests.
  28. */
  29. struct GNUNET_ATS_ConnectivitySuggestHandle
  30. {
  31. /**
  32. * ID of the peer for which address suggestion was requested.
  33. */
  34. struct GNUNET_PeerIdentity id;
  35. /**
  36. * Connecitivity handle this suggestion handle belongs to.
  37. */
  38. struct GNUNET_ATS_ConnectivityHandle *ch;
  39. };
  40. /**
  41. * Handle to the ATS subsystem for connectivity management.
  42. */
  43. struct GNUNET_ATS_ConnectivityHandle
  44. {
  45. /**
  46. * Our configuration.
  47. */
  48. const struct GNUNET_CONFIGURATION_Handle *cfg;
  49. /**
  50. * Map with the identities of all the peers for which we would
  51. * like to have address suggestions. The key is the PID, the
  52. * value is currently the `struct GNUNET_ATS_ConnectivitySuggestHandle`
  53. */
  54. struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
  55. /**
  56. * Connection to ATS service.
  57. */
  58. struct GNUNET_CLIENT_Connection *client;
  59. /**
  60. * Message queue for sending requests to the ATS service.
  61. */
  62. struct GNUNET_MQ_Handle *mq;
  63. /**
  64. * Task to trigger reconnect.
  65. */
  66. struct GNUNET_SCHEDULER_Task *task;
  67. };
  68. /**
  69. * Re-establish the connection to the ATS service.
  70. *
  71. * @param ch handle to use to re-connect.
  72. */
  73. static void
  74. reconnect (struct GNUNET_ATS_ConnectivityHandle *ch);
  75. /**
  76. * Re-establish the connection to the ATS service.
  77. *
  78. * @param cls handle to use to re-connect.
  79. * @param tc scheduler context
  80. */
  81. static void
  82. reconnect_task (void *cls,
  83. const struct GNUNET_SCHEDULER_TaskContext *tc)
  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. if (NULL != ch->client)
  103. {
  104. GNUNET_CLIENT_disconnect (ch->client);
  105. ch->client = NULL;
  106. }
  107. ch->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  108. &reconnect_task,
  109. ch);
  110. }
  111. /**
  112. * We encountered an error handling the MQ to the
  113. * ATS service. Reconnect.
  114. *
  115. * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
  116. * @param error details about the error
  117. */
  118. static void
  119. error_handler (void *cls,
  120. enum GNUNET_MQ_Error error)
  121. {
  122. struct GNUNET_ATS_ConnectivityHandle *ch = cls;
  123. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  124. "ATS connection died (code %d), reconnecting\n",
  125. (int) error);
  126. force_reconnect (ch);
  127. }
  128. /**
  129. * Transmit request for an address suggestion.
  130. *
  131. * @param cls the `struct GNUNET_ATS_ConnectivityHandle`
  132. * @param peer peer to ask for an address suggestion for
  133. * @param value the `struct GNUNET_ATS_SuggestHandle`
  134. * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
  135. * failure (message queue no longer exists)
  136. */
  137. static int
  138. transmit_suggestion (void *cls,
  139. const struct GNUNET_PeerIdentity *peer,
  140. void *value)
  141. {
  142. struct GNUNET_ATS_ConnectivityHandle *ch = cls;
  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->reserved = htonl (0);
  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->client);
  166. ch->client = GNUNET_CLIENT_connect ("ats", ch->cfg);
  167. if (NULL == ch->client)
  168. {
  169. force_reconnect (ch);
  170. return;
  171. }
  172. ch->mq = GNUNET_MQ_queue_for_connection_client (ch->client,
  173. handlers,
  174. &error_handler,
  175. ch);
  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_SuggestHandles`
  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->client)
  235. {
  236. GNUNET_CLIENT_disconnect (ch->client);
  237. ch->client = NULL;
  238. }
  239. if (NULL != ch->task)
  240. {
  241. GNUNET_SCHEDULER_cancel (ch->task);
  242. ch->task = NULL;
  243. }
  244. GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
  245. &free_sug_handle,
  246. NULL);
  247. GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
  248. GNUNET_free (ch);
  249. }
  250. /**
  251. * We would like to receive address suggestions for a peer. ATS will
  252. * respond with a call to the continuation immediately containing an address or
  253. * no address if none is available. ATS can suggest more addresses until we call
  254. * #GNUNET_ATS_connectivity_suggest_cancel().
  255. *
  256. * @param ch handle
  257. * @param peer identity of the peer we need an address for
  258. * @return suggest handle, NULL if a request is already pending
  259. */
  260. struct GNUNET_ATS_ConnectivitySuggestHandle *
  261. GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
  262. const struct GNUNET_PeerIdentity *peer)
  263. {
  264. struct GNUNET_ATS_ConnectivitySuggestHandle *s;
  265. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  266. "Requesting ATS to suggest address for `%s'\n",
  267. GNUNET_i2s (peer));
  268. s = GNUNET_new (struct GNUNET_ATS_ConnectivitySuggestHandle);
  269. s->ch = ch;
  270. s->id = *peer;
  271. if (GNUNET_OK !=
  272. GNUNET_CONTAINER_multipeermap_put (ch->sug_requests,
  273. &s->id,
  274. s,
  275. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  276. {
  277. GNUNET_break (0);
  278. return NULL;
  279. }
  280. if (NULL == ch->mq)
  281. return s;
  282. (void) transmit_suggestion (ch,
  283. &s->id,
  284. s);
  285. return s;
  286. }
  287. /**
  288. * We no longer care about being connected to a peer.
  289. *
  290. * @param sh handle to stop
  291. */
  292. void
  293. GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
  294. {
  295. struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
  296. struct GNUNET_MQ_Envelope *ev;
  297. struct RequestAddressMessage *m;
  298. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  299. "Telling ATS we no longer care for an address for `%s'\n",
  300. GNUNET_i2s (&sh->id));
  301. GNUNET_assert (GNUNET_OK ==
  302. GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests,
  303. &sh->id,
  304. sh));
  305. if (NULL == ch->mq)
  306. {
  307. GNUNET_free (sh);
  308. return;
  309. }
  310. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
  311. m->reserved = htonl (0);
  312. m->peer = sh->id;
  313. GNUNET_MQ_send (ch->mq, ev);
  314. GNUNET_free (sh);
  315. }
  316. /* end of ats_api_connectivity.c */