- distribute peers equally among island nodes on SuperMUC
[oweals/gnunet.git] / src / set / set_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 set/set_api.c
23  * @brief api for the set service
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_set_service.h"
31 #include "set.h"
32 #include "mq.h"
33
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
36
37 /**
38  * Opaque handle to a set.
39  */
40 struct GNUNET_SET_Handle
41 {
42   struct GNUNET_CLIENT_Connection *client;
43   struct GNUNET_MQ_MessageQueue *mq;
44   unsigned int messages_since_ack;
45 };
46
47 /**
48  * Opaque handle to a set operation request from another peer.
49  */
50 struct GNUNET_SET_Request
51 {
52   uint32_t request_id;
53   int accepted;
54 };
55
56
57 struct GNUNET_SET_OperationHandle
58 {
59   GNUNET_SET_ResultIterator result_cb;
60   void *result_cls;
61   struct GNUNET_SET_Handle *set;
62   uint32_t request_id;
63   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
64 };
65
66
67 /**
68  * Opaque handle to a listen operation.
69  */
70 struct GNUNET_SET_ListenHandle
71 {
72   struct GNUNET_CLIENT_Connection *client;
73   struct GNUNET_MQ_MessageQueue* mq;
74   GNUNET_SET_ListenCallback listen_cb;
75   void *listen_cls;
76 };
77
78
79 /**
80  * Handle result message for a set operation.
81  *
82  * @param cls the set
83  * @param mh the message
84  */
85 static void
86 handle_result (void *cls, const struct GNUNET_MessageHeader *mh)
87 {
88   struct ResultMessage *msg = (struct ResultMessage *) mh;
89   struct GNUNET_SET_Handle *set = cls;
90   struct GNUNET_SET_OperationHandle *oh;
91   struct GNUNET_SET_Element e;
92
93   if (set->messages_since_ack >= GNUNET_SET_ACK_WINDOW/2)
94   {
95     struct GNUNET_MQ_Message *mqm;
96     mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ACK);
97     GNUNET_MQ_send (set->mq, mqm);
98   }
99
100   oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
101   GNUNET_break (NULL != oh);
102   if (GNUNET_SCHEDULER_NO_TASK != oh->timeout_task)
103   {
104     GNUNET_SCHEDULER_cancel (oh->timeout_task);
105     oh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107   if (htons (msg->result_status) != GNUNET_SET_STATUS_OK)
108   {
109     if (NULL != oh->result_cb)
110       oh->result_cb (oh->result_cls, NULL, htons (msg->result_status));
111     GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
112     GNUNET_free (oh);
113     return;
114   }
115   e.data = &msg[1];
116   e.size = ntohs (mh->size) - sizeof (struct ResultMessage);
117   e.type = msg->element_type;
118   if (NULL != oh->result_cb)
119     oh->result_cb (oh->result_cls, &e, htons (msg->result_status));
120 }
121
122 /**
123  * Handle request message for a listen operation
124  *
125  * @param cls the listen handle
126  * @param mh the message
127  */
128 static void
129 handle_request (void *cls, const struct GNUNET_MessageHeader *mh)
130 {
131   struct RequestMessage *msg = (struct RequestMessage *) mh;
132   struct GNUNET_SET_ListenHandle *lh = cls;
133   struct GNUNET_SET_Request *req;
134
135   req = GNUNET_new (struct GNUNET_SET_Request);
136   req->request_id = ntohl (msg->request_id);
137   lh->listen_cb (lh->listen_cls, &msg->peer_id, &mh[1], req);
138   if (GNUNET_NO == req->accepted)
139   {
140     struct GNUNET_MQ_Message *mqm;
141     struct AcceptMessage *amsg;
142     
143     mqm = GNUNET_MQ_msg (amsg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
144     amsg->request_id = msg->request_id;
145     GNUNET_MQ_send (lh->mq, mqm);
146     GNUNET_free (req);
147   }
148 }
149
150
151 /**
152  * Create an empty set, supporting the specified operation.
153  *
154  * @param cfg configuration to use for connecting to the
155  *        set service
156  * @param op operation supported by the set
157  *        Note that the operation has to be specified
158  *        beforehand, as certain set operations need to maintain
159  *        data structures spefific to the operation
160  * @return a handle to the set
161  */
162 struct GNUNET_SET_Handle *
163 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
164                    enum GNUNET_SET_OperationType op)
165 {
166   struct GNUNET_SET_Handle *set;
167   struct GNUNET_MQ_Message *mqm;
168   struct SetCreateMessage *msg;
169   static const struct GNUNET_MQ_Handler mq_handlers[] = {
170     {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT},
171     GNUNET_MQ_HANDLERS_END
172   };
173
174   set = GNUNET_new (struct GNUNET_SET_Handle);
175   set->client = GNUNET_CLIENT_connect ("set", cfg);
176   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set client created\n");
177   GNUNET_assert (NULL != set->client);
178   set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers, set);
179   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
180   msg->operation = htons (op);
181   GNUNET_MQ_send (set->mq, mqm);
182   return set;
183 }
184
185
186 /**
187  * Add an element to the given set.
188  * After the element has been added (in the sense of being
189  * transmitted to the set service), cont will be called.
190  * Calls to add_element can be queued
191  *
192  * @param set set to add element to
193  * @param element element to add to the set
194  * @param cont continuation called after the element has been added
195  * @param cont_cls closure for cont
196  */
197 void
198 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
199                         const struct GNUNET_SET_Element *element,
200                         GNUNET_SET_Continuation cont,
201                         void *cont_cls)
202 {
203   struct GNUNET_MQ_Message *mqm;
204   struct ElementMessage *msg;
205
206   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
207   msg->element_type = element->type;
208   memcpy (&msg[1], element->data, element->size);
209   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
210   GNUNET_MQ_send (set->mq, mqm);
211 }
212
213
214 /**
215  * Remove an element to the given set.
216  * After the element has been removed (in the sense of the
217  * request being transmitted to the set service), cont will be called.
218  * Calls to remove_element can be queued
219  *
220  * @param set set to remove element from
221  * @param element element to remove from the set
222  * @param cont continuation called after the element has been removed
223  * @param cont_cls closure for cont
224  */
225 void
226 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
227                            const struct GNUNET_SET_Element *element,
228                            GNUNET_SET_Continuation cont,
229                            void *cont_cls)
230 {
231   struct GNUNET_MQ_Message *mqm;
232   struct ElementMessage *msg;
233
234   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
235   msg->element_type = element->type;
236   memcpy (&msg[1], element->data, element->size);
237   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
238   GNUNET_MQ_send (set->mq, mqm);
239 }
240
241
242 /**
243  * Destroy the set handle, and free all associated resources.
244  */
245 void
246 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
247 {
248   GNUNET_CLIENT_disconnect (set->client);
249   set->client = NULL;
250   GNUNET_MQ_destroy (set->mq);
251   set->mq = NULL;
252 }
253
254
255 /**
256  * Signature of the main function of a task.
257  *
258  * @param cls closure
259  * @param tc context information (why was this task triggered now)
260  */
261 static void
262 operation_timeout_task (void *cls,
263                         const struct GNUNET_SCHEDULER_TaskContext * tc)
264 {
265   struct GNUNET_SET_OperationHandle *oh = cls;
266   oh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
267   if (NULL != oh->result_cb)
268     oh->result_cb (oh->result_cls, NULL, GNUNET_SET_STATUS_TIMEOUT);
269   oh->result_cb = NULL;
270   oh->result_cls = NULL;
271   GNUNET_SET_operation_cancel (oh);
272 }
273
274
275 /**
276  * Evaluate a set operation with our set and the set of another peer.
277  *
278  * @param set set to use
279  * @param salt salt for HKDF (explain more here)
280  * @param other_peer peer with the other set
281  * @param app_id hash for the application using the set
282  * @param context_msg additional information for the request
283  * @param salt salt used for the set operation; sometimes set operations
284  *        fail due to hash collisions, using a different salt for each operation
285  *        makes it harder for an attacker to exploit this
286  * @param timeout result_cb will be called with GNUNET_SET_STATUS_TIMEOUT
287  *        if the operation is not done after the specified time
288  * @param result_mode specified how results will be returned,
289  *        see 'GNUNET_SET_ResultMode'.
290  * @param result_cb called on error or success
291  * @param result_cls closure for result_cb
292  * @return a handle to cancel the operation
293  */
294 struct GNUNET_SET_OperationHandle *
295 GNUNET_SET_evaluate (struct GNUNET_SET_Handle *set,
296                      const struct GNUNET_PeerIdentity *other_peer,
297                      const struct GNUNET_HashCode *app_id,
298                      const struct GNUNET_MessageHeader *context_msg,
299                      uint16_t salt,
300                      struct GNUNET_TIME_Relative timeout,
301                      enum GNUNET_SET_ResultMode result_mode,
302                      GNUNET_SET_ResultIterator result_cb,
303                      void *result_cls)
304 {
305   struct GNUNET_MQ_Message *mqm;
306   struct EvaluateMessage *msg;
307   struct GNUNET_SET_OperationHandle *oh;
308
309   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
310   oh->result_cb = result_cb;
311   oh->result_cls = result_cls;
312   oh->set = set;
313
314   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_EVALUATE);
315   msg->request_id = htonl (GNUNET_MQ_assoc_add (set->mq, mqm, oh));
316   msg->peer = *other_peer;
317   msg->app_id = *app_id;
318   
319   if (NULL != context_msg)
320     if (GNUNET_OK != GNUNET_MQ_nest (mqm, context_msg, ntohs (context_msg->size)))
321       GNUNET_assert (0);
322   
323   oh->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, operation_timeout_task, oh);
324   GNUNET_MQ_send (set->mq, mqm);
325
326   return oh;
327 }
328
329
330 /**
331  * Wait for set operation requests for the given application id
332  * 
333  * @param cfg configuration to use for connecting to
334  *            the set service
335  * @param operation operation we want to listen for
336  * @param app_id id of the application that handles set operation requests
337  * @param listen_cb called for each incoming request matching the operation
338  *                  and application id
339  * @param listen_cls handle for listen_cb
340  * @return a handle that can be used to cancel the listen operation
341  */
342 struct GNUNET_SET_ListenHandle *
343 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
344                    enum GNUNET_SET_OperationType operation,
345                    const struct GNUNET_HashCode *app_id,
346                    GNUNET_SET_ListenCallback listen_cb,
347                    void *listen_cls)
348 {
349   struct GNUNET_SET_ListenHandle *lh;
350   struct GNUNET_MQ_Message *mqm;
351   struct ListenMessage *msg;
352   static const struct GNUNET_MQ_Handler mq_handlers[] = {
353     {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
354     GNUNET_MQ_HANDLERS_END
355   };
356
357   lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
358   lh->client = GNUNET_CLIENT_connect ("set", cfg);
359   lh->listen_cb = listen_cb;
360   lh->listen_cls = listen_cls;
361   GNUNET_assert (NULL != lh->client);
362   lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers, lh);
363   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
364   msg->operation = htons (operation);
365   msg->app_id = *app_id;
366   GNUNET_MQ_send (lh->mq, mqm);
367
368   return lh;
369 }
370
371
372 /**
373  * Cancel the given listen operation.
374  *
375  * @param lh handle for the listen operation
376  */
377 void
378 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
379 {
380   GNUNET_MQ_destroy (lh->mq);
381   lh->mq = NULL;
382   GNUNET_CLIENT_disconnect (lh->client);
383   lh->client = NULL;
384   lh->listen_cb = NULL;
385   lh->listen_cls = NULL;
386 }
387
388
389 /**
390  * Accept a request we got via GNUNET_SET_listen.
391  *
392  * @param request request to accept
393  * @param set set used for the requested operation 
394  * @param timeout timeout for the set operation
395  * @param result_mode specified how results will be returned,
396  *        see 'GNUNET_SET_ResultMode'.
397  * @param result_cb callback for the results
398  * @param result_cls closure for result_cb
399  * @return a handle to cancel the operation
400  */
401 struct GNUNET_SET_OperationHandle *
402 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
403                    struct GNUNET_SET_Handle *set,
404                    struct GNUNET_TIME_Relative timeout,
405                    enum GNUNET_SET_ResultMode result_mode,
406                    GNUNET_SET_ResultIterator result_cb,
407                    void *result_cls)
408 {
409   struct GNUNET_MQ_Message *mqm;
410   struct AcceptMessage *msg;
411   struct GNUNET_SET_OperationHandle *oh;
412
413   /* don't accept a request twice! */
414   GNUNET_assert (GNUNET_NO == request->accepted);
415   request->accepted = GNUNET_YES;
416
417   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
418   oh->result_cb = result_cb;
419   oh->result_cls = result_cls;
420   oh->set = set;
421
422   mqm = GNUNET_MQ_msg (msg , GNUNET_MESSAGE_TYPE_SET_ACCEPT);
423   msg->request_id = htonl (request->request_id);
424   msg->accepted = 1;
425   GNUNET_MQ_send (set->mq, mqm);
426
427   oh->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, operation_timeout_task, oh);
428
429   return oh;
430 }
431
432
433 /**
434  * Cancel the given set operation.
435  *
436  * @param oh set operation to cancel
437  */
438 void
439 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
440 {
441   struct GNUNET_MQ_Message *mqm;
442   struct GNUNET_SET_OperationHandle *h_assoc;
443
444   h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
445   GNUNET_assert (h_assoc == oh);
446   mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_CANCEL);
447   GNUNET_MQ_send (oh->set->mq, mqm);
448   GNUNET_free (oh);
449 }
450