2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file set/gnunet-service-set_intersection.c
23 * @brief two-peer set intersection
24 * @author Christian Fuchs
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-set.h"
29 #include "gnunet_block_lib.h"
30 #include "set_protocol.h"
33 #define BLOOMFILTER_SIZE GNUNET_CRYPTO_HASH_LENGTH
35 #define CALCULATE_BF_SIZE(A, B, s, k) \
37 k = ceil(1 + log2((double) (2*B / (double) A)));\
38 if (k<1) k=1; /* k can be calculated as 0 */\
39 s = ceil((double) (A * k / log(2))); \
43 * Current phase we are in for a intersection operation.
45 enum IntersectionOperationPhase
48 * Alices has suggested an operation to bob,
49 * and is waiting for a bf or session end.
53 * Bob has accepted the operation, Bob and Alice are now exchanging bfs
54 * until one notices the their element count is equal
58 * if both peers have an equal peercount, they enter this state for
59 * one more turn, to see if they actually have agreed on a correct set.
60 * if a peer finds the same element count after the next iteration,
61 * it ends the the session
65 * The protocol is over.
66 * Results may still have to be sent to the client.
73 * State of an evaluate operation
79 * The bf we currently receive
81 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
84 * BF of the set's element.
86 struct GNUNET_CONTAINER_BloomFilter *local_bf;
89 * for multipart msgs we have to store the bloomfilter-data until we fully sent it.
94 * size of the bloomfilter
96 uint32_t bf_data_size;
99 * size of the bloomfilter
101 uint32_t bf_bits_per_element;
104 * Current state of the operation.
106 enum IntersectionOperationPhase phase;
109 * Generation in which the operation handle
112 unsigned int generation_created;
115 * Maps element-id-hashes to 'elements in our set'.
117 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
120 * Current element count contained within contained_elements
122 uint32_t my_element_count;
125 * Iterator for sending elements on the key to element mapping to the client.
127 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
130 * Evaluate operations are held in
133 struct OperationState *next;
136 * Evaluate operations are held in
139 struct OperationState *prev;
142 * Did we send the client that we are done?
144 int client_done_sent;
149 * Extra state required for efficient set intersection.
154 * Number of currently valid elements in the set which have not been removed
156 uint32_t current_set_element_count;
161 * Send a result message to the client indicating
162 * we removed an element
164 * @param op union operation
165 * @param element element to send
168 send_client_element (struct Operation *op,
169 struct GNUNET_SET_Element *element)
171 struct GNUNET_MQ_Envelope *ev;
172 struct GNUNET_SET_ResultMessage *rm;
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending removed element (size %u) to client\n", element->size);
175 GNUNET_assert (0 != op->spec->client_request_id);
176 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
179 GNUNET_MQ_discard (ev);
183 rm->result_status = htons (GNUNET_SET_STATUS_OK);
184 rm->request_id = htonl (op->spec->client_request_id);
185 rm->element_type = element->type;
186 memcpy (&rm[1], element->data, element->size);
187 GNUNET_MQ_send (op->spec->set->client_mq, ev);
194 * fills the contained-elements hashmap with all relevant
195 * elements and adds their mutated hashes to our local bloomfilter with mutator+1
198 * @param key current key code
199 * @param value value in the hash map
200 * @return #GNUNET_YES if we should continue to
205 iterator_initialization_by_alice (void *cls,
206 const struct GNUNET_HashCode *key,
209 struct ElementEntry *ee = value;
210 struct Operation *op = cls;
211 struct GNUNET_HashCode mutated_hash;
213 //only consider this element, if it is valid for us
214 if ((op->generation_created < ee->generation_removed)
215 && (op->generation_created >= ee->generation_added))
218 // not contained according to bob's bloomfilter
219 GNUNET_BLOCK_mingle_hash(&ee->element_hash,
222 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
224 if (GNUNET_SET_RESULT_REMOVED == op->spec->result_mode)
225 send_client_element (op, &ee->element);
229 op->state->my_element_count++;
230 GNUNET_assert (GNUNET_YES ==
231 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
232 &ee->element_hash, ee,
233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
239 * fills the contained-elements hashmap with all relevant
240 * elements and adds their mutated hashes to our local bloomfilter
243 * @param key current key code
244 * @param value value in the hash map
245 * @return #GNUNET_YES if we should continue to
250 iterator_initialization (void *cls,
251 const struct GNUNET_HashCode *key,
254 struct ElementEntry *ee = value;
255 struct Operation *op = cls;
257 //only consider this element, if it is valid for us
258 if ((op->generation_created < ee->generation_removed)
259 && (op->generation_created >= ee->generation_added))
262 GNUNET_assert (GNUNET_YES ==
263 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
264 &ee->element_hash, ee,
265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
271 * removes element from a hashmap if it is not contained within the
272 * provided remote bloomfilter. Then, fill our new bloomfilter.
275 * @param key current key code
276 * @param value value in the hash map
277 * @return #GNUNET_YES if we should continue to
282 iterator_bf_reduce (void *cls,
283 const struct GNUNET_HashCode *key,
286 struct ElementEntry *ee = value;
287 struct Operation *op = cls;
288 struct GNUNET_HashCode mutated_hash;
290 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
293 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
296 op->state->my_element_count--;
297 GNUNET_assert (GNUNET_YES ==
298 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
301 if (GNUNET_SET_RESULT_REMOVED == op->spec->result_mode)
302 send_client_element (op, &ee->element);
309 * create a bloomfilter based on the elements given
312 * @param key current key code
313 * @param value value in the hash map
314 * @return #GNUNET_YES if we should continue to
319 iterator_bf_create (void *cls,
320 const struct GNUNET_HashCode *key,
323 struct ElementEntry *ee = value;
324 struct Operation *op = cls;
325 struct GNUNET_HashCode mutated_hash;
327 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
329 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
335 * Inform the client that the union operation has failed,
336 * and proceed to destroy the evaluate operation.
338 * @param op the intersection operation to fail
341 fail_intersection_operation (struct Operation *op)
343 struct GNUNET_MQ_Envelope *ev;
344 struct GNUNET_SET_ResultMessage *msg;
346 if (op->state->my_elements)
347 GNUNET_CONTAINER_multihashmap_destroy(op->state->my_elements);
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "intersection operation failed\n");
351 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
352 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
353 msg->request_id = htonl (op->spec->client_request_id);
354 msg->element_type = htons (0);
355 GNUNET_MQ_send (op->spec->set->client_mq, ev);
356 _GSS_operation_destroy (op);
361 * Send a request for the evaluate operation to a remote peer
363 * @param op operation with the other peer
366 send_operation_request (struct Operation *op)
368 struct GNUNET_MQ_Envelope *ev;
369 struct OperationRequestMessage *msg;
371 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
372 op->spec->context_msg);
376 /* the context message is too large */
378 GNUNET_SERVER_client_disconnect (op->spec->set->client);
381 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
382 msg->app_id = op->spec->app_id;
383 msg->salt = htonl (op->spec->salt);
384 msg->element_count = htonl(op->state->my_element_count);
386 GNUNET_MQ_send (op->mq, ev);
388 if (NULL != op->spec->context_msg)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
393 if (NULL != op->spec->context_msg)
395 GNUNET_free (op->spec->context_msg);
396 op->spec->context_msg = NULL;
401 send_bloomfilter_multipart (struct Operation *op, uint32_t offset)
403 struct GNUNET_MQ_Envelope *ev;
405 uint32_t chunk_size = (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct BFPart));
406 uint32_t todo_size = op->state->bf_data_size - offset;
408 if (todo_size < chunk_size)
409 chunk_size = todo_size;
411 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART);
413 msg->chunk_length = htonl (chunk_size);
414 msg->chunk_offset = htonl (offset);
415 memcpy(&msg[1], &op->state->bf_data[offset], chunk_size);
417 GNUNET_MQ_send (op->mq, ev);
419 if (op->state->bf_data_size == offset + chunk_size)
422 GNUNET_free(op->state->bf_data);
423 op->state->bf_data = NULL;
427 send_bloomfilter_multipart (op, offset + chunk_size);
431 * Send a bloomfilter to our peer.
432 * that the operation is over.
433 * After the result done message has been sent to the client,
434 * destroy the evaluate operation.
436 * @param op intersection operation
439 send_bloomfilter (struct Operation *op)
441 struct GNUNET_MQ_Envelope *ev;
442 struct BFMessage *msg;
444 uint32_t bf_elementbits;
446 struct GNUNET_CONTAINER_BloomFilter * local_bf;
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending bf of size %u\n");
450 CALCULATE_BF_SIZE(op->state->my_element_count,
451 op->spec->remote_element_count,
455 local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
460 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
464 // send our bloomfilter
465 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > bf_size + sizeof (struct BFMessage)) {
467 chunk_size = bf_size;
468 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
469 GNUNET_assert (GNUNET_SYSERR !=
470 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
476 chunk_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BFMessage);
477 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
478 op->state->bf_data = (char *) GNUNET_malloc (bf_size);
479 GNUNET_assert (GNUNET_SYSERR !=
480 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
483 memcpy (&msg[1], op->state->bf_data, chunk_size);
484 op->state->bf_data_size = bf_size;
486 GNUNET_CONTAINER_bloomfilter_free (local_bf);
488 msg->sender_element_count = htonl (op->state->my_element_count);
489 msg->bloomfilter_total_length = htonl (bf_size);
490 msg->bloomfilter_length = htonl (chunk_size);
491 msg->bits_per_element = htonl (bf_elementbits);
492 msg->sender_mutator = htonl (op->spec->salt);
494 GNUNET_MQ_send (op->mq, ev);
496 if (op->state->bf_data)
497 send_bloomfilter_multipart (op, chunk_size);
502 * Signal to the client that the operation has finished and
503 * destroy the operation.
505 * @param cls operation to destroy
508 send_client_done_and_destroy (void *cls)
510 struct Operation *op = cls;
511 struct GNUNET_MQ_Envelope *ev;
512 struct GNUNET_SET_ResultMessage *rm;
513 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
514 rm->request_id = htonl (op->spec->client_request_id);
515 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
516 rm->element_type = htons (0);
517 GNUNET_MQ_send (op->spec->set->client_mq, ev);
518 _GSS_operation_destroy (op);
523 * Send all elements in the full result iterator.
525 * @param cls operation
528 send_remaining_elements (void *cls)
530 struct Operation *op = cls;
531 struct ElementEntry *remaining; //TODO rework this, key entry does not exist here
532 struct GNUNET_MQ_Envelope *ev;
533 struct GNUNET_SET_ResultMessage *rm;
534 struct GNUNET_SET_Element *element;
537 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter, NULL, (const void **) &remaining);
538 if (GNUNET_NO == res) {
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending done and destroy because iterator ran out\n");
540 send_client_done_and_destroy (op);
544 element = &remaining->element;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client (full set)\n", element->size);
546 GNUNET_assert (0 != op->spec->client_request_id);
548 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
549 GNUNET_assert (NULL != ev);
551 rm->result_status = htons (GNUNET_SET_STATUS_OK);
552 rm->request_id = htonl (op->spec->client_request_id);
553 rm->element_type = element->type;
554 memcpy (&rm[1], element->data, element->size);
556 GNUNET_MQ_notify_sent (ev, send_remaining_elements, op);
557 GNUNET_MQ_send (op->spec->set->client_mq, ev);
562 * Inform the peer that this operation is complete.
564 * @param op the intersection operation to fail
567 send_peer_done (struct Operation *op)
569 struct GNUNET_MQ_Envelope *ev;
571 op->state->phase = PHASE_FINISHED;
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Intersection succeeded, sending DONE\n");
573 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
574 op->state->local_bf = NULL;
576 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
577 GNUNET_MQ_send (op->mq, ev);
582 * Process a Bloomfilter once we got all the chunks
584 * @param op the intersection operation
587 process_bf (struct Operation *op){
588 uint32_t old_elements;
589 uint32_t peer_elements;
591 old_elements = op->state->my_element_count;
592 peer_elements = op->spec->remote_element_count;
593 switch (op->state->phase)
596 // If we are ot our first msg
597 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (op->state->my_element_count, GNUNET_YES);
599 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
600 &iterator_initialization_by_alice,
603 case PHASE_BF_EXCHANGE:
604 case PHASE_MAYBE_FINISHED:
605 // if we are bob or alice and are continuing operation
606 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
612 fail_intersection_operation(op);
614 // the iterators created a new BF with salt+1
615 // the peer needs this information for decoding the next BF
616 // this behavior can be modified at will later on.
619 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
620 op->state->remote_bf = NULL;
622 if ((0 == op->state->my_element_count) // fully disjoint
623 || ((op->state->phase == PHASE_MAYBE_FINISHED) // we agree on a shared set of elements
624 && (old_elements == op->state->my_element_count)
625 && (op->state->my_element_count == peer_elements))) {
626 // In the last round we though we were finished, we now know this is correct
631 op->state->phase = PHASE_BF_EXCHANGE;
632 if (op->state->my_element_count == peer_elements)
633 // maybe we are finished, but we do one more round to make certain
634 // we don't have false positives ...
635 op->state->phase = PHASE_MAYBE_FINISHED;
637 send_bloomfilter (op);
642 * Handle an BF multipart message from a remote peer.
644 * @param cls the intersection operation
645 * @param mh the header of the message
648 handle_p2p_bf_part (void *cls, const struct GNUNET_MessageHeader *mh)
650 struct Operation *op = cls;
651 const struct BFPart *msg = (const struct BFPart *) mh;
653 uint32_t chunk_offset;
655 chunk_size = ntohl(msg->chunk_length);
656 chunk_offset = ntohl(msg->chunk_offset);
658 if ((NULL == op->state->bf_data)
659 || (op->state->bf_data_size < chunk_size + chunk_offset)){
660 // unexpected multipart chunk
662 fail_intersection_operation(op);
666 memcpy (&op->state->bf_data[chunk_offset], (const char*) &msg[1], chunk_size);
668 if (op->state->bf_data_size != chunk_offset + chunk_size)
669 // wait for next chunk
672 op->state->remote_bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
673 op->state->bf_data_size,
674 op->state->bf_bits_per_element);
676 GNUNET_free (op->state->bf_data);
677 op->state->bf_data = NULL;
684 * Handle an BF message from a remote peer.
686 * @param cls the intersection operation
687 * @param mh the header of the message
690 handle_p2p_bf (void *cls, const struct GNUNET_MessageHeader *mh)
692 struct Operation *op = cls;
693 const struct BFMessage *msg = (const struct BFMessage *) mh;
696 uint32_t bf_bits_per_element;
698 switch (op->state->phase)
701 case PHASE_BF_EXCHANGE:
702 case PHASE_MAYBE_FINISHED:
703 if (NULL == op->state->bf_data) {
704 // no colliding multipart transaction going on currently
705 op->spec->salt = ntohl (msg->sender_mutator);
706 bf_size = ntohl (msg->bloomfilter_total_length);
707 bf_bits_per_element = ntohl (msg->bits_per_element);
708 chunk_size = ntohl (msg->bloomfilter_length);
709 op->spec->remote_element_count = ntohl(msg->sender_element_count);
710 if (bf_size == chunk_size) {
711 // single part, done here
712 op->state->remote_bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
714 bf_bits_per_element);
719 //first multipart chunk
720 op->state->bf_data = GNUNET_malloc (bf_size);
721 op->state->bf_data_size = bf_size;
722 op->state->bf_bits_per_element = bf_bits_per_element;
723 memcpy (op->state->bf_data, (const char*) &msg[1], chunk_size);
728 fail_intersection_operation (op);
734 * Handle an BF message from a remote peer.
736 * @param cls the intersection operation
737 * @param mh the header of the message
740 handle_p2p_element_info (void *cls, const struct GNUNET_MessageHeader *mh)
742 struct Operation *op = cls;
743 struct BFMessage *msg = (struct BFMessage *) mh;
745 op->spec->remote_element_count = ntohl(msg->sender_element_count);
746 if ((op->state->phase != PHASE_INITIAL)
747 || (op->state->my_element_count > op->spec->remote_element_count)){
749 fail_intersection_operation(op);
752 if (0 == op->state->my_element_count) {
757 op->state->phase = PHASE_BF_EXCHANGE;
758 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
760 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
761 &iterator_initialization,
764 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
765 op->state->remote_bf = NULL;
767 if (op->state->my_element_count == ntohl (msg->sender_element_count))
768 op->state->phase = PHASE_MAYBE_FINISHED;
770 send_bloomfilter (op);
775 * Send our element count to the peer, in case our element count is lower than his
777 * @param op intersection operation
780 send_element_count (struct Operation *op)
782 struct GNUNET_MQ_Envelope *ev;
783 struct BFMessage *msg;
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element count (bf_msg)\n");
787 // just send our element count, as the other peer must start
788 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
789 msg->sender_element_count = htonl (op->state->my_element_count);
790 msg->bloomfilter_length = htonl (0);
791 msg->sender_mutator = htonl (0);
793 GNUNET_MQ_send (op->mq, ev);
798 * Send a result message to the client indicating
799 * that the operation is over.
800 * After the result done message has been sent to the client,
801 * destroy the evaluate operation.
803 * @param op intersection operation
806 finish_and_destroy (struct Operation *op)
808 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
810 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending full result set\n");
813 op->state->full_result_iter =
814 GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
815 send_remaining_elements (op);
818 send_client_done_and_destroy (op);
823 * Handle a done message from a remote peer
825 * @param cls the union operation
826 * @param mh the message
829 handle_p2p_done (void *cls,
830 const struct GNUNET_MessageHeader *mh)
832 struct Operation *op = cls;
834 if ((op->state->phase = PHASE_FINISHED) || (op->state->phase = PHASE_MAYBE_FINISHED)){
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
837 finish_and_destroy (op);
842 fail_intersection_operation (op);
847 * Evaluate a union operation with
850 * @param op operation to evaluate
853 intersection_evaluate (struct Operation *op)
855 op->state = GNUNET_new (struct OperationState);
856 /* we started the operation, thus we have to send the operation request */
857 op->state->phase = PHASE_INITIAL;
858 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
859 op->state->my_element_count = op->spec->set->state->current_set_element_count;
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "evaluating intersection operation");
863 send_operation_request (op);
868 * Accept an union operation request from a remote peer.
869 * Only initializes the private operation state.
871 * @param op operation that will be accepted as a union operation
874 intersection_accept (struct Operation *op)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
877 op->state = GNUNET_new (struct OperationState);
878 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
879 op->state->my_element_count = op->spec->set->state->current_set_element_count;
881 // if Alice (the peer) has more elements than Bob (us), she should start
882 if (op->spec->remote_element_count < op->state->my_element_count){
883 op->state->phase = PHASE_INITIAL;
884 send_element_count(op);
887 // create a new bloomfilter in case we have fewer elements
888 op->state->phase = PHASE_BF_EXCHANGE;
889 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
891 GNUNET_CONSTANTS_BLOOMFILTER_K);
892 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
893 &iterator_initialization,
895 send_bloomfilter (op);
900 * Create a new set supporting the intersection operation
902 * @return the newly created set
904 static struct SetState *
905 intersection_set_create ()
907 struct SetState *set_state;
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "intersection set created\n");
911 set_state = GNUNET_new (struct SetState);
912 set_state->current_set_element_count = 0;
919 * Add the element from the given element message to the set.
921 * @param set_state state of the set want to add to
922 * @param ee the element to add to the set
925 intersection_add (struct SetState *set_state,
926 struct ElementEntry *ee)
928 set_state->current_set_element_count++;
933 * Destroy a set that supports the intersection operation
935 * @param set_state the set to destroy
938 intersection_set_destroy (struct SetState *set_state)
940 GNUNET_free (set_state);
945 * Remove the element given in the element message from the set.
947 * @param set_state state of the set to remove from
948 * @param element set element to remove
951 intersection_remove (struct SetState *set_state,
952 struct ElementEntry *element)
954 GNUNET_assert(0 < set_state->current_set_element_count);
955 set_state->current_set_element_count--;
960 * Dispatch messages for a intersection operation.
962 * @param op the state of the intersection evaluate operation
963 * @param mh the received message
964 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
965 * #GNUNET_OK otherwise
968 intersection_handle_p2p_message (struct Operation *op,
969 const struct GNUNET_MessageHeader *mh)
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
972 ntohs (mh->type), ntohs (mh->size));
973 switch (ntohs (mh->type))
975 /* this message handler is not active until after we received an
976 * operation request message, thus the ops request is not handled here
978 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO:
979 handle_p2p_element_info (op, mh);
981 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF:
982 handle_p2p_bf (op, mh);
984 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART:
985 handle_p2p_bf_part (op, mh);
987 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
988 handle_p2p_done (op, mh);
991 /* something wrong with cadet's message handlers? */
999 * handler for peer-disconnects, notifies the client about the aborted operation
1001 * @param op the destroyed operation
1004 intersection_peer_disconnect (struct Operation *op)
1006 if (PHASE_FINISHED != op->state->phase)
1008 struct GNUNET_MQ_Envelope *ev;
1009 struct GNUNET_SET_ResultMessage *msg;
1011 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
1012 msg->request_id = htonl (op->spec->client_request_id);
1013 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
1014 msg->element_type = htons (0);
1015 GNUNET_MQ_send (op->spec->set->client_mq, ev);
1016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
1017 _GSS_operation_destroy (op);
1020 // else: the session has already been concluded
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
1022 if (GNUNET_NO == op->state->client_done_sent)
1023 finish_and_destroy (op);
1028 * Destroy the union operation. Only things specific to the union operation are destroyed.
1030 * @param op union operation to destroy
1033 intersection_op_cancel (struct Operation *op)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op\n");
1036 /* check if the op was canceled twice */
1037 GNUNET_assert (NULL != op->state);
1038 if (NULL != op->state->remote_bf)
1040 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1041 op->state->remote_bf = NULL;
1043 if (NULL != op->state->local_bf)
1045 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1046 op->state->local_bf = NULL;
1048 if (NULL != op->state->my_elements)
1050 // no need to free the elements, they are still part of the set
1051 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1052 op->state->my_elements = NULL;
1054 GNUNET_free (op->state);
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op done\n");
1059 const struct SetVT *
1060 _GSS_intersection_vt ()
1062 static const struct SetVT intersection_vt = {
1063 .create = &intersection_set_create,
1064 .msg_handler = &intersection_handle_p2p_message,
1065 .add = &intersection_add,
1066 .remove = &intersection_remove,
1067 .destroy_set = &intersection_set_destroy,
1068 .evaluate = &intersection_evaluate,
1069 .accept = &intersection_accept,
1070 .peer_disconnect = &intersection_peer_disconnect,
1071 .cancel = &intersection_op_cancel,
1074 return &intersection_vt;