2 This file is part of GNUnet.
3 Copyright (C) 2012-2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief api for the set service
23 * @author Florian Dold
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_set_service.h"
33 #define LOG(kind, ...) GNUNET_log_from (kind, "set-api", __VA_ARGS__)
37 struct SetCopyRequest *next;
39 struct SetCopyRequest *prev;
43 GNUNET_SET_CopyReadyCallback cb;
47 * Opaque handle to a set.
49 struct GNUNET_SET_Handle
52 * Message queue for @e client.
54 struct GNUNET_MQ_Handle *mq;
57 * Linked list of operations on the set.
59 struct GNUNET_SET_OperationHandle *ops_head;
62 * Linked list of operations on the set.
64 struct GNUNET_SET_OperationHandle *ops_tail;
67 * Callback for the current iteration over the set,
68 * NULL if no iterator is active.
70 GNUNET_SET_ElementIterator iterator;
73 * Closure for @e iterator
78 * Should the set be destroyed once all operations are gone?
79 * #GNUNET_SYSERR if #GNUNET_SET_destroy() must raise this flag,
80 * #GNUNET_YES if #GNUNET_SET_destroy() did raise this flag.
82 int destroy_requested;
85 * Has the set become invalid (e.g. service died)?
90 * Both client and service count the number of iterators
91 * created so far to match replies with iterators.
93 uint16_t iteration_id;
96 * Configuration, needed when creating (lazy) copies.
98 const struct GNUNET_CONFIGURATION_Handle *cfg;
101 * Doubly linked list of copy requests.
103 struct SetCopyRequest *copy_req_head;
106 * Doubly linked list of copy requests.
108 struct SetCopyRequest *copy_req_tail;
113 * Handle for a set operation request from another peer.
115 struct GNUNET_SET_Request
118 * Id of the request, used to identify the request when
119 * accepting/rejecting it.
124 * Has the request been accepted already?
125 * #GNUNET_YES/#GNUNET_NO
132 * Handle to an operation. Only known to the service after committing
133 * the handle with a set.
135 struct GNUNET_SET_OperationHandle
138 * Function to be called when we have a result,
141 GNUNET_SET_ResultIterator result_cb;
144 * Closure for @e result_cb.
149 * Local set used for the operation,
150 * NULL if no set has been provided by conclude yet.
152 struct GNUNET_SET_Handle *set;
155 * Message sent to the server on calling conclude,
156 * NULL if conclude has been called.
158 struct GNUNET_MQ_Envelope *conclude_mqm;
161 * Address of the request if in the conclude message,
162 * used to patch the request id into the message when the set is known.
164 uint32_t *request_id_addr;
167 * Handles are kept in a linked list.
169 struct GNUNET_SET_OperationHandle *prev;
172 * Handles are kept in a linked list.
174 struct GNUNET_SET_OperationHandle *next;
177 * Request ID to identify the operation within the set.
184 * Opaque handle to a listen operation.
186 struct GNUNET_SET_ListenHandle
189 * Message queue for the client.
191 struct GNUNET_MQ_Handle*mq;
194 * Configuration handle for the listener, stored
195 * here to be able to reconnect transparently on
196 * connection failure.
198 const struct GNUNET_CONFIGURATION_Handle *cfg;
201 * Function to call on a new incoming request,
204 GNUNET_SET_ListenCallback listen_cb;
207 * Closure for @e listen_cb.
212 * Application ID we listen for.
214 struct GNUNET_HashCode app_id;
217 * Time to wait until we try to reconnect on failure.
219 struct GNUNET_TIME_Relative reconnect_backoff;
222 * Task for reconnecting when the listener fails.
224 struct GNUNET_SCHEDULER_Task *reconnect_task;
227 * Operation we listen for.
229 enum GNUNET_SET_OperationType operation;
233 /* mutual recursion with handle_copy_lazy */
234 static struct GNUNET_SET_Handle *
235 create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
236 enum GNUNET_SET_OperationType op,
237 const uint32_t *cookie);
241 * Handle element for iteration over the set. Notifies the
242 * iterator and sends an acknowledgement to the service.
244 * @param cls the `struct GNUNET_SET_Handle *`
245 * @param msg the message
248 handle_copy_lazy (void *cls,
249 const struct GNUNET_SET_CopyLazyResponseMessage *msg)
251 struct GNUNET_SET_Handle *set = cls;
252 struct SetCopyRequest *req;
253 struct GNUNET_SET_Handle *new_set;
255 req = set->copy_req_head;
258 /* Service sent us unsolicited lazy copy response */
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Handling response to lazy copy\n");
265 GNUNET_CONTAINER_DLL_remove (set->copy_req_head,
268 // We pass none as operation here, since it doesn't matter when
270 new_set = create_internal (set->cfg,
271 GNUNET_SET_OPERATION_NONE,
273 req->cb (req->cls, new_set);
279 * Check that the given @a msg is well-formed.
282 * @param msg message to check
283 * @return #GNUNET_OK if message is well-formed
286 check_iter_element (void *cls,
287 const struct GNUNET_SET_IterResponseMessage *msg)
289 /* minimum size was already checked, everything else is OK! */
295 * Handle element for iteration over the set. Notifies the
296 * iterator and sends an acknowledgement to the service.
298 * @param cls the `struct GNUNET_SET_Handle *`
299 * @param mh the message
302 handle_iter_element (void *cls,
303 const struct GNUNET_SET_IterResponseMessage *msg)
305 struct GNUNET_SET_Handle *set = cls;
306 GNUNET_SET_ElementIterator iter = set->iterator;
307 struct GNUNET_SET_Element element;
308 struct GNUNET_SET_IterAckMessage *ack_msg;
309 struct GNUNET_MQ_Envelope *ev;
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Received element in set iteration\n");
314 msize = ntohs (msg->header.size);
315 if (set->iteration_id != ntohs (msg->iteration_id))
317 /* element from a previous iteration, skip! */
322 element.size = msize - sizeof(struct GNUNET_SET_IterResponseMessage);
323 element.element_type = ntohs (msg->element_type);
324 element.data = &msg[1];
325 iter (set->iterator_cls,
328 ev = GNUNET_MQ_msg (ack_msg,
329 GNUNET_MESSAGE_TYPE_SET_ITER_ACK);
330 ack_msg->send_more = htonl ((NULL != iter));
331 GNUNET_MQ_send (set->mq, ev);
336 * Handle message signalling conclusion of iteration over the set.
337 * Notifies the iterator that we are done.
340 * @param mh the message
343 handle_iter_done (void *cls,
344 const struct GNUNET_MessageHeader *mh)
346 struct GNUNET_SET_Handle *set = cls;
347 GNUNET_SET_ElementIterator iter = set->iterator;
351 /* FIXME: if this is true, could cancel+start a fresh one
352 cause elements to go to the wrong iteration? */
353 LOG (GNUNET_ERROR_TYPE_INFO,
354 "Service completed set iteration that was already cancelled\n");
357 LOG (GNUNET_ERROR_TYPE_DEBUG,
358 "Set iteration completed\n");
359 set->destroy_requested = GNUNET_SYSERR;
360 set->iterator = NULL;
362 iter (set->iterator_cls,
364 if (GNUNET_SYSERR == set->destroy_requested)
365 set->destroy_requested = GNUNET_NO;
366 if (GNUNET_YES == set->destroy_requested)
367 GNUNET_SET_destroy (set);
372 * Check that the given @a msg is well-formed.
375 * @param msg message to check
376 * @return #GNUNET_OK if message is well-formed
379 check_result (void *cls,
380 const struct GNUNET_SET_ResultMessage *msg)
382 /* minimum size was already checked, everything else is OK! */
388 * Handle result message for a set operation.
391 * @param mh the message
394 handle_result (void *cls,
395 const struct GNUNET_SET_ResultMessage *msg)
397 struct GNUNET_SET_Handle *set = cls;
398 struct GNUNET_SET_OperationHandle *oh;
399 struct GNUNET_SET_Element e;
400 enum GNUNET_SET_Status result_status;
403 GNUNET_assert (NULL != set->mq);
404 result_status = (enum GNUNET_SET_Status) ntohs (msg->result_status);
405 LOG (GNUNET_ERROR_TYPE_DEBUG,
406 "Got result message with status %d\n",
409 oh = GNUNET_MQ_assoc_get (set->mq,
410 ntohl (msg->request_id));
413 /* 'oh' can be NULL if we canceled the operation, but the service
414 did not get the cancel message yet. */
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Ignoring result from canceled operation\n");
420 switch (result_status)
422 case GNUNET_SET_STATUS_OK:
423 case GNUNET_SET_STATUS_ADD_LOCAL:
424 case GNUNET_SET_STATUS_ADD_REMOTE:
427 case GNUNET_SET_STATUS_FAILURE:
428 case GNUNET_SET_STATUS_DONE:
431 case GNUNET_SET_STATUS_HALF_DONE:
432 /* not used anymore */
437 LOG (GNUNET_ERROR_TYPE_DEBUG,
438 "Treating result as final status\n");
439 GNUNET_MQ_assoc_remove (set->mq,
440 ntohl (msg->request_id));
441 GNUNET_CONTAINER_DLL_remove (set->ops_head,
444 /* Need to do this calculation _before_ the result callback,
445 as IF the application still has a valid set handle, it
446 may trigger destruction of the set during the callback. */
447 destroy_set = (GNUNET_YES == set->destroy_requested) &&
448 (NULL == set->ops_head);
449 if (NULL != oh->result_cb)
451 oh->result_cb (oh->result_cls,
453 GNUNET_ntohll (msg->current_size),
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "No callback for final status\n");
462 GNUNET_SET_destroy (set);
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "Treating result as element\n");
470 e.size = ntohs (msg->header.size) - sizeof(struct GNUNET_SET_ResultMessage);
471 e.element_type = ntohs (msg->element_type);
472 if (NULL != oh->result_cb)
473 oh->result_cb (oh->result_cls,
475 GNUNET_ntohll (msg->current_size),
481 * Destroy the given set operation.
483 * @param oh set operation to destroy
486 set_operation_destroy (struct GNUNET_SET_OperationHandle *oh)
488 struct GNUNET_SET_Handle *set = oh->set;
489 struct GNUNET_SET_OperationHandle *h_assoc;
491 if (NULL != oh->conclude_mqm)
492 GNUNET_MQ_discard (oh->conclude_mqm);
493 /* is the operation already commited? */
496 GNUNET_CONTAINER_DLL_remove (set->ops_head,
499 h_assoc = GNUNET_MQ_assoc_remove (set->mq,
501 GNUNET_assert ((NULL == h_assoc) ||
509 * Cancel the given set operation. We need to send an explicit cancel
510 * message, as all operations one one set communicate using one
513 * @param oh set operation to cancel
516 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
518 struct GNUNET_SET_Handle *set = oh->set;
519 struct GNUNET_SET_CancelMessage *m;
520 struct GNUNET_MQ_Envelope *mqm;
522 LOG (GNUNET_ERROR_TYPE_DEBUG,
523 "Cancelling SET operation\n");
526 mqm = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_SET_CANCEL);
527 m->request_id = htonl (oh->request_id);
528 GNUNET_MQ_send (set->mq, mqm);
530 set_operation_destroy (oh);
532 (GNUNET_YES == set->destroy_requested) &&
533 (NULL == set->ops_head))
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "Destroying set after operation cancel\n");
537 GNUNET_SET_destroy (set);
543 * We encountered an error communicating with the set service while
544 * performing a set operation. Report to the application.
546 * @param cls the `struct GNUNET_SET_Handle`
547 * @param error error code
550 handle_client_set_error (void *cls,
551 enum GNUNET_MQ_Error error)
553 struct GNUNET_SET_Handle *set = cls;
554 GNUNET_SET_ElementIterator iter = set->iterator;
556 LOG (GNUNET_ERROR_TYPE_ERROR,
557 "Handling client set error %d\n",
559 while (NULL != set->ops_head)
561 if ((NULL != set->ops_head->result_cb) &&
562 (GNUNET_NO == set->destroy_requested))
563 set->ops_head->result_cb (set->ops_head->result_cls,
566 GNUNET_SET_STATUS_FAILURE);
567 set_operation_destroy (set->ops_head);
569 set->iterator = NULL;
571 set->invalid = GNUNET_YES;
573 iter (set->iterator_cls,
581 static struct GNUNET_SET_Handle *
582 create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
583 enum GNUNET_SET_OperationType op,
584 const uint32_t *cookie)
586 struct GNUNET_SET_Handle *set = GNUNET_new (struct GNUNET_SET_Handle);
587 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
588 GNUNET_MQ_hd_var_size (result,
589 GNUNET_MESSAGE_TYPE_SET_RESULT,
590 struct GNUNET_SET_ResultMessage,
592 GNUNET_MQ_hd_var_size (iter_element,
593 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT,
594 struct GNUNET_SET_IterResponseMessage,
596 GNUNET_MQ_hd_fixed_size (iter_done,
597 GNUNET_MESSAGE_TYPE_SET_ITER_DONE,
598 struct GNUNET_MessageHeader,
600 GNUNET_MQ_hd_fixed_size (copy_lazy,
601 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE,
602 struct GNUNET_SET_CopyLazyResponseMessage,
604 GNUNET_MQ_handler_end ()
606 struct GNUNET_MQ_Envelope *mqm;
607 struct GNUNET_SET_CreateMessage *create_msg;
608 struct GNUNET_SET_CopyLazyConnectMessage *copy_msg;
611 set->mq = GNUNET_CLIENT_connect (cfg,
614 &handle_client_set_error,
623 LOG (GNUNET_ERROR_TYPE_DEBUG,
624 "Creating new set (operation %u)\n",
626 mqm = GNUNET_MQ_msg (create_msg,
627 GNUNET_MESSAGE_TYPE_SET_CREATE);
628 create_msg->operation = htonl (op);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Creating new set (lazy copy)\n",
635 mqm = GNUNET_MQ_msg (copy_msg,
636 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_CONNECT);
637 copy_msg->cookie = *cookie;
639 GNUNET_MQ_send (set->mq,
646 * Create an empty set, supporting the specified operation.
648 * @param cfg configuration to use for connecting to the
650 * @param op operation supported by the set
651 * Note that the operation has to be specified
652 * beforehand, as certain set operations need to maintain
653 * data structures spefific to the operation
654 * @return a handle to the set
656 struct GNUNET_SET_Handle *
657 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
658 enum GNUNET_SET_OperationType op)
660 struct GNUNET_SET_Handle *set;
662 set = create_internal (cfg,
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Creating set %p for operation %d\n",
674 * Add an element to the given set. After the element has been added
675 * (in the sense of being transmitted to the set service), @a cont
676 * will be called. Multiple calls to GNUNET_SET_add_element() can be
679 * @param set set to add element to
680 * @param element element to add to the set
681 * @param cont continuation called after the element has been added
682 * @param cont_cls closure for @a cont
683 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
684 * set is invalid (e.g. the set service crashed)
687 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
688 const struct GNUNET_SET_Element *element,
689 GNUNET_SET_Continuation cont,
692 struct GNUNET_MQ_Envelope *mqm;
693 struct GNUNET_SET_ElementMessage *msg;
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "adding element of type %u to set %p\n",
697 (unsigned int) element->element_type,
699 GNUNET_assert (NULL != set);
700 if (GNUNET_YES == set->invalid)
704 return GNUNET_SYSERR;
706 mqm = GNUNET_MQ_msg_extra (msg,
708 GNUNET_MESSAGE_TYPE_SET_ADD);
709 msg->element_type = htons (element->element_type);
710 GNUNET_memcpy (&msg[1],
713 GNUNET_MQ_notify_sent (mqm,
715 GNUNET_MQ_send (set->mq, mqm);
721 * Remove an element to the given set. After the element has been
722 * removed (in the sense of the request being transmitted to the set
723 * service), @a cont will be called. Multiple calls to
724 * GNUNET_SET_remove_element() can be queued
726 * @param set set to remove element from
727 * @param element element to remove from the set
728 * @param cont continuation called after the element has been removed
729 * @param cont_cls closure for @a cont
730 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
731 * set is invalid (e.g. the set service crashed)
734 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
735 const struct GNUNET_SET_Element *element,
736 GNUNET_SET_Continuation cont,
739 struct GNUNET_MQ_Envelope *mqm;
740 struct GNUNET_SET_ElementMessage *msg;
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Removing element from set %p\n",
745 if (GNUNET_YES == set->invalid)
749 return GNUNET_SYSERR;
751 mqm = GNUNET_MQ_msg_extra (msg,
753 GNUNET_MESSAGE_TYPE_SET_REMOVE);
754 msg->element_type = htons (element->element_type);
755 GNUNET_memcpy (&msg[1],
758 GNUNET_MQ_notify_sent (mqm,
760 GNUNET_MQ_send (set->mq, mqm);
766 * Destroy the set handle if no operations are left, mark the set
767 * for destruction otherwise.
769 * @param set set handle to destroy
772 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
774 /* destroying set while iterator is active is currently
775 not supported; we should expand the API to allow
776 clients to explicitly cancel the iteration! */
777 GNUNET_assert (NULL != set);
778 if ((NULL != set->ops_head) ||
779 (NULL != set->iterator) ||
780 (GNUNET_SYSERR == set->destroy_requested))
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Set operations are pending, delaying set destruction\n");
784 set->destroy_requested = GNUNET_YES;
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "Really destroying set\n");
791 GNUNET_MQ_destroy (set->mq);
799 * Prepare a set operation to be evaluated with another peer.
800 * The evaluation will not start until the client provides
801 * a local set with #GNUNET_SET_commit().
803 * @param other_peer peer with the other set
804 * @param app_id hash for the application using the set
805 * @param context_msg additional information for the request
806 * @param result_mode specified how results will be returned,
807 * see `enum GNUNET_SET_ResultMode`.
808 * @param result_cb called on error or success
809 * @param result_cls closure for @e result_cb
810 * @return a handle to cancel the operation
812 struct GNUNET_SET_OperationHandle *
813 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
814 const struct GNUNET_HashCode *app_id,
815 const struct GNUNET_MessageHeader *context_msg,
816 enum GNUNET_SET_ResultMode result_mode,
817 struct GNUNET_SET_Option options[],
818 GNUNET_SET_ResultIterator result_cb,
821 struct GNUNET_MQ_Envelope *mqm;
822 struct GNUNET_SET_OperationHandle *oh;
823 struct GNUNET_SET_EvaluateMessage *msg;
824 struct GNUNET_SET_Option *opt;
826 LOG (GNUNET_ERROR_TYPE_DEBUG,
827 "Client prepares set operation (%d)\n",
829 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
830 oh->result_cb = result_cb;
831 oh->result_cls = result_cls;
832 mqm = GNUNET_MQ_msg_nested_mh (msg,
833 GNUNET_MESSAGE_TYPE_SET_EVALUATE,
835 msg->app_id = *app_id;
836 msg->result_mode = htonl (result_mode);
837 msg->target_peer = *other_peer;
838 for (opt = options; opt->type != 0; opt++)
842 case GNUNET_SET_OPTION_BYZANTINE:
843 msg->byzantine = GNUNET_YES;
844 msg->byzantine_lower_bound = opt->v.num;
847 case GNUNET_SET_OPTION_FORCE_FULL:
848 msg->force_full = GNUNET_YES;
851 case GNUNET_SET_OPTION_FORCE_DELTA:
852 msg->force_delta = GNUNET_YES;
856 LOG (GNUNET_ERROR_TYPE_ERROR,
857 "Option with type %d not recognized\n", (int) opt->type);
860 oh->conclude_mqm = mqm;
861 oh->request_id_addr = &msg->request_id;
868 * Connect to the set service in order to listen for requests.
870 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
873 listen_connect (void *cls);
877 * Check validity of request message for a listen operation
879 * @param cls the listen handle
880 * @param msg the message
881 * @return #GNUNET_OK if the message is well-formed
884 check_request (void *cls,
885 const struct GNUNET_SET_RequestMessage *msg)
887 const struct GNUNET_MessageHeader *context_msg;
889 if (ntohs (msg->header.size) == sizeof(*msg))
890 return GNUNET_OK; /* no context message is OK */
891 context_msg = GNUNET_MQ_extract_nested_mh (msg);
892 if (NULL == context_msg)
894 /* malformed context message is NOT ok */
896 return GNUNET_SYSERR;
903 * Handle request message for a listen operation
905 * @param cls the listen handle
906 * @param msg the message
909 handle_request (void *cls,
910 const struct GNUNET_SET_RequestMessage *msg)
912 struct GNUNET_SET_ListenHandle *lh = cls;
913 struct GNUNET_SET_Request req;
914 const struct GNUNET_MessageHeader *context_msg;
915 struct GNUNET_MQ_Envelope *mqm;
916 struct GNUNET_SET_RejectMessage *rmsg;
918 LOG (GNUNET_ERROR_TYPE_DEBUG,
919 "Processing incoming operation request with id %u\n",
920 ntohl (msg->accept_id));
921 /* we got another valid request => reset the backoff */
922 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
923 req.accept_id = ntohl (msg->accept_id);
924 req.accepted = GNUNET_NO;
925 context_msg = GNUNET_MQ_extract_nested_mh (msg);
926 /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
927 lh->listen_cb (lh->listen_cls,
931 if (GNUNET_YES == req.accepted)
932 return; /* the accept-case is handled in #GNUNET_SET_accept() */
933 LOG (GNUNET_ERROR_TYPE_DEBUG,
934 "Rejected request %u\n",
935 ntohl (msg->accept_id));
936 mqm = GNUNET_MQ_msg (rmsg,
937 GNUNET_MESSAGE_TYPE_SET_REJECT);
938 rmsg->accept_reject_id = msg->accept_id;
939 GNUNET_MQ_send (lh->mq, mqm);
944 * Our connection with the set service encountered an error,
945 * re-initialize with exponential back-off.
947 * @param cls the `struct GNUNET_SET_ListenHandle *`
948 * @param error reason for the disconnect
951 handle_client_listener_error (void *cls,
952 enum GNUNET_MQ_Error error)
954 struct GNUNET_SET_ListenHandle *lh = cls;
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "Listener broke down (%d), re-connecting\n",
959 GNUNET_MQ_destroy (lh->mq);
961 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
964 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
969 * Connect to the set service in order to listen for requests.
971 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
974 listen_connect (void *cls)
976 struct GNUNET_SET_ListenHandle *lh = cls;
977 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
978 GNUNET_MQ_hd_var_size (request,
979 GNUNET_MESSAGE_TYPE_SET_REQUEST,
980 struct GNUNET_SET_RequestMessage,
982 GNUNET_MQ_handler_end ()
984 struct GNUNET_MQ_Envelope *mqm;
985 struct GNUNET_SET_ListenMessage *msg;
987 lh->reconnect_task = NULL;
988 GNUNET_assert (NULL == lh->mq);
989 lh->mq = GNUNET_CLIENT_connect (lh->cfg,
992 &handle_client_listener_error,
996 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
997 msg->operation = htonl (lh->operation);
998 msg->app_id = lh->app_id;
999 GNUNET_MQ_send (lh->mq,
1005 * Wait for set operation requests for the given application id
1007 * @param cfg configuration to use for connecting to
1008 * the set service, needs to be valid for the lifetime of the listen handle
1009 * @param operation operation we want to listen for
1010 * @param app_id id of the application that handles set operation requests
1011 * @param listen_cb called for each incoming request matching the operation
1012 * and application id
1013 * @param listen_cls handle for @a listen_cb
1014 * @return a handle that can be used to cancel the listen operation
1016 struct GNUNET_SET_ListenHandle *
1017 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
1018 enum GNUNET_SET_OperationType operation,
1019 const struct GNUNET_HashCode *app_id,
1020 GNUNET_SET_ListenCallback listen_cb,
1023 struct GNUNET_SET_ListenHandle *lh;
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "Starting listener for app %s\n",
1027 GNUNET_h2s (app_id));
1028 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
1029 lh->listen_cb = listen_cb;
1030 lh->listen_cls = listen_cls;
1032 lh->operation = operation;
1033 lh->app_id = *app_id;
1034 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1035 listen_connect (lh);
1046 * Cancel the given listen operation.
1048 * @param lh handle for the listen operation
1051 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "Canceling listener %s\n",
1055 GNUNET_h2s (&lh->app_id));
1058 GNUNET_MQ_destroy (lh->mq);
1061 if (NULL != lh->reconnect_task)
1063 GNUNET_SCHEDULER_cancel (lh->reconnect_task);
1064 lh->reconnect_task = NULL;
1071 * Accept a request we got via #GNUNET_SET_listen. Must be called during
1072 * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
1074 * Call #GNUNET_SET_commit to provide the local set to use for the operation,
1075 * and to begin the exchange with the remote peer.
1077 * @param request request to accept
1078 * @param result_mode specified how results will be returned,
1079 * see `enum GNUNET_SET_ResultMode`.
1080 * @param result_cb callback for the results
1081 * @param result_cls closure for @a result_cb
1082 * @return a handle to cancel the operation
1084 struct GNUNET_SET_OperationHandle *
1085 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
1086 enum GNUNET_SET_ResultMode result_mode,
1087 struct GNUNET_SET_Option options[],
1088 GNUNET_SET_ResultIterator result_cb,
1091 struct GNUNET_MQ_Envelope *mqm;
1092 struct GNUNET_SET_OperationHandle *oh;
1093 struct GNUNET_SET_AcceptMessage *msg;
1095 GNUNET_assert (GNUNET_NO == request->accepted);
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 "Client accepts set operation (%d) with id %u\n",
1099 request->accept_id);
1100 request->accepted = GNUNET_YES;
1101 mqm = GNUNET_MQ_msg (msg,
1102 GNUNET_MESSAGE_TYPE_SET_ACCEPT);
1103 msg->accept_reject_id = htonl (request->accept_id);
1104 msg->result_mode = htonl (result_mode);
1105 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
1106 oh->result_cb = result_cb;
1107 oh->result_cls = result_cls;
1108 oh->conclude_mqm = mqm;
1109 oh->request_id_addr = &msg->request_id;
1115 * Commit a set to be used with a set operation.
1116 * This function is called once we have fully constructed
1117 * the set that we want to use for the operation. At this
1118 * time, the P2P protocol can then begin to exchange the
1119 * set information and call the result callback with the
1120 * result information.
1122 * @param oh handle to the set operation
1123 * @param set the set to use for the operation
1124 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
1125 * set is invalid (e.g. the set service crashed)
1128 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
1129 struct GNUNET_SET_Handle *set)
1131 if (NULL != oh->set)
1133 /* Some other set was already committed for this
1134 * operation, there is a logic bug in the client of this API */
1138 GNUNET_assert (NULL != set);
1139 if (GNUNET_YES == set->invalid)
1140 return GNUNET_SYSERR;
1141 LOG (GNUNET_ERROR_TYPE_DEBUG,
1142 "Client commits to SET\n");
1143 GNUNET_assert (NULL != oh->conclude_mqm);
1145 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1148 oh->request_id = GNUNET_MQ_assoc_add (set->mq,
1150 *oh->request_id_addr = htonl (oh->request_id);
1151 GNUNET_MQ_send (set->mq,
1153 oh->conclude_mqm = NULL;
1154 oh->request_id_addr = NULL;
1160 * Iterate over all elements in the given set. Note that this
1161 * operation involves transferring every element of the set from the
1162 * service to the client, and is thus costly.
1164 * @param set the set to iterate over
1165 * @param iter the iterator to call for each element
1166 * @param iter_cls closure for @a iter
1167 * @return #GNUNET_YES if the iteration started successfuly,
1168 * #GNUNET_NO if another iteration is active
1169 * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
1172 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
1173 GNUNET_SET_ElementIterator iter,
1176 struct GNUNET_MQ_Envelope *ev;
1178 GNUNET_assert (NULL != iter);
1179 if (GNUNET_YES == set->invalid)
1180 return GNUNET_SYSERR;
1181 if (NULL != set->iterator)
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Iterating over set\n");
1185 set->iterator = iter;
1186 set->iterator_cls = iter_cls;
1187 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
1188 GNUNET_MQ_send (set->mq, ev);
1194 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
1195 GNUNET_SET_CopyReadyCallback cb,
1198 struct GNUNET_MQ_Envelope *ev;
1199 struct SetCopyRequest *req;
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "Creating lazy copy of set\n");
1203 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_PREPARE);
1204 GNUNET_MQ_send (set->mq, ev);
1206 req = GNUNET_new (struct SetCopyRequest);
1209 GNUNET_CONTAINER_DLL_insert (set->copy_req_head,
1216 * Create a copy of an element. The copy
1217 * must be GNUNET_free-d by the caller.
1219 * @param element the element to copy
1220 * @return the copied element
1222 struct GNUNET_SET_Element *
1223 GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element)
1225 struct GNUNET_SET_Element *copy;
1227 copy = GNUNET_malloc (element->size + sizeof(struct GNUNET_SET_Element));
1228 copy->size = element->size;
1229 copy->element_type = element->element_type;
1230 copy->data = ©[1];
1231 GNUNET_memcpy (©[1],
1239 * Hash a set element.
1241 * @param element the element that should be hashed
1242 * @param[out] ret_hash a pointer to where the hash of @a element
1246 GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element,
1247 struct GNUNET_HashCode *ret_hash)
1249 struct GNUNET_HashContext *ctx = GNUNET_CRYPTO_hash_context_start ();
1251 /* It's not guaranteed that the element data is always after the element header,
1252 so we need to hash the chunks separately. */
1253 GNUNET_CRYPTO_hash_context_read (ctx, &element->size, sizeof(uint16_t));
1254 GNUNET_CRYPTO_hash_context_read (ctx, &element->element_type,
1256 GNUNET_CRYPTO_hash_context_read (ctx, element->data, element->size);
1257 GNUNET_CRYPTO_hash_context_finish (ctx, ret_hash);
1261 /* end of set_api.c */