2 This file is part of GNUnet.
3 (C) 2012, 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.
23 * @brief api for the set service
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_set_service.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
37 * Opaque handle to a set.
39 struct GNUNET_SET_Handle
41 struct GNUNET_CLIENT_Connection *client;
42 struct GNUNET_MQ_Handle *mq;
43 unsigned int messages_since_ack;
44 struct GNUNET_SET_OperationHandle *ops_head;
45 struct GNUNET_SET_OperationHandle *ops_tail;
46 int destroy_requested;
51 * Opaque handle to a set operation request from another peer.
53 struct GNUNET_SET_Request
56 * Id of the request, used to identify the request when
57 * accepting/rejecting it.
62 * Has the request been accepted already?
63 * GNUNET_YES/GNUNET_NO
70 * Handle to an operation.
71 * Only known to the service after commiting
72 * the handle with a set.
74 struct GNUNET_SET_OperationHandle
77 * Function to be called when we have a result,
80 GNUNET_SET_ResultIterator result_cb;
83 * Closure for result_cb.
88 * Local set used for the operation,
89 * NULL if no set has been provided by conclude yet.
91 struct GNUNET_SET_Handle *set;
94 * Request ID to identify the operation within the set.
99 * Message sent to the server on calling conclude,
100 * NULL if conclude has been called.
102 struct GNUNET_MQ_Envelope *conclude_mqm;
105 * Address of the request if in the conclude message,
106 * used to patch the request id into the message when the set is known.
108 uint32_t *request_id_addr;
111 * Handles are kept in a linked list.
113 struct GNUNET_SET_OperationHandle *prev;
116 * Handles are kept in a linked list.
118 struct GNUNET_SET_OperationHandle *next;
124 * Opaque handle to a listen operation.
126 struct GNUNET_SET_ListenHandle
129 * Connection to the service.
131 struct GNUNET_CLIENT_Connection *client;
134 * Message queue for the client.
136 struct GNUNET_MQ_Handle* mq;
139 * Function to call on a new incoming request,
142 GNUNET_SET_ListenCallback listen_cb;
145 * Closure for listen_cb.
152 * Handle result message for a set operation.
155 * @param mh the message
158 handle_result (void *cls, const struct GNUNET_MessageHeader *mh)
160 struct GNUNET_SET_ResultMessage *msg = (struct GNUNET_SET_ResultMessage *) mh;
161 struct GNUNET_SET_Handle *set = cls;
162 struct GNUNET_SET_OperationHandle *oh;
163 struct GNUNET_SET_Element e;
164 enum GNUNET_SET_Status result_status;
166 GNUNET_assert (NULL != set);
167 GNUNET_assert (NULL != set->mq);
169 result_status = ntohs (msg->result_status);
171 if (set->messages_since_ack >= GNUNET_SET_ACK_WINDOW/2)
173 struct GNUNET_MQ_Envelope *mqm;
174 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ACK);
175 GNUNET_MQ_send (set->mq, mqm);
177 oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
178 GNUNET_assert (NULL != oh);
179 /* status is not STATUS_OK => there's no attached element,
180 * and this is the last result message we get */
181 if (GNUNET_SET_STATUS_OK != result_status)
183 GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
184 GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
185 if (GNUNET_YES == oh->set->destroy_requested)
186 GNUNET_SET_destroy (oh->set);
187 if (NULL != oh->result_cb)
188 oh->result_cb (oh->result_cls, NULL, result_status);
194 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
195 e.type = msg->element_type;
196 if (NULL != oh->result_cb)
197 oh->result_cb (oh->result_cls, &e, result_status);
201 * Handle request message for a listen operation
203 * @param cls the listen handle
204 * @param mh the message
207 handle_request (void *cls, const struct GNUNET_MessageHeader *mh)
209 struct GNUNET_SET_RequestMessage *msg = (struct GNUNET_SET_RequestMessage *) mh;
210 struct GNUNET_SET_ListenHandle *lh = cls;
211 struct GNUNET_SET_Request *req;
212 struct GNUNET_MessageHeader *context_msg;
214 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing operation request\n");
215 req = GNUNET_new (struct GNUNET_SET_Request);
216 req->accept_id = ntohl (msg->accept_id);
217 context_msg = GNUNET_MQ_extract_nested_mh (msg);
218 /* calling GNUNET_SET_accept in the listen cb will set req->accepted */
219 lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
221 if (GNUNET_NO == req->accepted)
223 struct GNUNET_MQ_Envelope *mqm;
224 struct GNUNET_SET_AcceptRejectMessage *amsg;
226 mqm = GNUNET_MQ_msg (amsg, GNUNET_MESSAGE_TYPE_SET_REJECT);
227 /* no request id, as we refused */
228 amsg->request_id = htonl (0);
229 amsg->accept_reject_id = msg->accept_id;
230 GNUNET_MQ_send (lh->mq, mqm);
232 LOG (GNUNET_ERROR_TYPE_DEBUG, "rejecting request\n");
235 LOG (GNUNET_ERROR_TYPE_DEBUG, "processed op request from service\n");
237 /* the accept-case is handled in GNUNET_SET_accept,
238 * as we have the accept message available there */
243 handle_client_listener_error (void *cls, enum GNUNET_MQ_Error error)
245 struct GNUNET_SET_ListenHandle *lh = cls;
247 lh->listen_cb (lh->listen_cls, NULL, NULL, NULL);
252 handle_client_set_error (void *cls, enum GNUNET_MQ_Error error)
254 struct GNUNET_SET_Handle *set = cls;
256 while (NULL != set->ops_head)
258 if (NULL != set->ops_head->result_cb)
259 set->ops_head->result_cb (set->ops_head->result_cls, NULL,
260 GNUNET_SET_STATUS_FAILURE);
261 GNUNET_SET_operation_cancel (set->ops_head);
264 /* FIXME: there should be a set error handler */
269 * Create an empty set, supporting the specified operation.
271 * @param cfg configuration to use for connecting to the
273 * @param op operation supported by the set
274 * Note that the operation has to be specified
275 * beforehand, as certain set operations need to maintain
276 * data structures spefific to the operation
277 * @return a handle to the set
279 struct GNUNET_SET_Handle *
280 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
281 enum GNUNET_SET_OperationType op)
283 struct GNUNET_SET_Handle *set;
284 struct GNUNET_MQ_Envelope *mqm;
285 struct GNUNET_SET_CreateMessage *msg;
286 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
287 {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT, 0},
288 GNUNET_MQ_HANDLERS_END
291 set = GNUNET_new (struct GNUNET_SET_Handle);
292 set->client = GNUNET_CLIENT_connect ("set", cfg);
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "set client created\n");
294 GNUNET_assert (NULL != set->client);
295 set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers,
296 handle_client_set_error, set);
297 GNUNET_assert (NULL != set->mq);
298 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
299 msg->operation = htons (op);
300 GNUNET_MQ_send (set->mq, mqm);
306 * Add an element to the given set.
307 * After the element has been added (in the sense of being
308 * transmitted to the set service), cont will be called.
309 * Calls to add_element can be queued
311 * @param set set to add element to
312 * @param element element to add to the set
313 * @param cont continuation called after the element has been added
314 * @param cont_cls closure for cont
317 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
318 const struct GNUNET_SET_Element *element,
319 GNUNET_SET_Continuation cont,
322 struct GNUNET_MQ_Envelope *mqm;
323 struct GNUNET_SET_ElementMessage *msg;
325 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
326 msg->element_type = element->type;
327 memcpy (&msg[1], element->data, element->size);
328 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
329 GNUNET_MQ_send (set->mq, mqm);
334 * Remove an element to the given set.
335 * After the element has been removed (in the sense of the
336 * request being transmitted to the set service), cont will be called.
337 * Calls to remove_element can be queued
339 * @param set set to remove element from
340 * @param element element to remove from the set
341 * @param cont continuation called after the element has been removed
342 * @param cont_cls closure for cont
345 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
346 const struct GNUNET_SET_Element *element,
347 GNUNET_SET_Continuation cont,
350 struct GNUNET_MQ_Envelope *mqm;
351 struct GNUNET_SET_ElementMessage *msg;
353 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
354 msg->element_type = element->type;
355 memcpy (&msg[1], element->data, element->size);
356 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
357 GNUNET_MQ_send (set->mq, mqm);
362 * Destroy the set handle, and free all associated resources.
365 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
367 if (NULL != set->ops_head)
369 set->destroy_requested = GNUNET_YES;
372 GNUNET_CLIENT_disconnect (set->client);
374 GNUNET_MQ_destroy (set->mq);
380 * Prepare a set operation to be evaluated with another peer.
381 * The evaluation will not start until the client provides
382 * a local set with GNUNET_SET_commit.
384 * @param other_peer peer with the other set
385 * @param app_id hash for the application using the set
386 * @param context_msg additional information for the request
387 * @param salt salt used for the set operation; sometimes set operations
388 * fail due to hash collisions, using a different salt for each operation
389 * makes it harder for an attacker to exploit this
390 * @param result_mode specified how results will be returned,
391 * see 'GNUNET_SET_ResultMode'.
392 * @param result_cb called on error or success
393 * @param result_cls closure for result_cb
394 * @return a handle to cancel the operation
396 struct GNUNET_SET_OperationHandle *
397 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
398 const struct GNUNET_HashCode *app_id,
399 const struct GNUNET_MessageHeader *context_msg,
401 enum GNUNET_SET_ResultMode result_mode,
402 GNUNET_SET_ResultIterator result_cb,
405 struct GNUNET_MQ_Envelope *mqm;
406 struct GNUNET_SET_OperationHandle *oh;
407 struct GNUNET_SET_EvaluateMessage *msg;
409 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
410 oh->result_cb = result_cb;
411 oh->result_cls = result_cls;
413 mqm = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_EVALUATE, context_msg);
415 msg->app_id = *app_id;
416 msg->target_peer = *other_peer;
419 oh->conclude_mqm = mqm;
420 oh->request_id_addr = &msg->request_id;
426 * Wait for set operation requests for the given application id
428 * @param cfg configuration to use for connecting to
430 * @param operation operation we want to listen for
431 * @param app_id id of the application that handles set operation requests
432 * @param listen_cb called for each incoming request matching the operation
434 * @param listen_cls handle for listen_cb
435 * @return a handle that can be used to cancel the listen operation
437 struct GNUNET_SET_ListenHandle *
438 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
439 enum GNUNET_SET_OperationType operation,
440 const struct GNUNET_HashCode *app_id,
441 GNUNET_SET_ListenCallback listen_cb,
444 struct GNUNET_SET_ListenHandle *lh;
445 struct GNUNET_MQ_Envelope *mqm;
446 struct GNUNET_SET_ListenMessage *msg;
447 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
448 {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
449 GNUNET_MQ_HANDLERS_END
452 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
453 lh->client = GNUNET_CLIENT_connect ("set", cfg);
454 lh->listen_cb = listen_cb;
455 lh->listen_cls = listen_cls;
456 GNUNET_assert (NULL != lh->client);
457 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers,
458 handle_client_listener_error, lh);
459 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
460 msg->operation = htons (operation);
461 msg->app_id = *app_id;
462 GNUNET_MQ_send (lh->mq, mqm);
469 * Cancel the given listen operation.
471 * @param lh handle for the listen operation
474 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
476 GNUNET_CLIENT_disconnect (lh->client);
477 GNUNET_MQ_destroy (lh->mq);
483 * Accept a request we got via GNUNET_SET_listen. Must be called during
484 * GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
486 * Call GNUNET_SET_conclude to provide the local set to use for the operation,
487 * and to begin the exchange with the remote peer.
489 * @param request request to accept
490 * @param result_mode specified how results will be returned,
491 * see 'GNUNET_SET_ResultMode'.
492 * @param result_cb callback for the results
493 * @param result_cls closure for result_cb
494 * @return a handle to cancel the operation
496 struct GNUNET_SET_OperationHandle *
497 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
498 enum GNUNET_SET_ResultMode result_mode,
499 GNUNET_SET_ResultIterator result_cb,
502 struct GNUNET_MQ_Envelope *mqm;
503 struct GNUNET_SET_OperationHandle *oh;
504 struct GNUNET_SET_AcceptRejectMessage *msg;
506 GNUNET_assert (NULL != request);
507 GNUNET_assert (GNUNET_NO == request->accepted);
508 request->accepted = GNUNET_YES;
510 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
511 oh->result_cb = result_cb;
512 oh->result_cls = result_cls;
514 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
515 msg->accept_reject_id = htonl (request->accept_id);
517 oh->conclude_mqm = mqm;
518 oh->request_id_addr = &msg->request_id;
525 * Cancel the given set operation.
526 * We need to send an explicit cancel message, as
527 * all operations communicate with the set's client
530 * @param oh set operation to cancel
533 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
535 struct GNUNET_MQ_Envelope *mqm;
536 struct GNUNET_SET_OperationHandle *h_assoc;
538 GNUNET_assert (NULL != oh->set);
540 GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
541 h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
542 GNUNET_assert (h_assoc == oh);
543 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_CANCEL);
544 GNUNET_MQ_send (oh->set->mq, mqm);
546 if (NULL != oh->conclude_mqm)
547 GNUNET_MQ_discard (oh->conclude_mqm);
549 if (GNUNET_YES == oh->set->destroy_requested)
550 GNUNET_SET_destroy (oh->set);
557 * Commit a set to be used with a set operation.
558 * This function is called once we have fully constructed
559 * the set that we want to use for the operation. At this
560 * time, the P2P protocol can then begin to exchange the
561 * set information and call the result callback with the
562 * result information.
564 * @param oh handle to the set operation
565 * @param set the set to use for the operation
568 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
569 struct GNUNET_SET_Handle *set)
571 GNUNET_assert (NULL == oh->set);
572 GNUNET_assert (NULL != oh->conclude_mqm);
574 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, oh);
575 oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
576 *oh->request_id_addr = htonl (oh->request_id);
577 GNUNET_MQ_send (set->mq, oh->conclude_mqm);
578 oh->conclude_mqm = NULL;
579 oh->request_id_addr = NULL;