697b04a36b25ffd5e2799d9f7fb32390e24975e7
[oweals/gnunet.git] / src / regex / gnunet-service-regex.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "regex_internal_lib.h"
30 #include "regex_ipc.h"
31
32
33 /**
34  * Information about one of our clients.
35  */
36 struct ClientEntry
37 {
38
39   /**
40    * Kept in DLL.
41    */
42   struct ClientEntry *next;
43
44   /**
45    * Kept in DLL.
46    */
47   struct ClientEntry *prev;
48
49   /**
50    * Handle identifying the client.
51    */
52   struct GNUNET_SERVER_Client *client;
53
54   /**
55    * Search handle (if this client is searching).
56    */
57   struct REGEX_INTERNAL_Search *sh;
58
59   /**
60    * Announcement handle (if this client is announcing).
61    */
62   struct REGEX_INTERNAL_Announcement *ah;
63
64   /**
65    * Refresh frequency for announcements.
66    */
67   struct GNUNET_TIME_Relative frequency;
68
69   /**
70    * Task for re-announcing.
71    */
72   GNUNET_SCHEDULER_TaskIdentifier refresh_task;
73
74 };
75
76
77 /**
78  * Connection to the DHT.
79  */
80 static struct GNUNET_DHT_Handle *dht;
81
82 /**
83  * Handle for doing statistics.
84  */
85 static struct GNUNET_STATISTICS_Handle *stats;
86
87 /**
88  * Head of list of clients.
89  */
90 static struct ClientEntry *client_head;
91
92 /**
93  * End of list of clients.
94  */
95 static struct ClientEntry *client_tail;
96
97 /**
98  * Our notification context, used to send back results to the client.
99  */
100 static struct GNUNET_SERVER_NotificationContext *nc;
101
102
103 /**
104  * Task run during shutdown.
105  *
106  * @param cls unused
107  * @param tc unused
108  */
109 static void
110 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   GNUNET_DHT_disconnect (dht);
113   dht = NULL;
114   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
115   stats = NULL;
116   GNUNET_SERVER_notification_context_destroy (nc);
117   nc = NULL;
118 }
119
120
121 /**
122  * A client disconnected.  Remove all of its data structure entries.
123  *
124  * @param cls closure, NULL
125  * @param client identification of the client
126  */
127 static void
128 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
129 {
130   struct ClientEntry *ce;
131   struct ClientEntry *nx;
132
133   nx = client_head;
134   for (ce = nx; NULL != ce; ce = nx)
135   {
136     nx = ce->next;
137     if (ce->client == client)
138     {
139       if (GNUNET_SCHEDULER_NO_TASK != ce->refresh_task)
140       {
141         GNUNET_SCHEDULER_cancel (ce->refresh_task);
142         ce->refresh_task = GNUNET_SCHEDULER_NO_TASK;
143       }
144       if (NULL != ce->ah)
145       {
146         REGEX_INTERNAL_announce_cancel (ce->ah);
147         ce->ah = NULL;
148       }
149       if (NULL != ce->sh)
150       {
151         REGEX_INTERNAL_search_cancel (ce->sh);
152         ce->sh = NULL;
153       }
154       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
155       GNUNET_free (ce);
156     }
157   }
158 }
159
160
161 /**
162  * Periodic task to refresh our announcement of the regex.
163  *
164  * @param cls the 'struct ClientEntry' of the client that triggered the
165  *        announcement
166  * @param tc scheduler context
167  */
168 static void
169 reannounce (void *cls,
170             const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   struct ClientEntry *ce = cls;
173
174   REGEX_INTERNAL_reannounce (ce->ah);
175   ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
176                                                    &reannounce,
177                                                    ce);
178 }
179
180
181 /**
182  * Handle ANNOUNCE message.
183  *
184  * @param cls closure
185  * @param client identification of the client
186  * @param message the actual message
187  */
188 static void
189 handle_announce (void *cls, 
190                  struct GNUNET_SERVER_Client *client,
191                  const struct GNUNET_MessageHeader *message)
192 {
193   const struct AnnounceMessage *am;
194   const char *regex;
195   struct ClientEntry *ce;
196   uint16_t size;
197
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]) )
203   {
204     GNUNET_break (0);
205     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
206     return;
207   }
208   ce = GNUNET_new (struct ClientEntry);
209   ce->client = client;
210   ce->ah = REGEX_INTERNAL_announce (dht,
211                                   &am->pid,
212                                   regex,
213                                   ntohs (am->compression),
214                                   stats);
215   if (NULL == ce->ah)
216   {
217     GNUNET_break (0);
218     GNUNET_free (ce);
219     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
220     return;
221   }
222   ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
223   ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
224                                                    &reannounce,
225                                                    ce);
226   GNUNET_CONTAINER_DLL_insert (client_head,
227                                client_tail, 
228                                ce);
229   GNUNET_SERVER_receive_done (client, GNUNET_OK);
230 }
231
232
233 /**
234  * Handle result, pass it back to the client.
235  *
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.
242  */
243 static void 
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)
250 {
251   struct ClientEntry *ce = cls;
252   struct ResultMessage *result;
253   struct GNUNET_PeerIdentity *gp;
254   uint16_t size;
255
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)
260   {
261     GNUNET_break (0);
262     return;
263   }
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);
270   result->id = *id;
271   gp = &result->id;
272   memcpy (&gp[1], 
273           get_path,
274           get_path_length * sizeof (struct GNUNET_PeerIdentity));
275   memcpy (&gp[1 + get_path_length], 
276           put_path, 
277           put_path_length * sizeof (struct GNUNET_PeerIdentity));
278   GNUNET_SERVER_notification_context_unicast (nc,
279                                               ce->client,
280                                               &result->header, GNUNET_NO);
281   GNUNET_free (result);
282 }
283
284
285 /**
286  * Handle SEARCH message.
287  *
288  * @param cls closure
289  * @param client identification of the client
290  * @param message the actual message
291  */
292 static void
293 handle_search (void *cls, 
294                struct GNUNET_SERVER_Client *client,
295                const struct GNUNET_MessageHeader *message)
296 {
297   const struct SearchMessage *sm;
298   const char *string;
299   struct ClientEntry *ce;
300   uint16_t size;
301
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]) )
307   {
308     GNUNET_break (0);
309     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
310     return;
311   }
312   ce = GNUNET_new (struct ClientEntry);
313   ce->client = client;
314   ce->sh = REGEX_INTERNAL_search (dht,
315                                 string,
316                                 &handle_search_result,
317                                 ce,
318                                 stats);
319   if (NULL == ce->sh)
320   {
321     GNUNET_break (0);
322     GNUNET_free (ce);
323     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
324     return;
325   }
326   GNUNET_CONTAINER_DLL_insert (client_head,
327                                client_tail, 
328                                ce);
329   GNUNET_SERVER_notification_context_add (nc, client);
330   GNUNET_SERVER_receive_done (client, GNUNET_OK);
331 }
332
333
334 /**
335  * Process regex requests.
336  *
337  * @param cls closure
338  * @param server the initialized server
339  * @param cfg configuration to use
340  */
341 static void
342 run (void *cls, struct GNUNET_SERVER_Handle *server,
343      const struct GNUNET_CONFIGURATION_Handle *cfg)
344 {
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},
348     {NULL, NULL, 0, 0}
349   };
350   dht = GNUNET_DHT_connect (cfg, 1024);
351   if (NULL == dht)
352   {
353     GNUNET_SCHEDULER_shutdown ();
354     return;
355   }
356   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
357                                 NULL);
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);
362 }
363
364
365 /**
366  * The main function for the regex service.
367  *
368  * @param argc number of arguments from the command line
369  * @param argv command line arguments
370  * @return 0 ok, 1 on error
371  */
372 int
373 main (int argc, char *const *argv)
374 {
375   return (GNUNET_OK ==
376           GNUNET_SERVICE_run (argc, argv, "regex",
377                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
378 }
379
380 /* end of gnunet-service-regex.c */