removing GNUNET_CRYPTO_ecc_key_free, use GNUNET_free directly instead
[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  * Private key for this peer.
104  */
105 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
106
107
108 /**
109  * Task run during shutdown.
110  *
111  * @param cls unused
112  * @param tc unused
113  */
114 static void
115 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   GNUNET_DHT_disconnect (dht);
118   dht = NULL;
119   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
120   stats = NULL;
121   GNUNET_SERVER_notification_context_destroy (nc);
122   nc = NULL;
123   GNUNET_free (my_private_key);
124   my_private_key = NULL;
125 }
126
127
128 /**
129  * A client disconnected.  Remove all of its data structure entries.
130  *
131  * @param cls closure, NULL
132  * @param client identification of the client
133  */
134 static void
135 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
136 {
137   struct ClientEntry *ce;
138   struct ClientEntry *nx;
139
140   nx = client_head;
141   for (ce = nx; NULL != ce; ce = nx)
142   {
143     nx = ce->next;
144     if (ce->client == client)
145     {
146       if (GNUNET_SCHEDULER_NO_TASK != ce->refresh_task)
147       {
148         GNUNET_SCHEDULER_cancel (ce->refresh_task);
149         ce->refresh_task = GNUNET_SCHEDULER_NO_TASK;
150       }
151       if (NULL != ce->ah)
152       {
153         REGEX_INTERNAL_announce_cancel (ce->ah);
154         ce->ah = NULL;
155       }
156       if (NULL != ce->sh)
157       {
158         REGEX_INTERNAL_search_cancel (ce->sh);
159         ce->sh = NULL;
160       }
161       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
162       GNUNET_free (ce);
163     }
164   }
165 }
166
167
168 /**
169  * Periodic task to refresh our announcement of the regex.
170  *
171  * @param cls the 'struct ClientEntry' of the client that triggered the
172  *        announcement
173  * @param tc scheduler context
174  */
175 static void
176 reannounce (void *cls,
177             const struct GNUNET_SCHEDULER_TaskContext *tc)
178 {
179   struct ClientEntry *ce = cls;
180
181   REGEX_INTERNAL_reannounce (ce->ah);
182   ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
183                                                    &reannounce,
184                                                    ce);
185 }
186
187
188 /**
189  * Handle ANNOUNCE message.
190  *
191  * @param cls closure
192  * @param client identification of the client
193  * @param message the actual message
194  */
195 static void
196 handle_announce (void *cls, 
197                  struct GNUNET_SERVER_Client *client,
198                  const struct GNUNET_MessageHeader *message)
199 {
200   const struct AnnounceMessage *am;
201   const char *regex;
202   struct ClientEntry *ce;
203   uint16_t size;
204
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]) )
210   {
211     GNUNET_break (0);
212     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
213     return;
214   }
215
216   ce = GNUNET_new (struct ClientEntry);
217   ce->client = client;
218   ce->frequency = GNUNET_TIME_relative_ntoh (am->refresh_delay);
219   ce->refresh_task = GNUNET_SCHEDULER_add_delayed (ce->frequency,
220                                                    &reannounce,
221                                                    ce);
222   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223               "Starting to announce regex `%s' every %s\n",
224               regex,
225               GNUNET_STRINGS_relative_time_to_string (ce->frequency,
226                                                       GNUNET_NO));
227   ce->ah = REGEX_INTERNAL_announce (dht,
228                                     my_private_key,
229                                     regex,
230                                     ntohs (am->compression),
231                                     stats);
232   if (NULL == ce->ah)
233   {
234     GNUNET_break (0);
235     GNUNET_SCHEDULER_cancel (ce->refresh_task);
236     GNUNET_free (ce);
237     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
238     return;
239   }
240   GNUNET_CONTAINER_DLL_insert (client_head,
241                                client_tail, 
242                                ce);
243   GNUNET_SERVER_receive_done (client, GNUNET_OK);
244 }
245
246
247 /**
248  * Handle result, pass it back to the client.
249  *
250  * @param cls the struct ClientEntry of the client searching
251  * @param id Peer providing a regex that matches the string.
252  * @param get_path Path of the get request.
253  * @param get_path_length Lenght of get_path.
254  * @param put_path Path of the put request.
255  * @param put_path_length Length of the put_path.
256  */
257 static void 
258 handle_search_result (void *cls,
259                       const struct GNUNET_PeerIdentity *id,
260                       const struct GNUNET_PeerIdentity *get_path,
261                       unsigned int get_path_length,
262                       const struct GNUNET_PeerIdentity *put_path,
263                       unsigned int put_path_length)
264 {
265   struct ClientEntry *ce = cls;
266   struct ResultMessage *result;
267   struct GNUNET_PeerIdentity *gp;
268   uint16_t size;
269
270   if ( (get_path_length >= 65536) ||
271        (put_path_length >= 65536) ||
272        ( (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity))
273        + sizeof (struct ResultMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
274   {
275     GNUNET_break (0);
276     return;
277   }
278   size = (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity) + sizeof (struct ResultMessage);
279   result = GNUNET_malloc (size);
280   result->header.size = htons (size);
281   result->header.type = htons (GNUNET_MESSAGE_TYPE_REGEX_RESULT);
282   result->get_path_length = htons ((uint16_t) get_path_length);
283   result->put_path_length = htons ((uint16_t) put_path_length);
284   result->id = *id;
285   gp = &result->id;
286   memcpy (&gp[1], 
287           get_path,
288           get_path_length * sizeof (struct GNUNET_PeerIdentity));
289   memcpy (&gp[1 + get_path_length], 
290           put_path, 
291           put_path_length * sizeof (struct GNUNET_PeerIdentity));
292   GNUNET_SERVER_notification_context_unicast (nc,
293                                               ce->client,
294                                               &result->header, GNUNET_NO);
295   GNUNET_free (result);
296 }
297
298
299 /**
300  * Handle SEARCH message.
301  *
302  * @param cls closure
303  * @param client identification of the client
304  * @param message the actual message
305  */
306 static void
307 handle_search (void *cls, 
308                struct GNUNET_SERVER_Client *client,
309                const struct GNUNET_MessageHeader *message)
310 {
311   const struct SearchMessage *sm;
312   const char *string;
313   struct ClientEntry *ce;
314   uint16_t size;
315
316   size = ntohs (message->size);
317   sm = (const struct SearchMessage *) message;
318   string = (const char *) &sm[1];
319   if ( (size <= sizeof (struct SearchMessage)) ||
320        ('\0' != string[size - sizeof (struct SearchMessage) - 1]) )
321   {
322     GNUNET_break (0);
323     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
324     return;
325   }
326   ce = GNUNET_new (struct ClientEntry);
327   ce->client = client;
328   ce->sh = REGEX_INTERNAL_search (dht,
329                                 string,
330                                 &handle_search_result,
331                                 ce,
332                                 stats);
333   if (NULL == ce->sh)
334   {
335     GNUNET_break (0);
336     GNUNET_free (ce);
337     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
338     return;
339   }
340   GNUNET_CONTAINER_DLL_insert (client_head,
341                                client_tail, 
342                                ce);
343   GNUNET_SERVER_notification_context_add (nc, client);
344   GNUNET_SERVER_receive_done (client, GNUNET_OK);
345 }
346
347
348 /**
349  * Process regex requests.
350  *
351  * @param cls closure
352  * @param server the initialized server
353  * @param cfg configuration to use
354  */
355 static void
356 run (void *cls, struct GNUNET_SERVER_Handle *server,
357      const struct GNUNET_CONFIGURATION_Handle *cfg)
358 {
359   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
360     {&handle_announce, NULL, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, 0},
361     {&handle_search, NULL, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, 0},
362     {NULL, NULL, 0, 0}
363   };
364
365   my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
366   if (NULL == my_private_key)
367   {   
368     GNUNET_SCHEDULER_shutdown ();
369     return;
370   }
371   dht = GNUNET_DHT_connect (cfg, 1024);
372   if (NULL == dht)
373   {
374     GNUNET_free (my_private_key);
375     my_private_key = NULL;
376     GNUNET_SCHEDULER_shutdown ();
377     return;
378   }
379   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
380                                 NULL);
381   nc = GNUNET_SERVER_notification_context_create (server, 1);
382   stats = GNUNET_STATISTICS_create ("regex", cfg);
383   GNUNET_SERVER_add_handlers (server, handlers);
384   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
385 }
386
387
388 /**
389  * The main function for the regex service.
390  *
391  * @param argc number of arguments from the command line
392  * @param argv command line arguments
393  * @return 0 ok, 1 on error
394  */
395 int
396 main (int argc, char *const *argv)
397 {
398   return (GNUNET_OK ==
399           GNUNET_SERVICE_run (argc, argv, "regex",
400                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
401 }
402
403 /* end of gnunet-service-regex.c */