transport_api_hello_get.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2013, 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 transport/transport_api_hello_get.c
  18. * @brief library to obtain our HELLO from our transport service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_protocols.h"
  27. #include "gnunet_transport_hello_service.h"
  28. #include "transport.h"
  29. /**
  30. * Functions to call with this peer's HELLO.
  31. */
  32. struct GNUNET_TRANSPORT_HelloGetHandle
  33. {
  34. /**
  35. * Our configuration.
  36. */
  37. const struct GNUNET_CONFIGURATION_Handle *cfg;
  38. /**
  39. * Transport handle.
  40. */
  41. struct GNUNET_MQ_Handle *mq;
  42. /**
  43. * Callback to call once we got our HELLO.
  44. */
  45. GNUNET_TRANSPORT_HelloUpdateCallback rec;
  46. /**
  47. * Closure for @e rec.
  48. */
  49. void *rec_cls;
  50. /**
  51. * Task for calling the HelloUpdateCallback when we already have a HELLO
  52. */
  53. struct GNUNET_SCHEDULER_Task *notify_task;
  54. /**
  55. * ID of the task trying to reconnect to the service.
  56. */
  57. struct GNUNET_SCHEDULER_Task *reconnect_task;
  58. /**
  59. * Delay until we try to reconnect.
  60. */
  61. struct GNUNET_TIME_Relative reconnect_delay;
  62. /**
  63. * Type of HELLOs client cares about.
  64. */
  65. enum GNUNET_TRANSPORT_AddressClass ac;
  66. };
  67. /**
  68. * Function we use for checking incoming HELLO messages.
  69. *
  70. * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
  71. * @param msg message received
  72. * @return #GNUNET_OK if message is well-formed
  73. */
  74. static int
  75. check_hello (void *cls,
  76. const struct GNUNET_MessageHeader *msg)
  77. {
  78. struct GNUNET_PeerIdentity me;
  79. if (GNUNET_OK !=
  80. GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
  81. &me))
  82. {
  83. GNUNET_break (0);
  84. return GNUNET_SYSERR;
  85. }
  86. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  87. "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
  88. (unsigned int) ntohs (msg->size),
  89. GNUNET_i2s (&me));
  90. return GNUNET_OK;
  91. }
  92. /**
  93. * Function we use for handling incoming HELLO messages.
  94. *
  95. * @param cls closure, a `struct GNUNET_TRANSPORT_HelloGetHandle *`
  96. * @param msg message received
  97. */
  98. static void
  99. handle_hello (void *cls,
  100. const struct GNUNET_MessageHeader *msg)
  101. {
  102. struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
  103. ghh->rec (ghh->rec_cls,
  104. msg);
  105. }
  106. /**
  107. * Function that will schedule the job that will try
  108. * to connect us again to the client.
  109. *
  110. * @param ghh transport service to reconnect
  111. */
  112. static void
  113. schedule_reconnect (struct GNUNET_TRANSPORT_HelloGetHandle *ghh);
  114. /**
  115. * Generic error handler, called with the appropriate
  116. * error code and the same closure specified at the creation of
  117. * the message queue.
  118. * Not every message queue implementation supports an error handler.
  119. *
  120. * @param cls closure with the `struct GNUNET_TRANSPORT_Handle *`
  121. * @param error error code
  122. */
  123. static void
  124. mq_error_handler (void *cls,
  125. enum GNUNET_MQ_Error error)
  126. {
  127. struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
  128. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  129. "Error receiving from transport service, disconnecting temporarily.\n");
  130. GNUNET_MQ_destroy (ghh->mq);
  131. ghh->mq = NULL;
  132. schedule_reconnect (ghh);
  133. }
  134. /**
  135. * Try again to connect to transport service.
  136. *
  137. * @param cls the handle to the transport service
  138. */
  139. static void
  140. reconnect (void *cls)
  141. {
  142. struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
  143. struct GNUNET_MQ_MessageHandler handlers[] = {
  144. GNUNET_MQ_hd_var_size (hello,
  145. GNUNET_MESSAGE_TYPE_HELLO,
  146. struct GNUNET_MessageHeader,
  147. ghh),
  148. GNUNET_MQ_handler_end ()
  149. };
  150. struct GNUNET_MQ_Envelope *env;
  151. struct StartMessage *s;
  152. ghh->reconnect_task = NULL;
  153. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  154. "Connecting to transport service.\n");
  155. GNUNET_assert (NULL == ghh->mq);
  156. ghh->mq = GNUNET_CLIENT_connect (ghh->cfg,
  157. "transport",
  158. handlers,
  159. &mq_error_handler,
  160. ghh);
  161. if (NULL == ghh->mq)
  162. return;
  163. env = GNUNET_MQ_msg (s,
  164. GNUNET_MESSAGE_TYPE_TRANSPORT_START);
  165. s->options = htonl (0);
  166. GNUNET_MQ_send (ghh->mq,
  167. env);
  168. }
  169. /**
  170. * Function that will schedule the job that will try
  171. * to connect us again to the client.
  172. *
  173. * @param ghh transport service to reconnect
  174. */
  175. static void
  176. schedule_reconnect (struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
  177. {
  178. ghh->reconnect_task =
  179. GNUNET_SCHEDULER_add_delayed (ghh->reconnect_delay,
  180. &reconnect,
  181. ghh);
  182. ghh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (ghh->reconnect_delay);
  183. }
  184. /**
  185. * Obtain the HELLO message for this peer. The callback given in this function
  186. * is never called synchronously.
  187. *
  188. * @param cfg configuration
  189. * @param ac which network type should the addresses from the HELLO belong to?
  190. * @param rec function to call with the HELLO, sender will be our peer
  191. * identity; message and sender will be NULL on timeout
  192. * (handshake with transport service pending/failed).
  193. * cost estimate will be 0.
  194. * @param rec_cls closure for @a rec
  195. * @return handle to cancel the operation
  196. */
  197. struct GNUNET_TRANSPORT_HelloGetHandle *
  198. GNUNET_TRANSPORT_hello_get (const struct GNUNET_CONFIGURATION_Handle *cfg,
  199. enum GNUNET_TRANSPORT_AddressClass ac,
  200. GNUNET_TRANSPORT_HelloUpdateCallback rec,
  201. void *rec_cls)
  202. {
  203. struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
  204. ghh = GNUNET_new (struct GNUNET_TRANSPORT_HelloGetHandle);
  205. ghh->rec = rec;
  206. ghh->rec_cls = rec_cls;
  207. ghh->cfg = cfg;
  208. ghh->ac = ac;
  209. reconnect (ghh);
  210. if (NULL == ghh->mq)
  211. {
  212. GNUNET_free (ghh);
  213. return NULL;
  214. }
  215. return ghh;
  216. }
  217. /**
  218. * Stop receiving updates about changes to our HELLO message.
  219. *
  220. * @param ghh handle to cancel
  221. */
  222. void
  223. GNUNET_TRANSPORT_hello_get_cancel (struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
  224. {
  225. if (NULL != ghh->reconnect_task)
  226. {
  227. GNUNET_SCHEDULER_cancel (ghh->reconnect_task);
  228. ghh->reconnect_task = NULL;
  229. }
  230. if (NULL != ghh->mq)
  231. {
  232. GNUNET_MQ_destroy (ghh->mq);
  233. ghh->mq = NULL;
  234. }
  235. GNUNET_free (ghh);
  236. }
  237. /* end of transport_api_hello_get.c */