2 This file is part of GNUnet
3 Copyright (C) 2012, 2013, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file regex/regex_api_search.c
22 * @brief access regex service to discover
23 * peers using matching strings
24 * @author Maximilian Szengel
25 * @author Christian Grothoff
28 #include "gnunet_protocols.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_regex_service.h"
31 #include "regex_ipc.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "regex-api",__VA_ARGS__)
37 * Handle to store data about a regex search.
39 struct GNUNET_REGEX_Search
42 * Connection to the regex service.
44 struct GNUNET_MQ_Handle *mq;
49 const struct GNUNET_CONFIGURATION_Handle *cfg;
52 * Function to call with results.
54 GNUNET_REGEX_Found callback;
57 * Closure for @e callback.
62 * Search string to transmit to the service.
69 * (Re)connect to the REGEX service for the given search @a s.
71 * @param s context for the search search for
74 search_reconnect (struct GNUNET_REGEX_Search *s);
78 * We got a response or disconnect after asking regex
79 * to do the search. Check it is well-formed.
81 * @param cls the `struct GNUNET_REGEX_Search` to handle reply for
82 * @param result the message
83 * @return #GNUNET_SYSERR if @a rm is not well-formed.
86 check_search_response (void *cls,
87 const struct ResultMessage *result)
89 uint16_t size = ntohs (result->header.size) - sizeof (*result);
90 uint16_t gpl = ntohs (result->get_path_length);
91 uint16_t ppl = ntohs (result->put_path_length);
93 if (size != (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity))
103 * We got a response or disconnect after asking regex
104 * to do the search. Handle it.
106 * @param cls the `struct GNUNET_REGEX_Search` to handle reply for
107 * @param result the message
110 handle_search_response (void *cls,
111 const struct ResultMessage *result)
113 struct GNUNET_REGEX_Search *s = cls;
114 uint16_t gpl = ntohs (result->get_path_length);
115 uint16_t ppl = ntohs (result->put_path_length);
116 const struct GNUNET_PeerIdentity *pid;
119 LOG (GNUNET_ERROR_TYPE_DEBUG,
120 "Got regex result %s\n",
122 s->callback (s->callback_cls,
132 * We got a disconnect after asking regex to do the announcement.
135 * @param cls the `struct GNUNET_REGEX_Search` to retry
136 * @param error error code
139 mq_error_handler (void *cls,
140 enum GNUNET_MQ_Error error)
142 struct GNUNET_REGEX_Search *s = cls;
144 GNUNET_MQ_destroy (s->mq);
146 search_reconnect (s);
151 * (Re)connect to the REGEX service for the given search @a s.
153 * @param s context for the search search for
156 search_reconnect (struct GNUNET_REGEX_Search *s)
158 struct GNUNET_MQ_MessageHandler handlers[] = {
159 GNUNET_MQ_hd_var_size (search_response,
160 GNUNET_MESSAGE_TYPE_REGEX_RESULT,
161 struct ResultMessage,
163 GNUNET_MQ_handler_end ()
165 size_t slen = strlen (s->string) + 1;
166 struct GNUNET_MQ_Envelope *env;
167 struct RegexSearchMessage *rsm;
169 GNUNET_assert (NULL == s->mq);
170 s->mq = GNUNET_CLIENT_connect (s->cfg,
177 env = GNUNET_MQ_msg_extra (rsm,
179 GNUNET_MESSAGE_TYPE_REGEX_SEARCH);
180 GNUNET_memcpy (&rsm[1],
183 GNUNET_MQ_send (s->mq,
189 * Search for a peer offering a regex matching certain string in the DHT.
190 * The search runs until #GNUNET_REGEX_search_cancel() is called, even if results
193 * @param cfg configuration to use
194 * @param string String to match against the regexes in the DHT.
195 * @param callback Callback for found peers.
196 * @param callback_cls Closure for @c callback.
197 * @return Handle to stop search and free resources.
198 * Must be freed by calling #GNUNET_REGEX_search_cancel().
200 struct GNUNET_REGEX_Search *
201 GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg,
203 GNUNET_REGEX_Found callback,
206 struct GNUNET_REGEX_Search *s;
207 size_t slen = strlen (string) + 1;
209 if (slen + sizeof (struct RegexSearchMessage) >= GNUNET_MAX_MESSAGE_SIZE)
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 _("Search string `%s' is too long!\n"),
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 "Starting regex search for %s\n",
220 s = GNUNET_new (struct GNUNET_REGEX_Search);
222 s->string = GNUNET_strdup (string);
223 s->callback = callback;
224 s->callback_cls = callback_cls;
225 search_reconnect (s);
228 GNUNET_free (s->string);
237 * Stop search and free all data used by a #GNUNET_REGEX_search() call.
239 * @param s Handle returned by a previous #GNUNET_REGEX_search() call.
242 GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s)
244 GNUNET_MQ_destroy (s->mq);
245 GNUNET_free (s->string);
250 /* end of regex_api_search.c */