2 This file is part of GNUnet.
3 (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 GNUNET_SCHEDULER_TaskIdentifier 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_EccPrivateKey *my_private_key;
109 * Task run during shutdown.
115 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_DHT_disconnect (dht);
119 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
121 GNUNET_SERVER_notification_context_destroy (nc);
123 GNUNET_CRYPTO_ecc_key_free (my_private_key);
124 my_private_key = NULL;
129 * A client disconnected. Remove all of its data structure entries.
131 * @param cls closure, NULL
132 * @param client identification of the client
135 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
137 struct ClientEntry *ce;
138 struct ClientEntry *nx;
141 for (ce = nx; NULL != ce; ce = nx)
144 if (ce->client == client)
146 if (GNUNET_SCHEDULER_NO_TASK != ce->refresh_task)
148 GNUNET_SCHEDULER_cancel (ce->refresh_task);
149 ce->refresh_task = GNUNET_SCHEDULER_NO_TASK;
153 REGEX_INTERNAL_announce_cancel (ce->ah);
158 REGEX_INTERNAL_search_cancel (ce->sh);
161 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
169 * Periodic task to refresh our announcement of the regex.
171 * @param cls the 'struct ClientEntry' of the client that triggered the
173 * @param tc scheduler context
176 reannounce (void *cls,
177 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 struct ClientEntry *ce = cls;
181 REGEX_INTERNAL_reannounce (ce->ah);
182 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
189 * Handle ANNOUNCE message.
192 * @param client identification of the client
193 * @param message the actual message
196 handle_announce (void *cls,
197 struct GNUNET_SERVER_Client *client,
198 const struct GNUNET_MessageHeader *message)
200 const struct AnnounceMessage *am;
202 struct ClientEntry *ce;
205 size = ntohs (message->size);
206 am = (const struct AnnounceMessage *) message;
207 regex = (const char *) &am[1];
208 if ( (size <= sizeof (struct AnnounceMessage)) ||
209 ('\0' != regex[size - sizeof (struct AnnounceMessage) - 1]) )
212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
215 ce = GNUNET_new (struct ClientEntry);
217 ce->ah = REGEX_INTERNAL_announce (dht,
220 ntohs (am->compression),
226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
229 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
230 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
233 GNUNET_CONTAINER_DLL_insert (client_head,
236 GNUNET_SERVER_receive_done (client, GNUNET_OK);
241 * Handle result, pass it back to the client.
243 * @param cls the struct ClientEntry of the client searching
244 * @param id Peer providing a regex that matches the string.
245 * @param get_path Path of the get request.
246 * @param get_path_length Lenght of get_path.
247 * @param put_path Path of the put request.
248 * @param put_path_length Length of the put_path.
251 handle_search_result (void *cls,
252 const struct GNUNET_PeerIdentity *id,
253 const struct GNUNET_PeerIdentity *get_path,
254 unsigned int get_path_length,
255 const struct GNUNET_PeerIdentity *put_path,
256 unsigned int put_path_length)
258 struct ClientEntry *ce = cls;
259 struct ResultMessage *result;
260 struct GNUNET_PeerIdentity *gp;
263 if ( (get_path_length >= 65536) ||
264 (put_path_length >= 65536) ||
265 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
266 + sizeof (struct ResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
271 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct ResultMessage);
272 result = GNUNET_malloc (size);
273 result->header.size = htons (size);
274 result->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_RESULT);
275 result->get_path_length = htons ((uint16_t) get_path_length);
276 result->put_path_length = htons ((uint16_t) put_path_length);
281 get_path_length * sizeof (struct GNUNET_PeerIdentity));
282 memcpy (&gp[1 + get_path_length],
284 put_path_length * sizeof (struct GNUNET_PeerIdentity));
285 GNUNET_SERVER_notification_context_unicast (nc,
287 &result->header, GNUNET_NO);
288 GNUNET_free (result);
293 * Handle SEARCH message.
296 * @param client identification of the client
297 * @param message the actual message
300 handle_search (void *cls,
301 struct GNUNET_SERVER_Client *client,
302 const struct GNUNET_MessageHeader *message)
304 const struct SearchMessage *sm;
306 struct ClientEntry *ce;
309 size = ntohs (message->size);
310 sm = (const struct SearchMessage *) message;
311 string = (const char *) &sm[1];
312 if ( (size <= sizeof (struct SearchMessage)) ||
313 ('\0' != string[size - sizeof (struct SearchMessage) - 1]) )
316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
319 ce = GNUNET_new (struct ClientEntry);
321 ce->sh = REGEX_INTERNAL_search (dht,
323 &handle_search_result,
330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
333 GNUNET_CONTAINER_DLL_insert (client_head,
336 GNUNET_SERVER_notification_context_add (nc, client);
337 GNUNET_SERVER_receive_done (client, GNUNET_OK);
342 * Process regex requests.
345 * @param server the initialized server
346 * @param cfg configuration to use
349 run (void *cls, struct GNUNET_SERVER_Handle *server,
350 const struct GNUNET_CONFIGURATION_Handle *cfg)
352 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
353 {&handle_announce, NULL, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 0},
354 {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
358 my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
359 if (NULL == my_private_key)
361 GNUNET_SCHEDULER_shutdown ();
364 dht = GNUNET_DHT_connect (cfg, 1024);
367 GNUNET_CRYPTO_ecc_key_free (my_private_key);
368 my_private_key = NULL;
369 GNUNET_SCHEDULER_shutdown ();
372 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
374 nc = GNUNET_SERVER_notification_context_create (server, 1);
375 stats = GNUNET_STATISTICS_create ("regex", cfg);
376 GNUNET_SERVER_add_handlers (server, handlers);
377 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
382 * The main function for the regex service.
384 * @param argc number of arguments from the command line
385 * @param argv command line arguments
386 * @return 0 ok, 1 on error
389 main (int argc, char *const *argv)
392 GNUNET_SERVICE_run (argc, argv, "regex",
393 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
396 /* end of gnunet-service-regex.c */