2 This file is part of GNUnet
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file regex/regex_api.c
22 * @brief access regex service to advertise capabilities via regex and discover
23 * respective 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"
34 * Handle to store cached data about a regex announce.
36 struct GNUNET_REGEX_Announcement
39 * Connection to the regex service.
41 struct GNUNET_CLIENT_Connection *client;
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Message we're sending to the service.
51 struct AnnounceMessage msg;
56 * We got a response (!?) or disconnect after asking regex
57 * to do the announcement. Retry.
59 * @param cls the 'struct GNUNET_REGEX_Announcement' to retry
60 * @param msg NULL on disconnect
63 handle_a_reconnect (void *cls,
64 const struct GNUNET_MessageHeader *msg);
68 * Try sending the announcement request to regex. On
69 * errors (i.e. regex died), try again.
71 * @param a the announcement to retry
74 retry_announcement (struct GNUNET_REGEX_Announcement *a)
76 GNUNET_assert (NULL != a->client);
77 GNUNET_assert (GNUNET_OK ==
78 GNUNET_CLIENT_transmit_and_get_response (a->client,
80 GNUNET_TIME_UNIT_FOREVER_REL,
88 * We got a response (!?) or disconnect after asking regex
89 * to do the announcement. Retry.
91 * @param cls the 'struct GNUNET_REGEX_Announcement' to retry
92 * @param msg NULL on disconnect
95 handle_a_reconnect (void *cls,
96 const struct GNUNET_MessageHeader *msg)
98 struct GNUNET_REGEX_Announcement *a = cls;
100 GNUNET_CLIENT_disconnect (a->client);
101 a->client = GNUNET_CLIENT_connect ("regex", a->cfg);
102 retry_announcement (a);
107 * Announce the given peer under the given regular expression. Does
108 * not free resources, must call GNUNET_REGEX_announce_cancel for
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.
118 struct GNUNET_REGEX_Announcement *
119 GNUNET_REGEX_announce (const struct GNUNET_CONFIGURATION_Handle *cfg,
121 struct GNUNET_TIME_Relative refresh_delay,
122 uint16_t compression)
124 struct GNUNET_REGEX_Announcement *a;
127 slen = strlen (regex) + 1;
128 if (slen + sizeof (struct AnnounceMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
133 a = GNUNET_malloc (sizeof (struct GNUNET_REGEX_Announcement) + slen);
135 a->client = GNUNET_CLIENT_connect ("regex", cfg);
136 if (NULL == a->client)
141 a->msg.header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE);
142 a->msg.header.size = htons (slen + sizeof (struct AnnounceMessage));
143 a->msg.compression = htons (compression);
144 a->msg.reserved = htons (0);
145 a->msg.refresh_delay = GNUNET_TIME_relative_hton (refresh_delay);
146 memcpy (&a[1], regex, slen);
147 retry_announcement (a);
153 * Stop announcing the regex specified by the given handle.
155 * @param a handle returned by a previous GNUNET_REGEX_announce call.
158 GNUNET_REGEX_announce_cancel (struct GNUNET_REGEX_Announcement *a)
160 GNUNET_CLIENT_disconnect (a->client);
166 * Handle to store data about a regex search.
168 struct GNUNET_REGEX_Search
171 * Connection to the regex service.
173 struct GNUNET_CLIENT_Connection *client;
178 const struct GNUNET_CONFIGURATION_Handle *cfg;
181 * Function to call with results.
183 GNUNET_REGEX_Found callback;
186 * Closure for 'callback'.
191 * Search message to transmit to the service.
193 struct SearchMessage *msg;
198 * We got a response or disconnect after asking regex
199 * to do the search. Handle it.
201 * @param cls the 'struct GNUNET_REGEX_Search' to retry
202 * @param msg NULL on disconnect
205 handle_search_response (void *cls,
206 const struct GNUNET_MessageHeader *msg);
210 * Try sending the search request to regex. On
211 * errors (i.e. regex died), try again.
213 * @param s the search to retry
216 retry_search (struct GNUNET_REGEX_Search *s)
218 GNUNET_assert (NULL != s->client);
219 GNUNET_assert (GNUNET_OK ==
220 GNUNET_CLIENT_transmit_and_get_response (s->client,
222 GNUNET_TIME_UNIT_FOREVER_REL,
224 &handle_search_response,
230 * We got a response or disconnect after asking regex
231 * to do the search. Handle it.
233 * @param cls the 'struct GNUNET_REGEX_Search' to retry
234 * @param msg NULL on disconnect, otherwise presumably a response
237 handle_search_response (void *cls,
238 const struct GNUNET_MessageHeader *msg)
240 struct GNUNET_REGEX_Search *s = cls;
241 const struct ResultMessage *result;
248 GNUNET_CLIENT_disconnect (s->client);
249 s->client = GNUNET_CLIENT_connect ("regex", s->cfg);
253 size = ntohs (msg->size);
254 if ( (GNUNET_MESSAGE_TYPE_REGEX_RESULT == ntohs (msg->type)) &&
255 (size >= sizeof (struct ResultMessage)) )
257 result = (const struct ResultMessage *) msg;
258 gpl = ntohs (result->get_path_length);
259 ppl = ntohs (result->put_path_length);
260 if (size == (sizeof (struct ResultMessage) +
261 (gpl + ppl) * sizeof (struct GNUNET_PeerIdentity)))
263 const struct GNUNET_PeerIdentity *pid;
265 GNUNET_CLIENT_receive (s->client,
266 &handle_search_response, s,
267 GNUNET_TIME_UNIT_FOREVER_REL);
269 s->callback (s->callback_cls,
277 GNUNET_CLIENT_disconnect (s->client);
278 s->client = GNUNET_CLIENT_connect ("regex", s->cfg);
284 * Search for a peer offering a regex matching certain string in the DHT.
285 * The search runs until GNUNET_REGEX_search_cancel is called, even if results
288 * @param cfg configuration to use
289 * @param string String to match against the regexes in the DHT.
290 * @param callback Callback for found peers.
291 * @param callback_cls Closure for @c callback.
292 * @return Handle to stop search and free resources.
293 * Must be freed by calling GNUNET_REGEX_search_cancel.
295 struct GNUNET_REGEX_Search *
296 GNUNET_REGEX_search (const struct GNUNET_CONFIGURATION_Handle *cfg,
298 GNUNET_REGEX_Found callback,
301 struct GNUNET_REGEX_Search *s;
304 slen = strlen (string) + 1;
305 s = GNUNET_new (struct GNUNET_REGEX_Search);
307 s->client = GNUNET_CLIENT_connect ("regex", cfg);
308 if (NULL == s->client)
313 s->callback = callback;
314 s->callback_cls = callback_cls;
315 s->msg = GNUNET_malloc (sizeof (struct SearchMessage) + slen);
316 s->msg->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_SEARCH);
317 s->msg->header.size = htons (sizeof (struct SearchMessage) + slen);
318 memcpy (&s->msg[1], string, slen);
325 * Stop search and free all data used by a GNUNET_REGEX_search call.
327 * @param s Handle returned by a previous GNUNET_REGEX_search call.
330 GNUNET_REGEX_search_cancel (struct GNUNET_REGEX_Search *s)
332 GNUNET_CLIENT_disconnect (s->client);
333 GNUNET_free (s->msg);
338 /* end of regex_api.c */