transport_api2_application.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010--2019 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_api2_application.c
  18. * @brief enable clients to ask TRANSPORT about establishing connections to peers
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. */
  22. #include "platform.h"
  23. #include "gnunet_transport_application_service.h"
  24. #include "gnunet_transport_core_service.h"
  25. #include "transport.h"
  26. #define LOG(kind, ...) \
  27. GNUNET_log_from (kind, "transport-application-api", __VA_ARGS__)
  28. /**
  29. * Handle for TRANSPORT address suggestion requests.
  30. */
  31. struct GNUNET_TRANSPORT_ApplicationSuggestHandle
  32. {
  33. /**
  34. * ID of the peer for which address suggestion was requested.
  35. */
  36. struct GNUNET_PeerIdentity id;
  37. /**
  38. * Connecitivity handle this suggestion handle belongs to.
  39. */
  40. struct GNUNET_TRANSPORT_ApplicationHandle *ch;
  41. /**
  42. * What preference is being expressed?
  43. */
  44. enum GNUNET_MQ_PriorityPreferences pk;
  45. /**
  46. * How much bandwidth does the client expect?
  47. */
  48. struct GNUNET_BANDWIDTH_Value32NBO bw;
  49. };
  50. /**
  51. * Handle to the TRANSPORT subsystem for application management.
  52. */
  53. struct GNUNET_TRANSPORT_ApplicationHandle
  54. {
  55. /**
  56. * Our configuration.
  57. */
  58. const struct GNUNET_CONFIGURATION_Handle *cfg;
  59. /**
  60. * Map with the identities of all the peers for which we would
  61. * like to have address suggestions. The key is the PID, the
  62. * value is currently the `struct GNUNET_TRANSPORT_ApplicationSuggestHandle`
  63. */
  64. struct GNUNET_CONTAINER_MultiPeerMap *sug_requests;
  65. /**
  66. * Message queue for sending requests to the TRANSPORT service.
  67. */
  68. struct GNUNET_MQ_Handle *mq;
  69. /**
  70. * Task to trigger reconnect.
  71. */
  72. struct GNUNET_SCHEDULER_Task *task;
  73. /**
  74. * Reconnect backoff delay.
  75. */
  76. struct GNUNET_TIME_Relative backoff;
  77. };
  78. /**
  79. * Re-establish the connection to the TRANSPORT service.
  80. *
  81. * @param ch handle to use to re-connect.
  82. */
  83. static void
  84. reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch);
  85. /**
  86. * Re-establish the connection to the TRANSPORT service.
  87. *
  88. * @param cls handle to use to re-connect.
  89. */
  90. static void
  91. reconnect_task (void *cls)
  92. {
  93. struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
  94. ch->task = NULL;
  95. reconnect (ch);
  96. }
  97. /**
  98. * Disconnect from TRANSPORT and then reconnect.
  99. *
  100. * @param ch our handle
  101. */
  102. static void
  103. force_reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
  104. {
  105. if (NULL != ch->mq)
  106. {
  107. GNUNET_MQ_destroy (ch->mq);
  108. ch->mq = NULL;
  109. }
  110. ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
  111. ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, &reconnect_task, ch);
  112. }
  113. /**
  114. * We encountered an error handling the MQ to the
  115. * TRANSPORT service. Reconnect.
  116. *
  117. * @param cls the `struct GNUNET_TRANSPORT_ApplicationHandle`
  118. * @param error details about the error
  119. */
  120. static void
  121. error_handler (void *cls, enum GNUNET_MQ_Error error)
  122. {
  123. struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
  124. LOG (GNUNET_ERROR_TYPE_DEBUG,
  125. "TRANSPORT connection died (code %d), reconnecting\n",
  126. (int) error);
  127. force_reconnect (ch);
  128. }
  129. /**
  130. * Transmit request for an address suggestion.
  131. *
  132. * @param cls the `struct GNUNET_TRANSPORT_ApplicationHandle`
  133. * @param peer peer to ask for an address suggestion for
  134. * @param value the `struct GNUNET_TRANSPORT_SuggestHandle`
  135. * @return #GNUNET_OK (continue to iterate), #GNUNET_SYSERR on
  136. * failure (message queue no longer exists)
  137. */
  138. static int
  139. transmit_suggestion (void *cls,
  140. const struct GNUNET_PeerIdentity *peer,
  141. void *value)
  142. {
  143. struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
  144. struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh = value;
  145. struct GNUNET_MQ_Envelope *ev;
  146. struct ExpressPreferenceMessage *m;
  147. if (NULL == ch->mq)
  148. return GNUNET_SYSERR;
  149. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_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 TRANSPORT service.
  158. *
  159. * @param ch handle to use to re-connect.
  160. */
  161. static void
  162. reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
  163. {
  164. static const struct GNUNET_MQ_MessageHandler handlers[] = { { NULL, 0, 0 } };
  165. GNUNET_assert (NULL == ch->mq);
  166. ch->mq =
  167. GNUNET_CLIENT_connect (ch->cfg, "transport", handlers, &error_handler, ch);
  168. if (NULL == ch->mq)
  169. {
  170. force_reconnect (ch);
  171. return;
  172. }
  173. GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
  174. &transmit_suggestion,
  175. ch);
  176. }
  177. /**
  178. * Initialize the TRANSPORT application suggestion client handle.
  179. *
  180. * @param cfg configuration to use
  181. * @return transport application handle, NULL on error
  182. */
  183. struct GNUNET_TRANSPORT_ApplicationHandle *
  184. GNUNET_TRANSPORT_application_init (
  185. const struct GNUNET_CONFIGURATION_Handle *cfg)
  186. {
  187. struct GNUNET_TRANSPORT_ApplicationHandle *ch;
  188. ch = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationHandle);
  189. ch->cfg = cfg;
  190. ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  191. reconnect (ch);
  192. return ch;
  193. }
  194. /**
  195. * Function called to free all `struct GNUNET_TRANSPORT_ApplicationSuggestHandle`s
  196. * in the map.
  197. *
  198. * @param cls NULL
  199. * @param key the key
  200. * @param value the value to free
  201. * @return #GNUNET_OK (continue to iterate)
  202. */
  203. static int
  204. free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  205. {
  206. struct GNUNET_TRANSPORT_ApplicationSuggestHandle *cur = value;
  207. GNUNET_free (cur);
  208. return GNUNET_OK;
  209. }
  210. /**
  211. * Client is done with TRANSPORT application management, release resources.
  212. *
  213. * @param ch handle to release
  214. */
  215. void
  216. GNUNET_TRANSPORT_application_done (
  217. struct GNUNET_TRANSPORT_ApplicationHandle *ch)
  218. {
  219. if (NULL != ch->mq)
  220. {
  221. GNUNET_MQ_destroy (ch->mq);
  222. ch->mq = NULL;
  223. }
  224. if (NULL != ch->task)
  225. {
  226. GNUNET_SCHEDULER_cancel (ch->task);
  227. ch->task = NULL;
  228. }
  229. GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests,
  230. &free_sug_handle,
  231. NULL);
  232. GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests);
  233. GNUNET_free (ch);
  234. }
  235. /**
  236. * An application would like TRANSPORT to connect to a peer.
  237. *
  238. * @param ch handle
  239. * @param peer identity of the peer we need an address for
  240. * @param pk what kind of application will the application require (can be
  241. * #GNUNET_MQ_PRIO_BACKGROUND, we will still try to connect)
  242. * @param bw desired bandwith, can be zero (we will still try to connect)
  243. * @return suggest handle, NULL if a request is already pending
  244. */
  245. struct GNUNET_TRANSPORT_ApplicationSuggestHandle *
  246. GNUNET_TRANSPORT_application_suggest (
  247. struct GNUNET_TRANSPORT_ApplicationHandle *ch,
  248. const struct GNUNET_PeerIdentity *peer,
  249. enum GNUNET_MQ_PriorityPreferences pk,
  250. struct GNUNET_BANDWIDTH_Value32NBO bw)
  251. {
  252. struct GNUNET_TRANSPORT_ApplicationSuggestHandle *s;
  253. s = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationSuggestHandle);
  254. s->ch = ch;
  255. s->id = *peer;
  256. s->pk = pk;
  257. s->bw = bw;
  258. (void) GNUNET_CONTAINER_multipeermap_put (
  259. ch->sug_requests,
  260. &s->id,
  261. s,
  262. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  263. LOG (GNUNET_ERROR_TYPE_DEBUG,
  264. "Requesting TRANSPORT to suggest address for `%s'\n",
  265. GNUNET_i2s (peer));
  266. if (NULL == ch->mq)
  267. return s;
  268. GNUNET_assert (GNUNET_OK == transmit_suggestion (ch, &s->id, s));
  269. return s;
  270. }
  271. /**
  272. * We no longer care about being connected to a peer.
  273. *
  274. * @param sh handle to stop
  275. */
  276. void
  277. GNUNET_TRANSPORT_application_suggest_cancel (
  278. struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
  279. {
  280. struct GNUNET_TRANSPORT_ApplicationHandle *ch = sh->ch;
  281. struct GNUNET_MQ_Envelope *ev;
  282. struct ExpressPreferenceMessage *m;
  283. LOG (GNUNET_ERROR_TYPE_DEBUG,
  284. "Telling TRANSPORT we no longer care for an address for `%s'\n",
  285. GNUNET_i2s (&sh->id));
  286. GNUNET_assert (
  287. GNUNET_OK ==
  288. GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, &sh->id, sh));
  289. if (NULL == ch->mq)
  290. {
  291. GNUNET_free (sh);
  292. return;
  293. }
  294. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL);
  295. m->pk = htonl ((uint32_t) sh->pk);
  296. m->bw = sh->bw;
  297. m->peer = sh->id;
  298. GNUNET_MQ_send (ch->mq, ev);
  299. GNUNET_free (sh);
  300. }
  301. /**
  302. * An application (or a communicator) has received a HELLO (or other address
  303. * data of another peer) and wants TRANSPORT to validate that the address is
  304. * correct. The result is NOT returned, in fact TRANSPORT may do nothing
  305. * (i.e. if it has too many active validations or recently tried this one
  306. * already). If the @a addr validates, TRANSPORT will persist the address
  307. * with PEERSTORE.
  308. *
  309. * @param ch handle
  310. * @param peer identity of the peer we have an address for
  311. * @param nt network type of @a addr (as claimed by the other peer);
  312. * used by TRANSPORT to avoid trying @a addr's that really cannot work
  313. * due to network type missmatches
  314. * @param addr address to validate
  315. */
  316. void
  317. GNUNET_TRANSPORT_application_validate (
  318. struct GNUNET_TRANSPORT_ApplicationHandle *ch,
  319. const struct GNUNET_PeerIdentity *peer,
  320. enum GNUNET_NetworkType nt,
  321. const char *addr)
  322. {
  323. struct GNUNET_MQ_Envelope *ev;
  324. struct RequestHelloValidationMessage *m;
  325. size_t alen;
  326. if (NULL == ch->mq)
  327. {
  328. GNUNET_log (
  329. GNUNET_ERROR_TYPE_WARNING,
  330. "Address validation for %s:%s skipped as transport is not connected\n",
  331. GNUNET_i2s (peer),
  332. addr);
  333. return;
  334. }
  335. alen = strlen (addr) + 1;
  336. ev =
  337. GNUNET_MQ_msg_extra (m,
  338. alen,
  339. GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION);
  340. m->peer = *peer;
  341. m->nt = htonl ((uint32_t) nt);
  342. memcpy (&m[1], addr, alen);
  343. GNUNET_MQ_send (ch->mq, ev);
  344. }
  345. /* end of transport_api2_application.c */