2 This file is part of GNUnet
3 (C) 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 2, 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.
22 * @file set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
28 #include "gnunet-service-set.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_crypto_lib.h"
32 #include "strata_estimator.h"
33 #include "set_protocol.h"
38 * Number of IBFs in a strata estimator.
40 #define SE_STRATA_COUNT 32
42 * Size of the IBFs in the strata estimator.
44 #define SE_IBF_SIZE 80
46 * hash num parameter for the difference digests and strata estimators
48 #define SE_IBF_HASH_NUM 3
51 * Number of buckets that can be transmitted in one message.
53 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
56 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
57 * Choose this value so that computing the IBF is still cheaper
58 * than transmitting all values.
60 #define MAX_IBF_ORDER (16)
64 * Current phase we are in for a union operation
66 enum UnionOperationPhase
69 * We sent the request message, and expect a strata estimator
73 * We sent the strata estimator, and expect an IBF
77 * We know what type of IBF the other peer wants to send us,
78 * and expect the remaining parts
80 PHASE_EXPECT_IBF_CONT,
82 * We are sending request and elements,
83 * and thus only expect elements from the other peer.
85 PHASE_EXPECT_ELEMENTS,
87 * We are expecting elements and requests, and send
88 * requested elements back to the other peer.
90 PHASE_EXPECT_ELEMENTS_AND_REQUESTS,
92 * The protocol is over.
93 * Results may still have to be sent to the client.
100 * State of an evaluate operation
103 struct UnionEvaluateOperation
106 * Local set the operation is evaluated on.
111 * Peer with the remote set
113 struct GNUNET_PeerIdentity peer;
116 * Application-specific identifier
118 struct GNUNET_HashCode app_id;
121 * Context message, given to us
122 * by the client, may be NULL.
124 struct GNUNET_MessageHeader *context_msg;
127 * Tunnel context for the peer we
128 * evaluate the union operation with.
130 struct TunnelContext *tc;
133 * Request ID to multiplex set operations to
134 * the client inhabiting the set.
139 * Number of ibf buckets received
141 unsigned int ibf_buckets_received;
144 * Copy of the set's strata estimator at the time of
145 * creation of this operation
147 struct StrataEstimator *se;
150 * The ibf we currently receive
152 struct InvertibleBloomFilter *remote_ibf;
155 * IBF of the set's element.
157 struct InvertibleBloomFilter *local_ibf;
160 * Maps IBF-Keys (specific to the current salt) to elements.
162 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
165 * Current state of the operation.
167 enum UnionOperationPhase phase;
170 * Salt to use for this operation.
175 * Generation in which the operation handle
178 unsigned int generation_created;
181 * Evaluate operations are held in
184 struct UnionEvaluateOperation *next;
187 * Evaluate operations are held in
190 struct UnionEvaluateOperation *prev;
195 * Information about the element in a set.
196 * All elements are stored in a hash-table
197 * from their hash-code to their 'struct Element',
198 * so that the remove and add operations are reasonably
204 * The actual element. The data for the element
205 * should be allocated at the end of this struct.
207 struct GNUNET_SET_Element element;
210 * Hash of the element.
211 * Will be used to derive the different IBF keys
212 * for different salts.
214 struct GNUNET_HashCode element_hash;
217 * Generation the element was added by the client.
218 * Operations of earlier generations will not consider the element.
220 unsigned int generation_added;
223 * GNUNET_YES if the element has been removed in some generation.
228 * Generation the element was removed by the client.
229 * Operations of later generations will not consider the element.
230 * Only valid if is_removed is GNUNET_YES.
232 unsigned int generation_removed;
235 * GNUNET_YES if the element is a remote element, and does not belong
236 * to the operation's set.
243 * Entries in the key-to-element map of the union set.
248 * IBF key for the entry, derived from the current salt.
250 struct IBF_Key ibf_key;
253 * The actual element associated with the key
255 struct ElementEntry *element;
258 * Element that collides with this element
261 struct KeyEntry *next_colliding;
265 * Used as a closure for sending elements
266 * with a specific IBF key.
268 struct SendElementClosure
271 * The IBF key whose matching elements should be
274 struct IBF_Key ibf_key;
277 * Operation for which the elements
280 struct UnionEvaluateOperation *eo;
285 * Extra state required for efficient set union.
290 * The strata estimator is only generated once for
292 * The IBF keys are derived from the element hashes with
295 struct StrataEstimator *se;
298 * Maps 'struct GNUNET_HashCode' to 'struct ElementEntry'.
300 struct GNUNET_CONTAINER_MultiHashMap *elements;
303 * Evaluate operations are held in
306 struct UnionEvaluateOperation *ops_head;
309 * Evaluate operations are held in
312 struct UnionEvaluateOperation *ops_tail;
315 * Current generation, that is, number of
316 * previously executed operations on this set
318 unsigned int current_generation;
324 * Iterator over hash map entries.
327 * @param key current key code
328 * @param value value in the hash map
329 * @return GNUNET_YES if we should continue to
334 destroy_elements_iterator (void *cls,
335 const struct GNUNET_HashCode * key,
338 struct ElementEntry *ee = value;
346 * Destroy the elements belonging to a union set.
348 * @param us union state that contains the elements
351 destroy_elements (struct UnionState *us)
353 if (NULL == us->elements)
355 GNUNET_CONTAINER_multihashmap_iterate (us->elements, destroy_elements_iterator, NULL);
356 GNUNET_CONTAINER_multihashmap_destroy (us->elements);
363 * Iterator over hash map entries.
366 * @param key current key code
367 * @param value value in the hash map
368 * @return GNUNET_YES if we should continue to
373 destroy_key_to_element_iter (void *cls,
377 struct KeyEntry *k = value;
381 struct KeyEntry *k_tmp = k;
382 k = k->next_colliding;
390 * Destroy a union operation, and free all resources
391 * associated with it.
393 * @param eo the union operation to destroy
396 _GSS_union_operation_destroy (struct UnionEvaluateOperation *eo)
398 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying union op\n");
402 GNUNET_MQ_destroy (eo->tc->mq);
403 GNUNET_MESH_tunnel_destroy (eo->tc->tunnel);
404 GNUNET_free (eo->tc);
408 if (NULL != eo->remote_ibf)
410 ibf_destroy (eo->remote_ibf);
411 eo->remote_ibf = NULL;
413 if (NULL != eo->local_ibf)
415 ibf_destroy (eo->local_ibf);
416 eo->local_ibf = NULL;
420 strata_estimator_destroy (eo->se);
423 if (NULL != eo->key_to_element)
425 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element, destroy_key_to_element_iter, NULL);
426 GNUNET_CONTAINER_multihashmap32_destroy (eo->key_to_element);
427 eo->key_to_element = NULL;
430 GNUNET_CONTAINER_DLL_remove (eo->set->state.u->ops_head,
431 eo->set->state.u->ops_tail,
436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "destroying union op done\n");
439 /* FIXME: do a garbage collection of the set generations */
444 * Inform the client that the union operation has failed,
445 * and proceed to destroy the evaluate operation.
447 * @param eo the union operation to fail
450 fail_union_operation (struct UnionEvaluateOperation *eo)
452 struct GNUNET_MQ_Envelope *mqm;
453 struct GNUNET_SET_ResultMessage *msg;
455 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
456 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
457 msg->request_id = htonl (eo->request_id);
458 GNUNET_MQ_send (eo->set->client_mq, mqm);
459 _GSS_union_operation_destroy (eo);
464 * Derive the IBF key from a hash code and
467 * @param src the hash code
468 * @param salt salt to use
469 * @return the derived IBF key
471 static struct IBF_Key
472 get_ibf_key (struct GNUNET_HashCode *src, uint16_t salt)
476 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
477 GCRY_MD_SHA512, GCRY_MD_SHA256,
479 &salt, sizeof (salt),
486 * Send a request for the evaluate operation to a remote peer
488 * @param eo operation with the other peer
491 send_operation_request (struct UnionEvaluateOperation *eo)
493 struct GNUNET_MQ_Envelope *mqm;
494 struct OperationRequestMessage *msg;
496 mqm = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
501 /* the context message is too large */
503 GNUNET_SERVER_client_disconnect (eo->set->client);
506 msg->operation = htons (GNUNET_SET_OPERATION_UNION);
507 msg->app_id = eo->app_id;
508 GNUNET_MQ_send (eo->tc->mq, mqm);
510 if (NULL != eo->context_msg)
512 GNUNET_free (eo->context_msg);
513 eo->context_msg = NULL;
516 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sent op request\n");
521 * Iterator to create the mapping between ibf keys
522 * and element entries.
525 * @param key current key code
526 * @param value value in the hash map
527 * @return GNUNET_YES if we should continue to
532 insert_element_iterator (void *cls,
536 struct KeyEntry *const new_k = cls;
537 struct KeyEntry *old_k = value;
539 GNUNET_assert (NULL != old_k);
542 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
544 new_k->next_colliding = old_k->next_colliding;
545 old_k->next_colliding = new_k;
548 old_k = old_k->next_colliding;
549 } while (NULL != old_k);
555 * Insert an element into the union operation's
556 * key-to-element mapping
558 * @param eo the union operation
559 * @param ee the element entry
562 insert_element (struct UnionEvaluateOperation *eo, struct ElementEntry *ee)
565 struct IBF_Key ibf_key;
568 ibf_key = get_ibf_key (&ee->element_hash, eo->salt);
569 k = GNUNET_new (struct KeyEntry);
571 k->ibf_key = ibf_key;
572 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element,
573 (uint32_t) ibf_key.key_val,
574 insert_element_iterator, k);
576 /* was the element inserted into a colliding bucket? */
577 if (GNUNET_SYSERR == ret)
580 GNUNET_CONTAINER_multihashmap32_put (eo->key_to_element, (uint32_t) ibf_key.key_val, k,
581 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
586 * Insert a key into an ibf.
590 * @param value the key entry to get the key from
593 prepare_ibf_iterator (void *cls,
597 struct InvertibleBloomFilter *ibf = cls;
598 struct KeyEntry *ke = value;
600 ibf_insert (ibf, ke->ibf_key);
606 * Iterator for initializing the
607 * key-to-element mapping of a union operation
609 * @param cls the union operation
611 * @param value the element entry to insert
612 * into the key-to-element mapping
615 init_key_to_element_iterator (void *cls,
616 const struct GNUNET_HashCode *key,
619 struct UnionEvaluateOperation *eo = cls;
620 struct ElementEntry *e = value;
622 /* make sure that the element belongs to the set at the time
623 * of creating the operation */
624 if ( (e->generation_added > eo->generation_created) ||
625 ( (GNUNET_YES == e->removed) &&
626 (e->generation_removed < eo->generation_created)))
629 insert_element (eo, e);
635 * Create an ibf with the operation's elements
636 * of the specified size
638 * @param eo the union operation
639 * @param size size of the ibf to create
642 prepare_ibf (struct UnionEvaluateOperation *eo, uint16_t size)
644 if (NULL == eo->key_to_element)
647 len = GNUNET_CONTAINER_multihashmap_size (eo->set->state.u->elements);
648 eo->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
649 GNUNET_CONTAINER_multihashmap_iterate (eo->set->state.u->elements,
650 init_key_to_element_iterator, eo);
652 if (NULL != eo->local_ibf)
653 ibf_destroy (eo->local_ibf);
654 eo->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
655 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element,
656 prepare_ibf_iterator, eo->local_ibf);
661 * Send an ibf of appropriate size.
663 * @param eo the union operation
664 * @param ibf_order order of the ibf to send, size=2^order
667 send_ibf (struct UnionEvaluateOperation *eo, uint16_t ibf_order)
669 unsigned int buckets_sent = 0;
670 struct InvertibleBloomFilter *ibf;
672 prepare_ibf (eo, 1<<ibf_order);
674 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending ibf of size %u\n", 1<<ibf_order);
678 while (buckets_sent < (1 << ibf_order))
680 unsigned int buckets_in_message;
681 struct GNUNET_MQ_Envelope *mqm;
682 struct IBFMessage *msg;
684 buckets_in_message = (1 << ibf_order) - buckets_sent;
685 /* limit to maximum */
686 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
687 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
689 mqm = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
690 GNUNET_MESSAGE_TYPE_SET_P2P_IBF);
691 msg->order = ibf_order;
692 msg->offset = htons (buckets_sent);
693 ibf_write_slice (ibf, buckets_sent,
694 buckets_in_message, &msg[1]);
695 buckets_sent += buckets_in_message;
696 GNUNET_MQ_send (eo->tc->mq, mqm);
699 eo->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
704 * Send a strata estimator to the remote peer.
706 * @param eo the union operation with the remote peer
709 send_strata_estimator (struct UnionEvaluateOperation *eo)
711 struct GNUNET_MQ_Envelope *mqm;
712 struct GNUNET_MessageHeader *strata_msg;
714 mqm = GNUNET_MQ_msg_header_extra (strata_msg,
715 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
716 GNUNET_MESSAGE_TYPE_SET_P2P_SE);
717 strata_estimator_write (eo->set->state.u->se, &strata_msg[1]);
718 GNUNET_MQ_send (eo->tc->mq, mqm);
719 eo->phase = PHASE_EXPECT_IBF;
724 * Compute the necessary order of an ibf
725 * from the size of the symmetric set difference.
727 * @param diff the difference
728 * @return the required size of the ibf
731 get_order_from_difference (unsigned int diff)
733 unsigned int ibf_order;
736 while ((1<<ibf_order) < (2 * diff))
738 if (ibf_order > MAX_IBF_ORDER)
739 ibf_order = MAX_IBF_ORDER;
745 * Handle a strata estimator from a remote peer
747 * @param cls the union operation
748 * @param mh the message
751 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
753 struct UnionEvaluateOperation *eo = cls;
754 struct StrataEstimator *remote_se;
758 if (eo->phase != PHASE_EXPECT_SE)
760 fail_union_operation (eo);
764 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
766 strata_estimator_read (&mh[1], remote_se);
767 GNUNET_assert (NULL != eo->se);
768 diff = strata_estimator_difference (remote_se, eo->se);
769 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got se, diff=%d\n", diff);
770 strata_estimator_destroy (remote_se);
771 strata_estimator_destroy (eo->se);
773 send_ibf (eo, get_order_from_difference (diff));
779 * Iterator to send elements to a remote peer
781 * @param cls closure with the element key and the union operation
783 * @param value the key entry
786 send_element_iterator (void *cls,
790 struct SendElementClosure *sec = cls;
791 struct IBF_Key ibf_key = sec->ibf_key;
792 struct UnionEvaluateOperation *eo = sec->eo;
793 struct KeyEntry *ke = value;
795 if (ke->ibf_key.key_val != ibf_key.key_val)
799 const struct GNUNET_SET_Element *const element = &ke->element->element;
800 struct GNUNET_MQ_Envelope *mqm;
801 struct GNUNET_MessageHeader *mh;
803 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
804 mqm = GNUNET_MQ_msg_header_extra (mh, element->size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
807 /* element too large */
811 memcpy (&mh[1], element->data, element->size);
812 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending element to client\n");
813 GNUNET_MQ_send (eo->tc->mq, mqm);
814 ke = ke->next_colliding;
820 * Send all elements that have the specified IBF key
821 * to the remote peer of the union operation
823 * @param eo union operation
824 * @param ibf_key IBF key of interest
827 send_elements_for_key (struct UnionEvaluateOperation *eo, struct IBF_Key ibf_key)
829 struct SendElementClosure send_cls;
831 send_cls.ibf_key = ibf_key;
833 GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element, (uint32_t) ibf_key.key_val,
834 &send_element_iterator, &send_cls);
839 * Decode which elements are missing on each side, and
840 * send the appropriate elemens and requests
842 * @param eo union operation
845 decode_and_send (struct UnionEvaluateOperation *eo)
849 struct InvertibleBloomFilter *diff_ibf;
851 GNUNET_assert (PHASE_EXPECT_ELEMENTS == eo->phase);
853 prepare_ibf (eo, eo->remote_ibf->size);
854 diff_ibf = ibf_dup (eo->local_ibf);
855 ibf_subtract (diff_ibf, eo->remote_ibf);
861 res = ibf_decode (diff_ibf, &side, &key);
862 if (GNUNET_SYSERR == res)
866 while (1<<next_order < diff_ibf->size)
869 if (next_order <= MAX_IBF_ORDER)
871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
872 "decoding failed, sending larger ibf (size %u)\n",
874 send_ibf (eo, next_order);
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
879 "set union failed: reached ibf limit\n");
883 if (GNUNET_NO == res)
885 struct GNUNET_MQ_Envelope *mqm;
887 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "transmitted all values, sending DONE\n");
888 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
889 GNUNET_MQ_send (eo->tc->mq, mqm);
894 send_elements_for_key (eo, key);
898 struct GNUNET_MQ_Envelope *mqm;
899 struct GNUNET_MessageHeader *msg;
901 /* FIXME: before sending the request, check if we may just have the element */
902 /* FIXME: merge multiple requests */
903 mqm = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
904 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
905 *(struct IBF_Key *) &msg[1] = key;
906 GNUNET_MQ_send (eo->tc->mq, mqm);
909 ibf_destroy (diff_ibf);
914 * Handle an IBF message from a remote peer.
916 * @param cls the union operation
917 * @param mh the header of the message
920 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
922 struct UnionEvaluateOperation *eo = cls;
923 struct IBFMessage *msg = (struct IBFMessage *) mh;
924 unsigned int buckets_in_message;
926 if ( (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
927 (eo->phase == PHASE_EXPECT_IBF) )
929 eo->phase = PHASE_EXPECT_IBF_CONT;
930 GNUNET_assert (NULL == eo->remote_ibf);
931 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "creating new ibf of order %u\n", 1<<msg->order);
932 eo->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
933 if (0 != ntohs (msg->offset))
936 fail_union_operation (eo);
939 else if (eo->phase == PHASE_EXPECT_IBF_CONT)
941 if ( (ntohs (msg->offset) != eo->ibf_buckets_received) ||
942 (1<<msg->order != eo->remote_ibf->size) )
945 fail_union_operation (eo);
950 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
952 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
955 fail_union_operation (eo);
959 ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
960 eo->ibf_buckets_received += buckets_in_message;
962 if (eo->ibf_buckets_received == eo->remote_ibf->size)
965 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received full strata estimator\n");
966 eo->phase = PHASE_EXPECT_ELEMENTS;
967 decode_and_send (eo);
973 * Send a result message to the client indicating
974 * that there is a new element.
976 * @param eo union operation
977 * @param element element to send
980 send_client_element (struct UnionEvaluateOperation *eo,
981 struct GNUNET_SET_Element *element)
983 struct GNUNET_MQ_Envelope *mqm;
984 struct GNUNET_SET_ResultMessage *rm;
986 GNUNET_assert (0 != eo->request_id);
987 mqm = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
990 GNUNET_MQ_discard (mqm);
994 rm->result_status = htons (GNUNET_SET_STATUS_OK);
995 rm->request_id = htonl (eo->request_id);
996 memcpy (&rm[1], element->data, element->size);
997 GNUNET_MQ_send (eo->set->client_mq, mqm);
1002 * Send a result message to the client indicating
1003 * that the operation is over.
1004 * After the result done message has been sent to the client,
1005 * destroy the evaluate operation.
1007 * @param eo union operation
1010 send_client_done_and_destroy (struct UnionEvaluateOperation *eo)
1012 struct GNUNET_MQ_Envelope *mqm;
1013 struct GNUNET_SET_ResultMessage *rm;
1015 GNUNET_assert (0 != eo->request_id);
1016 mqm = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
1017 rm->request_id = htonl (eo->request_id);
1018 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
1019 GNUNET_MQ_send (eo->set->client_mq, mqm);
1025 * Handle an element message from a remote peer.
1027 * @param cls the union operation
1028 * @param mh the message
1031 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
1033 struct UnionEvaluateOperation *eo = cls;
1034 struct ElementEntry *ee;
1035 uint16_t element_size;
1037 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got element from peer\n");
1039 if ( (eo->phase != PHASE_EXPECT_ELEMENTS) &&
1040 (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1042 fail_union_operation (eo);
1046 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1047 ee = GNUNET_malloc (sizeof *eo + element_size);
1048 memcpy (&ee[1], &mh[1], element_size);
1049 ee->element.data = &ee[1];
1050 ee->remote = GNUNET_YES;
1052 insert_element (eo, ee);
1053 send_client_element (eo, &ee->element);
1060 * Handle an element request from a remote peer.
1062 * @param cls the union operation
1063 * @param mh the message
1066 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1068 struct UnionEvaluateOperation *eo = cls;
1069 struct IBF_Key *ibf_key;
1070 unsigned int num_keys;
1072 /* look up elements and send them */
1073 if (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1076 fail_union_operation (eo);
1080 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1082 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1085 fail_union_operation (eo);
1089 ibf_key = (struct IBF_Key *) &mh[1];
1090 while (0 != num_keys--)
1092 send_elements_for_key (eo, *ibf_key);
1099 * Callback used for notifications
1101 * @param cls closure
1104 peer_done_sent_cb (void *cls)
1106 struct UnionEvaluateOperation *eo = cls;
1108 send_client_done_and_destroy (eo);
1113 * Handle a done message from a remote peer
1115 * @param cls the union operation
1116 * @param mh the message
1119 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1121 struct UnionEvaluateOperation *eo = cls;
1123 if (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1125 /* we got all requests, but still have to send our elements as response */
1126 struct GNUNET_MQ_Envelope *mqm;
1128 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got DONE, sending final DONE after elements\n");
1129 eo->phase = PHASE_FINISHED;
1130 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1131 GNUNET_MQ_notify_sent (mqm, peer_done_sent_cb, eo);
1132 GNUNET_MQ_send (eo->tc->mq, mqm);
1135 if (eo->phase == PHASE_EXPECT_ELEMENTS)
1137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got final DONE\n");
1138 eo->phase = PHASE_FINISHED;
1139 send_client_done_and_destroy (eo);
1143 fail_union_operation (eo);
1148 * Evaluate a union operation with
1151 * @param m the evaluate request message from the client
1152 * @param set the set to evaluate the operation with
1155 _GSS_union_evaluate (struct GNUNET_SET_EvaluateMessage *m, struct Set *set)
1157 struct UnionEvaluateOperation *eo;
1158 struct GNUNET_MessageHeader *context_msg;
1160 eo = GNUNET_new (struct UnionEvaluateOperation);
1161 eo->peer = m->target_peer;
1163 eo->request_id = htonl (m->request_id);
1164 GNUNET_assert (0 != eo->request_id);
1165 eo->se = strata_estimator_dup (set->state.u->se);
1166 eo->salt = ntohs (m->salt);
1167 eo->app_id = m->app_id;
1169 context_msg = GNUNET_MQ_extract_nested_mh (m);
1170 if (NULL != context_msg)
1172 eo->context_msg = GNUNET_copy_message (context_msg);
1175 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1176 "evaluating union operation, (app %s)\n",
1177 GNUNET_h2s (&eo->app_id));
1179 eo->tc = GNUNET_new (struct TunnelContext);
1180 eo->tc->tunnel = GNUNET_MESH_tunnel_create (mesh, eo->tc, &eo->peer,
1181 GNUNET_APPLICATION_TYPE_SET);
1182 GNUNET_assert (NULL != eo->tc->tunnel);
1183 eo->tc->peer = eo->peer;
1184 eo->tc->mq = GNUNET_MESH_mq_create (eo->tc->tunnel);
1185 /* we started the operation, thus we have to send the operation request */
1186 eo->phase = PHASE_EXPECT_SE;
1188 GNUNET_CONTAINER_DLL_insert (eo->set->state.u->ops_head,
1189 eo->set->state.u->ops_tail,
1192 send_operation_request (eo);
1197 * Accept an union operation request from a remote peer
1199 * @param m the accept message from the client
1200 * @param set the set of the client
1201 * @param incoming information about the requesting remote peer
1204 _GSS_union_accept (struct GNUNET_SET_AcceptRejectMessage *m, struct Set *set,
1205 struct Incoming *incoming)
1207 struct UnionEvaluateOperation *eo;
1209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "accepting set union operation\n");
1211 eo = GNUNET_new (struct UnionEvaluateOperation);
1212 eo->tc = incoming->tc;
1213 eo->generation_created = set->state.u->current_generation++;
1215 eo->salt = ntohs (incoming->salt);
1216 GNUNET_assert (0 != ntohl (m->request_id));
1217 eo->request_id = ntohl (m->request_id);
1218 eo->se = strata_estimator_dup (set->state.u->se);
1219 /* transfer ownership of mq and socket from incoming to eo */
1220 GNUNET_CONTAINER_DLL_insert (eo->set->state.u->ops_head,
1221 eo->set->state.u->ops_tail,
1223 /* kick off the operation */
1224 send_strata_estimator (eo);
1229 * Create a new set supporting the union operation
1231 * @return the newly created set
1234 _GSS_union_set_create (void)
1238 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "union set created\n");
1240 set = GNUNET_malloc (sizeof (struct Set) + sizeof (struct UnionState));
1241 set->state.u = (struct UnionState *) &set[1];
1242 set->operation = GNUNET_SET_OPERATION_UNION;
1243 /* keys of the hash map are stored in the element entrys, thus we do not
1244 * want the hash map to copy them */
1245 set->state.u->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1246 set->state.u->se = strata_estimator_create (SE_STRATA_COUNT,
1247 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1253 * Add the element from the given element message to the set.
1255 * @param m message with the element
1256 * @param set set to add the element to
1259 _GSS_union_add (struct GNUNET_SET_ElementMessage *m, struct Set *set)
1261 struct ElementEntry *ee;
1262 struct ElementEntry *ee_dup;
1263 uint16_t element_size;
1265 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adding element\n");
1267 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1268 element_size = ntohs (m->header.size) - sizeof *m;
1269 ee = GNUNET_malloc (element_size + sizeof *ee);
1270 ee->element.size = element_size;
1271 memcpy (&ee[1], &m[1], element_size);
1272 ee->element.data = &ee[1];
1273 ee->generation_added = set->state.u->current_generation;
1274 GNUNET_CRYPTO_hash (ee->element.data, element_size, &ee->element_hash);
1275 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->state.u->elements, &ee->element_hash);
1278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element inserted twice, ignoring\n");
1282 GNUNET_CONTAINER_multihashmap_put (set->state.u->elements, &ee->element_hash, ee,
1283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1284 strata_estimator_insert (set->state.u->se, get_ibf_key (&ee->element_hash, 0));
1289 * Destroy a set that supports the union operation
1291 * @param set the set to destroy, must be of type GNUNET_SET_OPERATION_UNION
1294 _GSS_union_set_destroy (struct Set *set)
1296 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1297 if (NULL != set->client)
1299 GNUNET_SERVER_client_drop (set->client);
1302 if (NULL != set->client_mq)
1304 GNUNET_MQ_destroy (set->client_mq);
1305 set->client_mq = NULL;
1308 if (NULL != set->state.u->se)
1310 strata_estimator_destroy (set->state.u->se);
1311 set->state.u->se = NULL;
1314 destroy_elements (set->state.u);
1316 while (NULL != set->state.u->ops_head)
1318 _GSS_union_operation_destroy (set->state.u->ops_head);
1323 * Remove the element given in the element message from the set.
1324 * Only marks the element as removed, so that older set operations can still exchange it.
1326 * @param m message with the element
1327 * @param set set to remove the element from
1330 _GSS_union_remove (struct GNUNET_SET_ElementMessage *m, struct Set *set)
1332 struct GNUNET_HashCode hash;
1333 struct ElementEntry *ee;
1335 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1336 GNUNET_CRYPTO_hash (&m[1], ntohs (m->header.size), &hash);
1337 ee = GNUNET_CONTAINER_multihashmap_get (set->state.u->elements, &hash);
1340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
1343 if (GNUNET_YES == ee->removed)
1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
1348 ee->removed = GNUNET_YES;
1349 ee->generation_removed = set->state.u->current_generation;
1354 * Dispatch messages for a union operation.
1356 * @param cls closure
1357 * @param tunnel mesh tunnel
1358 * @param tunnel_ctx tunnel context
1359 * @param mh message to process
1363 _GSS_union_handle_p2p_message (void *cls,
1364 struct GNUNET_MESH_Tunnel *tunnel,
1366 const struct GNUNET_MessageHeader *mh)
1368 struct TunnelContext *tc = *tunnel_ctx;
1369 struct UnionEvaluateOperation *eo;
1371 if (CONTEXT_OPERATION_UNION != tc->type)
1373 GNUNET_break_op (0);
1374 return GNUNET_SYSERR;
1379 switch (ntohs (mh->type))
1381 case GNUNET_MESSAGE_TYPE_SET_P2P_IBF:
1382 handle_p2p_ibf (eo, mh);
1384 case GNUNET_MESSAGE_TYPE_SET_P2P_SE:
1385 handle_p2p_strata_estimator (eo, mh);
1387 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1388 handle_p2p_elements (eo, mh);
1390 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1391 handle_p2p_element_requests (eo, mh);
1393 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
1394 handle_p2p_done (eo, mh);
1397 /* something wrong with mesh's message handlers? */