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 * Stream socket connected to the other peer
129 struct GNUNET_STREAM_Socket *socket;
132 * Message queue for the peer on the other
135 struct GNUNET_MQ_MessageQueue *mq;
138 * Request ID to multiplex set operations to
139 * the client inhabiting the set.
144 * Number of ibf buckets received
146 unsigned int ibf_buckets_received;
149 * Copy of the set's strata estimator at the time of
150 * creation of this operation
152 struct StrataEstimator *se;
155 * The ibf we currently receive
157 struct InvertibleBloomFilter *remote_ibf;
160 * IBF of the set's element.
162 struct InvertibleBloomFilter *local_ibf;
165 * Maps IBF-Keys (specific to the current salt) to elements.
167 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
170 * Current state of the operation.
172 enum UnionOperationPhase phase;
175 * Salt to use for this operation.
180 * Generation in which the operation handle
183 unsigned int generation_created;
186 * Evaluate operations are held in
189 struct UnionEvaluateOperation *next;
192 * Evaluate operations are held in
195 struct UnionEvaluateOperation *prev;
200 * Information about the element in a set.
201 * All elements are stored in a hash-table
202 * from their hash-code to their 'struct Element',
203 * so that the remove and add operations are reasonably
209 * The actual element. The data for the element
210 * should be allocated at the end of this struct.
212 struct GNUNET_SET_Element element;
215 * Hash of the element.
216 * Will be used to derive the different IBF keys
217 * for different salts.
219 struct GNUNET_HashCode element_hash;
222 * Generation the element was added by the client.
223 * Operations of earlier generations will not consider the element.
225 unsigned int generation_added;
228 * GNUNET_YES if the element has been removed in some generation.
233 * Generation the element was removed by the client.
234 * Operations of later generations will not consider the element.
235 * Only valid if is_removed is GNUNET_YES.
237 unsigned int generation_removed;
240 * GNUNET_YES if the element is a remote element, and does not belong
241 * to the operation's set.
248 * Information about the element used for
249 * a specific union operation.
254 * IBF key for the entry, derived from the current salt.
256 struct IBF_Key ibf_key;
259 * The actual element associated with the key
261 struct ElementEntry *element;
264 * Element that collides with this element
267 struct KeyEntry *next_colliding;
271 * Used as a closure for sending elements
272 * with a specific IBF key.
274 struct SendElementClosure
277 * The IBF key whose matching elements should be
280 struct IBF_Key ibf_key;
283 * Operation for which the elements
286 struct UnionEvaluateOperation *eo;
291 * Extra state required for efficient set union.
296 * The strata estimator is only generated once for
298 * The IBF keys are derived from the element hashes with
301 struct StrataEstimator *se;
304 * Maps 'struct GNUNET_HashCode' to 'struct ElementEntry'.
306 struct GNUNET_CONTAINER_MultiHashMap *elements;
309 * Evaluate operations are held in
312 struct UnionEvaluateOperation *ops_head;
315 * Evaluate operations are held in
318 struct UnionEvaluateOperation *ops_tail;
321 * Current generation, that is, number of
322 * previously executed operations on this set
324 unsigned int current_generation;
330 * Iterator over hash map entries.
333 * @param key current key code
334 * @param value value in the hash map
335 * @return GNUNET_YES if we should continue to
340 destroy_elements_iterator (void *cls,
341 const struct GNUNET_HashCode * key,
344 struct ElementEntry *ee = value;
352 * Destroy the elements belonging to a union set.
354 * @param us union state that contains the elements
357 destroy_elements (struct UnionState *us)
359 if (NULL == us->elements)
361 GNUNET_CONTAINER_multihashmap_iterate (us->elements, destroy_elements_iterator, NULL);
362 GNUNET_CONTAINER_multihashmap_destroy (us->elements);
369 * Iterator over hash map entries.
372 * @param key current key code
373 * @param value value in the hash map
374 * @return GNUNET_YES if we should continue to
379 destroy_key_to_element_iter (void *cls,
383 struct KeyEntry *k = value;
387 struct KeyEntry *k_tmp = k;
388 k = k->next_colliding;
396 * Destroy a union operation, and free all resources
397 * associated with it.
399 * @param eo the union operation to destroy
402 destroy_union_operation (struct UnionEvaluateOperation *eo)
406 GNUNET_MQ_destroy (eo->mq);
409 if (NULL != eo->socket)
411 GNUNET_STREAM_close (eo->socket);
414 if (NULL != eo->remote_ibf)
416 ibf_destroy (eo->remote_ibf);
417 eo->remote_ibf = NULL;
419 if (NULL != eo->local_ibf)
421 ibf_destroy (eo->local_ibf);
422 eo->local_ibf = NULL;
426 strata_estimator_destroy (eo->se);
429 if (NULL != eo->key_to_element)
431 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element, destroy_key_to_element_iter, NULL);
432 GNUNET_CONTAINER_multihashmap32_destroy (eo->key_to_element);
433 eo->key_to_element = NULL;
437 GNUNET_CONTAINER_DLL_remove (eo->set->state.u->ops_head,
438 eo->set->state.u->ops_tail,
441 /* FIXME: free and destroy everything else */
446 * Inform the client that the union operation has failed,
447 * and proceed to destroy the evaluate operation.
449 * @param eo the union operation to fail
452 fail_union_operation (struct UnionEvaluateOperation *eo)
454 struct GNUNET_MQ_Message *mqm;
455 struct ResultMessage *msg;
457 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
458 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
459 msg->request_id = htonl (eo->request_id);
460 GNUNET_MQ_send (eo->set->client_mq, mqm);
461 destroy_union_operation (eo);
466 * Derive the IBF key from a hash code and
469 * @param src the hash code
470 * @param salt salt to use
471 * @return the derived IBF key
473 static struct IBF_Key
474 get_ibf_key (struct GNUNET_HashCode *src, uint16_t salt)
478 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
479 GCRY_MD_SHA512, GCRY_MD_SHA256,
481 &salt, sizeof (salt),
488 * Send a request for the evaluate operation to a remote peer
490 * @param eo operation with the other peer
493 send_operation_request (struct UnionEvaluateOperation *eo)
495 struct GNUNET_MQ_Message *mqm;
496 struct OperationRequestMessage *msg;
498 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST);
499 if (NULL != eo->context_msg)
500 if (GNUNET_OK != GNUNET_MQ_nest (mqm, eo->context_msg, ntohs (eo->context_msg->size)))
502 /* the context message is too large */
504 GNUNET_SERVER_client_disconnect (eo->set->client);
505 GNUNET_MQ_discard (mqm);
508 msg->operation = htons (GNUNET_SET_OPERATION_UNION);
509 msg->app_id = eo->app_id;
510 GNUNET_MQ_send (eo->mq, mqm);
512 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sent op request\n");
517 * Iterator to create the mapping between ibf keys
518 * and element entries.
521 * @param key current key code
522 * @param value value in the hash map
523 * @return GNUNET_YES if we should continue to
528 insert_element_iterator (void *cls,
532 struct KeyEntry *const new_k = cls;
533 struct KeyEntry *old_k = value;
535 GNUNET_assert (NULL != old_k);
538 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
540 new_k->next_colliding = old_k;
541 old_k->next_colliding = new_k;
544 old_k = old_k->next_colliding;
545 } while (NULL != old_k);
551 * Insert an element into the union operation's
552 * key-to-element mapping
554 * @param the union operation
555 * @param ee the element entry
558 insert_element (struct UnionEvaluateOperation *eo, struct ElementEntry *ee)
561 struct IBF_Key ibf_key;
564 ibf_key = get_ibf_key (&ee->element_hash, eo->salt);
565 k = GNUNET_new (struct KeyEntry);
567 k->ibf_key = ibf_key;
568 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element,
569 (uint32_t) ibf_key.key_val,
570 insert_element_iterator, k);
571 /* was the element inserted into a colliding bucket? */
572 if (GNUNET_SYSERR == ret)
574 GNUNET_assert (NULL != k->next_colliding);
577 GNUNET_CONTAINER_multihashmap32_put (eo->key_to_element, (uint32_t) ibf_key.key_val, k,
578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
583 * Insert a key into an ibf.
587 * @param value the key entry to get the key from
590 prepare_ibf_iterator (void *cls,
594 struct InvertibleBloomFilter *ibf = cls;
595 struct KeyEntry *ke = value;
597 ibf_insert (ibf, ke->ibf_key);
603 * Iterator for initializing the
604 * key-to-element mapping of a union operation
606 * @param cls the union operation
608 * @param value the element entry to insert
609 * into the key-to-element mapping
612 init_key_to_element_iterator (void *cls,
613 const struct GNUNET_HashCode *key,
616 struct UnionEvaluateOperation *eo = cls;
617 struct ElementEntry *e = value;
619 /* make sure that the element belongs to the set at the time
620 * of creating the operation */
621 if ( (e->generation_added > eo->generation_created) ||
622 ( (GNUNET_YES == e->removed) &&
623 (e->generation_removed < eo->generation_created)))
626 insert_element (eo, e);
632 * Create an ibf with the operation's elements
633 * of the specified size
635 * @param eo the union operation
636 * @param size size of the ibf to create
639 prepare_ibf (struct UnionEvaluateOperation *eo, uint16_t size)
641 if (NULL == eo->key_to_element)
644 len = GNUNET_CONTAINER_multihashmap_size (eo->set->state.u->elements);
645 eo->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
646 GNUNET_CONTAINER_multihashmap_iterate (eo->set->state.u->elements,
647 init_key_to_element_iterator, eo);
649 if (NULL != eo->local_ibf)
650 ibf_destroy (eo->local_ibf);
651 eo->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
652 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element,
653 prepare_ibf_iterator, eo->local_ibf);
658 * Send an ibf of appropriate size.
660 * @param eo the union operation
661 * @param ibf_order order of the ibf to send, size=2^order
664 send_ibf (struct UnionEvaluateOperation *eo, uint16_t ibf_order)
666 unsigned int buckets_sent = 0;
667 struct InvertibleBloomFilter *ibf;
669 prepare_ibf (eo, 1<<ibf_order);
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending ibf of size %u\n", 1<<ibf_order);
675 while (buckets_sent < (1 << ibf_order))
677 unsigned int buckets_in_message;
678 struct GNUNET_MQ_Message *mqm;
679 struct IBFMessage *msg;
681 buckets_in_message = (1 << ibf_order) - buckets_sent;
682 /* limit to maximum */
683 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
684 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
686 mqm = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
687 GNUNET_MESSAGE_TYPE_SET_P2P_IBF);
688 msg->order = ibf_order;
689 msg->offset = htons (buckets_sent);
690 ibf_write_slice (ibf, buckets_sent,
691 buckets_in_message, &msg[1]);
692 buckets_sent += buckets_in_message;
693 GNUNET_MQ_send (eo->mq, mqm);
696 eo->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
701 * Send a strata estimator to the remote peer.
703 * @param eo the union operation with the remote peer
706 send_strata_estimator (struct UnionEvaluateOperation *eo)
708 struct GNUNET_MQ_Message *mqm;
709 struct GNUNET_MessageHeader *strata_msg;
711 mqm = GNUNET_MQ_msg_header_extra (strata_msg,
712 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
713 GNUNET_MESSAGE_TYPE_SET_P2P_SE);
714 strata_estimator_write (eo->set->state.u->se, &strata_msg[1]);
715 GNUNET_MQ_send (eo->mq, mqm);
716 eo->phase = PHASE_EXPECT_IBF;
721 * Compute the necessary order of an ibf
722 * from the size of the symmetric set difference.
724 * @param diff the difference
725 * @return the required size of the ibf
728 get_order_from_difference (unsigned int diff)
730 unsigned int ibf_order;
733 while ((1<<ibf_order) < (2 * diff))
735 if (ibf_order > MAX_IBF_ORDER)
736 ibf_order = MAX_IBF_ORDER;
742 * Handle a strata estimator from a remote peer
744 * @param the union operation
745 * @param mh the message
748 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
750 struct UnionEvaluateOperation *eo = cls;
751 struct StrataEstimator *remote_se;
755 if (eo->phase != PHASE_EXPECT_SE)
757 fail_union_operation (eo);
761 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
763 strata_estimator_read (&mh[1], remote_se);
764 GNUNET_assert (NULL != eo->se);
765 diff = strata_estimator_difference (remote_se, eo->se);
766 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got se, diff=%d\n", diff);
767 strata_estimator_destroy (remote_se);
768 strata_estimator_destroy (eo->se);
770 send_ibf (eo, get_order_from_difference (diff));
776 * Iterator to send elements to a remote peer
778 * @param cls closure with the element key and the union operation
780 * @param value the key entry
783 send_element_iterator (void *cls,
787 struct SendElementClosure *sec = cls;
788 struct IBF_Key ibf_key = sec->ibf_key;
789 struct UnionEvaluateOperation *eo = sec->eo;
790 struct KeyEntry *ke = value;
792 if (ke->ibf_key.key_val != ibf_key.key_val)
796 const struct GNUNET_SET_Element *const element = &ke->element->element;
797 struct GNUNET_MQ_Message *mqm;
799 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
800 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
801 if (GNUNET_OK != GNUNET_MQ_nest (mqm, element->data, element->size))
804 GNUNET_MQ_discard (mqm);
807 GNUNET_MQ_send (eo->mq, mqm);
808 ke = ke->next_colliding;
814 * Send all elements that have the specified IBF key
815 * to the remote peer of the union operation
817 * @param eo union operation
818 * @param ibf_key IBF key of interest
821 send_elements_for_key (struct UnionEvaluateOperation *eo, struct IBF_Key ibf_key)
823 struct SendElementClosure send_cls;
825 send_cls.ibf_key = ibf_key;
827 GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element, (uint32_t) ibf_key.key_val,
828 &send_element_iterator, &send_cls);
833 * Decode which elements are missing on each side, and
834 * send the appropriate elemens and requests
836 * @param eo union operation
839 decode_and_send (struct UnionEvaluateOperation *eo)
843 struct InvertibleBloomFilter *diff_ibf;
845 GNUNET_assert (PHASE_EXPECT_ELEMENTS == eo->phase);
847 prepare_ibf (eo, eo->remote_ibf->size);
848 diff_ibf = ibf_dup (eo->local_ibf);
849 ibf_subtract (diff_ibf, eo->remote_ibf);
855 res = ibf_decode (diff_ibf, &side, &key);
856 if (GNUNET_SYSERR == res)
860 while (1<<next_order < diff_ibf->size)
863 if (next_order <= MAX_IBF_ORDER)
865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
866 "decoding failed, sending larger ibf (size %u)\n",
868 send_ibf (eo, next_order);
872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
873 "set union failed: reached ibf limit\n");
877 if (GNUNET_NO == res)
879 struct GNUNET_MQ_Message *mqm;
881 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "transmitted all values, sending DONE\n");
882 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
883 GNUNET_MQ_send (eo->mq, mqm);
888 send_elements_for_key (eo, key);
892 struct GNUNET_MQ_Message *mqm;
893 struct GNUNET_MessageHeader *msg;
895 /* FIXME: before sending the request, check if we may just have the element */
896 /* FIXME: merge multiple requests */
897 mqm = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
898 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
899 *(struct IBF_Key *) &msg[1] = key;
900 GNUNET_MQ_send (eo->mq, mqm);
903 ibf_destroy (diff_ibf);
908 * Handle an IBF message from a remote peer.
910 * @param cls the union operation
911 * @param mh the header of the message
914 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
916 struct UnionEvaluateOperation *eo = cls;
917 struct IBFMessage *msg = (struct IBFMessage *) mh;
918 unsigned int buckets_in_message;
920 if ( (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
921 (eo->phase == PHASE_EXPECT_IBF) )
923 eo->phase = PHASE_EXPECT_IBF_CONT;
924 GNUNET_assert (NULL == eo->remote_ibf);
925 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "creating new ibf of order %u\n", 1<<msg->order);
926 eo->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
927 if (0 != ntohs (msg->offset))
930 fail_union_operation (eo);
933 else if (eo->phase == PHASE_EXPECT_IBF_CONT)
935 if ( (ntohs (msg->offset) != eo->ibf_buckets_received) ||
936 (1<<msg->order != eo->remote_ibf->size) )
939 fail_union_operation (eo);
944 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
946 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
949 fail_union_operation (eo);
953 ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
954 eo->ibf_buckets_received += buckets_in_message;
956 if (eo->ibf_buckets_received == eo->remote_ibf->size)
959 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received full strata estimator\n");
960 eo->phase = PHASE_EXPECT_ELEMENTS;
961 decode_and_send (eo);
967 * Send a result message to the client indicating
968 * that there is a new element.
970 * @param eo union operation
971 * @param element element to send
974 send_client_element (struct UnionEvaluateOperation *eo,
975 struct GNUNET_SET_Element *element)
977 struct GNUNET_MQ_Message *mqm;
978 struct ResultMessage *rm;
980 GNUNET_assert (0 != eo->request_id);
981 mqm = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
982 rm->result_status = htons (GNUNET_SET_STATUS_OK);
983 rm->request_id = htonl (eo->request_id);
984 if (GNUNET_OK != GNUNET_MQ_nest (mqm, element->data, element->size))
986 GNUNET_MQ_discard (mqm);
991 GNUNET_MQ_send (eo->set->client_mq, mqm);
996 * Callback used for notifications
1001 client_done_sent_cb (void *cls)
1003 //struct UnionEvaluateOperation *eo = cls;
1004 /* FIXME: destroy eo */
1009 * Send a result message to the client indicating
1010 * that the operation is over.
1011 * After the result done message has been sent to the client,
1012 * destroy the evaluate operation.
1014 * @param eo union operation
1015 * @param element element to send
1018 send_client_done_and_destroy (struct UnionEvaluateOperation *eo)
1020 struct GNUNET_MQ_Message *mqm;
1021 struct ResultMessage *rm;
1023 GNUNET_assert (0 != eo->request_id);
1024 mqm = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
1025 rm->request_id = htonl (eo->request_id);
1026 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
1027 GNUNET_MQ_notify_sent (mqm, client_done_sent_cb, eo);
1028 GNUNET_MQ_send (eo->set->client_mq, mqm);
1030 /* FIXME: destroy the eo */
1035 * Handle an element message from a remote peer.
1037 * @param cls the union operation
1038 * @param mh the message
1041 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
1043 struct UnionEvaluateOperation *eo = cls;
1044 struct ElementEntry *ee;
1045 uint16_t element_size;
1047 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got element from peer\n");
1049 if ( (eo->phase != PHASE_EXPECT_ELEMENTS) &&
1050 (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1052 fail_union_operation (eo);
1056 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1057 ee = GNUNET_malloc (sizeof *eo + element_size);
1058 memcpy (&ee[1], &mh[1], element_size);
1059 ee->element.data = &ee[1];
1060 ee->remote = GNUNET_YES;
1062 insert_element (eo, ee);
1063 send_client_element (eo, &ee->element);
1070 * Handle an element request from a remote peer.
1072 * @param cls the union operation
1073 * @param mh the message
1076 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1078 struct UnionEvaluateOperation *eo = cls;
1079 struct IBF_Key *ibf_key;
1080 unsigned int num_keys;
1082 /* look up elements and send them */
1083 if (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1086 fail_union_operation (eo);
1090 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1092 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1095 fail_union_operation (eo);
1099 ibf_key = (struct IBF_Key *) &mh[1];
1100 while (0 != num_keys--)
1102 send_elements_for_key (eo, *ibf_key);
1109 * Callback used for notifications
1111 * @param cls closure
1114 peer_done_sent_cb (void *cls)
1116 struct UnionEvaluateOperation *eo = cls;
1118 send_client_done_and_destroy (eo);
1123 * Handle a done message from a remote peer
1125 * @param cls the union operation
1126 * @param mh the message
1129 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1131 struct UnionEvaluateOperation *eo = cls;
1133 if (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1135 /* we got all requests, but still have to send our elements as response */
1136 struct GNUNET_MQ_Message *mqm;
1138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got DONE, sending final DONE after elements\n");
1139 eo->phase = PHASE_FINISHED;
1140 mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1141 GNUNET_MQ_notify_sent (mqm, peer_done_sent_cb, eo);
1142 GNUNET_MQ_send (eo->mq, mqm);
1145 if (eo->phase == PHASE_EXPECT_ELEMENTS)
1147 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got final DONE\n");
1148 eo->phase = PHASE_FINISHED;
1149 send_client_done_and_destroy (eo);
1153 fail_union_operation (eo);
1158 * The handlers array, used for both evaluate and accept
1160 static const struct GNUNET_MQ_Handler union_handlers[] = {
1161 {handle_p2p_elements, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS},
1162 {handle_p2p_strata_estimator, GNUNET_MESSAGE_TYPE_SET_P2P_SE},
1163 {handle_p2p_ibf, GNUNET_MESSAGE_TYPE_SET_P2P_IBF},
1164 {handle_p2p_element_requests, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS},
1165 {handle_p2p_done, GNUNET_MESSAGE_TYPE_SET_P2P_DONE},
1166 GNUNET_MQ_HANDLERS_END
1171 * Functions of this type will be called when a stream is established
1173 * @param cls the closure from GNUNET_STREAM_open
1174 * @param socket socket to use to communicate with the
1175 * other side (read/write)
1178 stream_open_cb (void *cls,
1179 struct GNUNET_STREAM_Socket *socket)
1181 struct UnionEvaluateOperation *eo = cls;
1183 GNUNET_assert (NULL == eo->mq);
1184 GNUNET_assert (socket == eo->socket);
1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 "open cb successful\n");
1187 eo->mq = GNUNET_STREAM_mq_create (eo->socket, union_handlers, NULL, eo);
1188 /* we started the operation, thus we have to send the operation request */
1189 send_operation_request (eo);
1190 eo->phase = PHASE_EXPECT_SE;
1195 * Evaluate a union operation with
1198 * @param m the evaluate request message from the client
1199 * @parem set the set to evaluate the operation with
1202 _GSS_union_evaluate (struct EvaluateMessage *m, struct Set *set)
1204 struct UnionEvaluateOperation *eo;
1206 eo = GNUNET_new (struct UnionEvaluateOperation);
1209 eo->request_id = htonl (m->request_id);
1210 GNUNET_assert (0 != eo->request_id);
1211 eo->se = strata_estimator_dup (set->state.u->se);
1212 eo->salt = ntohs (m->salt);
1213 eo->app_id = m->app_id;
1215 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1216 "evaluating union operation, (app %s)\n",
1217 GNUNET_h2s (&eo->app_id));
1220 GNUNET_STREAM_open (configuration, &eo->peer, GNUNET_APPLICATION_TYPE_SET,
1221 &stream_open_cb, eo,
1222 GNUNET_STREAM_OPTION_END);
1223 GNUNET_CONTAINER_DLL_insert (eo->set->state.u->ops_head,
1224 eo->set->state.u->ops_tail,
1226 /* the stream open callback will kick off the operation */
1231 * Accept an union operation request from a remote peer
1233 * @param m the accept message from the client
1234 * @param set the set of the client
1235 * @param incoming information about the requesting remote peer
1238 _GSS_union_accept (struct AcceptMessage *m, struct Set *set,
1239 struct Incoming *incoming)
1241 struct UnionEvaluateOperation *eo;
1243 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "accepting set union operation\n");
1245 eo = GNUNET_new (struct UnionEvaluateOperation);
1246 eo->generation_created = set->state.u->current_generation++;
1248 eo->peer = incoming->peer;
1249 eo->salt = ntohs (incoming->salt);
1250 GNUNET_assert (0 != ntohl (m->request_id));
1251 eo->request_id = ntohl (m->request_id);
1252 eo->se = strata_estimator_dup (set->state.u->se);
1253 eo->set = set; // FIXME: redundant!?
1254 eo->mq = incoming->mq;
1255 /* transfer ownership of mq and socket from incoming to eo */
1256 incoming->mq = NULL;
1257 eo->socket = incoming->socket;
1258 incoming->socket = NULL;
1259 /* the peer's socket is now ours, we'll receive all messages */
1260 GNUNET_MQ_replace_handlers (eo->mq, union_handlers, eo);
1262 GNUNET_CONTAINER_DLL_insert (eo->set->state.u->ops_head,
1263 eo->set->state.u->ops_tail,
1266 /* kick off the operation */
1267 send_strata_estimator (eo);
1272 * Create a new set supporting the union operation
1274 * @return the newly created set
1277 _GSS_union_set_create (void)
1281 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "union set created\n");
1283 set = GNUNET_malloc (sizeof (struct Set) + sizeof (struct UnionState));
1284 set->state.u = (struct UnionState *) &set[1];
1285 set->operation = GNUNET_SET_OPERATION_UNION;
1286 /* keys of the hash map are stored in the element entrys, thus we do not
1287 * want the hash map to copy them */
1288 set->state.u->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1289 set->state.u->se = strata_estimator_create (SE_STRATA_COUNT,
1290 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1296 * Add the element from the given element message to the set.
1298 * @param m message with the element
1299 * @param set set to add the element to
1302 _GSS_union_add (struct ElementMessage *m, struct Set *set)
1304 struct ElementEntry *ee;
1305 struct ElementEntry *ee_dup;
1306 uint16_t element_size;
1308 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adding element\n");
1310 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1311 element_size = ntohs (m->header.size) - sizeof *m;
1312 ee = GNUNET_malloc (element_size + sizeof *ee);
1313 ee->element.size = element_size;
1314 memcpy (&ee[1], &m[1], element_size);
1315 ee->element.data = &ee[1];
1316 ee->generation_added = set->state.u->current_generation;
1317 GNUNET_CRYPTO_hash (ee->element.data, element_size, &ee->element_hash);
1318 ee_dup = GNUNET_CONTAINER_multihashmap_get (set->state.u->elements, &ee->element_hash);
1321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element inserted twice, ignoring\n");
1325 GNUNET_CONTAINER_multihashmap_put (set->state.u->elements, &ee->element_hash, ee,
1326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1327 strata_estimator_insert (set->state.u->se, get_ibf_key (&ee->element_hash, 0));
1332 * Destroy a set that supports the union operation
1334 * @param the set to destroy, must be of type GNUNET_SET_OPERATION_UNION
1337 _GSS_union_set_destroy (struct Set *set)
1339 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1340 if (NULL != set->client)
1342 GNUNET_SERVER_client_drop (set->client);
1345 if (NULL != set->client_mq)
1347 GNUNET_MQ_destroy (set->client_mq);
1348 set->client_mq = NULL;
1351 if (NULL != set->state.u->se)
1353 strata_estimator_destroy (set->state.u->se);
1354 set->state.u->se = NULL;
1357 destroy_elements (set->state.u);
1359 while (NULL != set->state.u->ops_head)
1360 destroy_union_operation (set->state.u->ops_head);
1364 * Remove the element given in the element message from the set.
1365 * Only marks the element as removed, so that older set operations can still exchange it.
1367 * @param m message with the element
1368 * @param set set to remove the element from
1371 _GSS_union_remove (struct ElementMessage *m, struct Set *set)
1373 struct GNUNET_HashCode hash;
1374 struct ElementEntry *ee;
1376 GNUNET_assert (GNUNET_SET_OPERATION_UNION == set->operation);
1377 GNUNET_CRYPTO_hash (&m[1], ntohs (m->header.size), &hash);
1378 ee = GNUNET_CONTAINER_multihashmap_get (set->state.u->elements, &hash);
1381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
1384 if (GNUNET_YES == ee->removed)
1386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
1389 ee->removed = GNUNET_YES;
1390 ee->generation_removed = set->state.u->current_generation;