arm_monitor_api.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2012, 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 arm/arm_monitor_api.c
  18. * @brief API for monitoring the ARM service
  19. * @author Christian Grothoff
  20. * @author LRN
  21. */
  22. #include "platform.h"
  23. #include "gnunet_arm_service.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_protocols.h"
  26. #include "arm.h"
  27. #define INIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  28. #define LOG(kind, ...) GNUNET_log_from (kind, "arm-monitor-api", __VA_ARGS__)
  29. /**
  30. * Handle for interacting with ARM.
  31. */
  32. struct GNUNET_ARM_MonitorHandle
  33. {
  34. /**
  35. * Our control connection to the ARM service.
  36. */
  37. struct GNUNET_MQ_Handle *mq;
  38. /**
  39. * The configuration that we are using.
  40. */
  41. const struct GNUNET_CONFIGURATION_Handle *cfg;
  42. /**
  43. * ID of the reconnect task (if any).
  44. */
  45. struct GNUNET_SCHEDULER_Task *reconnect_task;
  46. /**
  47. * Current delay we use for re-trying to connect to core.
  48. */
  49. struct GNUNET_TIME_Relative retry_backoff;
  50. /**
  51. * Callback to invoke on status updates.
  52. */
  53. GNUNET_ARM_ServiceMonitorCallback service_status;
  54. /**
  55. * Closure for @e service_status.
  56. */
  57. void *service_status_cls;
  58. };
  59. /**
  60. * Connect to the ARM service for monitoring.
  61. *
  62. * @param h handle to connect
  63. * @return #GNUNET_OK on success
  64. */
  65. static int
  66. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
  67. /**
  68. * Task scheduled to try to re-connect to arm.
  69. *
  70. * @param cls the `struct GNUNET_ARM_MonitorHandle`
  71. */
  72. static void
  73. reconnect_arm_monitor_task (void *cls)
  74. {
  75. struct GNUNET_ARM_MonitorHandle *h = cls;
  76. h->reconnect_task = NULL;
  77. LOG (GNUNET_ERROR_TYPE_DEBUG,
  78. "Connecting to ARM service for monitoring after delay\n");
  79. GNUNET_break (GNUNET_OK == reconnect_arm_monitor (h));
  80. }
  81. /**
  82. * Close down any existing connection to the ARM service and
  83. * try re-establishing it later.
  84. *
  85. * @param h our handle
  86. */
  87. static void
  88. reconnect_arm_monitor_later (struct GNUNET_ARM_MonitorHandle *h)
  89. {
  90. if (NULL != h->mq)
  91. {
  92. GNUNET_MQ_destroy (h->mq);
  93. h->mq = NULL;
  94. }
  95. GNUNET_assert (NULL == h->reconnect_task);
  96. h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
  97. &reconnect_arm_monitor_task,
  98. h);
  99. h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
  100. }
  101. /**
  102. * Check notification messages received from ARM is well-formed.
  103. *
  104. * @param cls our `struct GNUNET_ARM_MonitorHandle`
  105. * @param msg the message received from the arm service
  106. * @return #GNUNET_OK if the message is well-formed
  107. */
  108. static int
  109. check_monitor_notify (void *cls, const struct GNUNET_ARM_StatusMessage *msg)
  110. {
  111. size_t sl =
  112. ntohs (msg->header.size) - sizeof(struct GNUNET_ARM_StatusMessage);
  113. const char *name = (const char *) &msg[1];
  114. (void) cls;
  115. if ((0 == sl) || ('\0' != name[sl - 1]))
  116. {
  117. GNUNET_break (0);
  118. return GNUNET_SYSERR;
  119. }
  120. return GNUNET_OK;
  121. }
  122. /**
  123. * Handler for notification messages received from ARM.
  124. *
  125. * @param cls our `struct GNUNET_ARM_MonitorHandle`
  126. * @param res the message received from the arm service
  127. */
  128. static void
  129. handle_monitor_notify (void *cls, const struct GNUNET_ARM_StatusMessage *res)
  130. {
  131. struct GNUNET_ARM_MonitorHandle *h = cls;
  132. enum GNUNET_ARM_ServiceMonitorStatus status;
  133. status = (enum GNUNET_ARM_ServiceMonitorStatus) ntohl (res->status);
  134. LOG (GNUNET_ERROR_TYPE_DEBUG,
  135. "Received notification from ARM for service `%s' with status %d\n",
  136. (const char *) &res[1],
  137. (int) status);
  138. if (NULL != h->service_status)
  139. h->service_status (h->service_status_cls, (const char *) &res[1], status);
  140. }
  141. /**
  142. * Generic error handler, called with the appropriate error code and
  143. * the same closure specified at the creation of the message queue.
  144. * Not every message queue implementation supports an error handler.
  145. *
  146. * @param cls closure with the `struct GNUNET_ARM_MonitorHandle *`
  147. * @param error error code
  148. */
  149. static void
  150. mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
  151. {
  152. struct GNUNET_ARM_MonitorHandle *h = cls;
  153. (void) error;
  154. reconnect_arm_monitor_later (h);
  155. }
  156. /**
  157. * Connect to the ARM service for monitoring.
  158. *
  159. * @param h handle to connect
  160. * @return #GNUNET_OK on success
  161. */
  162. static int
  163. reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
  164. {
  165. struct GNUNET_MQ_MessageHandler handlers[] =
  166. { GNUNET_MQ_hd_var_size (monitor_notify,
  167. GNUNET_MESSAGE_TYPE_ARM_STATUS,
  168. struct GNUNET_ARM_StatusMessage,
  169. h),
  170. GNUNET_MQ_handler_end () };
  171. struct GNUNET_MessageHeader *msg;
  172. struct GNUNET_MQ_Envelope *env;
  173. GNUNET_assert (NULL == h->mq);
  174. h->mq = GNUNET_CLIENT_connect (h->cfg, "arm", handlers, &mq_error_handler, h);
  175. if (NULL == h->mq)
  176. {
  177. if (NULL != h->service_status)
  178. h->service_status (h->service_status_cls,
  179. NULL,
  180. GNUNET_ARM_SERVICE_STOPPED);
  181. return GNUNET_SYSERR;
  182. }
  183. env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_ARM_MONITOR);
  184. GNUNET_MQ_send (h->mq, env);
  185. return GNUNET_OK;
  186. }
  187. /**
  188. * Setup a context for monitoring ARM, then
  189. * start connecting to the ARM service for monitoring using that context.
  190. *
  191. * @param cfg configuration to use (needed to contact ARM;
  192. * the ARM service may internally use a different
  193. * configuration to determine how to start the service).
  194. * @param cont callback to invoke on status updates
  195. * @param cont_cls closure for @a cont
  196. * @return context to use for further ARM monitor operations, NULL on error.
  197. */
  198. struct GNUNET_ARM_MonitorHandle *
  199. GNUNET_ARM_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
  200. GNUNET_ARM_ServiceMonitorCallback cont,
  201. void *cont_cls)
  202. {
  203. struct GNUNET_ARM_MonitorHandle *h;
  204. h = GNUNET_new (struct GNUNET_ARM_MonitorHandle);
  205. h->cfg = cfg;
  206. h->service_status = cont;
  207. h->service_status_cls = cont_cls;
  208. if (GNUNET_OK != reconnect_arm_monitor (h))
  209. {
  210. GNUNET_free (h);
  211. return NULL;
  212. }
  213. return h;
  214. }
  215. /**
  216. * Disconnect from the ARM service (if connected) and destroy the context.
  217. *
  218. * @param h the handle that was being used
  219. */
  220. void
  221. GNUNET_ARM_monitor_stop (struct GNUNET_ARM_MonitorHandle *h)
  222. {
  223. if (NULL != h->mq)
  224. {
  225. GNUNET_MQ_destroy (h->mq);
  226. h->mq = NULL;
  227. }
  228. if (NULL != h->reconnect_task)
  229. {
  230. GNUNET_SCHEDULER_cancel (h->reconnect_task);
  231. h->reconnect_task = NULL;
  232. }
  233. GNUNET_free (h);
  234. }
  235. /* end of arm_api.c */