2 This file is part of GNUnet
3 Copyright (C) 2013-2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file set/gnunet-service-set_union.c
22 * @brief two-peer set operations
23 * @author Florian Dold
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-set.h"
31 #include "gnunet-service-set_union.h"
32 #include "gnunet-service-set_union_strata_estimator.h"
33 #include "gnunet-service-set_protocol.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "set-union",__VA_ARGS__)
41 * Number of IBFs in a strata estimator.
43 #define SE_STRATA_COUNT 32
46 * Size of the IBFs in the strata estimator.
48 #define SE_IBF_SIZE 80
51 * The hash num parameter for the difference digests and strata estimators.
53 #define SE_IBF_HASH_NUM 4
56 * Number of buckets that can be transmitted in one message.
58 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
61 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
62 * Choose this value so that computing the IBF is still cheaper
63 * than transmitting all values.
65 #define MAX_IBF_ORDER (20)
68 * Number of buckets used in the ibf per estimated
75 * Current phase we are in for a union operation.
77 enum UnionOperationPhase
80 * We sent the request message, and expect a strata estimator.
85 * We sent the strata estimator, and expect an IBF. This phase is entered once
86 * upon initialization and later via #PHASE_EXPECT_ELEMENTS_AND_REQUESTS.
88 * XXX: could use better wording.
89 * XXX: repurposed to also expect a "request full set" message, should be renamed
91 * After receiving the complete IBF, we enter #PHASE_EXPECT_ELEMENTS
96 * Continuation for multi part IBFs.
98 PHASE_EXPECT_IBF_CONT,
101 * We are decoding an IBF.
103 PHASE_INVENTORY_ACTIVE,
106 * The other peer is decoding the IBF we just sent.
108 PHASE_INVENTORY_PASSIVE,
111 * The protocol is almost finished, but we still have to flush our message
112 * queue and/or expect some elements.
114 PHASE_FINISH_CLOSING,
117 * In the penultimate phase,
118 * we wait until all our demands
119 * are satisfied. Then we send a done
120 * message, and wait for another done message.
122 PHASE_FINISH_WAITING,
125 * In the ultimate phase, we wait until
126 * our demands are satisfied and then
127 * quit (sending another DONE message).
132 * After sending the full set, wait for responses with the elements
133 * that the local peer is missing.
140 * State of an evaluate operation with another peer.
142 struct OperationState
145 * Copy of the set's strata estimator at the time of
146 * creation of this operation.
148 struct StrataEstimator *se;
151 * The IBF we currently receive.
153 struct InvertibleBloomFilter *remote_ibf;
156 * The IBF with the local set's element.
158 struct InvertibleBloomFilter *local_ibf;
161 * Maps unsalted IBF-Keys to elements.
162 * Used as a multihashmap, the keys being the lower 32bit of the IBF-Key.
163 * Colliding IBF-Keys are linked.
165 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
168 * Current state of the operation.
170 enum UnionOperationPhase phase;
173 * Did we send the client that we are done?
175 int client_done_sent;
178 * Number of ibf buckets already received into the @a remote_ibf.
180 unsigned int ibf_buckets_received;
183 * Hashes for elements that we have demanded from the other peer.
185 struct GNUNET_CONTAINER_MultiHashMap *demanded_hashes;
188 * Salt that we're using for sending IBFs
193 * Salt for the IBF we've received and that we're currently decoding.
195 uint32_t salt_receive;
198 * Number of elements we received from the other peer
199 * that were not in the local set yet.
201 uint32_t received_fresh;
204 * Total number of elements received from the other peer.
206 uint32_t received_total;
209 * Initial size of our set, just before
210 * the operation started.
212 uint64_t initial_size;
217 * The key entry is used to associate an ibf key with an element.
222 * IBF key for the entry, derived from the current salt.
224 struct IBF_Key ibf_key;
227 * The actual element associated with the key.
229 * Only owned by the union operation if element->operation
232 struct ElementEntry *element;
235 * Did we receive this element?
236 * Even if element->is_foreign is false, we might
237 * have received the element, so this indicates that
238 * the other peer has it.
245 * Used as a closure for sending elements
246 * with a specific IBF key.
248 struct SendElementClosure
251 * The IBF key whose matching elements should be
254 struct IBF_Key ibf_key;
257 * Operation for which the elements
260 struct Operation *op;
265 * Extra state required for efficient set union.
270 * The strata estimator is only generated once for
272 * The IBF keys are derived from the element hashes with
275 struct StrataEstimator *se;
280 * Iterator over hash map entries, called to
281 * destroy the linked list of colliding ibf key entries.
284 * @param key current key code
285 * @param value value in the hash map
286 * @return #GNUNET_YES if we should continue to iterate,
290 destroy_key_to_element_iter (void *cls,
294 struct KeyEntry *k = value;
296 GNUNET_assert (NULL != k);
297 if (GNUNET_YES == k->element->remote)
299 GNUNET_free (k->element);
308 * Destroy the union operation. Only things specific to the union
309 * operation are destroyed.
311 * @param op union operation to destroy
314 union_op_cancel (struct Operation *op)
316 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "destroying union op\n");
318 /* check if the op was canceled twice */
319 GNUNET_assert (NULL != op->state);
320 if (NULL != op->state->remote_ibf)
322 ibf_destroy (op->state->remote_ibf);
323 op->state->remote_ibf = NULL;
325 if (NULL != op->state->demanded_hashes)
327 GNUNET_CONTAINER_multihashmap_destroy (op->state->demanded_hashes);
328 op->state->demanded_hashes = NULL;
330 if (NULL != op->state->local_ibf)
332 ibf_destroy (op->state->local_ibf);
333 op->state->local_ibf = NULL;
335 if (NULL != op->state->se)
337 strata_estimator_destroy (op->state->se);
338 op->state->se = NULL;
340 if (NULL != op->state->key_to_element)
342 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
343 &destroy_key_to_element_iter,
345 GNUNET_CONTAINER_multihashmap32_destroy (op->state->key_to_element);
346 op->state->key_to_element = NULL;
348 GNUNET_free (op->state);
350 LOG (GNUNET_ERROR_TYPE_DEBUG,
351 "destroying union op done\n");
356 * Inform the client that the union operation has failed,
357 * and proceed to destroy the evaluate operation.
359 * @param op the union operation to fail
362 fail_union_operation (struct Operation *op)
364 struct GNUNET_MQ_Envelope *ev;
365 struct GNUNET_SET_ResultMessage *msg;
367 LOG (GNUNET_ERROR_TYPE_ERROR,
368 "union operation failed\n");
369 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
370 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
371 msg->request_id = htonl (op->client_request_id);
372 msg->element_type = htons (0);
373 GNUNET_MQ_send (op->set->cs->mq,
375 _GSS_operation_destroy (op, GNUNET_YES);
380 * Derive the IBF key from a hash code and
383 * @param src the hash code
384 * @return the derived IBF key
386 static struct IBF_Key
387 get_ibf_key (const struct GNUNET_HashCode *src)
392 GNUNET_CRYPTO_kdf (&key, sizeof (key),
394 &salt, sizeof (salt),
401 * Context for #op_get_element_iterator
403 struct GetElementContext
408 struct GNUNET_HashCode hash;
418 * Iterator over the mapping from IBF keys to element entries. Checks if we
419 * have an element with a given GNUNET_HashCode.
422 * @param key current key code
423 * @param value value in the hash map
424 * @return #GNUNET_YES if we should search further,
425 * #GNUNET_NO if we've found the element.
428 op_get_element_iterator (void *cls,
432 struct GetElementContext *ctx = cls;
433 struct KeyEntry *k = value;
435 GNUNET_assert (NULL != k);
436 if (0 == GNUNET_CRYPTO_hash_cmp (&k->element->element_hash,
447 * Determine whether the given element is already in the operation's element
450 * @param op operation that should be tested for 'element_hash'
451 * @param element_hash hash of the element to look for
452 * @return #GNUNET_YES if the element has been found, #GNUNET_NO otherwise
454 static struct KeyEntry *
455 op_get_element (struct Operation *op,
456 const struct GNUNET_HashCode *element_hash)
459 struct IBF_Key ibf_key;
460 struct GetElementContext ctx = {{{ 0 }} , 0};
462 ctx.hash = *element_hash;
464 ibf_key = get_ibf_key (element_hash);
465 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
466 (uint32_t) ibf_key.key_val,
467 op_get_element_iterator,
470 /* was the iteration aborted because we found the element? */
471 if (GNUNET_SYSERR == ret)
473 GNUNET_assert (NULL != ctx.k);
481 * Insert an element into the union operation's
482 * key-to-element mapping. Takes ownership of 'ee'.
483 * Note that this does not insert the element in the set,
484 * only in the operation's key-element mapping.
485 * This is done to speed up re-tried operations, if some elements
486 * were transmitted, and then the IBF fails to decode.
488 * XXX: clarify ownership, doesn't sound right.
490 * @param op the union operation
491 * @param ee the element entry
492 * @parem received was this element received from the remote peer?
495 op_register_element (struct Operation *op,
496 struct ElementEntry *ee,
499 struct IBF_Key ibf_key;
502 ibf_key = get_ibf_key (&ee->element_hash);
503 k = GNUNET_new (struct KeyEntry);
505 k->ibf_key = ibf_key;
506 k->received = received;
507 GNUNET_assert (GNUNET_OK ==
508 GNUNET_CONTAINER_multihashmap32_put (op->state->key_to_element,
509 (uint32_t) ibf_key.key_val,
511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
519 salt_key (const struct IBF_Key *k_in,
521 struct IBF_Key *k_out)
524 uint64_t x = k_in->key_val;
526 x = (x >> s) | (x << (64 - s));
535 unsalt_key (const struct IBF_Key *k_in,
537 struct IBF_Key *k_out)
540 uint64_t x = k_in->key_val;
541 x = (x << s) | (x >> (64 - s));
547 * Insert a key into an ibf.
551 * @param value the key entry to get the key from
554 prepare_ibf_iterator (void *cls,
558 struct Operation *op = cls;
559 struct KeyEntry *ke = value;
560 struct IBF_Key salted_key;
562 LOG (GNUNET_ERROR_TYPE_DEBUG,
563 "[OP %x] inserting %lx (hash %s) into ibf\n",
565 (unsigned long) ke->ibf_key.key_val,
566 GNUNET_h2s (&ke->element->element_hash));
567 salt_key (&ke->ibf_key,
568 op->state->salt_send,
570 ibf_insert (op->state->local_ibf, salted_key);
576 * Iterator for initializing the
577 * key-to-element mapping of a union operation
579 * @param cls the union operation `struct Operation *`
581 * @param value the `struct ElementEntry *` to insert
582 * into the key-to-element mapping
583 * @return #GNUNET_YES (to continue iterating)
586 init_key_to_element_iterator (void *cls,
587 const struct GNUNET_HashCode *key,
590 struct Operation *op = cls;
591 struct ElementEntry *ee = value;
593 /* make sure that the element belongs to the set at the time
594 * of creating the operation */
596 _GSS_is_element_of_operation (ee,
599 GNUNET_assert (GNUNET_NO == ee->remote);
600 op_register_element (op,
608 * Initialize the IBF key to element mapping local to this set
611 * @param op the set union operation
614 initialize_key_to_element (struct Operation *op)
618 GNUNET_assert (NULL == op->state->key_to_element);
619 len = GNUNET_CONTAINER_multihashmap_size (op->set->content->elements);
620 op->state->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
621 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
622 &init_key_to_element_iterator,
628 * Create an ibf with the operation's elements
629 * of the specified size
631 * @param op the union operation
632 * @param size size of the ibf to create
633 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
636 prepare_ibf (struct Operation *op,
639 GNUNET_assert (NULL != op->state->key_to_element);
641 if (NULL != op->state->local_ibf)
642 ibf_destroy (op->state->local_ibf);
643 op->state->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
644 if (NULL == op->state->local_ibf)
646 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
647 "Failed to allocate local IBF\n");
648 return GNUNET_SYSERR;
650 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
651 &prepare_ibf_iterator,
658 * Send an ibf of appropriate size.
660 * Fragments the IBF into multiple messages if necessary.
662 * @param op the union operation
663 * @param ibf_order order of the ibf to send, size=2^order
664 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
667 send_ibf (struct Operation *op,
670 unsigned int buckets_sent = 0;
671 struct InvertibleBloomFilter *ibf;
674 prepare_ibf (op, 1<<ibf_order))
676 /* allocation failed */
677 return GNUNET_SYSERR;
680 LOG (GNUNET_ERROR_TYPE_DEBUG,
681 "sending ibf of size %u\n",
685 char name[64] = { 0 };
686 snprintf (name, sizeof (name), "# sent IBF (order %u)", ibf_order);
687 GNUNET_STATISTICS_update (_GSS_statistics, name, 1, GNUNET_NO);
690 ibf = op->state->local_ibf;
692 while (buckets_sent < (1 << ibf_order))
694 unsigned int buckets_in_message;
695 struct GNUNET_MQ_Envelope *ev;
696 struct IBFMessage *msg;
698 buckets_in_message = (1 << ibf_order) - buckets_sent;
699 /* limit to maximum */
700 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
701 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
703 ev = GNUNET_MQ_msg_extra (msg,
704 buckets_in_message * IBF_BUCKET_SIZE,
705 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF);
708 msg->order = ibf_order;
709 msg->offset = htonl (buckets_sent);
710 msg->salt = htonl (op->state->salt_send);
711 ibf_write_slice (ibf, buckets_sent,
712 buckets_in_message, &msg[1]);
713 buckets_sent += buckets_in_message;
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "ibf chunk size %u, %u/%u sent\n",
719 GNUNET_MQ_send (op->mq, ev);
722 /* The other peer must decode the IBF, so
724 op->state->phase = PHASE_INVENTORY_PASSIVE;
730 * Compute the necessary order of an ibf
731 * from the size of the symmetric set difference.
733 * @param diff the difference
734 * @return the required size of the ibf
737 get_order_from_difference (unsigned int diff)
739 unsigned int ibf_order;
742 while ( (1<<ibf_order) < (IBF_ALPHA * diff) ||
743 ((1<<ibf_order) < SE_IBF_HASH_NUM) )
745 if (ibf_order > MAX_IBF_ORDER)
746 ibf_order = MAX_IBF_ORDER;
747 // add one for correction
748 return ibf_order + 1;
753 * Send a set element.
755 * @param cls the union operation `struct Operation *`
757 * @param value the `struct ElementEntry *` to insert
758 * into the key-to-element mapping
759 * @return #GNUNET_YES (to continue iterating)
762 send_full_element_iterator (void *cls,
763 const struct GNUNET_HashCode *key,
766 struct Operation *op = cls;
767 struct GNUNET_SET_ElementMessage *emsg;
768 struct ElementEntry *ee = value;
769 struct GNUNET_SET_Element *el = &ee->element;
770 struct GNUNET_MQ_Envelope *ev;
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Sending element %s\n",
775 ev = GNUNET_MQ_msg_extra (emsg,
777 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_FULL_ELEMENT);
778 emsg->element_type = htons (el->element_type);
779 GNUNET_memcpy (&emsg[1],
782 GNUNET_MQ_send (op->mq,
789 * Switch to full set transmission for @a op.
791 * @param op operation to switch to full set transmission.
794 send_full_set (struct Operation *op)
796 struct GNUNET_MQ_Envelope *ev;
798 op->state->phase = PHASE_FULL_SENDING;
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Dedicing to transmit the full set\n");
801 /* FIXME: use a more memory-friendly way of doing this with an
802 iterator, just as we do in the non-full case! */
803 (void) GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
804 &send_full_element_iterator,
806 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_FULL_DONE);
807 GNUNET_MQ_send (op->mq,
813 * Handle a strata estimator from a remote peer
815 * @param cls the union operation
816 * @param msg the message
819 check_union_p2p_strata_estimator (void *cls,
820 const struct StrataEstimatorMessage *msg)
822 struct Operation *op = cls;
826 if (op->state->phase != PHASE_EXPECT_SE)
829 return GNUNET_SYSERR;
831 is_compressed = (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SEC == htons (msg->header.type));
832 len = ntohs (msg->header.size) - sizeof (struct StrataEstimatorMessage);
833 if ( (GNUNET_NO == is_compressed) &&
834 (len != SE_STRATA_COUNT * SE_IBF_SIZE * IBF_BUCKET_SIZE) )
837 return GNUNET_SYSERR;
844 * Handle a strata estimator from a remote peer
846 * @param cls the union operation
847 * @param msg the message
850 handle_union_p2p_strata_estimator (void *cls,
851 const struct StrataEstimatorMessage *msg)
853 struct Operation *op = cls;
854 struct StrataEstimator *remote_se;
860 is_compressed = (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SEC == htons (msg->header.type));
861 GNUNET_STATISTICS_update (_GSS_statistics,
862 "# bytes of SE received",
863 ntohs (msg->header.size),
865 len = ntohs (msg->header.size) - sizeof (struct StrataEstimatorMessage);
866 other_size = GNUNET_ntohll (msg->set_size);
867 remote_se = strata_estimator_create (SE_STRATA_COUNT,
870 if (NULL == remote_se)
872 /* insufficient resources, fail */
873 fail_union_operation (op);
877 strata_estimator_read (&msg[1],
882 /* decompression failed */
883 strata_estimator_destroy (remote_se);
884 fail_union_operation (op);
887 GNUNET_assert (NULL != op->state->se);
888 diff = strata_estimator_difference (remote_se,
894 strata_estimator_destroy (remote_se);
895 strata_estimator_destroy (op->state->se);
896 op->state->se = NULL;
897 LOG (GNUNET_ERROR_TYPE_DEBUG,
898 "got se diff=%d, using ibf size %d\n",
900 1U << get_order_from_difference (diff));
905 set_debug = getenv ("GNUNET_SET_BENCHMARK");
906 if ( (NULL != set_debug) &&
907 (0 == strcmp (set_debug, "1")) )
909 FILE *f = fopen ("set.log", "a");
910 fprintf (f, "%llu\n", (unsigned long long) diff);
915 if ( (GNUNET_YES == op->byzantine) &&
916 (other_size < op->byzantine_lower_bound) )
919 fail_union_operation (op);
923 if ( (GNUNET_YES == op->force_full) ||
924 (diff > op->state->initial_size / 4) ||
927 LOG (GNUNET_ERROR_TYPE_DEBUG,
928 "Deciding to go for full set transmission (diff=%d, own set=%u)\n",
930 op->state->initial_size);
931 GNUNET_STATISTICS_update (_GSS_statistics,
935 if ( (op->state->initial_size <= other_size) ||
942 struct GNUNET_MQ_Envelope *ev;
944 LOG (GNUNET_ERROR_TYPE_DEBUG,
945 "Telling other peer that we expect its full set\n");
946 op->state->phase = PHASE_EXPECT_IBF;
947 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_REQUEST_FULL);
948 GNUNET_MQ_send (op->mq,
954 GNUNET_STATISTICS_update (_GSS_statistics,
960 get_order_from_difference (diff)))
962 /* Internal error, best we can do is shut the connection */
963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
964 "Failed to send IBF, closing connection\n");
965 fail_union_operation (op);
969 GNUNET_CADET_receive_done (op->channel);
974 * Iterator to send elements to a remote peer
976 * @param cls closure with the element key and the union operation
978 * @param value the key entry
981 send_offers_iterator (void *cls,
985 struct SendElementClosure *sec = cls;
986 struct Operation *op = sec->op;
987 struct KeyEntry *ke = value;
988 struct GNUNET_MQ_Envelope *ev;
989 struct GNUNET_MessageHeader *mh;
991 /* Detect 32-bit key collision for the 64-bit IBF keys. */
992 if (ke->ibf_key.key_val != sec->ibf_key.key_val)
995 ev = GNUNET_MQ_msg_header_extra (mh,
996 sizeof (struct GNUNET_HashCode),
997 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_OFFER);
999 GNUNET_assert (NULL != ev);
1000 *(struct GNUNET_HashCode *) &mh[1] = ke->element->element_hash;
1001 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "[OP %x] sending element offer (%s) to peer\n",
1004 GNUNET_h2s (&ke->element->element_hash));
1005 GNUNET_MQ_send (op->mq, ev);
1011 * Send offers (in the form of GNUNET_Hash-es) to the remote peer for the given IBF key.
1013 * @param op union operation
1014 * @param ibf_key IBF key of interest
1017 send_offers_for_key (struct Operation *op,
1018 struct IBF_Key ibf_key)
1020 struct SendElementClosure send_cls;
1022 send_cls.ibf_key = ibf_key;
1024 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
1025 (uint32_t) ibf_key.key_val,
1026 &send_offers_iterator,
1032 * Decode which elements are missing on each side, and
1033 * send the appropriate offers and inquiries.
1035 * @param op union operation
1036 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1039 decode_and_send (struct Operation *op)
1042 struct IBF_Key last_key;
1044 unsigned int num_decoded;
1045 struct InvertibleBloomFilter *diff_ibf;
1047 GNUNET_assert (PHASE_INVENTORY_ACTIVE == op->state->phase);
1051 op->state->remote_ibf->size))
1054 /* allocation failed */
1055 return GNUNET_SYSERR;
1057 diff_ibf = ibf_dup (op->state->local_ibf);
1058 ibf_subtract (diff_ibf,
1059 op->state->remote_ibf);
1061 ibf_destroy (op->state->remote_ibf);
1062 op->state->remote_ibf = NULL;
1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1065 "decoding IBF (size=%u)\n",
1069 key.key_val = 0; /* just to avoid compiler thinking we use undef'ed variable */
1074 int cycle_detected = GNUNET_NO;
1078 res = ibf_decode (diff_ibf, &side, &key);
1079 if (res == GNUNET_OK)
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 "decoded ibf key %lx\n",
1083 (unsigned long) key.key_val);
1085 if ( (num_decoded > diff_ibf->size) ||
1086 ( (num_decoded > 1) &&
1087 (last_key.key_val == key.key_val) ) )
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "detected cyclic ibf (decoded %u/%u)\n",
1093 cycle_detected = GNUNET_YES;
1096 if ( (GNUNET_SYSERR == res) ||
1097 (GNUNET_YES == cycle_detected) )
1101 while (1<<next_order < diff_ibf->size)
1104 if (next_order <= MAX_IBF_ORDER)
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 "decoding failed, sending larger ibf (size %u)\n",
1109 GNUNET_STATISTICS_update (_GSS_statistics,
1113 op->state->salt_send++;
1115 send_ibf (op, next_order))
1117 /* Internal error, best we can do is shut the connection */
1118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1119 "Failed to send IBF, closing connection\n");
1120 fail_union_operation (op);
1121 ibf_destroy (diff_ibf);
1122 return GNUNET_SYSERR;
1127 GNUNET_STATISTICS_update (_GSS_statistics,
1128 "# of failed union operations (too large)",
1131 // XXX: Send the whole set, element-by-element
1132 LOG (GNUNET_ERROR_TYPE_ERROR,
1133 "set union failed: reached ibf limit\n");
1134 fail_union_operation (op);
1135 ibf_destroy (diff_ibf);
1136 return GNUNET_SYSERR;
1140 if (GNUNET_NO == res)
1142 struct GNUNET_MQ_Envelope *ev;
1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1145 "transmitted all values, sending DONE\n");
1146 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE);
1147 GNUNET_MQ_send (op->mq, ev);
1148 /* We now wait until we get a DONE message back
1149 * and then wait for our MQ to be flushed and all our
1150 * demands be delivered. */
1155 struct IBF_Key unsalted_key;
1158 op->state->salt_receive,
1160 send_offers_for_key (op,
1163 else if (-1 == side)
1165 struct GNUNET_MQ_Envelope *ev;
1166 struct InquiryMessage *msg;
1168 /* It may be nice to merge multiple requests, but with CADET's corking it is not worth
1169 * the effort additional complexity. */
1170 ev = GNUNET_MQ_msg_extra (msg,
1171 sizeof (struct IBF_Key),
1172 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_INQUIRY);
1173 msg->salt = htonl (op->state->salt_receive);
1174 GNUNET_memcpy (&msg[1],
1176 sizeof (struct IBF_Key));
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "sending element inquiry for IBF key %lx\n",
1179 (unsigned long) key.key_val);
1180 GNUNET_MQ_send (op->mq, ev);
1187 ibf_destroy (diff_ibf);
1193 * Check an IBF message from a remote peer.
1195 * Reassemble the IBF from multiple pieces, and
1196 * process the whole IBF once possible.
1198 * @param cls the union operation
1199 * @param msg the header of the message
1200 * @return #GNUNET_OK if @a msg is well-formed
1203 check_union_p2p_ibf (void *cls,
1204 const struct IBFMessage *msg)
1206 struct Operation *op = cls;
1207 unsigned int buckets_in_message;
1209 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1211 GNUNET_break_op (0);
1212 return GNUNET_SYSERR;
1214 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
1215 if (0 == buckets_in_message)
1217 GNUNET_break_op (0);
1218 return GNUNET_SYSERR;
1220 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
1222 GNUNET_break_op (0);
1223 return GNUNET_SYSERR;
1225 if (op->state->phase == PHASE_EXPECT_IBF_CONT)
1227 if (ntohl (msg->offset) != op->state->ibf_buckets_received)
1229 GNUNET_break_op (0);
1230 return GNUNET_SYSERR;
1232 if (1<<msg->order != op->state->remote_ibf->size)
1234 GNUNET_break_op (0);
1235 return GNUNET_SYSERR;
1237 if (ntohl (msg->salt) != op->state->salt_receive)
1239 GNUNET_break_op (0);
1240 return GNUNET_SYSERR;
1243 else if ( (op->state->phase != PHASE_INVENTORY_PASSIVE) &&
1244 (op->state->phase != PHASE_EXPECT_IBF) )
1246 GNUNET_break_op (0);
1247 return GNUNET_SYSERR;
1255 * Handle an IBF message from a remote peer.
1257 * Reassemble the IBF from multiple pieces, and
1258 * process the whole IBF once possible.
1260 * @param cls the union operation
1261 * @param msg the header of the message
1264 handle_union_p2p_ibf (void *cls,
1265 const struct IBFMessage *msg)
1267 struct Operation *op = cls;
1268 unsigned int buckets_in_message;
1270 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
1271 if ( (op->state->phase == PHASE_INVENTORY_PASSIVE) ||
1272 (op->state->phase == PHASE_EXPECT_IBF) )
1274 op->state->phase = PHASE_EXPECT_IBF_CONT;
1275 GNUNET_assert (NULL == op->state->remote_ibf);
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Creating new ibf of size %u\n",
1279 op->state->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
1280 op->state->salt_receive = ntohl (msg->salt);
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 "Receiving new IBF with salt %u\n",
1283 op->state->salt_receive);
1284 if (NULL == op->state->remote_ibf)
1286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287 "Failed to parse remote IBF, closing connection\n");
1288 fail_union_operation (op);
1291 op->state->ibf_buckets_received = 0;
1292 if (0 != ntohl (msg->offset))
1294 GNUNET_break_op (0);
1295 fail_union_operation (op);
1301 GNUNET_assert (op->state->phase == PHASE_EXPECT_IBF_CONT);
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "Received more of IBF\n");
1305 GNUNET_assert (NULL != op->state->remote_ibf);
1307 ibf_read_slice (&msg[1],
1308 op->state->ibf_buckets_received,
1310 op->state->remote_ibf);
1311 op->state->ibf_buckets_received += buckets_in_message;
1313 if (op->state->ibf_buckets_received == op->state->remote_ibf->size)
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "received full ibf\n");
1317 op->state->phase = PHASE_INVENTORY_ACTIVE;
1319 decode_and_send (op))
1321 /* Internal error, best we can do is shut down */
1322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1323 "Failed to decode IBF, closing connection\n");
1324 fail_union_operation (op);
1328 GNUNET_CADET_receive_done (op->channel);
1333 * Send a result message to the client indicating
1334 * that there is a new element.
1336 * @param op union operation
1337 * @param element element to send
1338 * @param status status to send with the new element
1341 send_client_element (struct Operation *op,
1342 struct GNUNET_SET_Element *element,
1345 struct GNUNET_MQ_Envelope *ev;
1346 struct GNUNET_SET_ResultMessage *rm;
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "sending element (size %u) to client\n",
1351 GNUNET_assert (0 != op->client_request_id);
1352 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
1355 GNUNET_MQ_discard (ev);
1359 rm->result_status = htons (status);
1360 rm->request_id = htonl (op->client_request_id);
1361 rm->element_type = htons (element->element_type);
1362 rm->current_size = GNUNET_htonll (GNUNET_CONTAINER_multihashmap32_size (op->state->key_to_element));
1363 GNUNET_memcpy (&rm[1],
1366 GNUNET_MQ_send (op->set->cs->mq,
1372 * Destroy remote channel.
1374 * @param op operation
1376 void destroy_channel (struct Operation *op)
1378 struct GNUNET_CADET_Channel *channel;
1380 if (NULL != (channel = op->channel))
1382 /* This will free op; called conditionally as this helper function
1383 is also called from within the channel disconnect handler. */
1385 GNUNET_CADET_channel_destroy (channel);
1391 * Signal to the client that the operation has finished and
1392 * destroy the operation.
1394 * @param cls operation to destroy
1397 send_client_done (void *cls)
1399 struct Operation *op = cls;
1400 struct GNUNET_MQ_Envelope *ev;
1401 struct GNUNET_SET_ResultMessage *rm;
1403 if (GNUNET_YES == op->state->client_done_sent) {
1407 if (PHASE_DONE != op->state->phase) {
1408 LOG (GNUNET_ERROR_TYPE_ERROR,
1409 "union operation failed\n");
1410 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
1411 rm->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1412 rm->request_id = htonl (op->client_request_id);
1413 rm->element_type = htons (0);
1414 GNUNET_MQ_send (op->set->cs->mq,
1419 op->state->client_done_sent = GNUNET_YES;
1421 LOG (GNUNET_ERROR_TYPE_INFO,
1422 "Signalling client that union operation is done\n");
1423 ev = GNUNET_MQ_msg (rm,
1424 GNUNET_MESSAGE_TYPE_SET_RESULT);
1425 rm->request_id = htonl (op->client_request_id);
1426 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
1427 rm->element_type = htons (0);
1428 rm->current_size = GNUNET_htonll (GNUNET_CONTAINER_multihashmap32_size (op->state->key_to_element));
1429 GNUNET_MQ_send (op->set->cs->mq,
1435 * Tests if the operation is finished, and if so notify.
1437 * @param op operation to check
1440 maybe_finish (struct Operation *op)
1442 unsigned int num_demanded;
1444 num_demanded = GNUNET_CONTAINER_multihashmap_size (op->state->demanded_hashes);
1446 if (PHASE_FINISH_WAITING == op->state->phase)
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "In PHASE_FINISH_WAITING, pending %u demands\n",
1451 if (0 == num_demanded)
1453 struct GNUNET_MQ_Envelope *ev;
1455 op->state->phase = PHASE_DONE;
1456 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DONE);
1457 GNUNET_MQ_send (op->mq,
1459 /* We now wait until the other peer sends P2P_OVER
1460 * after it got all elements from us. */
1463 if (PHASE_FINISH_CLOSING == op->state->phase)
1465 LOG (GNUNET_ERROR_TYPE_DEBUG,
1466 "In PHASE_FINISH_CLOSING, pending %u demands\n",
1468 if (0 == num_demanded)
1470 op->state->phase = PHASE_DONE;
1471 send_client_done (op);
1472 destroy_channel (op);
1479 * Check an element message from a remote peer.
1481 * @param cls the union operation
1482 * @param emsg the message
1485 check_union_p2p_elements (void *cls,
1486 const struct GNUNET_SET_ElementMessage *emsg)
1488 struct Operation *op = cls;
1490 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1492 GNUNET_break_op (0);
1493 return GNUNET_SYSERR;
1495 if (0 == GNUNET_CONTAINER_multihashmap_size (op->state->demanded_hashes))
1497 GNUNET_break_op (0);
1498 return GNUNET_SYSERR;
1505 * Handle an element message from a remote peer.
1506 * Sent by the other peer either because we decoded an IBF and placed a demand,
1507 * or because the other peer switched to full set transmission.
1509 * @param cls the union operation
1510 * @param emsg the message
1513 handle_union_p2p_elements (void *cls,
1514 const struct GNUNET_SET_ElementMessage *emsg)
1516 struct Operation *op = cls;
1517 struct ElementEntry *ee;
1518 struct KeyEntry *ke;
1519 uint16_t element_size;
1521 element_size = ntohs (emsg->header.size) - sizeof (struct GNUNET_SET_ElementMessage);
1522 ee = GNUNET_malloc (sizeof (struct ElementEntry) + element_size);
1523 GNUNET_memcpy (&ee[1],
1526 ee->element.size = element_size;
1527 ee->element.data = &ee[1];
1528 ee->element.element_type = ntohs (emsg->element_type);
1529 ee->remote = GNUNET_YES;
1530 GNUNET_SET_element_hash (&ee->element,
1533 GNUNET_CONTAINER_multihashmap_remove (op->state->demanded_hashes,
1537 /* We got something we didn't demand, since it's not in our map. */
1538 GNUNET_break_op (0);
1539 fail_union_operation (op);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG,
1544 "Got element (size %u, hash %s) from peer\n",
1545 (unsigned int) element_size,
1546 GNUNET_h2s (&ee->element_hash));
1548 GNUNET_STATISTICS_update (_GSS_statistics,
1549 "# received elements",
1552 GNUNET_STATISTICS_update (_GSS_statistics,
1553 "# exchanged elements",
1557 op->state->received_total++;
1559 ke = op_get_element (op, &ee->element_hash);
1562 /* Got repeated element. Should not happen since
1563 * we track demands. */
1564 GNUNET_STATISTICS_update (_GSS_statistics,
1565 "# repeated elements",
1568 ke->received = GNUNET_YES;
1573 LOG (GNUNET_ERROR_TYPE_DEBUG,
1574 "Registering new element from remote peer\n");
1575 op->state->received_fresh++;
1576 op_register_element (op, ee, GNUNET_YES);
1577 /* only send results immediately if the client wants it */
1578 switch (op->result_mode)
1580 case GNUNET_SET_RESULT_ADDED:
1581 send_client_element (op, &ee->element, GNUNET_SET_STATUS_OK);
1583 case GNUNET_SET_RESULT_SYMMETRIC:
1584 send_client_element (op, &ee->element, GNUNET_SET_STATUS_ADD_LOCAL);
1587 /* Result mode not supported, should have been caught earlier. */
1593 if ( (op->state->received_total > 8) &&
1594 (op->state->received_fresh < op->state->received_total / 3) )
1596 /* The other peer gave us lots of old elements, there's something wrong. */
1597 GNUNET_break_op (0);
1598 fail_union_operation (op);
1601 GNUNET_CADET_receive_done (op->channel);
1607 * Check a full element message from a remote peer.
1609 * @param cls the union operation
1610 * @param emsg the message
1613 check_union_p2p_full_element (void *cls,
1614 const struct GNUNET_SET_ElementMessage *emsg)
1616 struct Operation *op = cls;
1618 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1620 GNUNET_break_op (0);
1621 return GNUNET_SYSERR;
1623 // FIXME: check that we expect full elements here?
1629 * Handle an element message from a remote peer.
1631 * @param cls the union operation
1632 * @param emsg the message
1635 handle_union_p2p_full_element (void *cls,
1636 const struct GNUNET_SET_ElementMessage *emsg)
1638 struct Operation *op = cls;
1639 struct ElementEntry *ee;
1640 struct KeyEntry *ke;
1641 uint16_t element_size;
1643 element_size = ntohs (emsg->header.size) - sizeof (struct GNUNET_SET_ElementMessage);
1644 ee = GNUNET_malloc (sizeof (struct ElementEntry) + element_size);
1645 GNUNET_memcpy (&ee[1], &emsg[1], element_size);
1646 ee->element.size = element_size;
1647 ee->element.data = &ee[1];
1648 ee->element.element_type = ntohs (emsg->element_type);
1649 ee->remote = GNUNET_YES;
1650 GNUNET_SET_element_hash (&ee->element, &ee->element_hash);
1652 LOG (GNUNET_ERROR_TYPE_DEBUG,
1653 "Got element (full diff, size %u, hash %s) from peer\n",
1654 (unsigned int) element_size,
1655 GNUNET_h2s (&ee->element_hash));
1657 GNUNET_STATISTICS_update (_GSS_statistics,
1658 "# received elements",
1661 GNUNET_STATISTICS_update (_GSS_statistics,
1662 "# exchanged elements",
1666 op->state->received_total++;
1668 ke = op_get_element (op, &ee->element_hash);
1671 /* Got repeated element. Should not happen since
1672 * we track demands. */
1673 GNUNET_STATISTICS_update (_GSS_statistics,
1674 "# repeated elements",
1677 ke->received = GNUNET_YES;
1682 LOG (GNUNET_ERROR_TYPE_DEBUG,
1683 "Registering new element from remote peer\n");
1684 op->state->received_fresh++;
1685 op_register_element (op, ee, GNUNET_YES);
1686 /* only send results immediately if the client wants it */
1687 switch (op->result_mode)
1689 case GNUNET_SET_RESULT_ADDED:
1690 send_client_element (op, &ee->element, GNUNET_SET_STATUS_OK);
1692 case GNUNET_SET_RESULT_SYMMETRIC:
1693 send_client_element (op, &ee->element, GNUNET_SET_STATUS_ADD_LOCAL);
1696 /* Result mode not supported, should have been caught earlier. */
1702 if ( (GNUNET_YES == op->byzantine) &&
1703 (op->state->received_total > 384 + op->state->received_fresh * 4) &&
1704 (op->state->received_fresh < op->state->received_total / 6) )
1706 /* The other peer gave us lots of old elements, there's something wrong. */
1707 LOG (GNUNET_ERROR_TYPE_ERROR,
1708 "Other peer sent only %llu/%llu fresh elements, failing operation\n",
1709 (unsigned long long) op->state->received_fresh,
1710 (unsigned long long) op->state->received_total);
1711 GNUNET_break_op (0);
1712 fail_union_operation (op);
1715 GNUNET_CADET_receive_done (op->channel);
1720 * Send offers (for GNUNET_Hash-es) in response
1721 * to inquiries (for IBF_Key-s).
1723 * @param cls the union operation
1724 * @param msg the message
1727 check_union_p2p_inquiry (void *cls,
1728 const struct InquiryMessage *msg)
1730 struct Operation *op = cls;
1731 unsigned int num_keys;
1733 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1735 GNUNET_break_op (0);
1736 return GNUNET_SYSERR;
1738 if (op->state->phase != PHASE_INVENTORY_PASSIVE)
1740 GNUNET_break_op (0);
1741 return GNUNET_SYSERR;
1743 num_keys = (ntohs (msg->header.size) - sizeof (struct InquiryMessage))
1744 / sizeof (struct IBF_Key);
1745 if ((ntohs (msg->header.size) - sizeof (struct InquiryMessage))
1746 != num_keys * sizeof (struct IBF_Key))
1748 GNUNET_break_op (0);
1749 return GNUNET_SYSERR;
1756 * Send offers (for GNUNET_Hash-es) in response
1757 * to inquiries (for IBF_Key-s).
1759 * @param cls the union operation
1760 * @param msg the message
1763 handle_union_p2p_inquiry (void *cls,
1764 const struct InquiryMessage *msg)
1766 struct Operation *op = cls;
1767 const struct IBF_Key *ibf_key;
1768 unsigned int num_keys;
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Received union inquiry\n");
1772 num_keys = (ntohs (msg->header.size) - sizeof (struct InquiryMessage))
1773 / sizeof (struct IBF_Key);
1774 ibf_key = (const struct IBF_Key *) &msg[1];
1775 while (0 != num_keys--)
1777 struct IBF_Key unsalted_key;
1779 unsalt_key (ibf_key,
1782 send_offers_for_key (op,
1786 GNUNET_CADET_receive_done (op->channel);
1791 * Iterator over hash map entries, called to
1792 * destroy the linked list of colliding ibf key entries.
1794 * @param cls closure
1795 * @param key current key code
1796 * @param value value in the hash map
1797 * @return #GNUNET_YES if we should continue to iterate,
1798 * #GNUNET_NO if not.
1801 send_missing_full_elements_iter (void *cls,
1805 struct Operation *op = cls;
1806 struct KeyEntry *ke = value;
1807 struct GNUNET_MQ_Envelope *ev;
1808 struct GNUNET_SET_ElementMessage *emsg;
1809 struct ElementEntry *ee = ke->element;
1811 if (GNUNET_YES == ke->received)
1813 ev = GNUNET_MQ_msg_extra (emsg,
1815 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_FULL_ELEMENT);
1816 GNUNET_memcpy (&emsg[1],
1819 emsg->element_type = htons (ee->element.element_type);
1820 GNUNET_MQ_send (op->mq,
1827 * Handle a request for full set transmission.
1829 * @parem cls closure, a set union operation
1830 * @param mh the demand message
1833 handle_union_p2p_request_full (void *cls,
1834 const struct GNUNET_MessageHeader *mh)
1836 struct Operation *op = cls;
1838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1839 "Received request for full set transmission\n");
1840 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1842 GNUNET_break_op (0);
1843 fail_union_operation (op);
1846 if (PHASE_EXPECT_IBF != op->state->phase)
1848 GNUNET_break_op (0);
1849 fail_union_operation (op);
1853 // FIXME: we need to check that our set is larger than the
1854 // byzantine_lower_bound by some threshold
1856 GNUNET_CADET_receive_done (op->channel);
1861 * Handle a "full done" message.
1863 * @parem cls closure, a set union operation
1864 * @param mh the demand message
1867 handle_union_p2p_full_done (void *cls,
1868 const struct GNUNET_MessageHeader *mh)
1870 struct Operation *op = cls;
1872 switch (op->state->phase)
1874 case PHASE_EXPECT_IBF:
1876 struct GNUNET_MQ_Envelope *ev;
1878 LOG (GNUNET_ERROR_TYPE_DEBUG,
1879 "got FULL DONE, sending elements that other peer is missing\n");
1881 /* send all the elements that did not come from the remote peer */
1882 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
1883 &send_missing_full_elements_iter,
1886 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_UNION_P2P_FULL_DONE);
1887 GNUNET_MQ_send (op->mq,
1889 op->state->phase = PHASE_DONE;
1890 /* we now wait until the other peer sends us the OVER message*/
1893 case PHASE_FULL_SENDING:
1895 LOG (GNUNET_ERROR_TYPE_DEBUG,
1896 "got FULL DONE, finishing\n");
1897 /* We sent the full set, and got the response for that. We're done. */
1898 op->state->phase = PHASE_DONE;
1899 GNUNET_CADET_receive_done (op->channel);
1900 send_client_done (op);
1901 destroy_channel (op);
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1907 "Handle full done phase is %u\n",
1908 (unsigned) op->state->phase);
1909 GNUNET_break_op (0);
1910 fail_union_operation (op);
1913 GNUNET_CADET_receive_done (op->channel);
1918 * Check a demand by the other peer for elements based on a list
1919 * of `struct GNUNET_HashCode`s.
1921 * @parem cls closure, a set union operation
1922 * @param mh the demand message
1923 * @return #GNUNET_OK if @a mh is well-formed
1926 check_union_p2p_demand (void *cls,
1927 const struct GNUNET_MessageHeader *mh)
1929 struct Operation *op = cls;
1930 unsigned int num_hashes;
1932 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
1934 GNUNET_break_op (0);
1935 return GNUNET_SYSERR;
1937 num_hashes = (ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
1938 / sizeof (struct GNUNET_HashCode);
1939 if ((ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
1940 != num_hashes * sizeof (struct GNUNET_HashCode))
1942 GNUNET_break_op (0);
1943 return GNUNET_SYSERR;
1950 * Handle a demand by the other peer for elements based on a list
1951 * of `struct GNUNET_HashCode`s.
1953 * @parem cls closure, a set union operation
1954 * @param mh the demand message
1957 handle_union_p2p_demand (void *cls,
1958 const struct GNUNET_MessageHeader *mh)
1960 struct Operation *op = cls;
1961 struct ElementEntry *ee;
1962 struct GNUNET_SET_ElementMessage *emsg;
1963 const struct GNUNET_HashCode *hash;
1964 unsigned int num_hashes;
1965 struct GNUNET_MQ_Envelope *ev;
1967 num_hashes = (ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
1968 / sizeof (struct GNUNET_HashCode);
1969 for (hash = (const struct GNUNET_HashCode *) &mh[1];
1971 hash++, num_hashes--)
1973 ee = GNUNET_CONTAINER_multihashmap_get (op->set->content->elements,
1977 /* Demand for non-existing element. */
1978 GNUNET_break_op (0);
1979 fail_union_operation (op);
1982 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
1984 /* Probably confused lazily copied sets. */
1985 GNUNET_break_op (0);
1986 fail_union_operation (op);
1989 ev = GNUNET_MQ_msg_extra (emsg, ee->element.size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
1990 GNUNET_memcpy (&emsg[1], ee->element.data, ee->element.size);
1991 emsg->reserved = htons (0);
1992 emsg->element_type = htons (ee->element.element_type);
1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "[OP %x] Sending demanded element (size %u, hash %s) to peer\n",
1996 (unsigned int) ee->element.size,
1997 GNUNET_h2s (&ee->element_hash));
1998 GNUNET_MQ_send (op->mq, ev);
1999 GNUNET_STATISTICS_update (_GSS_statistics,
2000 "# exchanged elements",
2004 switch (op->result_mode)
2006 case GNUNET_SET_RESULT_ADDED:
2007 /* Nothing to do. */
2009 case GNUNET_SET_RESULT_SYMMETRIC:
2010 send_client_element (op, &ee->element, GNUNET_SET_STATUS_ADD_REMOTE);
2013 /* Result mode not supported, should have been caught earlier. */
2018 GNUNET_CADET_receive_done (op->channel);
2023 * Check offer (of `struct GNUNET_HashCode`s).
2025 * @param cls the union operation
2026 * @param mh the message
2027 * @return #GNUNET_OK if @a mh is well-formed
2030 check_union_p2p_offer (void *cls,
2031 const struct GNUNET_MessageHeader *mh)
2033 struct Operation *op = cls;
2034 unsigned int num_hashes;
2036 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
2038 GNUNET_break_op (0);
2039 return GNUNET_SYSERR;
2041 /* look up elements and send them */
2042 if ( (op->state->phase != PHASE_INVENTORY_PASSIVE) &&
2043 (op->state->phase != PHASE_INVENTORY_ACTIVE))
2045 GNUNET_break_op (0);
2046 return GNUNET_SYSERR;
2048 num_hashes = (ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
2049 / sizeof (struct GNUNET_HashCode);
2050 if ((ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader)) !=
2051 num_hashes * sizeof (struct GNUNET_HashCode))
2053 GNUNET_break_op (0);
2054 return GNUNET_SYSERR;
2061 * Handle offers (of `struct GNUNET_HashCode`s) and
2062 * respond with demands (of `struct GNUNET_HashCode`s).
2064 * @param cls the union operation
2065 * @param mh the message
2068 handle_union_p2p_offer (void *cls,
2069 const struct GNUNET_MessageHeader *mh)
2071 struct Operation *op = cls;
2072 const struct GNUNET_HashCode *hash;
2073 unsigned int num_hashes;
2075 num_hashes = (ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader))
2076 / sizeof (struct GNUNET_HashCode);
2077 for (hash = (const struct GNUNET_HashCode *) &mh[1];
2079 hash++, num_hashes--)
2081 struct ElementEntry *ee;
2082 struct GNUNET_MessageHeader *demands;
2083 struct GNUNET_MQ_Envelope *ev;
2085 ee = GNUNET_CONTAINER_multihashmap_get (op->set->content->elements,
2088 if (GNUNET_YES == _GSS_is_element_of_operation (ee, op))
2092 GNUNET_CONTAINER_multihashmap_contains (op->state->demanded_hashes,
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "Skipped sending duplicate demand\n");
2100 GNUNET_assert (GNUNET_OK ==
2101 GNUNET_CONTAINER_multihashmap_put (op->state->demanded_hashes,
2104 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2106 LOG (GNUNET_ERROR_TYPE_DEBUG,
2107 "[OP %x] Requesting element (hash %s)\n",
2108 (void *) op, GNUNET_h2s (hash));
2109 ev = GNUNET_MQ_msg_header_extra (demands,
2110 sizeof (struct GNUNET_HashCode),
2111 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_DEMAND);
2112 GNUNET_memcpy (&demands[1],
2114 sizeof (struct GNUNET_HashCode));
2115 GNUNET_MQ_send (op->mq, ev);
2117 GNUNET_CADET_receive_done (op->channel);
2122 * Handle a done message from a remote peer
2124 * @param cls the union operation
2125 * @param mh the message
2128 handle_union_p2p_done (void *cls,
2129 const struct GNUNET_MessageHeader *mh)
2131 struct Operation *op = cls;
2133 if (GNUNET_SET_OPERATION_UNION != op->set->operation)
2135 GNUNET_break_op (0);
2136 fail_union_operation (op);
2139 switch (op->state->phase)
2141 case PHASE_INVENTORY_PASSIVE:
2142 /* We got all requests, but still have to send our elements in response. */
2143 op->state->phase = PHASE_FINISH_WAITING;
2145 LOG (GNUNET_ERROR_TYPE_DEBUG,
2146 "got DONE (as passive partner), waiting for our demands to be satisfied\n");
2147 /* The active peer is done sending offers
2148 * and inquiries. This means that all
2149 * our responses to that (demands and offers)
2150 * must be in flight (queued or in mesh).
2152 * We should notify the active peer once
2153 * all our demands are satisfied, so that the active
2154 * peer can quit if we gave him everything.
2156 GNUNET_CADET_receive_done (op->channel);
2159 case PHASE_INVENTORY_ACTIVE:
2160 LOG (GNUNET_ERROR_TYPE_DEBUG,
2161 "got DONE (as active partner), waiting to finish\n");
2162 /* All demands of the other peer are satisfied,
2163 * and we processed all offers, thus we know
2164 * exactly what our demands must be.
2166 * We'll close the channel
2167 * to the other peer once our demands are met.
2169 op->state->phase = PHASE_FINISH_CLOSING;
2170 GNUNET_CADET_receive_done (op->channel);
2174 GNUNET_break_op (0);
2175 fail_union_operation (op);
2181 * Handle a over message from a remote peer
2183 * @param cls the union operation
2184 * @param mh the message
2187 handle_union_p2p_over (void *cls,
2188 const struct GNUNET_MessageHeader *mh)
2190 send_client_done (cls);
2195 * Initiate operation to evaluate a set union with a remote peer.
2197 * @param op operation to perform (to be initialized)
2198 * @param opaque_context message to be transmitted to the listener
2199 * to convince him to accept, may be NULL
2201 static struct OperationState *
2202 union_evaluate (struct Operation *op,
2203 const struct GNUNET_MessageHeader *opaque_context)
2205 struct OperationState *state;
2206 struct GNUNET_MQ_Envelope *ev;
2207 struct OperationRequestMessage *msg;
2209 ev = GNUNET_MQ_msg_nested_mh (msg,
2210 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
2214 /* the context message is too large */
2218 state = GNUNET_new (struct OperationState);
2219 state->demanded_hashes = GNUNET_CONTAINER_multihashmap_create (32,
2221 /* copy the current generation's strata estimator for this operation */
2222 state->se = strata_estimator_dup (op->set->state->se);
2223 /* we started the operation, thus we have to send the operation request */
2224 state->phase = PHASE_EXPECT_SE;
2225 state->salt_receive = state->salt_send = 42; // FIXME?????
2226 LOG (GNUNET_ERROR_TYPE_DEBUG,
2227 "Initiating union operation evaluation\n");
2228 GNUNET_STATISTICS_update (_GSS_statistics,
2229 "# of total union operations",
2232 GNUNET_STATISTICS_update (_GSS_statistics,
2233 "# of initiated union operations",
2236 msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
2237 GNUNET_MQ_send (op->mq,
2240 if (NULL != opaque_context)
2241 LOG (GNUNET_ERROR_TYPE_DEBUG,
2242 "sent op request with context message\n");
2244 LOG (GNUNET_ERROR_TYPE_DEBUG,
2245 "sent op request without context message\n");
2248 initialize_key_to_element (op);
2249 state->initial_size = GNUNET_CONTAINER_multihashmap32_size (state->key_to_element);
2255 * Accept an union operation request from a remote peer.
2256 * Only initializes the private operation state.
2258 * @param op operation that will be accepted as a union operation
2260 static struct OperationState *
2261 union_accept (struct Operation *op)
2263 struct OperationState *state;
2264 const struct StrataEstimator *se;
2265 struct GNUNET_MQ_Envelope *ev;
2266 struct StrataEstimatorMessage *strata_msg;
2271 LOG (GNUNET_ERROR_TYPE_DEBUG,
2272 "accepting set union operation\n");
2273 GNUNET_STATISTICS_update (_GSS_statistics,
2274 "# of accepted union operations",
2277 GNUNET_STATISTICS_update (_GSS_statistics,
2278 "# of total union operations",
2282 state = GNUNET_new (struct OperationState);
2283 state->se = strata_estimator_dup (op->set->state->se);
2284 state->demanded_hashes = GNUNET_CONTAINER_multihashmap_create (32,
2286 state->salt_receive = state->salt_send = 42; // FIXME?????
2288 initialize_key_to_element (op);
2289 state->initial_size = GNUNET_CONTAINER_multihashmap32_size (state->key_to_element);
2291 /* kick off the operation */
2293 buf = GNUNET_malloc (se->strata_count * IBF_BUCKET_SIZE * se->ibf_size);
2294 len = strata_estimator_write (se,
2296 if (len < se->strata_count * IBF_BUCKET_SIZE * se->ibf_size)
2297 type = GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SEC;
2299 type = GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE;
2300 ev = GNUNET_MQ_msg_extra (strata_msg,
2303 GNUNET_memcpy (&strata_msg[1],
2307 strata_msg->set_size
2308 = GNUNET_htonll (GNUNET_CONTAINER_multihashmap_size (op->set->content->elements));
2309 GNUNET_MQ_send (op->mq,
2311 state->phase = PHASE_EXPECT_IBF;
2317 * Create a new set supporting the union operation
2319 * We maintain one strata estimator per set and then manipulate it over the
2320 * lifetime of the set, as recreating a strata estimator would be expensive.
2322 * @return the newly created set, NULL on error
2324 static struct SetState *
2325 union_set_create (void)
2327 struct SetState *set_state;
2329 LOG (GNUNET_ERROR_TYPE_DEBUG,
2330 "union set created\n");
2331 set_state = GNUNET_new (struct SetState);
2332 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
2333 SE_IBF_SIZE, SE_IBF_HASH_NUM);
2334 if (NULL == set_state->se)
2336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2337 "Failed to allocate strata estimator\n");
2338 GNUNET_free (set_state);
2346 * Add the element from the given element message to the set.
2348 * @param set_state state of the set want to add to
2349 * @param ee the element to add to the set
2352 union_add (struct SetState *set_state,
2353 struct ElementEntry *ee)
2355 strata_estimator_insert (set_state->se,
2356 get_ibf_key (&ee->element_hash));
2361 * Remove the element given in the element message from the set.
2362 * Only marks the element as removed, so that older set operations can still exchange it.
2364 * @param set_state state of the set to remove from
2365 * @param ee set element to remove
2368 union_remove (struct SetState *set_state,
2369 struct ElementEntry *ee)
2371 strata_estimator_remove (set_state->se,
2372 get_ibf_key (&ee->element_hash));
2377 * Destroy a set that supports the union operation.
2379 * @param set_state the set to destroy
2382 union_set_destroy (struct SetState *set_state)
2384 if (NULL != set_state->se)
2386 strata_estimator_destroy (set_state->se);
2387 set_state->se = NULL;
2389 GNUNET_free (set_state);
2394 * Copy union-specific set state.
2396 * @param state source state for copying the union state
2397 * @return a copy of the union-specific set state
2399 static struct SetState *
2400 union_copy_state (struct SetState *state)
2402 struct SetState *new_state;
2404 GNUNET_assert ( (NULL != state) &&
2405 (NULL != state->se) );
2406 new_state = GNUNET_new (struct SetState);
2407 new_state->se = strata_estimator_dup (state->se);
2414 * Handle case where channel went down for an operation.
2416 * @param op operation that lost the channel
2419 union_channel_death (struct Operation *op)
2421 send_client_done (op);
2422 _GSS_operation_destroy (op,
2428 * Get the table with implementing functions for
2431 * @return the operation specific VTable
2433 const struct SetVT *
2436 static const struct SetVT union_vt = {
2437 .create = &union_set_create,
2439 .remove = &union_remove,
2440 .destroy_set = &union_set_destroy,
2441 .evaluate = &union_evaluate,
2442 .accept = &union_accept,
2443 .cancel = &union_op_cancel,
2444 .copy_state = &union_copy_state,
2445 .channel_death = &union_channel_death