2 This file is part of GNUnet.
3 Copyright (C) 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.
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.
42 struct ClientEntry *next;
47 struct ClientEntry *prev;
50 * Handle identifying the client.
52 struct GNUNET_SERVER_Client *client;
55 * Search handle (if this client is searching).
57 struct REGEX_INTERNAL_Search *sh;
60 * Announcement handle (if this client is announcing).
62 struct REGEX_INTERNAL_Announcement *ah;
65 * Refresh frequency for announcements.
67 struct GNUNET_TIME_Relative frequency;
70 * Task for re-announcing.
72 struct GNUNET_SCHEDULER_Task * refresh_task;
78 * Connection to the DHT.
80 static struct GNUNET_DHT_Handle *dht;
83 * Handle for doing statistics.
85 static struct GNUNET_STATISTICS_Handle *stats;
88 * Head of list of clients.
90 static struct ClientEntry *client_head;
93 * End of list of clients.
95 static struct ClientEntry *client_tail;
98 * Our notification context, used to send back results to the client.
100 static struct GNUNET_SERVER_NotificationContext *nc;
103 * Private key for this peer.
105 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
109 * A client disconnected. Remove all of its data structure entries.
111 * @param cls closure, NULL
112 * @param client identification of the client
115 handle_client_disconnect (void *cls,
116 struct GNUNET_SERVER_Client *client)
118 struct ClientEntry *ce;
119 struct ClientEntry *nx;
122 for (ce = nx; NULL != ce; ce = nx)
125 if (ce->client == client)
127 if (NULL != ce->refresh_task)
129 GNUNET_SCHEDULER_cancel (ce->refresh_task);
130 ce->refresh_task = NULL;
134 REGEX_INTERNAL_announce_cancel (ce->ah);
139 REGEX_INTERNAL_search_cancel (ce->sh);
142 GNUNET_CONTAINER_DLL_remove (client_head,
152 * Task run during shutdown.
158 cleanup_task (void *cls,
159 const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct ClientEntry *ce;
163 while (NULL != (ce = client_head))
164 handle_client_disconnect (NULL,
166 GNUNET_DHT_disconnect (dht);
168 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
170 GNUNET_SERVER_notification_context_destroy (nc);
172 GNUNET_free (my_private_key);
173 my_private_key = NULL;
178 * Periodic task to refresh our announcement of the regex.
180 * @param cls the 'struct ClientEntry' of the client that triggered the
182 * @param tc scheduler context
185 reannounce (void *cls,
186 const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct ClientEntry *ce = cls;
190 REGEX_INTERNAL_reannounce (ce->ah);
191 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
198 * Handle ANNOUNCE message.
201 * @param client identification of the client
202 * @param message the actual message
205 handle_announce (void *cls,
206 struct GNUNET_SERVER_Client *client,
207 const struct GNUNET_MessageHeader *message)
209 const struct AnnounceMessage *am;
211 struct ClientEntry *ce;
214 size = ntohs (message->size);
215 am = (const struct AnnounceMessage *) message;
216 regex = (const char *) &am[1];
217 if ( (size <= sizeof (struct AnnounceMessage)) ||
218 ('\0' != regex[size - sizeof (struct AnnounceMessage) - 1]) )
221 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
225 ce = GNUNET_new (struct ClientEntry);
227 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
228 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Starting to announce regex `%s' every %s\n",
234 GNUNET_STRINGS_relative_time_to_string (ce->frequency,
236 ce->ah = REGEX_INTERNAL_announce (dht,
239 ntohs (am->compression),
244 GNUNET_SCHEDULER_cancel (ce->refresh_task);
246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
249 GNUNET_CONTAINER_DLL_insert (client_head,
252 GNUNET_SERVER_receive_done (client, GNUNET_OK);
257 * Handle result, pass it back to the client.
259 * @param cls the struct ClientEntry of the client searching
260 * @param id Peer providing a regex that matches the string.
261 * @param get_path Path of the get request.
262 * @param get_path_length Lenght of @a get_path.
263 * @param put_path Path of the put request.
264 * @param put_path_length Length of the @a put_path.
267 handle_search_result (void *cls,
268 const struct GNUNET_PeerIdentity *id,
269 const struct GNUNET_PeerIdentity *get_path,
270 unsigned int get_path_length,
271 const struct GNUNET_PeerIdentity *put_path,
272 unsigned int put_path_length)
274 struct ClientEntry *ce = cls;
275 struct ResultMessage *result;
276 struct GNUNET_PeerIdentity *gp;
279 if ( (get_path_length >= 65536) ||
280 (put_path_length >= 65536) ||
281 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
282 + sizeof (struct ResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
287 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct ResultMessage);
288 result = GNUNET_malloc (size);
289 result->header.size = htons (size);
290 result->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_RESULT);
291 result->get_path_length = htons ((uint16_t) get_path_length);
292 result->put_path_length = htons ((uint16_t) put_path_length);
297 get_path_length * sizeof (struct GNUNET_PeerIdentity));
298 memcpy (&gp[1 + get_path_length],
300 put_path_length * sizeof (struct GNUNET_PeerIdentity));
301 GNUNET_SERVER_notification_context_unicast (nc,
303 &result->header, GNUNET_NO);
304 GNUNET_free (result);
309 * Handle SEARCH message.
312 * @param client identification of the client
313 * @param message the actual message
316 handle_search (void *cls,
317 struct GNUNET_SERVER_Client *client,
318 const struct GNUNET_MessageHeader *message)
320 const struct RegexSearchMessage *sm;
322 struct ClientEntry *ce;
325 size = ntohs (message->size);
326 sm = (const struct RegexSearchMessage *) message;
327 string = (const char *) &sm[1];
328 if ( (size <= sizeof (struct RegexSearchMessage)) ||
329 ('\0' != string[size - sizeof (struct RegexSearchMessage) - 1]) )
332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Starting to search for `%s'\n",
338 ce = GNUNET_new (struct ClientEntry);
340 ce->sh = REGEX_INTERNAL_search (dht,
342 &handle_search_result,
349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
352 GNUNET_CONTAINER_DLL_insert (client_head,
355 GNUNET_SERVER_notification_context_add (nc, client);
356 GNUNET_SERVER_receive_done (client, GNUNET_OK);
361 * Process regex requests.
364 * @param server the initialized server
365 * @param cfg configuration to use
368 run (void *cls, struct GNUNET_SERVER_Handle *server,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
371 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
372 {&handle_announce, NULL, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 0},
373 {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
377 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
378 if (NULL == my_private_key)
380 GNUNET_SCHEDULER_shutdown ();
383 dht = GNUNET_DHT_connect (cfg, 1024);
386 GNUNET_free (my_private_key);
387 my_private_key = NULL;
388 GNUNET_SCHEDULER_shutdown ();
391 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
394 nc = GNUNET_SERVER_notification_context_create (server, 1);
395 stats = GNUNET_STATISTICS_create ("regex", cfg);
396 GNUNET_SERVER_add_handlers (server, handlers);
397 GNUNET_SERVER_disconnect_notify (server,
398 &handle_client_disconnect,
404 * The main function for the regex service.
406 * @param argc number of arguments from the command line
407 * @param argv command line arguments
408 * @return 0 ok, 1 on error
411 main (int argc, char *const *argv)
414 GNUNET_SERVICE_run (argc, argv, "regex",
415 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
418 /* end of gnunet-service-regex.c */