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 s = ceil((double) (A * k / log(2))); \
42 * Current phase we are in for a intersection operation.
44 enum IntersectionOperationPhase
47 * Alices has suggested an operation to bob,
48 * and is waiting for a bf or session end.
52 * Bob has accepted the operation, Bob and Alice are now exchanging bfs
53 * until one notices the their element count is equal
57 * if both peers have an equal peercount, they enter this state for
58 * one more turn, to see if they actually have agreed on a correct set.
59 * if a peer finds the same element count after the next iteration,
60 * it ends the the session
64 * The protocol is over.
65 * Results may still have to be sent to the client.
72 * State of an evaluate operation
78 * The bf we currently receive
80 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
83 * BF of the set's element.
85 struct GNUNET_CONTAINER_BloomFilter *local_bf;
88 * for multipart msgs we have to store the bloomfilter-data until we fully sent it.
93 * size of the bloomfilter
95 uint32_t bf_data_size;
98 * size of the bloomfilter
100 uint32_t bf_bits_per_element;
103 * Current state of the operation.
105 enum IntersectionOperationPhase phase;
108 * Generation in which the operation handle
111 unsigned int generation_created;
114 * Maps element-id-hashes to 'elements in our set'.
116 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
119 * Current element count contained within contained_elements
121 uint32_t my_element_count;
124 * Iterator for sending elements on the key to element mapping to the client.
126 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
129 * Evaluate operations are held in
132 struct OperationState *next;
135 * Evaluate operations are held in
138 struct OperationState *prev;
141 * Did we send the client that we are done?
143 int client_done_sent;
148 * Extra state required for efficient set intersection.
153 * Number of currently valid elements in the set which have not been removed
155 uint32_t current_set_element_count;
162 * fills the contained-elements hashmap with all relevant
163 * elements and adds their mutated hashes to our local bloomfilter with mutator+1
166 * @param key current key code
167 * @param value value in the hash map
168 * @return #GNUNET_YES if we should continue to
173 iterator_initialization_by_alice (void *cls,
174 const struct GNUNET_HashCode *key,
177 struct ElementEntry *ee = value;
178 struct Operation *op = cls;
179 struct GNUNET_HashCode mutated_hash;
181 //only consider this element, if it is valid for us
182 if ((op->generation_created >= ee->generation_removed)
183 || (op->generation_created < ee->generation_added))
186 // not contained according to bob's bloomfilter
187 GNUNET_BLOCK_mingle_hash(&ee->element_hash,
190 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
194 op->state->my_element_count++;
195 GNUNET_assert (GNUNET_YES ==
196 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
197 &ee->element_hash, ee,
198 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
204 * fills the contained-elements hashmap with all relevant
205 * elements and adds their mutated hashes to our local bloomfilter
208 * @param key current key code
209 * @param value value in the hash map
210 * @return #GNUNET_YES if we should continue to
215 iterator_initialization (void *cls,
216 const struct GNUNET_HashCode *key,
219 struct ElementEntry *ee = value;
220 struct Operation *op = cls;
222 //only consider this element, if it is valid for us
223 if ((op->generation_created >= ee->generation_removed)
224 || (op->generation_created < ee->generation_added))
227 GNUNET_assert (GNUNET_YES ==
228 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
229 &ee->element_hash, ee,
230 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
236 * removes element from a hashmap if it is not contained within the
237 * provided remote bloomfilter. Then, fill our new bloomfilter.
240 * @param key current key code
241 * @param value value in the hash map
242 * @return #GNUNET_YES if we should continue to
247 iterator_bf_reduce (void *cls,
248 const struct GNUNET_HashCode *key,
251 struct ElementEntry *ee = value;
252 struct Operation *op = cls;
253 struct GNUNET_HashCode mutated_hash;
255 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
258 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
261 op->state->my_element_count--;
262 GNUNET_assert (GNUNET_YES ==
263 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
272 * create a bloomfilter based on the elements given
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_create (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);
292 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
298 * Inform the client that the union operation has failed,
299 * and proceed to destroy the evaluate operation.
301 * @param op the intersection operation to fail
304 fail_intersection_operation (struct Operation *op)
306 struct GNUNET_MQ_Envelope *ev;
307 struct GNUNET_SET_ResultMessage *msg;
309 if (op->state->my_elements)
310 GNUNET_CONTAINER_multihashmap_destroy(op->state->my_elements);
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "intersection operation failed\n");
314 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
315 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
316 msg->request_id = htonl (op->spec->client_request_id);
317 msg->element_type = htons (0);
318 GNUNET_MQ_send (op->spec->set->client_mq, ev);
319 _GSS_operation_destroy (op);
324 * Send a request for the evaluate operation to a remote peer
326 * @param op operation with the other peer
329 send_operation_request (struct Operation *op)
331 struct GNUNET_MQ_Envelope *ev;
332 struct OperationRequestMessage *msg;
334 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
335 op->spec->context_msg);
339 /* the context message is too large */
341 GNUNET_SERVER_client_disconnect (op->spec->set->client);
344 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
345 msg->app_id = op->spec->app_id;
346 msg->salt = htonl (op->spec->salt);
347 msg->element_count = htonl(op->state->my_element_count);
349 GNUNET_MQ_send (op->mq, ev);
351 if (NULL != op->spec->context_msg)
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
356 if (NULL != op->spec->context_msg)
358 GNUNET_free (op->spec->context_msg);
359 op->spec->context_msg = NULL;
364 send_bloomfilter_multipart (struct Operation *op, uint32_t offset)
366 struct GNUNET_MQ_Envelope *ev;
368 uint32_t chunk_size = (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof(struct BFPart));
369 uint32_t todo_size = op->state->bf_data_size - offset;
371 if (todo_size < chunk_size)
372 chunk_size = todo_size;
374 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART);
376 msg->chunk_length = htonl (chunk_size);
377 msg->chunk_offset = htonl (offset);
378 memcpy(&msg[1], &op->state->bf_data[offset], chunk_size);
380 GNUNET_MQ_send (op->mq, ev);
382 if (op->state->bf_data_size == offset + chunk_size)
385 GNUNET_free(op->state->bf_data);
386 op->state->bf_data = NULL;
390 send_bloomfilter_multipart (op, offset + chunk_size);
394 * Send a bloomfilter to our peer.
395 * that the operation is over.
396 * After the result done message has been sent to the client,
397 * destroy the evaluate operation.
399 * @param op intersection operation
402 send_bloomfilter (struct Operation *op)
404 struct GNUNET_MQ_Envelope *ev;
405 struct BFMessage *msg;
407 uint32_t bf_elementbits;
409 struct GNUNET_CONTAINER_BloomFilter * local_bf;
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending bf of size %u\n");
413 CALCULATE_BF_SIZE(op->state->my_element_count,
414 op->spec->remote_element_count,
418 local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
423 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
427 // send our bloomfilter
428 if (GNUNET_SERVER_MAX_MESSAGE_SIZE > bf_size + sizeof (struct BFMessage)) {
430 chunk_size = bf_size;
431 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
432 GNUNET_assert (GNUNET_SYSERR !=
433 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
439 chunk_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1 - sizeof (struct BFMessage);
440 ev = GNUNET_MQ_msg_extra (msg, chunk_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
441 op->state->bf_data = (char *) GNUNET_malloc (bf_size);
442 GNUNET_assert (GNUNET_SYSERR !=
443 GNUNET_CONTAINER_bloomfilter_get_raw_data (local_bf,
446 memcpy (&msg[1], op->state->bf_data, chunk_size);
447 op->state->bf_data_size = bf_size;
449 GNUNET_CONTAINER_bloomfilter_free (local_bf);
451 msg->sender_element_count = htonl (op->state->my_element_count);
452 msg->bloomfilter_total_length = htonl (bf_size);
453 msg->bloomfilter_length = htonl (chunk_size);
454 msg->bits_per_element = htonl (bf_elementbits);
455 msg->sender_mutator = htonl (op->spec->salt);
457 GNUNET_MQ_send (op->mq, ev);
459 if (op->state->bf_data)
460 send_bloomfilter_multipart (op, chunk_size);
465 * Signal to the client that the operation has finished and
466 * destroy the operation.
468 * @param cls operation to destroy
471 send_client_done_and_destroy (void *cls)
473 struct Operation *op = cls;
474 struct GNUNET_MQ_Envelope *ev;
475 struct GNUNET_SET_ResultMessage *rm;
476 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
477 rm->request_id = htonl (op->spec->client_request_id);
478 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
479 rm->element_type = htons (0);
480 GNUNET_MQ_send (op->spec->set->client_mq, ev);
481 _GSS_operation_destroy (op);
486 * Send all elements in the full result iterator.
488 * @param cls operation
491 send_remaining_elements (void *cls)
493 struct Operation *op = cls;
494 struct ElementEntry *remaining; //TODO rework this, key entry does not exist here
495 struct GNUNET_MQ_Envelope *ev;
496 struct GNUNET_SET_ResultMessage *rm;
497 struct GNUNET_SET_Element *element;
500 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter, NULL, (const void **) &remaining);
501 if (GNUNET_NO == res) {
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending done and destroy because iterator ran out\n");
503 send_client_done_and_destroy (op);
507 element = &remaining->element;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client (full set)\n", element->size);
509 GNUNET_assert (0 != op->spec->client_request_id);
511 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
512 GNUNET_assert (NULL != ev);
514 rm->result_status = htons (GNUNET_SET_STATUS_OK);
515 rm->request_id = htonl (op->spec->client_request_id);
516 rm->element_type = element->type;
517 memcpy (&rm[1], element->data, element->size);
519 GNUNET_MQ_notify_sent (ev, send_remaining_elements, op);
520 GNUNET_MQ_send (op->spec->set->client_mq, ev);
525 * Inform the peer that this operation is complete.
527 * @param op the intersection operation to fail
530 send_peer_done (struct Operation *op)
532 struct GNUNET_MQ_Envelope *ev;
534 op->state->phase = PHASE_FINISHED;
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Intersection succeeded, sending DONE\n");
536 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
537 op->state->local_bf = NULL;
539 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
540 GNUNET_MQ_send (op->mq, ev);
545 * Process a Bloomfilter once we got all the chunks
547 * @param op the intersection operation
550 process_bf (struct Operation *op){
551 uint32_t old_elements;
552 uint32_t peer_elements;
554 old_elements = op->state->my_element_count;
555 peer_elements = op->spec->remote_element_count;
556 switch (op->state->phase)
559 // If we are ot our first msg
560 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (op->state->my_element_count, GNUNET_YES);
562 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
563 &iterator_initialization_by_alice,
566 case PHASE_BF_EXCHANGE:
567 case PHASE_MAYBE_FINISHED:
568 // if we are bob or alice and are continuing operation
569 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
575 fail_intersection_operation(op);
577 // the iterators created a new BF with salt+1
578 // the peer needs this information for decoding the next BF
579 // this behavior can be modified at will later on.
582 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
583 op->state->remote_bf = NULL;
585 if ((0 == op->state->my_element_count) // fully disjoint
586 || ((op->state->phase == PHASE_MAYBE_FINISHED) // we agree on a shared set of elements
587 && (old_elements == op->state->my_element_count)
588 && (op->state->my_element_count == peer_elements))) {
589 // In the last round we though we were finished, we now know this is correct
594 op->state->phase = PHASE_BF_EXCHANGE;
595 if (op->state->my_element_count == peer_elements)
596 // maybe we are finished, but we do one more round to make certain
597 // we don't have false positives ...
598 op->state->phase = PHASE_MAYBE_FINISHED;
600 send_bloomfilter (op);
605 * Handle an BF multipart message from a remote peer.
607 * @param cls the intersection operation
608 * @param mh the header of the message
611 handle_p2p_bf_part (void *cls, const struct GNUNET_MessageHeader *mh)
613 struct Operation *op = cls;
614 const struct BFPart *msg = (const struct BFPart *) mh;
616 uint32_t chunk_offset;
618 chunk_size = ntohl(msg->chunk_length);
619 chunk_offset = ntohl(msg->chunk_offset);
621 if ((NULL == op->state->bf_data)
622 || (op->state->bf_data_size < chunk_size + chunk_offset)){
623 // unexpected multipart chunk
625 fail_intersection_operation(op);
629 memcpy (&op->state->bf_data[chunk_offset], (const char*) &msg[1], chunk_size);
631 if (op->state->bf_data_size != chunk_offset + chunk_size)
632 // wait for next chunk
635 op->state->remote_bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
636 op->state->bf_data_size,
637 op->state->bf_bits_per_element);
639 GNUNET_free (op->state->bf_data);
640 op->state->bf_data = NULL;
647 * Handle an BF message from a remote peer.
649 * @param cls the intersection operation
650 * @param mh the header of the message
653 handle_p2p_bf (void *cls, const struct GNUNET_MessageHeader *mh)
655 struct Operation *op = cls;
656 const struct BFMessage *msg = (const struct BFMessage *) mh;
659 uint32_t bf_bits_per_element;
661 switch (op->state->phase)
664 case PHASE_BF_EXCHANGE:
665 case PHASE_MAYBE_FINISHED:
666 if (NULL == op->state->bf_data) {
667 // no colliding multipart transaction going on currently
668 op->spec->salt = ntohl (msg->sender_mutator);
669 bf_size = ntohl (msg->bloomfilter_total_length);
670 bf_bits_per_element = ntohl (msg->bits_per_element);
671 chunk_size = ntohl (msg->bloomfilter_length);
672 op->spec->remote_element_count = ntohl(msg->sender_element_count);
673 if (bf_size == chunk_size) {
674 // single part, done here
675 op->state->remote_bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
677 bf_bits_per_element);
682 //first multipart chunk
683 op->state->bf_data = GNUNET_malloc (bf_size);
684 op->state->bf_data_size = bf_size;
685 op->state->bf_bits_per_element = bf_bits_per_element;
686 memcpy (op->state->bf_data, (const char*) &msg[1], chunk_size);
691 fail_intersection_operation (op);
697 * Handle an BF message from a remote peer.
699 * @param cls the intersection operation
700 * @param mh the header of the message
703 handle_p2p_element_info (void *cls, const struct GNUNET_MessageHeader *mh)
705 struct Operation *op = cls;
706 struct BFMessage *msg = (struct BFMessage *) mh;
708 op->spec->remote_element_count = ntohl(msg->sender_element_count);
709 if ((op->state->phase != PHASE_INITIAL)
710 || (op->state->my_element_count > op->spec->remote_element_count)){
712 fail_intersection_operation(op);
715 op->state->phase = PHASE_BF_EXCHANGE;
716 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
718 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
719 &iterator_initialization,
722 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
723 op->state->remote_bf = NULL;
725 if (op->state->my_element_count == ntohl (msg->sender_element_count))
726 op->state->phase = PHASE_MAYBE_FINISHED;
728 send_bloomfilter (op);
733 * Send our element to the peer, in case our element count is lower than his
735 * @param op intersection operation
738 send_element_count (struct Operation *op)
740 struct GNUNET_MQ_Envelope *ev;
741 struct BFMessage *msg;
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element count (bf_msg)\n");
745 // just send our element count, as the other peer must start
746 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
747 msg->sender_element_count = htonl (op->state->my_element_count);
748 msg->bloomfilter_length = htonl (0);
749 msg->sender_mutator = htonl (0);
751 GNUNET_MQ_send (op->mq, ev);
756 * Send a result message to the client indicating
757 * that the operation is over.
758 * After the result done message has been sent to the client,
759 * destroy the evaluate operation.
761 * @param op intersection operation
764 finish_and_destroy (struct Operation *op)
766 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
768 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending full result set\n");
771 op->state->full_result_iter =
772 GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
773 send_remaining_elements (op);
776 send_client_done_and_destroy (op);
781 * Handle a done message from a remote peer
783 * @param cls the union operation
784 * @param mh the message
787 handle_p2p_done (void *cls,
788 const struct GNUNET_MessageHeader *mh)
790 struct Operation *op = cls;
792 if ((op->state->phase = PHASE_FINISHED) || (op->state->phase = PHASE_MAYBE_FINISHED)){
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
795 finish_and_destroy (op);
800 fail_intersection_operation (op);
805 * Evaluate a union operation with
808 * @param op operation to evaluate
811 intersection_evaluate (struct Operation *op)
813 op->state = GNUNET_new (struct OperationState);
814 /* we started the operation, thus we have to send the operation request */
815 op->state->phase = PHASE_INITIAL;
816 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
817 op->state->my_element_count = op->spec->set->state->current_set_element_count;
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "evaluating intersection operation");
821 send_operation_request (op);
826 * Accept an union operation request from a remote peer.
827 * Only initializes the private operation state.
829 * @param op operation that will be accepted as a union operation
832 intersection_accept (struct Operation *op)
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
835 op->state = GNUNET_new (struct OperationState);
836 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
837 op->state->my_element_count = op->spec->set->state->current_set_element_count;
839 // if Alice (the peer) has more elements than Bob (us), she should start
840 if (op->spec->remote_element_count < op->state->my_element_count){
841 op->state->phase = PHASE_INITIAL;
842 send_element_count(op);
845 // create a new bloomfilter in case we have fewer elements
846 op->state->phase = PHASE_BF_EXCHANGE;
847 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
849 GNUNET_CONSTANTS_BLOOMFILTER_K);
850 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
851 &iterator_initialization,
853 send_bloomfilter (op);
858 * Create a new set supporting the intersection operation
860 * @return the newly created set
862 static struct SetState *
863 intersection_set_create ()
865 struct SetState *set_state;
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "intersection set created\n");
869 set_state = GNUNET_new (struct SetState);
870 set_state->current_set_element_count = 0;
877 * Add the element from the given element message to the set.
879 * @param set_state state of the set want to add to
880 * @param ee the element to add to the set
883 intersection_add (struct SetState *set_state,
884 struct ElementEntry *ee)
886 GNUNET_assert(0 < set_state->current_set_element_count);
887 set_state->current_set_element_count++;
892 * Destroy a set that supports the intersection operation
894 * @param set_state the set to destroy
897 intersection_set_destroy (struct SetState *set_state)
899 GNUNET_free (set_state);
904 * Remove the element given in the element message from the set.
906 * @param set_state state of the set to remove from
907 * @param element set element to remove
910 intersection_remove (struct SetState *set_state,
911 struct ElementEntry *element)
913 GNUNET_assert(0 < set_state->current_set_element_count);
914 set_state->current_set_element_count--;
919 * Dispatch messages for a intersection operation.
921 * @param op the state of the intersection evaluate operation
922 * @param mh the received message
923 * @return #GNUNET_SYSERR if the tunnel should be disconnected,
924 * #GNUNET_OK otherwise
927 intersection_handle_p2p_message (struct Operation *op,
928 const struct GNUNET_MessageHeader *mh)
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
931 ntohs (mh->type), ntohs (mh->size));
932 switch (ntohs (mh->type))
934 /* this message handler is not active until after we received an
935 * operation request message, thus the ops request is not handled here
937 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO:
938 handle_p2p_element_info (op, mh);
940 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF:
941 handle_p2p_bf (op, mh);
943 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF_PART:
944 handle_p2p_bf_part (op, mh);
946 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
947 handle_p2p_done (op, mh);
950 /* something wrong with mesh's message handlers? */
958 * handler for peer-disconnects, notifies the client about the aborted operation
960 * @param op the destroyed operation
963 intersection_peer_disconnect (struct Operation *op)
965 if (PHASE_FINISHED != op->state->phase)
967 struct GNUNET_MQ_Envelope *ev;
968 struct GNUNET_SET_ResultMessage *msg;
970 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
971 msg->request_id = htonl (op->spec->client_request_id);
972 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
973 msg->element_type = htons (0);
974 GNUNET_MQ_send (op->spec->set->client_mq, ev);
975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
976 _GSS_operation_destroy (op);
979 // else: the session has already been concluded
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
981 if (GNUNET_NO == op->state->client_done_sent)
982 finish_and_destroy (op);
987 * Destroy the union operation. Only things specific to the union operation are destroyed.
989 * @param op union operation to destroy
992 intersection_op_cancel (struct Operation *op)
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op\n");
995 /* check if the op was canceled twice */
996 GNUNET_assert (NULL != op->state);
997 if (NULL != op->state->remote_bf)
999 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1000 op->state->remote_bf = NULL;
1002 if (NULL != op->state->local_bf)
1004 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1005 op->state->local_bf = NULL;
1007 if (NULL != op->state->my_elements)
1009 // no need to free the elements, they are still part of the set
1010 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1011 op->state->my_elements = NULL;
1013 GNUNET_free (op->state);
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op done\n");
1018 const struct SetVT *
1019 _GSS_intersection_vt ()
1021 static const struct SetVT intersection_vt = {
1022 .create = &intersection_set_create,
1023 .msg_handler = &intersection_handle_p2p_message,
1024 .add = &intersection_add,
1025 .remove = &intersection_remove,
1026 .destroy_set = &intersection_set_destroy,
1027 .evaluate = &intersection_evaluate,
1028 .accept = &intersection_accept,
1029 .peer_disconnect = &intersection_peer_disconnect,
1030 .cancel = &intersection_op_cancel,
1033 return &intersection_vt;