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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
31 #include "gnunet-service-set_intersection.h"
36 * Current phase we are in for a intersection operation.
38 enum IntersectionOperationPhase
41 * We are just starting.
46 * We have send the number of our elements to the other
47 * peer, but did not setup our element set yet.
52 * We have initialized our set and are now reducing it by exchanging
53 * Bloom filters until one party notices the their element hashes
59 * We must next send the P2P DONE message (after finishing mostly
60 * with the local client). Then we will wait for the channel to close.
65 * We have received the P2P DONE message, and must finish with the
66 * local client before terminating the channel.
71 * The protocol is over. Results may still have to be sent to the
80 * State of an evaluate operation with another peer.
85 * The bf we currently receive
87 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
90 * BF of the set's element.
92 struct GNUNET_CONTAINER_BloomFilter *local_bf;
95 * Remaining elements in the intersection operation.
96 * Maps element-id-hashes to 'elements in our set'.
98 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
101 * Iterator for sending the final set of @e my_elements to the client.
103 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
106 * Evaluate operations are held in a linked list.
108 struct OperationState *next;
111 * Evaluate operations are held in a linked list.
113 struct OperationState *prev;
116 * For multipart BF transmissions, we have to store the
117 * bloomfilter-data until we fully received it.
122 * XOR of the keys of all of the elements (remaining) in my set.
123 * Always updated when elements are added or removed to
126 struct GNUNET_HashCode my_xor;
129 * XOR of the keys of all of the elements (remaining) in
130 * the other peer's set. Updated when we receive the
131 * other peer's Bloom filter.
133 struct GNUNET_HashCode other_xor;
136 * How many bytes of @e bf_data are valid?
138 uint32_t bf_data_offset;
141 * Current element count contained within @e my_elements.
142 * (May differ briefly during initialization.)
144 uint32_t my_element_count;
147 * size of the bloomfilter in @e bf_data.
149 uint32_t bf_data_size;
152 * size of the bloomfilter
154 uint32_t bf_bits_per_element;
157 * Salt currently used for BF construction (by us or the other peer,
158 * depending on where we are in the code).
163 * Current state of the operation.
165 enum IntersectionOperationPhase phase;
168 * Generation in which the operation handle
171 unsigned int generation_created;
174 * Did we send the client that we are done?
176 int client_done_sent;
179 * Set whenever we reach the state where the death of the
180 * channel is perfectly find and should NOT result in the
181 * operation being cancelled.
183 int channel_death_expected;
188 * Extra state required for efficient set intersection.
189 * Merely tracks the total number of elements.
194 * Number of currently valid elements in the set which have not been
197 uint32_t current_set_element_count;
202 * If applicable in the current operation mode, send a result message
203 * to the client indicating we removed an element.
205 * @param op intersection operation
206 * @param element element to send
209 send_client_removed_element (struct Operation *op,
210 struct GNUNET_SET_Element *element)
212 struct GNUNET_MQ_Envelope *ev;
213 struct GNUNET_SET_ResultMessage *rm;
215 if (GNUNET_SET_RESULT_REMOVED != op->result_mode)
216 return; /* Wrong mode for transmitting removed elements */
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Sending removed element (size %u) to client\n",
220 GNUNET_assert (0 != op->client_request_id);
221 ev = GNUNET_MQ_msg_extra (rm,
223 GNUNET_MESSAGE_TYPE_SET_RESULT);
229 rm->result_status = htons (GNUNET_SET_STATUS_OK);
230 rm->request_id = htonl (op->client_request_id);
231 rm->element_type = element->element_type;
232 GNUNET_memcpy (&rm[1],
235 GNUNET_MQ_send (op->set->cs->mq,
241 * Fills the "my_elements" hashmap with all relevant elements.
243 * @param cls the `struct Operation *` we are performing
244 * @param key current key code
245 * @param value the `struct ElementEntry *` from the hash map
246 * @return #GNUNET_YES (we should continue to iterate)
249 filtered_map_initialization (void *cls,
250 const struct GNUNET_HashCode *key,
253 struct Operation *op = cls;
254 struct ElementEntry *ee = value;
255 struct GNUNET_HashCode mutated_hash;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "FIMA called for %s:%u\n",
260 GNUNET_h2s (&ee->element_hash),
263 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Reduced initialization, not starting with %s:%u (wrong generation)\n",
267 GNUNET_h2s (&ee->element_hash),
269 return GNUNET_YES; /* element not valid in our operation's generation */
272 /* Test if element is in other peer's bloomfilter */
273 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Testing mingled hash %s with salt %u\n",
278 GNUNET_h2s (&mutated_hash),
281 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
284 /* remove this element */
285 send_client_removed_element (op,
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Reduced initialization, not starting with %s:%u\n",
289 GNUNET_h2s (&ee->element_hash),
293 op->state->my_element_count++;
294 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Filtered initialization of my_elements, adding %s:%u\n",
299 GNUNET_h2s (&ee->element_hash),
301 GNUNET_break (GNUNET_YES ==
302 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
312 * Removes elements from our hashmap if they are not contained within the
313 * provided remote bloomfilter.
315 * @param cls closure with the `struct Operation *`
316 * @param key current key code
317 * @param value value in the hash map
318 * @return #GNUNET_YES (we should continue to iterate)
321 iterator_bf_reduce (void *cls,
322 const struct GNUNET_HashCode *key,
325 struct Operation *op = cls;
326 struct ElementEntry *ee = value;
327 struct GNUNET_HashCode mutated_hash;
329 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Testing mingled hash %s with salt %u\n",
334 GNUNET_h2s (&mutated_hash),
337 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
340 GNUNET_break (0 < op->state->my_element_count);
341 op->state->my_element_count--;
342 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Bloom filter reduction of my_elements, removing %s:%u\n",
347 GNUNET_h2s (&ee->element_hash),
349 GNUNET_assert (GNUNET_YES ==
350 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
353 send_client_removed_element (op,
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Bloom filter reduction of my_elements, keeping %s:%u\n",
360 GNUNET_h2s (&ee->element_hash),
368 * Create initial bloomfilter based on all the elements given.
370 * @param cls the `struct Operation *`
371 * @param key current key code
372 * @param value the `struct ElementEntry` to process
373 * @return #GNUNET_YES (we should continue to iterate)
376 iterator_bf_create (void *cls,
377 const struct GNUNET_HashCode *key,
380 struct Operation *op = cls;
381 struct ElementEntry *ee = value;
382 struct GNUNET_HashCode mutated_hash;
384 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Initializing BF with hash %s with salt %u\n",
389 GNUNET_h2s (&mutated_hash),
391 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
398 * Inform the client that the intersection operation has failed,
399 * and proceed to destroy the evaluate operation.
401 * @param op the intersection operation to fail
404 fail_intersection_operation (struct Operation *op)
406 struct GNUNET_MQ_Envelope *ev;
407 struct GNUNET_SET_ResultMessage *msg;
409 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
410 "Intersection operation failed\n");
411 if (NULL != op->state->my_elements)
413 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
414 op->state->my_elements = NULL;
416 ev = GNUNET_MQ_msg (msg,
417 GNUNET_MESSAGE_TYPE_SET_RESULT);
418 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
419 msg->request_id = htonl (op->client_request_id);
420 msg->element_type = htons (0);
421 GNUNET_MQ_send (op->set->cs->mq,
423 _GSS_operation_destroy (op,
429 * Send a bloomfilter to our peer. After the result done message has
430 * been sent to the client, destroy the evaluate operation.
432 * @param op intersection operation
435 send_bloomfilter (struct Operation *op)
437 struct GNUNET_MQ_Envelope *ev;
438 struct BFMessage *msg;
440 uint32_t bf_elementbits;
445 /* We consider the ratio of the set sizes to determine
446 the number of bits per element, as the smaller set
447 should use more bits to maximize its set reduction
448 potential and minimize overall bandwidth consumption. */
449 bf_elementbits = 2 + ceil (log2((double)
450 (op->remote_element_count /
451 (double) op->state->my_element_count)));
452 if (bf_elementbits < 1)
453 bf_elementbits = 1; /* make sure k is not 0 */
454 /* optimize BF-size to ~50% of bits set */
455 bf_size = ceil ((double) (op->state->my_element_count
456 * bf_elementbits / log(2)));
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Sending Bloom filter (%u) of size %u bytes\n",
459 (unsigned int) bf_elementbits,
460 (unsigned int) bf_size);
461 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
464 op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
466 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
470 /* send our Bloom filter */
471 chunk_size = 60 * 1024 - sizeof (struct BFMessage);
472 if (bf_size <= chunk_size)
475 chunk_size = bf_size;
476 ev = GNUNET_MQ_msg_extra (msg,
478 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
479 GNUNET_assert (GNUNET_SYSERR !=
480 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
483 msg->sender_element_count = htonl (op->state->my_element_count);
484 msg->bloomfilter_total_length = htonl (bf_size);
485 msg->bits_per_element = htonl (bf_elementbits);
486 msg->sender_mutator = htonl (op->state->salt);
487 msg->element_xor_hash = op->state->my_xor;
488 GNUNET_MQ_send (op->mq, ev);
493 bf_data = GNUNET_malloc (bf_size);
494 GNUNET_assert (GNUNET_SYSERR !=
495 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
499 while (offset < bf_size)
501 if (bf_size - chunk_size < offset)
502 chunk_size = bf_size - offset;
503 ev = GNUNET_MQ_msg_extra (msg,
505 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
506 GNUNET_memcpy (&msg[1],
509 offset += chunk_size;
510 msg->sender_element_count = htonl (op->state->my_element_count);
511 msg->bloomfilter_total_length = htonl (bf_size);
512 msg->bits_per_element = htonl (bf_elementbits);
513 msg->sender_mutator = htonl (op->state->salt);
514 msg->element_xor_hash = op->state->my_xor;
515 GNUNET_MQ_send (op->mq, ev);
517 GNUNET_free (bf_data);
519 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
520 op->state->local_bf = NULL;
525 * Signal to the client that the operation has finished and
526 * destroy the operation.
528 * @param cls operation to destroy
531 send_client_done_and_destroy (void *cls)
533 struct Operation *op = cls;
534 struct GNUNET_MQ_Envelope *ev;
535 struct GNUNET_SET_ResultMessage *rm;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Intersection succeeded, sending DONE to local client\n");
539 ev = GNUNET_MQ_msg (rm,
540 GNUNET_MESSAGE_TYPE_SET_RESULT);
541 rm->request_id = htonl (op->client_request_id);
542 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
543 rm->element_type = htons (0);
544 GNUNET_MQ_send (op->set->cs->mq,
546 _GSS_operation_destroy (op,
552 * Remember that we are done dealing with the local client
553 * AND have sent the other peer our message that we are done,
554 * so we are not just waiting for the channel to die before
555 * telling the local client that we are done as our last act.
557 * @param cls the `struct Operation`.
560 finished_local_operations (void *cls)
562 struct Operation *op = cls;
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "DONE sent to other peer, now waiting for other end to close the channel\n");
566 op->state->phase = PHASE_FINISHED;
567 op->state->channel_death_expected = GNUNET_YES;
572 * Notify the other peer that we are done. Once this message
573 * is out, we still need to notify the local client that we
576 * @param op operation to notify for.
579 send_p2p_done (struct Operation *op)
581 struct GNUNET_MQ_Envelope *ev;
582 struct IntersectionDoneMessage *idm;
584 GNUNET_assert (PHASE_MUST_SEND_DONE == op->state->phase);
585 GNUNET_assert (GNUNET_NO == op->state->channel_death_expected);
586 ev = GNUNET_MQ_msg (idm,
587 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE);
588 idm->final_element_count = htonl (op->state->my_element_count);
589 idm->element_xor_hash = op->state->my_xor;
590 GNUNET_MQ_notify_sent (ev,
591 &finished_local_operations,
593 GNUNET_MQ_send (op->mq,
599 * Send all elements in the full result iterator.
601 * @param cls the `struct Operation *`
604 send_remaining_elements (void *cls)
606 struct Operation *op = cls;
608 const struct ElementEntry *ee;
609 struct GNUNET_MQ_Envelope *ev;
610 struct GNUNET_SET_ResultMessage *rm;
611 const struct GNUNET_SET_Element *element;
614 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter,
617 if (GNUNET_NO == res)
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Sending done and destroy because iterator ran out\n");
621 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
622 op->state->full_result_iter = NULL;
623 if (PHASE_DONE_RECEIVED == op->state->phase)
625 op->state->phase = PHASE_FINISHED;
626 send_client_done_and_destroy (op);
628 else if (PHASE_MUST_SEND_DONE == op->state->phase)
639 element = &ee->element;
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Sending element %s:%u to client (full set)\n",
642 GNUNET_h2s (&ee->element_hash),
644 GNUNET_assert (0 != op->client_request_id);
645 ev = GNUNET_MQ_msg_extra (rm,
647 GNUNET_MESSAGE_TYPE_SET_RESULT);
648 GNUNET_assert (NULL != ev);
649 rm->result_status = htons (GNUNET_SET_STATUS_OK);
650 rm->request_id = htonl (op->client_request_id);
651 rm->element_type = element->element_type;
652 GNUNET_memcpy (&rm[1],
655 GNUNET_MQ_notify_sent (ev,
656 &send_remaining_elements,
658 GNUNET_MQ_send (op->set->cs->mq,
664 * Fills the "my_elements" hashmap with the initial set of
665 * (non-deleted) elements from the set of the specification.
667 * @param cls closure with the `struct Operation *`
668 * @param key current key code for the element
669 * @param value value in the hash map with the `struct ElementEntry *`
670 * @return #GNUNET_YES (we should continue to iterate)
673 initialize_map_unfiltered (void *cls,
674 const struct GNUNET_HashCode *key,
677 struct ElementEntry *ee = value;
678 struct Operation *op = cls;
680 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
681 return GNUNET_YES; /* element not live in operation's generation */
682 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Initial full initialization of my_elements, adding %s:%u\n",
687 GNUNET_h2s (&ee->element_hash),
689 GNUNET_break (GNUNET_YES ==
690 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
693 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
699 * Send our element count to the peer, in case our element count is
702 * @param op intersection operation
705 send_element_count (struct Operation *op)
707 struct GNUNET_MQ_Envelope *ev;
708 struct IntersectionElementInfoMessage *msg;
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Sending our element count (%u)\n",
712 op->state->my_element_count);
713 ev = GNUNET_MQ_msg (msg,
714 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
715 msg->sender_element_count = htonl (op->state->my_element_count);
716 GNUNET_MQ_send (op->mq, ev);
721 * We go first, initialize our map with all elements and
722 * send the first Bloom filter.
724 * @param op operation to start exchange for
727 begin_bf_exchange (struct Operation *op)
729 op->state->phase = PHASE_BF_EXCHANGE;
730 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
731 &initialize_map_unfiltered,
733 send_bloomfilter (op);
738 * Handle the initial `struct IntersectionElementInfoMessage` from a
741 * @param cls the intersection operation
742 * @param mh the header of the message
745 handle_intersection_p2p_element_info (void *cls,
746 const struct IntersectionElementInfoMessage *msg)
748 struct Operation *op = cls;
750 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
753 fail_intersection_operation(op);
756 op->remote_element_count = ntohl (msg->sender_element_count);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Received remote element count (%u), I have %u\n",
759 op->remote_element_count,
760 op->state->my_element_count);
761 if ( ( (PHASE_INITIAL != op->state->phase) &&
762 (PHASE_COUNT_SENT != op->state->phase) ) ||
763 (op->state->my_element_count > op->remote_element_count) ||
764 (0 == op->state->my_element_count) ||
765 (0 == op->remote_element_count) )
768 fail_intersection_operation(op);
771 GNUNET_break (NULL == op->state->remote_bf);
772 begin_bf_exchange (op);
773 GNUNET_CADET_receive_done (op->channel);
778 * Process a Bloomfilter once we got all the chunks.
780 * @param op the intersection operation
783 process_bf (struct Operation *op)
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n",
788 op->remote_element_count,
789 op->state->my_element_count,
790 GNUNET_CONTAINER_multihashmap_size (op->set->content->elements));
791 switch (op->state->phase)
795 fail_intersection_operation(op);
797 case PHASE_COUNT_SENT:
798 /* This is the first BF being sent, build our initial map with
799 filtering in place */
800 op->state->my_element_count = 0;
801 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
802 &filtered_map_initialization,
805 case PHASE_BF_EXCHANGE:
806 /* Update our set by reduction */
807 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
811 case PHASE_MUST_SEND_DONE:
813 fail_intersection_operation(op);
815 case PHASE_DONE_RECEIVED:
817 fail_intersection_operation(op);
821 fail_intersection_operation(op);
824 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
825 op->state->remote_bf = NULL;
827 if ( (0 == op->state->my_element_count) || /* fully disjoint */
828 ( (op->state->my_element_count == op->remote_element_count) &&
829 (0 == memcmp (&op->state->my_xor,
830 &op->state->other_xor,
831 sizeof (struct GNUNET_HashCode))) ) )
834 op->state->phase = PHASE_MUST_SEND_DONE;
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Intersection succeeded, sending DONE to other peer\n");
837 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
838 op->state->local_bf = NULL;
839 if (GNUNET_SET_RESULT_FULL == op->result_mode)
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Sending full result set (%u elements)\n",
843 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
844 op->state->full_result_iter
845 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
846 send_remaining_elements (op);
852 op->state->phase = PHASE_BF_EXCHANGE;
853 send_bloomfilter (op);
858 * Check an BF message from a remote peer.
860 * @param cls the intersection operation
861 * @param msg the header of the message
862 * @return #GNUNET_OK if @a msg is well-formed
865 check_intersection_p2p_bf (void *cls,
866 const struct BFMessage *msg)
868 struct Operation *op = cls;
870 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
873 return GNUNET_SYSERR;
880 * Handle an BF message from a remote peer.
882 * @param cls the intersection operation
883 * @param msg the header of the message
886 handle_intersection_p2p_bf (void *cls,
887 const struct BFMessage *msg)
889 struct Operation *op = cls;
892 uint32_t bf_bits_per_element;
894 switch (op->state->phase)
898 fail_intersection_operation (op);
900 case PHASE_COUNT_SENT:
901 case PHASE_BF_EXCHANGE:
902 bf_size = ntohl (msg->bloomfilter_total_length);
903 bf_bits_per_element = ntohl (msg->bits_per_element);
904 chunk_size = htons (msg->header.size) - sizeof (struct BFMessage);
905 op->state->other_xor = msg->element_xor_hash;
906 if (bf_size == chunk_size)
908 if (NULL != op->state->bf_data)
911 fail_intersection_operation (op);
914 /* single part, done here immediately */
916 = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
918 bf_bits_per_element);
919 op->state->salt = ntohl (msg->sender_mutator);
920 op->remote_element_count = ntohl (msg->sender_element_count);
924 /* multipart chunk */
925 if (NULL == op->state->bf_data)
927 /* first chunk, initialize */
928 op->state->bf_data = GNUNET_malloc (bf_size);
929 op->state->bf_data_size = bf_size;
930 op->state->bf_bits_per_element = bf_bits_per_element;
931 op->state->bf_data_offset = 0;
932 op->state->salt = ntohl (msg->sender_mutator);
933 op->remote_element_count = ntohl (msg->sender_element_count);
938 if ( (op->state->bf_data_size != bf_size) ||
939 (op->state->bf_bits_per_element != bf_bits_per_element) ||
940 (op->state->bf_data_offset + chunk_size > bf_size) ||
941 (op->state->salt != ntohl (msg->sender_mutator)) ||
942 (op->remote_element_count != ntohl (msg->sender_element_count)) )
945 fail_intersection_operation (op);
949 GNUNET_memcpy (&op->state->bf_data[op->state->bf_data_offset],
950 (const char*) &msg[1],
952 op->state->bf_data_offset += chunk_size;
953 if (op->state->bf_data_offset == bf_size)
955 /* last chunk, run! */
957 = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
959 bf_bits_per_element);
960 GNUNET_free (op->state->bf_data);
961 op->state->bf_data = NULL;
962 op->state->bf_data_size = 0;
968 fail_intersection_operation (op);
971 GNUNET_CADET_receive_done (op->channel);
976 * Remove all elements from our hashmap.
978 * @param cls closure with the `struct Operation *`
979 * @param key current key code
980 * @param value value in the hash map
981 * @return #GNUNET_YES (we should continue to iterate)
984 filter_all (void *cls,
985 const struct GNUNET_HashCode *key,
988 struct Operation *op = cls;
989 struct ElementEntry *ee = value;
991 GNUNET_break (0 < op->state->my_element_count);
992 op->state->my_element_count--;
993 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Final reduction of my_elements, removing %s:%u\n",
998 GNUNET_h2s (&ee->element_hash),
1000 GNUNET_assert (GNUNET_YES ==
1001 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
1004 send_client_removed_element (op,
1011 * Handle a done message from a remote peer
1013 * @param cls the intersection operation
1014 * @param mh the message
1017 handle_intersection_p2p_done (void *cls,
1018 const struct IntersectionDoneMessage *idm)
1020 struct Operation *op = cls;
1022 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
1024 GNUNET_break_op (0);
1025 fail_intersection_operation (op);
1028 if (PHASE_BF_EXCHANGE != op->state->phase)
1030 /* wrong phase to conclude? FIXME: Or should we allow this
1031 if the other peer has _initially_ already an empty set? */
1032 GNUNET_break_op (0);
1033 fail_intersection_operation (op);
1036 if (0 == ntohl (idm->final_element_count))
1038 /* other peer determined empty set is the intersection,
1039 remove all elements */
1040 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
1044 if ( (op->state->my_element_count != ntohl (idm->final_element_count)) ||
1045 (0 != memcmp (&op->state->my_xor,
1046 &idm->element_xor_hash,
1047 sizeof (struct GNUNET_HashCode))) )
1049 /* Other peer thinks we are done, but we disagree on the result! */
1050 GNUNET_break_op (0);
1051 fail_intersection_operation (op);
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Got IntersectionDoneMessage, have %u elements in intersection\n",
1056 op->state->my_element_count);
1057 op->state->phase = PHASE_DONE_RECEIVED;
1058 GNUNET_CADET_receive_done (op->channel);
1060 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1061 if (GNUNET_SET_RESULT_FULL == op->result_mode)
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Sending full result set to client (%u elements)\n",
1065 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
1066 op->state->full_result_iter
1067 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
1068 send_remaining_elements (op);
1071 op->state->phase = PHASE_FINISHED;
1072 send_client_done_and_destroy (op);
1077 * Initiate a set intersection operation with a remote peer.
1079 * @param op operation that is created, should be initialized to
1080 * begin the evaluation
1081 * @param opaque_context message to be transmitted to the listener
1082 * to convince him to accept, may be NULL
1083 * @return operation-specific state to keep in @a op
1085 static struct OperationState *
1086 intersection_evaluate (struct Operation *op,
1087 const struct GNUNET_MessageHeader *opaque_context)
1089 struct OperationState *state;
1090 struct GNUNET_MQ_Envelope *ev;
1091 struct OperationRequestMessage *msg;
1093 ev = GNUNET_MQ_msg_nested_mh (msg,
1094 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
1098 /* the context message is too large!? */
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Initiating intersection operation evaluation\n");
1104 state = GNUNET_new (struct OperationState);
1105 /* we started the operation, thus we have to send the operation request */
1106 state->phase = PHASE_INITIAL;
1107 state->my_element_count = op->set->state->current_set_element_count;
1109 = GNUNET_CONTAINER_multihashmap_create (state->my_element_count,
1112 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
1113 msg->element_count = htonl (state->my_element_count);
1114 GNUNET_MQ_send (op->mq,
1116 state->phase = PHASE_COUNT_SENT;
1117 if (NULL != opaque_context)
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Sent op request with context message\n");
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Sent op request without context message\n");
1128 * Accept an intersection operation request from a remote peer. Only
1129 * initializes the private operation state.
1131 * @param op operation that will be accepted as an intersection operation
1133 static struct OperationState *
1134 intersection_accept (struct Operation *op)
1136 struct OperationState *state;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "Accepting set intersection operation\n");
1140 state = GNUNET_new (struct OperationState);
1141 state->phase = PHASE_INITIAL;
1142 state->my_element_count
1143 = op->set->state->current_set_element_count;
1145 = GNUNET_CONTAINER_multihashmap_create (GNUNET_MIN (state->my_element_count,
1146 op->remote_element_count),
1149 if (op->remote_element_count < state->my_element_count)
1151 /* If the other peer (Alice) has fewer elements than us (Bob),
1152 we just send the count as Alice should send the first BF */
1153 send_element_count (op);
1154 state->phase = PHASE_COUNT_SENT;
1157 /* We have fewer elements, so we start with the BF */
1158 begin_bf_exchange (op);
1164 * Destroy the intersection operation. Only things specific to the
1165 * intersection operation are destroyed.
1167 * @param op intersection operation to destroy
1170 intersection_op_cancel (struct Operation *op)
1172 /* check if the op was canceled twice */
1173 GNUNET_assert (NULL != op->state);
1174 if (NULL != op->state->remote_bf)
1176 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1177 op->state->remote_bf = NULL;
1179 if (NULL != op->state->local_bf)
1181 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1182 op->state->local_bf = NULL;
1184 if (NULL != op->state->my_elements)
1186 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1187 op->state->my_elements = NULL;
1189 if (NULL != op->state->full_result_iter)
1191 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
1192 op->state->full_result_iter = NULL;
1194 GNUNET_free (op->state);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Destroying intersection op state done\n");
1202 * Create a new set supporting the intersection operation.
1204 * @return the newly created set
1206 static struct SetState *
1207 intersection_set_create ()
1209 struct SetState *set_state;
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Intersection set created\n");
1213 set_state = GNUNET_new (struct SetState);
1214 set_state->current_set_element_count = 0;
1221 * Add the element from the given element message to the set.
1223 * @param set_state state of the set want to add to
1224 * @param ee the element to add to the set
1227 intersection_add (struct SetState *set_state,
1228 struct ElementEntry *ee)
1230 set_state->current_set_element_count++;
1235 * Destroy a set that supports the intersection operation
1237 * @param set_state the set to destroy
1240 intersection_set_destroy (struct SetState *set_state)
1242 GNUNET_free (set_state);
1247 * Remove the element given in the element message from the set.
1249 * @param set_state state of the set to remove from
1250 * @param element set element to remove
1253 intersection_remove (struct SetState *set_state,
1254 struct ElementEntry *element)
1256 GNUNET_assert (0 < set_state->current_set_element_count);
1257 set_state->current_set_element_count--;
1262 * Callback for channel death for the intersection operation.
1264 * @param op operation that lost the channel
1267 intersection_channel_death (struct Operation *op)
1269 if (GNUNET_YES == op->state->channel_death_expected)
1271 /* oh goodie, we are done! */
1272 send_client_done_and_destroy (op);
1276 /* sorry, channel went down early, too bad. */
1277 _GSS_operation_destroy (op,
1284 * Get the table with implementing functions for set intersection.
1286 * @return the operation specific VTable
1288 const struct SetVT *
1289 _GSS_intersection_vt ()
1291 static const struct SetVT intersection_vt = {
1292 .create = &intersection_set_create,
1293 .add = &intersection_add,
1294 .remove = &intersection_remove,
1295 .destroy_set = &intersection_set_destroy,
1296 .evaluate = &intersection_evaluate,
1297 .accept = &intersection_accept,
1298 .cancel = &intersection_op_cancel,
1299 .channel_death = &intersection_channel_death,
1302 return &intersection_vt;