regex_api_search.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_search.c
  18. * @brief access regex service to discover
  19. * peers using matching strings
  20. * @author Maximilian Szengel
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_protocols.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_regex_service.h"
  27. #include "regex_ipc.h"
  28. #define LOG(kind, ...) GNUNET_log_from (kind, "regex-api", __VA_ARGS__)
  29. /**
  30. * Handle to store data about a regex search.
  31. */
  32. struct GNUNET_REGEX_Search
  33. {
  34. /**
  35. * Connection to the regex service.
  36. */
  37. struct GNUNET_MQ_Handle *mq;
  38. /**
  39. * Our configuration.
  40. */
  41. const struct GNUNET_CONFIGURATION_Handle *cfg;
  42. /**
  43. * Function to call with results.
  44. */
  45. GNUNET_REGEX_Found callback;
  46. /**
  47. * Closure for @e callback.
  48. */
  49. void *callback_cls;
  50. /**
  51. * Search string to transmit to the service.
  52. */
  53. char *string;
  54. };
  55. /**
  56. * (Re)connect to the REGEX service for the given search @a s.
  57. *
  58. * @param s context for the search search for
  59. */
  60. static void
  61. search_reconnect (struct GNUNET_REGEX_Search *s);
  62. /**
  63. * We got a response or disconnect after asking regex
  64. * to do the search. Check it is well-formed.
  65. *
  66. * @param cls the `struct GNUNET_REGEX_Search` to handle reply for
  67. * @param result the message
  68. * @return #GNUNET_SYSERR if @a rm is not well-formed.
  69. */
  70. static int
  71. check_search_response (void *cls,
  72. const struct ResultMessage *result)
  73. {
  74. uint16_t size = ntohs (result->header.size) - sizeof(*result);
  75. uint16_t gpl = ntohs (result->get_path_length);
  76. uint16_t ppl = ntohs (result->put_path_length);
  77. if (size != (gpl + ppl) * sizeof(struct GNUNET_PeerIdentity))
  78. {
  79. GNUNET_break (0);
  80. return GNUNET_SYSERR;
  81. }
  82. return GNUNET_OK;
  83. }
  84. /**
  85. * We got a response or disconnect after asking regex
  86. * to do the search. Handle it.
  87. *
  88. * @param cls the `struct GNUNET_REGEX_Search` to handle reply for
  89. * @param result the message
  90. */
  91. static void
  92. handle_search_response (void *cls,
  93. const struct ResultMessage *result)
  94. {
  95. struct GNUNET_REGEX_Search *s = cls;
  96. uint16_t gpl = ntohs (result->get_path_length);
  97. uint16_t ppl = ntohs (result->put_path_length);
  98. const struct GNUNET_PeerIdentity *pid;
  99. pid = &result->id;
  100. LOG (GNUNET_ERROR_TYPE_DEBUG,
  101. "Got regex result %s\n",
  102. GNUNET_i2s (pid));
  103. s->callback (s->callback_cls,
  104. pid,
  105. &pid[1],
  106. gpl,
  107. &pid[1 + gpl],
  108. ppl);
  109. }
  110. /**
  111. * We got a disconnect after asking regex to do the announcement.
  112. * Retry.
  113. *
  114. * @param cls the `struct GNUNET_REGEX_Search` to retry
  115. * @param error error code
  116. */
  117. static void
  118. mq_error_handler (void *cls,
  119. enum GNUNET_MQ_Error error)
  120. {
  121. struct GNUNET_REGEX_Search *s = cls;
  122. GNUNET_MQ_destroy (s->mq);
  123. s->mq = NULL;
  124. search_reconnect (s);
  125. }
  126. /**
  127. * (Re)connect to the REGEX service for the given search @a s.
  128. *
  129. * @param s context for the search search for
  130. */
  131. static void
  132. search_reconnect (struct GNUNET_REGEX_Search *s)
  133. {
  134. struct GNUNET_MQ_MessageHandler handlers[] = {
  135. GNUNET_MQ_hd_var_size (search_response,
  136. GNUNET_MESSAGE_TYPE_REGEX_RESULT,
  137. struct ResultMessage,
  138. s),
  139. GNUNET_MQ_handler_end ()
  140. };
  141. size_t slen = strlen (s->string) + 1;
  142. struct GNUNET_MQ_Envelope *env;
  143. struct RegexSearchMessage *rsm;
  144. GNUNET_assert (NULL == s->mq);
  145. s->mq = GNUNET_CLIENT_connect (s->cfg,
  146. "regex",
  147. handlers,
  148. &mq_error_handler,
  149. s);
  150. if (NULL == s->mq)
  151. return;
  152. env = GNUNET_MQ_msg_extra (rsm,
  153. slen,
  154. GNUNET_MESSAGE_TYPE_REGEX_SEARCH);
  155. GNUNET_memcpy (&rsm[1],
  156. s->string,
  157. slen);
  158. GNUNET_MQ_send (s->mq,
  159. env);
  160. }
  161. /**
  162. * Search for a peer offering a regex matching certain string in the DHT.
  163. * The search runs until #GNUNET_REGEX_search_cancel() is called, even if results
  164. * are returned.
  165. *
  166. * @param cfg configuration to use
  167. * @param string String to match against the regexes in the DHT.
  168. * @param callback Callback for found peers.
  169. * @param callback_cls Closure for @c callback.
  170. * @return Handle to stop search and free resources.
  171. * Must be freed by calling #GNUNET_REGEX_search_cancel().
  172. */
  173. struct GNUNET_REGEX_Search *
  174. GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg,
  175. const char *string,
  176. GNUNET_REGEX_Found callback,
  177. void *callback_cls)
  178. {
  179. struct GNUNET_REGEX_Search *s;
  180. size_t slen = strlen (string) + 1;
  181. if (slen + sizeof(struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE)
  182. {
  183. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  184. _ ("Search string `%s' is too long!\n"),
  185. string);
  186. GNUNET_break (0);
  187. return NULL;
  188. }
  189. LOG (GNUNET_ERROR_TYPE_DEBUG,
  190. "Starting regex search for %s\n",
  191. string);
  192. s = GNUNET_new (struct GNUNET_REGEX_Search);
  193. s->cfg = cfg;
  194. s->string = GNUNET_strdup (string);
  195. s->callback = callback;
  196. s->callback_cls = callback_cls;
  197. search_reconnect (s);
  198. if (NULL == s->mq)
  199. {
  200. GNUNET_free (s->string);
  201. GNUNET_free (s);
  202. return NULL;
  203. }
  204. return s;
  205. }
  206. /**
  207. * Stop search and free all data used by a #GNUNET_REGEX_search() call.
  208. *
  209. * @param s Handle returned by a previous #GNUNET_REGEX_search() call.
  210. */
  211. void
  212. GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s)
  213. {
  214. GNUNET_MQ_destroy (s->mq);
  215. GNUNET_free (s->string);
  216. GNUNET_free (s);
  217. }
  218. /* end of regex_api_search.c */