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 * Current phase we are in for a intersection operation.
37 enum IntersectionOperationPhase
40 * Alices has suggested an operation to bob,
41 * and is waiting for a bf or session end.
45 * Bob has accepted the operation, Bob and Alice are now exchanging bfs
46 * until one notices the their element count is equal
50 * if both peers have an equal peercount, they enter this state for
51 * one more turn, to see if they actually have agreed on a correct set.
52 * if a peer finds the same element count after the next iteration,
53 * it ends the the session
57 * The protocol is over.
58 * Results may still have to be sent to the client.
65 * State of an evaluate operation
71 * The bf we currently receive
73 struct GNUNET_CONTAINER_BloomFilter *remote_bf;
76 * BF of the set's element.
78 struct GNUNET_CONTAINER_BloomFilter *local_bf;
81 * Current state of the operation.
83 enum IntersectionOperationPhase phase;
86 * Generation in which the operation handle
89 unsigned int generation_created;
92 * Maps element-id-hashes to 'elements in our set'.
94 struct GNUNET_CONTAINER_MultiHashMap *my_elements;
97 * Current element count contained within contained_elements
99 uint32_t my_element_count;
102 * Iterator for sending elements on the key to element mapping to the client.
104 struct GNUNET_CONTAINER_MultiHashMapIterator *full_result_iter;
107 * Evaluate operations are held in
110 struct OperationState *next;
113 * Evaluate operations are held in
116 struct OperationState *prev;
119 * Did we send the client that we are done?
121 int client_done_sent;
126 * Extra state required for efficient set intersection.
131 * Number of currently valid elements in the set which have not been removed
133 uint32_t current_set_element_count;
140 * fills the contained-elements hashmap with all relevant
141 * elements and adds their mutated hashes to our local bloomfilter with mutator+1
144 * @param key current key code
145 * @param value value in the hash map
146 * @return #GNUNET_YES if we should continue to
151 iterator_initialization_by_alice (void *cls,
152 const struct GNUNET_HashCode *key,
154 struct ElementEntry *ee = value;
155 struct Operation *op = cls;
156 struct GNUNET_HashCode mutated_hash;
158 //only consider this element, if it is valid for us
159 if ((op->generation_created >= ee->generation_removed)
160 || (op->generation_created < ee->generation_added))
163 // not contained according to bob's bloomfilter
164 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
165 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
169 op->state->my_element_count++;
170 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
171 &ee->element_hash, ee,
172 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
174 // create our own bloomfilter with salt+1
175 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt+1, &mutated_hash);
176 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
183 * fills the contained-elements hashmap with all relevant
184 * elements and adds their mutated hashes to our local bloomfilter
187 * @param key current key code
188 * @param value value in the hash map
189 * @return #GNUNET_YES if we should continue to
194 iterator_initialization (void *cls,
195 const struct GNUNET_HashCode *key,
197 struct ElementEntry *ee = value;
198 struct Operation *op = cls;
199 struct GNUNET_HashCode mutated_hash;
201 //only consider this element, if it is valid for us
202 if ((op->generation_created >= ee->generation_removed)
203 || (op->generation_created < ee->generation_added))
206 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
207 &ee->element_hash, ee,
208 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
210 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
212 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
220 * removes element from a hashmap if it is not contained within the
221 * provided remote bloomfilter. Then, fill our new bloomfilter.
224 * @param key current key code
225 * @param value value in the hash map
226 * @return #GNUNET_YES if we should continue to
231 iterator_bf_round (void *cls,
232 const struct GNUNET_HashCode *key,
234 struct ElementEntry *ee = value;
235 struct Operation *op = cls;
236 struct GNUNET_HashCode mutated_hash;
238 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt, &mutated_hash);
240 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
242 op->state->my_element_count--;
243 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
249 GNUNET_BLOCK_mingle_hash(&ee->element_hash, op->spec->salt+1, &mutated_hash);
251 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
258 * Inform the client that the union operation has failed,
259 * and proceed to destroy the evaluate operation.
261 * @param op the intersection operation to fail
264 fail_intersection_operation (struct Operation *op)
266 struct GNUNET_MQ_Envelope *ev;
267 struct GNUNET_SET_ResultMessage *msg;
269 if (op->state->my_elements)
270 GNUNET_CONTAINER_multihashmap_destroy(op->state->my_elements);
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "intersection operation failed\n");
274 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
275 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
276 msg->request_id = htonl (op->spec->client_request_id);
277 msg->element_type = htons (0);
278 GNUNET_MQ_send (op->spec->set->client_mq, ev);
279 _GSS_operation_destroy (op);
284 * Send a request for the evaluate operation to a remote peer
286 * @param eo operation with the other peer
289 send_operation_request (struct Operation *op)
291 struct GNUNET_MQ_Envelope *ev;
292 struct OperationRequestMessage *msg;
294 ev = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST,
295 op->spec->context_msg);
299 /* the context message is too large */
301 GNUNET_SERVER_client_disconnect (op->spec->set->client);
304 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
305 msg->app_id = op->spec->app_id;
306 msg->salt = htonl (op->spec->salt);
307 msg->element_count = htonl(op->state->my_element_count);
309 GNUNET_MQ_send (op->mq, ev);
311 if (NULL != op->spec->context_msg)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request with context message\n");
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sent op request without context message\n");
316 if (NULL != op->spec->context_msg)
318 GNUNET_free (op->spec->context_msg);
319 op->spec->context_msg = NULL;
325 * Send a bloomfilter to our peer.
326 * that the operation is over.
327 * After the result done message has been sent to the client,
328 * destroy the evaluate operation.
330 * @param eo intersection operation
333 send_bloomfilter (struct Operation *op)
335 struct GNUNET_MQ_Envelope *ev;
336 struct BFMessage *msg;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending bf of size %u\n");
341 // send our bloomfilter
342 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (op->state->local_bf);
344 ev = GNUNET_MQ_msg_extra (msg, bf_size, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF);
346 msg->sender_element_count = htonl (op->state->my_element_count);
347 msg->bloomfilter_length = htonl (bf_size);
348 msg->sender_mutator = htonl (op->spec->salt);
349 GNUNET_assert (GNUNET_SYSERR !=
350 GNUNET_CONTAINER_bloomfilter_get_raw_data (op->state->local_bf,
353 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
354 op->state->local_bf = NULL;
355 GNUNET_MQ_send (op->mq, ev);
360 * Signal to the client that the operation has finished and
361 * destroy the operation.
363 * @param cls operation to destroy
366 send_client_done_and_destroy (void *cls)
368 struct Operation *op = cls;
369 struct GNUNET_MQ_Envelope *ev;
370 struct GNUNET_SET_ResultMessage *rm;
371 ev = GNUNET_MQ_msg (rm, GNUNET_MESSAGE_TYPE_SET_RESULT);
372 rm->request_id = htonl (op->spec->client_request_id);
373 rm->result_status = htons (GNUNET_SET_STATUS_DONE);
374 rm->element_type = htons (0);
375 GNUNET_MQ_send (op->spec->set->client_mq, ev);
376 _GSS_operation_destroy (op);
381 * Send all elements in the full result iterator.
383 * @param cls operation
386 send_remaining_elements (void *cls)
388 struct Operation *op = cls;
389 struct ElementEntry *remaining; //TODO rework this, key entry does not exist here
390 struct GNUNET_MQ_Envelope *ev;
391 struct GNUNET_SET_ResultMessage *rm;
392 struct GNUNET_SET_Element *element;
395 res = GNUNET_CONTAINER_multihashmap_iterator_next (op->state->full_result_iter, NULL, (const void **) &remaining);
396 if (GNUNET_NO == res) {
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending done and destroy because iterator ran out\n");
398 send_client_done_and_destroy (op);
402 element = &remaining->element;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element (size %u) to client (full set)\n", element->size);
404 GNUNET_assert (0 != op->spec->client_request_id);
406 ev = GNUNET_MQ_msg_extra (rm, element->size, GNUNET_MESSAGE_TYPE_SET_RESULT);
407 GNUNET_assert (NULL != ev);
409 rm->result_status = htons (GNUNET_SET_STATUS_OK);
410 rm->request_id = htonl (op->spec->client_request_id);
411 rm->element_type = element->type;
412 memcpy (&rm[1], element->data, element->size);
414 GNUNET_MQ_notify_sent (ev, send_remaining_elements, op);
415 GNUNET_MQ_send (op->spec->set->client_mq, ev);
420 * Inform the peer that this operation is complete.
422 * @param eo the intersection operation to fail
425 send_peer_done (struct Operation *op)
427 struct GNUNET_MQ_Envelope *ev;
429 op->state->phase = PHASE_FINISHED;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Intersection succeeded, sending DONE\n");
431 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
432 op->state->local_bf = NULL;
434 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_P2P_DONE);
435 GNUNET_MQ_send (op->mq, ev);
440 * Handle an BF message from a remote peer.
442 * @param cls the intersection operation
443 * @param mh the header of the message
446 handle_p2p_bf (void *cls, const struct GNUNET_MessageHeader *mh)
448 struct Operation *op = cls;
449 const struct BFMessage *msg = (const struct BFMessage *) mh;
450 uint32_t old_elements;
451 uint32_t peer_elements;
453 old_elements = op->state->my_element_count;
454 op->spec->salt = ntohl (msg->sender_mutator);
456 op->state->remote_bf = GNUNET_CONTAINER_bloomfilter_init ((const char*) &msg[1],
458 ntohl (msg->bloomfilter_length));
459 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
461 GNUNET_CONSTANTS_BLOOMFILTER_K);
462 switch (op->state->phase)
465 // If we are ot our first msg
466 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (op->state->my_element_count, GNUNET_YES);
468 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
469 &iterator_initialization_by_alice,
472 case PHASE_BF_EXCHANGE:
473 case PHASE_MAYBE_FINISHED:
474 // if we are bob or alice and are continuing operation
475 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
481 fail_intersection_operation(op);
483 // the iterators created a new BF with salt+1
484 // the peer needs this information for decoding the next BF
485 // this behavior can be modified at will later on.
488 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
489 op->state->remote_bf = NULL;
491 peer_elements = ntohl(msg->sender_element_count);
492 if ((op->state->phase == PHASE_MAYBE_FINISHED)
493 && (old_elements == op->state->my_element_count)
494 && (op->state->my_element_count == peer_elements)){
495 // In the last round we though we were finished, we now know this is correct
500 op->state->phase = PHASE_BF_EXCHANGE;
501 // maybe we are finished, but we do one more round to make certain
502 // we don't have false positives ...
503 if (op->state->my_element_count == peer_elements)
504 op->state->phase = PHASE_MAYBE_FINISHED;
506 send_bloomfilter (op);
511 * Handle an BF message from a remote peer.
513 * @param cls the intersection operation
514 * @param mh the header of the message
517 handle_p2p_element_info (void *cls, const struct GNUNET_MessageHeader *mh)
519 struct Operation *op = cls;
520 struct BFMessage *msg = (struct BFMessage *) mh;
522 op->spec->remote_element_count = ntohl(msg->sender_element_count);
523 if ((op->state->phase != PHASE_INITIAL)
524 || (op->state->my_element_count > op->spec->remote_element_count)){
526 fail_intersection_operation(op);
529 op->state->phase = PHASE_BF_EXCHANGE;
530 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
532 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
534 GNUNET_CONSTANTS_BLOOMFILTER_K);
535 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
536 &iterator_initialization,
539 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
540 op->state->remote_bf = NULL;
542 if (op->state->my_element_count == ntohl (msg->sender_element_count))
543 op->state->phase = PHASE_MAYBE_FINISHED;
545 send_bloomfilter (op);
550 * Send our element to the peer, in case our element count is lower than his
552 * @param eo intersection operation
555 send_element_count (struct Operation *op)
557 struct GNUNET_MQ_Envelope *ev;
558 struct BFMessage *msg;
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending element count (bf_msg)\n");
562 // just send our element count, as the other peer must start
563 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO);
565 msg->sender_element_count = htonl (op->state->my_element_count);
566 msg->bloomfilter_length = htonl (0);
567 msg->sender_mutator = htonl (0);
569 GNUNET_MQ_send (op->mq, ev);
573 * Send a result message to the client indicating
574 * that the operation is over.
575 * After the result done message has been sent to the client,
576 * destroy the evaluate operation.
578 * @param op intersection operation
581 finish_and_destroy (struct Operation *op)
583 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
585 if (GNUNET_SET_RESULT_FULL == op->spec->result_mode)
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending full result set\n");
588 op->state->full_result_iter =
589 GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
590 send_remaining_elements (op);
593 send_client_done_and_destroy (op);
597 * Handle a done message from a remote peer
599 * @param cls the union operation
600 * @param mh the message
603 handle_p2p_done (void *cls, const struct GNUNET_MessageHeader *mh)
605 struct Operation *op = cls;
607 if ((op->state->phase = PHASE_FINISHED) || (op->state->phase = PHASE_MAYBE_FINISHED)){
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got final DONE\n");
610 finish_and_destroy (op);
615 fail_intersection_operation (op);
620 * Evaluate a union operation with
623 * @param op operation to evaluate
626 intersection_evaluate (struct Operation *op)
628 op->state = GNUNET_new (struct OperationState);
629 /* we started the operation, thus we have to send the operation request */
630 op->state->phase = PHASE_INITIAL;
631 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
632 op->state->my_element_count = op->spec->set->state->current_set_element_count;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "evaluating intersection operation");
635 send_operation_request (op);
639 * Accept an union operation request from a remote peer.
640 * Only initializes the private operation state.
642 * @param op operation that will be accepted as a union operation
645 intersection_accept (struct Operation *op)
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "accepting set union operation\n");
648 op->state = GNUNET_new (struct OperationState);
649 op->state->my_elements = GNUNET_CONTAINER_multihashmap_create(1, GNUNET_YES);
650 op->state->my_element_count = op->spec->set->state->current_set_element_count;
652 // if Alice (the peer) has more elements than Bob (us), she should start
653 if (op->spec->remote_element_count < op->state->my_element_count){
654 op->state->phase = PHASE_INITIAL;
655 send_element_count(op);
658 // create a new bloomfilter in case we have fewer elements
659 op->state->phase = PHASE_BF_EXCHANGE;
660 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
662 GNUNET_CONSTANTS_BLOOMFILTER_K);
663 GNUNET_CONTAINER_multihashmap_iterate (op->spec->set->elements,
664 &iterator_initialization,
666 send_bloomfilter (op);
671 * Create a new set supporting the intersection operation
673 * @return the newly created set
675 static struct SetState *
676 intersection_set_create (void)
678 struct SetState *set_state;
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "intersection set created\n");
682 set_state = GNUNET_new (struct SetState);
683 set_state->current_set_element_count = 0;
690 * Add the element from the given element message to the set.
692 * @param set_state state of the set want to add to
693 * @param ee the element to add to the set
696 intersection_add (struct SetState *set_state, struct ElementEntry *ee)
698 GNUNET_assert(0 < set_state->current_set_element_count);
699 set_state->current_set_element_count++;
704 * Destroy a set that supports the intersection operation
706 * @param set_state the set to destroy
709 intersection_set_destroy (struct SetState *set_state)
711 GNUNET_free (set_state);
716 * Remove the element given in the element message from the set.
718 * @param set_state state of the set to remove from
719 * @param element set element to remove
722 intersection_remove (struct SetState *set_state, struct ElementEntry *element)
724 GNUNET_assert(0 < set_state->current_set_element_count);
725 set_state->current_set_element_count--;
731 * Dispatch messages for a intersection operation.
733 * @param eo the state of the intersection evaluate operation
734 * @param mh the received message
735 * @return GNUNET_SYSERR if the tunnel should be disconnected,
736 * GNUNET_OK otherwise
739 intersection_handle_p2p_message (struct Operation *op,
740 const struct GNUNET_MessageHeader *mh)
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "received p2p message (t: %u, s: %u)\n",
743 ntohs (mh->type), ntohs (mh->size));
744 switch (ntohs (mh->type))
746 /* this message handler is not active until after we received an
747 * operation request message, thus the ops request is not handled here
749 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO:
750 handle_p2p_element_info (op, mh);
752 case GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF:
753 handle_p2p_bf (op, mh);
755 case GNUNET_MESSAGE_TYPE_SET_P2P_DONE:
756 handle_p2p_done (op, mh);
759 /* something wrong with mesh's message handlers? */
767 * handler for peer-disconnects, notifies the client about the aborted operation
769 * @param op the destroyed operation
772 intersection_peer_disconnect (struct Operation *op)
774 if (PHASE_FINISHED != op->state->phase)
776 struct GNUNET_MQ_Envelope *ev;
777 struct GNUNET_SET_ResultMessage *msg;
779 ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_RESULT);
780 msg->request_id = htonl (op->spec->client_request_id);
781 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
782 msg->element_type = htons (0);
783 GNUNET_MQ_send (op->spec->set->client_mq, ev);
784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "other peer disconnected prematurely\n");
785 _GSS_operation_destroy (op);
788 // else: the session has already been concluded
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "other peer disconnected (finished)\n");
790 if (GNUNET_NO == op->state->client_done_sent)
791 finish_and_destroy (op);
796 * Destroy the union operation. Only things specific to the union operation are destroyed.
798 * @param op union operation to destroy
801 intersection_op_cancel (struct Operation *op)
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op\n");
804 /* check if the op was canceled twice */
805 GNUNET_assert (NULL != op->state);
806 if (NULL != op->state->remote_bf)
808 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
809 op->state->remote_bf = NULL;
811 if (NULL != op->state->local_bf)
813 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
814 op->state->local_bf = NULL;
816 if (NULL != op->state->my_elements)
818 // no need to free the elements, they are still part of the set
819 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
820 op->state->my_elements = NULL;
822 GNUNET_free (op->state);
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying intersection op done\n");
828 _GSS_intersection_vt ()
830 static const struct SetVT intersection_vt = {
831 .create = &intersection_set_create,
832 .msg_handler = &intersection_handle_p2p_message,
833 .add = &intersection_add,
834 .remove = &intersection_remove,
835 .destroy_set = &intersection_set_destroy,
836 .evaluate = &intersection_evaluate,
837 .accept = &intersection_accept,
838 .peer_disconnect = &intersection_peer_disconnect,
839 .cancel = &intersection_op_cancel,
842 return &intersection_vt;