GNUnet 0.28.0-dev.3-20-gf1136b0b8
 
Loading...
Searching...
No Matches
gnunet-service-set_intersection.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2013-2017 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero 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.
9
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.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
29#include "gnunet-service-set.h"
30#include "gnunet_block_lib.h"
33#include <gcrypt.h>
34
35
77
78
185
186
192{
198};
199
200
208static void
210 struct GNUNET_SET_Element *element)
211{
212 struct GNUNET_MQ_Envelope *ev;
213 struct GNUNET_SET_ResultMessage *rm;
214
215 if (GNUNET_SET_RESULT_REMOVED != op->result_mode)
216 return; /* Wrong mode for transmitting removed elements */
218 "Sending removed element (size %u) to client\n",
219 element->size);
221 "# Element removed messages sent",
222 1,
223 GNUNET_NO);
224 GNUNET_assert (0 != op->client_request_id);
225 ev = GNUNET_MQ_msg_extra (rm,
226 element->size,
228 if (NULL == ev)
229 {
230 GNUNET_break (0);
231 return;
232 }
234 rm->request_id = htonl (op->client_request_id);
235 rm->element_type = element->element_type;
236 GNUNET_memcpy (&rm[1],
237 element->data,
238 element->size);
239 GNUNET_MQ_send (op->set->cs->mq,
240 ev);
241}
242
243
252static int
254 const struct GNUNET_HashCode *key,
255 void *value)
256{
257 struct Operation *op = cls;
258 struct ElementEntry *ee = value;
259 struct GNUNET_HashCode mutated_hash;
260
261
263 "FIMA called for %s:%u\n",
265 ee->element.size);
266
268 {
270 "Reduced initialization, not starting with %s:%u (wrong generation)\n",
272 ee->element.size);
273 return GNUNET_YES; /* element not valid in our operation's generation */
274 }
275
276 /* Test if element is in other peer's bloomfilter */
278 op->state->salt,
279 &mutated_hash);
281 "Testing mingled hash %s with salt %u\n",
282 GNUNET_h2s (&mutated_hash),
283 op->state->salt);
284 if (GNUNET_NO ==
285 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
286 &mutated_hash))
287 {
288 /* remove this element */
290 &ee->element);
292 "Reduced initialization, not starting with %s:%u\n",
294 ee->element.size);
295 return GNUNET_YES;
296 }
297 op->state->my_element_count++;
298 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
299 &ee->element_hash,
300 &op->state->my_xor);
302 "Filtered initialization of my_elements, adding %s:%u\n",
304 ee->element.size);
306 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
307 &ee->element_hash,
308 ee,
310
311 return GNUNET_YES;
312}
313
314
324static int
326 const struct GNUNET_HashCode *key,
327 void *value)
328{
329 struct Operation *op = cls;
330 struct ElementEntry *ee = value;
331 struct GNUNET_HashCode mutated_hash;
332
334 op->state->salt,
335 &mutated_hash);
337 "Testing mingled hash %s with salt %u\n",
338 GNUNET_h2s (&mutated_hash),
339 op->state->salt);
340 if (GNUNET_NO ==
341 GNUNET_CONTAINER_bloomfilter_test (op->state->remote_bf,
342 &mutated_hash))
343 {
344 GNUNET_break (0 < op->state->my_element_count);
345 op->state->my_element_count--;
346 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
347 &ee->element_hash,
348 &op->state->my_xor);
350 "Bloom filter reduction of my_elements, removing %s:%u\n",
352 ee->element.size);
354 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
355 &ee->element_hash,
356 ee));
358 &ee->element);
359 }
360 else
361 {
363 "Bloom filter reduction of my_elements, keeping %s:%u\n",
365 ee->element.size);
366 }
367 return GNUNET_YES;
368}
369
370
379static int
381 const struct GNUNET_HashCode *key,
382 void *value)
383{
384 struct Operation *op = cls;
385 struct ElementEntry *ee = value;
386 struct GNUNET_HashCode mutated_hash;
387
389 op->state->salt,
390 &mutated_hash);
392 "Initializing BF with hash %s with salt %u\n",
393 GNUNET_h2s (&mutated_hash),
394 op->state->salt);
395 GNUNET_CONTAINER_bloomfilter_add (op->state->local_bf,
396 &mutated_hash);
397 return GNUNET_YES;
398}
399
400
407static void
409{
410 struct GNUNET_MQ_Envelope *ev;
412
414 "Intersection operation failed\n");
416 "# Intersection operations failed",
417 1,
418 GNUNET_NO);
419 if (NULL != op->state->my_elements)
420 {
421 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
422 op->state->my_elements = NULL;
423 }
424 ev = GNUNET_MQ_msg (msg,
426 msg->result_status = htons (GNUNET_SET_STATUS_FAILURE);
427 msg->request_id = htonl (op->client_request_id);
428 msg->element_type = htons (0);
429 GNUNET_MQ_send (op->set->cs->mq,
430 ev);
432 GNUNET_YES);
433}
434
435
442static void
444{
445 struct GNUNET_MQ_Envelope *ev;
446 struct BFMessage *msg;
447 uint32_t bf_size;
448 uint32_t bf_elementbits;
449 uint32_t chunk_size;
450 char *bf_data;
451 uint32_t offset;
452
453 /* We consider the ratio of the set sizes to determine
454 the number of bits per element, as the smaller set
455 should use more bits to maximize its set reduction
456 potential and minimize overall bandwidth consumption. */
457 bf_elementbits = 2 + ceil (log2 ((double)
458 (op->remote_element_count
459 / (double) op->state->my_element_count)));
460 if (bf_elementbits < 1)
461 bf_elementbits = 1; /* make sure k is not 0 */
462 /* optimize BF-size to ~50% of bits set */
463 bf_size = ceil ((double) (op->state->my_element_count
464 * bf_elementbits / log (2)));
466 "Sending Bloom filter (%u) of size %u bytes\n",
467 (unsigned int) bf_elementbits,
468 (unsigned int) bf_size);
469 op->state->local_bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
470 bf_size,
471 bf_elementbits);
472 op->state->salt = GNUNET_CRYPTO_random_u32 (UINT32_MAX);
473 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
475 op);
476
477 /* send our Bloom filter */
479 "# Intersection Bloom filters sent",
480 1,
481 GNUNET_NO);
482 chunk_size = 60 * 1024 - sizeof(struct BFMessage);
483 if (bf_size <= chunk_size)
484 {
485 /* singlepart */
486 chunk_size = bf_size;
488 chunk_size,
492 op->state->local_bf,
493 (char *) &msg[1],
494 bf_size));
495 msg->sender_element_count = htonl (op->state->my_element_count);
496 msg->bloomfilter_total_length = htonl (bf_size);
497 msg->bits_per_element = htonl (bf_elementbits);
498 msg->sender_mutator = htonl (op->state->salt);
499 msg->element_xor_hash = op->state->my_xor;
500 GNUNET_MQ_send (op->mq, ev);
501 }
502 else
503 {
504 /* multipart */
505 bf_data = GNUNET_malloc (bf_size);
508 op->state->local_bf,
509 bf_data,
510 bf_size));
511 offset = 0;
512 while (offset < bf_size)
513 {
514 if (bf_size - chunk_size < offset)
515 chunk_size = bf_size - offset;
517 chunk_size,
519 GNUNET_memcpy (&msg[1],
520 &bf_data[offset],
521 chunk_size);
522 offset += chunk_size;
523 msg->sender_element_count = htonl (op->state->my_element_count);
524 msg->bloomfilter_total_length = htonl (bf_size);
525 msg->bits_per_element = htonl (bf_elementbits);
526 msg->sender_mutator = htonl (op->state->salt);
527 msg->element_xor_hash = op->state->my_xor;
528 GNUNET_MQ_send (op->mq, ev);
529 }
530 GNUNET_free (bf_data);
531 }
532 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
533 op->state->local_bf = NULL;
534}
535
536
543static void
545{
546 struct Operation *op = cls;
547 struct GNUNET_MQ_Envelope *ev;
548 struct GNUNET_SET_ResultMessage *rm;
549
551 "Intersection succeeded, sending DONE to local client\n");
553 "# Intersection operations succeeded",
554 1,
555 GNUNET_NO);
556 ev = GNUNET_MQ_msg (rm,
558 rm->request_id = htonl (op->client_request_id);
560 rm->element_type = htons (0);
561 GNUNET_MQ_send (op->set->cs->mq,
562 ev);
564 GNUNET_YES);
565}
566
567
576static void
578{
579 struct Operation *op = cls;
580
582 "DONE sent to other peer, now waiting for other end to close the channel\n");
583 op->state->phase = PHASE_FINISHED;
584 op->state->channel_death_expected = GNUNET_YES;
585}
586
587
595static void
597{
598 struct GNUNET_MQ_Envelope *ev;
599 struct IntersectionDoneMessage *idm;
600
601 GNUNET_assert (PHASE_MUST_SEND_DONE == op->state->phase);
602 GNUNET_assert (GNUNET_NO == op->state->channel_death_expected);
603 ev = GNUNET_MQ_msg (idm,
605 idm->final_element_count = htonl (op->state->my_element_count);
606 idm->element_xor_hash = op->state->my_xor;
609 op);
610 GNUNET_MQ_send (op->mq,
611 ev);
612}
613
614
620static void
622{
623 struct Operation *op = cls;
624 const void *nxt;
625 const struct ElementEntry *ee;
626 struct GNUNET_MQ_Envelope *ev;
627 struct GNUNET_SET_ResultMessage *rm;
628 const struct GNUNET_SET_Element *element;
629 int res;
630
632 op->state->full_result_iter,
633 NULL,
634 &nxt);
635 if (GNUNET_NO == res)
636 {
638 "Sending done and destroy because iterator ran out\n");
640 op->state->full_result_iter);
641 op->state->full_result_iter = NULL;
642 if (PHASE_DONE_RECEIVED == op->state->phase)
643 {
644 op->state->phase = PHASE_FINISHED;
646 }
647 else if (PHASE_MUST_SEND_DONE == op->state->phase)
648 {
650 }
651 else
652 {
653 GNUNET_assert (0);
654 }
655 return;
656 }
657 ee = nxt;
658 element = &ee->element;
660 "Sending element %s:%u to client (full set)\n",
662 element->size);
663 GNUNET_assert (0 != op->client_request_id);
664 ev = GNUNET_MQ_msg_extra (rm,
665 element->size,
667 GNUNET_assert (NULL != ev);
669 rm->request_id = htonl (op->client_request_id);
670 rm->element_type = element->element_type;
671 GNUNET_memcpy (&rm[1],
672 element->data,
673 element->size);
676 op);
677 GNUNET_MQ_send (op->set->cs->mq,
678 ev);
679}
680
681
691static int
693 const struct GNUNET_HashCode *key,
694 void *value)
695{
696 struct ElementEntry *ee = value;
697 struct Operation *op = cls;
698
700 return GNUNET_YES; /* element not live in operation's generation */
701 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
702 &ee->element_hash,
703 &op->state->my_xor);
705 "Initial full initialization of my_elements, adding %s:%u\n",
707 ee->element.size);
709 GNUNET_CONTAINER_multihashmap_put (op->state->my_elements,
710 &ee->element_hash,
711 ee,
713 return GNUNET_YES;
714}
715
716
723static void
725{
726 struct GNUNET_MQ_Envelope *ev;
728
730 "Sending our element count (%u)\n",
731 op->state->my_element_count);
732 ev = GNUNET_MQ_msg (msg,
734 msg->sender_element_count = htonl (op->state->my_element_count);
735 GNUNET_MQ_send (op->mq, ev);
736}
737
738
745static void
747{
748 op->state->phase = PHASE_BF_EXCHANGE;
749 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
751 op);
753}
754
755
756void
758 const struct
760{
761 struct Operation *op = cls;
762
763 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
764 {
765 GNUNET_break_op (0);
767 return;
768 }
769 op->remote_element_count = ntohl (msg->sender_element_count);
771 "Received remote element count (%u), I have %u\n",
772 op->remote_element_count,
773 op->state->my_element_count);
774 if (((PHASE_INITIAL != op->state->phase) &&
775 (PHASE_COUNT_SENT != op->state->phase)) ||
776 (op->state->my_element_count > op->remote_element_count) ||
777 (0 == op->state->my_element_count) ||
778 (0 == op->remote_element_count))
779 {
780 GNUNET_break_op (0);
782 return;
783 }
784 GNUNET_break (NULL == op->state->remote_bf);
786 GNUNET_CADET_receive_done (op->channel);
787}
788
789
795static void
797{
799 "Received BF in phase %u, foreign count is %u, my element count is %u/%u\n",
800 op->state->phase,
801 op->remote_element_count,
802 op->state->my_element_count,
803 GNUNET_CONTAINER_multihashmap_size (op->set->content->elements));
804 switch (op->state->phase)
805 {
806 case PHASE_INITIAL:
807 GNUNET_break_op (0);
809 return;
810
811 case PHASE_COUNT_SENT:
812 /* This is the first BF being sent, build our initial map with
813 filtering in place */
814 op->state->my_element_count = 0;
815 GNUNET_CONTAINER_multihashmap_iterate (op->set->content->elements,
817 op);
818 break;
819
821 /* Update our set by reduction */
822 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
824 op);
825 break;
826
828 GNUNET_break_op (0);
830 return;
831
833 GNUNET_break_op (0);
835 return;
836
837 case PHASE_FINISHED:
838 GNUNET_break_op (0);
840 return;
841 }
842 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
843 op->state->remote_bf = NULL;
844
845 if ((0 == op->state->my_element_count) || /* fully disjoint */
846 ((op->state->my_element_count == op->remote_element_count) &&
847 (0 == GNUNET_memcmp (&op->state->my_xor,
848 &op->state->other_xor))))
849 {
850 /* we are done */
851 op->state->phase = PHASE_MUST_SEND_DONE;
853 "Intersection succeeded, sending DONE to other peer\n");
854 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
855 op->state->local_bf = NULL;
856 if (GNUNET_SET_RESULT_FULL == op->result_mode)
857 {
859 "Sending full result set (%u elements)\n",
860 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
861 op->state->full_result_iter
863 op->state->my_elements);
865 return;
866 }
868 return;
869 }
870 op->state->phase = PHASE_BF_EXCHANGE;
872}
873
874
882int
884 const struct BFMessage *msg)
885{
886 struct Operation *op = cls;
887
888 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
889 {
890 GNUNET_break_op (0);
891 return GNUNET_SYSERR;
892 }
893 return GNUNET_OK;
894}
895
896
903void
905 const struct BFMessage *msg)
906{
907 struct Operation *op = cls;
908 uint32_t bf_size;
909 uint32_t chunk_size;
910 uint32_t bf_bits_per_element;
911
912 switch (op->state->phase)
913 {
914 case PHASE_INITIAL:
915 GNUNET_break_op (0);
917 return;
918
919 case PHASE_COUNT_SENT:
921 bf_size = ntohl (msg->bloomfilter_total_length);
922 bf_bits_per_element = ntohl (msg->bits_per_element);
923 chunk_size = htons (msg->header.size) - sizeof(struct BFMessage);
924 op->state->other_xor = msg->element_xor_hash;
925 if (bf_size == chunk_size)
926 {
927 if (NULL != op->state->bf_data)
928 {
929 GNUNET_break_op (0);
931 return;
932 }
933 /* single part, done here immediately */
934 op->state->remote_bf
935 = GNUNET_CONTAINER_bloomfilter_init ((const char *) &msg[1],
936 bf_size,
937 bf_bits_per_element);
938 op->state->salt = ntohl (msg->sender_mutator);
939 op->remote_element_count = ntohl (msg->sender_element_count);
940 process_bf (op);
941 break;
942 }
943 /* multipart chunk */
944 if (NULL == op->state->bf_data)
945 {
946 /* first chunk, initialize */
947 op->state->bf_data = GNUNET_malloc (bf_size);
948 op->state->bf_data_size = bf_size;
949 op->state->bf_bits_per_element = bf_bits_per_element;
950 op->state->bf_data_offset = 0;
951 op->state->salt = ntohl (msg->sender_mutator);
952 op->remote_element_count = ntohl (msg->sender_element_count);
953 }
954 else
955 {
956 /* increment */
957 if ((op->state->bf_data_size != bf_size) ||
958 (op->state->bf_bits_per_element != bf_bits_per_element) ||
959 (op->state->bf_data_offset + chunk_size > bf_size) ||
960 (op->state->salt != ntohl (msg->sender_mutator)) ||
961 (op->remote_element_count != ntohl (msg->sender_element_count)))
962 {
963 GNUNET_break_op (0);
965 return;
966 }
967 }
968 GNUNET_memcpy (&op->state->bf_data[op->state->bf_data_offset],
969 (const char *) &msg[1],
970 chunk_size);
971 op->state->bf_data_offset += chunk_size;
972 if (op->state->bf_data_offset == bf_size)
973 {
974 /* last chunk, run! */
975 op->state->remote_bf
976 = GNUNET_CONTAINER_bloomfilter_init (op->state->bf_data,
977 bf_size,
978 bf_bits_per_element);
979 GNUNET_free (op->state->bf_data);
980 op->state->bf_data = NULL;
981 op->state->bf_data_size = 0;
982 process_bf (op);
983 }
984 break;
985
986 default:
987 GNUNET_break_op (0);
989 return;
990 }
991 GNUNET_CADET_receive_done (op->channel);
992}
993
994
1003static int
1004filter_all (void *cls,
1005 const struct GNUNET_HashCode *key,
1006 void *value)
1007{
1008 struct Operation *op = cls;
1009 struct ElementEntry *ee = value;
1010
1011 GNUNET_break (0 < op->state->my_element_count);
1012 op->state->my_element_count--;
1013 GNUNET_CRYPTO_hash_xor (&op->state->my_xor,
1014 &ee->element_hash,
1015 &op->state->my_xor);
1017 "Final reduction of my_elements, removing %s:%u\n",
1018 GNUNET_h2s (&ee->element_hash),
1019 ee->element.size);
1021 GNUNET_CONTAINER_multihashmap_remove (op->state->my_elements,
1022 &ee->element_hash,
1023 ee));
1025 &ee->element);
1026 return GNUNET_YES;
1027}
1028
1029
1036void
1038 const struct IntersectionDoneMessage *idm)
1039{
1040 struct Operation *op = cls;
1041
1042 if (GNUNET_SET_OPERATION_INTERSECTION != op->set->operation)
1043 {
1044 GNUNET_break_op (0);
1046 return;
1047 }
1048 if (PHASE_BF_EXCHANGE != op->state->phase)
1049 {
1050 /* wrong phase to conclude? FIXME: Or should we allow this
1051 if the other peer has _initially_ already an empty set? */
1052 GNUNET_break_op (0);
1054 return;
1055 }
1056 if (0 == ntohl (idm->final_element_count))
1057 {
1058 /* other peer determined empty set is the intersection,
1059 remove all elements */
1060 GNUNET_CONTAINER_multihashmap_iterate (op->state->my_elements,
1061 &filter_all,
1062 op);
1063 }
1064 if ((op->state->my_element_count != ntohl (idm->final_element_count)) ||
1065 (0 != GNUNET_memcmp (&op->state->my_xor,
1066 &idm->element_xor_hash)))
1067 {
1068 /* Other peer thinks we are done, but we disagree on the result! */
1069 GNUNET_break_op (0);
1071 return;
1072 }
1074 "Got IntersectionDoneMessage, have %u elements in intersection\n",
1075 op->state->my_element_count);
1076 op->state->phase = PHASE_DONE_RECEIVED;
1077 GNUNET_CADET_receive_done (op->channel);
1078
1079 GNUNET_assert (GNUNET_NO == op->state->client_done_sent);
1080 if (GNUNET_SET_RESULT_FULL == op->result_mode)
1081 {
1083 "Sending full result set to client (%u elements)\n",
1084 GNUNET_CONTAINER_multihashmap_size (op->state->my_elements));
1085 op->state->full_result_iter
1086 = GNUNET_CONTAINER_multihashmap_iterator_create (op->state->my_elements);
1088 return;
1089 }
1090 op->state->phase = PHASE_FINISHED;
1092}
1093
1094
1104static struct OperationState *
1106 const struct GNUNET_MessageHeader *opaque_context)
1107{
1108 struct OperationState *state;
1109 struct GNUNET_MQ_Envelope *ev;
1111
1114 opaque_context);
1115 if (NULL == ev)
1116 {
1117 /* the context message is too large!? */
1118 GNUNET_break (0);
1119 return NULL;
1120 }
1122 "Initiating intersection operation evaluation\n");
1123 state = GNUNET_new (struct OperationState);
1124 /* we started the operation, thus we have to send the operation request */
1125 state->phase = PHASE_INITIAL;
1126 state->my_element_count = op->set->state->current_set_element_count;
1127 state->my_elements
1128 = GNUNET_CONTAINER_multihashmap_create (state->my_element_count,
1129 GNUNET_YES);
1130
1131 msg->operation = htonl (GNUNET_SET_OPERATION_INTERSECTION);
1132 msg->element_count = htonl (state->my_element_count);
1133 GNUNET_MQ_send (op->mq,
1134 ev);
1135 state->phase = PHASE_COUNT_SENT;
1136 if (NULL != opaque_context)
1138 "Sent op request with context message\n");
1139 else
1141 "Sent op request without context message\n");
1142 return state;
1143}
1144
1145
1152static struct OperationState *
1154{
1155 struct OperationState *state;
1156
1158 "Accepting set intersection operation\n");
1159 state = GNUNET_new (struct OperationState);
1160 state->phase = PHASE_INITIAL;
1161 state->my_element_count
1162 = op->set->state->current_set_element_count;
1163 state->my_elements
1165 op->remote_element_count
1166 ),
1167 GNUNET_YES);
1168 op->state = state;
1169 if (op->remote_element_count < state->my_element_count)
1170 {
1171 /* If the other peer (Alice) has fewer elements than us (Bob),
1172 we just send the count as Alice should send the first BF */
1174 state->phase = PHASE_COUNT_SENT;
1175 return state;
1176 }
1177 /* We have fewer elements, so we start with the BF */
1179 return state;
1180}
1181
1182
1189static void
1191{
1192 /* check if the op was canceled twice */
1193 GNUNET_assert (NULL != op->state);
1194 if (NULL != op->state->remote_bf)
1195 {
1196 GNUNET_CONTAINER_bloomfilter_free (op->state->remote_bf);
1197 op->state->remote_bf = NULL;
1198 }
1199 if (NULL != op->state->local_bf)
1200 {
1201 GNUNET_CONTAINER_bloomfilter_free (op->state->local_bf);
1202 op->state->local_bf = NULL;
1203 }
1204 if (NULL != op->state->my_elements)
1205 {
1206 GNUNET_CONTAINER_multihashmap_destroy (op->state->my_elements);
1207 op->state->my_elements = NULL;
1208 }
1209 if (NULL != op->state->full_result_iter)
1210 {
1212 op->state->full_result_iter);
1213 op->state->full_result_iter = NULL;
1214 }
1215 GNUNET_free (op->state);
1216 op->state = NULL;
1218 "Destroying intersection op state done\n");
1219}
1220
1221
1227static struct SetState *
1229{
1230 struct SetState *set_state;
1231
1233 "Intersection set created\n");
1234 set_state = GNUNET_new (struct SetState);
1235 set_state->current_set_element_count = 0;
1236
1237 return set_state;
1238}
1239
1240
1247static void
1248intersection_add (struct SetState *set_state,
1249 struct ElementEntry *ee)
1250{
1251 set_state->current_set_element_count++;
1252}
1253
1254
1260static void
1262{
1263 GNUNET_free (set_state);
1264}
1265
1266
1273static void
1275 struct ElementEntry *element)
1276{
1278 set_state->current_set_element_count--;
1279}
1280
1281
1287static void
1289{
1290 if (GNUNET_YES == op->state->channel_death_expected)
1291 {
1292 /* oh goodie, we are done! */
1294 }
1295 else
1296 {
1297 /* sorry, channel went down early, too bad. */
1299 GNUNET_YES);
1300 }
1301}
1302
1303
1309const struct SetVT *
1311{
1312 static const struct SetVT intersection_vt = {
1314 .add = &intersection_add,
1315 .remove = &intersection_remove,
1316 .destroy_set = &intersection_set_destroy,
1317 .evaluate = &intersection_evaluate,
1318 .accept = &intersection_accept,
1319 .cancel = &intersection_op_cancel,
1320 .channel_death = &intersection_channel_death,
1321 };
1322
1323 return &intersection_vt;
1324}
struct GNUNET_MessageHeader * msg
Definition 005.c:2
static struct GNUNET_ARM_Operation * op
Current operation.
Definition gnunet-arm.c:143
struct GNUNET_HashCode key
The key used in the DHT.
static int state
The current state of the parser.
static char * res
Currently read line or NULL on EOF.
static char * value
Value of the record to add/remove.
int _GSS_is_element_of_operation(struct ElementEntry *ee, struct Operation *op)
Is element ee part of the set used by op?
struct GNUNET_STATISTICS_Handle * _GSS_statistics
Statistics handle.
void _GSS_operation_destroy(struct Operation *op, int gc)
Destroy the given operation.
common components for the implementation the different set operations
const struct SetVT * _GSS_intersection_vt()
Get the table with implementing functions for set intersection.
static void intersection_remove(struct SetState *set_state, struct ElementEntry *element)
Remove the element given in the element message from the set.
void handle_intersection_p2p_bf(void *cls, const struct BFMessage *msg)
Handle an BF message from a remote peer.
void handle_intersection_p2p_element_info(void *cls, const struct IntersectionElementInfoMessage *msg)
Handle the initial struct IntersectionElementInfoMessage from a remote peer.
IntersectionOperationPhase
Current phase we are in for a intersection operation.
@ PHASE_INITIAL
We are just starting.
@ PHASE_DONE_RECEIVED
We have received the P2P DONE message, and must finish with the local client before terminating the c...
@ PHASE_FINISHED
The protocol is over.
@ PHASE_BF_EXCHANGE
We have initialized our set and are now reducing it by exchanging Bloom filters until one party notic...
@ PHASE_COUNT_SENT
We have send the number of our elements to the other peer, but did not setup our element set yet.
@ PHASE_MUST_SEND_DONE
We must next send the P2P DONE message (after finishing mostly with the local client).
static int iterator_bf_reduce(void *cls, const struct GNUNET_HashCode *key, void *value)
Removes elements from our hashmap if they are not contained within the provided remote bloomfilter.
static struct SetState * intersection_set_create()
Create a new set supporting the intersection operation.
static void begin_bf_exchange(struct Operation *op)
We go first, initialize our map with all elements and send the first Bloom filter.
static void intersection_set_destroy(struct SetState *set_state)
Destroy a set that supports the intersection operation.
static int filter_all(void *cls, const struct GNUNET_HashCode *key, void *value)
Remove all elements from our hashmap.
static int filtered_map_initialization(void *cls, const struct GNUNET_HashCode *key, void *value)
Fills the "my_elements" hashmap with all relevant elements.
static void send_p2p_done(struct Operation *op)
Notify the other peer that we are done.
static void send_remaining_elements(void *cls)
Send all elements in the full result iterator.
void handle_intersection_p2p_done(void *cls, const struct IntersectionDoneMessage *idm)
Handle a done message from a remote peer.
static void fail_intersection_operation(struct Operation *op)
Inform the client that the intersection operation has failed, and proceed to destroy the evaluate ope...
static void finished_local_operations(void *cls)
Remember that we are done dealing with the local client AND have sent the other peer our message that...
int check_intersection_p2p_bf(void *cls, const struct BFMessage *msg)
Check an BF message from a remote peer.
static struct OperationState * intersection_evaluate(struct Operation *op, const struct GNUNET_MessageHeader *opaque_context)
Initiate a set intersection operation with a remote peer.
static void send_client_done_and_destroy(void *cls)
Signal to the client that the operation has finished and destroy the operation.
static int initialize_map_unfiltered(void *cls, const struct GNUNET_HashCode *key, void *value)
Fills the "my_elements" hashmap with the initial set of (non-deleted) elements from the set of the sp...
static void send_bloomfilter(struct Operation *op)
Send a bloomfilter to our peer.
static void intersection_op_cancel(struct Operation *op)
Destroy the intersection operation.
static void process_bf(struct Operation *op)
Process a Bloomfilter once we got all the chunks.
static void send_element_count(struct Operation *op)
Send our element count to the peer, in case our element count is lower than theirs.
static void intersection_add(struct SetState *set_state, struct ElementEntry *ee)
Add the element from the given element message to the set.
static void intersection_channel_death(struct Operation *op)
Callback for channel death for the intersection operation.
static void send_client_removed_element(struct Operation *op, struct GNUNET_SET_Element *element)
If applicable in the current operation mode, send a result message to the client indicating we remove...
static struct OperationState * intersection_accept(struct Operation *op)
Accept an intersection operation request from a remote peer.
static int iterator_bf_create(void *cls, const struct GNUNET_HashCode *key, void *value)
Create initial bloomfilter based on all the elements given.
two-peer set operations
Peer-to-Peer messages for gnunet set.
Library for data block manipulation.
API to create, modify and access statistics.
void GNUNET_BLOCK_mingle_hash(const struct GNUNET_HashCode *in, uint32_t mingle_number, struct GNUNET_HashCode *hc)
Mingle hash with the mingle_number to produce different bits.
Definition block.c:96
struct GNUNET_CONTAINER_BloomFilter * GNUNET_CONTAINER_bloomfilter_init(const char *data, size_t size, unsigned int k)
Create a Bloom filter from raw bits.
void GNUNET_CONTAINER_bloomfilter_add(struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Add an element to the filter.
bool GNUNET_CONTAINER_bloomfilter_test(const struct GNUNET_CONTAINER_BloomFilter *bf, const struct GNUNET_HashCode *e)
Test if an element is in the filter.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_bloomfilter_get_raw_data(const struct GNUNET_CONTAINER_BloomFilter *bf, char *data, size_t size)
Copy the raw data of this Bloom filter into the given data array.
void GNUNET_CONTAINER_bloomfilter_free(struct GNUNET_CONTAINER_BloomFilter *bf)
Free the space associated with a filter in memory, flush to drive if needed (do not free the space on...
void GNUNET_CADET_receive_done(struct GNUNET_CADET_Channel *channel)
Indicate readiness to receive the next message on a channel.
Definition cadet_api.c:875
uint32_t GNUNET_CRYPTO_random_u32(uint32_t i)
Produce a random value.
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_iterator_next(struct GNUNET_CONTAINER_MultiHashMapIterator *iter, struct GNUNET_HashCode *key, const void **value)
Retrieve the next element from the hash map at the iterator's position.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
unsigned int GNUNET_CONTAINER_multihashmap_size(const struct GNUNET_CONTAINER_MultiHashMap *map)
Get the number of key-value pairs in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CONTAINER_multihashmap_iterator_destroy(struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
Destroy a multihashmap iterator.
struct GNUNET_CONTAINER_MultiHashMapIterator * GNUNET_CONTAINER_multihashmap_iterator_create(const struct GNUNET_CONTAINER_MultiHashMap *map)
Create an iterator for a multihashmap.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition mq.c:305
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
#define GNUNET_MQ_msg_nested_mh(mvar, type, mh)
Allocate a GNUNET_MQ_Envelope, and append a payload message after the given message struct.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore.
Definition mq.c:655
#define GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST
Request a set operation from a remote peer.
#define GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_DONE
Intersection operation is done.
#define GNUNET_MESSAGE_TYPE_SET_RESULT
Create an empty set.
#define GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_ELEMENT_INFO
Information about the element count for intersection.
#define GNUNET_MESSAGE_TYPE_SET_INTERSECTION_P2P_BF
Bloom filter message for intersection exchange started by Bob.
@ GNUNET_SET_STATUS_OK
Everything went ok, we are transmitting an element of the result (in set, or to be removed from set,...
@ GNUNET_SET_STATUS_FAILURE
The other peer refused to to the operation with us, or something went wrong.
@ GNUNET_SET_STATUS_DONE
Success, all elements have been sent (and received).
@ GNUNET_SET_RESULT_REMOVED
Client gets only elements that have been removed from the set.
@ GNUNET_SET_RESULT_FULL
Client gets every element in the resulting set.
@ GNUNET_SET_OPERATION_INTERSECTION
Set intersection, only return elements that are in both sets.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
Bloom filter messages exchanged for set intersection calculation.
Information about an element element in the set.
struct GNUNET_SET_Element element
The actual element.
struct GNUNET_HashCode element_hash
Hash of the element.
Internal representation of the hash map.
A 512-bit hashcode.
Header for all communications.
Element stored in a set.
uint16_t size
Number of bytes in the buffer pointed to by data.
const void * data
Actual data of the element.
uint16_t element_type
Application-specific element type.
Message sent by the service to the client to indicate an element that is removed (set intersection) o...
Definition set.h:245
uint32_t request_id
id the result belongs to
Definition set.h:259
uint16_t result_status
Was the evaluation successful? Contains an enum GNUNET_SET_Status in NBO.
Definition set.h:265
uint16_t element_type
Type of the element attached to the message, if any.
Definition set.h:270
Last message, send to confirm the final set.
uint32_t final_element_count
Final number of elements in intersection.
struct GNUNET_HashCode element_xor_hash
XOR of all hashes over all elements remaining in the set.
During intersection, the first (and possibly second) message send it the number of elements in the se...
State of an evaluate operation with another peer.
uint32_t bf_bits_per_element
size of the bloomfilter
struct GNUNET_CONTAINER_MultiHashMap * my_elements
Remaining elements in the intersection operation.
uint32_t salt
Salt currently used for BF construction (by us or the other peer, depending on where we are in the co...
struct GNUNET_CONTAINER_BloomFilter * local_bf
BF of the set's element.
struct OperationState * next
Evaluate operations are held in a linked list.
unsigned int generation_created
Generation in which the operation handle was created.
uint32_t bf_data_offset
How many bytes of bf_data are valid?
struct GNUNET_HashCode my_xor
XOR of the keys of all of the elements (remaining) in my set.
enum IntersectionOperationPhase phase
Current state of the operation.
char * bf_data
For multipart BF transmissions, we have to store the bloomfilter-data until we fully received it.
struct GNUNET_HashCode other_xor
XOR of the keys of all of the elements (remaining) in the other peer's set.
uint32_t my_element_count
Current element count contained within my_elements.
struct GNUNET_CONTAINER_MultiHashMapIterator * full_result_iter
Iterator for sending the final set of my_elements to the client.
int channel_death_expected
Set whenever we reach the state where the death of the channel is perfectly find and should NOT resul...
uint32_t bf_data_size
size of the bloomfilter in bf_data.
struct GNUNET_CONTAINER_BloomFilter * remote_bf
The bf we currently receive.
struct OperationState * prev
Evaluate operations are held in a linked list.
int client_done_sent
Did we send the client that we are done?
Operation context used to execute a set operation.
uint32_t bf_bits_per_element
size of the bloomfilter
Extra state required for efficient set intersection.
uint32_t current_set_element_count
Number of currently valid elements in the set which have not been removed.
Dispatch table for a specific set operation.
SetCreateImpl create
Callback for the set creation.