regex_api_announce.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 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 regex/regex_api_announce.c
  18. * @brief access regex service to advertise capabilities via regex
  19. * @author Maximilian Szengel
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_protocols.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_regex_service.h"
  26. #include "regex_ipc.h"
  27. #define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__)
  28. /**
  29. * Handle to store cached data about a regex announce.
  30. */
  31. struct GNUNET_REGEX_Announcement
  32. {
  33. /**
  34. * Connection to the regex service.
  35. */
  36. struct GNUNET_MQ_Handle *mq;
  37. /**
  38. * Our configuration.
  39. */
  40. const struct GNUNET_CONFIGURATION_Handle *cfg;
  41. /**
  42. * Message we're sending to the service.
  43. */
  44. char *regex;
  45. /**
  46. * Frequency of announcements.
  47. */
  48. struct GNUNET_TIME_Relative refresh_delay;
  49. /**
  50. * Number of characters per edge.
  51. */
  52. uint16_t compression;
  53. };
  54. /**
  55. * (Re)connect to the REGEX service with the given announcement @a a.
  56. *
  57. * @param a REGEX to announce.
  58. */
  59. static void
  60. announce_reconnect (struct GNUNET_REGEX_Announcement *a);
  61. /**
  62. * We got a disconnect after asking regex to do the announcement.
  63. * Retry.
  64. *
  65. * @param cls the `struct GNUNET_REGEX_Announcement` to retry
  66. * @param error error code
  67. */
  68. static void
  69. announce_mq_error_handler (void *cls,
  70. enum GNUNET_MQ_Error error)
  71. {
  72. struct GNUNET_REGEX_Announcement *a = cls;
  73. GNUNET_MQ_destroy (a->mq);
  74. a->mq = NULL;
  75. announce_reconnect (a);
  76. }
  77. /**
  78. * (Re)connect to the REGEX service with the given announcement @a a.
  79. *
  80. * @param a REGEX to announce.
  81. */
  82. static void
  83. announce_reconnect (struct GNUNET_REGEX_Announcement *a)
  84. {
  85. struct GNUNET_MQ_Envelope *env;
  86. struct AnnounceMessage *am;
  87. size_t slen;
  88. a->mq = GNUNET_CLIENT_connect (a->cfg,
  89. "regex",
  90. NULL,
  91. &announce_mq_error_handler,
  92. a);
  93. if (NULL == a->mq)
  94. return;
  95. slen = strlen (a->regex) + 1;
  96. env = GNUNET_MQ_msg_extra (am,
  97. slen,
  98. GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE);
  99. am->compression = htons (a->compression);
  100. am->reserved = htons (0);
  101. am->refresh_delay = GNUNET_TIME_relative_hton (a->refresh_delay);
  102. GNUNET_memcpy (&am[1],
  103. a->regex,
  104. slen);
  105. GNUNET_MQ_send (a->mq,
  106. env);
  107. }
  108. /**
  109. * Announce the given peer under the given regular expression.
  110. *
  111. * @param cfg configuration to use
  112. * @param regex Regular expression to announce.
  113. * @param refresh_delay after what delay should the announcement be repeated?
  114. * @param compression How many characters per edge can we squeeze?
  115. * @return Handle to reuse o free cached resources.
  116. * Must be freed by calling #GNUNET_REGEX_announce_cancel().
  117. */
  118. struct GNUNET_REGEX_Announcement *
  119. GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg,
  120. const char *regex,
  121. struct GNUNET_TIME_Relative refresh_delay,
  122. uint16_t compression)
  123. {
  124. struct GNUNET_REGEX_Announcement *a;
  125. size_t slen;
  126. slen = strlen (regex) + 1;
  127. if (slen + sizeof (struct AnnounceMessage) >= GNUNET_MAX_MESSAGE_SIZE)
  128. {
  129. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  130. _("Regex `%s' is too long!\n"),
  131. regex);
  132. GNUNET_break (0);
  133. return NULL;
  134. }
  135. a = GNUNET_new (struct GNUNET_REGEX_Announcement);
  136. a->cfg = cfg;
  137. a->refresh_delay = refresh_delay;
  138. a->compression = compression;
  139. a->regex = GNUNET_strdup (regex);
  140. announce_reconnect (a);
  141. if (NULL == a->mq)
  142. {
  143. GNUNET_free (a->regex);
  144. GNUNET_free (a);
  145. return NULL;
  146. }
  147. return a;
  148. }
  149. /**
  150. * Stop announcing the regex specified by the given handle.
  151. *
  152. * @param a handle returned by a previous #GNUNET_REGEX_announce() call.
  153. */
  154. void
  155. GNUNET_REGEX_announce_cancel (struct GNUNET_REGEX_Announcement *a)
  156. {
  157. GNUNET_MQ_destroy (a->mq);
  158. GNUNET_free (a->regex);
  159. GNUNET_free (a);
  160. }
  161. /* end of regex_api_announce.c */