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_intersection.c
23 * @brief two-peer set intersection
24 * @author Christian Fuchs
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 IntersectionOperationPhase
73 * We sent the request message, and expect a strata estimator
77 * We sent the strata estimator, and expect an IBF
81 * We know what type of IBF the other peer wants to send us,
82 * and expect the remaining parts
84 PHASE_EXPECT_IBF_CONT,
86 * We are sending request and elements,
87 * and thus only expect elements from the other peer.
89 PHASE_EXPECT_ELEMENTS,
91 * We are expecting elements and requests, and send
92 * requested elements back to the other peer.
94 PHASE_EXPECT_ELEMENTS_AND_REQUESTS,
96 * The protocol is over.
97 * Results may still have to be sent to the client.
104 * State of an evaluate operation
107 struct OperationState
110 * Tunnel to the remote peer.
112 struct GNUNET_MESH_Tunnel *tunnel;
115 * Detail information about the set operation,
116 * including the set to use.
118 struct OperationSpecification *spec;
121 * Message queue for the peer.
123 struct GNUNET_MQ_Handle *mq;
126 * Number of ibf buckets received
128 unsigned int ibf_buckets_received;
131 * Copy of the set's strata estimator at the time of
132 * creation of this operation
134 struct StrataEstimator *se;
137 * The ibf we currently receive
139 struct InvertibleBloomFilter *remote_ibf;
142 * IBF of the set's element.
144 struct InvertibleBloomFilter *local_ibf;
147 * Maps IBF-Keys (specific to the current salt) to elements.
148 * Used as a multihashmap, the keys being the lower 32bit of the IBF-Key.
149 * Colliding IBF-Keys are linked.
151 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
154 * Current state of the operation.
156 enum IntersectionOperationPhase phase;
159 * Generation in which the operation handle
162 unsigned int generation_created;
165 * Set state of the set that this operation
171 * Evaluate operations are held in
174 struct OperationState *next;
177 * Evaluate operations are held in
180 struct OperationState *prev;
183 * Did we send the client that we are done?
185 int client_done_sent;
190 * The key entry is used to associate an ibf key with
196 * IBF key for the entry, derived from the current salt.
198 struct IBF_Key ibf_key;
201 * The actual element associated with the key
203 struct ElementEntry *element;
206 * Element that collides with this element
209 struct KeyEntry *next_colliding;
214 * Used as a closure for sending elements
215 * with a specific IBF key.
217 struct SendElementClosure
220 * The IBF key whose matching elements should be
223 struct IBF_Key ibf_key;
226 * Operation for which the elements
229 struct OperationState *eo;
234 * Extra state required for efficient set union.
239 * The strata estimator is only generated once for
241 * The IBF keys are derived from the element hashes with
244 struct StrataEstimator *se;
247 * Evaluate operations are held in
250 struct OperationState *ops_head;
253 * Evaluate operations are held in
256 struct OperationState *ops_tail;
261 * Iterator over hash map entries.
264 * @param key current key code
265 * @param value value in the hash map
266 * @return GNUNET_YES if we should continue to
271 destroy_key_to_element_iter (void *cls,
275 struct KeyEntry *k = value;
279 struct KeyEntry *k_tmp = k;
280 k = k->next_colliding;
281 if (GNUNET_YES == k_tmp->element->remote)
283 GNUNET_free (k_tmp->element);
284 k_tmp->element = NULL;
293 * Destroy a union operation, and free all resources
294 * associated with it.
296 * @param eo the union operation to destroy
299 intersection_operation_destroy (struct OperationState *eo)
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op\n");
302 GNUNET_CONTAINER_DLL_remove (eo->set->state->ops_head,
303 eo->set->state->ops_tail,
307 GNUNET_MQ_destroy (eo->mq);
310 if (NULL != eo->tunnel)
312 struct GNUNET_MESH_Tunnel *t = eo->tunnel;
314 GNUNET_MESH_tunnel_destroy (t);
316 if (NULL != eo->remote_ibf)
318 ibf_destroy (eo->remote_ibf);
319 eo->remote_ibf = NULL;
321 if (NULL != eo->local_ibf)
323 ibf_destroy (eo->local_ibf);
324 eo->local_ibf = NULL;
328 strata_estimator_destroy (eo->se);
331 if (NULL != eo->key_to_element)
333 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element, destroy_key_to_element_iter, NULL);
334 GNUNET_CONTAINER_multihashmap32_destroy (eo->key_to_element);
335 eo->key_to_element = NULL;
337 if (NULL != eo->spec)
339 if (NULL != eo->spec->context_msg)
341 GNUNET_free (eo->spec->context_msg);
342 eo->spec->context_msg = NULL;
344 GNUNET_free (eo->spec);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union op done\n");
351 /* FIXME: do a garbage collection of the set generations */
356 * Inform the client that the union operation has failed,
357 * and proceed to destroy the evaluate operation.
359 * @param eo the union operation to fail
362 fail_intersection_operation (struct OperationState *eo)
364 struct GNUNET_MQ_Envelope *ev;
365 struct GNUNET_SET_ResultMessage *msg;
367 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
368 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
369 msg->request_id = htonl (eo->spec->client_request_id);
370 msg->element_type = htons (0);
371 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
372 union_operation_destroy (eo);
377 * Derive the IBF key from a hash code and
380 * @param src the hash code
381 * @param salt salt to use
382 * @return the derived IBF key
384 static struct IBF_Key
385 get_ibf_key (struct GNUNET_HashCode *src, uint16_t salt)
389 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
390 GCRY_MD_SHA512, GCRY_MD_SHA256,
392 &salt, sizeof (salt),
399 * Send a request for the evaluate operation to a remote peer
401 * @param eo operation with the other peer
404 send_operation_request (struct OperationState *eo)
406 struct GNUNET_MQ_Envelope *ev;
407 struct OperationRequestMessage *msg;
409 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
410 eo->spec->context_msg);
414 /* the context message is too large */
416 GNUNET_SERVER_client_disconnect (eo->spec->set->client);
419 msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
420 msg->app_id = eo->spec->app_id;
421 msg->salt = htonl (eo->spec->salt);
422 GNUNET_MQ_send (eo->mq, ev);
424 if (NULL != eo->spec->context_msg)
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
429 if (NULL != eo->spec->context_msg)
431 GNUNET_free (eo->spec->context_msg);
432 eo->spec->context_msg = NULL;
439 * Iterator to create the mapping between ibf keys
440 * and element entries.
443 * @param key current key code
444 * @param value value in the hash map
445 * @return GNUNET_YES if we should continue to
450 op_register_element_iterator (void *cls,
454 struct KeyEntry *const new_k = cls;
455 struct KeyEntry *old_k = value;
457 GNUNET_assert (NULL != old_k);
460 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
462 new_k->next_colliding = old_k->next_colliding;
463 old_k->next_colliding = new_k;
466 old_k = old_k->next_colliding;
467 } while (NULL != old_k);
473 * Insert an element into the union operation's
474 * key-to-element mapping. Takes ownership of 'ee'.
475 * Note that this does not insert the element in the set,
476 * only in the operation's key-element mapping.
477 * This is done to speed up re-tried operations, if some elements
478 * were transmitted, and then the IBF fails to decode.
480 * @param eo the union operation
481 * @param ee the element entry
484 op_register_element (struct OperationState *eo, struct ElementEntry *ee)
487 struct IBF_Key ibf_key;
490 ibf_key = get_ibf_key (&ee->element_hash, eo->spec->salt);
491 k = GNUNET_new (struct KeyEntry);
493 k->ibf_key = ibf_key;
494 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element,
495 (uint32_t) ibf_key.key_val,
496 op_register_element_iterator, k);
498 /* was the element inserted into a colliding bucket? */
499 if (GNUNET_SYSERR == ret)
502 GNUNET_CONTAINER_multihashmap32_put (eo->key_to_element, (uint32_t) ibf_key.key_val, k,
503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
508 * Insert a key into an ibf.
512 * @param value the key entry to get the key from
515 prepare_ibf_iterator (void *cls,
519 struct InvertibleBloomFilter *ibf = cls;
520 struct KeyEntry *ke = value;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inserting %x into ibf\n", ke->ibf_key.key_val);
524 ibf_insert (ibf, ke->ibf_key);
530 * Iterator for initializing the
531 * key-to-element mapping of a union operation
533 * @param cls the union operation
535 * @param value the element entry to insert
536 * into the key-to-element mapping
537 * @return GNUNET_YES to continue iterating,
541 init_key_to_element_iterator (void *cls,
542 const struct GNUNET_HashCode *key,
545 struct OperationState *eo = cls;
546 struct ElementEntry *e = value;
548 /* make sure that the element belongs to the set at the time
549 * of creating the operation */
550 if ( (e->generation_added > eo->generation_created) ||
551 ( (GNUNET_YES == e->removed) &&
552 (e->generation_removed < eo->generation_created)))
555 GNUNET_assert (GNUNET_NO == e->remote);
557 op_register_element (eo, e);
563 * Create an ibf with the operation's elements
564 * of the specified size
566 * @param eo the union operation
567 * @param size size of the ibf to create
570 prepare_ibf (struct OperationState *eo, uint16_t size)
572 if (NULL == eo->key_to_element)
575 len = GNUNET_CONTAINER_multihashmap_size (eo->set->elements);
576 eo->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
577 GNUNET_CONTAINER_multihashmap_iterate (eo->set->elements,
578 init_key_to_element_iterator, eo);
580 if (NULL != eo->local_ibf)
581 ibf_destroy (eo->local_ibf);
582 eo->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
583 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element,
584 prepare_ibf_iterator, eo->local_ibf);
589 * Send an ibf of appropriate size.
591 * @param eo the union operation
592 * @param ibf_order order of the ibf to send, size=2^order
595 send_ibf (struct OperationState *eo, uint16_t ibf_order)
597 unsigned int buckets_sent = 0;
598 struct InvertibleBloomFilter *ibf;
600 prepare_ibf (eo, 1<<ibf_order);
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending ibf of size %u\n", 1<<ibf_order);
606 while (buckets_sent < (1 << ibf_order))
608 unsigned int buckets_in_message;
609 struct GNUNET_MQ_Envelope *ev;
610 struct IBFMessage *msg;
612 buckets_in_message = (1 << ibf_order) - buckets_sent;
613 /* limit to maximum */
614 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
615 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
617 ev = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
618 GNUNET_MESSAGE_TYPE_SET_P2P_IBF);
620 msg->order = ibf_order;
621 msg->offset = htons (buckets_sent);
622 ibf_write_slice (ibf, buckets_sent,
623 buckets_in_message, &msg[1]);
624 buckets_sent += buckets_in_message;
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ibf chunk size %u, %u/%u sent\n",
626 buckets_in_message, buckets_sent, 1<<ibf_order);
627 GNUNET_MQ_send (eo->mq, ev);
630 eo->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
635 * Send a strata estimator to the remote peer.
637 * @param eo the union operation with the remote peer
640 send_strata_estimator (struct OperationState *eo)
642 struct GNUNET_MQ_Envelope *ev;
643 struct GNUNET_MessageHeader *strata_msg;
645 ev = GNUNET_MQ_msg_header_extra (strata_msg,
646 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
647 GNUNET_MESSAGE_TYPE_SET_P2P_SE);
648 strata_estimator_write (eo->set->state->se, &strata_msg[1]);
649 GNUNET_MQ_send (eo->mq, ev);
650 eo->phase = PHASE_EXPECT_IBF;
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent SE, expecting IBF\n");
656 * Compute the necessary order of an ibf
657 * from the size of the symmetric set difference.
659 * @param diff the difference
660 * @return the required size of the ibf
663 get_order_from_difference (unsigned int diff)
665 unsigned int ibf_order;
668 while ((1<<ibf_order) < (IBF_ALPHA * diff) || (1<<ibf_order) < SE_IBF_HASH_NUM)
670 if (ibf_order > MAX_IBF_ORDER)
671 ibf_order = MAX_IBF_ORDER;
677 * Handle a strata estimator from a remote peer
679 * @param cls the union operation
680 * @param mh the message
683 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
685 struct OperationState *eo = cls;
686 struct StrataEstimator *remote_se;
689 if (eo->phase != PHASE_EXPECT_SE)
691 fail_union_operation (eo);
695 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
697 strata_estimator_read (&mh[1], remote_se);
698 GNUNET_assert (NULL != eo->se);
699 diff = strata_estimator_difference (remote_se, eo->se);
700 strata_estimator_destroy (remote_se);
701 strata_estimator_destroy (eo->se);
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se diff=%d, using ibf size %d\n",
704 diff, 1<<get_order_from_difference (diff));
705 send_ibf (eo, get_order_from_difference (diff));
711 * Iterator to send elements to a remote peer
713 * @param cls closure with the element key and the union operation
715 * @param value the key entry
718 send_element_iterator (void *cls,
722 struct SendElementClosure *sec = cls;
723 struct IBF_Key ibf_key = sec->ibf_key;
724 struct OperationState *eo = sec->eo;
725 struct KeyEntry *ke = value;
727 if (ke->ibf_key.key_val != ibf_key.key_val)
731 const struct GNUNET_SET_Element *const element = &ke->element->element;
732 struct GNUNET_MQ_Envelope *ev;
733 struct GNUNET_MessageHeader *mh;
735 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
736 ev = GNUNET_MQ_msg_header_extra (mh, element->size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
739 /* element too large */
743 memcpy (&mh[1], element->data, element->size);
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (%s) to peer\n",
745 GNUNET_h2s (&ke->element->element_hash));
746 GNUNET_MQ_send (eo->mq, ev);
747 ke = ke->next_colliding;
753 * Send all elements that have the specified IBF key
754 * to the remote peer of the union operation
756 * @param eo union operation
757 * @param ibf_key IBF key of interest
760 send_elements_for_key (struct OperationState *eo, struct IBF_Key ibf_key)
762 struct SendElementClosure send_cls;
764 send_cls.ibf_key = ibf_key;
766 GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element, (uint32_t) ibf_key.key_val,
767 &send_element_iterator, &send_cls);
772 * Decode which elements are missing on each side, and
773 * send the appropriate elemens and requests
775 * @param eo union operation
778 decode_and_send (struct OperationState *eo)
781 struct IBF_Key last_key;
783 unsigned int num_decoded;
784 struct InvertibleBloomFilter *diff_ibf;
786 GNUNET_assert (PHASE_EXPECT_ELEMENTS == eo->phase);
788 prepare_ibf (eo, eo->remote_ibf->size);
789 diff_ibf = ibf_dup (eo->local_ibf);
790 ibf_subtract (diff_ibf, eo->remote_ibf);
792 ibf_destroy (eo->remote_ibf);
793 eo->remote_ibf = NULL;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoding IBF (size=%u)\n", diff_ibf->size);
798 last_key.key_val = 0;
803 int cycle_detected = GNUNET_NO;
807 res = ibf_decode (diff_ibf, &side, &key);
808 if (res == GNUNET_OK)
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoded ibf key %lx\n",
813 if (num_decoded > diff_ibf->size || (num_decoded > 1 && last_key.key_val == key.key_val))
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "detected cyclic ibf (decoded %u/%u)\n",
816 num_decoded, diff_ibf->size);
817 cycle_detected = GNUNET_YES;
820 if ((GNUNET_SYSERR == res) || (GNUNET_YES == cycle_detected))
824 while (1<<next_order < diff_ibf->size)
827 if (next_order <= MAX_IBF_ORDER)
829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
830 "decoding failed, sending larger ibf (size %u)\n",
832 send_ibf (eo, next_order);
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837 "set union failed: reached ibf limit\n");
841 if (GNUNET_NO == res)
843 struct GNUNET_MQ_Envelope *ev;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmitted all values, sending DONE\n");
846 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
847 GNUNET_MQ_send (eo->mq, ev);
852 send_elements_for_key (eo, key);
856 struct GNUNET_MQ_Envelope *ev;
857 struct GNUNET_MessageHeader *msg;
859 /* FIXME: before sending the request, check if we may just have the element */
860 /* FIXME: merge multiple requests */
861 /* FIXME: remember somewhere that we already requested the element,
862 * so that we don't request it again with the next ibf if decoding fails */
863 ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
864 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
866 *(struct IBF_Key *) &msg[1] = key;
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element request\n");
868 GNUNET_MQ_send (eo->mq, ev);
875 ibf_destroy (diff_ibf);
880 * Handle an IBF message from a remote peer.
882 * @param cls the union operation
883 * @param mh the header of the message
886 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
888 struct OperationState *eo = cls;
889 struct IBFMessage *msg = (struct IBFMessage *) mh;
890 unsigned int buckets_in_message;
892 if ( (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
893 (eo->phase == PHASE_EXPECT_IBF) )
895 eo->phase = PHASE_EXPECT_IBF_CONT;
896 GNUNET_assert (NULL == eo->remote_ibf);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "creating new ibf of size %u\n", 1<<msg->order);
898 eo->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
899 eo->ibf_buckets_received = 0;
900 if (0 != ntohs (msg->offset))
903 fail_union_operation (eo);
907 else if (eo->phase == PHASE_EXPECT_IBF_CONT)
909 if ( (ntohs (msg->offset) != eo->ibf_buckets_received) ||
910 (1<<msg->order != eo->remote_ibf->size) )
913 fail_union_operation (eo);
918 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
920 if (0 == buckets_in_message)
923 fail_union_operation (eo);
927 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
930 fail_union_operation (eo);
934 ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
935 eo->ibf_buckets_received += buckets_in_message;
937 if (eo->ibf_buckets_received == eo->remote_ibf->size)
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received full ibf\n");
940 eo->phase = PHASE_EXPECT_ELEMENTS;
941 decode_and_send (eo);
947 * Send a result message to the client indicating
948 * that there is a new element.
950 * @param eo union operation
951 * @param element element to send
954 send_client_element (struct OperationState *eo,
955 struct GNUNET_SET_Element *element)
957 struct GNUNET_MQ_Envelope *ev;
958 struct GNUNET_SET_ResultMessage *rm;
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client\n", element->size);
961 GNUNET_assert (0 != eo->spec->client_request_id);
962 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
965 GNUNET_MQ_discard (ev);
969 rm->result_status = htons (GNUNET_SET_STATUS_OK);
970 rm->request_id = htonl (eo->spec->client_request_id);
971 rm->element_type = element->type;
972 memcpy (&rm[1], element->data, element->size);
973 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
978 * Send a result message to the client indicating
979 * that the operation is over.
980 * After the result done message has been sent to the client,
981 * destroy the evaluate operation.
983 * @param eo union operation
986 send_client_done_and_destroy (struct OperationState *eo)
988 struct GNUNET_MQ_Envelope *ev;
989 struct GNUNET_SET_ResultMessage *rm;
991 GNUNET_assert (GNUNET_NO == eo->client_done_sent);
993 eo->client_done_sent = GNUNET_YES;
995 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
996 rm->request_id = htonl (eo->spec->client_request_id);
997 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
998 rm->element_type = htons (0);
999 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
1001 union_operation_destroy (eo);
1006 * Handle an element message from a remote peer.
1008 * @param cls the union operation
1009 * @param mh the message
1012 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
1014 struct OperationState *eo = cls;
1015 struct ElementEntry *ee;
1016 uint16_t element_size;
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got element from peer\n");
1020 if ( (eo->phase != PHASE_EXPECT_ELEMENTS) &&
1021 (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1023 fail_union_operation (eo);
1027 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1028 ee = GNUNET_malloc (sizeof *ee + element_size);
1029 memcpy (&ee[1], &mh[1], element_size);
1030 ee->element.size = element_size;
1031 ee->element.data = &ee[1];
1032 ee->remote = GNUNET_YES;
1033 GNUNET_CRYPTO_hash (ee->element.data, ee->element.size, &ee->element_hash);
1035 /* FIXME: see if the element has already been inserted! */
1037 op_register_element (eo, ee);
1038 send_client_element (eo, &ee->element);
1043 * Handle an element request from a remote peer.
1045 * @param cls the union operation
1046 * @param mh the message
1049 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1051 struct OperationState *eo = cls;
1052 struct IBF_Key *ibf_key;
1053 unsigned int num_keys;
1055 /* look up elements and send them */
1056 if (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1059 fail_union_operation (eo);
1063 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1065 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1068 fail_union_operation (eo);
1072 ibf_key = (struct IBF_Key *) &mh[1];
1073 while (0 != num_keys--)
1075 send_elements_for_key (eo, *ibf_key);
1082 * Handle a done message from a remote peer
1084 * @param cls the union operation
1085 * @param mh the message
1088 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1090 struct OperationState *eo = cls;
1091 struct GNUNET_MQ_Envelope *ev;
1093 if (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1095 /* we got all requests, but still have to send our elements as response */
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got DONE, sending final DONE after elements\n");
1098 eo->phase = PHASE_FINISHED;
1099 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1100 GNUNET_MQ_send (eo->mq, ev);
1103 if (eo->phase == PHASE_EXPECT_ELEMENTS)
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
1106 eo->phase = PHASE_FINISHED;
1107 send_client_done_and_destroy (eo);
1111 fail_union_operation (eo);
1116 * Evaluate a union operation with
1119 * @param spec specification of the operation the evaluate
1120 * @param tunnel tunnel already connected to the partner peer
1121 * @param tc tunnel context, passed here so all new incoming
1122 * messages are directly going to the union operations
1123 * @return a handle to the operation
1126 intersection_evaluate (struct OperationSpecification *spec,
1127 struct GNUNET_MESH_Tunnel *tunnel,
1128 struct TunnelContext *tc)
1130 struct OperationState *eo;
1132 eo = GNUNET_new (struct OperationState);
1133 tc->vt = _GSS_union_vt ();
1135 eo->se = strata_estimator_dup (spec->set->state->se);
1136 eo->generation_created = spec->set->current_generation++;
1137 eo->set = spec->set;
1139 eo->tunnel = tunnel;
1140 eo->mq = GNUNET_MESH_mq_create (tunnel);
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "evaluating union operation, (app %s)\n",
1144 GNUNET_h2s (&eo->spec->app_id));
1146 /* we started the operation, thus we have to send the operation request */
1147 eo->phase = PHASE_EXPECT_SE;
1149 GNUNET_CONTAINER_DLL_insert (eo->set->state->ops_head,
1150 eo->set->state->ops_tail,
1153 send_operation_request (eo);
1158 * Accept an union operation request from a remote peer
1160 * @param spec all necessary information about the operation
1161 * @param tunnel open tunnel to the partner's peer
1162 * @param tc tunnel context, passed here so all new incoming
1163 * messages are directly going to the union operations
1167 intersection_accept (struct OperationSpecification *spec,
1168 struct GNUNET_MESH_Tunnel *tunnel,
1169 struct TunnelContext *tc)
1171 struct OperationState *eo;
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
1175 eo = GNUNET_new (struct OperationState);
1176 tc->vt = _GSS_union_vt ();
1178 eo->set = spec->set;
1179 eo->generation_created = eo->set->current_generation++;
1181 eo->tunnel = tunnel;
1182 eo->mq = GNUNET_MESH_mq_create (tunnel);
1183 eo->se = strata_estimator_dup (eo->set->state->se);
1184 /* transfer ownership of mq and socket from incoming to eo */
1185 GNUNET_CONTAINER_DLL_insert (eo->set->state->ops_head,
1186 eo->set->state->ops_tail,
1188 /* kick off the operation */
1189 send_strata_estimator (eo);
1194 * Create a new set supporting the union operation
1196 * @return the newly created set
1198 static struct SetState *
1199 intersection_set_create (void)
1201 struct SetState *set_state;
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "union set created\n");
1205 set_state = GNUNET_new (struct SetState);
1206 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
1207 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1213 * Add the element from the given element message to the set.
1215 * @param set_state state of the set want to add to
1216 * @param ee the element to add to the set
1219 intersection_add (struct SetState *set_state, struct ElementEntry *ee)
1221 strata_estimator_insert (set_state->se, get_ibf_key (&ee->element_hash, 0));
1226 * Destroy a set that supports the union operation
1228 * @param set_state the set to destroy
1231 intersection_set_destroy (struct SetState *set_state)
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying union set\n");
1234 /* important to destroy operations before the rest of the set */
1235 while (NULL != set_state->ops_head)
1236 union_operation_destroy (set_state->ops_head);
1237 if (NULL != set_state->se)
1239 strata_estimator_destroy (set_state->se);
1240 set_state->se = NULL;
1242 GNUNET_free (set_state);
1247 * Remove the element given in the element message from the set.
1248 * Only marks the element as removed, so that older set operations can still exchange it.
1250 * @param set_state state of the set to remove from
1251 * @param element set element to remove
1254 intersection_remove (struct SetState *set_state, struct ElementEntry *element)
1256 /* FIXME: remove from strata estimator */
1261 * Dispatch messages for a union operation.
1263 * @param eo the state of the union evaluate operation
1264 * @param mh the received message
1265 * @return GNUNET_SYSERR if the tunnel should be disconnected,
1266 * GNUNET_OK otherwise
1269 intersection_handle_p2p_message (struct OperationState *eo,
1270 const struct GNUNET_MessageHeader *mh)
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
1273 ntohs (mh->type), ntohs (mh->size));
1274 switch (ntohs (mh->type))
1276 case GNUNET_MESSAGE_TYPE_SET_P2P_IBF:
1277 handle_p2p_ibf (eo, mh);
1279 case GNUNET_MESSAGE_TYPE_SET_P2P_SE:
1280 handle_p2p_strata_estimator (eo, mh);
1282 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1283 handle_p2p_elements (eo, mh);
1285 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1286 handle_p2p_element_requests (eo, mh);
1288 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
1289 handle_p2p_done (eo, mh);
1292 /* something wrong with mesh's message handlers? */
1300 intersection_peer_disconnect (struct OperationState *op)
1302 /* Are we already disconnected? */
1303 if (NULL == op->tunnel)
1308 GNUNET_MQ_destroy (op->mq);
1311 if (PHASE_FINISHED != op->phase)
1313 struct GNUNET_MQ_Envelope *ev;
1314 struct GNUNET_SET_ResultMessage *msg;
1316 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
1317 msg->request_id = htonl (op->spec->client_request_id);
1318 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1319 msg->element_type = htons (0);
1320 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
1322 union_operation_destroy (op);
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
1326 if (GNUNET_NO == op->client_done_sent)
1327 send_client_done_and_destroy (op);
1332 intersection_op_cancel (struct SetState *set_state, uint32_t op_id)
1334 /* FIXME: implement */
1338 const struct SetVT *
1339 _GSS_intersection_vt ()
1341 static const struct SetVT union_vt = {
1342 .create = &union_set_create,
1343 .msg_handler = &union_handle_p2p_message,
1345 .remove = &union_remove,
1346 .destroy_set = &union_set_destroy,
1347 .evaluate = &union_evaluate,
1348 .accept = &union_accept,
1349 .peer_disconnect = &union_peer_disconnect,
1350 .cancel = &union_op_cancel,