2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
28 #include "gnunet-service-set.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_crypto_lib.h"
32 #include "strata_estimator.h"
33 #include "set_protocol.h"
38 * Number of IBFs in a strata estimator.
40 #define SE_STRATA_COUNT 32
42 * Size of the IBFs in the strata estimator.
44 #define SE_IBF_SIZE 80
46 * hash num parameter for the difference digests and strata estimators
48 #define SE_IBF_HASH_NUM 3
51 * Number of buckets that can be transmitted in one message.
53 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
56 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
57 * Choose this value so that computing the IBF is still cheaper
58 * than transmitting all values.
60 #define MAX_IBF_ORDER (16)
63 * Number of buckets used in the ibf per estimated
70 * Current phase we are in for a union operation.
72 enum UnionOperationPhase
75 * We sent the request message, and expect a strata estimator
79 * We sent the strata estimator, and expect an IBF
83 * We know what type of IBF the other peer wants to send us,
84 * and expect the remaining parts
86 PHASE_EXPECT_IBF_CONT,
88 * We are sending request and elements,
89 * and thus only expect elements from the other peer.
91 PHASE_EXPECT_ELEMENTS,
93 * We are expecting elements and requests, and send
94 * requested elements back to the other peer.
96 PHASE_EXPECT_ELEMENTS_AND_REQUESTS,
98 * The protocol is over.
99 * Results may still have to be sent to the client.
106 * State of an evaluate operation
109 struct OperationState
112 * Tunnel to the remote peer.
114 struct GNUNET_MESH_Tunnel *tunnel;
117 * Detail information about the set operation,
118 * including the set to use.
120 struct OperationSpecification *spec;
123 * Message queue for the peer.
125 struct GNUNET_MQ_Handle *mq;
128 * Number of ibf buckets received
130 unsigned int ibf_buckets_received;
133 * Copy of the set's strata estimator at the time of
134 * creation of this operation
136 struct StrataEstimator *se;
139 * The ibf we currently receive
141 struct InvertibleBloomFilter *remote_ibf;
144 * IBF of the set's element.
146 struct InvertibleBloomFilter *local_ibf;
149 * Maps IBF-Keys (specific to the current salt) to elements.
151 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
154 * Current state of the operation.
156 enum UnionOperationPhase phase;
159 * Generation in which the operation handle
162 unsigned int generation_created;
165 * Set state of the set that this operation
168 struct SetState *set_state;
171 * Evaluate operations are held in
174 struct OperationState *next;
177 * Evaluate operations are held in
180 struct OperationState *prev;
185 * Information about an element element in the set.
186 * All elements are stored in a hash-table
187 * from their hash-code to their 'struct Element',
188 * so that the remove and add operations are reasonably
194 * The actual element. The data for the element
195 * should be allocated at the end of this struct.
197 struct GNUNET_SET_Element element;
200 * Hash of the element.
201 * Will be used to derive the different IBF keys
202 * for different salts.
204 struct GNUNET_HashCode element_hash;
207 * Generation the element was added by the client.
208 * Operations of earlier generations will not consider the element.
210 unsigned int generation_added;
213 * GNUNET_YES if the element has been removed in some generation.
218 * Generation the element was removed by the client.
219 * Operations of later generations will not consider the element.
220 * Only valid if is_removed is GNUNET_YES.
222 unsigned int generation_removed;
225 * GNUNET_YES if the element is a remote element, and does not belong
226 * to the operation's set.
233 * The key entry is used to associate an ibf key with
239 * IBF key for the entry, derived from the current salt.
241 struct IBF_Key ibf_key;
244 * The actual element associated with the key
246 struct ElementEntry *element;
249 * Element that collides with this element
252 struct KeyEntry *next_colliding;
257 * Used as a closure for sending elements
258 * with a specific IBF key.
260 struct SendElementClosure
263 * The IBF key whose matching elements should be
266 struct IBF_Key ibf_key;
269 * Operation for which the elements
272 struct OperationState *eo;
277 * Extra state required for efficient set union.
282 * The strata estimator is only generated once for
284 * The IBF keys are derived from the element hashes with
287 struct StrataEstimator *se;
290 * Maps 'struct GNUNET_HashCode' to 'struct ElementEntry'.
292 struct GNUNET_CONTAINER_MultiHashMap *elements;
295 * Evaluate operations are held in
298 struct OperationState *ops_head;
301 * Evaluate operations are held in
304 struct OperationState *ops_tail;
307 * Current generation, that is, number of
308 * previously executed operations on this set
310 unsigned int current_generation;
316 * Iterator over hash map entries.
319 * @param key current key code
320 * @param value value in the hash map
321 * @return GNUNET_YES if we should continue to
326 destroy_elements_iterator (void *cls,
327 const struct GNUNET_HashCode * key,
330 struct ElementEntry *ee = value;
338 * Iterator over hash map entries.
341 * @param key current key code
342 * @param value value in the hash map
343 * @return GNUNET_YES if we should continue to
348 destroy_key_to_element_iter (void *cls,
352 struct KeyEntry *k = value;
356 struct KeyEntry *k_tmp = k;
357 k = k->next_colliding;
358 if (GNUNET_YES == k_tmp->element->remote)
360 GNUNET_free (k_tmp->element);
361 k_tmp->element = NULL;
370 * Destroy a union operation, and free all resources
371 * associated with it.
373 * @param eo the union operation to destroy
376 union_operation_destroy (struct OperationState *eo)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op\n");
379 GNUNET_CONTAINER_DLL_remove (eo->set_state->ops_head,
380 eo->set_state->ops_tail,
384 GNUNET_MQ_destroy (eo->mq);
387 if (NULL != eo->tunnel)
389 struct GNUNET_MESH_Tunnel *t = eo->tunnel;
391 GNUNET_MESH_tunnel_destroy (t);
393 if (NULL != eo->remote_ibf)
395 ibf_destroy (eo->remote_ibf);
396 eo->remote_ibf = NULL;
398 if (NULL != eo->local_ibf)
400 ibf_destroy (eo->local_ibf);
401 eo->local_ibf = NULL;
405 strata_estimator_destroy (eo->se);
408 if (NULL != eo->key_to_element)
410 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element, destroy_key_to_element_iter, NULL);
411 GNUNET_CONTAINER_multihashmap32_destroy (eo->key_to_element);
412 eo->key_to_element = NULL;
414 if (NULL != eo->spec)
416 if (NULL != eo->spec->context_msg)
418 GNUNET_free (eo->spec->context_msg);
419 eo->spec->context_msg = NULL;
421 GNUNET_free (eo->spec);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op done\n");
428 /* FIXME: do a garbage collection of the set generations */
433 * Inform the client that the union operation has failed,
434 * and proceed to destroy the evaluate operation.
436 * @param eo the union operation to fail
439 fail_union_operation (struct OperationState *eo)
441 struct GNUNET_MQ_Envelope *ev;
442 struct GNUNET_SET_ResultMessage *msg;
444 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
445 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
446 msg->request_id = htonl (eo->spec->client_request_id);
447 msg->element_type = htons (0);
448 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
449 union_operation_destroy (eo);
454 * Derive the IBF key from a hash code and
457 * @param src the hash code
458 * @param salt salt to use
459 * @return the derived IBF key
461 static struct IBF_Key
462 get_ibf_key (struct GNUNET_HashCode *src, uint16_t salt)
466 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
467 GCRY_MD_SHA512, GCRY_MD_SHA256,
469 &salt, sizeof (salt),
476 * Send a request for the evaluate operation to a remote peer
478 * @param eo operation with the other peer
481 send_operation_request (struct OperationState *eo)
483 struct GNUNET_MQ_Envelope *ev;
484 struct OperationRequestMessage *msg;
486 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
487 eo->spec->context_msg);
491 /* the context message is too large */
493 GNUNET_SERVER_client_disconnect (eo->spec->set->client);
496 msg->operation = htons (GNUNET_SET_OPERATION_UNION);
497 msg->app_id = eo->spec->app_id;
498 msg->salt = htonl (eo->spec->salt);
499 GNUNET_MQ_send (eo->mq, ev);
501 if (NULL != eo->spec->context_msg)
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
506 if (NULL != eo->spec->context_msg)
508 GNUNET_free (eo->spec->context_msg);
509 eo->spec->context_msg = NULL;
516 * Iterator to create the mapping between ibf keys
517 * and element entries.
520 * @param key current key code
521 * @param value value in the hash map
522 * @return GNUNET_YES if we should continue to
527 insert_element_iterator (void *cls,
531 struct KeyEntry *const new_k = cls;
532 struct KeyEntry *old_k = value;
534 GNUNET_assert (NULL != old_k);
537 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
539 new_k->next_colliding = old_k->next_colliding;
540 old_k->next_colliding = new_k;
543 old_k = old_k->next_colliding;
544 } while (NULL != old_k);
550 * Insert an element into the union operation's
551 * key-to-element mapping. Takes ownership of 'ee'.
553 * @param eo the union operation
554 * @param ee the element entry
557 insert_element (struct OperationState *eo, struct ElementEntry *ee)
560 struct IBF_Key ibf_key;
563 ibf_key = get_ibf_key (&ee->element_hash, eo->spec->salt);
564 k = GNUNET_new (struct KeyEntry);
566 k->ibf_key = ibf_key;
567 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element,
568 (uint32_t) ibf_key.key_val,
569 insert_element_iterator, k);
571 /* was the element inserted into a colliding bucket? */
572 if (GNUNET_SYSERR == ret)
575 GNUNET_CONTAINER_multihashmap32_put (eo->key_to_element, (uint32_t) ibf_key.key_val, k,
576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
581 * Insert a key into an ibf.
585 * @param value the key entry to get the key from
588 prepare_ibf_iterator (void *cls,
592 struct InvertibleBloomFilter *ibf = cls;
593 struct KeyEntry *ke = value;
595 ibf_insert (ibf, ke->ibf_key);
601 * Iterator for initializing the
602 * key-to-element mapping of a union operation
604 * @param cls the union operation
606 * @param value the element entry to insert
607 * into the key-to-element mapping
610 init_key_to_element_iterator (void *cls,
611 const struct GNUNET_HashCode *key,
614 struct OperationState *eo = cls;
615 struct ElementEntry *e = value;
617 /* make sure that the element belongs to the set at the time
618 * of creating the operation */
619 if ( (e->generation_added > eo->generation_created) ||
620 ( (GNUNET_YES == e->removed) &&
621 (e->generation_removed < eo->generation_created)))
624 e->remote = GNUNET_NO;
626 insert_element (eo, e);
632 * Create an ibf with the operation's elements
633 * of the specified size
635 * @param eo the union operation
636 * @param size size of the ibf to create
639 prepare_ibf (struct OperationState *eo, uint16_t size)
641 if (NULL == eo->key_to_element)
644 len = GNUNET_CONTAINER_multihashmap_size (eo->set_state->elements);
645 eo->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
646 GNUNET_CONTAINER_multihashmap_iterate (eo->set_state->elements,
647 init_key_to_element_iterator, eo);
649 if (NULL != eo->local_ibf)
650 ibf_destroy (eo->local_ibf);
651 eo->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
652 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element,
653 prepare_ibf_iterator, eo->local_ibf);
658 * Send an ibf of appropriate size.
660 * @param eo the union operation
661 * @param ibf_order order of the ibf to send, size=2^order
664 send_ibf (struct OperationState *eo, uint16_t ibf_order)
666 unsigned int buckets_sent = 0;
667 struct InvertibleBloomFilter *ibf;
669 prepare_ibf (eo, 1<<ibf_order);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending ibf of size %u\n", 1<<ibf_order);
675 while (buckets_sent < (1 << ibf_order))
677 unsigned int buckets_in_message;
678 struct GNUNET_MQ_Envelope *ev;
679 struct IBFMessage *msg;
681 buckets_in_message = (1 << ibf_order) - buckets_sent;
682 /* limit to maximum */
683 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
684 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
686 ev = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
687 GNUNET_MESSAGE_TYPE_SET_P2P_IBF);
689 msg->order = ibf_order;
690 msg->offset = htons (buckets_sent);
691 ibf_write_slice (ibf, buckets_sent,
692 buckets_in_message, &msg[1]);
693 buckets_sent += buckets_in_message;
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ibf chunk size %u, %u/%u sent\n",
695 buckets_in_message, buckets_sent, 1<<ibf_order);
696 GNUNET_MQ_send (eo->mq, ev);
699 eo->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
704 * Send a strata estimator to the remote peer.
706 * @param eo the union operation with the remote peer
709 send_strata_estimator (struct OperationState *eo)
711 struct GNUNET_MQ_Envelope *ev;
712 struct GNUNET_MessageHeader *strata_msg;
714 ev = GNUNET_MQ_msg_header_extra (strata_msg,
715 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
716 GNUNET_MESSAGE_TYPE_SET_P2P_SE);
717 strata_estimator_write (eo->set_state->se, &strata_msg[1]);
718 GNUNET_MQ_send (eo->mq, ev);
719 eo->phase = PHASE_EXPECT_IBF;
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent SE, expecting IBF\n");
725 * Compute the necessary order of an ibf
726 * from the size of the symmetric set difference.
728 * @param diff the difference
729 * @return the required size of the ibf
732 get_order_from_difference (unsigned int diff)
734 unsigned int ibf_order;
737 while ((1<<ibf_order) < (IBF_ALPHA * diff))
739 if (ibf_order > MAX_IBF_ORDER)
740 ibf_order = MAX_IBF_ORDER;
746 * Handle a strata estimator from a remote peer
748 * @param cls the union operation
749 * @param mh the message
752 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
754 struct OperationState *eo = cls;
755 struct StrataEstimator *remote_se;
758 if (eo->phase != PHASE_EXPECT_SE)
760 fail_union_operation (eo);
764 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
766 strata_estimator_read (&mh[1], remote_se);
767 GNUNET_assert (NULL != eo->se);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se, calculating diff\n");
769 diff = strata_estimator_difference (remote_se, eo->se);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "se diff=%d\n", diff);
771 strata_estimator_destroy (remote_se);
772 strata_estimator_destroy (eo->se);
774 send_ibf (eo, get_order_from_difference (diff));
780 * Iterator to send elements to a remote peer
782 * @param cls closure with the element key and the union operation
784 * @param value the key entry
787 send_element_iterator (void *cls,
791 struct SendElementClosure *sec = cls;
792 struct IBF_Key ibf_key = sec->ibf_key;
793 struct OperationState *eo = sec->eo;
794 struct KeyEntry *ke = value;
796 if (ke->ibf_key.key_val != ibf_key.key_val)
800 const struct GNUNET_SET_Element *const element = &ke->element->element;
801 struct GNUNET_MQ_Envelope *ev;
802 struct GNUNET_MessageHeader *mh;
804 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
805 ev = GNUNET_MQ_msg_header_extra (mh, element->size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
808 /* element too large */
812 memcpy (&mh[1], element->data, element->size);
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (%s) to peer\n",
814 GNUNET_h2s (&ke->element->element_hash));
815 GNUNET_MQ_send (eo->mq, ev);
816 ke = ke->next_colliding;
822 * Send all elements that have the specified IBF key
823 * to the remote peer of the union operation
825 * @param eo union operation
826 * @param ibf_key IBF key of interest
829 send_elements_for_key (struct OperationState *eo, struct IBF_Key ibf_key)
831 struct SendElementClosure send_cls;
833 send_cls.ibf_key = ibf_key;
835 GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element, (uint32_t) ibf_key.key_val,
836 &send_element_iterator, &send_cls);
841 * Decode which elements are missing on each side, and
842 * send the appropriate elemens and requests
844 * @param eo union operation
847 decode_and_send (struct OperationState *eo)
850 struct IBF_Key last_key;
852 unsigned int num_decoded;
853 struct InvertibleBloomFilter *diff_ibf;
855 GNUNET_assert (PHASE_EXPECT_ELEMENTS == eo->phase);
857 prepare_ibf (eo, eo->remote_ibf->size);
858 diff_ibf = ibf_dup (eo->local_ibf);
859 ibf_subtract (diff_ibf, eo->remote_ibf);
861 ibf_destroy (eo->remote_ibf);
862 eo->remote_ibf = NULL;
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoding IBF (size=%u)\n", diff_ibf->size);
875 res = ibf_decode (diff_ibf, &side, &key);
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoded ibf key %lx\n",
879 if (num_decoded > diff_ibf->size || (num_decoded > 1 && last_key.key_val == key.key_val))
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "detected cyclic ibf (decoded %u/%u)\n",
881 num_decoded, diff_ibf->size);
882 if ((GNUNET_SYSERR == res) || (num_decoded > diff_ibf->size) ||
883 (num_decoded > 1 && last_key.key_val == key.key_val))
887 while (1<<next_order < diff_ibf->size)
890 if (next_order <= MAX_IBF_ORDER)
892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
893 "decoding failed, sending larger ibf (size %u)\n",
895 send_ibf (eo, next_order);
899 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
900 "set union failed: reached ibf limit\n");
904 if (GNUNET_NO == res)
906 struct GNUNET_MQ_Envelope *ev;
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmitted all values, sending DONE\n");
909 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
910 GNUNET_MQ_send (eo->mq, ev);
915 send_elements_for_key (eo, key);
919 struct GNUNET_MQ_Envelope *ev;
920 struct GNUNET_MessageHeader *msg;
922 /* FIXME: before sending the request, check if we may just have the element */
923 /* FIXME: merge multiple requests */
924 ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
925 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
927 *(struct IBF_Key *) &msg[1] = key;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element request\n");
929 GNUNET_MQ_send (eo->mq, ev);
936 ibf_destroy (diff_ibf);
941 * Handle an IBF message from a remote peer.
943 * @param cls the union operation
944 * @param mh the header of the message
947 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
949 struct OperationState *eo = cls;
950 struct IBFMessage *msg = (struct IBFMessage *) mh;
951 unsigned int buckets_in_message;
953 if ( (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
954 (eo->phase == PHASE_EXPECT_IBF) )
956 eo->phase = PHASE_EXPECT_IBF_CONT;
957 GNUNET_assert (NULL == eo->remote_ibf);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "creating new ibf of size %u\n", 1<<msg->order);
959 eo->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
960 eo->ibf_buckets_received = 0;
961 if (0 != ntohs (msg->offset))
964 fail_union_operation (eo);
968 else if (eo->phase == PHASE_EXPECT_IBF_CONT)
970 if ( (ntohs (msg->offset) != eo->ibf_buckets_received) ||
971 (1<<msg->order != eo->remote_ibf->size) )
974 fail_union_operation (eo);
979 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
981 if (0 == buckets_in_message)
984 fail_union_operation (eo);
988 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
991 fail_union_operation (eo);
995 ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
996 eo->ibf_buckets_received += buckets_in_message;
998 if (eo->ibf_buckets_received == eo->remote_ibf->size)
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received full ibf\n");
1001 eo->phase = PHASE_EXPECT_ELEMENTS;
1002 decode_and_send (eo);
1008 * Send a result message to the client indicating
1009 * that there is a new element.
1011 * @param eo union operation
1012 * @param element element to send
1015 send_client_element (struct OperationState *eo,
1016 struct GNUNET_SET_Element *element)
1018 struct GNUNET_MQ_Envelope *ev;
1019 struct GNUNET_SET_ResultMessage *rm;
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client\n", element->size);
1022 GNUNET_assert (0 != eo->spec->client_request_id);
1023 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
1026 GNUNET_MQ_discard (ev);
1030 rm->result_status = htons (GNUNET_SET_STATUS_OK);
1031 rm->request_id = htonl (eo->spec->client_request_id);
1032 rm->element_type = element->type;
1033 memcpy (&rm[1], element->data, element->size);
1034 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
1039 * Send a result message to the client indicating
1040 * that the operation is over.
1041 * After the result done message has been sent to the client,
1042 * destroy the evaluate operation.
1044 * @param eo union operation
1047 send_client_done_and_destroy (struct OperationState *eo)
1049 struct GNUNET_MQ_Envelope *ev;
1050 struct GNUNET_SET_ResultMessage *rm;
1052 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
1053 rm->request_id = htonl (eo->spec->client_request_id);
1054 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
1055 rm->element_type = htons (0);
1056 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
1062 * Handle an element message from a remote peer.
1064 * @param cls the union operation
1065 * @param mh the message
1068 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
1070 struct OperationState *eo = cls;
1071 struct ElementEntry *ee;
1072 uint16_t element_size;
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got element from peer\n");
1076 if ( (eo->phase != PHASE_EXPECT_ELEMENTS) &&
1077 (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1079 fail_union_operation (eo);
1083 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1084 ee = GNUNET_malloc (sizeof *eo + element_size);
1085 memcpy (&ee[1], &mh[1], element_size);
1086 ee->element.size = element_size;
1087 ee->element.data = &ee[1];
1088 ee->remote = GNUNET_YES;
1089 GNUNET_CRYPTO_hash (ee->element.data, ee->element.size, &ee->element_hash);
1091 insert_element (eo, ee);
1092 send_client_element (eo, &ee->element);
1097 * Handle an element request from a remote peer.
1099 * @param cls the union operation
1100 * @param mh the message
1103 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1105 struct OperationState *eo = cls;
1106 struct IBF_Key *ibf_key;
1107 unsigned int num_keys;
1109 /* look up elements and send them */
1110 if (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1113 fail_union_operation (eo);
1117 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1119 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1122 fail_union_operation (eo);
1126 ibf_key = (struct IBF_Key *) &mh[1];
1127 while (0 != num_keys--)
1129 send_elements_for_key (eo, *ibf_key);
1136 * Callback used for notifications
1138 * @param cls closure
1141 peer_done_sent_cb (void *cls)
1143 struct OperationState *eo = cls;
1145 send_client_done_and_destroy (eo);
1150 * Handle a done message from a remote peer
1152 * @param cls the union operation
1153 * @param mh the message
1156 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1158 struct OperationState *eo = cls;
1160 if (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1162 /* we got all requests, but still have to send our elements as response */
1163 struct GNUNET_MQ_Envelope *ev;
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got DONE, sending final DONE after elements\n");
1166 eo->phase = PHASE_FINISHED;
1167 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1168 GNUNET_MQ_notify_sent (ev, peer_done_sent_cb, eo);
1169 GNUNET_MQ_send (eo->mq, ev);
1172 if (eo->phase == PHASE_EXPECT_ELEMENTS)
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
1175 eo->phase = PHASE_FINISHED;
1176 send_client_done_and_destroy (eo);
1180 fail_union_operation (eo);
1185 * Evaluate a union operation with
1188 * @param spec specification of the operation the evaluate
1189 * @param tunnel tunnel already connected to the partner peer
1190 * @param tc tunnel context, passed here so all new incoming
1191 * messages are directly going to the union operations
1192 * @return a handle to the operation
1195 union_evaluate (struct OperationSpecification *spec,
1196 struct GNUNET_MESH_Tunnel *tunnel,
1197 struct TunnelContext *tc)
1199 struct OperationState *eo;
1201 eo = GNUNET_new (struct OperationState);
1202 tc->vt = _GSS_union_vt ();
1204 eo->se = strata_estimator_dup (spec->set->state->se);
1205 eo->set_state = spec->set->state;
1207 eo->tunnel = tunnel;
1208 eo->mq = GNUNET_MESH_mq_create (tunnel);
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1211 "evaluating union operation, (app %s)\n",
1212 GNUNET_h2s (&eo->spec->app_id));
1214 /* we started the operation, thus we have to send the operation request */
1215 eo->phase = PHASE_EXPECT_SE;
1217 GNUNET_CONTAINER_DLL_insert (eo->set_state->ops_head,
1218 eo->set_state->ops_tail,
1221 send_operation_request (eo);
1226 * Accept an union operation request from a remote peer
1228 * @param spec all necessary information about the operation
1229 * @param tunnel open tunnel to the partner's peer
1230 * @param tc tunnel context, passed here so all new incoming
1231 * messages are directly going to the union operations
1235 union_accept (struct OperationSpecification *spec,
1236 struct GNUNET_MESH_Tunnel *tunnel,
1237 struct TunnelContext *tc)
1239 struct OperationState *eo;
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
1243 eo = GNUNET_new (struct OperationState);
1244 tc->vt = _GSS_union_vt ();
1246 eo->set_state = spec->set->state;
1247 eo->generation_created = eo->set_state->current_generation++;
1249 eo->tunnel = tunnel;
1250 eo->mq = GNUNET_MESH_mq_create (tunnel);
1251 eo->se = strata_estimator_dup (eo->set_state->se);
1252 /* transfer ownership of mq and socket from incoming to eo */
1253 GNUNET_CONTAINER_DLL_insert (eo->set_state->ops_head,
1254 eo->set_state->ops_tail,
1256 /* kick off the operation */
1257 send_strata_estimator (eo);
1262 * Create a new set supporting the union operation
1264 * @return the newly created set
1266 static struct SetState *
1267 union_set_create (void)
1269 struct SetState *set_state;
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "union set created\n");
1273 set_state = GNUNET_new (struct SetState);
1274 /* keys of the hash map are stored in the element entrys, thus we do not
1275 * want the hash map to copy them */
1276 set_state->elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1277 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
1278 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1284 * Add the element from the given element message to the set.
1286 * @param set_state state of the set want to add to
1287 * @param element the element to add to the set
1290 union_add (struct SetState *set_state, const struct GNUNET_SET_Element *element)
1292 struct ElementEntry *ee;
1293 struct ElementEntry *ee_dup;
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding union element of size %u\n", element->size);
1297 ee = GNUNET_malloc (element->size + sizeof *ee);
1298 ee->element.size = element->size;
1299 memcpy (&ee[1], element->data, element->size);
1300 ee->element.data = &ee[1];
1301 ee->generation_added = set_state->current_generation;
1302 ee->remote = GNUNET_NO;
1303 GNUNET_CRYPTO_hash (ee->element.data, element->size, &ee->element_hash);
1304 ee_dup = GNUNET_CONTAINER_multihashmap_get (set_state->elements,
1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element inserted twice, ignoring\n");
1312 GNUNET_CONTAINER_multihashmap_put (set_state->elements, &ee->element_hash, ee,
1313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1314 strata_estimator_insert (set_state->se, get_ibf_key (&ee->element_hash, 0));
1319 * Destroy a set that supports the union operation
1321 * @param set_state the set to destroy
1324 union_set_destroy (struct SetState *set_state)
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union set\n");
1327 /* important to destroy operations before the rest of the set */
1328 while (NULL != set_state->ops_head)
1329 union_operation_destroy (set_state->ops_head);
1330 if (NULL != set_state->se)
1332 strata_estimator_destroy (set_state->se);
1333 set_state->se = NULL;
1335 if (NULL != set_state->elements)
1337 GNUNET_CONTAINER_multihashmap_iterate (set_state->elements,
1338 destroy_elements_iterator, NULL);
1339 GNUNET_CONTAINER_multihashmap_destroy (set_state->elements);
1340 set_state->elements = NULL;
1343 GNUNET_free (set_state);
1347 * Remove the element given in the element message from the set.
1348 * Only marks the element as removed, so that older set operations can still exchange it.
1350 * @param set_state state of the set to remove from
1351 * @param element set element to remove
1354 union_remove (struct SetState *set_state, const struct GNUNET_SET_Element *element)
1356 struct GNUNET_HashCode hash;
1357 struct ElementEntry *ee;
1359 GNUNET_CRYPTO_hash (element->data, element->size, &hash);
1360 ee = GNUNET_CONTAINER_multihashmap_get (set_state->elements, &hash);
1363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove non-existing element\n");
1366 if (GNUNET_YES == ee->removed)
1368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "client tried to remove element twice\n");
1371 ee->removed = GNUNET_YES;
1372 ee->generation_removed = set_state->current_generation;
1377 * Dispatch messages for a union operation.
1379 * @param eo the state of the union evaluate operation
1380 * @param mh the received message
1381 * @return GNUNET_SYSERR if the tunnel should be disconnected,
1382 * GNUNET_OK otherwise
1385 union_handle_p2p_message (struct OperationState *eo,
1386 const struct GNUNET_MessageHeader *mh)
1388 switch (ntohs (mh->type))
1390 case GNUNET_MESSAGE_TYPE_SET_P2P_IBF:
1391 handle_p2p_ibf (eo, mh);
1393 case GNUNET_MESSAGE_TYPE_SET_P2P_SE:
1394 handle_p2p_strata_estimator (eo, mh);
1396 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1397 handle_p2p_elements (eo, mh);
1399 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1400 handle_p2p_element_requests (eo, mh);
1402 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
1403 handle_p2p_done (eo, mh);
1406 /* something wrong with mesh's message handlers? */
1414 union_peer_disconnect (struct OperationState *op)
1416 /* Are we already disconnected? */
1417 if (NULL == op->tunnel)
1422 GNUNET_MQ_destroy (op->mq);
1425 if (PHASE_FINISHED != op->phase)
1427 struct GNUNET_MQ_Envelope *ev;
1428 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, "other peer disconnected prematurely\n");
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
1440 union_operation_destroy (op);
1445 union_op_cancel (struct SetState *set_state, uint32_t op_id)
1447 /* FIXME: implement */
1452 * Iterator over hash map entries.
1454 * @param cls closure
1455 * @param key current key code
1456 * @param value value in the hash map
1457 * @return GNUNET_YES if we should continue to
1462 send_iter_element_iter (void *cls,
1463 const struct GNUNET_HashCode *key,
1466 struct ElementEntry *ee = value;
1467 struct Set *set = cls;
1468 struct GNUNET_SET_IterResponseMessage *m;
1469 struct GNUNET_MQ_Envelope *ev;
1471 ev = GNUNET_MQ_msg_extra (m, ee->element.size,
1472 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT);
1474 m->element_type = ee->element.type;
1475 memcpy (&m[1], ee->element.data, ee->element.size);
1476 GNUNET_MQ_send (set->client_mq, ev);
1483 * Send all elements of the union set to the client.
1485 * @param set set to iterate over
1488 union_iterate (struct Set *set)
1490 struct GNUNET_MQ_Envelope *ev;
1492 GNUNET_CONTAINER_multihashmap_iterate (set->state->elements, send_iter_element_iter, set);
1493 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_DONE);
1494 GNUNET_MQ_send (set->client_mq, ev);
1498 const struct SetVT *
1501 static const struct SetVT union_vt = {
1502 .create = &union_set_create,
1503 .msg_handler = &union_handle_p2p_message,
1505 .remove = &union_remove,
1506 .destroy_set = &union_set_destroy,
1507 .evaluate = &union_evaluate,
1508 .accept = &union_accept,
1509 .peer_disconnect = &union_peer_disconnect,
1510 .cancel = &union_op_cancel,
1511 .iterate = &union_iterate