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.
22 * @file set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-set.h"
30 #include "strata_estimator.h"
31 #include "set_protocol.h"
36 * Number of IBFs in a strata estimator.
38 #define SE_STRATA_COUNT 32
40 * Size of the IBFs in the strata estimator.
42 #define SE_IBF_SIZE 80
44 * hash num parameter for the difference digests and strata estimators
46 #define SE_IBF_HASH_NUM 4
49 * Number of buckets that can be transmitted in one message.
51 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
54 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
55 * Choose this value so that computing the IBF is still cheaper
56 * than transmitting all values.
58 #define MAX_IBF_ORDER (16)
61 * Number of buckets used in the ibf per estimated
68 * Current phase we are in for a union operation.
70 enum UnionOperationPhase
73 * 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
84 * Continuation for multi part IBFs.
86 PHASE_EXPECT_IBF_CONT,
88 * We are sending request and elements,
89 * and thus only expect elements from the other peer.
91 * We are currently decoding an IBF until it can no longer be decoded,
92 * we currently send requests and expect elements
93 * The remote peer is in PHASE_EXPECT_ELEMENTS_AND_REQUESTS
95 PHASE_EXPECT_ELEMENTS,
97 * We are expecting elements and requests, and send
98 * requested elements back to the other peer.
100 * We are in this phase if we have SENT an IBF for the remote peer to decode.
101 * We expect requests, send elements or could receive an new IBF, which takes
102 * us via PHASE_EXPECT_IBF to phase PHASE_EXPECT_ELEMENTS
104 * The remote peer is thus in:
105 * PHASE_EXPECT_ELEMENTS
107 PHASE_EXPECT_ELEMENTS_AND_REQUESTS,
109 * The protocol is over.
110 * Results may still have to be sent to the client.
117 * State of an evaluate operation
120 struct OperationState
123 * Number of ibf buckets received
125 unsigned int ibf_buckets_received;
128 * Copy of the set's strata estimator at the time of
129 * creation of this operation
131 struct StrataEstimator *se;
134 * The ibf we currently receive
136 struct InvertibleBloomFilter *remote_ibf;
139 * IBF of the set's element.
141 struct InvertibleBloomFilter *local_ibf;
144 * Maps IBF-Keys (specific to the current salt) to elements.
145 * Used as a multihashmap, the keys being the lower 32bit of the IBF-Key.
146 * Colliding IBF-Keys are linked.
148 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
151 * Iterator for sending elements on the key to element mapping to the client.
153 struct GNUNET_CONTAINER_MultiHashMap32Iterator *full_result_iter;
156 * Current state of the operation.
158 enum UnionOperationPhase phase;
161 * Did we send the client that we are done?
163 int client_done_sent;
168 * The key entry is used to associate an ibf key with
174 * IBF key for the entry, derived from the current salt.
176 struct IBF_Key ibf_key;
179 * The actual element associated with the key.
181 struct ElementEntry *element;
184 * Element that collides with this element
185 * on the ibf key. All colliding entries must have the same ibf key.
187 struct KeyEntry *next_colliding;
192 * Used as a closure for sending elements
193 * with a specific IBF key.
195 struct SendElementClosure
198 * The IBF key whose matching elements should be
201 struct IBF_Key ibf_key;
204 * Operation for which the elements
207 struct Operation *op;
212 * Extra state required for efficient set union.
217 * The strata estimator is only generated once for
219 * The IBF keys are derived from the element hashes with
222 struct StrataEstimator *se;
227 * Iterator over hash map entries.
230 * @param key current key code
231 * @param value value in the hash map
232 * @return #GNUNET_YES if we should continue to
237 destroy_key_to_element_iter (void *cls,
241 struct KeyEntry *k = value;
242 /* destroy the linked list of colliding ibf key entries */
245 struct KeyEntry *k_tmp = k;
246 k = k->next_colliding;
247 if (GNUNET_YES == k_tmp->element->remote)
249 GNUNET_free (k_tmp->element);
250 k_tmp->element = NULL;
259 * Destroy the union operation. Only things specific to the union operation are destroyed.
261 * @param op union operation to destroy
264 union_op_cancel (struct Operation *op)
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op\n");
267 /* check if the op was canceled twice */
268 GNUNET_assert (NULL != op->state);
269 if (NULL != op->state->remote_ibf)
271 ibf_destroy (op->state->remote_ibf);
272 op->state->remote_ibf = NULL;
274 if (NULL != op->state->local_ibf)
276 ibf_destroy (op->state->local_ibf);
277 op->state->local_ibf = NULL;
279 if (NULL != op->state->se)
281 strata_estimator_destroy (op->state->se);
282 op->state->se = NULL;
284 if (NULL != op->state->key_to_element)
286 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element, destroy_key_to_element_iter, NULL);
287 GNUNET_CONTAINER_multihashmap32_destroy (op->state->key_to_element);
288 op->state->key_to_element = NULL;
290 GNUNET_free (op->state);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op done\n");
297 * Inform the client that the union operation has failed,
298 * and proceed to destroy the evaluate operation.
300 * @param op the union operation to fail
303 fail_union_operation (struct Operation *op)
305 struct GNUNET_MQ_Envelope *ev;
306 struct GNUNET_SET_ResultMessage *msg;
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "union operation failed\n");
310 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
311 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
312 msg->request_id = htonl (op->spec->client_request_id);
313 msg->element_type = htons (0);
314 GNUNET_MQ_send (op->spec->set->client_mq, ev);
315 _GSS_operation_destroy (op);
320 * Derive the IBF key from a hash code and
323 * @param src the hash code
324 * @param salt salt to use
325 * @return the derived IBF key
327 static struct IBF_Key
328 get_ibf_key (const struct GNUNET_HashCode *src, uint16_t salt)
332 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
333 GCRY_MD_SHA512, GCRY_MD_SHA256,
335 &salt, sizeof (salt),
342 * Send a request for the evaluate operation to a remote peer
344 * @param op operation with the other peer
347 send_operation_request (struct Operation *op)
349 struct GNUNET_MQ_Envelope *ev;
350 struct OperationRequestMessage *msg;
352 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
353 op->spec->context_msg);
357 /* the context message is too large */
359 GNUNET_SERVER_client_disconnect (op->spec->set->client);
362 msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
363 msg->app_id = op->spec->app_id;
364 msg->salt = htonl (op->spec->salt);
365 GNUNET_MQ_send (op->mq, ev);
367 if (NULL != op->spec->context_msg)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
372 if (NULL != op->spec->context_msg)
374 GNUNET_free (op->spec->context_msg);
375 op->spec->context_msg = NULL;
381 * Iterator to create the mapping between ibf keys
382 * and element entries.
385 * @param key current key code
386 * @param value value in the hash map
387 * @return #GNUNET_YES if we should continue to
392 op_register_element_iterator (void *cls,
396 struct KeyEntry *const new_k = cls;
397 struct KeyEntry *old_k = value;
399 GNUNET_assert (NULL != old_k);
400 /* check if our ibf key collides with the ibf key in the existing entry */
401 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
403 /* insert the the new key in the collision chain */
404 new_k->next_colliding = old_k->next_colliding;
405 old_k->next_colliding = new_k;
406 /* signal to the caller that we were able to insert into a colliding bucket */
414 * Iterator to create the mapping between ibf keys
415 * and element entries.
418 * @param key current key code
419 * @param value value in the hash map
420 * @return #GNUNET_YES if we should continue to
425 op_has_element_iterator (void *cls,
429 struct GNUNET_HashCode *element_hash = cls;
430 struct KeyEntry *k = value;
432 GNUNET_assert (NULL != k);
435 if (0 == GNUNET_CRYPTO_hash_cmp (&k->element->element_hash, element_hash))
437 k = k->next_colliding;
444 * Determine whether the given element is already in the operation's element
447 * @param op operation that should be tested for 'element_hash'
448 * @param element_hash hash of the element to look for
449 * @return #GNUNET_YES if the element has been found, #GNUNET_NO otherwise
452 op_has_element (struct Operation *op, const struct GNUNET_HashCode *element_hash)
455 struct IBF_Key ibf_key;
457 ibf_key = get_ibf_key (element_hash, op->spec->salt);
458 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
459 (uint32_t) ibf_key.key_val,
460 op_has_element_iterator, (void *) element_hash);
462 /* was the iteration aborted because we found the element? */
463 if (GNUNET_SYSERR == ret)
470 * Insert an element into the union operation's
471 * key-to-element mapping. Takes ownership of 'ee'.
472 * Note that this does not insert the element in the set,
473 * only in the operation's key-element mapping.
474 * This is done to speed up re-tried operations, if some elements
475 * were transmitted, and then the IBF fails to decode.
477 * @param op the union operation
478 * @param ee the element entry
481 op_register_element (struct Operation *op, struct ElementEntry *ee)
484 struct IBF_Key ibf_key;
487 ibf_key = get_ibf_key (&ee->element_hash, op->spec->salt);
488 k = GNUNET_new (struct KeyEntry);
490 k->ibf_key = ibf_key;
491 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element,
492 (uint32_t) ibf_key.key_val,
493 op_register_element_iterator, k);
495 /* was the element inserted into a colliding bucket? */
496 if (GNUNET_SYSERR == ret)
499 GNUNET_CONTAINER_multihashmap32_put (op->state->key_to_element, (uint32_t) ibf_key.key_val, k,
500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
505 * Insert a key into an ibf.
509 * @param value the key entry to get the key from
512 prepare_ibf_iterator (void *cls,
516 struct InvertibleBloomFilter *ibf = cls;
517 struct KeyEntry *ke = value;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inserting %x into ibf\n", ke->ibf_key.key_val);
521 ibf_insert (ibf, ke->ibf_key);
527 * Iterator for initializing the
528 * key-to-element mapping of a union operation
530 * @param cls the union operation
532 * @param value the element entry to insert
533 * into the key-to-element mapping
534 * @return GNUNET_YES to continue iterating,
538 init_key_to_element_iterator (void *cls,
539 const struct GNUNET_HashCode *key,
542 struct Operation *op = cls;
543 struct ElementEntry *e = value;
545 /* make sure that the element belongs to the set at the time
546 * of creating the operation */
547 if ( (e->generation_added > op->generation_created) ||
548 ( (GNUNET_YES == e->removed) &&
549 (e->generation_removed < op->generation_created)))
552 GNUNET_assert (GNUNET_NO == e->remote);
554 op_register_element (op, e);
560 * Create an ibf with the operation's elements
561 * of the specified size
563 * @param op the union operation
564 * @param size size of the ibf to create
567 prepare_ibf (struct Operation *op, uint16_t size)
569 if (NULL == op->state->key_to_element)
572 len = GNUNET_CONTAINER_multihashmap_size (op->spec->set->elements);
573 op->state->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
574 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
575 init_key_to_element_iterator, op);
577 if (NULL != op->state->local_ibf)
578 ibf_destroy (op->state->local_ibf);
579 op->state->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
580 GNUNET_CONTAINER_multihashmap32_iterate (op->state->key_to_element,
581 prepare_ibf_iterator, op->state->local_ibf);
586 * Send an ibf of appropriate size.
588 * @param op the union operation
589 * @param ibf_order order of the ibf to send, size=2^order
592 send_ibf (struct Operation *op, uint16_t ibf_order)
594 unsigned int buckets_sent = 0;
595 struct InvertibleBloomFilter *ibf;
597 prepare_ibf (op, 1<<ibf_order);
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending ibf of size %u\n", 1<<ibf_order);
601 ibf = op->state->local_ibf;
603 while (buckets_sent < (1 << ibf_order))
605 unsigned int buckets_in_message;
606 struct GNUNET_MQ_Envelope *ev;
607 struct IBFMessage *msg;
609 buckets_in_message = (1 << ibf_order) - buckets_sent;
610 /* limit to maximum */
611 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
612 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
614 ev = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
615 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF);
617 msg->order = ibf_order;
618 msg->offset = htons (buckets_sent);
619 ibf_write_slice (ibf, buckets_sent,
620 buckets_in_message, &msg[1]);
621 buckets_sent += buckets_in_message;
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ibf chunk size %u, %u/%u sent\n",
623 buckets_in_message, buckets_sent, 1<<ibf_order);
624 GNUNET_MQ_send (op->mq, ev);
627 op->state->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
632 * Send a strata estimator to the remote peer.
634 * @param op the union operation with the remote peer
637 send_strata_estimator (struct Operation *op)
639 struct GNUNET_MQ_Envelope *ev;
640 struct GNUNET_MessageHeader *strata_msg;
642 ev = GNUNET_MQ_msg_header_extra (strata_msg,
643 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
644 GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE);
645 strata_estimator_write (op->state->se, &strata_msg[1]);
646 GNUNET_MQ_send (op->mq, ev);
647 op->state->phase = PHASE_EXPECT_IBF;
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent SE, expecting IBF\n");
653 * Compute the necessary order of an ibf
654 * from the size of the symmetric set difference.
656 * @param diff the difference
657 * @return the required size of the ibf
660 get_order_from_difference (unsigned int diff)
662 unsigned int ibf_order;
665 while ((1<<ibf_order) < (IBF_ALPHA * diff) || (1<<ibf_order) < SE_IBF_HASH_NUM)
667 if (ibf_order > MAX_IBF_ORDER)
668 ibf_order = MAX_IBF_ORDER;
674 * Handle a strata estimator from a remote peer
676 * @param cls the union operation
677 * @param mh the message
680 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
682 struct Operation *op = cls;
683 struct StrataEstimator *remote_se;
686 if (op->state->phase != PHASE_EXPECT_SE)
688 fail_union_operation (op);
692 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
694 strata_estimator_read (&mh[1], remote_se);
695 GNUNET_assert (NULL != op->state->se);
696 diff = strata_estimator_difference (remote_se, op->state->se);
697 strata_estimator_destroy (remote_se);
698 strata_estimator_destroy (op->state->se);
699 op->state->se = NULL;
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se diff=%d, using ibf size %d\n",
701 diff, 1<<get_order_from_difference (diff));
702 send_ibf (op, get_order_from_difference (diff));
708 * Iterator to send elements to a remote peer
710 * @param cls closure with the element key and the union operation
712 * @param value the key entry
715 send_element_iterator (void *cls,
719 struct SendElementClosure *sec = cls;
720 struct IBF_Key ibf_key = sec->ibf_key;
721 struct Operation *op = sec->op;
722 struct KeyEntry *ke = value;
724 if (ke->ibf_key.key_val != ibf_key.key_val)
728 const struct GNUNET_SET_Element *const element = &ke->element->element;
729 struct GNUNET_MQ_Envelope *ev;
730 struct GNUNET_MessageHeader *mh;
732 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
733 ev = GNUNET_MQ_msg_header_extra (mh, element->size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
736 /* element too large */
740 memcpy (&mh[1], element->data, element->size);
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (%s) to peer\n",
742 GNUNET_h2s (&ke->element->element_hash));
743 GNUNET_MQ_send (op->mq, ev);
744 ke = ke->next_colliding;
750 * Send all elements that have the specified IBF key
751 * to the remote peer of the union operation
753 * @param op union operation
754 * @param ibf_key IBF key of interest
757 send_elements_for_key (struct Operation *op, struct IBF_Key ibf_key)
759 struct SendElementClosure send_cls;
761 send_cls.ibf_key = ibf_key;
763 GNUNET_CONTAINER_multihashmap32_get_multiple (op->state->key_to_element, (uint32_t) ibf_key.key_val,
764 &send_element_iterator, &send_cls);
769 * Decode which elements are missing on each side, and
770 * send the appropriate elemens and requests
772 * @param op union operation
775 decode_and_send (struct Operation *op)
778 struct IBF_Key last_key;
780 unsigned int num_decoded;
781 struct InvertibleBloomFilter *diff_ibf;
783 GNUNET_assert (PHASE_EXPECT_ELEMENTS == op->state->phase);
785 prepare_ibf (op, op->state->remote_ibf->size);
786 diff_ibf = ibf_dup (op->state->local_ibf);
787 ibf_subtract (diff_ibf, op->state->remote_ibf);
789 ibf_destroy (op->state->remote_ibf);
790 op->state->remote_ibf = NULL;
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoding IBF (size=%u)\n", diff_ibf->size);
795 last_key.key_val = 0;
800 int cycle_detected = GNUNET_NO;
804 res = ibf_decode (diff_ibf, &side, &key);
805 if (res == GNUNET_OK)
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoded ibf key %lx\n",
810 if (num_decoded > diff_ibf->size || (num_decoded > 1 && last_key.key_val == key.key_val))
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "detected cyclic ibf (decoded %u/%u)\n",
813 num_decoded, diff_ibf->size);
814 cycle_detected = GNUNET_YES;
817 if ((GNUNET_SYSERR == res) || (GNUNET_YES == cycle_detected))
821 while (1<<next_order < diff_ibf->size)
824 if (next_order <= MAX_IBF_ORDER)
826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 "decoding failed, sending larger ibf (size %u)\n",
829 send_ibf (op, next_order);
833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
834 "set union failed: reached ibf limit\n");
838 if (GNUNET_NO == res)
840 struct GNUNET_MQ_Envelope *ev;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmitted all values, sending DONE\n");
843 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
844 GNUNET_MQ_send (op->mq, ev);
849 send_elements_for_key (op, key);
853 struct GNUNET_MQ_Envelope *ev;
854 struct GNUNET_MessageHeader *msg;
856 /* It may be nice to merge multiple requests, but with mesh's corking it is not worth
857 * the effort additional complexity. */
858 ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
859 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
861 *(struct IBF_Key *) &msg[1] = key;
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element request\n");
863 GNUNET_MQ_send (op->mq, ev);
870 ibf_destroy (diff_ibf);
875 * Handle an IBF message from a remote peer.
877 * @param cls the union operation
878 * @param mh the header of the message
881 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
883 struct Operation *op = cls;
884 struct IBFMessage *msg = (struct IBFMessage *) mh;
885 unsigned int buckets_in_message;
887 if ( (op->state->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
888 (op->state->phase == PHASE_EXPECT_IBF) )
890 op->state->phase = PHASE_EXPECT_IBF_CONT;
891 GNUNET_assert (NULL == op->state->remote_ibf);
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "creating new ibf of size %u\n", 1<<msg->order);
893 op->state->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
894 op->state->ibf_buckets_received = 0;
895 if (0 != ntohs (msg->offset))
898 fail_union_operation (op);
902 else if (op->state->phase == PHASE_EXPECT_IBF_CONT)
904 if ( (ntohs (msg->offset) != op->state->ibf_buckets_received) ||
905 (1<<msg->order != op->state->remote_ibf->size) )
908 fail_union_operation (op);
913 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
915 if (0 == buckets_in_message)
918 fail_union_operation (op);
922 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
925 fail_union_operation (op);
929 ibf_read_slice (&msg[1], op->state->ibf_buckets_received, buckets_in_message, op->state->remote_ibf);
930 op->state->ibf_buckets_received += buckets_in_message;
932 if (op->state->ibf_buckets_received == op->state->remote_ibf->size)
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received full ibf\n");
935 op->state->phase = PHASE_EXPECT_ELEMENTS;
936 decode_and_send (op);
942 * Send a result message to the client indicating
943 * that there is a new element.
945 * @param op union operation
946 * @param element element to send
949 send_client_element (struct Operation *op,
950 struct GNUNET_SET_Element *element)
952 struct GNUNET_MQ_Envelope *ev;
953 struct GNUNET_SET_ResultMessage *rm;
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client\n", element->size);
956 GNUNET_assert (0 != op->spec->client_request_id);
957 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
960 GNUNET_MQ_discard (ev);
964 rm->result_status = htons (GNUNET_SET_STATUS_OK);
965 rm->request_id = htonl (op->spec->client_request_id);
966 rm->element_type = element->type;
967 memcpy (&rm[1], element->data, element->size);
968 GNUNET_MQ_send (op->spec->set->client_mq, ev);
973 * Signal to the client that the operation has finished and
974 * destroy the operation.
976 * @param cls operation to destroy
979 send_done_and_destroy (void *cls)
981 struct Operation *op = cls;
982 struct GNUNET_MQ_Envelope *ev;
983 struct GNUNET_SET_ResultMessage *rm;
984 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
985 rm->request_id = htonl (op->spec->client_request_id);
986 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
987 rm->element_type = htons (0);
988 GNUNET_MQ_send (op->spec->set->client_mq, ev);
989 _GSS_operation_destroy (op);
994 * Send all remaining elements in the full result iterator.
996 * @param cls operation
999 send_remaining_elements (void *cls)
1001 struct Operation *op = cls;
1002 struct KeyEntry *ke;
1005 res = GNUNET_CONTAINER_multihashmap32_iterator_next (op->state->full_result_iter, NULL, (const void **) &ke);
1006 if (GNUNET_NO == res)
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending done and destroy because iterator ran out\n");
1009 send_done_and_destroy (op);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending elements from key entry\n");
1017 struct GNUNET_MQ_Envelope *ev;
1018 struct GNUNET_SET_ResultMessage *rm;
1019 struct GNUNET_SET_Element *element;
1020 element = &ke->element->element;
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client (full set)\n", element->size);
1023 GNUNET_assert (0 != op->spec->client_request_id);
1024 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
1027 GNUNET_MQ_discard (ev);
1031 rm->result_status = htons (GNUNET_SET_STATUS_OK);
1032 rm->request_id = htonl (op->spec->client_request_id);
1033 rm->element_type = element->type;
1034 memcpy (&rm[1], element->data, element->size);
1035 if (ke->next_colliding == NULL)
1037 GNUNET_MQ_notify_sent (ev, send_remaining_elements, op);
1038 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1041 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1042 ke = ke->next_colliding;
1048 * Send a result message to the client indicating
1049 * that the operation is over.
1050 * After the result done message has been sent to the client,
1051 * destroy the evaluate operation.
1053 * @param op union operation
1056 finish_and_destroy (struct Operation *op)
1058 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1060 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending full result set\n");
1063 GNUNET_assert (NULL == op->state->full_result_iter);
1064 op->state->full_result_iter =
1065 GNUNET_CONTAINER_multihashmap32_iterator_create (op->state->key_to_element);
1066 send_remaining_elements (op);
1069 send_done_and_destroy (op);
1074 * Handle an element message from a remote peer.
1076 * @param cls the union operation
1077 * @param mh the message
1080 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
1082 struct Operation *op = cls;
1083 struct ElementEntry *ee;
1084 uint16_t element_size;
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got element from peer\n");
1088 if ( (op->state->phase != PHASE_EXPECT_ELEMENTS) &&
1089 (op->state->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1091 fail_union_operation (op);
1095 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1096 ee = GNUNET_malloc (sizeof *ee + element_size);
1097 memcpy (&ee[1], &mh[1], element_size);
1098 ee->element.size = element_size;
1099 ee->element.data = &ee[1];
1100 ee->remote = GNUNET_YES;
1101 GNUNET_CRYPTO_hash (ee->element.data, ee->element.size, &ee->element_hash);
1103 if (GNUNET_YES == op_has_element (op, &ee->element_hash))
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got existing element from peer\n");
1110 op_register_element (op, ee);
1111 /* only send results immediately if the client wants it */
1112 if (GNUNET_SET_RESULT_ADDED == op->spec->result_mode)
1113 send_client_element (op, &ee->element);
1118 * Handle an element request from a remote peer.
1120 * @param cls the union operation
1121 * @param mh the message
1124 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1126 struct Operation *op = cls;
1127 struct IBF_Key *ibf_key;
1128 unsigned int num_keys;
1130 /* look up elements and send them */
1131 if (op->state->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1134 fail_union_operation (op);
1138 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1140 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1143 fail_union_operation (op);
1147 ibf_key = (struct IBF_Key *) &mh[1];
1148 while (0 != num_keys--)
1150 send_elements_for_key (op, *ibf_key);
1157 * Handle a done message from a remote peer
1159 * @param cls the union operation
1160 * @param mh the message
1163 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1165 struct Operation *op = cls;
1166 struct GNUNET_MQ_Envelope *ev;
1168 if (op->state->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1170 /* we got all requests, but still have to send our elements as response */
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got DONE, sending final DONE after elements\n");
1173 op->state->phase = PHASE_FINISHED;
1174 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1175 GNUNET_MQ_send (op->mq, ev);
1178 if (op->state->phase == PHASE_EXPECT_ELEMENTS)
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
1181 op->state->phase = PHASE_FINISHED;
1182 finish_and_destroy (op);
1186 fail_union_operation (op);
1191 * Evaluate a union operation with
1194 * @param op operation to evaluate
1197 union_evaluate (struct Operation *op)
1199 op->state = GNUNET_new (struct OperationState);
1200 // copy the current generation's strata estimator for this operation
1201 op->state->se = strata_estimator_dup (op->spec->set->state->se);
1202 /* we started the operation, thus we have to send the operation request */
1203 op->state->phase = PHASE_EXPECT_SE;
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "evaluating union operation");
1205 send_operation_request (op);
1210 * Accept an union operation request from a remote peer.
1211 * Only initializes the private operation state.
1213 * @param op operation that will be accepted as a union operation
1216 union_accept (struct Operation *op)
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
1219 op->state = GNUNET_new (struct OperationState);
1220 op->state->se = strata_estimator_dup (op->spec->set->state->se);
1221 /* kick off the operation */
1222 send_strata_estimator (op);
1227 * Create a new set supporting the union operation
1229 * We maintain one strata estimator per set and then manipulate it over the
1230 * lifetime of the set, as recreating a strata estimator would be expensive.
1232 * @return the newly created set
1234 static struct SetState *
1235 union_set_create (void)
1237 struct SetState *set_state;
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "union set created\n");
1241 set_state = GNUNET_new (struct SetState);
1242 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
1243 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1249 * Add the element from the given element message to the set.
1251 * @param set_state state of the set want to add to
1252 * @param ee the element to add to the set
1255 union_add (struct SetState *set_state, struct ElementEntry *ee)
1257 strata_estimator_insert (set_state->se, get_ibf_key (&ee->element_hash, 0));
1262 * Remove the element given in the element message from the set.
1263 * Only marks the element as removed, so that older set operations can still exchange it.
1265 * @param set_state state of the set to remove from
1266 * @param ee set element to remove
1269 union_remove (struct SetState *set_state, struct ElementEntry *ee)
1271 strata_estimator_remove (set_state->se, get_ibf_key (&ee->element_hash, 0));
1276 * Destroy a set that supports the union operation.
1278 * @param set_state the set to destroy
1281 union_set_destroy (struct SetState *set_state)
1283 if (NULL != set_state->se)
1285 strata_estimator_destroy (set_state->se);
1286 set_state->se = NULL;
1288 GNUNET_free (set_state);
1293 * Dispatch messages for a union operation.
1295 * @param op the state of the union evaluate operation
1296 * @param mh the received message
1297 * @return GNUNET_SYSERR if the tunnel should be disconnected,
1298 * GNUNET_OK otherwise
1301 union_handle_p2p_message (struct Operation *op,
1302 const struct GNUNET_MessageHeader *mh)
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
1305 ntohs (mh->type), ntohs (mh->size));
1306 switch (ntohs (mh->type))
1308 case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_IBF:
1309 handle_p2p_ibf (op, mh);
1311 case GNUNET_MESSAGE_TYPE_SET_UNION_P2P_SE:
1312 handle_p2p_strata_estimator (op, mh);
1314 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1315 handle_p2p_elements (op, mh);
1317 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1318 handle_p2p_element_requests (op, mh);
1320 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
1321 handle_p2p_done (op, mh);
1324 /* something wrong with mesh's message handlers? */
1331 * handler for peer-disconnects, notifies the client
1332 * about the aborted operation in case the op was not concluded
1334 * @param op the destroyed operation
1337 union_peer_disconnect (struct Operation *op)
1339 if (PHASE_FINISHED != op->state->phase)
1341 struct GNUNET_MQ_Envelope *ev;
1342 struct GNUNET_SET_ResultMessage *msg;
1344 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
1345 msg->request_id = htonl (op->spec->client_request_id);
1346 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1347 msg->element_type = htons (0);
1348 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
1350 _GSS_operation_destroy (op);
1353 // else: the session has already been concluded
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
1355 if (GNUNET_NO == op->state->client_done_sent)
1356 finish_and_destroy (op);
1361 * Get the table with implementing functions for
1364 * @return the operation specific VTable
1366 const struct SetVT *
1369 static const struct SetVT union_vt = {
1370 .create = &union_set_create,
1371 .msg_handler = &union_handle_p2p_message,
1373 .remove = &union_remove,
1374 .destroy_set = &union_set_destroy,
1375 .evaluate = &union_evaluate,
1376 .accept = &union_accept,
1377 .peer_disconnect = &union_peer_disconnect,
1378 .cancel = &union_op_cancel,