2 This file is part of GNUnet
3 Copyright (C) 2013-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file set/gnunet-service-set_intersection.c
20 * @brief two-peer set intersection
21 * @author Christian Fuchs
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
27 #include "gnunet-service-set.h"
28 #include "gnunet_block_lib.h"
29 #include "gnunet-service-set_protocol.h"
30 #include "gnunet-service-set_intersection.h"
35 * Current phase we are in for a intersection operation.
37 enum IntersectionOperationPhase
40 * We are just starting.
45 * We have send the number of our elements to the other
46 * peer, but did not setup our element set yet.
51 * We have initialized our set and are now reducing it by exchanging
52 * Bloom filters until one party notices the their element hashes
58 * We must next send the P2P DONE message (after finishing mostly
59 * with the local client). Then we will wait for the channel to close.
64 * We have received the P2P DONE message, and must finish with the
65 * local client before terminating the channel.
70 * The protocol is over. Results may still have to be sent to the
79 * State of an evaluate operation with another peer.
84 * The bf we currently receive
86 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
89 * BF of the set's element.
91 struct GNUNET_CONTAINER_BloomFilter *local_bf;
94 * Remaining elements in the intersection operation.
95 * Maps element-id-hashes to 'elements in our set'.
97 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
100 * Iterator for sending the final set of @e my_elements to the client.
102 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
105 * Evaluate operations are held in a linked list.
107 struct OperationState *next;
110 * Evaluate operations are held in a linked list.
112 struct OperationState *prev;
115 * For multipart BF transmissions, we have to store the
116 * bloomfilter-data until we fully received it.
121 * XOR of the keys of all of the elements (remaining) in my set.
122 * Always updated when elements are added or removed to
125 struct GNUNET_HashCode my_xor;
128 * XOR of the keys of all of the elements (remaining) in
129 * the other peer's set. Updated when we receive the
130 * other peer's Bloom filter.
132 struct GNUNET_HashCode other_xor;
135 * How many bytes of @e bf_data are valid?
137 uint32_t bf_data_offset;
140 * Current element count contained within @e my_elements.
141 * (May differ briefly during initialization.)
143 uint32_t my_element_count;
146 * size of the bloomfilter in @e bf_data.
148 uint32_t bf_data_size;
151 * size of the bloomfilter
153 uint32_t bf_bits_per_element;
156 * Salt currently used for BF construction (by us or the other peer,
157 * depending on where we are in the code).
162 * Current state of the operation.
164 enum IntersectionOperationPhase phase;
167 * Generation in which the operation handle
170 unsigned int generation_created;
173 * Did we send the client that we are done?
175 int client_done_sent;
178 * Set whenever we reach the state where the death of the
179 * channel is perfectly find and should NOT result in the
180 * operation being cancelled.
182 int channel_death_expected;
187 * Extra state required for efficient set intersection.
188 * Merely tracks the total number of elements.
193 * Number of currently valid elements in the set which have not been
196 uint32_t current_set_element_count;
201 * If applicable in the current operation mode, send a result message
202 * to the client indicating we removed an element.
204 * @param op intersection operation
205 * @param element element to send
208 send_client_removed_element (struct Operation *op,
209 struct GNUNET_SET_Element *element)
211 struct GNUNET_MQ_Envelope *ev;
212 struct GNUNET_SET_ResultMessage *rm;
214 if (GNUNET_SET_RESULT_REMOVED != op->result_mode)
215 return; /* Wrong mode for transmitting removed elements */
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Sending removed element (size %u) to client\n",
219 GNUNET_STATISTICS_update (_GSS_statistics,
220 "# Element removed messages sent",
223 GNUNET_assert (0 != op->client_request_id);
224 ev = GNUNET_MQ_msg_extra (rm,
226 GNUNET_MESSAGE_TYPE_SET_RESULT);
232 rm->result_status = htons (GNUNET_SET_STATUS_OK);
233 rm->request_id = htonl (op->client_request_id);
234 rm->element_type = element->element_type;
235 GNUNET_memcpy (&rm[1],
238 GNUNET_MQ_send (op->set->cs->mq,
244 * Fills the "my_elements" hashmap with all relevant elements.
246 * @param cls the `struct Operation *` we are performing
247 * @param key current key code
248 * @param value the `struct ElementEntry *` from the hash map
249 * @return #GNUNET_YES (we should continue to iterate)
252 filtered_map_initialization (void *cls,
253 const struct GNUNET_HashCode *key,
256 struct Operation *op = cls;
257 struct ElementEntry *ee = value;
258 struct GNUNET_HashCode mutated_hash;
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "FIMA called for %s:%u\n",
263 GNUNET_h2s (&ee->element_hash),
266 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Reduced initialization, not starting with %s:%u (wrong generation)\n",
270 GNUNET_h2s (&ee->element_hash),
272 return GNUNET_YES; /* element not valid in our operation's generation */
275 /* Test if element is in other peer's bloomfilter */
276 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Testing mingled hash %s with salt %u\n",
281 GNUNET_h2s (&mutated_hash),
284 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
287 /* remove this element */
288 send_client_removed_element (op,
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Reduced initialization, not starting with %s:%u\n",
292 GNUNET_h2s (&ee->element_hash),
296 op->state->my_element_count++;
297 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Filtered initialization of my_elements, adding %s:%u\n",
302 GNUNET_h2s (&ee->element_hash),
304 GNUNET_break (GNUNET_YES ==
305 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
315 * Removes elements from our hashmap if they are not contained within the
316 * provided remote bloomfilter.
318 * @param cls closure with the `struct Operation *`
319 * @param key current key code
320 * @param value value in the hash map
321 * @return #GNUNET_YES (we should continue to iterate)
324 iterator_bf_reduce (void *cls,
325 const struct GNUNET_HashCode *key,
328 struct Operation *op = cls;
329 struct ElementEntry *ee = value;
330 struct GNUNET_HashCode mutated_hash;
332 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Testing mingled hash %s with salt %u\n",
337 GNUNET_h2s (&mutated_hash),
340 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
343 GNUNET_break (0 < op->state->my_element_count);
344 op->state->my_element_count--;
345 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Bloom filter reduction of my_elements, removing %s:%u\n",
350 GNUNET_h2s (&ee->element_hash),
352 GNUNET_assert (GNUNET_YES ==
353 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
356 send_client_removed_element (op,
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Bloom filter reduction of my_elements, keeping %s:%u\n",
363 GNUNET_h2s (&ee->element_hash),
371 * Create initial bloomfilter based on all the elements given.
373 * @param cls the `struct Operation *`
374 * @param key current key code
375 * @param value the `struct ElementEntry` to process
376 * @return #GNUNET_YES (we should continue to iterate)
379 iterator_bf_create (void *cls,
380 const struct GNUNET_HashCode *key,
383 struct Operation *op = cls;
384 struct ElementEntry *ee = value;
385 struct GNUNET_HashCode mutated_hash;
387 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Initializing BF with hash %s with salt %u\n",
392 GNUNET_h2s (&mutated_hash),
394 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
401 * Inform the client that the intersection operation has failed,
402 * and proceed to destroy the evaluate operation.
404 * @param op the intersection operation to fail
407 fail_intersection_operation (struct Operation *op)
409 struct GNUNET_MQ_Envelope *ev;
410 struct GNUNET_SET_ResultMessage *msg;
412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
413 "Intersection operation failed\n");
414 GNUNET_STATISTICS_update (_GSS_statistics,
415 "# Intersection operations failed",
418 if (NULL != op->state->my_elements)
420 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
421 op->state->my_elements = NULL;
423 ev = GNUNET_MQ_msg (msg,
424 GNUNET_MESSAGE_TYPE_SET_RESULT);
425 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
426 msg->request_id = htonl (op->client_request_id);
427 msg->element_type = htons (0);
428 GNUNET_MQ_send (op->set->cs->mq,
430 _GSS_operation_destroy (op,
436 * Send a bloomfilter to our peer. After the result done message has
437 * been sent to the client, destroy the evaluate operation.
439 * @param op intersection operation
442 send_bloomfilter (struct Operation *op)
444 struct GNUNET_MQ_Envelope *ev;
445 struct BFMessage *msg;
447 uint32_t bf_elementbits;
452 /* We consider the ratio of the set sizes to determine
453 the number of bits per element, as the smaller set
454 should use more bits to maximize its set reduction
455 potential and minimize overall bandwidth consumption. */
456 bf_elementbits = 2 + ceil (log2((double)
457 (op->remote_element_count /
458 (double) op->state->my_element_count)));
459 if (bf_elementbits < 1)
460 bf_elementbits = 1; /* make sure k is not 0 */
461 /* optimize BF-size to ~50% of bits set */
462 bf_size = ceil ((double) (op->state->my_element_count
463 * bf_elementbits / log(2)));
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Sending Bloom filter (%u) of size %u bytes\n",
466 (unsigned int) bf_elementbits,
467 (unsigned int) bf_size);
468 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
471 op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
473 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
477 /* send our Bloom filter */
478 GNUNET_STATISTICS_update (_GSS_statistics,
479 "# Intersection Bloom filters sent",
482 chunk_size = 60 * 1024 - sizeof (struct BFMessage);
483 if (bf_size <= chunk_size)
486 chunk_size = bf_size;
487 ev = GNUNET_MQ_msg_extra (msg,
489 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
490 GNUNET_assert (GNUNET_SYSERR !=
491 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
494 msg->sender_element_count = htonl (op->state->my_element_count);
495 msg->bloomfilter_total_length = htonl (bf_size);
496 msg->bits_per_element = htonl (bf_elementbits);
497 msg->sender_mutator = htonl (op->state->salt);
498 msg->element_xor_hash = op->state->my_xor;
499 GNUNET_MQ_send (op->mq, ev);
504 bf_data = GNUNET_malloc (bf_size);
505 GNUNET_assert (GNUNET_SYSERR !=
506 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
510 while (offset < bf_size)
512 if (bf_size - chunk_size < offset)
513 chunk_size = bf_size - offset;
514 ev = GNUNET_MQ_msg_extra (msg,
516 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
517 GNUNET_memcpy (&msg[1],
520 offset += chunk_size;
521 msg->sender_element_count = htonl (op->state->my_element_count);
522 msg->bloomfilter_total_length = htonl (bf_size);
523 msg->bits_per_element = htonl (bf_elementbits);
524 msg->sender_mutator = htonl (op->state->salt);
525 msg->element_xor_hash = op->state->my_xor;
526 GNUNET_MQ_send (op->mq, ev);
528 GNUNET_free (bf_data);
530 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
531 op->state->local_bf = NULL;
536 * Signal to the client that the operation has finished and
537 * destroy the operation.
539 * @param cls operation to destroy
542 send_client_done_and_destroy (void *cls)
544 struct Operation *op = cls;
545 struct GNUNET_MQ_Envelope *ev;
546 struct GNUNET_SET_ResultMessage *rm;
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Intersection succeeded, sending DONE to local client\n");
550 GNUNET_STATISTICS_update (_GSS_statistics,
551 "# Intersection operations succeeded",
554 ev = GNUNET_MQ_msg (rm,
555 GNUNET_MESSAGE_TYPE_SET_RESULT);
556 rm->request_id = htonl (op->client_request_id);
557 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
558 rm->element_type = htons (0);
559 GNUNET_MQ_send (op->set->cs->mq,
561 _GSS_operation_destroy (op,
567 * Remember that we are done dealing with the local client
568 * AND have sent the other peer our message that we are done,
569 * so we are not just waiting for the channel to die before
570 * telling the local client that we are done as our last act.
572 * @param cls the `struct Operation`.
575 finished_local_operations (void *cls)
577 struct Operation *op = cls;
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "DONE sent to other peer, now waiting for other end to close the channel\n");
581 op->state->phase = PHASE_FINISHED;
582 op->state->channel_death_expected = GNUNET_YES;
587 * Notify the other peer that we are done. Once this message
588 * is out, we still need to notify the local client that we
591 * @param op operation to notify for.
594 send_p2p_done (struct Operation *op)
596 struct GNUNET_MQ_Envelope *ev;
597 struct IntersectionDoneMessage *idm;
599 GNUNET_assert (PHASE_MUST_SEND_DONE == op->state->phase);
600 GNUNET_assert (GNUNET_NO == op->state->channel_death_expected);
601 ev = GNUNET_MQ_msg (idm,
602 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE);
603 idm->final_element_count = htonl (op->state->my_element_count);
604 idm->element_xor_hash = op->state->my_xor;
605 GNUNET_MQ_notify_sent (ev,
606 &finished_local_operations,
608 GNUNET_MQ_send (op->mq,
614 * Send all elements in the full result iterator.
616 * @param cls the `struct Operation *`
619 send_remaining_elements (void *cls)
621 struct Operation *op = cls;
623 const struct ElementEntry *ee;
624 struct GNUNET_MQ_Envelope *ev;
625 struct GNUNET_SET_ResultMessage *rm;
626 const struct GNUNET_SET_Element *element;
629 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter,
632 if (GNUNET_NO == res)
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Sending done and destroy because iterator ran out\n");
636 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
637 op->state->full_result_iter = NULL;
638 if (PHASE_DONE_RECEIVED == op->state->phase)
640 op->state->phase = PHASE_FINISHED;
641 send_client_done_and_destroy (op);
643 else if (PHASE_MUST_SEND_DONE == op->state->phase)
654 element = &ee->element;
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Sending element %s:%u to client (full set)\n",
657 GNUNET_h2s (&ee->element_hash),
659 GNUNET_assert (0 != op->client_request_id);
660 ev = GNUNET_MQ_msg_extra (rm,
662 GNUNET_MESSAGE_TYPE_SET_RESULT);
663 GNUNET_assert (NULL != ev);
664 rm->result_status = htons (GNUNET_SET_STATUS_OK);
665 rm->request_id = htonl (op->client_request_id);
666 rm->element_type = element->element_type;
667 GNUNET_memcpy (&rm[1],
670 GNUNET_MQ_notify_sent (ev,
671 &send_remaining_elements,
673 GNUNET_MQ_send (op->set->cs->mq,
679 * Fills the "my_elements" hashmap with the initial set of
680 * (non-deleted) elements from the set of the specification.
682 * @param cls closure with the `struct Operation *`
683 * @param key current key code for the element
684 * @param value value in the hash map with the `struct ElementEntry *`
685 * @return #GNUNET_YES (we should continue to iterate)
688 initialize_map_unfiltered (void *cls,
689 const struct GNUNET_HashCode *key,
692 struct ElementEntry *ee = value;
693 struct Operation *op = cls;
695 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
696 return GNUNET_YES; /* element not live in operation's generation */
697 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Initial full initialization of my_elements, adding %s:%u\n",
702 GNUNET_h2s (&ee->element_hash),
704 GNUNET_break (GNUNET_YES ==
705 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
714 * Send our element count to the peer, in case our element count is
717 * @param op intersection operation
720 send_element_count (struct Operation *op)
722 struct GNUNET_MQ_Envelope *ev;
723 struct IntersectionElementInfoMessage *msg;
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Sending our element count (%u)\n",
727 op->state->my_element_count);
728 ev = GNUNET_MQ_msg (msg,
729 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
730 msg->sender_element_count = htonl (op->state->my_element_count);
731 GNUNET_MQ_send (op->mq, ev);
736 * We go first, initialize our map with all elements and
737 * send the first Bloom filter.
739 * @param op operation to start exchange for
742 begin_bf_exchange (struct Operation *op)
744 op->state->phase = PHASE_BF_EXCHANGE;
745 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
746 &initialize_map_unfiltered,
748 send_bloomfilter (op);
753 * Handle the initial `struct IntersectionElementInfoMessage` from a
756 * @param cls the intersection operation
757 * @param mh the header of the message
760 handle_intersection_p2p_element_info (void *cls,
761 const struct IntersectionElementInfoMessage *msg)
763 struct Operation *op = cls;
765 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
768 fail_intersection_operation(op);
771 op->remote_element_count = ntohl (msg->sender_element_count);
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Received remote element count (%u), I have %u\n",
774 op->remote_element_count,
775 op->state->my_element_count);
776 if ( ( (PHASE_INITIAL != op->state->phase) &&
777 (PHASE_COUNT_SENT != op->state->phase) ) ||
778 (op->state->my_element_count > op->remote_element_count) ||
779 (0 == op->state->my_element_count) ||
780 (0 == op->remote_element_count) )
783 fail_intersection_operation(op);
786 GNUNET_break (NULL == op->state->remote_bf);
787 begin_bf_exchange (op);
788 GNUNET_CADET_receive_done (op->channel);
793 * Process a Bloomfilter once we got all the chunks.
795 * @param op the intersection operation
798 process_bf (struct Operation *op)
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n",
803 op->remote_element_count,
804 op->state->my_element_count,
805 GNUNET_CONTAINER_multihashmap_size (op->set->content->elements));
806 switch (op->state->phase)
810 fail_intersection_operation(op);
812 case PHASE_COUNT_SENT:
813 /* This is the first BF being sent, build our initial map with
814 filtering in place */
815 op->state->my_element_count = 0;
816 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
817 &filtered_map_initialization,
820 case PHASE_BF_EXCHANGE:
821 /* Update our set by reduction */
822 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
826 case PHASE_MUST_SEND_DONE:
828 fail_intersection_operation(op);
830 case PHASE_DONE_RECEIVED:
832 fail_intersection_operation(op);
836 fail_intersection_operation(op);
839 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
840 op->state->remote_bf = NULL;
842 if ( (0 == op->state->my_element_count) || /* fully disjoint */
843 ( (op->state->my_element_count == op->remote_element_count) &&
844 (0 == memcmp (&op->state->my_xor,
845 &op->state->other_xor,
846 sizeof (struct GNUNET_HashCode))) ) )
849 op->state->phase = PHASE_MUST_SEND_DONE;
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Intersection succeeded, sending DONE to other peer\n");
852 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
853 op->state->local_bf = NULL;
854 if (GNUNET_SET_RESULT_FULL == op->result_mode)
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Sending full result set (%u elements)\n",
858 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
859 op->state->full_result_iter
860 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
861 send_remaining_elements (op);
867 op->state->phase = PHASE_BF_EXCHANGE;
868 send_bloomfilter (op);
873 * Check an BF message from a remote peer.
875 * @param cls the intersection operation
876 * @param msg the header of the message
877 * @return #GNUNET_OK if @a msg is well-formed
880 check_intersection_p2p_bf (void *cls,
881 const struct BFMessage *msg)
883 struct Operation *op = cls;
885 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
888 return GNUNET_SYSERR;
895 * Handle an BF message from a remote peer.
897 * @param cls the intersection operation
898 * @param msg the header of the message
901 handle_intersection_p2p_bf (void *cls,
902 const struct BFMessage *msg)
904 struct Operation *op = cls;
907 uint32_t bf_bits_per_element;
909 switch (op->state->phase)
913 fail_intersection_operation (op);
915 case PHASE_COUNT_SENT:
916 case PHASE_BF_EXCHANGE:
917 bf_size = ntohl (msg->bloomfilter_total_length);
918 bf_bits_per_element = ntohl (msg->bits_per_element);
919 chunk_size = htons (msg->header.size) - sizeof (struct BFMessage);
920 op->state->other_xor = msg->element_xor_hash;
921 if (bf_size == chunk_size)
923 if (NULL != op->state->bf_data)
926 fail_intersection_operation (op);
929 /* single part, done here immediately */
931 = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
933 bf_bits_per_element);
934 op->state->salt = ntohl (msg->sender_mutator);
935 op->remote_element_count = ntohl (msg->sender_element_count);
939 /* multipart chunk */
940 if (NULL == op->state->bf_data)
942 /* first chunk, initialize */
943 op->state->bf_data = GNUNET_malloc (bf_size);
944 op->state->bf_data_size = bf_size;
945 op->state->bf_bits_per_element = bf_bits_per_element;
946 op->state->bf_data_offset = 0;
947 op->state->salt = ntohl (msg->sender_mutator);
948 op->remote_element_count = ntohl (msg->sender_element_count);
953 if ( (op->state->bf_data_size != bf_size) ||
954 (op->state->bf_bits_per_element != bf_bits_per_element) ||
955 (op->state->bf_data_offset + chunk_size > bf_size) ||
956 (op->state->salt != ntohl (msg->sender_mutator)) ||
957 (op->remote_element_count != ntohl (msg->sender_element_count)) )
960 fail_intersection_operation (op);
964 GNUNET_memcpy (&op->state->bf_data[op->state->bf_data_offset],
965 (const char*) &msg[1],
967 op->state->bf_data_offset += chunk_size;
968 if (op->state->bf_data_offset == bf_size)
970 /* last chunk, run! */
972 = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
974 bf_bits_per_element);
975 GNUNET_free (op->state->bf_data);
976 op->state->bf_data = NULL;
977 op->state->bf_data_size = 0;
983 fail_intersection_operation (op);
986 GNUNET_CADET_receive_done (op->channel);
991 * Remove all elements from our hashmap.
993 * @param cls closure with the `struct Operation *`
994 * @param key current key code
995 * @param value value in the hash map
996 * @return #GNUNET_YES (we should continue to iterate)
999 filter_all (void *cls,
1000 const struct GNUNET_HashCode *key,
1003 struct Operation *op = cls;
1004 struct ElementEntry *ee = value;
1006 GNUNET_break (0 < op->state->my_element_count);
1007 op->state->my_element_count--;
1008 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
1010 &op->state->my_xor);
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Final reduction of my_elements, removing %s:%u\n",
1013 GNUNET_h2s (&ee->element_hash),
1015 GNUNET_assert (GNUNET_YES ==
1016 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
1019 send_client_removed_element (op,
1026 * Handle a done message from a remote peer
1028 * @param cls the intersection operation
1029 * @param mh the message
1032 handle_intersection_p2p_done (void *cls,
1033 const struct IntersectionDoneMessage *idm)
1035 struct Operation *op = cls;
1037 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
1039 GNUNET_break_op (0);
1040 fail_intersection_operation (op);
1043 if (PHASE_BF_EXCHANGE != op->state->phase)
1045 /* wrong phase to conclude? FIXME: Or should we allow this
1046 if the other peer has _initially_ already an empty set? */
1047 GNUNET_break_op (0);
1048 fail_intersection_operation (op);
1051 if (0 == ntohl (idm->final_element_count))
1053 /* other peer determined empty set is the intersection,
1054 remove all elements */
1055 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
1059 if ( (op->state->my_element_count != ntohl (idm->final_element_count)) ||
1060 (0 != memcmp (&op->state->my_xor,
1061 &idm->element_xor_hash,
1062 sizeof (struct GNUNET_HashCode))) )
1064 /* Other peer thinks we are done, but we disagree on the result! */
1065 GNUNET_break_op (0);
1066 fail_intersection_operation (op);
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "Got IntersectionDoneMessage, have %u elements in intersection\n",
1071 op->state->my_element_count);
1072 op->state->phase = PHASE_DONE_RECEIVED;
1073 GNUNET_CADET_receive_done (op->channel);
1075 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1076 if (GNUNET_SET_RESULT_FULL == op->result_mode)
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Sending full result set to client (%u elements)\n",
1080 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
1081 op->state->full_result_iter
1082 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
1083 send_remaining_elements (op);
1086 op->state->phase = PHASE_FINISHED;
1087 send_client_done_and_destroy (op);
1092 * Initiate a set intersection operation with a remote peer.
1094 * @param op operation that is created, should be initialized to
1095 * begin the evaluation
1096 * @param opaque_context message to be transmitted to the listener
1097 * to convince it to accept, may be NULL
1098 * @return operation-specific state to keep in @a op
1100 static struct OperationState *
1101 intersection_evaluate (struct Operation *op,
1102 const struct GNUNET_MessageHeader *opaque_context)
1104 struct OperationState *state;
1105 struct GNUNET_MQ_Envelope *ev;
1106 struct OperationRequestMessage *msg;
1108 ev = GNUNET_MQ_msg_nested_mh (msg,
1109 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
1113 /* the context message is too large!? */
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "Initiating intersection operation evaluation\n");
1119 state = GNUNET_new (struct OperationState);
1120 /* we started the operation, thus we have to send the operation request */
1121 state->phase = PHASE_INITIAL;
1122 state->my_element_count = op->set->state->current_set_element_count;
1124 = GNUNET_CONTAINER_multihashmap_create (state->my_element_count,
1127 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
1128 msg->element_count = htonl (state->my_element_count);
1129 GNUNET_MQ_send (op->mq,
1131 state->phase = PHASE_COUNT_SENT;
1132 if (NULL != opaque_context)
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Sent op request with context message\n");
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "Sent op request without context message\n");
1143 * Accept an intersection operation request from a remote peer. Only
1144 * initializes the private operation state.
1146 * @param op operation that will be accepted as an intersection operation
1148 static struct OperationState *
1149 intersection_accept (struct Operation *op)
1151 struct OperationState *state;
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "Accepting set intersection operation\n");
1155 state = GNUNET_new (struct OperationState);
1156 state->phase = PHASE_INITIAL;
1157 state->my_element_count
1158 = op->set->state->current_set_element_count;
1160 = GNUNET_CONTAINER_multihashmap_create (GNUNET_MIN (state->my_element_count,
1161 op->remote_element_count),
1164 if (op->remote_element_count < state->my_element_count)
1166 /* If the other peer (Alice) has fewer elements than us (Bob),
1167 we just send the count as Alice should send the first BF */
1168 send_element_count (op);
1169 state->phase = PHASE_COUNT_SENT;
1172 /* We have fewer elements, so we start with the BF */
1173 begin_bf_exchange (op);
1179 * Destroy the intersection operation. Only things specific to the
1180 * intersection operation are destroyed.
1182 * @param op intersection operation to destroy
1185 intersection_op_cancel (struct Operation *op)
1187 /* check if the op was canceled twice */
1188 GNUNET_assert (NULL != op->state);
1189 if (NULL != op->state->remote_bf)
1191 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1192 op->state->remote_bf = NULL;
1194 if (NULL != op->state->local_bf)
1196 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1197 op->state->local_bf = NULL;
1199 if (NULL != op->state->my_elements)
1201 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1202 op->state->my_elements = NULL;
1204 if (NULL != op->state->full_result_iter)
1206 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
1207 op->state->full_result_iter = NULL;
1209 GNUNET_free (op->state);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Destroying intersection op state done\n");
1217 * Create a new set supporting the intersection operation.
1219 * @return the newly created set
1221 static struct SetState *
1222 intersection_set_create ()
1224 struct SetState *set_state;
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Intersection set created\n");
1228 set_state = GNUNET_new (struct SetState);
1229 set_state->current_set_element_count = 0;
1236 * Add the element from the given element message to the set.
1238 * @param set_state state of the set want to add to
1239 * @param ee the element to add to the set
1242 intersection_add (struct SetState *set_state,
1243 struct ElementEntry *ee)
1245 set_state->current_set_element_count++;
1250 * Destroy a set that supports the intersection operation
1252 * @param set_state the set to destroy
1255 intersection_set_destroy (struct SetState *set_state)
1257 GNUNET_free (set_state);
1262 * Remove the element given in the element message from the set.
1264 * @param set_state state of the set to remove from
1265 * @param element set element to remove
1268 intersection_remove (struct SetState *set_state,
1269 struct ElementEntry *element)
1271 GNUNET_assert (0 < set_state->current_set_element_count);
1272 set_state->current_set_element_count--;
1277 * Callback for channel death for the intersection operation.
1279 * @param op operation that lost the channel
1282 intersection_channel_death (struct Operation *op)
1284 if (GNUNET_YES == op->state->channel_death_expected)
1286 /* oh goodie, we are done! */
1287 send_client_done_and_destroy (op);
1291 /* sorry, channel went down early, too bad. */
1292 _GSS_operation_destroy (op,
1299 * Get the table with implementing functions for set intersection.
1301 * @return the operation specific VTable
1303 const struct SetVT *
1304 _GSS_intersection_vt ()
1306 static const struct SetVT intersection_vt = {
1307 .create = &intersection_set_create,
1308 .add = &intersection_add,
1309 .remove = &intersection_remove,
1310 .destroy_set = &intersection_set_destroy,
1311 .evaluate = &intersection_evaluate,
1312 .accept = &intersection_accept,
1313 .cancel = &intersection_op_cancel,
1314 .channel_death = &intersection_channel_death,
1317 return &intersection_vt;