ats_api2_application.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_api2_application.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_application_service.h"
  24. #include "ats2.h"
  25. #define LOG(kind, ...) GNUNET_log_from(kind, "ats-application-api", __VA_ARGS__)
  26. /**
  27. * Handle for ATS address suggestion requests.
  28. */
  29. struct GNUNET_ATS_ApplicationSuggestHandle {
  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_ApplicationHandle *ch;
  38. /**
  39. * What preference is being expressed?
  40. */
  41. enum GNUNET_MQ_PreferenceKind pk;
  42. /**
  43. * How much bandwidth does the client expect?
  44. */
  45. struct GNUNET_BANDWIDTH_Value32NBO bw;
  46. };
  47. /**
  48. * Handle to the ATS subsystem for application management.
  49. */
  50. struct GNUNET_ATS_ApplicationHandle {
  51. /**
  52. * Our configuration.
  53. */
  54. const struct GNUNET_CONFIGURATION_Handle *cfg;
  55. /**
  56. * Map with the identities of all the peers for which we would
  57. * like to have address suggestions. The key is the PID, the
  58. * value is currently the `struct GNUNET_ATS_ApplicationSuggestHandle`
  59. */
  60. struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
  61. /**
  62. * Message queue for sending requests to the ATS service.
  63. */
  64. struct GNUNET_MQ_Handle *mq;
  65. /**
  66. * Task to trigger reconnect.
  67. */
  68. struct GNUNET_SCHEDULER_Task *task;
  69. /**
  70. * Reconnect backoff delay.
  71. */
  72. struct GNUNET_TIME_Relative backoff;
  73. };
  74. /**
  75. * Re-establish the connection to the ATS service.
  76. *
  77. * @param ch handle to use to re-connect.
  78. */
  79. static void
  80. reconnect(struct GNUNET_ATS_ApplicationHandle *ch);
  81. /**
  82. * Re-establish the connection to the ATS service.
  83. *
  84. * @param cls handle to use to re-connect.
  85. */
  86. static void
  87. reconnect_task(void *cls)
  88. {
  89. struct GNUNET_ATS_ApplicationHandle *ch = cls;
  90. ch->task = NULL;
  91. reconnect(ch);
  92. }
  93. /**
  94. * Disconnect from ATS and then reconnect.
  95. *
  96. * @param ch our handle
  97. */
  98. static void
  99. force_reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
  100. {
  101. if (NULL != ch->mq)
  102. {
  103. GNUNET_MQ_destroy(ch->mq);
  104. ch->mq = NULL;
  105. }
  106. ch->backoff = GNUNET_TIME_STD_BACKOFF(ch->backoff);
  107. ch->task = GNUNET_SCHEDULER_add_delayed(ch->backoff,
  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_ApplicationHandle`
  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_ApplicationHandle *ch = cls;
  123. LOG(GNUNET_ERROR_TYPE_DEBUG,
  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_ApplicationHandle`
  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_ApplicationHandle *ch = cls;
  143. struct GNUNET_ATS_ApplicationSuggestHandle *sh = value;
  144. struct GNUNET_MQ_Envelope *ev;
  145. struct ExpressPreferenceMessage *m;
  146. if (NULL == ch->mq)
  147. return GNUNET_SYSERR;
  148. ev = GNUNET_MQ_msg(m,
  149. GNUNET_MESSAGE_TYPE_ATS_SUGGEST);
  150. m->pk = htonl((uint32_t)sh->pk);
  151. m->bw = sh->bw;
  152. m->peer = *peer;
  153. GNUNET_MQ_send(ch->mq, ev);
  154. return GNUNET_OK;
  155. }
  156. /**
  157. * Re-establish the connection to the ATS service.
  158. *
  159. * @param ch handle to use to re-connect.
  160. */
  161. static void
  162. reconnect(struct GNUNET_ATS_ApplicationHandle *ch)
  163. {
  164. static const struct GNUNET_MQ_MessageHandler handlers[] = {
  165. { NULL, 0, 0 }
  166. };
  167. GNUNET_assert(NULL == ch->mq);
  168. ch->mq = GNUNET_CLIENT_connect(ch->cfg,
  169. "ats",
  170. handlers,
  171. &error_handler,
  172. ch);
  173. if (NULL == ch->mq)
  174. {
  175. force_reconnect(ch);
  176. return;
  177. }
  178. GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
  179. &transmit_suggestion,
  180. ch);
  181. }
  182. /**
  183. * Initialize the ATS application suggestion client handle.
  184. *
  185. * @param cfg configuration to use
  186. * @return ats application handle, NULL on error
  187. */
  188. struct GNUNET_ATS_ApplicationHandle *
  189. GNUNET_ATS_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
  190. {
  191. struct GNUNET_ATS_ApplicationHandle *ch;
  192. ch = GNUNET_new(struct GNUNET_ATS_ApplicationHandle);
  193. ch->cfg = cfg;
  194. ch->sug_requests = GNUNET_CONTAINER_multipeermap_create(32,
  195. GNUNET_YES);
  196. reconnect(ch);
  197. return ch;
  198. }
  199. /**
  200. * Function called to free all `struct GNUNET_ATS_ApplicationSuggestHandle`s
  201. * in the map.
  202. *
  203. * @param cls NULL
  204. * @param key the key
  205. * @param value the value to free
  206. * @return #GNUNET_OK (continue to iterate)
  207. */
  208. static int
  209. free_sug_handle(void *cls,
  210. const struct GNUNET_PeerIdentity *key,
  211. void *value)
  212. {
  213. struct GNUNET_ATS_ApplicationSuggestHandle *cur = value;
  214. GNUNET_free(cur);
  215. return GNUNET_OK;
  216. }
  217. /**
  218. * Client is done with ATS application management, release resources.
  219. *
  220. * @param ch handle to release
  221. */
  222. void
  223. GNUNET_ATS_application_done(struct GNUNET_ATS_ApplicationHandle *ch)
  224. {
  225. if (NULL != ch->mq)
  226. {
  227. GNUNET_MQ_destroy(ch->mq);
  228. ch->mq = NULL;
  229. }
  230. if (NULL != ch->task)
  231. {
  232. GNUNET_SCHEDULER_cancel(ch->task);
  233. ch->task = NULL;
  234. }
  235. GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
  236. &free_sug_handle,
  237. NULL);
  238. GNUNET_CONTAINER_multipeermap_destroy(ch->sug_requests);
  239. GNUNET_free(ch);
  240. }
  241. /**
  242. * We would like to receive address suggestions for a peer. ATS will
  243. * respond with a call to the continuation immediately containing an address or
  244. * no address if none is available. ATS can suggest more addresses until we call
  245. * #GNUNET_ATS_application_suggest_cancel().
  246. *
  247. * @param ch handle
  248. * @param peer identity of the peer we need an address for
  249. * @param pk what kind of application will the application require (can be
  250. * #GNUNET_MQ_PREFERENCE_NONE, we will still try to connect)
  251. * @param bw desired bandwith, can be zero (we will still try to connect)
  252. * @return suggest handle, NULL if a request is already pending
  253. */
  254. struct GNUNET_ATS_ApplicationSuggestHandle *
  255. GNUNET_ATS_application_suggest(struct GNUNET_ATS_ApplicationHandle *ch,
  256. const struct GNUNET_PeerIdentity *peer,
  257. enum GNUNET_MQ_PreferenceKind pk,
  258. struct GNUNET_BANDWIDTH_Value32NBO bw)
  259. {
  260. struct GNUNET_ATS_ApplicationSuggestHandle *s;
  261. s = GNUNET_new(struct GNUNET_ATS_ApplicationSuggestHandle);
  262. s->ch = ch;
  263. s->id = *peer;
  264. s->pk = pk;
  265. s->bw = bw;
  266. (void)GNUNET_CONTAINER_multipeermap_put(ch->sug_requests,
  267. &s->id,
  268. s,
  269. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  270. LOG(GNUNET_ERROR_TYPE_DEBUG,
  271. "Requesting ATS to suggest address for `%s'\n",
  272. GNUNET_i2s(peer));
  273. if (NULL == ch->mq)
  274. return s;
  275. GNUNET_assert(GNUNET_OK ==
  276. transmit_suggestion(ch,
  277. &s->id,
  278. s));
  279. return s;
  280. }
  281. /**
  282. * We no longer care about being connected to a peer.
  283. *
  284. * @param sh handle to stop
  285. */
  286. void
  287. GNUNET_ATS_application_suggest_cancel(struct GNUNET_ATS_ApplicationSuggestHandle *sh)
  288. {
  289. struct GNUNET_ATS_ApplicationHandle *ch = sh->ch;
  290. struct GNUNET_MQ_Envelope *ev;
  291. struct ExpressPreferenceMessage *m;
  292. LOG(GNUNET_ERROR_TYPE_DEBUG,
  293. "Telling ATS we no longer care for an address for `%s'\n",
  294. GNUNET_i2s(&sh->id));
  295. GNUNET_assert(GNUNET_OK ==
  296. GNUNET_CONTAINER_multipeermap_remove(ch->sug_requests,
  297. &sh->id,
  298. sh));
  299. if (NULL == ch->mq)
  300. {
  301. GNUNET_free(sh);
  302. return;
  303. }
  304. ev = GNUNET_MQ_msg(m,
  305. GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL);
  306. m->pk = htonl((uint32_t)sh->pk);
  307. m->bw = sh->bw;
  308. m->peer = sh->id;
  309. GNUNET_MQ_send(ch->mq,
  310. ev);
  311. GNUNET_free(sh);
  312. }
  313. /* end of ats_api2_application.c */