cadet_api_list_peers.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011, 2017, 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 cadet/cadet_api_list_peers.c
  18. * @brief cadet api: client implementation of cadet service
  19. * @author Bartlomiej Polot
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_constants.h"
  25. #include "gnunet_cadet_service.h"
  26. #include "cadet.h"
  27. #include "cadet_protocol.h"
  28. /**
  29. * Operation handle.
  30. */
  31. struct GNUNET_CADET_PeersLister
  32. {
  33. /**
  34. * Monitor callback
  35. */
  36. GNUNET_CADET_PeersCB peers_cb;
  37. /**
  38. * Info callback closure for @c info_cb.
  39. */
  40. void *peers_cb_cls;
  41. /**
  42. * Message queue to talk to CADET service.
  43. */
  44. struct GNUNET_MQ_Handle *mq;
  45. /**
  46. * Configuration we use.
  47. */
  48. const struct GNUNET_CONFIGURATION_Handle *cfg;
  49. /**
  50. * Task to reconnect.
  51. */
  52. struct GNUNET_SCHEDULER_Task *reconnect_task;
  53. /**
  54. * Backoff for reconnect attempts.
  55. */
  56. struct GNUNET_TIME_Relative backoff;
  57. };
  58. /**
  59. * Process a local reply about info on all tunnels, pass info to the user.
  60. *
  61. * @param cls a `struct GNUNET_CADET_PeersLister`
  62. * @param info Message itself.
  63. */
  64. static void
  65. handle_get_peers (void *cls,
  66. const struct GNUNET_CADET_LocalInfoPeers *info)
  67. {
  68. struct GNUNET_CADET_PeersLister *pl = cls;
  69. struct GNUNET_CADET_PeerListEntry ple;
  70. ple.peer = info->destination;
  71. ple.have_tunnel = (int) ntohs (info->tunnel);
  72. ple.n_paths = (unsigned int) ntohs (info->paths);
  73. ple.best_path_length = (unsigned int) ntohl (info->best_path_length);
  74. pl->peers_cb (pl->peers_cb_cls,
  75. &ple);
  76. }
  77. /**
  78. * Process a end of list reply about info on all peers.
  79. *
  80. * @param cls a `struct GNUNET_CADET_PeersLister`
  81. * @param msg Message itself.
  82. */
  83. static void
  84. handle_get_peers_end (void *cls,
  85. const struct GNUNET_MessageHeader *msg)
  86. {
  87. struct GNUNET_CADET_PeersLister *pl = cls;
  88. (void) msg;
  89. pl->peers_cb (pl->peers_cb_cls,
  90. NULL);
  91. GNUNET_CADET_list_peers_cancel (pl);
  92. }
  93. /**
  94. * Reconnect to the service and try again.
  95. *
  96. * @param cls a `struct GNUNET_CADET_PeersLister` operation
  97. */
  98. static void
  99. reconnect (void *cls);
  100. /**
  101. * Function called on connection trouble. Reconnects.
  102. *
  103. * @param cls a `struct GNUNET_CADET_PeersLister`
  104. * @param error error code from MQ
  105. */
  106. static void
  107. error_handler (void *cls,
  108. enum GNUNET_MQ_Error error)
  109. {
  110. struct GNUNET_CADET_PeersLister *pl = cls;
  111. GNUNET_MQ_destroy (pl->mq);
  112. pl->mq = NULL;
  113. pl->backoff = GNUNET_TIME_randomized_backoff (pl->backoff,
  114. GNUNET_TIME_UNIT_MINUTES);
  115. pl->reconnect_task = GNUNET_SCHEDULER_add_delayed (pl->backoff,
  116. &reconnect,
  117. pl);
  118. }
  119. /**
  120. * Reconnect to the service and try again.
  121. *
  122. * @param cls a `struct GNUNET_CADET_PeersLister` operation
  123. */
  124. static void
  125. reconnect (void *cls)
  126. {
  127. struct GNUNET_CADET_PeersLister *pl = cls;
  128. struct GNUNET_MQ_MessageHandler handlers[] = {
  129. GNUNET_MQ_hd_fixed_size (get_peers,
  130. GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
  131. struct GNUNET_CADET_LocalInfoPeers,
  132. pl),
  133. GNUNET_MQ_hd_fixed_size (get_peers_end,
  134. GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END,
  135. struct GNUNET_MessageHeader,
  136. pl),
  137. GNUNET_MQ_handler_end ()
  138. };
  139. struct GNUNET_MessageHeader *msg;
  140. struct GNUNET_MQ_Envelope *env;
  141. pl->reconnect_task = NULL;
  142. pl->mq = GNUNET_CLIENT_connect (pl->cfg,
  143. "cadet",
  144. handlers,
  145. &error_handler,
  146. pl);
  147. if (NULL == pl->mq)
  148. return;
  149. env = GNUNET_MQ_msg (msg,
  150. GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS);
  151. GNUNET_MQ_send (pl->mq,
  152. env);
  153. }
  154. /**
  155. * Request information about peers known to the running cadet service.
  156. * The callback will be called for every peer known to the service.
  157. * Only one info request (of any kind) can be active at once.
  158. *
  159. * @param cfg configuration to use
  160. * @param callback Function to call with the requested data.
  161. * @param callback_cls Closure for @c callback.
  162. * @return NULL on error
  163. */
  164. struct GNUNET_CADET_PeersLister *
  165. GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
  166. GNUNET_CADET_PeersCB callback,
  167. void *callback_cls)
  168. {
  169. struct GNUNET_CADET_PeersLister *pl;
  170. if (NULL == callback)
  171. {
  172. GNUNET_break (0);
  173. return NULL;
  174. }
  175. pl = GNUNET_new (struct GNUNET_CADET_PeersLister);
  176. pl->peers_cb = callback;
  177. pl->peers_cb_cls = callback_cls;
  178. pl->cfg = cfg;
  179. reconnect (pl);
  180. if (NULL == pl->mq)
  181. {
  182. GNUNET_free (pl);
  183. return NULL;
  184. }
  185. return pl;
  186. }
  187. /**
  188. * Cancel a peer info request. The callback will not be called (anymore).
  189. *
  190. * @param pl operation handle
  191. * @return Closure given to GNUNET_CADET_get_peers().
  192. */
  193. void *
  194. GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl)
  195. {
  196. void *ret = pl->peers_cb_cls;
  197. if (NULL != pl->mq)
  198. GNUNET_MQ_destroy (pl->mq);
  199. if (NULL != pl->reconnect_task)
  200. GNUNET_SCHEDULER_cancel (pl->reconnect_task);
  201. GNUNET_free (pl);
  202. return ret;
  203. }
  204. /* end of cadet_api_list_peers.c */