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
42 * Client connected to the set service.
44 struct GNUNET_CLIENT_Connection *client;
47 * Message queue for 'client'.
49 struct GNUNET_MQ_Handle *mq;
52 * Linked list of operations on the set.
54 struct GNUNET_SET_OperationHandle *ops_head;
57 * Linked list of operations on the set.
59 struct GNUNET_SET_OperationHandle *ops_tail;
62 * Should the set be destroyed once all operations are gone?
64 int destroy_requested;
67 * Has the set become invalid (e.g. service died)?
72 * Callback for the current iteration over the set,
73 * NULL if no iterator is active.
75 GNUNET_SET_ElementIterator iterator;
78 * Closure for 'iterator'
85 * Opaque handle to a set operation request from another peer.
87 struct GNUNET_SET_Request
90 * Id of the request, used to identify the request when
91 * accepting/rejecting it.
96 * Has the request been accepted already?
97 * GNUNET_YES/GNUNET_NO
104 * Handle to an operation.
105 * Only known to the service after commiting
106 * the handle with a set.
108 struct GNUNET_SET_OperationHandle
111 * Function to be called when we have a result,
114 GNUNET_SET_ResultIterator result_cb;
117 * Closure for result_cb.
122 * Local set used for the operation,
123 * NULL if no set has been provided by conclude yet.
125 struct GNUNET_SET_Handle *set;
128 * Request ID to identify the operation within the set.
133 * Message sent to the server on calling conclude,
134 * NULL if conclude has been called.
136 struct GNUNET_MQ_Envelope *conclude_mqm;
139 * Address of the request if in the conclude message,
140 * used to patch the request id into the message when the set is known.
142 uint32_t *request_id_addr;
145 * Handles are kept in a linked list.
147 struct GNUNET_SET_OperationHandle *prev;
150 * Handles are kept in a linked list.
152 struct GNUNET_SET_OperationHandle *next;
157 * Opaque handle to a listen operation.
159 struct GNUNET_SET_ListenHandle
162 * Connection to the service.
164 struct GNUNET_CLIENT_Connection *client;
167 * Message queue for the client.
169 struct GNUNET_MQ_Handle* mq;
172 * Configuration handle for the listener, stored
173 * here to be able to reconnect transparently on
174 * connection failure.
176 const struct GNUNET_CONFIGURATION_Handle *cfg;
179 * Function to call on a new incoming request,
182 GNUNET_SET_ListenCallback listen_cb;
185 * Closure for listen_cb.
190 * Operation we listen for.
192 enum GNUNET_SET_OperationType operation;
195 * Application ID we listen for.
197 struct GNUNET_HashCode app_id;
200 * Time to wait until we try to reconnect on failure.
202 struct GNUNET_TIME_Relative reconnect_backoff;
206 /* forward declaration */
208 listen_connect (void *cls,
209 const struct GNUNET_SCHEDULER_TaskContext *tc);
213 * Handle element for iteration over the set.
216 * @param mh the message
219 handle_iter_element (void *cls, const struct GNUNET_MessageHeader *mh)
221 struct GNUNET_SET_Handle *set = cls;
222 struct GNUNET_SET_Element element;
223 const struct GNUNET_SET_IterResponseMessage *msg =
224 (const struct GNUNET_SET_IterResponseMessage *) mh;
225 struct GNUNET_SET_IterAckMessage *ack_msg;
226 struct GNUNET_MQ_Envelope *ev;
228 if (NULL == set->iterator)
231 element.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_IterResponseMessage);
232 element.type = htons (msg->element_type);
233 element.data = &msg[1];
234 set->iterator (set->iterator_cls, &element);
235 ev = GNUNET_MQ_msg (ack_msg, GNUNET_MESSAGE_TYPE_SET_ITER_ACK);
236 ack_msg->send_more = htonl (1);
237 GNUNET_MQ_send (set->mq, ev);
242 * Handle element for iteration over the set.
245 * @param mh the message
248 handle_iter_done (void *cls, const struct GNUNET_MessageHeader *mh)
250 struct GNUNET_SET_Handle *set = cls;
252 if (NULL == set->iterator)
255 set->iterator (set->iterator_cls, NULL);
260 * Handle result message for a set operation.
263 * @param mh the message
266 handle_result (void *cls, const struct GNUNET_MessageHeader *mh)
268 const struct GNUNET_SET_ResultMessage *msg;
269 struct GNUNET_SET_Handle *set = cls;
270 struct GNUNET_SET_OperationHandle *oh;
271 struct GNUNET_SET_Element e;
272 enum GNUNET_SET_Status result_status;
274 msg = (const struct GNUNET_SET_ResultMessage *) mh;
275 GNUNET_assert (NULL != set);
276 GNUNET_assert (NULL != set->mq);
278 result_status = ntohs (msg->result_status);
280 oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
281 GNUNET_assert (NULL != oh);
282 /* status is not STATUS_OK => there's no attached element,
283 * and this is the last result message we get */
284 if (GNUNET_SET_STATUS_OK != result_status)
286 GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
287 GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
288 if (GNUNET_YES == oh->set->destroy_requested)
289 GNUNET_SET_destroy (oh->set);
290 if (NULL != oh->result_cb)
291 oh->result_cb (oh->result_cls, NULL, result_status);
297 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
298 e.type = msg->element_type;
299 if (NULL != oh->result_cb)
300 oh->result_cb (oh->result_cls, &e, result_status);
305 * Handle request message for a listen operation
307 * @param cls the listen handle
308 * @param mh the message
311 handle_request (void *cls, const struct GNUNET_MessageHeader *mh)
313 const struct GNUNET_SET_RequestMessage *msg = (const struct GNUNET_SET_RequestMessage *) mh;
314 struct GNUNET_SET_ListenHandle *lh = cls;
315 struct GNUNET_SET_Request *req;
316 struct GNUNET_MessageHeader *context_msg;
318 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing operation request\n");
319 req = GNUNET_new (struct GNUNET_SET_Request);
320 req->accept_id = ntohl (msg->accept_id);
321 context_msg = GNUNET_MQ_extract_nested_mh (msg);
322 /* calling GNUNET_SET_accept in the listen cb will set req->accepted */
323 lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
325 /* we got another request => reset the backoff */
326 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
328 if (GNUNET_NO == req->accepted)
330 struct GNUNET_MQ_Envelope *mqm;
331 struct GNUNET_SET_AcceptRejectMessage *amsg;
333 mqm = GNUNET_MQ_msg (amsg, GNUNET_MESSAGE_TYPE_SET_REJECT);
334 /* no request id, as we refused */
335 amsg->request_id = htonl (0);
336 amsg->accept_reject_id = msg->accept_id;
337 GNUNET_MQ_send (lh->mq, mqm);
338 LOG (GNUNET_ERROR_TYPE_DEBUG, "rejecting request\n");
342 LOG (GNUNET_ERROR_TYPE_DEBUG, "processed op request from service\n");
344 /* the accept-case is handled in GNUNET_SET_accept,
345 * as we have the accept message available there */
350 handle_client_listener_error (void *cls, enum GNUNET_MQ_Error error)
352 struct GNUNET_SET_ListenHandle *lh = cls;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "listener broke down, re-connecting\n");
355 GNUNET_CLIENT_disconnect (lh->client);
357 GNUNET_MQ_destroy (lh->mq);
360 GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff, listen_connect, lh);
361 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
366 * Destroy the set handle if no operations are left, mark the set
367 * for destruction otherwise.
369 * @param set set handle to destroy
372 set_destroy (struct GNUNET_SET_Handle *set)
374 if (NULL != set->ops_head)
376 set->destroy_requested = GNUNET_YES;
379 GNUNET_CLIENT_disconnect (set->client);
381 GNUNET_MQ_destroy (set->mq);
391 * Cancel the given set operation. We need to send an explicit cancel message,
392 * as all operations one one set communicate using one handle.
394 * In contrast to GNUNET_SET_operation_cancel, this function indicates whether
395 * the set of the operation has been destroyed because all operations are done and
396 * the set's destruction was requested before.
398 * @param oh set operation to cancel
399 * @return GNUNET_YES if the set of the operation was destroyed
402 set_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
406 if (NULL != oh->conclude_mqm)
407 GNUNET_MQ_discard (oh->conclude_mqm);
409 /* is the operation already commited? */
412 struct GNUNET_SET_OperationHandle *h_assoc;
413 struct GNUNET_MQ_Envelope *mqm;
415 GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
416 h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
417 GNUNET_assert ((h_assoc == NULL) || (h_assoc == oh));
418 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_CANCEL);
419 GNUNET_MQ_send (oh->set->mq, mqm);
421 if (GNUNET_YES == oh->set->destroy_requested)
422 ret = set_destroy (oh->set);
432 * Cancel the given set operation. We need to send an explicit cancel message,
433 * as all operations one one set communicate using one handle.
435 * @param oh set operation to cancel
438 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
440 (void) set_operation_cancel (oh);
445 handle_client_set_error (void *cls, enum GNUNET_MQ_Error error)
447 struct GNUNET_SET_Handle *set = cls;
449 while (NULL != set->ops_head)
451 if (NULL != set->ops_head->result_cb)
452 set->ops_head->result_cb (set->ops_head->result_cls, NULL,
453 GNUNET_SET_STATUS_FAILURE);
454 if (GNUNET_YES == set_operation_cancel (set->ops_head))
455 return; /* stop if the set is destroyed */
457 set->invalid = GNUNET_YES;
462 * Create an empty set, supporting the specified operation.
464 * @param cfg configuration to use for connecting to the
466 * @param op operation supported by the set
467 * Note that the operation has to be specified
468 * beforehand, as certain set operations need to maintain
469 * data structures spefific to the operation
470 * @return a handle to the set
472 struct GNUNET_SET_Handle *
473 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
474 enum GNUNET_SET_OperationType op)
476 struct GNUNET_SET_Handle *set;
477 struct GNUNET_MQ_Envelope *mqm;
478 struct GNUNET_SET_CreateMessage *msg;
479 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
480 {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT, 0},
481 {handle_iter_element, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, 0},
482 {handle_iter_done, GNUNET_MESSAGE_TYPE_SET_ITER_DONE, 0},
483 GNUNET_MQ_HANDLERS_END
486 set = GNUNET_new (struct GNUNET_SET_Handle);
487 set->client = GNUNET_CLIENT_connect ("set", cfg);
488 LOG (GNUNET_ERROR_TYPE_DEBUG, "set client created\n");
489 GNUNET_assert (NULL != set->client);
490 set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers,
491 handle_client_set_error, set);
492 GNUNET_assert (NULL != set->mq);
493 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
494 msg->operation = htons (op);
495 GNUNET_MQ_send (set->mq, mqm);
501 * Add an element to the given set.
502 * After the element has been added (in the sense of being
503 * transmitted to the set service), cont will be called.
504 * Calls to add_element can be queued
506 * @param set set to add element to
507 * @param element element to add to the set
508 * @param cont continuation called after the element has been added
509 * @param cont_cls closure for @a cont
510 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
511 * set is invalid (e.g. the set service crashed)
514 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
515 const struct GNUNET_SET_Element *element,
516 GNUNET_SET_Continuation cont,
519 struct GNUNET_MQ_Envelope *mqm;
520 struct GNUNET_SET_ElementMessage *msg;
522 if (GNUNET_YES == set->invalid)
526 return GNUNET_SYSERR;
529 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
530 msg->element_type = element->type;
531 memcpy (&msg[1], element->data, element->size);
532 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
533 GNUNET_MQ_send (set->mq, mqm);
539 * Remove an element to the given set.
540 * After the element has been removed (in the sense of the
541 * request being transmitted to the set service), cont will be called.
542 * Calls to remove_element can be queued
544 * @param set set to remove element from
545 * @param element element to remove from the set
546 * @param cont continuation called after the element has been removed
547 * @param cont_cls closure for cont
548 * @return GNUNET_OK on success, GNUNET_SYSERR if the
549 * set is invalid (e.g. the set service crashed)
552 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
553 const struct GNUNET_SET_Element *element,
554 GNUNET_SET_Continuation cont,
557 struct GNUNET_MQ_Envelope *mqm;
558 struct GNUNET_SET_ElementMessage *msg;
560 if (GNUNET_YES == set->invalid)
564 return GNUNET_SYSERR;
567 mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
568 msg->element_type = element->type;
569 memcpy (&msg[1], element->data, element->size);
570 GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
571 GNUNET_MQ_send (set->mq, mqm);
577 * Destroy the set handle, and free all associated resources.
579 * @param set set handle to destroy
582 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
584 (void) set_destroy (set);
589 * Prepare a set operation to be evaluated with another peer.
590 * The evaluation will not start until the client provides
591 * a local set with GNUNET_SET_commit.
593 * @param other_peer peer with the other set
594 * @param app_id hash for the application using the set
595 * @param context_msg additional information for the request
596 * @param salt salt used for the set operation; sometimes set operations
597 * fail due to hash collisions, using a different salt for each operation
598 * makes it harder for an attacker to exploit this
599 * @param result_mode specified how results will be returned,
600 * see 'GNUNET_SET_ResultMode'.
601 * @param result_cb called on error or success
602 * @param result_cls closure for result_cb
603 * @return a handle to cancel the operation
605 struct GNUNET_SET_OperationHandle *
606 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
607 const struct GNUNET_HashCode *app_id,
608 const struct GNUNET_MessageHeader *context_msg,
610 enum GNUNET_SET_ResultMode result_mode,
611 GNUNET_SET_ResultIterator result_cb,
614 struct GNUNET_MQ_Envelope *mqm;
615 struct GNUNET_SET_OperationHandle *oh;
616 struct GNUNET_SET_EvaluateMessage *msg;
618 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
619 oh->result_cb = result_cb;
620 oh->result_cls = result_cls;
622 mqm = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_EVALUATE, context_msg);
624 msg->app_id = *app_id;
625 msg->result_mode = htons (result_mode);
626 msg->target_peer = *other_peer;
628 oh->conclude_mqm = mqm;
629 oh->request_id_addr = &msg->request_id;
636 * Connect to the set service in order to listen
639 * @param cls the listen handle to connect
640 * @param tc task context if invoked as a task, NULL otherwise
643 listen_connect (void *cls,
644 const struct GNUNET_SCHEDULER_TaskContext *tc)
646 struct GNUNET_MQ_Envelope *mqm;
647 struct GNUNET_SET_ListenMessage *msg;
648 struct GNUNET_SET_ListenHandle *lh = cls;
649 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
650 {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
651 GNUNET_MQ_HANDLERS_END
654 GNUNET_assert (NULL == lh->client);
655 lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
656 if (NULL == lh->client)
658 LOG (GNUNET_ERROR_TYPE_ERROR,
659 "could not connect to set (wrong configuration?), giving up listening\n");
662 GNUNET_assert (NULL == lh->mq);
663 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers,
664 handle_client_listener_error, lh);
665 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
666 msg->operation = htonl (lh->operation);
667 msg->app_id = lh->app_id;
668 GNUNET_MQ_send (lh->mq, mqm);
673 * Wait for set operation requests for the given application id
675 * @param cfg configuration to use for connecting to
676 * the set service, needs to be valid for the lifetime of the listen handle
677 * @param operation operation we want to listen for
678 * @param app_id id of the application that handles set operation requests
679 * @param listen_cb called for each incoming request matching the operation
681 * @param listen_cls handle for listen_cb
682 * @return a handle that can be used to cancel the listen operation
684 struct GNUNET_SET_ListenHandle *
685 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
686 enum GNUNET_SET_OperationType operation,
687 const struct GNUNET_HashCode *app_id,
688 GNUNET_SET_ListenCallback listen_cb,
691 struct GNUNET_SET_ListenHandle *lh;
693 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
694 lh->listen_cb = listen_cb;
695 lh->listen_cls = listen_cls;
697 lh->operation = operation;
698 lh->app_id = *app_id;
699 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
700 listen_connect (lh, NULL);
706 * Cancel the given listen operation.
708 * @param lh handle for the listen operation
711 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
713 LOG (GNUNET_ERROR_TYPE_DEBUG, "canceling listener\n");
714 GNUNET_MQ_destroy (lh->mq);
715 GNUNET_CLIENT_disconnect (lh->client);
721 * Accept a request we got via #GNUNET_SET_listen. Must be called during
722 * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
724 * Call #GNUNET_SET_commit to provide the local set to use for the operation,
725 * and to begin the exchange with the remote peer.
727 * @param request request to accept
728 * @param result_mode specified how results will be returned,
729 * see 'GNUNET_SET_ResultMode'.
730 * @param result_cb callback for the results
731 * @param result_cls closure for result_cb
732 * @return a handle to cancel the operation
734 struct GNUNET_SET_OperationHandle *
735 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
736 enum GNUNET_SET_ResultMode result_mode,
737 GNUNET_SET_ResultIterator result_cb,
740 struct GNUNET_MQ_Envelope *mqm;
741 struct GNUNET_SET_OperationHandle *oh;
742 struct GNUNET_SET_AcceptRejectMessage *msg;
744 GNUNET_assert (NULL != request);
745 GNUNET_assert (GNUNET_NO == request->accepted);
746 request->accepted = GNUNET_YES;
748 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
749 oh->result_cb = result_cb;
750 oh->result_cls = result_cls;
752 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
753 msg->accept_reject_id = htonl (request->accept_id);
754 msg->result_mode = htons (result_mode);
756 oh->conclude_mqm = mqm;
757 oh->request_id_addr = &msg->request_id;
764 * Commit a set to be used with a set operation.
765 * This function is called once we have fully constructed
766 * the set that we want to use for the operation. At this
767 * time, the P2P protocol can then begin to exchange the
768 * set information and call the result callback with the
769 * result information.
771 * @param oh handle to the set operation
772 * @param set the set to use for the operation
773 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
774 * set is invalid (e.g. the set service crashed)
777 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
778 struct GNUNET_SET_Handle *set)
780 GNUNET_assert (NULL == oh->set);
781 if (GNUNET_YES == set->invalid)
782 return GNUNET_SYSERR;
783 GNUNET_assert (NULL != oh->conclude_mqm);
785 GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, oh);
786 oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
787 *oh->request_id_addr = htonl (oh->request_id);
788 GNUNET_MQ_send (set->mq, oh->conclude_mqm);
789 oh->conclude_mqm = NULL;
790 oh->request_id_addr = NULL;
796 * Iterate over all elements in the given set.
797 * Note that this operation involves transferring every element of the set
798 * from the service to the client, and is thus costly.
800 * @param set the set to iterate over
801 * @param iter the iterator to call for each element
802 * @param cls closure for @a iter
803 * @return #GNUNET_YES if the iteration started successfuly,
804 * #GNUNET_NO if another iteration is active
805 * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
808 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set, GNUNET_SET_ElementIterator iter, void *cls)
810 struct GNUNET_MQ_Envelope *ev;
812 GNUNET_assert (NULL != iter);
814 if (GNUNET_YES == set->invalid)
815 return GNUNET_SYSERR;
816 if (NULL != set->iterator)
819 set->iterator = iter;
820 set->iterator_cls = cls;
821 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
822 GNUNET_MQ_send (set->mq, ev);
826 /* end of set_api.c */