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 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.
21 * @file set/gnunet-service-set_union.c
22 * @brief two-peer set operations
23 * @author Florian Dold
26 #include "gnunet_util_lib.h"
27 #include "gnunet-service-set.h"
29 #include "gnunet-service-set_union_strata_estimator.h"
30 #include "gnunet-service-set_protocol.h"
35 * Number of IBFs in a strata estimator.
37 #define SE_STRATA_COUNT 32
39 * Size of the IBFs in the strata estimator.
41 #define SE_IBF_SIZE 80
43 * hash num parameter for the difference digests and strata estimators
45 #define SE_IBF_HASH_NUM 4
48 * Number of buckets that can be transmitted in one message.
50 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
53 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
54 * Choose this value so that computing the IBF is still cheaper
55 * than transmitting all values.
57 #define MAX_IBF_ORDER (16)
60 * Number of buckets used in the ibf per estimated
67 * Current phase we are in for a union operation.
69 enum UnionOperationPhase
72 * We sent the request message, and expect a strata estimator
77 * We sent the strata estimator, and expect an IBF. This phase is entered once
78 * upon initialization and later via #PHASE_EXPECT_ELEMENTS_AND_REQUESTS.
80 * After receiving the complete IBF, we enter #PHASE_EXPECT_ELEMENTS
85 * Continuation for multi part IBFs.
87 PHASE_EXPECT_IBF_CONT,
90 * We are sending request and elements,
91 * and thus only expect elements from the other peer.
93 * We are currently decoding an IBF until it can no longer be decoded,
94 * we currently send requests and expect elements
95 * The remote peer is in #PHASE_EXPECT_ELEMENTS_AND_REQUESTS
97 PHASE_EXPECT_ELEMENTS,
100 * We are expecting elements and requests, and send
101 * requested elements back to the other peer.
103 * We are in this phase if we have SENT an IBF for the remote peer to decode.
104 * We expect requests, send elements or could receive an new IBF, which takes
105 * us via #PHASE_EXPECT_IBF to phase #PHASE_EXPECT_ELEMENTS
107 * The remote peer is thus in:
108 * #PHASE_EXPECT_ELEMENTS
110 PHASE_EXPECT_ELEMENTS_AND_REQUESTS,
113 * The protocol is over.
114 * Results may still have to be sent to the client.
121 * State of an evaluate operation with another peer.
123 struct OperationState
127 * Copy of the set's strata estimator at the time of
128 * creation of this operation
130 struct StrataEstimator *se;
133 * The ibf we currently receive
135 struct InvertibleBloomFilter *remote_ibf;
138 * IBF of the set's element.
140 struct InvertibleBloomFilter *local_ibf;
143 * Maps IBF-Keys (specific to the current salt) to elements.
144 * Used as a multihashmap, the keys being the lower 32bit of the IBF-Key.
145 * Colliding IBF-Keys are linked.
147 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
150 * Iterator for sending elements on the key to element mapping to the client.
152 struct GNUNET_CONTAINER_MultiHashMap32Iterator *full_result_iter;
155 * Current state of the operation.
157 enum UnionOperationPhase phase;
160 * Did we send the client that we are done?
162 int client_done_sent;
165 * Number of ibf buckets received
167 unsigned int ibf_buckets_received;
173 * The key entry is used to associate an ibf key with an element.
178 * IBF key for the entry, derived from the current salt.
180 struct IBF_Key ibf_key;
183 * The actual element associated with the key.
185 struct ElementEntry *element;
188 * Element that collides with this element
189 * on the ibf key. All colliding entries must have the same ibf key.
191 struct KeyEntry *next_colliding;
196 * Used as a closure for sending elements
197 * with a specific IBF key.
199 struct SendElementClosure
202 * The IBF key whose matching elements should be
205 struct IBF_Key ibf_key;
208 * Operation for which the elements
211 struct Operation *op;
216 * Extra state required for efficient set union.
221 * The strata estimator is only generated once for
223 * The IBF keys are derived from the element hashes with
226 struct StrataEstimator *se;
231 * Iterator over hash map entries, called to
232 * destroy the linked list of colliding ibf key entries.
235 * @param key current key code
236 * @param value value in the hash map
237 * @return #GNUNET_YES if we should continue to
242 destroy_key_to_element_iter (void *cls,
246 struct KeyEntry *k = value;
250 struct KeyEntry *k_tmp = k;
252 k = k->next_colliding;
253 if (GNUNET_YES == k_tmp->element->remote)
255 GNUNET_free (k_tmp->element);
256 k_tmp->element = NULL;
265 * Destroy the union operation. Only things specific to the union
266 * operation are destroyed.
268 * @param op union operation to destroy
271 union_op_cancel (struct Operation *op)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "destroying union op\n");
275 /* check if the op was canceled twice */
276 GNUNET_assert (NULL != op->state);
277 if (NULL != op->state->remote_ibf)
279 ibf_destroy (op->state->remote_ibf);
280 op->state->remote_ibf = NULL;
282 if (NULL != op->state->local_ibf)
284 ibf_destroy (op->state->local_ibf);
285 op->state->local_ibf = NULL;
287 if (NULL != op->state->se)
289 strata_estimator_destroy (op->state->se);
290 op->state->se = NULL;
292 if (NULL != op->state->key_to_element)
294 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
295 &destroy_key_to_element_iter,
297 GNUNET_CONTAINER_multihashmap32_destroy (op->state->key_to_element);
298 op->state->key_to_element = NULL;
300 GNUNET_free (op->state);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "destroying union op done\n");
308 * Inform the client that the union operation has failed,
309 * and proceed to destroy the evaluate operation.
311 * @param op the union operation to fail
314 fail_union_operation (struct Operation *op)
316 struct GNUNET_MQ_Envelope *ev;
317 struct GNUNET_SET_ResultMessage *msg;
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "union operation failed\n");
321 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
322 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
323 msg->request_id = htonl (op->spec->client_request_id);
324 msg->element_type = htons (0);
325 GNUNET_MQ_send (op->spec->set->client_mq, ev);
326 _GSS_operation_destroy (op, GNUNET_YES);
331 * Derive the IBF key from a hash code and
334 * @param src the hash code
335 * @param salt salt to use
336 * @return the derived IBF key
338 static struct IBF_Key
339 get_ibf_key (const struct GNUNET_HashCode *src,
344 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
345 GCRY_MD_SHA512, GCRY_MD_SHA256,
347 &salt, sizeof (salt),
354 * Iterator to create the mapping between ibf keys
355 * and element entries.
358 * @param key current key code
359 * @param value value in the hash map
360 * @return #GNUNET_YES if we should continue to iterate,
364 op_register_element_iterator (void *cls,
368 struct KeyEntry *const new_k = cls;
369 struct KeyEntry *old_k = value;
371 GNUNET_assert (NULL != old_k);
372 /* check if our ibf key collides with the ibf key in the existing entry */
373 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
375 /* insert the the new key in the collision chain */
376 new_k->next_colliding = old_k->next_colliding;
377 old_k->next_colliding = new_k;
378 /* signal to the caller that we were able to insert into a colliding bucket */
386 * Iterator to create the mapping between ibf keys
387 * and element entries.
390 * @param key current key code
391 * @param value value in the hash map
392 * @return #GNUNET_YES (we should continue to iterate)
395 op_has_element_iterator (void *cls,
399 struct GNUNET_HashCode *element_hash = cls;
400 struct KeyEntry *k = value;
402 GNUNET_assert (NULL != k);
405 if (0 == GNUNET_CRYPTO_hash_cmp (&k->element->element_hash,
408 k = k->next_colliding;
415 * Determine whether the given element is already in the operation's element
418 * @param op operation that should be tested for 'element_hash'
419 * @param element_hash hash of the element to look for
420 * @return #GNUNET_YES if the element has been found, #GNUNET_NO otherwise
423 op_has_element (struct Operation *op,
424 const struct GNUNET_HashCode *element_hash)
427 struct IBF_Key ibf_key;
429 ibf_key = get_ibf_key (element_hash, op->spec->salt);
430 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
431 (uint32_t) ibf_key.key_val,
432 op_has_element_iterator,
433 (void *) element_hash);
435 /* was the iteration aborted because we found the element? */
436 if (GNUNET_SYSERR == ret)
443 * Insert an element into the union operation's
444 * key-to-element mapping. Takes ownership of 'ee'.
445 * Note that this does not insert the element in the set,
446 * only in the operation's key-element mapping.
447 * This is done to speed up re-tried operations, if some elements
448 * were transmitted, and then the IBF fails to decode.
450 * @param op the union operation
451 * @param ee the element entry
454 op_register_element (struct Operation *op,
455 struct ElementEntry *ee)
458 struct IBF_Key ibf_key;
461 ibf_key = get_ibf_key (&ee->element_hash, op->spec->salt);
462 k = GNUNET_new (struct KeyEntry);
464 k->ibf_key = ibf_key;
465 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
466 (uint32_t) ibf_key.key_val,
467 op_register_element_iterator,
470 /* was the element inserted into a colliding bucket? */
471 if (GNUNET_SYSERR == ret)
473 GNUNET_CONTAINER_multihashmap32_put (op->state->key_to_element,
474 (uint32_t) ibf_key.key_val,
476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
481 * Insert a key into an ibf.
485 * @param value the key entry to get the key from
488 prepare_ibf_iterator (void *cls,
492 struct InvertibleBloomFilter *ibf = cls;
493 struct KeyEntry *ke = value;
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "inserting %x into ibf\n",
497 ke->ibf_key.key_val);
498 ibf_insert (ibf, ke->ibf_key);
504 * Iterator for initializing the
505 * key-to-element mapping of a union operation
507 * @param cls the union operation `struct Operation *`
509 * @param value the `struct ElementEntry *` to insert
510 * into the key-to-element mapping
511 * @return #GNUNET_YES (to continue iterating)
514 init_key_to_element_iterator (void *cls,
515 const struct GNUNET_HashCode *key,
518 struct Operation *op = cls;
519 struct ElementEntry *e = value;
521 /* make sure that the element belongs to the set at the time
522 * of creating the operation */
523 if ( (e->generation_added > op->generation_created) ||
524 ( (GNUNET_YES == e->removed) &&
525 (e->generation_removed < op->generation_created)))
528 GNUNET_assert (GNUNET_NO == e->remote);
530 op_register_element (op, e);
536 * Create an ibf with the operation's elements
537 * of the specified size
539 * @param op the union operation
540 * @param size size of the ibf to create
543 prepare_ibf (struct Operation *op,
546 if (NULL == op->state->key_to_element)
549 len = GNUNET_CONTAINER_multihashmap_size (op->spec->set->elements);
550 op->state->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
551 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
552 init_key_to_element_iterator, op);
554 if (NULL != op->state->local_ibf)
555 ibf_destroy (op->state->local_ibf);
556 op->state->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
557 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
558 &prepare_ibf_iterator,
559 op->state->local_ibf);
564 * Send an ibf of appropriate size.
566 * @param op the union operation
567 * @param ibf_order order of the ibf to send, size=2^order
570 send_ibf (struct Operation *op,
573 unsigned int buckets_sent = 0;
574 struct InvertibleBloomFilter *ibf;
576 prepare_ibf (op, 1<<ibf_order);
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "sending ibf of size %u\n",
582 ibf = op->state->local_ibf;
584 while (buckets_sent < (1 << ibf_order))
586 unsigned int buckets_in_message;
587 struct GNUNET_MQ_Envelope *ev;
588 struct IBFMessage *msg;
590 buckets_in_message = (1 << ibf_order) - buckets_sent;
591 /* limit to maximum */
592 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
593 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
595 ev = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
596 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF);
598 msg->order = ibf_order;
599 msg->offset = htons (buckets_sent);
600 ibf_write_slice (ibf, buckets_sent,
601 buckets_in_message, &msg[1]);
602 buckets_sent += buckets_in_message;
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "ibf chunk size %u, %u/%u sent\n",
608 GNUNET_MQ_send (op->mq, ev);
611 op->state->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
616 * Send a strata estimator to the remote peer.
618 * @param op the union operation with the remote peer
621 send_strata_estimator (struct Operation *op)
623 struct GNUNET_MQ_Envelope *ev;
624 struct GNUNET_MessageHeader *strata_msg;
626 ev = GNUNET_MQ_msg_header_extra (strata_msg,
627 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
628 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE);
629 strata_estimator_write (op->state->se, &strata_msg[1]);
630 GNUNET_MQ_send (op->mq, ev);
631 op->state->phase = PHASE_EXPECT_IBF;
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633 "sent SE, expecting IBF\n");
638 * Compute the necessary order of an ibf
639 * from the size of the symmetric set difference.
641 * @param diff the difference
642 * @return the required size of the ibf
645 get_order_from_difference (unsigned int diff)
647 unsigned int ibf_order;
650 while ( (1<<ibf_order) < (IBF_ALPHA * diff) ||
651 ((1<<ibf_order) < SE_IBF_HASH_NUM) )
653 if (ibf_order > MAX_IBF_ORDER)
654 ibf_order = MAX_IBF_ORDER;
660 * Handle a strata estimator from a remote peer
662 * @param cls the union operation
663 * @param mh the message
664 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
665 * #GNUNET_OK otherwise
668 handle_p2p_strata_estimator (void *cls,
669 const struct GNUNET_MessageHeader *mh)
671 struct Operation *op = cls;
672 struct StrataEstimator *remote_se;
675 if (op->state->phase != PHASE_EXPECT_SE)
677 fail_union_operation (op);
679 return GNUNET_SYSERR;
681 if (ntohs (mh->size) !=
682 SE_STRATA_COUNT * SE_IBF_SIZE * IBF_BUCKET_SIZE +
683 sizeof (struct GNUNET_MessageHeader))
685 fail_union_operation (op);
687 return GNUNET_SYSERR;
689 remote_se = strata_estimator_create (SE_STRATA_COUNT,
692 strata_estimator_read (&mh[1], remote_se);
693 GNUNET_assert (NULL != op->state->se);
694 diff = strata_estimator_difference (remote_se,
696 strata_estimator_destroy (remote_se);
697 strata_estimator_destroy (op->state->se);
698 op->state->se = NULL;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "got se diff=%d, using ibf size %d\n",
702 1<<get_order_from_difference (diff));
704 get_order_from_difference (diff));
710 * Iterator to send elements to a remote peer
712 * @param cls closure with the element key and the union operation
714 * @param value the key entry
717 send_element_iterator (void *cls,
721 struct SendElementClosure *sec = cls;
722 struct IBF_Key ibf_key = sec->ibf_key;
723 struct Operation *op = sec->op;
724 struct KeyEntry *ke = value;
726 if (ke->ibf_key.key_val != ibf_key.key_val)
730 const struct GNUNET_SET_Element *const element = &ke->element->element;
731 struct GNUNET_MQ_Envelope *ev;
732 struct GNUNET_MessageHeader *mh;
734 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
735 ev = GNUNET_MQ_msg_header_extra (mh,
737 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
740 /* element too large */
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "sending element (%s) to peer\n",
749 GNUNET_h2s (&ke->element->element_hash));
750 GNUNET_MQ_send (op->mq, ev);
751 ke = ke->next_colliding;
758 * Send all elements that have the specified IBF key
759 * to the remote peer of the union operation
761 * @param op union operation
762 * @param ibf_key IBF key of interest
765 send_elements_for_key (struct Operation *op,
766 struct IBF_Key ibf_key)
768 struct SendElementClosure send_cls;
770 send_cls.ibf_key = ibf_key;
772 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
773 (uint32_t) ibf_key.key_val,
774 &send_element_iterator,
780 * Decode which elements are missing on each side, and
781 * send the appropriate elemens and requests
783 * @param op union operation
786 decode_and_send (struct Operation *op)
789 struct IBF_Key last_key;
791 unsigned int num_decoded;
792 struct InvertibleBloomFilter *diff_ibf;
794 GNUNET_assert (PHASE_EXPECT_ELEMENTS == op->state->phase);
796 prepare_ibf (op, op->state->remote_ibf->size);
797 diff_ibf = ibf_dup (op->state->local_ibf);
798 ibf_subtract (diff_ibf, op->state->remote_ibf);
800 ibf_destroy (op->state->remote_ibf);
801 op->state->remote_ibf = NULL;
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804 "decoding IBF (size=%u)\n",
808 last_key.key_val = 0;
813 int cycle_detected = GNUNET_NO;
817 res = ibf_decode (diff_ibf, &side, &key);
818 if (res == GNUNET_OK)
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "decoded ibf key %lx\n",
824 if ( (num_decoded > diff_ibf->size) ||
825 (num_decoded > 1 && last_key.key_val == key.key_val) )
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "detected cyclic ibf (decoded %u/%u)\n",
831 cycle_detected = GNUNET_YES;
834 if ( (GNUNET_SYSERR == res) ||
835 (GNUNET_YES == cycle_detected) )
839 while (1<<next_order < diff_ibf->size)
842 if (next_order <= MAX_IBF_ORDER)
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "decoding failed, sending larger ibf (size %u)\n",
847 send_ibf (op, next_order);
851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
852 "set union failed: reached ibf limit\n");
856 if (GNUNET_NO == res)
858 struct GNUNET_MQ_Envelope *ev;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "transmitted all values, sending DONE\n");
862 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE);
863 GNUNET_MQ_send (op->mq, ev);
868 send_elements_for_key (op, key);
872 struct GNUNET_MQ_Envelope *ev;
873 struct GNUNET_MessageHeader *msg;
875 /* It may be nice to merge multiple requests, but with cadet's corking it is not worth
876 * the effort additional complexity. */
877 ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
878 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
880 *(struct IBF_Key *) &msg[1] = key;
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
882 "sending element request\n");
883 GNUNET_MQ_send (op->mq, ev);
890 ibf_destroy (diff_ibf);
895 * Handle an IBF message from a remote peer.
897 * @param cls the union operation
898 * @param mh the header of the message
899 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
900 * #GNUNET_OK otherwise
903 handle_p2p_ibf (void *cls,
904 const struct GNUNET_MessageHeader *mh)
906 struct Operation *op = cls;
907 const struct IBFMessage *msg;
908 unsigned int buckets_in_message;
910 if (ntohs (mh->size) < sizeof (struct IBFMessage))
913 fail_union_operation (op);
914 return GNUNET_SYSERR;
916 msg = (const struct IBFMessage *) mh;
917 if ( (op->state->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
918 (op->state->phase == PHASE_EXPECT_IBF) )
920 op->state->phase = PHASE_EXPECT_IBF_CONT;
921 GNUNET_assert (NULL == op->state->remote_ibf);
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Creating new ibf of size %u\n",
925 op->state->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
926 op->state->ibf_buckets_received = 0;
927 if (0 != ntohs (msg->offset))
930 fail_union_operation (op);
931 return GNUNET_SYSERR;
934 else if (op->state->phase == PHASE_EXPECT_IBF_CONT)
936 if ( (ntohs (msg->offset) != op->state->ibf_buckets_received) ||
937 (1<<msg->order != op->state->remote_ibf->size) )
940 fail_union_operation (op);
941 return GNUNET_SYSERR;
945 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
947 if (0 == buckets_in_message)
950 fail_union_operation (op);
951 return GNUNET_SYSERR;
954 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
957 fail_union_operation (op);
958 return GNUNET_SYSERR;
961 ibf_read_slice (&msg[1],
962 op->state->ibf_buckets_received,
964 op->state->remote_ibf);
965 op->state->ibf_buckets_received += buckets_in_message;
967 if (op->state->ibf_buckets_received == op->state->remote_ibf->size)
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "received full ibf\n");
971 op->state->phase = PHASE_EXPECT_ELEMENTS;
972 decode_and_send (op);
979 * Send a result message to the client indicating
980 * that there is a new element.
982 * @param op union operation
983 * @param element element to send
986 send_client_element (struct Operation *op,
987 struct GNUNET_SET_Element *element)
989 struct GNUNET_MQ_Envelope *ev;
990 struct GNUNET_SET_ResultMessage *rm;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "sending element (size %u) to client\n",
995 GNUNET_assert (0 != op->spec->client_request_id);
996 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
999 GNUNET_MQ_discard (ev);
1003 rm->result_status = htons (GNUNET_SET_STATUS_OK);
1004 rm->request_id = htonl (op->spec->client_request_id);
1005 rm->element_type = element->element_type;
1006 memcpy (&rm[1], element->data, element->size);
1007 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1012 * Signal to the client that the operation has finished and
1013 * destroy the operation.
1015 * @param cls operation to destroy
1018 send_done_and_destroy (void *cls)
1020 struct Operation *op = cls;
1021 struct GNUNET_MQ_Envelope *ev;
1022 struct GNUNET_SET_ResultMessage *rm;
1024 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
1025 rm->request_id = htonl (op->spec->client_request_id);
1026 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
1027 rm->element_type = htons (0);
1028 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1029 _GSS_operation_destroy (op, GNUNET_YES);
1037 * Send all remaining elements in the full result iterator.
1039 * @param cls operation
1042 send_remaining_elements (void *cls)
1044 struct Operation *op = cls;
1045 struct KeyEntry *ke;
1048 res = GNUNET_CONTAINER_multihashmap32_iterator_next (op->state->full_result_iter,
1050 (const void **) &ke);
1051 if (GNUNET_NO == res)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "sending done and destroy because iterator ran out\n");
1055 send_done_and_destroy (op);
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "sending elements from key entry\n");
1062 struct GNUNET_MQ_Envelope *ev;
1063 struct GNUNET_SET_ResultMessage *rm;
1064 struct GNUNET_SET_Element *element;
1065 element = &ke->element->element;
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "sending element (size %u) to client (full set)\n",
1070 GNUNET_assert (0 != op->spec->client_request_id);
1071 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
1074 GNUNET_MQ_discard (ev);
1078 rm->result_status = htons (GNUNET_SET_STATUS_OK);
1079 rm->request_id = htonl (op->spec->client_request_id);
1080 rm->element_type = element->element_type;
1081 memcpy (&rm[1], element->data, element->size);
1082 if (NULL == ke->next_colliding)
1084 GNUNET_MQ_notify_sent (ev, send_remaining_elements, op);
1085 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1088 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1089 ke = ke->next_colliding;
1095 * Send a result message to the client indicating
1096 * that the operation is over.
1097 * After the result done message has been sent to the client,
1098 * destroy the evaluate operation.
1100 * @param op union operation
1103 finish_and_destroy (struct Operation *op)
1105 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1107 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
1109 /* prevent that the op is free'd by the tunnel end handler */
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "sending full result set\n");
1112 GNUNET_assert (NULL == op->state->full_result_iter);
1113 op->state->full_result_iter =
1114 GNUNET_CONTAINER_multihashmap32_iterator_create (op->state->key_to_element);
1115 send_remaining_elements (op);
1118 send_done_and_destroy (op);
1123 * Handle an element message from a remote peer.
1125 * @param cls the union operation
1126 * @param mh the message
1129 handle_p2p_elements (void *cls,
1130 const struct GNUNET_MessageHeader *mh)
1132 struct Operation *op = cls;
1133 struct ElementEntry *ee;
1134 uint16_t element_size;
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Got element from peer\n");
1138 if ( (op->state->phase != PHASE_EXPECT_ELEMENTS) &&
1139 (op->state->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1141 fail_union_operation (op);
1142 GNUNET_break_op (0);
1145 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1146 ee = GNUNET_malloc (sizeof (struct ElementEntry) + element_size);
1147 memcpy (&ee[1], &mh[1], element_size);
1148 ee->element.size = element_size;
1149 ee->element.data = &ee[1];
1150 ee->remote = GNUNET_YES;
1151 GNUNET_CRYPTO_hash (ee->element.data,
1155 if (GNUNET_YES == op_has_element (op, &ee->element_hash))
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "got existing element from peer\n");
1163 op_register_element (op, ee);
1164 /* only send results immediately if the client wants it */
1165 if (GNUNET_SET_RESULT_ADDED == op->spec->result_mode)
1166 send_client_element (op, &ee->element);
1171 * Handle an element request from a remote peer.
1173 * @param cls the union operation
1174 * @param mh the message
1177 handle_p2p_element_requests (void *cls,
1178 const struct GNUNET_MessageHeader *mh)
1180 struct Operation *op = cls;
1181 const struct IBF_Key *ibf_key;
1182 unsigned int num_keys;
1184 /* look up elements and send them */
1185 if (op->state->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1187 GNUNET_break_op (0);
1188 fail_union_operation (op);
1191 num_keys = (ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
1192 / sizeof (struct IBF_Key);
1193 if ((ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
1194 != num_keys * sizeof (struct IBF_Key))
1196 GNUNET_break_op (0);
1197 fail_union_operation (op);
1201 ibf_key = (const struct IBF_Key *) &mh[1];
1202 while (0 != num_keys--)
1204 send_elements_for_key (op, *ibf_key);
1211 * Handle a done message from a remote peer
1213 * @param cls the union operation
1214 * @param mh the message
1217 handle_p2p_done (void *cls,
1218 const struct GNUNET_MessageHeader *mh)
1220 struct Operation *op = cls;
1221 struct GNUNET_MQ_Envelope *ev;
1223 if (op->state->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1225 /* we got all requests, but still have to send our elements as response */
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "got DONE, sending final DONE after elements\n");
1229 op->state->phase = PHASE_FINISHED;
1230 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE);
1231 GNUNET_MQ_send (op->mq, ev);
1234 if (op->state->phase == PHASE_EXPECT_ELEMENTS)
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "got final DONE\n");
1238 op->state->phase = PHASE_FINISHED;
1239 finish_and_destroy (op);
1242 GNUNET_break_op (0);
1243 fail_union_operation (op);
1248 * Initiate operation to evaluate a set union with a remote peer.
1250 * @param op operation to perform (to be initialized)
1251 * @param opaque_context message to be transmitted to the listener
1252 * to convince him to accept, may be NULL
1255 union_evaluate (struct Operation *op,
1256 const struct GNUNET_MessageHeader *opaque_context)
1258 struct GNUNET_MQ_Envelope *ev;
1259 struct OperationRequestMessage *msg;
1261 op->state = GNUNET_new (struct OperationState);
1262 /* copy the current generation's strata estimator for this operation */
1263 op->state->se = strata_estimator_dup (op->spec->set->state->se);
1264 /* we started the operation, thus we have to send the operation request */
1265 op->state->phase = PHASE_EXPECT_SE;
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Initiating union operation evaluation\n");
1268 ev = GNUNET_MQ_msg_nested_mh (msg,
1269 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
1273 /* the context message is too large */
1275 GNUNET_SERVER_client_disconnect (op->spec->set->client);
1278 msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
1279 msg->app_id = op->spec->app_id;
1280 GNUNET_MQ_send (op->mq, ev);
1282 if (NULL != opaque_context)
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "sent op request with context message\n");
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1287 "sent op request without context message\n");
1292 * Accept an union operation request from a remote peer.
1293 * Only initializes the private operation state.
1295 * @param op operation that will be accepted as a union operation
1298 union_accept (struct Operation *op)
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "accepting set union operation\n");
1302 op->state = GNUNET_new (struct OperationState);
1303 op->state->se = strata_estimator_dup (op->spec->set->state->se);
1304 /* kick off the operation */
1305 send_strata_estimator (op);
1310 * Create a new set supporting the union operation
1312 * We maintain one strata estimator per set and then manipulate it over the
1313 * lifetime of the set, as recreating a strata estimator would be expensive.
1315 * @return the newly created set
1317 static struct SetState *
1318 union_set_create (void)
1320 struct SetState *set_state;
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "union set created\n");
1324 set_state = GNUNET_new (struct SetState);
1325 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
1326 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1332 * Add the element from the given element message to the set.
1334 * @param set_state state of the set want to add to
1335 * @param ee the element to add to the set
1338 union_add (struct SetState *set_state, struct ElementEntry *ee)
1340 strata_estimator_insert (set_state->se,
1341 get_ibf_key (&ee->element_hash, 0));
1346 * Remove the element given in the element message from the set.
1347 * Only marks the element as removed, so that older set operations can still exchange it.
1349 * @param set_state state of the set to remove from
1350 * @param ee set element to remove
1353 union_remove (struct SetState *set_state, struct ElementEntry *ee)
1355 strata_estimator_remove (set_state->se,
1356 get_ibf_key (&ee->element_hash, 0));
1361 * Destroy a set that supports the union operation.
1363 * @param set_state the set to destroy
1366 union_set_destroy (struct SetState *set_state)
1368 if (NULL != set_state->se)
1370 strata_estimator_destroy (set_state->se);
1371 set_state->se = NULL;
1373 GNUNET_free (set_state);
1378 * Dispatch messages for a union operation.
1380 * @param op the state of the union evaluate operation
1381 * @param mh the received message
1382 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
1383 * #GNUNET_OK otherwise
1386 union_handle_p2p_message (struct Operation *op,
1387 const struct GNUNET_MessageHeader *mh)
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "received p2p message (t: %u, s: %u)\n",
1393 switch (ntohs (mh->type))
1395 case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF:
1396 return handle_p2p_ibf (op, mh);
1397 case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE:
1398 return handle_p2p_strata_estimator (op, mh);
1399 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1400 handle_p2p_elements (op, mh);
1402 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1403 handle_p2p_element_requests (op, mh);
1405 case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE:
1406 handle_p2p_done (op, mh);
1409 /* something wrong with cadet's message handlers? */
1416 * handler for peer-disconnects, notifies the client
1417 * about the aborted operation in case the op was not concluded
1419 * @param op the destroyed operation
1422 union_peer_disconnect (struct Operation *op)
1424 if (PHASE_FINISHED != op->state->phase)
1426 struct GNUNET_MQ_Envelope *ev;
1427 struct GNUNET_SET_ResultMessage *msg;
1429 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
1430 msg->request_id = htonl (op->spec->client_request_id);
1431 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1432 msg->element_type = htons (0);
1433 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1435 "other peer disconnected prematurely\n");
1436 _GSS_operation_destroy (op, GNUNET_YES);
1439 // else: the session has already been concluded
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "other peer disconnected (finished)\n");
1442 if (GNUNET_NO == op->state->client_done_sent)
1443 finish_and_destroy (op);
1448 * Get the table with implementing functions for
1451 * @return the operation specific VTable
1453 const struct SetVT *
1456 static const struct SetVT union_vt = {
1457 .create = &union_set_create,
1458 .msg_handler = &union_handle_p2p_message,
1460 .remove = &union_remove,
1461 .destroy_set = &union_set_destroy,
1462 .evaluate = &union_evaluate,
1463 .accept = &union_accept,
1464 .peer_disconnect = &union_peer_disconnect,
1465 .cancel = &union_op_cancel,