2 This file is part of GNUnet
3 (C) 2013, 2014 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.
21 * @file set/gnunet-service-set_intersection.c
22 * @brief two-peer set intersection
23 * @author Christian Fuchs
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-set.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet-service-set_protocol.h"
35 * Current phase we are in for a intersection operation.
37 enum IntersectionOperationPhase
40 * Alices has suggested an operation to bob,
41 * and is waiting for a bf or session end.
46 * Bob has accepted the operation, Bob and Alice are now exchanging bfs
47 * until one notices the their element hashes are equal.
52 * The protocol is over. Results may still have to be sent to the
60 * State of an evaluate operation with another peer.
65 * The bf we currently receive
67 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
70 * BF of the set's element.
72 struct GNUNET_CONTAINER_BloomFilter *local_bf;
75 * Remaining elements in the intersection operation.
76 * Maps element-id-hashes to 'elements in our set'.
78 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
81 * Iterator for sending the final set of @e my_elements to the client.
83 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
86 * Evaluate operations are held in a linked list.
88 struct OperationState *next;
91 * Evaluate operations are held in a linked list.
93 struct OperationState *prev;
96 * For multipart BF transmissions, we have to store the
97 * bloomfilter-data until we fully received it.
102 * XOR of the keys of all of the elements (remaining) in my set.
103 * Always updated when elements are added or removed to
106 struct GNUNET_HashCode my_xor;
109 * XOR of the keys of all of the elements (remaining) in
110 * the other peer's set. Updated when we receive the
111 * other peer's Bloom filter.
113 struct GNUNET_HashCode other_xor;
116 * How many bytes of @e bf_data are valid?
118 uint32_t bf_data_offset;
121 * Current element count contained within @e my_elements.
122 * (May differ briefly during initialization.)
124 uint32_t my_element_count;
127 * size of the bloomfilter in @e bf_data.
129 uint32_t bf_data_size;
132 * size of the bloomfilter
134 uint32_t bf_bits_per_element;
137 * Salt currently used for BF construction (by us or the other peer,
138 * depending on where we are in the code).
143 * Current state of the operation.
145 enum IntersectionOperationPhase phase;
148 * Generation in which the operation handle
151 unsigned int generation_created;
154 * Did we send the client that we are done?
156 int client_done_sent;
161 * Extra state required for efficient set intersection.
162 * Merely tracks the total number of elements.
167 * Number of currently valid elements in the set which have not been
170 uint32_t current_set_element_count;
175 * If applicable in the current operation mode, send a result message
176 * to the client indicating we removed an element.
178 * @param op intersection operation
179 * @param element element to send
182 send_client_removed_element (struct Operation *op,
183 struct GNUNET_SET_Element *element)
185 struct GNUNET_MQ_Envelope *ev;
186 struct GNUNET_SET_ResultMessage *rm;
188 if (GNUNET_SET_RESULT_REMOVED != op->spec->result_mode)
189 return; /* Wrong mode for transmitting removed elements */
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Sending removed element (size %u) to client\n",
193 GNUNET_assert (0 != op->spec->client_request_id);
194 ev = GNUNET_MQ_msg_extra (rm,
196 GNUNET_MESSAGE_TYPE_SET_RESULT);
202 rm->result_status = htons (GNUNET_SET_STATUS_OK);
203 rm->request_id = htonl (op->spec->client_request_id);
204 rm->element_type = element->element_type;
208 GNUNET_MQ_send (op->spec->set->client_mq,
214 * Fills the "my_elements" hashmap with all relevant elements.
216 * @param cls the `struct Operation *` we are performing
217 * @param key current key code
218 * @param value the `struct ElementEntry *` from the hash map
219 * @return #GNUNET_YES (we should continue to iterate)
222 filtered_map_initialization (void *cls,
223 const struct GNUNET_HashCode *key,
226 struct Operation *op = cls;
227 struct ElementEntry *ee = value;
228 struct GNUNET_HashCode mutated_hash;
230 if ( (op->generation_created < ee->generation_removed) &&
231 (op->generation_created >= ee->generation_added) )
232 return GNUNET_YES; /* element not valid in our operation's generation */
234 /* Test if element is in Bob's bloomfilter */
235 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
239 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
242 /* remove this element */
243 send_client_removed_element (op,
247 op->state->my_element_count++;
248 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
251 GNUNET_break (GNUNET_YES ==
252 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
262 * Removes elements from our hashmap if they are not contained within the
263 * provided remote bloomfilter.
265 * @param cls closure with the `struct Operation *`
266 * @param key current key code
267 * @param value value in the hash map
268 * @return #GNUNET_YES (we should continue to iterate)
271 iterator_bf_reduce (void *cls,
272 const struct GNUNET_HashCode *key,
275 struct Operation *op = cls;
276 struct ElementEntry *ee = value;
277 struct GNUNET_HashCode mutated_hash;
279 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
283 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
286 GNUNET_break (0 < op->state->my_element_count);
287 op->state->my_element_count--;
288 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
291 GNUNET_assert (GNUNET_YES ==
292 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
295 send_client_removed_element (op,
303 * Create initial bloomfilter based on all the elements given.
305 * @param cls the `struct Operation *`
306 * @param key current key code
307 * @param value the `struct ElementEntry` to process
308 * @return #GNUNET_YES (we should continue to iterate)
311 iterator_bf_create (void *cls,
312 const struct GNUNET_HashCode *key,
315 struct Operation *op = cls;
316 struct ElementEntry *ee = value;
317 struct GNUNET_HashCode mutated_hash;
319 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
322 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
329 * Inform the client that the intersection operation has failed,
330 * and proceed to destroy the evaluate operation.
332 * @param op the intersection operation to fail
335 fail_intersection_operation (struct Operation *op)
337 struct GNUNET_MQ_Envelope *ev;
338 struct GNUNET_SET_ResultMessage *msg;
340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
341 "Intersection operation failed\n");
342 if (NULL != op->state->my_elements)
344 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
345 op->state->my_elements = NULL;
347 ev = GNUNET_MQ_msg (msg,
348 GNUNET_MESSAGE_TYPE_SET_RESULT);
349 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
350 msg->request_id = htonl (op->spec->client_request_id);
351 msg->element_type = htons (0);
352 GNUNET_MQ_send (op->spec->set->client_mq,
354 _GSS_operation_destroy (op,
360 * Send a bloomfilter to our peer. After the result done message has
361 * been sent to the client, destroy the evaluate operation.
363 * @param op intersection operation
366 send_bloomfilter (struct Operation *op)
368 struct GNUNET_MQ_Envelope *ev;
369 struct BFMessage *msg;
371 uint32_t bf_elementbits;
373 struct GNUNET_CONTAINER_BloomFilter *local_bf;
377 /* We consider the ratio of the set sizes to determine
378 the number of bits per element, as the smaller set
379 should use more bits to maximize its set reduction
380 potential and minimize overall bandwidth consumption. */
381 bf_elementbits = 2 + ceil (log2((double)
382 (op->spec->remote_element_count /
383 (double) op->state->my_element_count)));
384 if (bf_elementbits < 1)
385 bf_elementbits = 1; /* make sure k is not 0 */
386 /* optimize BF-size to ~50% of bits set */
387 bf_size = ceil ((double) (op->state->my_element_count
388 * bf_elementbits / log(2)));
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Sending bf of size %u\n",
391 (unsigned int) bf_size);
392 local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
395 op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
397 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
401 /* send our Bloom filter */
402 chunk_size = 60 * 1024 - sizeof (struct BFMessage);
403 if (bf_size <= chunk_size)
406 chunk_size = bf_size;
407 ev = GNUNET_MQ_msg_extra (msg,
409 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
410 GNUNET_assert (GNUNET_SYSERR !=
411 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
414 msg->sender_element_count = htonl (op->state->my_element_count);
415 msg->bloomfilter_total_length = htonl (bf_size);
416 msg->bits_per_element = htonl (bf_elementbits);
417 msg->sender_mutator = htonl (op->state->salt);
418 msg->element_xor_hash = op->state->my_xor;
419 GNUNET_MQ_send (op->mq, ev);
424 bf_data = GNUNET_malloc (bf_size);
425 GNUNET_assert (GNUNET_SYSERR !=
426 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
430 while (offset < bf_size)
432 if (bf_size - chunk_size < offset)
433 chunk_size = bf_size - offset;
434 ev = GNUNET_MQ_msg_extra (msg,
436 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
440 offset += chunk_size;
441 msg->sender_element_count = htonl (op->state->my_element_count);
442 msg->bloomfilter_total_length = htonl (bf_size);
443 msg->bits_per_element = htonl (bf_elementbits);
444 msg->sender_mutator = htonl (op->state->salt);
445 msg->element_xor_hash = op->state->my_xor;
446 GNUNET_MQ_send (op->mq, ev);
448 GNUNET_free (bf_data);
450 GNUNET_CONTAINER_bloomfilter_free (local_bf);
455 * Signal to the client that the operation has finished and
456 * destroy the operation.
458 * @param cls operation to destroy
461 send_client_done_and_destroy (void *cls)
463 struct Operation *op = cls;
464 struct GNUNET_MQ_Envelope *ev;
465 struct GNUNET_SET_ResultMessage *rm;
467 ev = GNUNET_MQ_msg (rm,
468 GNUNET_MESSAGE_TYPE_SET_RESULT);
469 rm->request_id = htonl (op->spec->client_request_id);
470 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
471 rm->element_type = htons (0);
472 GNUNET_MQ_send (op->spec->set->client_mq,
474 _GSS_operation_destroy (op,
480 * Send all elements in the full result iterator.
482 * @param cls the `struct Operation *`
485 send_remaining_elements (void *cls)
487 struct Operation *op = cls;
489 const struct ElementEntry *ee;
490 struct GNUNET_MQ_Envelope *ev;
491 struct GNUNET_SET_ResultMessage *rm;
492 const struct GNUNET_SET_Element *element;
495 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter,
498 if (GNUNET_NO == res)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Sending done and destroy because iterator ran out\n");
502 send_client_done_and_destroy (op);
506 element = &ee->element;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Sending element (size %u) to client (full set)\n",
510 GNUNET_assert (0 != op->spec->client_request_id);
511 ev = GNUNET_MQ_msg_extra (rm,
513 GNUNET_MESSAGE_TYPE_SET_RESULT);
514 GNUNET_assert (NULL != ev);
515 rm->result_status = htons (GNUNET_SET_STATUS_OK);
516 rm->request_id = htonl (op->spec->client_request_id);
517 rm->element_type = element->element_type;
521 GNUNET_MQ_notify_sent (ev,
522 &send_remaining_elements,
524 GNUNET_MQ_send (op->spec->set->client_mq,
530 * Inform the peer that this operation is complete.
532 * @param op the intersection operation to fail
535 send_peer_done (struct Operation *op)
537 struct GNUNET_MQ_Envelope *ev;
538 struct IntersectionDoneMessage *idm;
540 op->state->phase = PHASE_FINISHED;
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Intersection succeeded, sending DONE\n");
543 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
544 op->state->local_bf = NULL;
546 ev = GNUNET_MQ_msg (idm,
547 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE);
548 idm->final_element_count = htonl (op->state->my_element_count);
549 idm->element_xor_hash = op->state->my_xor;
550 GNUNET_MQ_send (op->mq,
556 * Process a Bloomfilter once we got all the chunks.
558 * @param op the intersection operation
561 process_bf (struct Operation *op)
563 switch (op->state->phase)
566 /* This is the first BF being sent, build our initial map with
567 filtering in place */
568 op->state->my_elements
569 = GNUNET_CONTAINER_multihashmap_create (op->spec->remote_element_count,
571 GNUNET_break (0 == op->state->my_element_count);
572 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
573 &filtered_map_initialization,
576 case PHASE_BF_EXCHANGE:
577 /* Update our set by reduction */
578 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
584 fail_intersection_operation(op);
587 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
588 op->state->remote_bf = NULL;
590 if ( (0 == op->state->my_element_count) || /* fully disjoint */
591 ( (op->state->my_element_count == op->spec->remote_element_count) &&
592 (0 == memcmp (&op->state->my_xor,
593 &op->state->other_xor,
594 sizeof (struct GNUNET_HashCode))) ) )
600 op->state->phase = PHASE_BF_EXCHANGE;
601 send_bloomfilter (op);
606 * Handle an BF message from a remote peer.
608 * @param cls the intersection operation
609 * @param mh the header of the message
612 handle_p2p_bf (void *cls,
613 const struct GNUNET_MessageHeader *mh)
615 struct Operation *op = cls;
616 const struct BFMessage *msg;
619 uint32_t bf_bits_per_element;
622 msize = htons (mh->size);
623 if (msize < sizeof (struct BFMessage))
626 fail_intersection_operation (op);
629 msg = (const struct BFMessage *) mh;
630 switch (op->state->phase)
634 fail_intersection_operation (op);
636 case PHASE_BF_EXCHANGE:
637 bf_size = ntohl (msg->bloomfilter_total_length);
638 bf_bits_per_element = ntohl (msg->bits_per_element);
639 chunk_size = msize - sizeof (struct BFMessage);
640 op->state->other_xor = msg->element_xor_hash;
641 if (bf_size == chunk_size)
643 if (NULL != op->state->bf_data)
646 fail_intersection_operation (op);
649 /* single part, done here immediately */
651 = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
653 bf_bits_per_element);
654 op->state->salt = ntohl (msg->sender_mutator);
658 /* multipart chunk */
659 if (NULL == op->state->bf_data)
661 /* first chunk, initialize */
662 op->state->bf_data = GNUNET_malloc (bf_size);
663 op->state->bf_data_size = bf_size;
664 op->state->bf_bits_per_element = bf_bits_per_element;
665 op->state->bf_data_offset = 0;
666 op->state->salt = ntohl (msg->sender_mutator);
667 op->spec->remote_element_count = ntohl (msg->sender_element_count);
672 if ( (op->state->bf_data_size != bf_size) ||
673 (op->state->bf_bits_per_element != bf_bits_per_element) ||
674 (op->state->bf_data_offset + chunk_size > bf_size) ||
675 (op->state->salt != ntohl (msg->sender_mutator)) ||
676 (op->spec->remote_element_count != ntohl (msg->sender_element_count)) )
679 fail_intersection_operation (op);
683 memcpy (&op->state->bf_data[op->state->bf_data_offset],
684 (const char*) &msg[1],
686 op->state->bf_data_offset += chunk_size;
687 if (op->state->bf_data_offset == bf_size)
689 /* last chunk, run! */
691 = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
693 bf_bits_per_element);
694 GNUNET_free (op->state->bf_data);
695 op->state->bf_data = NULL;
696 op->state->bf_data_size = 0;
702 fail_intersection_operation (op);
709 * Fills the "my_elements" hashmap with the initial set of
710 * (non-deleted) elements from the set of the specification.
712 * @param cls closure with the `struct Operation *`
713 * @param key current key code for the element
714 * @param value value in the hash map with the `struct ElementEntry *`
715 * @return #GNUNET_YES (we should continue to iterate)
718 initialize_map_unfiltered (void *cls,
719 const struct GNUNET_HashCode *key,
722 struct ElementEntry *ee = value;
723 struct Operation *op = cls;
725 if ( (op->generation_created < ee->generation_removed) &&
726 (op->generation_created >= ee->generation_added) )
727 return GNUNET_YES; /* element not live in operation's generation */
728 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
731 GNUNET_break (GNUNET_YES ==
732 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
735 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
741 * Send our element count to the peer, in case our element count is
744 * @param op intersection operation
747 send_element_count (struct Operation *op)
749 struct GNUNET_MQ_Envelope *ev;
750 struct IntersectionElementInfoMessage *msg;
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "Sending our element count (bf_msg)\n");
754 ev = GNUNET_MQ_msg (msg,
755 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
756 msg->sender_element_count = htonl (op->state->my_element_count);
757 GNUNET_MQ_send (op->mq, ev);
762 * We go first, initialize our map with all elements and
763 * send the first Bloom filter.
765 * @param op operation to start exchange for
768 begin_bf_exchange (struct Operation *op)
770 GNUNET_break (PHASE_INITIAL == op->state->phase);
771 op->state->phase = PHASE_BF_EXCHANGE;
772 op->state->my_elements
773 = GNUNET_CONTAINER_multihashmap_create (op->state->my_element_count,
775 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
776 &initialize_map_unfiltered,
778 send_bloomfilter (op);
783 * Handle the initial `struct IntersectionElementInfoMessage` from a
786 * @param cls the intersection operation
787 * @param mh the header of the message
790 handle_p2p_element_info (void *cls,
791 const struct GNUNET_MessageHeader *mh)
793 struct Operation *op = cls;
794 const struct IntersectionElementInfoMessage *msg;
796 if (ntohs (mh->size) != sizeof (struct IntersectionElementInfoMessage))
799 fail_intersection_operation(op);
802 msg = (const struct IntersectionElementInfoMessage *) mh;
803 op->spec->remote_element_count = ntohl (msg->sender_element_count);
804 if ( (PHASE_INITIAL != op->state->phase) ||
805 (op->state->my_element_count > op->spec->remote_element_count) ||
806 (0 == op->state->my_element_count) ||
807 (0 == op->spec->remote_element_count) )
810 fail_intersection_operation(op);
813 GNUNET_break (NULL == op->state->remote_bf);
814 begin_bf_exchange (op);
819 * Send a result message to the client indicating that the operation
820 * is over. After the result done message has been sent to the
821 * client, destroy the evaluate operation.
823 * @param op intersection operation
826 finish_and_destroy (struct Operation *op)
828 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
830 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Sending full result set\n");
834 op->state->full_result_iter
835 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
836 send_remaining_elements (op);
839 send_client_done_and_destroy (op);
844 * Remove all elements from our hashmap.
846 * @param cls closure with the `struct Operation *`
847 * @param key current key code
848 * @param value value in the hash map
849 * @return #GNUNET_YES (we should continue to iterate)
852 filter_all (void *cls,
853 const struct GNUNET_HashCode *key,
856 struct Operation *op = cls;
857 struct ElementEntry *ee = value;
859 GNUNET_break (0 < op->state->my_element_count);
860 op->state->my_element_count--;
861 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
864 GNUNET_assert (GNUNET_YES ==
865 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
868 send_client_removed_element (op,
875 * Handle a done message from a remote peer
877 * @param cls the intersection operation
878 * @param mh the message
881 handle_p2p_done (void *cls,
882 const struct GNUNET_MessageHeader *mh)
884 struct Operation *op = cls;
885 const struct IntersectionDoneMessage *idm;
887 if (PHASE_BF_EXCHANGE != op->state->phase)
889 /* wrong phase to conclude? FIXME: Or should we allow this
890 if the other peer has _initially_ already an empty set? */
892 fail_intersection_operation (op);
895 if (ntohs (mh->size) != sizeof (struct IntersectionDoneMessage))
898 fail_intersection_operation (op);
901 idm = (const struct IntersectionDoneMessage *) mh;
902 if (0 == ntohl (idm->final_element_count))
904 /* other peer determined empty set is the intersection,
905 remove all elements */
906 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
910 if ( (op->state->my_element_count != ntohl (idm->final_element_count)) ||
911 (0 != memcmp (&op->state->my_xor,
912 &idm->element_xor_hash,
913 sizeof (struct GNUNET_HashCode))) )
915 /* Other peer thinks we are done, but we disagree on the result! */
917 fail_intersection_operation (op);
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 op->state->phase = PHASE_FINISHED;
923 finish_and_destroy (op);
928 * Initiate a set intersection operation with a remote peer.
930 * @param op operation that is created, should be initialized to
931 * begin the evaluation
932 * @param opaque_context message to be transmitted to the listener
933 * to convince him to accept, may be NULL
936 intersection_evaluate (struct Operation *op,
937 const struct GNUNET_MessageHeader *opaque_context)
939 struct GNUNET_MQ_Envelope *ev;
940 struct OperationRequestMessage *msg;
942 op->state = GNUNET_new (struct OperationState);
943 /* we started the operation, thus we have to send the operation request */
944 op->state->phase = PHASE_INITIAL;
945 op->state->my_element_count = op->spec->set->state->current_set_element_count;
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Initiating intersection operation evaluation");
949 ev = GNUNET_MQ_msg_nested_mh (msg,
950 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
954 /* the context message is too large!? */
956 GNUNET_SERVER_client_disconnect (op->spec->set->client);
959 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
960 msg->app_id = op->spec->app_id;
961 msg->element_count = htonl (op->state->my_element_count);
962 GNUNET_MQ_send (op->mq,
964 if (NULL != opaque_context)
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "Sent op request with context message\n");
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Sent op request without context message\n");
974 * Accept an intersection operation request from a remote peer. Only
975 * initializes the private operation state.
977 * @param op operation that will be accepted as an intersection operation
980 intersection_accept (struct Operation *op)
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Accepting set intersection operation\n");
984 op->state = GNUNET_new (struct OperationState);
985 op->state->phase = PHASE_INITIAL;
986 op->state->my_element_count
987 = op->spec->set->state->current_set_element_count;
988 op->state->my_elements
989 = GNUNET_CONTAINER_multihashmap_create
990 (GNUNET_MIN (op->state->my_element_count,
991 op->spec->remote_element_count),
993 if (op->spec->remote_element_count < op->state->my_element_count)
995 /* If the other peer (Alice) has fewer elements than us (Bob),
996 we just send the count as Alice should send the first BF */
997 send_element_count (op);
998 op->state->phase = PHASE_BF_EXCHANGE;
1001 /* We have fewer elements, so we start with the BF */
1002 begin_bf_exchange (op);
1007 * Dispatch messages for a intersection operation.
1009 * @param op the state of the intersection evaluate operation
1010 * @param mh the received message
1011 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
1012 * #GNUNET_OK otherwise
1015 intersection_handle_p2p_message (struct Operation *op,
1016 const struct GNUNET_MessageHeader *mh)
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Received p2p message (t: %u, s: %u)\n",
1020 ntohs (mh->type), ntohs (mh->size));
1021 switch (ntohs (mh->type))
1023 /* this message handler is not active until after we received an
1024 * operation request message, thus the ops request is not handled here
1026 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO:
1027 handle_p2p_element_info (op, mh);
1029 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF:
1030 handle_p2p_bf (op, mh);
1032 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE:
1033 handle_p2p_done (op, mh);
1036 /* something wrong with cadet's message handlers? */
1044 * Handler for peer-disconnects, notifies the client about the aborted
1045 * operation. If we did not expect anything from the other peer, we
1046 * gracefully terminate the operation.
1048 * @param op the destroyed operation
1051 intersection_peer_disconnect (struct Operation *op)
1053 if (PHASE_FINISHED != op->state->phase)
1055 fail_intersection_operation (op);
1058 /* the session has already been concluded */
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Other peer disconnected (finished)\n");
1061 if (GNUNET_NO == op->state->client_done_sent)
1062 finish_and_destroy (op);
1067 * Destroy the intersection operation. Only things specific to the
1068 * intersection operation are destroyed.
1070 * @param op intersection operation to destroy
1073 intersection_op_cancel (struct Operation *op)
1075 /* check if the op was canceled twice */
1076 GNUNET_assert (NULL != op->state);
1077 if (NULL != op->state->remote_bf)
1079 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1080 op->state->remote_bf = NULL;
1082 if (NULL != op->state->local_bf)
1084 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1085 op->state->local_bf = NULL;
1087 if (NULL != op->state->my_elements)
1089 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1090 op->state->my_elements = NULL;
1092 GNUNET_free (op->state);
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Destroying intersection op state done\n");
1100 * Create a new set supporting the intersection operation.
1102 * @return the newly created set
1104 static struct SetState *
1105 intersection_set_create ()
1107 struct SetState *set_state;
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Intersection set created\n");
1111 set_state = GNUNET_new (struct SetState);
1112 set_state->current_set_element_count = 0;
1119 * Add the element from the given element message to the set.
1121 * @param set_state state of the set want to add to
1122 * @param ee the element to add to the set
1125 intersection_add (struct SetState *set_state,
1126 struct ElementEntry *ee)
1128 set_state->current_set_element_count++;
1133 * Destroy a set that supports the intersection operation
1135 * @param set_state the set to destroy
1138 intersection_set_destroy (struct SetState *set_state)
1140 GNUNET_free (set_state);
1145 * Remove the element given in the element message from the set.
1147 * @param set_state state of the set to remove from
1148 * @param element set element to remove
1151 intersection_remove (struct SetState *set_state,
1152 struct ElementEntry *element)
1154 GNUNET_assert (0 < set_state->current_set_element_count);
1155 set_state->current_set_element_count--;
1160 * Get the table with implementing functions for set intersection.
1162 * @return the operation specific VTable
1164 const struct SetVT *
1165 _GSS_intersection_vt ()
1167 static const struct SetVT intersection_vt = {
1168 .create = &intersection_set_create,
1169 .msg_handler = &intersection_handle_p2p_message,
1170 .add = &intersection_add,
1171 .remove = &intersection_remove,
1172 .destroy_set = &intersection_set_destroy,
1173 .evaluate = &intersection_evaluate,
1174 .accept = &intersection_accept,
1175 .peer_disconnect = &intersection_peer_disconnect,
1176 .cancel = &intersection_op_cancel,
1179 return &intersection_vt;