peerinfo_api_notify.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 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 peerinfo/peerinfo_api_notify.c
  18. * @brief notify API to access peerinfo service
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_peerinfo_service.h"
  24. #include "gnunet_protocols.h"
  25. #include "peerinfo.h"
  26. #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
  27. /**
  28. * Context for the info handler.
  29. */
  30. struct GNUNET_PEERINFO_NotifyContext
  31. {
  32. /**
  33. * Our connection to the PEERINFO service.
  34. */
  35. struct GNUNET_MQ_Handle *mq;
  36. /**
  37. * Function to call with information.
  38. */
  39. GNUNET_PEERINFO_Processor callback;
  40. /**
  41. * Closure for @e callback.
  42. */
  43. void *callback_cls;
  44. /**
  45. * Configuration.
  46. */
  47. const struct GNUNET_CONFIGURATION_Handle *cfg;
  48. /**
  49. * Tasked used for delayed re-connection attempt.
  50. */
  51. struct GNUNET_SCHEDULER_Task *task;
  52. /**
  53. * Include friend only HELLOs in callbacks
  54. */
  55. int include_friend_only;
  56. };
  57. /**
  58. * Task to re-try connecting to peerinfo.
  59. *
  60. * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
  61. */
  62. static void
  63. reconnect (void *cls);
  64. /**
  65. * We encountered an error, reconnect to the service.
  66. *
  67. * @param nc context to reconnect
  68. */
  69. static void
  70. do_reconnect (struct GNUNET_PEERINFO_NotifyContext *nc)
  71. {
  72. GNUNET_MQ_destroy (nc->mq);
  73. nc->mq = NULL;
  74. nc->task = GNUNET_SCHEDULER_add_now (&reconnect,
  75. nc);
  76. }
  77. /**
  78. * We got a disconnect after asking regex to do the announcement.
  79. * Retry.
  80. *
  81. * @param cls the `struct GNUNET_PEERINFO_NotifyContext` to retry
  82. * @param error error code
  83. */
  84. static void
  85. mq_error_handler (void *cls,
  86. enum GNUNET_MQ_Error error)
  87. {
  88. struct GNUNET_PEERINFO_NotifyContext *nc = cls;
  89. do_reconnect (nc);
  90. }
  91. /**
  92. * Check that a peerinfo information message is well-formed.
  93. *
  94. * @param cls closure
  95. * @param im message received
  96. * @return #GNUNET_OK if the message is well-formed
  97. */
  98. static int
  99. check_notification (void *cls,
  100. const struct InfoMessage *im)
  101. {
  102. uint16_t ms = ntohs (im->header.size) - sizeof (*im);
  103. if (ms >= sizeof (struct GNUNET_MessageHeader))
  104. {
  105. const struct GNUNET_HELLO_Message *hello;
  106. hello = (const struct GNUNET_HELLO_Message *) &im[1];
  107. if (ms != GNUNET_HELLO_size (hello))
  108. {
  109. GNUNET_break (0);
  110. return GNUNET_SYSERR;
  111. }
  112. return GNUNET_OK;
  113. }
  114. if (0 != ms)
  115. {
  116. GNUNET_break (0);
  117. return GNUNET_SYSERR;
  118. }
  119. return GNUNET_OK; /* odd... */
  120. }
  121. /**
  122. * Receive a peerinfo information message, process it.
  123. *
  124. * @param cls closure
  125. * @param im message received
  126. */
  127. static void
  128. handle_notification (void *cls,
  129. const struct InfoMessage *im)
  130. {
  131. struct GNUNET_PEERINFO_NotifyContext *nc = cls;
  132. const struct GNUNET_HELLO_Message *hello;
  133. uint16_t ms = ntohs (im->header.size) - sizeof (struct InfoMessage);
  134. if (0 == ms)
  135. return;
  136. hello = (const struct GNUNET_HELLO_Message *) &im[1];
  137. LOG (GNUNET_ERROR_TYPE_DEBUG,
  138. "Received information about peer `%s' from peerinfo database\n",
  139. GNUNET_i2s (&im->peer));
  140. nc->callback (nc->callback_cls,
  141. &im->peer,
  142. hello,
  143. NULL);
  144. }
  145. /**
  146. * Type of a function to call when we receive a message from the
  147. * service. Call the iterator with the result and (if applicable)
  148. * continue to receive more messages or trigger processing the next
  149. * event (if applicable).
  150. *
  151. * @param cls closure
  152. * @param msg message received, NULL on timeout or fatal error
  153. */
  154. static void
  155. handle_end_iteration (void *cls,
  156. const struct GNUNET_MessageHeader *msg)
  157. {
  158. /* these are ignored by the notify API */
  159. }
  160. /**
  161. * Task to re-try connecting to peerinfo.
  162. *
  163. * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
  164. */
  165. static void
  166. reconnect (void *cls)
  167. {
  168. struct GNUNET_PEERINFO_NotifyContext *nc = cls;
  169. struct GNUNET_MQ_MessageHandler handlers[] = {
  170. GNUNET_MQ_hd_var_size (notification,
  171. GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
  172. struct InfoMessage,
  173. nc),
  174. GNUNET_MQ_hd_fixed_size (end_iteration,
  175. GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
  176. struct GNUNET_MessageHeader,
  177. nc),
  178. GNUNET_MQ_handler_end ()
  179. };
  180. struct GNUNET_MQ_Envelope *env;
  181. struct NotifyMessage *nm;
  182. nc->task = NULL;
  183. nc->mq = GNUNET_CLIENT_connect (nc->cfg,
  184. "peerinfo",
  185. handlers,
  186. &mq_error_handler,
  187. nc);
  188. if (NULL == nc->mq)
  189. return;
  190. env = GNUNET_MQ_msg (nm,
  191. GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
  192. nm->include_friend_only = htonl (nc->include_friend_only);
  193. GNUNET_MQ_send (nc->mq,
  194. env);
  195. }
  196. /**
  197. * Call a method whenever our known information about peers
  198. * changes. Initially calls the given function for all known
  199. * peers and then only signals changes.
  200. *
  201. * If @a include_friend_only is set to #GNUNET_YES peerinfo will include HELLO
  202. * messages which are intended for friend to friend mode and which do not
  203. * have to be gossiped. Otherwise these messages are skipped.
  204. *
  205. * @param cfg configuration to use
  206. * @param include_friend_only include HELLO messages for friends only
  207. * @param callback the method to call for each peer
  208. * @param callback_cls closure for @a callback
  209. * @return NULL on error
  210. */
  211. struct GNUNET_PEERINFO_NotifyContext *
  212. GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
  213. int include_friend_only,
  214. GNUNET_PEERINFO_Processor callback,
  215. void *callback_cls)
  216. {
  217. struct GNUNET_PEERINFO_NotifyContext *nc;
  218. nc = GNUNET_new (struct GNUNET_PEERINFO_NotifyContext);
  219. nc->cfg = cfg;
  220. nc->callback = callback;
  221. nc->callback_cls = callback_cls;
  222. nc->include_friend_only = include_friend_only;
  223. reconnect (nc);
  224. if (NULL == nc->mq)
  225. {
  226. LOG (GNUNET_ERROR_TYPE_WARNING,
  227. "Could not connect to PEERINFO service.\n");
  228. GNUNET_free (nc);
  229. return NULL;
  230. }
  231. return nc;
  232. }
  233. /**
  234. * Stop notifying about changes.
  235. *
  236. * @param nc context to stop notifying
  237. */
  238. void
  239. GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
  240. {
  241. if (NULL != nc->mq)
  242. {
  243. GNUNET_MQ_destroy (nc->mq);
  244. nc->mq = NULL;
  245. }
  246. if (NULL != nc->task)
  247. {
  248. GNUNET_SCHEDULER_cancel (nc->task);
  249. nc->task = NULL;
  250. }
  251. GNUNET_free (nc);
  252. }
  253. /* end of peerinfo_api_notify.c */