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 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.
16 * @file set/gnunet-service-set_intersection.c
17 * @brief two-peer set intersection
18 * @author Christian Fuchs
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet-service-set.h"
24 #include "gnunet_block_lib.h"
25 #include "gnunet-service-set_protocol.h"
26 #include "gnunet-service-set_intersection.h"
31 * Current phase we are in for a intersection operation.
33 enum IntersectionOperationPhase
36 * We are just starting.
41 * We have send the number of our elements to the other
42 * peer, but did not setup our element set yet.
47 * We have initialized our set and are now reducing it by exchanging
48 * Bloom filters until one party notices the their element hashes
54 * We must next send the P2P DONE message (after finishing mostly
55 * with the local client). Then we will wait for the channel to close.
60 * We have received the P2P DONE message, and must finish with the
61 * local client before terminating the channel.
66 * The protocol is over. Results may still have to be sent to the
75 * State of an evaluate operation with another peer.
80 * The bf we currently receive
82 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
85 * BF of the set's element.
87 struct GNUNET_CONTAINER_BloomFilter *local_bf;
90 * Remaining elements in the intersection operation.
91 * Maps element-id-hashes to 'elements in our set'.
93 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
96 * Iterator for sending the final set of @e my_elements to the client.
98 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
101 * Evaluate operations are held in a linked list.
103 struct OperationState *next;
106 * Evaluate operations are held in a linked list.
108 struct OperationState *prev;
111 * For multipart BF transmissions, we have to store the
112 * bloomfilter-data until we fully received it.
117 * XOR of the keys of all of the elements (remaining) in my set.
118 * Always updated when elements are added or removed to
121 struct GNUNET_HashCode my_xor;
124 * XOR of the keys of all of the elements (remaining) in
125 * the other peer's set. Updated when we receive the
126 * other peer's Bloom filter.
128 struct GNUNET_HashCode other_xor;
131 * How many bytes of @e bf_data are valid?
133 uint32_t bf_data_offset;
136 * Current element count contained within @e my_elements.
137 * (May differ briefly during initialization.)
139 uint32_t my_element_count;
142 * size of the bloomfilter in @e bf_data.
144 uint32_t bf_data_size;
147 * size of the bloomfilter
149 uint32_t bf_bits_per_element;
152 * Salt currently used for BF construction (by us or the other peer,
153 * depending on where we are in the code).
158 * Current state of the operation.
160 enum IntersectionOperationPhase phase;
163 * Generation in which the operation handle
166 unsigned int generation_created;
169 * Did we send the client that we are done?
171 int client_done_sent;
174 * Set whenever we reach the state where the death of the
175 * channel is perfectly find and should NOT result in the
176 * operation being cancelled.
178 int channel_death_expected;
183 * Extra state required for efficient set intersection.
184 * Merely tracks the total number of elements.
189 * Number of currently valid elements in the set which have not been
192 uint32_t current_set_element_count;
197 * If applicable in the current operation mode, send a result message
198 * to the client indicating we removed an element.
200 * @param op intersection operation
201 * @param element element to send
204 send_client_removed_element (struct Operation *op,
205 struct GNUNET_SET_Element *element)
207 struct GNUNET_MQ_Envelope *ev;
208 struct GNUNET_SET_ResultMessage *rm;
210 if (GNUNET_SET_RESULT_REMOVED != op->result_mode)
211 return; /* Wrong mode for transmitting removed elements */
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Sending removed element (size %u) to client\n",
215 GNUNET_assert (0 != op->client_request_id);
216 ev = GNUNET_MQ_msg_extra (rm,
218 GNUNET_MESSAGE_TYPE_SET_RESULT);
224 rm->result_status = htons (GNUNET_SET_STATUS_OK);
225 rm->request_id = htonl (op->client_request_id);
226 rm->element_type = element->element_type;
227 GNUNET_memcpy (&rm[1],
230 GNUNET_MQ_send (op->set->cs->mq,
236 * Fills the "my_elements" hashmap with all relevant elements.
238 * @param cls the `struct Operation *` we are performing
239 * @param key current key code
240 * @param value the `struct ElementEntry *` from the hash map
241 * @return #GNUNET_YES (we should continue to iterate)
244 filtered_map_initialization (void *cls,
245 const struct GNUNET_HashCode *key,
248 struct Operation *op = cls;
249 struct ElementEntry *ee = value;
250 struct GNUNET_HashCode mutated_hash;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "FIMA called for %s:%u\n",
255 GNUNET_h2s (&ee->element_hash),
258 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Reduced initialization, not starting with %s:%u (wrong generation)\n",
262 GNUNET_h2s (&ee->element_hash),
264 return GNUNET_YES; /* element not valid in our operation's generation */
267 /* Test if element is in other peer's bloomfilter */
268 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Testing mingled hash %s with salt %u\n",
273 GNUNET_h2s (&mutated_hash),
276 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
279 /* remove this element */
280 send_client_removed_element (op,
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Reduced initialization, not starting with %s:%u\n",
284 GNUNET_h2s (&ee->element_hash),
288 op->state->my_element_count++;
289 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Filtered initialization of my_elements, adding %s:%u\n",
294 GNUNET_h2s (&ee->element_hash),
296 GNUNET_break (GNUNET_YES ==
297 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
307 * Removes elements from our hashmap if they are not contained within the
308 * provided remote bloomfilter.
310 * @param cls closure with the `struct Operation *`
311 * @param key current key code
312 * @param value value in the hash map
313 * @return #GNUNET_YES (we should continue to iterate)
316 iterator_bf_reduce (void *cls,
317 const struct GNUNET_HashCode *key,
320 struct Operation *op = cls;
321 struct ElementEntry *ee = value;
322 struct GNUNET_HashCode mutated_hash;
324 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Testing mingled hash %s with salt %u\n",
329 GNUNET_h2s (&mutated_hash),
332 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
335 GNUNET_break (0 < op->state->my_element_count);
336 op->state->my_element_count--;
337 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Bloom filter reduction of my_elements, removing %s:%u\n",
342 GNUNET_h2s (&ee->element_hash),
344 GNUNET_assert (GNUNET_YES ==
345 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
348 send_client_removed_element (op,
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Bloom filter reduction of my_elements, keeping %s:%u\n",
355 GNUNET_h2s (&ee->element_hash),
363 * Create initial bloomfilter based on all the elements given.
365 * @param cls the `struct Operation *`
366 * @param key current key code
367 * @param value the `struct ElementEntry` to process
368 * @return #GNUNET_YES (we should continue to iterate)
371 iterator_bf_create (void *cls,
372 const struct GNUNET_HashCode *key,
375 struct Operation *op = cls;
376 struct ElementEntry *ee = value;
377 struct GNUNET_HashCode mutated_hash;
379 GNUNET_BLOCK_mingle_hash (&ee->element_hash,
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Initializing BF with hash %s with salt %u\n",
384 GNUNET_h2s (&mutated_hash),
386 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
393 * Inform the client that the intersection operation has failed,
394 * and proceed to destroy the evaluate operation.
396 * @param op the intersection operation to fail
399 fail_intersection_operation (struct Operation *op)
401 struct GNUNET_MQ_Envelope *ev;
402 struct GNUNET_SET_ResultMessage *msg;
404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
405 "Intersection operation failed\n");
406 if (NULL != op->state->my_elements)
408 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
409 op->state->my_elements = NULL;
411 ev = GNUNET_MQ_msg (msg,
412 GNUNET_MESSAGE_TYPE_SET_RESULT);
413 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
414 msg->request_id = htonl (op->client_request_id);
415 msg->element_type = htons (0);
416 GNUNET_MQ_send (op->set->cs->mq,
418 _GSS_operation_destroy (op,
424 * Send a bloomfilter to our peer. After the result done message has
425 * been sent to the client, destroy the evaluate operation.
427 * @param op intersection operation
430 send_bloomfilter (struct Operation *op)
432 struct GNUNET_MQ_Envelope *ev;
433 struct BFMessage *msg;
435 uint32_t bf_elementbits;
440 /* We consider the ratio of the set sizes to determine
441 the number of bits per element, as the smaller set
442 should use more bits to maximize its set reduction
443 potential and minimize overall bandwidth consumption. */
444 bf_elementbits = 2 + ceil (log2((double)
445 (op->remote_element_count /
446 (double) op->state->my_element_count)));
447 if (bf_elementbits < 1)
448 bf_elementbits = 1; /* make sure k is not 0 */
449 /* optimize BF-size to ~50% of bits set */
450 bf_size = ceil ((double) (op->state->my_element_count
451 * bf_elementbits / log(2)));
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Sending Bloom filter (%u) of size %u bytes\n",
454 (unsigned int) bf_elementbits,
455 (unsigned int) bf_size);
456 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
459 op->state->salt = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
461 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
465 /* send our Bloom filter */
466 chunk_size = 60 * 1024 - sizeof (struct BFMessage);
467 if (bf_size <= chunk_size)
470 chunk_size = bf_size;
471 ev = GNUNET_MQ_msg_extra (msg,
473 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
474 GNUNET_assert (GNUNET_SYSERR !=
475 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
478 msg->sender_element_count = htonl (op->state->my_element_count);
479 msg->bloomfilter_total_length = htonl (bf_size);
480 msg->bits_per_element = htonl (bf_elementbits);
481 msg->sender_mutator = htonl (op->state->salt);
482 msg->element_xor_hash = op->state->my_xor;
483 GNUNET_MQ_send (op->mq, ev);
488 bf_data = GNUNET_malloc (bf_size);
489 GNUNET_assert (GNUNET_SYSERR !=
490 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
494 while (offset < bf_size)
496 if (bf_size - chunk_size < offset)
497 chunk_size = bf_size - offset;
498 ev = GNUNET_MQ_msg_extra (msg,
500 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
501 GNUNET_memcpy (&msg[1],
504 offset += chunk_size;
505 msg->sender_element_count = htonl (op->state->my_element_count);
506 msg->bloomfilter_total_length = htonl (bf_size);
507 msg->bits_per_element = htonl (bf_elementbits);
508 msg->sender_mutator = htonl (op->state->salt);
509 msg->element_xor_hash = op->state->my_xor;
510 GNUNET_MQ_send (op->mq, ev);
512 GNUNET_free (bf_data);
514 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
515 op->state->local_bf = NULL;
520 * Signal to the client that the operation has finished and
521 * destroy the operation.
523 * @param cls operation to destroy
526 send_client_done_and_destroy (void *cls)
528 struct Operation *op = cls;
529 struct GNUNET_MQ_Envelope *ev;
530 struct GNUNET_SET_ResultMessage *rm;
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Intersection succeeded, sending DONE to local client\n");
534 ev = GNUNET_MQ_msg (rm,
535 GNUNET_MESSAGE_TYPE_SET_RESULT);
536 rm->request_id = htonl (op->client_request_id);
537 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
538 rm->element_type = htons (0);
539 GNUNET_MQ_send (op->set->cs->mq,
541 _GSS_operation_destroy (op,
547 * Remember that we are done dealing with the local client
548 * AND have sent the other peer our message that we are done,
549 * so we are not just waiting for the channel to die before
550 * telling the local client that we are done as our last act.
552 * @param cls the `struct Operation`.
555 finished_local_operations (void *cls)
557 struct Operation *op = cls;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "DONE sent to other peer, now waiting for other end to close the channel\n");
561 op->state->phase = PHASE_FINISHED;
562 op->state->channel_death_expected = GNUNET_YES;
567 * Notify the other peer that we are done. Once this message
568 * is out, we still need to notify the local client that we
571 * @param op operation to notify for.
574 send_p2p_done (struct Operation *op)
576 struct GNUNET_MQ_Envelope *ev;
577 struct IntersectionDoneMessage *idm;
579 GNUNET_assert (PHASE_MUST_SEND_DONE == op->state->phase);
580 GNUNET_assert (GNUNET_NO == op->state->channel_death_expected);
581 ev = GNUNET_MQ_msg (idm,
582 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE);
583 idm->final_element_count = htonl (op->state->my_element_count);
584 idm->element_xor_hash = op->state->my_xor;
585 GNUNET_MQ_notify_sent (ev,
586 &finished_local_operations,
588 GNUNET_MQ_send (op->mq,
594 * Send all elements in the full result iterator.
596 * @param cls the `struct Operation *`
599 send_remaining_elements (void *cls)
601 struct Operation *op = cls;
603 const struct ElementEntry *ee;
604 struct GNUNET_MQ_Envelope *ev;
605 struct GNUNET_SET_ResultMessage *rm;
606 const struct GNUNET_SET_Element *element;
609 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter,
612 if (GNUNET_NO == res)
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Sending done and destroy because iterator ran out\n");
616 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
617 op->state->full_result_iter = NULL;
618 if (PHASE_DONE_RECEIVED == op->state->phase)
620 op->state->phase = PHASE_FINISHED;
621 send_client_done_and_destroy (op);
623 else if (PHASE_MUST_SEND_DONE == op->state->phase)
634 element = &ee->element;
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Sending element %s:%u to client (full set)\n",
637 GNUNET_h2s (&ee->element_hash),
639 GNUNET_assert (0 != op->client_request_id);
640 ev = GNUNET_MQ_msg_extra (rm,
642 GNUNET_MESSAGE_TYPE_SET_RESULT);
643 GNUNET_assert (NULL != ev);
644 rm->result_status = htons (GNUNET_SET_STATUS_OK);
645 rm->request_id = htonl (op->client_request_id);
646 rm->element_type = element->element_type;
647 GNUNET_memcpy (&rm[1],
650 GNUNET_MQ_notify_sent (ev,
651 &send_remaining_elements,
653 GNUNET_MQ_send (op->set->cs->mq,
659 * Fills the "my_elements" hashmap with the initial set of
660 * (non-deleted) elements from the set of the specification.
662 * @param cls closure with the `struct Operation *`
663 * @param key current key code for the element
664 * @param value value in the hash map with the `struct ElementEntry *`
665 * @return #GNUNET_YES (we should continue to iterate)
668 initialize_map_unfiltered (void *cls,
669 const struct GNUNET_HashCode *key,
672 struct ElementEntry *ee = value;
673 struct Operation *op = cls;
675 if (GNUNET_NO == _GSS_is_element_of_operation (ee, op))
676 return GNUNET_YES; /* element not live in operation's generation */
677 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Initial full initialization of my_elements, adding %s:%u\n",
682 GNUNET_h2s (&ee->element_hash),
684 GNUNET_break (GNUNET_YES ==
685 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
694 * Send our element count to the peer, in case our element count is
697 * @param op intersection operation
700 send_element_count (struct Operation *op)
702 struct GNUNET_MQ_Envelope *ev;
703 struct IntersectionElementInfoMessage *msg;
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706 "Sending our element count (%u)\n",
707 op->state->my_element_count);
708 ev = GNUNET_MQ_msg (msg,
709 GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
710 msg->sender_element_count = htonl (op->state->my_element_count);
711 GNUNET_MQ_send (op->mq, ev);
716 * We go first, initialize our map with all elements and
717 * send the first Bloom filter.
719 * @param op operation to start exchange for
722 begin_bf_exchange (struct Operation *op)
724 op->state->phase = PHASE_BF_EXCHANGE;
725 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
726 &initialize_map_unfiltered,
728 send_bloomfilter (op);
733 * Handle the initial `struct IntersectionElementInfoMessage` from a
736 * @param cls the intersection operation
737 * @param mh the header of the message
740 handle_intersection_p2p_element_info (void *cls,
741 const struct IntersectionElementInfoMessage *msg)
743 struct Operation *op = cls;
745 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
748 fail_intersection_operation(op);
751 op->remote_element_count = ntohl (msg->sender_element_count);
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "Received remote element count (%u), I have %u\n",
754 op->remote_element_count,
755 op->state->my_element_count);
756 if ( ( (PHASE_INITIAL != op->state->phase) &&
757 (PHASE_COUNT_SENT != op->state->phase) ) ||
758 (op->state->my_element_count > op->remote_element_count) ||
759 (0 == op->state->my_element_count) ||
760 (0 == op->remote_element_count) )
763 fail_intersection_operation(op);
766 GNUNET_break (NULL == op->state->remote_bf);
767 begin_bf_exchange (op);
768 GNUNET_CADET_receive_done (op->channel);
773 * Process a Bloomfilter once we got all the chunks.
775 * @param op the intersection operation
778 process_bf (struct Operation *op)
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n",
783 op->remote_element_count,
784 op->state->my_element_count,
785 GNUNET_CONTAINER_multihashmap_size (op->set->content->elements));
786 switch (op->state->phase)
790 fail_intersection_operation(op);
792 case PHASE_COUNT_SENT:
793 /* This is the first BF being sent, build our initial map with
794 filtering in place */
795 op->state->my_element_count = 0;
796 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
797 &filtered_map_initialization,
800 case PHASE_BF_EXCHANGE:
801 /* Update our set by reduction */
802 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
806 case PHASE_MUST_SEND_DONE:
808 fail_intersection_operation(op);
810 case PHASE_DONE_RECEIVED:
812 fail_intersection_operation(op);
816 fail_intersection_operation(op);
819 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
820 op->state->remote_bf = NULL;
822 if ( (0 == op->state->my_element_count) || /* fully disjoint */
823 ( (op->state->my_element_count == op->remote_element_count) &&
824 (0 == memcmp (&op->state->my_xor,
825 &op->state->other_xor,
826 sizeof (struct GNUNET_HashCode))) ) )
829 op->state->phase = PHASE_MUST_SEND_DONE;
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Intersection succeeded, sending DONE to other peer\n");
832 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
833 op->state->local_bf = NULL;
834 if (GNUNET_SET_RESULT_FULL == op->result_mode)
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Sending full result set (%u elements)\n",
838 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
839 op->state->full_result_iter
840 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
841 send_remaining_elements (op);
847 op->state->phase = PHASE_BF_EXCHANGE;
848 send_bloomfilter (op);
853 * Check an BF message from a remote peer.
855 * @param cls the intersection operation
856 * @param msg the header of the message
857 * @return #GNUNET_OK if @a msg is well-formed
860 check_intersection_p2p_bf (void *cls,
861 const struct BFMessage *msg)
863 struct Operation *op = cls;
865 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
868 return GNUNET_SYSERR;
875 * Handle an BF message from a remote peer.
877 * @param cls the intersection operation
878 * @param msg the header of the message
881 handle_intersection_p2p_bf (void *cls,
882 const struct BFMessage *msg)
884 struct Operation *op = cls;
887 uint32_t bf_bits_per_element;
889 switch (op->state->phase)
893 fail_intersection_operation (op);
895 case PHASE_COUNT_SENT:
896 case PHASE_BF_EXCHANGE:
897 bf_size = ntohl (msg->bloomfilter_total_length);
898 bf_bits_per_element = ntohl (msg->bits_per_element);
899 chunk_size = htons (msg->header.size) - sizeof (struct BFMessage);
900 op->state->other_xor = msg->element_xor_hash;
901 if (bf_size == chunk_size)
903 if (NULL != op->state->bf_data)
906 fail_intersection_operation (op);
909 /* single part, done here immediately */
911 = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
913 bf_bits_per_element);
914 op->state->salt = ntohl (msg->sender_mutator);
915 op->remote_element_count = ntohl (msg->sender_element_count);
919 /* multipart chunk */
920 if (NULL == op->state->bf_data)
922 /* first chunk, initialize */
923 op->state->bf_data = GNUNET_malloc (bf_size);
924 op->state->bf_data_size = bf_size;
925 op->state->bf_bits_per_element = bf_bits_per_element;
926 op->state->bf_data_offset = 0;
927 op->state->salt = ntohl (msg->sender_mutator);
928 op->remote_element_count = ntohl (msg->sender_element_count);
933 if ( (op->state->bf_data_size != bf_size) ||
934 (op->state->bf_bits_per_element != bf_bits_per_element) ||
935 (op->state->bf_data_offset + chunk_size > bf_size) ||
936 (op->state->salt != ntohl (msg->sender_mutator)) ||
937 (op->remote_element_count != ntohl (msg->sender_element_count)) )
940 fail_intersection_operation (op);
944 GNUNET_memcpy (&op->state->bf_data[op->state->bf_data_offset],
945 (const char*) &msg[1],
947 op->state->bf_data_offset += chunk_size;
948 if (op->state->bf_data_offset == bf_size)
950 /* last chunk, run! */
952 = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
954 bf_bits_per_element);
955 GNUNET_free (op->state->bf_data);
956 op->state->bf_data = NULL;
957 op->state->bf_data_size = 0;
963 fail_intersection_operation (op);
966 GNUNET_CADET_receive_done (op->channel);
971 * Remove all elements from our hashmap.
973 * @param cls closure with the `struct Operation *`
974 * @param key current key code
975 * @param value value in the hash map
976 * @return #GNUNET_YES (we should continue to iterate)
979 filter_all (void *cls,
980 const struct GNUNET_HashCode *key,
983 struct Operation *op = cls;
984 struct ElementEntry *ee = value;
986 GNUNET_break (0 < op->state->my_element_count);
987 op->state->my_element_count--;
988 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
992 "Final reduction of my_elements, removing %s:%u\n",
993 GNUNET_h2s (&ee->element_hash),
995 GNUNET_assert (GNUNET_YES ==
996 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
999 send_client_removed_element (op,
1006 * Handle a done message from a remote peer
1008 * @param cls the intersection operation
1009 * @param mh the message
1012 handle_intersection_p2p_done (void *cls,
1013 const struct IntersectionDoneMessage *idm)
1015 struct Operation *op = cls;
1017 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
1019 GNUNET_break_op (0);
1020 fail_intersection_operation (op);
1023 if (PHASE_BF_EXCHANGE != op->state->phase)
1025 /* wrong phase to conclude? FIXME: Or should we allow this
1026 if the other peer has _initially_ already an empty set? */
1027 GNUNET_break_op (0);
1028 fail_intersection_operation (op);
1031 if (0 == ntohl (idm->final_element_count))
1033 /* other peer determined empty set is the intersection,
1034 remove all elements */
1035 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
1039 if ( (op->state->my_element_count != ntohl (idm->final_element_count)) ||
1040 (0 != memcmp (&op->state->my_xor,
1041 &idm->element_xor_hash,
1042 sizeof (struct GNUNET_HashCode))) )
1044 /* Other peer thinks we are done, but we disagree on the result! */
1045 GNUNET_break_op (0);
1046 fail_intersection_operation (op);
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "Got IntersectionDoneMessage, have %u elements in intersection\n",
1051 op->state->my_element_count);
1052 op->state->phase = PHASE_DONE_RECEIVED;
1053 GNUNET_CADET_receive_done (op->channel);
1055 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1056 if (GNUNET_SET_RESULT_FULL == op->result_mode)
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Sending full result set to client (%u elements)\n",
1060 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
1061 op->state->full_result_iter
1062 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
1063 send_remaining_elements (op);
1066 op->state->phase = PHASE_FINISHED;
1067 send_client_done_and_destroy (op);
1072 * Initiate a set intersection operation with a remote peer.
1074 * @param op operation that is created, should be initialized to
1075 * begin the evaluation
1076 * @param opaque_context message to be transmitted to the listener
1077 * to convince him to accept, may be NULL
1078 * @return operation-specific state to keep in @a op
1080 static struct OperationState *
1081 intersection_evaluate (struct Operation *op,
1082 const struct GNUNET_MessageHeader *opaque_context)
1084 struct OperationState *state;
1085 struct GNUNET_MQ_Envelope *ev;
1086 struct OperationRequestMessage *msg;
1088 ev = GNUNET_MQ_msg_nested_mh (msg,
1089 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
1093 /* the context message is too large!? */
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Initiating intersection operation evaluation\n");
1099 state = GNUNET_new (struct OperationState);
1100 /* we started the operation, thus we have to send the operation request */
1101 state->phase = PHASE_INITIAL;
1102 state->my_element_count = op->set->state->current_set_element_count;
1104 = GNUNET_CONTAINER_multihashmap_create (state->my_element_count,
1107 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
1108 msg->element_count = htonl (state->my_element_count);
1109 GNUNET_MQ_send (op->mq,
1111 state->phase = PHASE_COUNT_SENT;
1112 if (NULL != opaque_context)
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Sent op request with context message\n");
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Sent op request without context message\n");
1123 * Accept an intersection operation request from a remote peer. Only
1124 * initializes the private operation state.
1126 * @param op operation that will be accepted as an intersection operation
1128 static struct OperationState *
1129 intersection_accept (struct Operation *op)
1131 struct OperationState *state;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Accepting set intersection operation\n");
1135 state = GNUNET_new (struct OperationState);
1136 state->phase = PHASE_INITIAL;
1137 state->my_element_count
1138 = op->set->state->current_set_element_count;
1140 = GNUNET_CONTAINER_multihashmap_create (GNUNET_MIN (state->my_element_count,
1141 op->remote_element_count),
1144 if (op->remote_element_count < state->my_element_count)
1146 /* If the other peer (Alice) has fewer elements than us (Bob),
1147 we just send the count as Alice should send the first BF */
1148 send_element_count (op);
1149 state->phase = PHASE_COUNT_SENT;
1152 /* We have fewer elements, so we start with the BF */
1153 begin_bf_exchange (op);
1159 * Destroy the intersection operation. Only things specific to the
1160 * intersection operation are destroyed.
1162 * @param op intersection operation to destroy
1165 intersection_op_cancel (struct Operation *op)
1167 /* check if the op was canceled twice */
1168 GNUNET_assert (NULL != op->state);
1169 if (NULL != op->state->remote_bf)
1171 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1172 op->state->remote_bf = NULL;
1174 if (NULL != op->state->local_bf)
1176 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1177 op->state->local_bf = NULL;
1179 if (NULL != op->state->my_elements)
1181 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1182 op->state->my_elements = NULL;
1184 if (NULL != op->state->full_result_iter)
1186 GNUNET_CONTAINER_multihashmap_iterator_destroy (op->state->full_result_iter);
1187 op->state->full_result_iter = NULL;
1189 GNUNET_free (op->state);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Destroying intersection op state done\n");
1197 * Create a new set supporting the intersection operation.
1199 * @return the newly created set
1201 static struct SetState *
1202 intersection_set_create ()
1204 struct SetState *set_state;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Intersection set created\n");
1208 set_state = GNUNET_new (struct SetState);
1209 set_state->current_set_element_count = 0;
1216 * Add the element from the given element message to the set.
1218 * @param set_state state of the set want to add to
1219 * @param ee the element to add to the set
1222 intersection_add (struct SetState *set_state,
1223 struct ElementEntry *ee)
1225 set_state->current_set_element_count++;
1230 * Destroy a set that supports the intersection operation
1232 * @param set_state the set to destroy
1235 intersection_set_destroy (struct SetState *set_state)
1237 GNUNET_free (set_state);
1242 * Remove the element given in the element message from the set.
1244 * @param set_state state of the set to remove from
1245 * @param element set element to remove
1248 intersection_remove (struct SetState *set_state,
1249 struct ElementEntry *element)
1251 GNUNET_assert (0 < set_state->current_set_element_count);
1252 set_state->current_set_element_count--;
1257 * Callback for channel death for the intersection operation.
1259 * @param op operation that lost the channel
1262 intersection_channel_death (struct Operation *op)
1264 if (GNUNET_YES == op->state->channel_death_expected)
1266 /* oh goodie, we are done! */
1267 send_client_done_and_destroy (op);
1271 /* sorry, channel went down early, too bad. */
1272 _GSS_operation_destroy (op,
1279 * Get the table with implementing functions for set intersection.
1281 * @return the operation specific VTable
1283 const struct SetVT *
1284 _GSS_intersection_vt ()
1286 static const struct SetVT intersection_vt = {
1287 .create = &intersection_set_create,
1288 .add = &intersection_add,
1289 .remove = &intersection_remove,
1290 .destroy_set = &intersection_set_destroy,
1291 .evaluate = &intersection_evaluate,
1292 .accept = &intersection_accept,
1293 .cancel = &intersection_op_cancel,
1294 .channel_death = &intersection_channel_death,
1297 return &intersection_vt;