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 "gnunet_regex_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 GNUNET_REGEX_search_handle *sh;
60 * Announcement handle (if this client is announcing).
62 struct GNUNET_REGEX_announce_handle *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;
104 * Task run during shutdown.
110 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 GNUNET_DHT_disconnect (dht);
114 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
116 GNUNET_SERVER_notification_context_destroy (nc);
122 * A client disconnected. Remove all of its data structure entries.
124 * @param cls closure, NULL
125 * @param client identification of the client
128 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
130 struct ClientEntry *ce;
131 struct ClientEntry *nx;
134 for (ce = nx; NULL != ce; ce = nx)
137 if (ce->client == client)
139 if (GNUNET_SCHEDULER_NO_TASK != ce->refresh_task)
141 GNUNET_SCHEDULER_cancel (ce->refresh_task);
142 ce->refresh_task = GNUNET_SCHEDULER_NO_TASK;
146 GNUNET_REGEX_announce_cancel (ce->ah);
151 GNUNET_REGEX_search_cancel (ce->sh);
154 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
162 * Periodic task to refresh our announcement of the regex.
164 * @param cls the 'struct ClientEntry' of the client that triggered the
166 * @param tc scheduler context
169 reannounce (void *cls,
170 const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct ClientEntry *ce = cls;
174 GNUNET_REGEX_reannounce (ce->ah);
175 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
182 * Handle ANNOUNCE message.
185 * @param client identification of the client
186 * @param message the actual message
189 handle_announce (void *cls,
190 struct GNUNET_SERVER_Client *client,
191 const struct GNUNET_MessageHeader *message)
193 const struct AnnounceMessage *am;
195 struct ClientEntry *ce;
198 size = ntohs (message->size);
199 am = (const struct AnnounceMessage *) message;
200 regex = (const char *) &am[1];
201 if ( (size <= sizeof (struct AnnounceMessage)) ||
202 ('\0' != regex[size - sizeof (struct AnnounceMessage) - 1]) )
205 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
208 ce = GNUNET_new (struct ClientEntry);
210 ce->ah = GNUNET_REGEX_announce (dht,
213 ntohs (am->compression),
219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
222 ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
223 ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
226 GNUNET_CONTAINER_DLL_insert (client_head,
229 GNUNET_SERVER_receive_done (client, GNUNET_OK);
234 * Handle result, pass it back to the client.
236 * @param cls the struct ClientEntry of the client searching
237 * @param id Peer providing a regex that matches the string.
238 * @param get_path Path of the get request.
239 * @param get_path_length Lenght of get_path.
240 * @param put_path Path of the put request.
241 * @param put_path_length Length of the put_path.
244 handle_search_result (void *cls,
245 const struct GNUNET_PeerIdentity *id,
246 const struct GNUNET_PeerIdentity *get_path,
247 unsigned int get_path_length,
248 const struct GNUNET_PeerIdentity *put_path,
249 unsigned int put_path_length)
251 struct ClientEntry *ce = cls;
252 struct ResultMessage *result;
253 struct GNUNET_PeerIdentity *gp;
256 if ( (get_path_length >= 65536) ||
257 (put_path_length >= 65536) ||
258 ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
259 + sizeof (struct ResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
264 size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct ResultMessage);
265 result = GNUNET_malloc (size);
266 result->header.size = htons (size);
267 result->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_RESULT);
268 result->get_path_length = htons ((uint16_t) get_path_length);
269 result->put_path_length = htons ((uint16_t) put_path_length);
274 get_path_length * sizeof (struct GNUNET_PeerIdentity));
275 memcpy (&gp[1 + get_path_length],
277 put_path_length * sizeof (struct GNUNET_PeerIdentity));
278 GNUNET_SERVER_notification_context_unicast (nc,
280 &result->header, GNUNET_NO);
281 GNUNET_free (result);
286 * Handle SEARCH message.
289 * @param client identification of the client
290 * @param message the actual message
293 handle_search (void *cls,
294 struct GNUNET_SERVER_Client *client,
295 const struct GNUNET_MessageHeader *message)
297 const struct SearchMessage *sm;
299 struct ClientEntry *ce;
302 size = ntohs (message->size);
303 sm = (const struct SearchMessage *) message;
304 string = (const char *) &sm[1];
305 if ( (size <= sizeof (struct SearchMessage)) ||
306 ('\0' != string[size - sizeof (struct SearchMessage) - 1]) )
309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
312 ce = GNUNET_new (struct ClientEntry);
314 ce->sh = GNUNET_REGEX_search (dht,
316 &handle_search_result,
323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
326 GNUNET_CONTAINER_DLL_insert (client_head,
329 GNUNET_SERVER_notification_context_add (nc, client);
330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
335 * Process regex requests.
338 * @param server the initialized server
339 * @param cfg configuration to use
342 run (void *cls, struct GNUNET_SERVER_Handle *server,
343 const struct GNUNET_CONFIGURATION_Handle *cfg)
345 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
346 {&handle_announce, NULL, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 0},
347 {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
350 dht = GNUNET_DHT_connect (cfg, 1024);
353 GNUNET_SCHEDULER_shutdown ();
356 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
358 nc = GNUNET_SERVER_notification_context_create (server, 1);
359 stats = GNUNET_STATISTICS_create ("regex", cfg);
360 GNUNET_SERVER_add_handlers (server, handlers);
361 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
366 * The main function for the regex service.
368 * @param argc number of arguments from the command line
369 * @param argv command line arguments
370 * @return 0 ok, 1 on error
373 main (int argc, char *const *argv)
376 GNUNET_SERVICE_run (argc, argv, "regex",
377 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
380 /* end of gnunet-service-regex.c */