2 This file is part of GNUnet.
3 Copyright (C) 2013 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.
22 * @file regex/gnunet-service-regex.c
23 * @brief service to advertise capabilities described as regex and to
24 * lookup capabilities by regex
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "regex_internal_lib.h"
30 #include "regex_ipc.h"
34 * Information about one of our clients.
40 * Queue for transmissions to @e client.
42 struct GNUNET_MQ_Handle *mq;
45 * Handle identifying the client.
47 struct GNUNET_SERVICE_Client *client;
50 * Search handle (if this client is searching).
52 struct REGEX_INTERNAL_Search *sh;
55 * Announcement handle (if this client is announcing).
57 struct REGEX_INTERNAL_Announcement *ah;
60 * Refresh frequency for announcements.
62 struct GNUNET_TIME_Relative frequency;
65 * Task for re-announcing.
67 struct GNUNET_SCHEDULER_Task *refresh_task;
73 * Connection to the DHT.
75 static struct GNUNET_DHT_Handle *dht;
78 * Handle for doing statistics.
80 static struct GNUNET_STATISTICS_Handle *stats;
83 * Private key for this peer.
85 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
89 * Task run during shutdown.
94 cleanup_task (void *cls)
96 GNUNET_DHT_disconnect (dht);
98 GNUNET_STATISTICS_destroy (stats,
101 GNUNET_free (my_private_key);
102 my_private_key = NULL;
107 * Periodic task to refresh our announcement of the regex.
109 * @param cls the `struct ClientEntry *` of the client that triggered the
113 reannounce (void *cls)
115 struct ClientEntry *ce = cls;
117 REGEX_INTERNAL_reannounce (ce->ah);
118 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
125 * Check ANNOUNCE message.
127 * @param cls identification of the client
128 * @param am the actual message
129 * @return #GNUNET_OK if @am is well-formed
132 check_announce (void *cls,
133 const struct AnnounceMessage *am)
135 struct ClientEntry *ce = cls;
139 size = ntohs (am->header.size) - sizeof (*am);
140 regex = (const char *) &am[1];
141 if ('\0' != regex[size - 1])
144 return GNUNET_SYSERR;
148 /* only one announcement per client allowed */
150 return GNUNET_SYSERR;
157 * Handle ANNOUNCE message.
159 * @param cls identification of the client
160 * @param am the actual message
163 handle_announce (void *cls,
164 const struct AnnounceMessage *am)
166 struct ClientEntry *ce = cls;
169 regex = (const char *) &am[1];
170 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
171 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Starting to announce regex `%s' every %s\n",
177 GNUNET_STRINGS_relative_time_to_string (ce->frequency,
179 ce->ah = REGEX_INTERNAL_announce (dht,
182 ntohs (am->compression),
187 GNUNET_SCHEDULER_cancel (ce->refresh_task);
188 ce->refresh_task = NULL;
189 GNUNET_SERVICE_client_drop (ce->client);
192 GNUNET_SERVICE_client_continue (ce->client);
197 * Handle result, pass it back to the client.
199 * @param cls the struct ClientEntry of the client searching
200 * @param id Peer providing a regex that matches the string.
201 * @param get_path Path of the get request.
202 * @param get_path_length Lenght of @a get_path.
203 * @param put_path Path of the put request.
204 * @param put_path_length Length of the @a put_path.
207 handle_search_result (void *cls,
208 const struct GNUNET_PeerIdentity *id,
209 const struct GNUNET_PeerIdentity *get_path,
210 unsigned int get_path_length,
211 const struct GNUNET_PeerIdentity *put_path,
212 unsigned int put_path_length)
214 struct ClientEntry *ce = cls;
215 struct GNUNET_MQ_Envelope *env;
216 struct ResultMessage *result;
217 struct GNUNET_PeerIdentity *gp;
220 if ( (get_path_length >= 65536) ||
221 (put_path_length >= 65536) ||
222 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
223 + sizeof (struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE)
228 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity);
229 env = GNUNET_MQ_msg_extra (result,
231 GNUNET_MESSAGE_TYPE_REGEX_RESULT);
232 result->get_path_length = htons ((uint16_t) get_path_length);
233 result->put_path_length = htons ((uint16_t) put_path_length);
236 GNUNET_memcpy (&gp[1],
238 get_path_length * sizeof (struct GNUNET_PeerIdentity));
239 GNUNET_memcpy (&gp[1 + get_path_length],
241 put_path_length * sizeof (struct GNUNET_PeerIdentity));
242 GNUNET_MQ_send (ce->mq,
248 * Check SEARCH message.
250 * @param cls identification of the client
251 * @param message the actual message
254 check_search (void *cls,
255 const struct RegexSearchMessage *sm)
257 struct ClientEntry *ce = cls;
261 size = ntohs (sm->header.size) - sizeof (*sm);
262 string = (const char *) &sm[1];
263 if ('\0' != string[size - 1])
266 return GNUNET_SYSERR;
270 /* only one search allowed per client */
272 return GNUNET_SYSERR;
279 * Handle SEARCH message.
281 * @param cls identification of the client
282 * @param message the actual message
285 handle_search (void *cls,
286 const struct RegexSearchMessage *sm)
288 struct ClientEntry *ce = cls;
291 string = (const char *) &sm[1];
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Starting to search for `%s'\n",
295 ce->sh = REGEX_INTERNAL_search (dht,
297 &handle_search_result,
303 GNUNET_SERVICE_client_drop (ce->client);
306 GNUNET_SERVICE_client_continue (ce->client);
311 * Process regex requests.
314 * @param cfg configuration to use
315 * @param service the initialized service
319 const struct GNUNET_CONFIGURATION_Handle *cfg,
320 struct GNUNET_SERVICE_Handle *service)
322 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
323 if (NULL == my_private_key)
325 GNUNET_SCHEDULER_shutdown ();
328 dht = GNUNET_DHT_connect (cfg, 1024);
331 GNUNET_free (my_private_key);
332 my_private_key = NULL;
333 GNUNET_SCHEDULER_shutdown ();
336 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
338 stats = GNUNET_STATISTICS_create ("regex", cfg);
343 * Callback called when a client connects to the service.
345 * @param cls closure for the service
346 * @param c the new client that connected to the service
347 * @param mq the message queue used to send messages to the client
351 client_connect_cb (void *cls,
352 struct GNUNET_SERVICE_Client *c,
353 struct GNUNET_MQ_Handle *mq)
355 struct ClientEntry *ce;
357 ce = GNUNET_new (struct ClientEntry);
365 * Callback called when a client disconnected from the service
367 * @param cls closure for the service
368 * @param c the client that disconnected
369 * @param internal_cls should be equal to @a c
372 client_disconnect_cb (void *cls,
373 struct GNUNET_SERVICE_Client *c,
376 struct ClientEntry *ce = internal_cls;
378 if (NULL != ce->refresh_task)
380 GNUNET_SCHEDULER_cancel (ce->refresh_task);
381 ce->refresh_task = NULL;
385 REGEX_INTERNAL_announce_cancel (ce->ah);
390 REGEX_INTERNAL_search_cancel (ce->sh);
398 * Define "main" method using service macro.
402 GNUNET_SERVICE_OPTION_NONE,
405 &client_disconnect_cb,
407 GNUNET_MQ_hd_var_size (announce,
408 GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE,
409 struct AnnounceMessage,
411 GNUNET_MQ_hd_var_size (search,
412 GNUNET_MESSAGE_TYPE_REGEX_SEARCH,
413 struct RegexSearchMessage,
415 GNUNET_MQ_handler_end ());
418 /* end of gnunet-service-regex.c */