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"
29 #include "strata_estimator.h"
30 #include "set_protocol.h"
35 * Number of IBFs in a strata estimator.
37 #define SE_STRATA_COUNT 32
39 * Size of the IBFs in the strata estimator.
41 #define SE_IBF_SIZE 80
43 * hash num parameter for the difference digests and strata estimators
45 #define SE_IBF_HASH_NUM 4
48 * Number of buckets that can be transmitted in one message.
50 #define MAX_BUCKETS_PER_MESSAGE ((1<<15) / IBF_BUCKET_SIZE)
53 * The maximum size of an ibf we use is 2^(MAX_IBF_ORDER).
54 * Choose this value so that computing the IBF is still cheaper
55 * than transmitting all values.
57 #define MAX_IBF_ORDER (16)
60 * Number of buckets used in the ibf per estimated
67 * Current phase we are in for a intersection operation.
69 enum IntersectionOperationPhase
72 * We sent the request message, and expect a BF
76 * We sent the request message, and expect a BF
80 * The protocol is over.
81 * Results may still have to be sent to the client.
88 * State of an evaluate operation
94 * Tunnel to the remote peer.
96 struct GNUNET_MESH_Tunnel *tunnel;
99 * Detail information about the set operation,
100 * including the set to use.
102 struct OperationSpecification *spec;
105 * Message queue for the peer.
107 struct GNUNET_MQ_Handle *mq;
110 * Number of ibf buckets received
112 unsigned int ibf_buckets_received;
115 * Copy of the set's strata estimator at the time of
116 * creation of this operation
118 struct StrataEstimator *se;
121 * The ibf we currently receive
123 struct InvertibleBloomFilter *remote_ibf;
126 * IBF of the set's element.
128 struct InvertibleBloomFilter *local_ibf;
131 * Maps IBF-Keys (specific to the current salt) to elements.
132 * Used as a multihashmap, the keys being the lower 32bit of the IBF-Key.
133 * Colliding IBF-Keys are linked.
135 struct GNUNET_CONTAINER_MultiHashMap32 *key_to_element;
138 * Current state of the operation.
140 enum IntersectionOperationPhase phase;
143 * Generation in which the operation handle
146 unsigned int generation_created;
149 * Set state of the set that this operation
155 * Evaluate operations are held in
158 struct OperationState *next;
161 * Evaluate operations are held in
164 struct OperationState *prev;
167 * Did we send the client that we are done?
169 int client_done_sent;
174 * The key entry is used to associate an ibf key with
180 * IBF key for the entry, derived from the current salt.
182 struct IBF_Key ibf_key;
185 * The actual element associated with the key
187 struct ElementEntry *element;
190 * Element that collides with this element
193 struct KeyEntry *next_colliding;
198 * Used as a closure for sending elements
199 * with a specific IBF key.
201 struct SendElementClosure
204 * The IBF key whose matching elements should be
207 struct IBF_Key ibf_key;
210 * Operation for which the elements
213 struct OperationState *eo;
218 * Extra state required for efficient set intersection.
223 * The strata estimator is only generated once for
225 * The IBF keys are derived from the element hashes with
228 struct StrataEstimator *se;
231 * Evaluate operations are held in
234 struct OperationState *ops_head;
237 * Evaluate operations are held in
240 struct OperationState *ops_tail;
245 * Iterator over hash map entries.
248 * @param key current key code
249 * @param value value in the hash map
250 * @return GNUNET_YES if we should continue to
255 destroy_key_to_element_iter (void *cls,
259 struct KeyEntry *k = value;
263 struct KeyEntry *k_tmp = k;
264 k = k->next_colliding;
265 if (GNUNET_YES == k_tmp->element->remote)
267 GNUNET_free (k_tmp->element);
268 k_tmp->element = NULL;
277 * Destroy a intersection operation, and free all resources
278 * associated with it.
280 * @param eo the intersection operation to destroy
283 intersection_operation_destroy (struct OperationState *eo)
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op\n");
286 GNUNET_CONTAINER_DLL_remove (eo->set->state->ops_head,
287 eo->set->state->ops_tail,
291 GNUNET_MQ_destroy (eo->mq);
294 if (NULL != eo->tunnel)
296 struct GNUNET_MESH_Tunnel *t = eo->tunnel;
298 GNUNET_MESH_tunnel_destroy (t);
300 if (NULL != eo->remote_ibf)
302 ibf_destroy (eo->remote_ibf);
303 eo->remote_ibf = NULL;
305 if (NULL != eo->local_ibf)
307 ibf_destroy (eo->local_ibf);
308 eo->local_ibf = NULL;
312 strata_estimator_destroy (eo->se);
315 if (NULL != eo->key_to_element)
317 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element, destroy_key_to_element_iter, NULL);
318 GNUNET_CONTAINER_multihashmap32_destroy (eo->key_to_element);
319 eo->key_to_element = NULL;
321 if (NULL != eo->spec)
323 if (NULL != eo->spec->context_msg)
325 GNUNET_free (eo->spec->context_msg);
326 eo->spec->context_msg = NULL;
328 GNUNET_free (eo->spec);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op done\n");
335 /* FIXME: do a garbage collection of the set generations */
340 * Inform the client that the intersection operation has failed,
341 * and proceed to destroy the evaluate operation.
343 * @param eo the intersection operation to fail
346 fail_intersection_operation (struct OperationState *eo)
348 struct GNUNET_MQ_Envelope *ev;
349 struct GNUNET_SET_ResultMessage *msg;
351 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
352 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
353 msg->request_id = htonl (eo->spec->client_request_id);
354 msg->element_type = htons (0);
355 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
356 intersection_operation_destroy (eo);
361 * Derive the IBF key from a hash code and
364 * @param src the hash code
365 * @param salt salt to use
366 * @return the derived IBF key
368 static struct IBF_Key
369 get_ibf_key (struct GNUNET_HashCode *src, uint16_t salt)
373 GNUNET_CRYPTO_hkdf (&key, sizeof (key),
374 GCRY_MD_SHA512, GCRY_MD_SHA256,
376 &salt, sizeof (salt),
383 * Send a request for the evaluate operation to a remote peer
385 * @param eo operation with the other peer
388 send_operation_request (struct OperationState *eo)
390 struct GNUNET_MQ_Envelope *ev;
391 struct OperationRequestMessage *msg;
393 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
394 eo->spec->context_msg);
398 /* the context message is too large */
400 GNUNET_SERVER_client_disconnect (eo->spec->set->client);
403 msg->operation = htonl (GNUNET_SET_OPERATION_UNION);
404 msg->app_id = eo->spec->app_id;
405 msg->salt = htonl (eo->spec->salt);
406 GNUNET_MQ_send (eo->mq, ev);
408 if (NULL != eo->spec->context_msg)
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
413 if (NULL != eo->spec->context_msg)
415 GNUNET_free (eo->spec->context_msg);
416 eo->spec->context_msg = NULL;
423 * Iterator to create the mapping between ibf keys
424 * and element entries.
427 * @param key current key code
428 * @param value value in the hash map
429 * @return GNUNET_YES if we should continue to
434 op_register_element_iterator (void *cls,
438 struct KeyEntry *const new_k = cls;
439 struct KeyEntry *old_k = value;
441 GNUNET_assert (NULL != old_k);
444 if (old_k->ibf_key.key_val == new_k->ibf_key.key_val)
446 new_k->next_colliding = old_k->next_colliding;
447 old_k->next_colliding = new_k;
450 old_k = old_k->next_colliding;
451 } while (NULL != old_k);
457 * Insert an element into the intersection operation's
458 * key-to-element mapping. Takes ownership of 'ee'.
459 * Note that this does not insert the element in the set,
460 * only in the operation's key-element mapping.
461 * This is done to speed up re-tried operations, if some elements
462 * were transmitted, and then the IBF fails to decode.
464 * @param eo the intersection operation
465 * @param ee the element entry
468 op_register_element (struct OperationState *eo, struct ElementEntry *ee)
471 struct IBF_Key ibf_key;
474 ibf_key = get_ibf_key (&ee->element_hash, eo->spec->salt);
475 k = GNUNET_new (struct KeyEntry);
477 k->ibf_key = ibf_key;
478 ret = GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element,
479 (uint32_t) ibf_key.key_val,
480 op_register_element_iterator, k);
482 /* was the element inserted into a colliding bucket? */
483 if (GNUNET_SYSERR == ret)
486 GNUNET_CONTAINER_multihashmap32_put (eo->key_to_element, (uint32_t) ibf_key.key_val, k,
487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
492 * Insert a key into an ibf.
496 * @param value the key entry to get the key from
499 prepare_ibf_iterator (void *cls,
503 struct InvertibleBloomFilter *ibf = cls;
504 struct KeyEntry *ke = value;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "inserting %x into ibf\n", ke->ibf_key.key_val);
508 ibf_insert (ibf, ke->ibf_key);
514 * Iterator for initializing the
515 * key-to-element mapping of a intersection operation
517 * @param cls the intersection operation
519 * @param value the element entry to insert
520 * into the key-to-element mapping
521 * @return GNUNET_YES to continue iterating,
525 init_key_to_element_iterator (void *cls,
526 const struct GNUNET_HashCode *key,
529 struct OperationState *eo = cls;
530 struct ElementEntry *e = value;
532 /* make sure that the element belongs to the set at the time
533 * of creating the operation */
534 if ( (e->generation_added > eo->generation_created) ||
535 ( (GNUNET_YES == e->removed) &&
536 (e->generation_removed < eo->generation_created)))
539 GNUNET_assert (GNUNET_NO == e->remote);
541 op_register_element (eo, e);
547 * Create an ibf with the operation's elements
548 * of the specified size
550 * @param eo the intersection operation
551 * @param size size of the ibf to create
554 prepare_ibf (struct OperationState *eo, uint16_t size)
556 if (NULL == eo->key_to_element)
559 len = GNUNET_CONTAINER_multihashmap_size (eo->set->elements);
560 eo->key_to_element = GNUNET_CONTAINER_multihashmap32_create (len + 1);
561 GNUNET_CONTAINER_multihashmap_iterate (eo->set->elements,
562 init_key_to_element_iterator, eo);
564 if (NULL != eo->local_ibf)
565 ibf_destroy (eo->local_ibf);
566 eo->local_ibf = ibf_create (size, SE_IBF_HASH_NUM);
567 GNUNET_CONTAINER_multihashmap32_iterate (eo->key_to_element,
568 prepare_ibf_iterator, eo->local_ibf);
573 * Send an ibf of appropriate size.
575 * @param eo the intersection operation
576 * @param ibf_order order of the ibf to send, size=2^order
579 send_ibf (struct OperationState *eo, uint16_t ibf_order)
581 unsigned int buckets_sent = 0;
582 struct InvertibleBloomFilter *ibf;
584 prepare_ibf (eo, 1<<ibf_order);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending ibf of size %u\n", 1<<ibf_order);
590 while (buckets_sent < (1 << ibf_order))
592 unsigned int buckets_in_message;
593 struct GNUNET_MQ_Envelope *ev;
594 struct IBFMessage *msg;
596 buckets_in_message = (1 << ibf_order) - buckets_sent;
597 /* limit to maximum */
598 if (buckets_in_message > MAX_BUCKETS_PER_MESSAGE)
599 buckets_in_message = MAX_BUCKETS_PER_MESSAGE;
601 ev = GNUNET_MQ_msg_extra (msg, buckets_in_message * IBF_BUCKET_SIZE,
602 GNUNET_MESSAGE_TYPE_SET_P2P_IBF);
604 msg->order = ibf_order;
605 msg->offset = htons (buckets_sent);
606 ibf_write_slice (ibf, buckets_sent,
607 buckets_in_message, &msg[1]);
608 buckets_sent += buckets_in_message;
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ibf chunk size %u, %u/%u sent\n",
610 buckets_in_message, buckets_sent, 1<<ibf_order);
611 GNUNET_MQ_send (eo->mq, ev);
614 eo->phase = PHASE_EXPECT_ELEMENTS_AND_REQUESTS;
619 * Send a strata estimator to the remote peer.
621 * @param eo the intersection operation with the remote peer
624 send_strata_estimator (struct OperationState *eo)
626 struct GNUNET_MQ_Envelope *ev;
627 struct GNUNET_MessageHeader *strata_msg;
629 ev = GNUNET_MQ_msg_header_extra (strata_msg,
630 SE_STRATA_COUNT * IBF_BUCKET_SIZE * SE_IBF_SIZE,
631 GNUNET_MESSAGE_TYPE_SET_P2P_SE);
632 strata_estimator_write (eo->set->state->se, &strata_msg[1]);
633 GNUNET_MQ_send (eo->mq, ev);
634 eo->phase = PHASE_EXPECT_BF;
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent SE, expecting IBF\n");
640 * Compute the necessary order of an ibf
641 * from the size of the symmetric set difference.
643 * @param diff the difference
644 * @return the required size of the ibf
647 get_order_from_difference (unsigned int diff)
649 unsigned int ibf_order;
652 while ((1<<ibf_order) < (IBF_ALPHA * diff) || (1<<ibf_order) < SE_IBF_HASH_NUM)
654 if (ibf_order > MAX_IBF_ORDER)
655 ibf_order = MAX_IBF_ORDER;
661 * Handle a strata estimator from a remote peer
663 * @param cls the intersection operation
664 * @param mh the message
667 handle_p2p_strata_estimator (void *cls, const struct GNUNET_MessageHeader *mh)
669 struct OperationState *eo = cls;
670 struct StrataEstimator *remote_se;
673 if (eo->phase != PHASE_EXPECT_SE)
675 fail_intersection_operation (eo);
679 remote_se = strata_estimator_create (SE_STRATA_COUNT, SE_IBF_SIZE,
681 strata_estimator_read (&mh[1], remote_se);
682 GNUNET_assert (NULL != eo->se);
683 diff = strata_estimator_difference (remote_se, eo->se);
684 strata_estimator_destroy (remote_se);
685 strata_estimator_destroy (eo->se);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got se diff=%d, using ibf size %d\n",
688 diff, 1<<get_order_from_difference (diff));
689 send_ibf (eo, get_order_from_difference (diff));
695 * Iterator to send elements to a remote peer
697 * @param cls closure with the element key and the intersection operation
699 * @param value the key entry
702 send_element_iterator (void *cls,
706 struct SendElementClosure *sec = cls;
707 struct IBF_Key ibf_key = sec->ibf_key;
708 struct OperationState *eo = sec->eo;
709 struct KeyEntry *ke = value;
711 if (ke->ibf_key.key_val != ibf_key.key_val)
715 const struct GNUNET_SET_Element *const element = &ke->element->element;
716 struct GNUNET_MQ_Envelope *ev;
717 struct GNUNET_MessageHeader *mh;
719 GNUNET_assert (ke->ibf_key.key_val == ibf_key.key_val);
720 ev = GNUNET_MQ_msg_header_extra (mh, element->size, GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS);
723 /* element too large */
727 memcpy (&mh[1], element->data, element->size);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (%s) to peer\n",
729 GNUNET_h2s (&ke->element->element_hash));
730 GNUNET_MQ_send (eo->mq, ev);
731 ke = ke->next_colliding;
737 * Send all elements that have the specified IBF key
738 * to the remote peer of the intersection operation
740 * @param eo intersection operation
741 * @param ibf_key IBF key of interest
744 send_elements_for_key (struct OperationState *eo, struct IBF_Key ibf_key)
746 struct SendElementClosure send_cls;
748 send_cls.ibf_key = ibf_key;
750 GNUNET_CONTAINER_multihashmap32_get_multiple (eo->key_to_element, (uint32_t) ibf_key.key_val,
751 &send_element_iterator, &send_cls);
756 * Decode which elements are missing on each side, and
757 * send the appropriate elemens and requests
759 * @param eo intersection operation
762 decode_and_send (struct OperationState *eo)
765 struct IBF_Key last_key;
767 unsigned int num_decoded;
768 struct InvertibleBloomFilter *diff_ibf;
770 GNUNET_assert (PHASE_EXPECT_ELEMENTS == eo->phase);
772 prepare_ibf (eo, eo->remote_ibf->size);
773 diff_ibf = ibf_dup (eo->local_ibf);
774 ibf_subtract (diff_ibf, eo->remote_ibf);
776 ibf_destroy (eo->remote_ibf);
777 eo->remote_ibf = NULL;
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoding IBF (size=%u)\n", diff_ibf->size);
782 last_key.key_val = 0;
787 int cycle_detected = GNUNET_NO;
791 res = ibf_decode (diff_ibf, &side, &key);
792 if (res == GNUNET_OK)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "decoded ibf key %lx\n",
797 if (num_decoded > diff_ibf->size || (num_decoded > 1 && last_key.key_val == key.key_val))
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "detected cyclic ibf (decoded %u/%u)\n",
800 num_decoded, diff_ibf->size);
801 cycle_detected = GNUNET_YES;
804 if ((GNUNET_SYSERR == res) || (GNUNET_YES == cycle_detected))
808 while (1<<next_order < diff_ibf->size)
811 if (next_order <= MAX_IBF_ORDER)
813 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
814 "decoding failed, sending larger ibf (size %u)\n",
816 send_ibf (eo, next_order);
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 "set intersection failed: reached ibf limit\n");
825 if (GNUNET_NO == res)
827 struct GNUNET_MQ_Envelope *ev;
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmitted all values, sending DONE\n");
830 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
831 GNUNET_MQ_send (eo->mq, ev);
836 send_elements_for_key (eo, key);
840 struct GNUNET_MQ_Envelope *ev;
841 struct GNUNET_MessageHeader *msg;
843 /* FIXME: before sending the request, check if we may just have the element */
844 /* FIXME: merge multiple requests */
845 /* FIXME: remember somewhere that we already requested the element,
846 * so that we don't request it again with the next ibf if decoding fails */
847 ev = GNUNET_MQ_msg_header_extra (msg, sizeof (struct IBF_Key),
848 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS);
850 *(struct IBF_Key *) &msg[1] = key;
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element request\n");
852 GNUNET_MQ_send (eo->mq, ev);
859 ibf_destroy (diff_ibf);
864 * Handle an IBF message from a remote peer.
866 * @param cls the intersection operation
867 * @param mh the header of the message
870 handle_p2p_ibf (void *cls, const struct GNUNET_MessageHeader *mh)
872 struct OperationState *eo = cls;
873 struct IBFMessage *msg = (struct IBFMessage *) mh;
874 unsigned int buckets_in_message;
876 if ( (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS) ||
877 (eo->phase == PHASE_EXPECT_BF) )
879 eo->phase = PHASE_EXPECT_BF_CONT;
880 GNUNET_assert (NULL == eo->remote_ibf);
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "creating new ibf of size %u\n", 1<<msg->order);
882 eo->remote_ibf = ibf_create (1<<msg->order, SE_IBF_HASH_NUM);
883 eo->ibf_buckets_received = 0;
884 if (0 != ntohs (msg->offset))
887 fail_intersection_operation (eo);
891 else if (eo->phase == PHASE_EXPECT_BF_CONT)
893 if ( (ntohs (msg->offset) != eo->ibf_buckets_received) ||
894 (1<<msg->order != eo->remote_ibf->size) )
897 fail_intersection_operation (eo);
902 buckets_in_message = (ntohs (msg->header.size) - sizeof *msg) / IBF_BUCKET_SIZE;
904 if (0 == buckets_in_message)
907 fail_intersection_operation (eo);
911 if ((ntohs (msg->header.size) - sizeof *msg) != buckets_in_message * IBF_BUCKET_SIZE)
914 fail_intersection_operation (eo);
918 ibf_read_slice (&msg[1], eo->ibf_buckets_received, buckets_in_message, eo->remote_ibf);
919 eo->ibf_buckets_received += buckets_in_message;
921 if (eo->ibf_buckets_received == eo->remote_ibf->size)
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received full ibf\n");
924 eo->phase = PHASE_EXPECT_ELEMENTS;
925 decode_and_send (eo);
931 * Send a result message to the client indicating
932 * that there is a new element.
934 * @param eo intersection operation
935 * @param element element to send
938 send_client_element (struct OperationState *eo,
939 struct GNUNET_SET_Element *element)
941 struct GNUNET_MQ_Envelope *ev;
942 struct GNUNET_SET_ResultMessage *rm;
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client\n", element->size);
945 GNUNET_assert (0 != eo->spec->client_request_id);
946 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
949 GNUNET_MQ_discard (ev);
953 rm->result_status = htons (GNUNET_SET_STATUS_OK);
954 rm->request_id = htonl (eo->spec->client_request_id);
955 rm->element_type = element->type;
956 memcpy (&rm[1], element->data, element->size);
957 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
962 * Send a result message to the client indicating
963 * that the operation is over.
964 * After the result done message has been sent to the client,
965 * destroy the evaluate operation.
967 * @param eo intersection operation
970 send_client_done_and_destroy (struct OperationState *eo)
972 struct GNUNET_MQ_Envelope *ev;
973 struct GNUNET_SET_ResultMessage *rm;
975 GNUNET_assert (GNUNET_NO == eo->client_done_sent);
977 eo->client_done_sent = GNUNET_YES;
979 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
980 rm->request_id = htonl (eo->spec->client_request_id);
981 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
982 rm->element_type = htons (0);
983 GNUNET_MQ_send (eo->spec->set->client_mq, ev);
985 intersection_operation_destroy (eo);
990 * Handle an element message from a remote peer.
992 * @param cls the intersection operation
993 * @param mh the message
996 handle_p2p_elements (void *cls, const struct GNUNET_MessageHeader *mh)
998 struct OperationState *eo = cls;
999 struct ElementEntry *ee;
1000 uint16_t element_size;
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got element from peer\n");
1004 if ( (eo->phase != PHASE_EXPECT_ELEMENTS) &&
1005 (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS) )
1007 fail_intersection_operation (eo);
1011 element_size = ntohs (mh->size) - sizeof (struct GNUNET_MessageHeader);
1012 ee = GNUNET_malloc (sizeof *ee + element_size);
1013 memcpy (&ee[1], &mh[1], element_size);
1014 ee->element.size = element_size;
1015 ee->element.data = &ee[1];
1016 ee->remote = GNUNET_YES;
1017 GNUNET_CRYPTO_hash (ee->element.data, ee->element.size, &ee->element_hash);
1019 /* FIXME: see if the element has already been inserted! */
1021 op_register_element (eo, ee);
1022 send_client_element (eo, &ee->element);
1027 * Handle an element request from a remote peer.
1029 * @param cls the intersection operation
1030 * @param mh the message
1033 handle_p2p_element_requests (void *cls, const struct GNUNET_MessageHeader *mh)
1035 struct OperationState *eo = cls;
1036 struct IBF_Key *ibf_key;
1037 unsigned int num_keys;
1039 /* look up elements and send them */
1040 if (eo->phase != PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1043 fail_intersection_operation (eo);
1047 num_keys = (ntohs (mh->size) - sizeof *mh) / sizeof (struct IBF_Key);
1049 if ((ntohs (mh->size) - sizeof *mh) != num_keys * sizeof (struct IBF_Key))
1052 fail_intersection_operation (eo);
1056 ibf_key = (struct IBF_Key *) &mh[1];
1057 while (0 != num_keys--)
1059 send_elements_for_key (eo, *ibf_key);
1066 * Handle a done message from a remote peer
1068 * @param cls the intersection operation
1069 * @param mh the message
1072 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
1074 struct OperationState *eo = cls;
1075 struct GNUNET_MQ_Envelope *ev;
1077 if (eo->phase == PHASE_EXPECT_ELEMENTS_AND_REQUESTS)
1079 /* we got all requests, but still have to send our elements as response */
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got DONE, sending final DONE after elements\n");
1082 eo->phase = PHASE_FINISHED;
1083 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
1084 GNUNET_MQ_send (eo->mq, ev);
1087 if (eo->phase == PHASE_EXPECT_ELEMENTS)
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
1090 eo->phase = PHASE_FINISHED;
1091 send_client_done_and_destroy (eo);
1095 fail_intersection_operation (eo);
1100 * Evaluate a intersection operation with
1103 * @param spec specification of the operation the evaluate
1104 * @param tunnel tunnel already connected to the partner peer
1105 * @param tc tunnel context, passed here so all new incoming
1106 * messages are directly going to the intersection operations
1107 * @return a handle to the operation
1110 intersection_evaluate (struct OperationSpecification *spec,
1111 struct GNUNET_MESH_Tunnel *tunnel,
1112 struct TunnelContext *tc)
1114 struct OperationState *eo;
1116 eo = GNUNET_new (struct OperationState);
1117 tc->vt = _GSS_intersection_vt ();
1119 eo->se = strata_estimator_dup (spec->set->state->se);
1120 eo->generation_created = spec->set->current_generation++;
1121 eo->set = spec->set;
1123 eo->tunnel = tunnel;
1124 eo->mq = GNUNET_MESH_mq_create (tunnel);
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "evaluating intersection operation, (app %s)\n",
1128 GNUNET_h2s (&eo->spec->app_id));
1130 /* we started the operation, thus we have to send the operation request */
1131 eo->phase = PHASE_EXPECT_SE;
1133 GNUNET_CONTAINER_DLL_insert (eo->set->state->ops_head,
1134 eo->set->state->ops_tail,
1137 send_operation_request (eo);
1142 * Accept an intersection operation request from a remote peer
1144 * @param spec all necessary information about the operation
1145 * @param tunnel open tunnel to the partner's peer
1146 * @param tc tunnel context, passed here so all new incoming
1147 * messages are directly going to the intersection operations
1151 intersection_accept (struct OperationSpecification *spec,
1152 struct GNUNET_MESH_Tunnel *tunnel,
1153 struct TunnelContext *tc)
1155 struct OperationState *eo;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set intersection operation\n");
1159 eo = GNUNET_new (struct OperationState);
1160 tc->vt = _GSS_intersection_vt ();
1162 eo->set = spec->set;
1163 eo->generation_created = eo->set->current_generation++;
1165 eo->tunnel = tunnel;
1166 eo->mq = GNUNET_MESH_mq_create (tunnel);
1167 eo->se = strata_estimator_dup (eo->set->state->se);
1168 /* transfer ownership of mq and socket from incoming to eo */
1169 GNUNET_CONTAINER_DLL_insert (eo->set->state->ops_head,
1170 eo->set->state->ops_tail,
1172 /* kick off the operation */
1173 send_strata_estimator (eo);
1178 * Create a new set supporting the intersection operation
1180 * @return the newly created set
1182 static struct SetState *
1183 intersection_set_create (void)
1185 struct SetState *set_state;
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "intersection set created\n");
1189 set_state = GNUNET_new (struct SetState);
1190 set_state->se = strata_estimator_create (SE_STRATA_COUNT,
1191 SE_IBF_SIZE, SE_IBF_HASH_NUM);
1197 * Add the element from the given element message to the set.
1199 * @param set_state state of the set want to add to
1200 * @param ee the element to add to the set
1203 intersection_add (struct SetState *set_state, struct ElementEntry *ee)
1205 strata_estimator_insert (set_state->se, get_ibf_key (&ee->element_hash, 0));
1210 * Destroy a set that supports the intersection operation
1212 * @param set_state the set to destroy
1215 intersection_set_destroy (struct SetState *set_state)
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection set\n");
1218 /* important to destroy operations before the rest of the set */
1219 while (NULL != set_state->ops_head)
1220 intersection_operation_destroy (set_state->ops_head);
1221 if (NULL != set_state->se)
1223 strata_estimator_destroy (set_state->se);
1224 set_state->se = NULL;
1226 GNUNET_free (set_state);
1231 * Remove the element given in the element message from the set.
1232 * Only marks the element as removed, so that older set operations can still exchange it.
1234 * @param set_state state of the set to remove from
1235 * @param element set element to remove
1238 intersection_remove (struct SetState *set_state, struct ElementEntry *element)
1240 /* FIXME: remove from strata estimator */
1245 * Dispatch messages for a intersection operation.
1247 * @param eo the state of the intersection evaluate operation
1248 * @param mh the received message
1249 * @return GNUNET_SYSERR if the tunnel should be disconnected,
1250 * GNUNET_OK otherwise
1253 intersection_handle_p2p_message (struct OperationState *eo,
1254 const struct GNUNET_MessageHeader *mh)
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
1257 ntohs (mh->type), ntohs (mh->size));
1258 switch (ntohs (mh->type))
1260 case GNUNET_MESSAGE_TYPE_SET_P2P_IBF:
1261 handle_p2p_ibf (eo, mh);
1263 case GNUNET_MESSAGE_TYPE_SET_P2P_SE:
1264 handle_p2p_strata_estimator (eo, mh);
1266 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS:
1267 handle_p2p_elements (eo, mh);
1269 case GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS:
1270 handle_p2p_element_requests (eo, mh);
1272 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
1273 handle_p2p_done (eo, mh);
1276 /* something wrong with mesh's message handlers? */
1284 intersection_peer_disconnect (struct OperationState *op)
1286 /* Are we already disconnected? */
1287 if (NULL == op->tunnel)
1292 GNUNET_MQ_destroy (op->mq);
1295 if (PHASE_FINISHED != op->phase)
1297 struct GNUNET_MQ_Envelope *ev;
1298 struct GNUNET_SET_ResultMessage *msg;
1300 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
1301 msg->request_id = htonl (op->spec->client_request_id);
1302 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1303 msg->element_type = htons (0);
1304 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
1306 intersection_operation_destroy (op);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
1310 if (GNUNET_NO == op->client_done_sent)
1311 send_client_done_and_destroy (op);
1316 intersection_op_cancel (struct SetState *set_state, uint32_t op_id)
1318 /* FIXME: implement */
1322 const struct SetVT *
1323 _GSS_intersection_vt ()
1325 static const struct SetVT intersection_vt = {
1326 .create = &intersection_set_create,
1327 .msg_handler = &intersection_handle_p2p_message,
1328 .add = &intersection_add,
1329 .remove = &intersection_remove,
1330 .destroy_set = &intersection_set_destroy,
1331 .evaluate = &intersection_evaluate,
1332 .accept = &intersection_accept,
1333 .peer_disconnect = &intersection_peer_disconnect,
1334 .cancel = &intersection_op_cancel,
1337 return &intersection_vt;