aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-service-rps.c')
-rw-r--r--src/rps/gnunet-service-rps.c4228
1 files changed, 2124 insertions, 2104 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index bbfdba3ac..32f3103ed 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file rps/gnunet-service-rps.c 22 * @file rps/gnunet-service-rps.c
@@ -54,7 +54,7 @@
54// hist_size_init, hist_size_max 54// hist_size_init, hist_size_max
55 55
56/*********************************************************************** 56/***********************************************************************
57 * Old gnunet-service-rps_peers.c 57* Old gnunet-service-rps_peers.c
58***********************************************************************/ 58***********************************************************************/
59 59
60/** 60/**
@@ -65,7 +65,7 @@
65/** 65/**
66 * Get peer flag of given peer context. 66 * Get peer flag of given peer context.
67 */ 67 */
68#define check_peer_flag_set(peer_ctx, mask)\ 68#define check_peer_flag_set(peer_ctx, mask) \
69 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO) 69 ((peer_ctx->peer_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
70 70
71/** 71/**
@@ -76,7 +76,7 @@
76/** 76/**
77 * Get channel flag of given channel context. 77 * Get channel flag of given channel context.
78 */ 78 */
79#define check_channel_flag_set(channel_flags, mask)\ 79#define check_channel_flag_set(channel_flags, mask) \
80 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO) 80 ((*channel_flags) & (mask) ? GNUNET_YES : GNUNET_NO)
81 81
82/** 82/**
@@ -92,8 +92,7 @@
92 * When an operation cannot be executed right now this struct is used to store 92 * When an operation cannot be executed right now this struct is used to store
93 * the callback and closure for later execution. 93 * the callback and closure for later execution.
94 */ 94 */
95struct PeerPendingOp 95struct PeerPendingOp {
96{
97 /** 96 /**
98 * Callback 97 * Callback
99 */ 98 */
@@ -111,8 +110,7 @@ struct PeerPendingOp
111 * This is used to keep track of all messages that have not been sent yet. When 110 * This is used to keep track of all messages that have not been sent yet. When
112 * a peer is to be removed the pending messages can be removed properly. 111 * a peer is to be removed the pending messages can be removed properly.
113 */ 112 */
114struct PendingMessage 113struct PendingMessage {
115{
116 /** 114 /**
117 * DLL next, prev 115 * DLL next, prev
118 */ 116 */
@@ -148,8 +146,7 @@ struct ChannelCtx;
148 * status about the channels, the pending operations on this peer and some flags 146 * status about the channels, the pending operations on this peer and some flags
149 * about the status of the peer itself. (online, valid, ...) 147 * about the status of the peer itself. (online, valid, ...)
150 */ 148 */
151struct PeerContext 149struct PeerContext {
152{
153 /** 150 /**
154 * The Sub this context belongs to. 151 * The Sub this context belongs to.
155 */ 152 */
@@ -224,8 +221,7 @@ struct PeerContext
224/** 221/**
225 * @brief Closure to #valid_peer_iterator 222 * @brief Closure to #valid_peer_iterator
226 */ 223 */
227struct PeersIteratorCls 224struct PeersIteratorCls {
228{
229 /** 225 /**
230 * Iterator function 226 * Iterator function
231 */ 227 */
@@ -240,8 +236,7 @@ struct PeersIteratorCls
240/** 236/**
241 * @brief Context for a channel 237 * @brief Context for a channel
242 */ 238 */
243struct ChannelCtx 239struct ChannelCtx {
244{
245 /** 240 /**
246 * @brief The channel itself 241 * @brief The channel itself
247 */ 242 */
@@ -266,8 +261,7 @@ struct ChannelCtx
266/** 261/**
267 * If type is 2 This struct is used to store the attacked peers in a DLL 262 * If type is 2 This struct is used to store the attacked peers in a DLL
268 */ 263 */
269struct AttackedPeer 264struct AttackedPeer {
270{
271 /** 265 /**
272 * DLL 266 * DLL
273 */ 267 */
@@ -302,8 +296,7 @@ struct AttackedPeer
302 * Essentially one instance of brahms that only connects to other instances 296 * Essentially one instance of brahms that only connects to other instances
303 * with the same (secret) value. 297 * with the same (secret) value.
304 */ 298 */
305struct Sub 299struct Sub {
306{
307 /** 300 /**
308 * @brief Hash of the shared value that defines Subs. 301 * @brief Hash of the shared value that defines Subs.
309 */ 302 */
@@ -448,7 +441,7 @@ struct Sub
448 441
449 442
450/*********************************************************************** 443/***********************************************************************
451 * Globals 444* Globals
452***********************************************************************/ 445***********************************************************************/
453 446
454/** 447/**
@@ -590,15 +583,15 @@ static struct Sub *msub;
590static const uint32_t num_valid_peers_max = UINT32_MAX; 583static const uint32_t num_valid_peers_max = UINT32_MAX;
591 584
592/*********************************************************************** 585/***********************************************************************
593 * /Globals 586* /Globals
594***********************************************************************/ 587***********************************************************************/
595 588
596 589
597static void 590static void
598do_round (void *cls); 591do_round(void *cls);
599 592
600static void 593static void
601do_mal_round (void *cls); 594do_mal_round(void *cls);
602 595
603 596
604/** 597/**
@@ -610,16 +603,16 @@ do_mal_round (void *cls);
610 * @return the #PeerContext 603 * @return the #PeerContext
611 */ 604 */
612static struct PeerContext * 605static struct PeerContext *
613get_peer_ctx (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, 606get_peer_ctx(const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
614 const struct GNUNET_PeerIdentity *peer) 607 const struct GNUNET_PeerIdentity *peer)
615{ 608{
616 struct PeerContext *ctx; 609 struct PeerContext *ctx;
617 int ret; 610 int ret;
618 611
619 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer); 612 ret = GNUNET_CONTAINER_multipeermap_contains(peer_map, peer);
620 GNUNET_assert (GNUNET_YES == ret); 613 GNUNET_assert(GNUNET_YES == ret);
621 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer); 614 ctx = GNUNET_CONTAINER_multipeermap_get(peer_map, peer);
622 GNUNET_assert (NULL != ctx); 615 GNUNET_assert(NULL != ctx);
623 return ctx; 616 return ctx;
624} 617}
625 618
@@ -635,17 +628,17 @@ get_peer_ctx (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
635 * #GNUNET_NO if peer is not knwon 628 * #GNUNET_NO if peer is not knwon
636 */ 629 */
637static int 630static int
638check_peer_known (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, 631check_peer_known(const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
639 const struct GNUNET_PeerIdentity *peer) 632 const struct GNUNET_PeerIdentity *peer)
640{ 633{
641 if (NULL != peer_map) 634 if (NULL != peer_map)
642 { 635 {
643 return GNUNET_CONTAINER_multipeermap_contains (peer_map, peer); 636 return GNUNET_CONTAINER_multipeermap_contains(peer_map, peer);
644 } 637 }
645 else 638 else
646 { 639 {
647 return GNUNET_NO; 640 return GNUNET_NO;
648 } 641 }
649} 642}
650 643
651 644
@@ -658,27 +651,27 @@ check_peer_known (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
658 * @return the #PeerContext 651 * @return the #PeerContext
659 */ 652 */
660static struct PeerContext * 653static struct PeerContext *
661create_peer_ctx (struct Sub *sub, 654create_peer_ctx(struct Sub *sub,
662 const struct GNUNET_PeerIdentity *peer) 655 const struct GNUNET_PeerIdentity *peer)
663{ 656{
664 struct PeerContext *ctx; 657 struct PeerContext *ctx;
665 int ret; 658 int ret;
666 659
667 GNUNET_assert (GNUNET_NO == check_peer_known (sub->peer_map, peer)); 660 GNUNET_assert(GNUNET_NO == check_peer_known(sub->peer_map, peer));
668 661
669 ctx = GNUNET_new (struct PeerContext); 662 ctx = GNUNET_new(struct PeerContext);
670 ctx->peer_id = *peer; 663 ctx->peer_id = *peer;
671 ctx->sub = sub; 664 ctx->sub = sub;
672 ret = GNUNET_CONTAINER_multipeermap_put (sub->peer_map, peer, ctx, 665 ret = GNUNET_CONTAINER_multipeermap_put(sub->peer_map, peer, ctx,
673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
674 GNUNET_assert (GNUNET_OK == ret); 667 GNUNET_assert(GNUNET_OK == ret);
675 if (sub == msub) 668 if (sub == msub)
676 { 669 {
677 GNUNET_STATISTICS_set (stats, 670 GNUNET_STATISTICS_set(stats,
678 "# known peers", 671 "# known peers",
679 GNUNET_CONTAINER_multipeermap_size (sub->peer_map), 672 GNUNET_CONTAINER_multipeermap_size(sub->peer_map),
680 GNUNET_NO); 673 GNUNET_NO);
681 } 674 }
682 return ctx; 675 return ctx;
683} 676}
684 677
@@ -692,14 +685,14 @@ create_peer_ctx (struct Sub *sub,
692 * @return the context 685 * @return the context
693 */ 686 */
694static struct PeerContext * 687static struct PeerContext *
695create_or_get_peer_ctx (struct Sub *sub, 688create_or_get_peer_ctx(struct Sub *sub,
696 const struct GNUNET_PeerIdentity *peer) 689 const struct GNUNET_PeerIdentity *peer)
697{ 690{
698 if (GNUNET_NO == check_peer_known (sub->peer_map, peer)) 691 if (GNUNET_NO == check_peer_known(sub->peer_map, peer))
699 { 692 {
700 return create_peer_ctx (sub, peer); 693 return create_peer_ctx(sub, peer);
701 } 694 }
702 return get_peer_ctx (sub->peer_map, peer); 695 return get_peer_ctx(sub->peer_map, peer);
703} 696}
704 697
705 698
@@ -714,25 +707,25 @@ create_or_get_peer_ctx (struct Sub *sub,
714 * #GNUNET_NO otherwise 707 * #GNUNET_NO otherwise
715 */ 708 */
716static int 709static int
717check_connected (struct PeerContext *peer_ctx) 710check_connected(struct PeerContext *peer_ctx)
718{ 711{
719 /* If we don't know about this peer we don't know whether it's online */ 712 /* If we don't know about this peer we don't know whether it's online */
720 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, 713 if (GNUNET_NO == check_peer_known(peer_ctx->sub->peer_map,
721 &peer_ctx->peer_id)) 714 &peer_ctx->peer_id))
722 { 715 {
723 return GNUNET_NO; 716 return GNUNET_NO;
724 } 717 }
725 /* Get the context */ 718 /* Get the context */
726 peer_ctx = get_peer_ctx (peer_ctx->sub->peer_map, &peer_ctx->peer_id); 719 peer_ctx = get_peer_ctx(peer_ctx->sub->peer_map, &peer_ctx->peer_id);
727 /* If we have no channel to this peer we don't know whether it's online */ 720 /* If we have no channel to this peer we don't know whether it's online */
728 if ( (NULL == peer_ctx->send_channel_ctx) && 721 if ((NULL == peer_ctx->send_channel_ctx) &&
729 (NULL == peer_ctx->recv_channel_ctx) ) 722 (NULL == peer_ctx->recv_channel_ctx))
730 { 723 {
731 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE); 724 UNSET_PEER_FLAG(peer_ctx, Peers_ONLINE);
732 return GNUNET_NO; 725 return GNUNET_NO;
733 } 726 }
734 /* Otherwise (if we have a channel, we know that it's online */ 727 /* Otherwise (if we have a channel, we know that it's online */
735 SET_PEER_FLAG (peer_ctx, Peers_ONLINE); 728 SET_PEER_FLAG(peer_ctx, Peers_ONLINE);
736 return GNUNET_YES; 729 return GNUNET_YES;
737} 730}
738 731
@@ -740,8 +733,7 @@ check_connected (struct PeerContext *peer_ctx)
740/** 733/**
741 * @brief The closure to #get_rand_peer_iterator. 734 * @brief The closure to #get_rand_peer_iterator.
742 */ 735 */
743struct GetRandPeerIteratorCls 736struct GetRandPeerIteratorCls {
744{
745 /** 737 /**
746 * @brief The index of the peer to return. 738 * @brief The index of the peer to return.
747 * Will be decreased until 0. 739 * Will be decreased until 0.
@@ -772,18 +764,19 @@ struct GetRandPeerIteratorCls
772 * #GNUNET_NO if not. 764 * #GNUNET_NO if not.
773 */ 765 */
774static int 766static int
775get_rand_peer_iterator (void *cls, 767get_rand_peer_iterator(void *cls,
776 const struct GNUNET_PeerIdentity *peer, 768 const struct GNUNET_PeerIdentity *peer,
777 void *value) 769 void *value)
778{ 770{
779 struct GetRandPeerIteratorCls *iterator_cls = cls; 771 struct GetRandPeerIteratorCls *iterator_cls = cls;
780 (void) value; 772
773 (void)value;
781 774
782 if (0 >= iterator_cls->index) 775 if (0 >= iterator_cls->index)
783 { 776 {
784 iterator_cls->peer = peer; 777 iterator_cls->peer = peer;
785 return GNUNET_NO; 778 return GNUNET_NO;
786 } 779 }
787 iterator_cls->index--; 780 iterator_cls->index--;
788 return GNUNET_YES; 781 return GNUNET_YES;
789} 782}
@@ -798,19 +791,19 @@ get_rand_peer_iterator (void *cls,
798 * @return a random peer 791 * @return a random peer
799 */ 792 */
800static const struct GNUNET_PeerIdentity * 793static const struct GNUNET_PeerIdentity *
801get_random_peer_from_peermap (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers) 794get_random_peer_from_peermap(struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
802{ 795{
803 struct GetRandPeerIteratorCls *iterator_cls; 796 struct GetRandPeerIteratorCls *iterator_cls;
804 const struct GNUNET_PeerIdentity *ret; 797 const struct GNUNET_PeerIdentity *ret;
805 798
806 iterator_cls = GNUNET_new (struct GetRandPeerIteratorCls); 799 iterator_cls = GNUNET_new(struct GetRandPeerIteratorCls);
807 iterator_cls->index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 800 iterator_cls->index = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
808 GNUNET_CONTAINER_multipeermap_size (valid_peers)); 801 GNUNET_CONTAINER_multipeermap_size(valid_peers));
809 (void) GNUNET_CONTAINER_multipeermap_iterate (valid_peers, 802 (void)GNUNET_CONTAINER_multipeermap_iterate(valid_peers,
810 get_rand_peer_iterator, 803 get_rand_peer_iterator,
811 iterator_cls); 804 iterator_cls);
812 ret = iterator_cls->peer; 805 ret = iterator_cls->peer;
813 GNUNET_free (iterator_cls); 806 GNUNET_free(iterator_cls);
814 return ret; 807 return ret;
815} 808}
816 809
@@ -827,8 +820,8 @@ get_random_peer_from_peermap (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
827 * #GNUNET_NO otherwise 820 * #GNUNET_NO otherwise
828 */ 821 */
829static int 822static int
830add_valid_peer (const struct GNUNET_PeerIdentity *peer, 823add_valid_peer(const struct GNUNET_PeerIdentity *peer,
831 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers) 824 struct GNUNET_CONTAINER_MultiPeerMap *valid_peers)
832{ 825{
833 const struct GNUNET_PeerIdentity *rand_peer; 826 const struct GNUNET_PeerIdentity *rand_peer;
834 int ret; 827 int ret;
@@ -836,26 +829,26 @@ add_valid_peer (const struct GNUNET_PeerIdentity *peer,
836 ret = GNUNET_YES; 829 ret = GNUNET_YES;
837 /* Remove random peers until there is space for a new one */ 830 /* Remove random peers until there is space for a new one */
838 while (num_valid_peers_max <= 831 while (num_valid_peers_max <=
839 GNUNET_CONTAINER_multipeermap_size (valid_peers)) 832 GNUNET_CONTAINER_multipeermap_size(valid_peers))
840 { 833 {
841 rand_peer = get_random_peer_from_peermap (valid_peers); 834 rand_peer = get_random_peer_from_peermap(valid_peers);
842 GNUNET_CONTAINER_multipeermap_remove_all (valid_peers, rand_peer); 835 GNUNET_CONTAINER_multipeermap_remove_all(valid_peers, rand_peer);
843 ret = GNUNET_NO; 836 ret = GNUNET_NO;
844 } 837 }
845 (void) GNUNET_CONTAINER_multipeermap_put (valid_peers, peer, NULL, 838 (void)GNUNET_CONTAINER_multipeermap_put(valid_peers, peer, NULL,
846 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 839 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
847 if (valid_peers == msub->valid_peers) 840 if (valid_peers == msub->valid_peers)
848 { 841 {
849 GNUNET_STATISTICS_set (stats, 842 GNUNET_STATISTICS_set(stats,
850 "# valid peers", 843 "# valid peers",
851 GNUNET_CONTAINER_multipeermap_size (valid_peers), 844 GNUNET_CONTAINER_multipeermap_size(valid_peers),
852 GNUNET_NO); 845 GNUNET_NO);
853 } 846 }
854 return ret; 847 return ret;
855} 848}
856 849
857static void 850static void
858remove_pending_message (struct PendingMessage *pending_msg, int cancel); 851remove_pending_message(struct PendingMessage *pending_msg, int cancel);
859 852
860/** 853/**
861 * @brief Set the peer flag to living and 854 * @brief Set the peer flag to living and
@@ -866,62 +859,62 @@ remove_pending_message (struct PendingMessage *pending_msg, int cancel);
866 * @param peer_ctx the #PeerContext of the peer to set online 859 * @param peer_ctx the #PeerContext of the peer to set online
867 */ 860 */
868static void 861static void
869set_peer_online (struct PeerContext *peer_ctx) 862set_peer_online(struct PeerContext *peer_ctx)
870{ 863{
871 struct GNUNET_PeerIdentity *peer; 864 struct GNUNET_PeerIdentity *peer;
872 unsigned int i; 865 unsigned int i;
873 866
874 peer = &peer_ctx->peer_id; 867 peer = &peer_ctx->peer_id;
875 LOG (GNUNET_ERROR_TYPE_DEBUG, 868 LOG(GNUNET_ERROR_TYPE_DEBUG,
876 "Peer %s is online and valid, calling %i pending operations on it\n", 869 "Peer %s is online and valid, calling %i pending operations on it\n",
877 GNUNET_i2s (peer), 870 GNUNET_i2s(peer),
878 peer_ctx->num_pending_ops); 871 peer_ctx->num_pending_ops);
879 872
880 if (NULL != peer_ctx->online_check_pending) 873 if (NULL != peer_ctx->online_check_pending)
881 { 874 {
882 LOG (GNUNET_ERROR_TYPE_DEBUG, 875 LOG(GNUNET_ERROR_TYPE_DEBUG,
883 "Removing pending online check for peer %s\n", 876 "Removing pending online check for peer %s\n",
884 GNUNET_i2s (&peer_ctx->peer_id)); 877 GNUNET_i2s(&peer_ctx->peer_id));
885 // TODO wait until cadet sets mq->cancel_impl 878 // TODO wait until cadet sets mq->cancel_impl
886 //GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev); 879 //GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
887 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES); 880 remove_pending_message(peer_ctx->online_check_pending, GNUNET_YES);
888 peer_ctx->online_check_pending = NULL; 881 peer_ctx->online_check_pending = NULL;
889 } 882 }
890 883
891 SET_PEER_FLAG (peer_ctx, Peers_ONLINE); 884 SET_PEER_FLAG(peer_ctx, Peers_ONLINE);
892 885
893 /* Call pending operations */ 886 /* Call pending operations */
894 for (i = 0; i < peer_ctx->num_pending_ops; i++) 887 for (i = 0; i < peer_ctx->num_pending_ops; i++)
895 { 888 {
896 peer_ctx->pending_ops[i].op (peer_ctx->pending_ops[i].op_cls, peer); 889 peer_ctx->pending_ops[i].op(peer_ctx->pending_ops[i].op_cls, peer);
897 } 890 }
898 GNUNET_array_grow (peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0); 891 GNUNET_array_grow(peer_ctx->pending_ops, peer_ctx->num_pending_ops, 0);
899} 892}
900 893
901static void 894static void
902cleanup_destroyed_channel (void *cls, 895cleanup_destroyed_channel(void *cls,
903 const struct GNUNET_CADET_Channel *channel); 896 const struct GNUNET_CADET_Channel *channel);
904 897
905/* Declaration of handlers */ 898/* Declaration of handlers */
906static void 899static void
907handle_peer_check (void *cls, 900handle_peer_check(void *cls,
908 const struct GNUNET_MessageHeader *msg); 901 const struct GNUNET_MessageHeader *msg);
909 902
910static void 903static void
911handle_peer_push (void *cls, 904handle_peer_push(void *cls,
912 const struct GNUNET_MessageHeader *msg); 905 const struct GNUNET_MessageHeader *msg);
913 906
914static void 907static void
915handle_peer_pull_request (void *cls, 908handle_peer_pull_request(void *cls,
916 const struct GNUNET_MessageHeader *msg); 909 const struct GNUNET_MessageHeader *msg);
917 910
918static int 911static int
919check_peer_pull_reply (void *cls, 912check_peer_pull_reply(void *cls,
920 const struct GNUNET_RPS_P2P_PullReplyMessage *msg); 913 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
921 914
922static void 915static void
923handle_peer_pull_reply (void *cls, 916handle_peer_pull_reply(void *cls,
924 const struct GNUNET_RPS_P2P_PullReplyMessage *msg); 917 const struct GNUNET_RPS_P2P_PullReplyMessage *msg);
925 918
926/* End declaration of handlers */ 919/* End declaration of handlers */
927 920
@@ -933,10 +926,11 @@ handle_peer_pull_reply (void *cls,
933 * @return The channel context 926 * @return The channel context
934 */ 927 */
935static struct ChannelCtx * 928static struct ChannelCtx *
936add_channel_ctx (struct PeerContext *peer_ctx) 929add_channel_ctx(struct PeerContext *peer_ctx)
937{ 930{
938 struct ChannelCtx *channel_ctx; 931 struct ChannelCtx *channel_ctx;
939 channel_ctx = GNUNET_new (struct ChannelCtx); 932
933 channel_ctx = GNUNET_new(struct ChannelCtx);
940 channel_ctx->peer_ctx = peer_ctx; 934 channel_ctx->peer_ctx = peer_ctx;
941 return channel_ctx; 935 return channel_ctx;
942} 936}
@@ -948,28 +942,29 @@ add_channel_ctx (struct PeerContext *peer_ctx)
948 * @param channel_ctx The channel context. 942 * @param channel_ctx The channel context.
949 */ 943 */
950static void 944static void
951remove_channel_ctx (struct ChannelCtx *channel_ctx) 945remove_channel_ctx(struct ChannelCtx *channel_ctx)
952{ 946{
953 struct PeerContext *peer_ctx = channel_ctx->peer_ctx; 947 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
954 948
955 if (NULL != channel_ctx->destruction_task) 949 if (NULL != channel_ctx->destruction_task)
956 { 950 {
957 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task); 951 GNUNET_SCHEDULER_cancel(channel_ctx->destruction_task);
958 channel_ctx->destruction_task = NULL; 952 channel_ctx->destruction_task = NULL;
959 } 953 }
960 954
961 GNUNET_free (channel_ctx); 955 GNUNET_free(channel_ctx);
962 956
963 if (NULL == peer_ctx) return; 957 if (NULL == peer_ctx)
958 return;
964 if (channel_ctx == peer_ctx->send_channel_ctx) 959 if (channel_ctx == peer_ctx->send_channel_ctx)
965 { 960 {
966 peer_ctx->send_channel_ctx = NULL; 961 peer_ctx->send_channel_ctx = NULL;
967 peer_ctx->mq = NULL; 962 peer_ctx->mq = NULL;
968 } 963 }
969 else if (channel_ctx == peer_ctx->recv_channel_ctx) 964 else if (channel_ctx == peer_ctx->recv_channel_ctx)
970 { 965 {
971 peer_ctx->recv_channel_ctx = NULL; 966 peer_ctx->recv_channel_ctx = NULL;
972 } 967 }
973} 968}
974 969
975 970
@@ -980,47 +975,47 @@ remove_channel_ctx (struct ChannelCtx *channel_ctx)
980 * @return the #GNUNET_CADET_Channel used to send data to @a peer_ctx 975 * @return the #GNUNET_CADET_Channel used to send data to @a peer_ctx
981 */ 976 */
982struct GNUNET_CADET_Channel * 977struct GNUNET_CADET_Channel *
983get_channel (struct PeerContext *peer_ctx) 978get_channel(struct PeerContext *peer_ctx)
984{ 979{
985 /* There exists a copy-paste-clone in run() */ 980 /* There exists a copy-paste-clone in run() */
986 struct GNUNET_MQ_MessageHandler cadet_handlers[] = { 981 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
987 GNUNET_MQ_hd_fixed_size (peer_check, 982 GNUNET_MQ_hd_fixed_size(peer_check,
988 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE, 983 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
989 struct GNUNET_MessageHeader, 984 struct GNUNET_MessageHeader,
990 NULL), 985 NULL),
991 GNUNET_MQ_hd_fixed_size (peer_push, 986 GNUNET_MQ_hd_fixed_size(peer_push,
992 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH, 987 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
993 struct GNUNET_MessageHeader, 988 struct GNUNET_MessageHeader,
994 NULL), 989 NULL),
995 GNUNET_MQ_hd_fixed_size (peer_pull_request, 990 GNUNET_MQ_hd_fixed_size(peer_pull_request,
996 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 991 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
997 struct GNUNET_MessageHeader, 992 struct GNUNET_MessageHeader,
998 NULL), 993 NULL),
999 GNUNET_MQ_hd_var_size (peer_pull_reply, 994 GNUNET_MQ_hd_var_size(peer_pull_reply,
1000 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY, 995 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
1001 struct GNUNET_RPS_P2P_PullReplyMessage, 996 struct GNUNET_RPS_P2P_PullReplyMessage,
1002 NULL), 997 NULL),
1003 GNUNET_MQ_handler_end () 998 GNUNET_MQ_handler_end()
1004 }; 999 };
1005 1000
1006 1001
1007 if (NULL == peer_ctx->send_channel_ctx) 1002 if (NULL == peer_ctx->send_channel_ctx)
1008 { 1003 {
1009 LOG (GNUNET_ERROR_TYPE_DEBUG, 1004 LOG(GNUNET_ERROR_TYPE_DEBUG,
1010 "Trying to establish channel to peer %s\n", 1005 "Trying to establish channel to peer %s\n",
1011 GNUNET_i2s (&peer_ctx->peer_id)); 1006 GNUNET_i2s(&peer_ctx->peer_id));
1012 peer_ctx->send_channel_ctx = add_channel_ctx (peer_ctx); 1007 peer_ctx->send_channel_ctx = add_channel_ctx(peer_ctx);
1013 peer_ctx->send_channel_ctx->channel = 1008 peer_ctx->send_channel_ctx->channel =
1014 GNUNET_CADET_channel_create (cadet_handle, 1009 GNUNET_CADET_channel_create(cadet_handle,
1015 peer_ctx->send_channel_ctx, /* context */ 1010 peer_ctx->send_channel_ctx, /* context */
1016 &peer_ctx->peer_id, 1011 &peer_ctx->peer_id,
1017 &peer_ctx->sub->hash, 1012 &peer_ctx->sub->hash,
1018 NULL, /* WindowSize handler */ 1013 NULL, /* WindowSize handler */
1019 &cleanup_destroyed_channel, /* Disconnect handler */ 1014 &cleanup_destroyed_channel, /* Disconnect handler */
1020 cadet_handlers); 1015 cadet_handlers);
1021 } 1016 }
1022 GNUNET_assert (NULL != peer_ctx->send_channel_ctx); 1017 GNUNET_assert(NULL != peer_ctx->send_channel_ctx);
1023 GNUNET_assert (NULL != peer_ctx->send_channel_ctx->channel); 1018 GNUNET_assert(NULL != peer_ctx->send_channel_ctx->channel);
1024 return peer_ctx->send_channel_ctx->channel; 1019 return peer_ctx->send_channel_ctx->channel;
1025} 1020}
1026 1021
@@ -1035,12 +1030,12 @@ get_channel (struct PeerContext *peer_ctx)
1035 * @return the #GNUNET_MQ_Handle 1030 * @return the #GNUNET_MQ_Handle
1036 */ 1031 */
1037static struct GNUNET_MQ_Handle * 1032static struct GNUNET_MQ_Handle *
1038get_mq (struct PeerContext *peer_ctx) 1033get_mq(struct PeerContext *peer_ctx)
1039{ 1034{
1040 if (NULL == peer_ctx->mq) 1035 if (NULL == peer_ctx->mq)
1041 { 1036 {
1042 peer_ctx->mq = GNUNET_CADET_get_mq (get_channel (peer_ctx)); 1037 peer_ctx->mq = GNUNET_CADET_get_mq(get_channel(peer_ctx));
1043 } 1038 }
1044 return peer_ctx->mq; 1039 return peer_ctx->mq;
1045} 1040}
1046 1041
@@ -1053,19 +1048,19 @@ get_mq (struct PeerContext *peer_ctx)
1053 * @return pointer to pending message 1048 * @return pointer to pending message
1054 */ 1049 */
1055static struct PendingMessage * 1050static struct PendingMessage *
1056insert_pending_message (struct PeerContext *peer_ctx, 1051insert_pending_message(struct PeerContext *peer_ctx,
1057 struct GNUNET_MQ_Envelope *ev, 1052 struct GNUNET_MQ_Envelope *ev,
1058 const char *type) 1053 const char *type)
1059{ 1054{
1060 struct PendingMessage *pending_msg; 1055 struct PendingMessage *pending_msg;
1061 1056
1062 pending_msg = GNUNET_new (struct PendingMessage); 1057 pending_msg = GNUNET_new(struct PendingMessage);
1063 pending_msg->ev = ev; 1058 pending_msg->ev = ev;
1064 pending_msg->peer_ctx = peer_ctx; 1059 pending_msg->peer_ctx = peer_ctx;
1065 pending_msg->type = type; 1060 pending_msg->type = type;
1066 GNUNET_CONTAINER_DLL_insert (peer_ctx->pending_messages_head, 1061 GNUNET_CONTAINER_DLL_insert(peer_ctx->pending_messages_head,
1067 peer_ctx->pending_messages_tail, 1062 peer_ctx->pending_messages_tail,
1068 pending_msg); 1063 pending_msg);
1069 return pending_msg; 1064 return pending_msg;
1070} 1065}
1071 1066
@@ -1077,22 +1072,23 @@ insert_pending_message (struct PeerContext *peer_ctx,
1077 * @param cancel whether to cancel the pending message, too 1072 * @param cancel whether to cancel the pending message, too
1078 */ 1073 */
1079static void 1074static void
1080remove_pending_message (struct PendingMessage *pending_msg, int cancel) 1075remove_pending_message(struct PendingMessage *pending_msg, int cancel)
1081{ 1076{
1082 struct PeerContext *peer_ctx; 1077 struct PeerContext *peer_ctx;
1083 (void) cancel; 1078
1079 (void)cancel;
1084 1080
1085 peer_ctx = pending_msg->peer_ctx; 1081 peer_ctx = pending_msg->peer_ctx;
1086 GNUNET_assert (NULL != peer_ctx); 1082 GNUNET_assert(NULL != peer_ctx);
1087 GNUNET_CONTAINER_DLL_remove (peer_ctx->pending_messages_head, 1083 GNUNET_CONTAINER_DLL_remove(peer_ctx->pending_messages_head,
1088 peer_ctx->pending_messages_tail, 1084 peer_ctx->pending_messages_tail,
1089 pending_msg); 1085 pending_msg);
1090 // TODO wait for the cadet implementation of message cancellation 1086 // TODO wait for the cadet implementation of message cancellation
1091 //if (GNUNET_YES == cancel) 1087 //if (GNUNET_YES == cancel)
1092 //{ 1088 //{
1093 // GNUNET_MQ_send_cancel (pending_msg->ev); 1089 // GNUNET_MQ_send_cancel (pending_msg->ev);
1094 //} 1090 //}
1095 GNUNET_free (pending_msg); 1091 GNUNET_free(pending_msg);
1096} 1092}
1097 1093
1098 1094
@@ -1103,20 +1099,20 @@ remove_pending_message (struct PendingMessage *pending_msg, int cancel)
1103 * @param cls #PeerContext of peer with pending online check 1099 * @param cls #PeerContext of peer with pending online check
1104 */ 1100 */
1105static void 1101static void
1106mq_online_check_successful (void *cls) 1102mq_online_check_successful(void *cls)
1107{ 1103{
1108 struct PeerContext *peer_ctx = cls; 1104 struct PeerContext *peer_ctx = cls;
1109 1105
1110 if (NULL != peer_ctx->online_check_pending) 1106 if (NULL != peer_ctx->online_check_pending)
1111 { 1107 {
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, 1108 LOG(GNUNET_ERROR_TYPE_DEBUG,
1113 "Online check for peer %s was successfull\n", 1109 "Online check for peer %s was successfull\n",
1114 GNUNET_i2s (&peer_ctx->peer_id)); 1110 GNUNET_i2s(&peer_ctx->peer_id));
1115 remove_pending_message (peer_ctx->online_check_pending, GNUNET_YES); 1111 remove_pending_message(peer_ctx->online_check_pending, GNUNET_YES);
1116 peer_ctx->online_check_pending = NULL; 1112 peer_ctx->online_check_pending = NULL;
1117 set_peer_online (peer_ctx); 1113 set_peer_online(peer_ctx);
1118 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers); 1114 (void)add_valid_peer(&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1119 } 1115 }
1120} 1116}
1121 1117
1122/** 1118/**
@@ -1125,30 +1121,30 @@ mq_online_check_successful (void *cls)
1125 * @param peer_ctx the context of the peer 1121 * @param peer_ctx the context of the peer
1126 */ 1122 */
1127static void 1123static void
1128check_peer_online (struct PeerContext *peer_ctx) 1124check_peer_online(struct PeerContext *peer_ctx)
1129{ 1125{
1130 LOG (GNUNET_ERROR_TYPE_DEBUG, 1126 LOG(GNUNET_ERROR_TYPE_DEBUG,
1131 "Get informed about peer %s getting online\n", 1127 "Get informed about peer %s getting online\n",
1132 GNUNET_i2s (&peer_ctx->peer_id)); 1128 GNUNET_i2s(&peer_ctx->peer_id));
1133 1129
1134 struct GNUNET_MQ_Handle *mq; 1130 struct GNUNET_MQ_Handle *mq;
1135 struct GNUNET_MQ_Envelope *ev; 1131 struct GNUNET_MQ_Envelope *ev;
1136 1132
1137 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE); 1133 ev = GNUNET_MQ_msg_header(GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE);
1138 peer_ctx->online_check_pending = 1134 peer_ctx->online_check_pending =
1139 insert_pending_message (peer_ctx, ev, "Check online"); 1135 insert_pending_message(peer_ctx, ev, "Check online");
1140 mq = get_mq (peer_ctx); 1136 mq = get_mq(peer_ctx);
1141 GNUNET_MQ_notify_sent (ev, 1137 GNUNET_MQ_notify_sent(ev,
1142 mq_online_check_successful, 1138 mq_online_check_successful,
1143 peer_ctx); 1139 peer_ctx);
1144 GNUNET_MQ_send (mq, ev); 1140 GNUNET_MQ_send(mq, ev);
1145 if (peer_ctx->sub == msub) 1141 if (peer_ctx->sub == msub)
1146 { 1142 {
1147 GNUNET_STATISTICS_update (stats, 1143 GNUNET_STATISTICS_update(stats,
1148 "# pending online checks", 1144 "# pending online checks",
1149 1, 1145 1,
1150 GNUNET_NO); 1146 GNUNET_NO);
1151 } 1147 }
1152} 1148}
1153 1149
1154 1150
@@ -1165,8 +1161,8 @@ check_peer_online (struct PeerContext *peer_ctx)
1165 * #GNUNET_NO otherwise 1161 * #GNUNET_NO otherwise
1166 */ 1162 */
1167static int 1163static int
1168check_operation_scheduled (const struct PeerContext *peer_ctx, 1164check_operation_scheduled(const struct PeerContext *peer_ctx,
1169 const PeerOp peer_op) 1165 const PeerOp peer_op)
1170{ 1166{
1171 unsigned int i; 1167 unsigned int i;
1172 1168
@@ -1183,20 +1179,20 @@ check_operation_scheduled (const struct PeerContext *peer_ctx,
1183 * @param cls Context of the channel 1179 * @param cls Context of the channel
1184 */ 1180 */
1185static void 1181static void
1186destroy_channel (struct ChannelCtx *channel_ctx) 1182destroy_channel(struct ChannelCtx *channel_ctx)
1187{ 1183{
1188 struct GNUNET_CADET_Channel *channel; 1184 struct GNUNET_CADET_Channel *channel;
1189 1185
1190 if (NULL != channel_ctx->destruction_task) 1186 if (NULL != channel_ctx->destruction_task)
1191 { 1187 {
1192 GNUNET_SCHEDULER_cancel (channel_ctx->destruction_task); 1188 GNUNET_SCHEDULER_cancel(channel_ctx->destruction_task);
1193 channel_ctx->destruction_task = NULL; 1189 channel_ctx->destruction_task = NULL;
1194 } 1190 }
1195 GNUNET_assert (channel_ctx->channel != NULL); 1191 GNUNET_assert(channel_ctx->channel != NULL);
1196 channel = channel_ctx->channel; 1192 channel = channel_ctx->channel;
1197 channel_ctx->channel = NULL; 1193 channel_ctx->channel = NULL;
1198 GNUNET_CADET_channel_destroy (channel); 1194 GNUNET_CADET_channel_destroy(channel);
1199 remove_channel_ctx (channel_ctx); 1195 remove_channel_ctx(channel_ctx);
1200} 1196}
1201 1197
1202 1198
@@ -1208,12 +1204,12 @@ destroy_channel (struct ChannelCtx *channel_ctx)
1208 * @param cls 1204 * @param cls
1209 */ 1205 */
1210static void 1206static void
1211destroy_channel_cb (void *cls) 1207destroy_channel_cb(void *cls)
1212{ 1208{
1213 struct ChannelCtx *channel_ctx = cls; 1209 struct ChannelCtx *channel_ctx = cls;
1214 1210
1215 channel_ctx->destruction_task = NULL; 1211 channel_ctx->destruction_task = NULL;
1216 destroy_channel (channel_ctx); 1212 destroy_channel(channel_ctx);
1217} 1213}
1218 1214
1219 1215
@@ -1228,15 +1224,15 @@ destroy_channel_cb (void *cls)
1228 * @param channel_ctx channel to be destroyed. 1224 * @param channel_ctx channel to be destroyed.
1229 */ 1225 */
1230static void 1226static void
1231schedule_channel_destruction (struct ChannelCtx *channel_ctx) 1227schedule_channel_destruction(struct ChannelCtx *channel_ctx)
1232{ 1228{
1233 GNUNET_assert (NULL == 1229 GNUNET_assert(NULL ==
1234 channel_ctx->destruction_task); 1230 channel_ctx->destruction_task);
1235 GNUNET_assert (NULL != 1231 GNUNET_assert(NULL !=
1236 channel_ctx->channel); 1232 channel_ctx->channel);
1237 channel_ctx->destruction_task = 1233 channel_ctx->destruction_task =
1238 GNUNET_SCHEDULER_add_now (&destroy_channel_cb, 1234 GNUNET_SCHEDULER_add_now(&destroy_channel_cb,
1239 channel_ctx); 1235 channel_ctx);
1240} 1236}
1241 1237
1242 1238
@@ -1254,98 +1250,98 @@ schedule_channel_destruction (struct ChannelCtx *channel_ctx)
1254 * #GNUNET_NO otherwise 1250 * #GNUNET_NO otherwise
1255 */ 1251 */
1256static int 1252static int
1257destroy_peer (struct PeerContext *peer_ctx) 1253destroy_peer(struct PeerContext *peer_ctx)
1258{ 1254{
1259 GNUNET_assert (NULL != peer_ctx); 1255 GNUNET_assert(NULL != peer_ctx);
1260 GNUNET_assert (NULL != peer_ctx->sub->peer_map); 1256 GNUNET_assert(NULL != peer_ctx->sub->peer_map);
1261 if (GNUNET_NO == 1257 if (GNUNET_NO ==
1262 GNUNET_CONTAINER_multipeermap_contains (peer_ctx->sub->peer_map, 1258 GNUNET_CONTAINER_multipeermap_contains(peer_ctx->sub->peer_map,
1263 &peer_ctx->peer_id)) 1259 &peer_ctx->peer_id))
1264 { 1260 {
1265 return GNUNET_NO; 1261 return GNUNET_NO;
1266 } 1262 }
1267 SET_PEER_FLAG (peer_ctx, Peers_TO_DESTROY); 1263 SET_PEER_FLAG(peer_ctx, Peers_TO_DESTROY);
1268 LOG (GNUNET_ERROR_TYPE_DEBUG, 1264 LOG(GNUNET_ERROR_TYPE_DEBUG,
1269 "Going to remove peer %s\n", 1265 "Going to remove peer %s\n",
1270 GNUNET_i2s (&peer_ctx->peer_id)); 1266 GNUNET_i2s(&peer_ctx->peer_id));
1271 UNSET_PEER_FLAG (peer_ctx, Peers_ONLINE); 1267 UNSET_PEER_FLAG(peer_ctx, Peers_ONLINE);
1272 1268
1273 /* Clear list of pending operations */ 1269 /* Clear list of pending operations */
1274 // TODO this probably leaks memory 1270 // TODO this probably leaks memory
1275 // ('only' the cls to the function. Not sure what to do with it) 1271 // ('only' the cls to the function. Not sure what to do with it)
1276 GNUNET_array_grow (peer_ctx->pending_ops, 1272 GNUNET_array_grow(peer_ctx->pending_ops,
1277 peer_ctx->num_pending_ops, 1273 peer_ctx->num_pending_ops,
1278 0); 1274 0);
1279 /* Remove all pending messages */ 1275 /* Remove all pending messages */
1280 while (NULL != peer_ctx->pending_messages_head) 1276 while (NULL != peer_ctx->pending_messages_head)
1281 { 1277 {
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, 1278 LOG(GNUNET_ERROR_TYPE_DEBUG,
1283 "Removing unsent %s\n", 1279 "Removing unsent %s\n",
1284 peer_ctx->pending_messages_head->type); 1280 peer_ctx->pending_messages_head->type);
1285 /* Cancle pending message, too */ 1281 /* Cancle pending message, too */
1286 if ( (NULL != peer_ctx->online_check_pending) && 1282 if ((NULL != peer_ctx->online_check_pending) &&
1287 (0 == memcmp (peer_ctx->pending_messages_head, 1283 (0 == memcmp(peer_ctx->pending_messages_head,
1288 peer_ctx->online_check_pending, 1284 peer_ctx->online_check_pending,
1289 sizeof (struct PendingMessage))) ) 1285 sizeof(struct PendingMessage))))
1290 {
1291 peer_ctx->online_check_pending = NULL;
1292 if (peer_ctx->sub == msub)
1293 { 1286 {
1294 GNUNET_STATISTICS_update (stats, 1287 peer_ctx->online_check_pending = NULL;
1295 "# pending online checks", 1288 if (peer_ctx->sub == msub)
1296 -1, 1289 {
1297 GNUNET_NO); 1290 GNUNET_STATISTICS_update(stats,
1291 "# pending online checks",
1292 -1,
1293 GNUNET_NO);
1294 }
1298 } 1295 }
1299 } 1296 remove_pending_message(peer_ctx->pending_messages_head,
1300 remove_pending_message (peer_ctx->pending_messages_head, 1297 GNUNET_YES);
1301 GNUNET_YES); 1298 }
1302 }
1303 1299
1304 /* If we are still waiting for notification whether this peer is online 1300 /* If we are still waiting for notification whether this peer is online
1305 * cancel the according task */ 1301 * cancel the according task */
1306 if (NULL != peer_ctx->online_check_pending) 1302 if (NULL != peer_ctx->online_check_pending)
1307 { 1303 {
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1304 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1309 "Removing pending online check for peer %s\n", 1305 "Removing pending online check for peer %s\n",
1310 GNUNET_i2s (&peer_ctx->peer_id)); 1306 GNUNET_i2s(&peer_ctx->peer_id));
1311 // TODO wait until cadet sets mq->cancel_impl 1307 // TODO wait until cadet sets mq->cancel_impl
1312 //GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev); 1308 //GNUNET_MQ_send_cancel (peer_ctx->online_check_pending->ev);
1313 remove_pending_message (peer_ctx->online_check_pending, 1309 remove_pending_message(peer_ctx->online_check_pending,
1314 GNUNET_YES); 1310 GNUNET_YES);
1315 peer_ctx->online_check_pending = NULL; 1311 peer_ctx->online_check_pending = NULL;
1316 } 1312 }
1317 1313
1318 if (NULL != peer_ctx->send_channel_ctx) 1314 if (NULL != peer_ctx->send_channel_ctx)
1319 { 1315 {
1320 /* This is possibly called from within channel destruction */ 1316 /* This is possibly called from within channel destruction */
1321 peer_ctx->send_channel_ctx->peer_ctx = NULL; 1317 peer_ctx->send_channel_ctx->peer_ctx = NULL;
1322 schedule_channel_destruction (peer_ctx->send_channel_ctx); 1318 schedule_channel_destruction(peer_ctx->send_channel_ctx);
1323 peer_ctx->send_channel_ctx = NULL; 1319 peer_ctx->send_channel_ctx = NULL;
1324 peer_ctx->mq = NULL; 1320 peer_ctx->mq = NULL;
1325 } 1321 }
1326 if (NULL != peer_ctx->recv_channel_ctx) 1322 if (NULL != peer_ctx->recv_channel_ctx)
1327 { 1323 {
1328 /* This is possibly called from within channel destruction */ 1324 /* This is possibly called from within channel destruction */
1329 peer_ctx->recv_channel_ctx->peer_ctx = NULL; 1325 peer_ctx->recv_channel_ctx->peer_ctx = NULL;
1330 schedule_channel_destruction (peer_ctx->recv_channel_ctx); 1326 schedule_channel_destruction(peer_ctx->recv_channel_ctx);
1331 peer_ctx->recv_channel_ctx = NULL; 1327 peer_ctx->recv_channel_ctx = NULL;
1332 } 1328 }
1333 1329
1334 if (GNUNET_YES != 1330 if (GNUNET_YES !=
1335 GNUNET_CONTAINER_multipeermap_remove_all (peer_ctx->sub->peer_map, 1331 GNUNET_CONTAINER_multipeermap_remove_all(peer_ctx->sub->peer_map,
1336 &peer_ctx->peer_id)) 1332 &peer_ctx->peer_id))
1337 { 1333 {
1338 LOG (GNUNET_ERROR_TYPE_WARNING, 1334 LOG(GNUNET_ERROR_TYPE_WARNING,
1339 "removing peer from peer_ctx->sub->peer_map failed\n"); 1335 "removing peer from peer_ctx->sub->peer_map failed\n");
1340 } 1336 }
1341 if (peer_ctx->sub == msub) 1337 if (peer_ctx->sub == msub)
1342 { 1338 {
1343 GNUNET_STATISTICS_set (stats, 1339 GNUNET_STATISTICS_set(stats,
1344 "# known peers", 1340 "# known peers",
1345 GNUNET_CONTAINER_multipeermap_size (peer_ctx->sub->peer_map), 1341 GNUNET_CONTAINER_multipeermap_size(peer_ctx->sub->peer_map),
1346 GNUNET_NO); 1342 GNUNET_NO);
1347 } 1343 }
1348 GNUNET_free (peer_ctx); 1344 GNUNET_free(peer_ctx);
1349 return GNUNET_YES; 1345 return GNUNET_YES;
1350} 1346}
1351 1347
@@ -1360,14 +1356,15 @@ destroy_peer (struct PeerContext *peer_ctx)
1360 * #GNUNET_NO if not. 1356 * #GNUNET_NO if not.
1361 */ 1357 */
1362static int 1358static int
1363peermap_clear_iterator (void *cls, 1359peermap_clear_iterator(void *cls,
1364 const struct GNUNET_PeerIdentity *key, 1360 const struct GNUNET_PeerIdentity *key,
1365 void *value) 1361 void *value)
1366{ 1362{
1367 struct Sub *sub = cls; 1363 struct Sub *sub = cls;
1368 (void) value;
1369 1364
1370 destroy_peer (get_peer_ctx (sub->peer_map, key)); 1365 (void)value;
1366
1367 destroy_peer(get_peer_ctx(sub->peer_map, key));
1371 return GNUNET_YES; 1368 return GNUNET_YES;
1372} 1369}
1373 1370
@@ -1380,31 +1377,32 @@ peermap_clear_iterator (void *cls,
1380 * @param cls type of the message that was sent 1377 * @param cls type of the message that was sent
1381 */ 1378 */
1382static void 1379static void
1383mq_notify_sent_cb (void *cls) 1380mq_notify_sent_cb(void *cls)
1384{ 1381{
1385 struct PendingMessage *pending_msg = (struct PendingMessage *) cls; 1382 struct PendingMessage *pending_msg = (struct PendingMessage *)cls;
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, 1383
1384 LOG(GNUNET_ERROR_TYPE_DEBUG,
1387 "%s was sent.\n", 1385 "%s was sent.\n",
1388 pending_msg->type); 1386 pending_msg->type);
1389 if (pending_msg->peer_ctx->sub == msub) 1387 if (pending_msg->peer_ctx->sub == msub)
1390 { 1388 {
1391 if (0 == strncmp ("PULL REPLY", pending_msg->type, 10)) 1389 if (0 == strncmp("PULL REPLY", pending_msg->type, 10))
1392 GNUNET_STATISTICS_update(stats, "# pull replys sent", 1, GNUNET_NO); 1390 GNUNET_STATISTICS_update(stats, "# pull replys sent", 1, GNUNET_NO);
1393 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12)) 1391 if (0 == strncmp("PULL REQUEST", pending_msg->type, 12))
1394 GNUNET_STATISTICS_update(stats, "# pull requests sent", 1, GNUNET_NO); 1392 GNUNET_STATISTICS_update(stats, "# pull requests sent", 1, GNUNET_NO);
1395 if (0 == strncmp ("PUSH", pending_msg->type, 4)) 1393 if (0 == strncmp("PUSH", pending_msg->type, 4))
1396 GNUNET_STATISTICS_update(stats, "# pushes sent", 1, GNUNET_NO); 1394 GNUNET_STATISTICS_update(stats, "# pushes sent", 1, GNUNET_NO);
1397 if (0 == strncmp ("PULL REQUEST", pending_msg->type, 12) && 1395 if (0 == strncmp("PULL REQUEST", pending_msg->type, 12) &&
1398 NULL != map_single_hop && 1396 NULL != map_single_hop &&
1399 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop, 1397 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
1400 &pending_msg->peer_ctx->peer_id)) 1398 &pending_msg->peer_ctx->peer_id))
1401 GNUNET_STATISTICS_update(stats, 1399 GNUNET_STATISTICS_update(stats,
1402 "# pull requests sent (multi-hop peer)", 1400 "# pull requests sent (multi-hop peer)",
1403 1, 1401 1,
1404 GNUNET_NO); 1402 GNUNET_NO);
1405 } 1403 }
1406 /* Do not cancle message */ 1404 /* Do not cancle message */
1407 remove_pending_message (pending_msg, GNUNET_NO); 1405 remove_pending_message(pending_msg, GNUNET_NO);
1408} 1406}
1409 1407
1410 1408
@@ -1423,29 +1421,30 @@ mq_notify_sent_cb (void *cls)
1423 * #GNUNET_NO if not. 1421 * #GNUNET_NO if not.
1424 */ 1422 */
1425static int 1423static int
1426store_peer_presistently_iterator (void *cls, 1424store_peer_presistently_iterator(void *cls,
1427 const struct GNUNET_PeerIdentity *peer, 1425 const struct GNUNET_PeerIdentity *peer,
1428 void *value) 1426 void *value)
1429{ 1427{
1430 const struct GNUNET_DISK_FileHandle *fh = cls; 1428 const struct GNUNET_DISK_FileHandle *fh = cls;
1431 char peer_string[128]; 1429 char peer_string[128];
1432 int size; 1430 int size;
1433 ssize_t ret; 1431 ssize_t ret;
1434 (void) value; 1432
1433 (void)value;
1435 1434
1436 if (NULL == peer) 1435 if (NULL == peer)
1437 { 1436 {
1438 return GNUNET_YES; 1437 return GNUNET_YES;
1439 } 1438 }
1440 size = GNUNET_snprintf (peer_string, 1439 size = GNUNET_snprintf(peer_string,
1441 sizeof (peer_string), 1440 sizeof(peer_string),
1442 "%s\n", 1441 "%s\n",
1443 GNUNET_i2s_full (peer)); 1442 GNUNET_i2s_full(peer));
1444 GNUNET_assert (53 == size); 1443 GNUNET_assert(53 == size);
1445 ret = GNUNET_DISK_file_write (fh, 1444 ret = GNUNET_DISK_file_write(fh,
1446 peer_string, 1445 peer_string,
1447 size); 1446 size);
1448 GNUNET_assert (size == ret); 1447 GNUNET_assert(size == ret);
1449 return GNUNET_YES; 1448 return GNUNET_YES;
1450} 1449}
1451 1450
@@ -1456,54 +1455,54 @@ store_peer_presistently_iterator (void *cls,
1456 * @param sub Sub for which to store the valid peers 1455 * @param sub Sub for which to store the valid peers
1457 */ 1456 */
1458static void 1457static void
1459store_valid_peers (const struct Sub *sub) 1458store_valid_peers(const struct Sub *sub)
1460{ 1459{
1461 struct GNUNET_DISK_FileHandle *fh; 1460 struct GNUNET_DISK_FileHandle *fh;
1462 uint32_t number_written_peers; 1461 uint32_t number_written_peers;
1463 int ret; 1462 int ret;
1464 1463
1465 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7)) 1464 if (0 == strncmp("DISABLE", sub->filename_valid_peers, 7))
1466 { 1465 {
1467 return; 1466 return;
1468 } 1467 }
1469 1468
1470 ret = GNUNET_DISK_directory_create_for_file (sub->filename_valid_peers); 1469 ret = GNUNET_DISK_directory_create_for_file(sub->filename_valid_peers);
1471 if (GNUNET_SYSERR == ret) 1470 if (GNUNET_SYSERR == ret)
1472 { 1471 {
1473 LOG (GNUNET_ERROR_TYPE_WARNING, 1472 LOG(GNUNET_ERROR_TYPE_WARNING,
1474 "Not able to create directory for file `%s'\n", 1473 "Not able to create directory for file `%s'\n",
1475 sub->filename_valid_peers); 1474 sub->filename_valid_peers);
1476 GNUNET_break (0); 1475 GNUNET_break(0);
1477 } 1476 }
1478 else if (GNUNET_NO == ret) 1477 else if (GNUNET_NO == ret)
1479 { 1478 {
1480 LOG (GNUNET_ERROR_TYPE_WARNING, 1479 LOG(GNUNET_ERROR_TYPE_WARNING,
1481 "Directory for file `%s' exists but is not writable for us\n", 1480 "Directory for file `%s' exists but is not writable for us\n",
1482 sub->filename_valid_peers); 1481 sub->filename_valid_peers);
1483 GNUNET_break (0); 1482 GNUNET_break(0);
1484 } 1483 }
1485 fh = GNUNET_DISK_file_open (sub->filename_valid_peers, 1484 fh = GNUNET_DISK_file_open(sub->filename_valid_peers,
1486 GNUNET_DISK_OPEN_WRITE | 1485 GNUNET_DISK_OPEN_WRITE |
1487 GNUNET_DISK_OPEN_CREATE, 1486 GNUNET_DISK_OPEN_CREATE,
1488 GNUNET_DISK_PERM_USER_READ | 1487 GNUNET_DISK_PERM_USER_READ |
1489 GNUNET_DISK_PERM_USER_WRITE); 1488 GNUNET_DISK_PERM_USER_WRITE);
1490 if (NULL == fh) 1489 if (NULL == fh)
1491 { 1490 {
1492 LOG (GNUNET_ERROR_TYPE_WARNING, 1491 LOG(GNUNET_ERROR_TYPE_WARNING,
1493 "Not able to write valid peers to file `%s'\n", 1492 "Not able to write valid peers to file `%s'\n",
1494 sub->filename_valid_peers); 1493 sub->filename_valid_peers);
1495 return; 1494 return;
1496 } 1495 }
1497 LOG (GNUNET_ERROR_TYPE_DEBUG, 1496 LOG(GNUNET_ERROR_TYPE_DEBUG,
1498 "Writing %u valid peers to disk\n", 1497 "Writing %u valid peers to disk\n",
1499 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); 1498 GNUNET_CONTAINER_multipeermap_size(sub->valid_peers));
1500 number_written_peers = 1499 number_written_peers =
1501 GNUNET_CONTAINER_multipeermap_iterate (sub->valid_peers, 1500 GNUNET_CONTAINER_multipeermap_iterate(sub->valid_peers,
1502 store_peer_presistently_iterator, 1501 store_peer_presistently_iterator,
1503 fh); 1502 fh);
1504 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); 1503 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
1505 GNUNET_assert (number_written_peers == 1504 GNUNET_assert(number_written_peers ==
1506 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); 1505 GNUNET_CONTAINER_multipeermap_size(sub->valid_peers));
1507} 1506}
1508 1507
1509 1508
@@ -1517,38 +1516,38 @@ store_valid_peers (const struct Sub *sub)
1517 * @return The peer id 1516 * @return The peer id
1518 */ 1517 */
1519static const struct GNUNET_PeerIdentity * 1518static const struct GNUNET_PeerIdentity *
1520s2i_full (const char *string_repr) 1519s2i_full(const char *string_repr)
1521{ 1520{
1522 struct GNUNET_PeerIdentity *peer; 1521 struct GNUNET_PeerIdentity *peer;
1523 size_t len; 1522 size_t len;
1524 int ret; 1523 int ret;
1525 1524
1526 peer = GNUNET_new (struct GNUNET_PeerIdentity); 1525 peer = GNUNET_new(struct GNUNET_PeerIdentity);
1527 len = strlen (string_repr); 1526 len = strlen(string_repr);
1528 if (52 > len) 1527 if (52 > len)
1529 { 1528 {
1530 LOG (GNUNET_ERROR_TYPE_WARNING, 1529 LOG(GNUNET_ERROR_TYPE_WARNING,
1531 "Not able to convert string representation of PeerID to PeerID\n" 1530 "Not able to convert string representation of PeerID to PeerID\n"
1532 "Sting representation: %s (len %lu) - too short\n", 1531 "Sting representation: %s (len %lu) - too short\n",
1533 string_repr, 1532 string_repr,
1534 len); 1533 len);
1535 GNUNET_break (0); 1534 GNUNET_break(0);
1536 } 1535 }
1537 else if (52 < len) 1536 else if (52 < len)
1538 { 1537 {
1539 len = 52; 1538 len = 52;
1540 } 1539 }
1541 ret = GNUNET_CRYPTO_eddsa_public_key_from_string (string_repr, 1540 ret = GNUNET_CRYPTO_eddsa_public_key_from_string(string_repr,
1542 len, 1541 len,
1543 &peer->public_key); 1542 &peer->public_key);
1544 if (GNUNET_OK != ret) 1543 if (GNUNET_OK != ret)
1545 { 1544 {
1546 LOG (GNUNET_ERROR_TYPE_WARNING, 1545 LOG(GNUNET_ERROR_TYPE_WARNING,
1547 "Not able to convert string representation of PeerID to PeerID\n" 1546 "Not able to convert string representation of PeerID to PeerID\n"
1548 "Sting representation: %s\n", 1547 "Sting representation: %s\n",
1549 string_repr); 1548 string_repr);
1550 GNUNET_break (0); 1549 GNUNET_break(0);
1551 } 1550 }
1552 return peer; 1551 return peer;
1553} 1552}
1554 1553
@@ -1559,7 +1558,7 @@ s2i_full (const char *string_repr)
1559 * @param sub Sub for which to restore the valid peers 1558 * @param sub Sub for which to restore the valid peers
1560 */ 1559 */
1561static void 1560static void
1562restore_valid_peers (const struct Sub *sub) 1561restore_valid_peers(const struct Sub *sub)
1563{ 1562{
1564 off_t file_size; 1563 off_t file_size;
1565 uint32_t num_peers; 1564 uint32_t num_peers;
@@ -1570,53 +1569,53 @@ restore_valid_peers (const struct Sub *sub)
1570 char *str_repr; 1569 char *str_repr;
1571 const struct GNUNET_PeerIdentity *peer; 1570 const struct GNUNET_PeerIdentity *peer;
1572 1571
1573 if (0 == strncmp ("DISABLE", sub->filename_valid_peers, 7)) 1572 if (0 == strncmp("DISABLE", sub->filename_valid_peers, 7))
1574 { 1573 {
1575 return; 1574 return;
1576 } 1575 }
1577 1576
1578 if (GNUNET_OK != GNUNET_DISK_file_test (sub->filename_valid_peers)) 1577 if (GNUNET_OK != GNUNET_DISK_file_test(sub->filename_valid_peers))
1579 { 1578 {
1580 return; 1579 return;
1581 } 1580 }
1582 fh = GNUNET_DISK_file_open (sub->filename_valid_peers, 1581 fh = GNUNET_DISK_file_open(sub->filename_valid_peers,
1583 GNUNET_DISK_OPEN_READ, 1582 GNUNET_DISK_OPEN_READ,
1584 GNUNET_DISK_PERM_NONE); 1583 GNUNET_DISK_PERM_NONE);
1585 GNUNET_assert (NULL != fh); 1584 GNUNET_assert(NULL != fh);
1586 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &file_size)); 1585 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_handle_size(fh, &file_size));
1587 num_peers = file_size / 53; 1586 num_peers = file_size / 53;
1588 buf = GNUNET_malloc (file_size); 1587 buf = GNUNET_malloc(file_size);
1589 size_read = GNUNET_DISK_file_read (fh, buf, file_size); 1588 size_read = GNUNET_DISK_file_read(fh, buf, file_size);
1590 GNUNET_assert (size_read == file_size); 1589 GNUNET_assert(size_read == file_size);
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, 1590 LOG(GNUNET_ERROR_TYPE_DEBUG,
1592 "Restoring %" PRIu32 " peers from file `%s'\n", 1591 "Restoring %" PRIu32 " peers from file `%s'\n",
1593 num_peers, 1592 num_peers,
1594 sub->filename_valid_peers); 1593 sub->filename_valid_peers);
1595 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53) 1594 for (iter_buf = buf; iter_buf < buf + file_size - 1; iter_buf += 53)
1596 { 1595 {
1597 str_repr = GNUNET_strndup (iter_buf, 53); 1596 str_repr = GNUNET_strndup(iter_buf, 53);
1598 peer = s2i_full (str_repr); 1597 peer = s2i_full(str_repr);
1599 GNUNET_free (str_repr); 1598 GNUNET_free(str_repr);
1600 add_valid_peer (peer, sub->valid_peers); 1599 add_valid_peer(peer, sub->valid_peers);
1601 LOG (GNUNET_ERROR_TYPE_DEBUG, 1600 LOG(GNUNET_ERROR_TYPE_DEBUG,
1602 "Restored valid peer %s from disk\n", 1601 "Restored valid peer %s from disk\n",
1603 GNUNET_i2s_full (peer)); 1602 GNUNET_i2s_full(peer));
1604 } 1603 }
1605 iter_buf = NULL; 1604 iter_buf = NULL;
1606 GNUNET_free (buf); 1605 GNUNET_free(buf);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, 1606 LOG(GNUNET_ERROR_TYPE_DEBUG,
1608 "num_peers: %" PRIu32 ", _size (sub->valid_peers): %u\n", 1607 "num_peers: %" PRIu32 ", _size (sub->valid_peers): %u\n",
1609 num_peers, 1608 num_peers,
1610 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); 1609 GNUNET_CONTAINER_multipeermap_size(sub->valid_peers));
1611 if (num_peers != GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)) 1610 if (num_peers != GNUNET_CONTAINER_multipeermap_size(sub->valid_peers))
1612 { 1611 {
1613 LOG (GNUNET_ERROR_TYPE_WARNING, 1612 LOG(GNUNET_ERROR_TYPE_WARNING,
1614 "Number of restored peers does not match file size. Have probably duplicates.\n"); 1613 "Number of restored peers does not match file size. Have probably duplicates.\n");
1615 } 1614 }
1616 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); 1615 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fh));
1617 LOG (GNUNET_ERROR_TYPE_DEBUG, 1616 LOG(GNUNET_ERROR_TYPE_DEBUG,
1618 "Restored %u valid peers from disk\n", 1617 "Restored %u valid peers from disk\n",
1619 GNUNET_CONTAINER_multipeermap_size (sub->valid_peers)); 1618 GNUNET_CONTAINER_multipeermap_size(sub->valid_peers));
1620} 1619}
1621 1620
1622 1621
@@ -1626,22 +1625,22 @@ restore_valid_peers (const struct Sub *sub)
1626 * @param sub Sub for which the storage is deleted 1625 * @param sub Sub for which the storage is deleted
1627 */ 1626 */
1628static void 1627static void
1629peers_terminate (struct Sub *sub) 1628peers_terminate(struct Sub *sub)
1630{ 1629{
1631 if (GNUNET_SYSERR == 1630 if (GNUNET_SYSERR ==
1632 GNUNET_CONTAINER_multipeermap_iterate (sub->peer_map, 1631 GNUNET_CONTAINER_multipeermap_iterate(sub->peer_map,
1633 &peermap_clear_iterator, 1632 &peermap_clear_iterator,
1634 sub)) 1633 sub))
1635 { 1634 {
1636 LOG (GNUNET_ERROR_TYPE_WARNING, 1635 LOG(GNUNET_ERROR_TYPE_WARNING,
1637 "Iteration destroying peers was aborted.\n"); 1636 "Iteration destroying peers was aborted.\n");
1638 } 1637 }
1639 GNUNET_CONTAINER_multipeermap_destroy (sub->peer_map); 1638 GNUNET_CONTAINER_multipeermap_destroy(sub->peer_map);
1640 sub->peer_map = NULL; 1639 sub->peer_map = NULL;
1641 store_valid_peers (sub); 1640 store_valid_peers(sub);
1642 GNUNET_free (sub->filename_valid_peers); 1641 GNUNET_free(sub->filename_valid_peers);
1643 sub->filename_valid_peers = NULL; 1642 sub->filename_valid_peers = NULL;
1644 GNUNET_CONTAINER_multipeermap_destroy (sub->valid_peers); 1643 GNUNET_CONTAINER_multipeermap_destroy(sub->valid_peers);
1645 sub->valid_peers = NULL; 1644 sub->valid_peers = NULL;
1646} 1645}
1647 1646
@@ -1657,14 +1656,15 @@ peers_terminate (struct Sub *sub)
1657 * #GNUNET_NO if not. 1656 * #GNUNET_NO if not.
1658 */ 1657 */
1659static int 1658static int
1660valid_peer_iterator (void *cls, 1659valid_peer_iterator(void *cls,
1661 const struct GNUNET_PeerIdentity *peer, 1660 const struct GNUNET_PeerIdentity *peer,
1662 void *value) 1661 void *value)
1663{ 1662{
1664 struct PeersIteratorCls *it_cls = cls; 1663 struct PeersIteratorCls *it_cls = cls;
1665 (void) value;
1666 1664
1667 return it_cls->iterator (it_cls->cls, peer); 1665 (void)value;
1666
1667 return it_cls->iterator(it_cls->cls, peer);
1668} 1668}
1669 1669
1670 1670
@@ -1678,20 +1678,20 @@ valid_peer_iterator (void *cls,
1678 * #GNUNET_SYSERR if it aborted iteration 1678 * #GNUNET_SYSERR if it aborted iteration
1679 */ 1679 */
1680static int 1680static int
1681get_valid_peers (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers, 1681get_valid_peers(struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1682 PeersIterator iterator, 1682 PeersIterator iterator,
1683 void *it_cls) 1683 void *it_cls)
1684{ 1684{
1685 struct PeersIteratorCls *cls; 1685 struct PeersIteratorCls *cls;
1686 int ret; 1686 int ret;
1687 1687
1688 cls = GNUNET_new (struct PeersIteratorCls); 1688 cls = GNUNET_new(struct PeersIteratorCls);
1689 cls->iterator = iterator; 1689 cls->iterator = iterator;
1690 cls->cls = it_cls; 1690 cls->cls = it_cls;
1691 ret = GNUNET_CONTAINER_multipeermap_iterate (valid_peers, 1691 ret = GNUNET_CONTAINER_multipeermap_iterate(valid_peers,
1692 valid_peer_iterator, 1692 valid_peer_iterator,
1693 cls); 1693 cls);
1694 GNUNET_free (cls); 1694 GNUNET_free(cls);
1695 return ret; 1695 return ret;
1696} 1696}
1697 1697
@@ -1709,14 +1709,14 @@ get_valid_peers (struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1709 * #GNUNET_NO otherwise 1709 * #GNUNET_NO otherwise
1710 */ 1710 */
1711static int 1711static int
1712insert_peer (struct Sub *sub, 1712insert_peer(struct Sub *sub,
1713 const struct GNUNET_PeerIdentity *peer) 1713 const struct GNUNET_PeerIdentity *peer)
1714{ 1714{
1715 if (GNUNET_YES == check_peer_known (sub->peer_map, peer)) 1715 if (GNUNET_YES == check_peer_known(sub->peer_map, peer))
1716 { 1716 {
1717 return GNUNET_NO; /* We already know this peer - nothing to do */ 1717 return GNUNET_NO; /* We already know this peer - nothing to do */
1718 } 1718 }
1719 (void) create_peer_ctx (sub, peer); 1719 (void)create_peer_ctx(sub, peer);
1720 return GNUNET_YES; 1720 return GNUNET_YES;
1721} 1721}
1722 1722
@@ -1733,18 +1733,18 @@ insert_peer (struct Sub *sub,
1733 * #GNUNET_NO otherwise 1733 * #GNUNET_NO otherwise
1734 */ 1734 */
1735static int 1735static int
1736check_peer_flag (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map, 1736check_peer_flag(const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
1737 const struct GNUNET_PeerIdentity *peer, 1737 const struct GNUNET_PeerIdentity *peer,
1738 enum Peers_PeerFlags flags) 1738 enum Peers_PeerFlags flags)
1739{ 1739{
1740 struct PeerContext *peer_ctx; 1740 struct PeerContext *peer_ctx;
1741 1741
1742 if (GNUNET_NO == check_peer_known (peer_map, peer)) 1742 if (GNUNET_NO == check_peer_known(peer_map, peer))
1743 { 1743 {
1744 return GNUNET_SYSERR; 1744 return GNUNET_SYSERR;
1745 } 1745 }
1746 peer_ctx = get_peer_ctx (peer_map, peer); 1746 peer_ctx = get_peer_ctx(peer_map, peer);
1747 return check_peer_flag_set (peer_ctx, flags); 1747 return check_peer_flag_set(peer_ctx, flags);
1748} 1748}
1749 1749
1750/** 1750/**
@@ -1758,19 +1758,19 @@ check_peer_flag (const struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
1758 * #GNUNET_NO otherwise 1758 * #GNUNET_NO otherwise
1759 */ 1759 */
1760static int 1760static int
1761issue_peer_online_check (struct Sub *sub, 1761issue_peer_online_check(struct Sub *sub,
1762 const struct GNUNET_PeerIdentity *peer) 1762 const struct GNUNET_PeerIdentity *peer)
1763{ 1763{
1764 struct PeerContext *peer_ctx; 1764 struct PeerContext *peer_ctx;
1765 1765
1766 (void) insert_peer (sub, peer); // TODO even needed? 1766 (void)insert_peer(sub, peer); // TODO even needed?
1767 peer_ctx = get_peer_ctx (sub->peer_map, peer); 1767 peer_ctx = get_peer_ctx(sub->peer_map, peer);
1768 if ( (GNUNET_NO == check_peer_flag (sub->peer_map, peer, Peers_ONLINE)) && 1768 if ((GNUNET_NO == check_peer_flag(sub->peer_map, peer, Peers_ONLINE)) &&
1769 (NULL == peer_ctx->online_check_pending) ) 1769 (NULL == peer_ctx->online_check_pending))
1770 { 1770 {
1771 check_peer_online (peer_ctx); 1771 check_peer_online(peer_ctx);
1772 return GNUNET_YES; 1772 return GNUNET_YES;
1773 } 1773 }
1774 return GNUNET_NO; 1774 return GNUNET_NO;
1775} 1775}
1776 1776
@@ -1789,20 +1789,20 @@ issue_peer_online_check (struct Sub *sub,
1789 * #GNUNET_SYSERR if peer is not known 1789 * #GNUNET_SYSERR if peer is not known
1790 */ 1790 */
1791static int 1791static int
1792check_removable (const struct PeerContext *peer_ctx) 1792check_removable(const struct PeerContext *peer_ctx)
1793{ 1793{
1794 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_ctx->sub->peer_map, 1794 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(peer_ctx->sub->peer_map,
1795 &peer_ctx->peer_id)) 1795 &peer_ctx->peer_id))
1796 { 1796 {
1797 return GNUNET_SYSERR; 1797 return GNUNET_SYSERR;
1798 } 1798 }
1799 1799
1800 if ( (NULL != peer_ctx->recv_channel_ctx) || 1800 if ((NULL != peer_ctx->recv_channel_ctx) ||
1801 (NULL != peer_ctx->pending_messages_head) || 1801 (NULL != peer_ctx->pending_messages_head) ||
1802 (GNUNET_YES == check_peer_flag_set (peer_ctx, Peers_PULL_REPLY_PENDING)) ) 1802 (GNUNET_YES == check_peer_flag_set(peer_ctx, Peers_PULL_REPLY_PENDING)))
1803 { 1803 {
1804 return GNUNET_NO; 1804 return GNUNET_NO;
1805 } 1805 }
1806 return GNUNET_YES; 1806 return GNUNET_YES;
1807} 1807}
1808 1808
@@ -1819,10 +1819,10 @@ check_removable (const struct PeerContext *peer_ctx)
1819 * #GNUNET_NO if peer is not valid 1819 * #GNUNET_NO if peer is not valid
1820 */ 1820 */
1821static int 1821static int
1822check_peer_valid (const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers, 1822check_peer_valid(const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1823 const struct GNUNET_PeerIdentity *peer) 1823 const struct GNUNET_PeerIdentity *peer)
1824{ 1824{
1825 return GNUNET_CONTAINER_multipeermap_contains (valid_peers, peer); 1825 return GNUNET_CONTAINER_multipeermap_contains(valid_peers, peer);
1826} 1826}
1827 1827
1828 1828
@@ -1834,11 +1834,11 @@ check_peer_valid (const struct GNUNET_CONTAINER_MultiPeerMap *valid_peers,
1834 * @param peer_ctx Context of the target peer 1834 * @param peer_ctx Context of the target peer
1835 */ 1835 */
1836static void 1836static void
1837indicate_sending_intention (struct PeerContext *peer_ctx) 1837indicate_sending_intention(struct PeerContext *peer_ctx)
1838{ 1838{
1839 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map, 1839 GNUNET_assert(GNUNET_YES == check_peer_known(peer_ctx->sub->peer_map,
1840 &peer_ctx->peer_id)); 1840 &peer_ctx->peer_id));
1841 (void) get_channel (peer_ctx); 1841 (void)get_channel(peer_ctx);
1842} 1842}
1843 1843
1844 1844
@@ -1852,12 +1852,12 @@ indicate_sending_intention (struct PeerContext *peer_ctx)
1852 * #GNUNET_NO otherwise 1852 * #GNUNET_NO otherwise
1853 */ 1853 */
1854static int 1854static int
1855check_peer_send_intention (const struct PeerContext *peer_ctx) 1855check_peer_send_intention(const struct PeerContext *peer_ctx)
1856{ 1856{
1857 if (NULL != peer_ctx->recv_channel_ctx) 1857 if (NULL != peer_ctx->recv_channel_ctx)
1858 { 1858 {
1859 return GNUNET_YES; 1859 return GNUNET_YES;
1860 } 1860 }
1861 return GNUNET_NO; 1861 return GNUNET_NO;
1862} 1862}
1863 1863
@@ -1873,36 +1873,36 @@ check_peer_send_intention (const struct PeerContext *peer_ctx)
1873 * (can be NULL -- that's not an error) 1873 * (can be NULL -- that's not an error)
1874 */ 1874 */
1875static void * 1875static void *
1876handle_inbound_channel (void *cls, 1876handle_inbound_channel(void *cls,
1877 struct GNUNET_CADET_Channel *channel, 1877 struct GNUNET_CADET_Channel *channel,
1878 const struct GNUNET_PeerIdentity *initiator) 1878 const struct GNUNET_PeerIdentity *initiator)
1879{ 1879{
1880 struct PeerContext *peer_ctx; 1880 struct PeerContext *peer_ctx;
1881 struct ChannelCtx *channel_ctx; 1881 struct ChannelCtx *channel_ctx;
1882 struct Sub *sub = cls; 1882 struct Sub *sub = cls;
1883 1883
1884 LOG (GNUNET_ERROR_TYPE_DEBUG, 1884 LOG(GNUNET_ERROR_TYPE_DEBUG,
1885 "New channel was established to us (Peer %s).\n", 1885 "New channel was established to us (Peer %s).\n",
1886 GNUNET_i2s (initiator)); 1886 GNUNET_i2s(initiator));
1887 GNUNET_assert (NULL != channel); /* according to cadet API */ 1887 GNUNET_assert(NULL != channel); /* according to cadet API */
1888 /* Make sure we 'know' about this peer */ 1888 /* Make sure we 'know' about this peer */
1889 peer_ctx = create_or_get_peer_ctx (sub, initiator); 1889 peer_ctx = create_or_get_peer_ctx(sub, initiator);
1890 set_peer_online (peer_ctx); 1890 set_peer_online(peer_ctx);
1891 (void) add_valid_peer (&peer_ctx->peer_id, peer_ctx->sub->valid_peers); 1891 (void)add_valid_peer(&peer_ctx->peer_id, peer_ctx->sub->valid_peers);
1892 channel_ctx = add_channel_ctx (peer_ctx); 1892 channel_ctx = add_channel_ctx(peer_ctx);
1893 channel_ctx->channel = channel; 1893 channel_ctx->channel = channel;
1894 /* We only accept one incoming channel per peer */ 1894 /* We only accept one incoming channel per peer */
1895 if (GNUNET_YES == check_peer_send_intention (get_peer_ctx (sub->peer_map, 1895 if (GNUNET_YES == check_peer_send_intention(get_peer_ctx(sub->peer_map,
1896 initiator))) 1896 initiator)))
1897 { 1897 {
1898 LOG (GNUNET_ERROR_TYPE_WARNING, 1898 LOG(GNUNET_ERROR_TYPE_WARNING,
1899 "Already got one receive channel. Destroying old one.\n"); 1899 "Already got one receive channel. Destroying old one.\n");
1900 GNUNET_break_op (0); 1900 GNUNET_break_op(0);
1901 destroy_channel (peer_ctx->recv_channel_ctx); 1901 destroy_channel(peer_ctx->recv_channel_ctx);
1902 peer_ctx->recv_channel_ctx = channel_ctx; 1902 peer_ctx->recv_channel_ctx = channel_ctx;
1903 /* return the channel context */ 1903 /* return the channel context */
1904 return channel_ctx; 1904 return channel_ctx;
1905 } 1905 }
1906 peer_ctx->recv_channel_ctx = channel_ctx; 1906 peer_ctx->recv_channel_ctx = channel_ctx;
1907 return channel_ctx; 1907 return channel_ctx;
1908} 1908}
@@ -1917,17 +1917,17 @@ handle_inbound_channel (void *cls,
1917 * #GNUNET_NO otherwise 1917 * #GNUNET_NO otherwise
1918 */ 1918 */
1919static int 1919static int
1920check_sending_channel_exists (const struct PeerContext *peer_ctx) 1920check_sending_channel_exists(const struct PeerContext *peer_ctx)
1921{ 1921{
1922 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, 1922 if (GNUNET_NO == check_peer_known(peer_ctx->sub->peer_map,
1923 &peer_ctx->peer_id)) 1923 &peer_ctx->peer_id))
1924 { /* If no such peer exists, there is no channel */ 1924 { /* If no such peer exists, there is no channel */
1925 return GNUNET_NO; 1925 return GNUNET_NO;
1926 } 1926 }
1927 if (NULL == peer_ctx->send_channel_ctx) 1927 if (NULL == peer_ctx->send_channel_ctx)
1928 { 1928 {
1929 return GNUNET_NO; 1929 return GNUNET_NO;
1930 } 1930 }
1931 return GNUNET_YES; 1931 return GNUNET_YES;
1932} 1932}
1933 1933
@@ -1941,19 +1941,19 @@ check_sending_channel_exists (const struct PeerContext *peer_ctx)
1941 * #GNUNET_NO otherwise 1941 * #GNUNET_NO otherwise
1942 */ 1942 */
1943static int 1943static int
1944destroy_sending_channel (struct PeerContext *peer_ctx) 1944destroy_sending_channel(struct PeerContext *peer_ctx)
1945{ 1945{
1946 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, 1946 if (GNUNET_NO == check_peer_known(peer_ctx->sub->peer_map,
1947 &peer_ctx->peer_id)) 1947 &peer_ctx->peer_id))
1948 { 1948 {
1949 return GNUNET_NO; 1949 return GNUNET_NO;
1950 } 1950 }
1951 if (NULL != peer_ctx->send_channel_ctx) 1951 if (NULL != peer_ctx->send_channel_ctx)
1952 { 1952 {
1953 destroy_channel (peer_ctx->send_channel_ctx); 1953 destroy_channel(peer_ctx->send_channel_ctx);
1954 (void) check_connected (peer_ctx); 1954 (void)check_connected(peer_ctx);
1955 return GNUNET_YES; 1955 return GNUNET_YES;
1956 } 1956 }
1957 return GNUNET_NO; 1957 return GNUNET_NO;
1958} 1958}
1959 1959
@@ -1968,23 +1968,23 @@ destroy_sending_channel (struct PeerContext *peer_ctx)
1968 * @param type type of the message 1968 * @param type type of the message
1969 */ 1969 */
1970static void 1970static void
1971send_message (struct PeerContext *peer_ctx, 1971send_message(struct PeerContext *peer_ctx,
1972 struct GNUNET_MQ_Envelope *ev, 1972 struct GNUNET_MQ_Envelope *ev,
1973 const char *type) 1973 const char *type)
1974{ 1974{
1975 struct PendingMessage *pending_msg; 1975 struct PendingMessage *pending_msg;
1976 struct GNUNET_MQ_Handle *mq; 1976 struct GNUNET_MQ_Handle *mq;
1977 1977
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1978 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1979 "Sending message to %s of type %s\n", 1979 "Sending message to %s of type %s\n",
1980 GNUNET_i2s (&peer_ctx->peer_id), 1980 GNUNET_i2s(&peer_ctx->peer_id),
1981 type); 1981 type);
1982 pending_msg = insert_pending_message (peer_ctx, ev, type); 1982 pending_msg = insert_pending_message(peer_ctx, ev, type);
1983 mq = get_mq (peer_ctx); 1983 mq = get_mq(peer_ctx);
1984 GNUNET_MQ_notify_sent (ev, 1984 GNUNET_MQ_notify_sent(ev,
1985 mq_notify_sent_cb, 1985 mq_notify_sent_cb,
1986 pending_msg); 1986 pending_msg);
1987 GNUNET_MQ_send (mq, ev); 1987 GNUNET_MQ_send(mq, ev);
1988} 1988}
1989 1989
1990/** 1990/**
@@ -2000,44 +2000,43 @@ send_message (struct PeerContext *peer_ctx,
2000 * #GNUNET_NO otherwise 2000 * #GNUNET_NO otherwise
2001 */ 2001 */
2002static int 2002static int
2003schedule_operation (struct PeerContext *peer_ctx, 2003schedule_operation(struct PeerContext *peer_ctx,
2004 const PeerOp peer_op, 2004 const PeerOp peer_op,
2005 void *cls) 2005 void *cls)
2006{ 2006{
2007 struct PeerPendingOp pending_op; 2007 struct PeerPendingOp pending_op;
2008 2008
2009 GNUNET_assert (GNUNET_YES == check_peer_known (peer_ctx->sub->peer_map, 2009 GNUNET_assert(GNUNET_YES == check_peer_known(peer_ctx->sub->peer_map,
2010 &peer_ctx->peer_id)); 2010 &peer_ctx->peer_id));
2011 2011
2012 //TODO if ONLINE execute immediately 2012 //TODO if ONLINE execute immediately
2013 2013
2014 if (GNUNET_NO == check_operation_scheduled (peer_ctx, peer_op)) 2014 if (GNUNET_NO == check_operation_scheduled(peer_ctx, peer_op))
2015 { 2015 {
2016 pending_op.op = peer_op; 2016 pending_op.op = peer_op;
2017 pending_op.op_cls = cls; 2017 pending_op.op_cls = cls;
2018 GNUNET_array_append (peer_ctx->pending_ops, 2018 GNUNET_array_append(peer_ctx->pending_ops,
2019 peer_ctx->num_pending_ops, 2019 peer_ctx->num_pending_ops,
2020 pending_op); 2020 pending_op);
2021 return GNUNET_YES; 2021 return GNUNET_YES;
2022 } 2022 }
2023 return GNUNET_NO; 2023 return GNUNET_NO;
2024} 2024}
2025 2025
2026/*********************************************************************** 2026/***********************************************************************
2027 * /Old gnunet-service-rps_peers.c 2027* /Old gnunet-service-rps_peers.c
2028***********************************************************************/ 2028***********************************************************************/
2029 2029
2030 2030
2031/*********************************************************************** 2031/***********************************************************************
2032 * Housekeeping with clients 2032* Housekeeping with clients
2033***********************************************************************/ 2033***********************************************************************/
2034 2034
2035/** 2035/**
2036 * Closure used to pass the client and the id to the callback 2036 * Closure used to pass the client and the id to the callback
2037 * that replies to a client's request 2037 * that replies to a client's request
2038 */ 2038 */
2039struct ReplyCls 2039struct ReplyCls {
2040{
2041 /** 2040 /**
2042 * DLL 2041 * DLL
2043 */ 2042 */
@@ -2064,8 +2063,7 @@ struct ReplyCls
2064/** 2063/**
2065 * Struct used to store the context of a connected client. 2064 * Struct used to store the context of a connected client.
2066 */ 2065 */
2067struct ClientContext 2066struct ClientContext {
2068{
2069 /** 2067 /**
2070 * DLL 2068 * DLL
2071 */ 2069 */
@@ -2106,7 +2104,7 @@ struct ClientContext *cli_ctx_head;
2106struct ClientContext *cli_ctx_tail; 2104struct ClientContext *cli_ctx_tail;
2107 2105
2108/*********************************************************************** 2106/***********************************************************************
2109 * /Housekeeping with clients 2107* /Housekeeping with clients
2110***********************************************************************/ 2108***********************************************************************/
2111 2109
2112 2110
@@ -2114,7 +2112,7 @@ struct ClientContext *cli_ctx_tail;
2114 2112
2115 2113
2116/*********************************************************************** 2114/***********************************************************************
2117 * Util functions 2115* Util functions
2118***********************************************************************/ 2116***********************************************************************/
2119 2117
2120 2118
@@ -2122,21 +2120,21 @@ struct ClientContext *cli_ctx_tail;
2122 * Print peerlist to log. 2120 * Print peerlist to log.
2123 */ 2121 */
2124static void 2122static void
2125print_peer_list (struct GNUNET_PeerIdentity *list, 2123print_peer_list(struct GNUNET_PeerIdentity *list,
2126 unsigned int len) 2124 unsigned int len)
2127{ 2125{
2128 unsigned int i; 2126 unsigned int i;
2129 2127
2130 LOG (GNUNET_ERROR_TYPE_DEBUG, 2128 LOG(GNUNET_ERROR_TYPE_DEBUG,
2131 "Printing peer list of length %u at %p:\n", 2129 "Printing peer list of length %u at %p:\n",
2132 len, 2130 len,
2133 list); 2131 list);
2134 for (i = 0 ; i < len ; i++) 2132 for (i = 0; i < len; i++)
2135 { 2133 {
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, 2134 LOG(GNUNET_ERROR_TYPE_DEBUG,
2137 "%u. peer: %s\n", 2135 "%u. peer: %s\n",
2138 i, GNUNET_i2s (&list[i])); 2136 i, GNUNET_i2s(&list[i]));
2139 } 2137 }
2140} 2138}
2141 2139
2142 2140
@@ -2144,33 +2142,33 @@ print_peer_list (struct GNUNET_PeerIdentity *list,
2144 * Remove peer from list. 2142 * Remove peer from list.
2145 */ 2143 */
2146static void 2144static void
2147rem_from_list (struct GNUNET_PeerIdentity **peer_list, 2145rem_from_list(struct GNUNET_PeerIdentity **peer_list,
2148 unsigned int *list_size, 2146 unsigned int *list_size,
2149 const struct GNUNET_PeerIdentity *peer) 2147 const struct GNUNET_PeerIdentity *peer)
2150{ 2148{
2151 unsigned int i; 2149 unsigned int i;
2152 struct GNUNET_PeerIdentity *tmp; 2150 struct GNUNET_PeerIdentity *tmp;
2153 2151
2154 tmp = *peer_list; 2152 tmp = *peer_list;
2155 2153
2156 LOG (GNUNET_ERROR_TYPE_DEBUG, 2154 LOG(GNUNET_ERROR_TYPE_DEBUG,
2157 "Removing peer %s from list at %p\n", 2155 "Removing peer %s from list at %p\n",
2158 GNUNET_i2s (peer), 2156 GNUNET_i2s(peer),
2159 tmp); 2157 tmp);
2160 2158
2161 for ( i = 0 ; i < *list_size ; i++ ) 2159 for (i = 0; i < *list_size; i++)
2162 {
2163 if (0 == GNUNET_memcmp (&tmp[i], peer))
2164 { 2160 {
2165 if (i < *list_size -1) 2161 if (0 == GNUNET_memcmp(&tmp[i], peer))
2166 { /* Not at the last entry -- shift peers left */ 2162 {
2167 memmove (&tmp[i], &tmp[i +1], 2163 if (i < *list_size - 1)
2168 ((*list_size) - i -1) * sizeof (struct GNUNET_PeerIdentity)); 2164 { /* Not at the last entry -- shift peers left */
2169 } 2165 memmove(&tmp[i], &tmp[i + 1],
2170 /* Remove last entry (should be now useless PeerID) */ 2166 ((*list_size) - i - 1) * sizeof(struct GNUNET_PeerIdentity));
2171 GNUNET_array_grow (tmp, *list_size, (*list_size) -1); 2167 }
2172 } 2168 /* Remove last entry (should be now useless PeerID) */
2173 } 2169 GNUNET_array_grow(tmp, *list_size, (*list_size) - 1);
2170 }
2171 }
2174 *peer_list = tmp; 2172 *peer_list = tmp;
2175} 2173}
2176 2174
@@ -2185,8 +2183,8 @@ rem_from_list (struct GNUNET_PeerIdentity **peer_list,
2185 * GNUNET_NO if peer was not inserted 2183 * GNUNET_NO if peer was not inserted
2186 */ 2184 */
2187static void 2185static void
2188insert_in_view_op (void *cls, 2186insert_in_view_op(void *cls,
2189 const struct GNUNET_PeerIdentity *peer); 2187 const struct GNUNET_PeerIdentity *peer);
2190 2188
2191/** 2189/**
2192 * Insert PeerID in #view 2190 * Insert PeerID in #view
@@ -2200,32 +2198,32 @@ insert_in_view_op (void *cls,
2200 * GNUNET_NO if peer was not inserted 2198 * GNUNET_NO if peer was not inserted
2201 */ 2199 */
2202static int 2200static int
2203insert_in_view (struct Sub *sub, 2201insert_in_view(struct Sub *sub,
2204 const struct GNUNET_PeerIdentity *peer) 2202 const struct GNUNET_PeerIdentity *peer)
2205{ 2203{
2206 struct PeerContext *peer_ctx; 2204 struct PeerContext *peer_ctx;
2207 int online; 2205 int online;
2208 int ret; 2206 int ret;
2209 2207
2210 online = check_peer_flag (sub->peer_map, peer, Peers_ONLINE); 2208 online = check_peer_flag(sub->peer_map, peer, Peers_ONLINE);
2211 peer_ctx = get_peer_ctx (sub->peer_map, peer); // TODO indirection needed? 2209 peer_ctx = get_peer_ctx(sub->peer_map, peer); // TODO indirection needed?
2212 if ( (GNUNET_NO == online) || 2210 if ((GNUNET_NO == online) ||
2213 (GNUNET_SYSERR == online) ) /* peer is not even known */ 2211 (GNUNET_SYSERR == online)) /* peer is not even known */
2214 { 2212 {
2215 (void) issue_peer_online_check (sub, peer); 2213 (void)issue_peer_online_check(sub, peer);
2216 (void) schedule_operation (peer_ctx, insert_in_view_op, sub); 2214 (void)schedule_operation(peer_ctx, insert_in_view_op, sub);
2217 return GNUNET_NO; 2215 return GNUNET_NO;
2218 } 2216 }
2219 /* Open channel towards peer to keep connection open */ 2217 /* Open channel towards peer to keep connection open */
2220 indicate_sending_intention (peer_ctx); 2218 indicate_sending_intention(peer_ctx);
2221 ret = View_put (sub->view, peer); 2219 ret = View_put(sub->view, peer);
2222 if (peer_ctx->sub == msub) 2220 if (peer_ctx->sub == msub)
2223 { 2221 {
2224 GNUNET_STATISTICS_set (stats, 2222 GNUNET_STATISTICS_set(stats,
2225 "view size", 2223 "view size",
2226 View_size (peer_ctx->sub->view), 2224 View_size(peer_ctx->sub->view),
2227 GNUNET_NO); 2225 GNUNET_NO);
2228 } 2226 }
2229 return ret; 2227 return ret;
2230} 2228}
2231 2229
@@ -2238,31 +2236,33 @@ insert_in_view (struct Sub *sub,
2238 * @param view_size the size of the view array (can be 0) 2236 * @param view_size the size of the view array (can be 0)
2239 */ 2237 */
2240static void 2238static void
2241send_view (const struct ClientContext *cli_ctx, 2239send_view(const struct ClientContext *cli_ctx,
2242 const struct GNUNET_PeerIdentity *view_array, 2240 const struct GNUNET_PeerIdentity *view_array,
2243 uint64_t view_size) 2241 uint64_t view_size)
2244{ 2242{
2245 struct GNUNET_MQ_Envelope *ev; 2243 struct GNUNET_MQ_Envelope *ev;
2246 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg; 2244 struct GNUNET_RPS_CS_DEBUG_ViewReply *out_msg;
2247 struct Sub *sub; 2245 struct Sub *sub;
2248 2246
2249 if (NULL == view_array) 2247 if (NULL == view_array)
2250 { 2248 {
2251 if (NULL == cli_ctx->sub) sub = msub; 2249 if (NULL == cli_ctx->sub)
2252 else sub = cli_ctx->sub; 2250 sub = msub;
2253 view_size = View_size (sub->view); 2251 else
2254 view_array = View_get_as_array (sub->view); 2252 sub = cli_ctx->sub;
2255 } 2253 view_size = View_size(sub->view);
2256 2254 view_array = View_get_as_array(sub->view);
2257 ev = GNUNET_MQ_msg_extra (out_msg, 2255 }
2258 view_size * sizeof (struct GNUNET_PeerIdentity), 2256
2259 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY); 2257 ev = GNUNET_MQ_msg_extra(out_msg,
2260 out_msg->num_peers = htonl (view_size); 2258 view_size * sizeof(struct GNUNET_PeerIdentity),
2261 2259 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY);
2262 GNUNET_memcpy (&out_msg[1], 2260 out_msg->num_peers = htonl(view_size);
2263 view_array, 2261
2264 view_size * sizeof (struct GNUNET_PeerIdentity)); 2262 GNUNET_memcpy(&out_msg[1],
2265 GNUNET_MQ_send (cli_ctx->mq, ev); 2263 view_array,
2264 view_size * sizeof(struct GNUNET_PeerIdentity));
2265 GNUNET_MQ_send(cli_ctx->mq, ev);
2266} 2266}
2267 2267
2268 2268
@@ -2276,24 +2276,24 @@ send_view (const struct ClientContext *cli_ctx,
2276 * @param view_size the size of the view array (can be 0) 2276 * @param view_size the size of the view array (can be 0)
2277 */ 2277 */
2278static void 2278static void
2279send_stream_peers (const struct ClientContext *cli_ctx, 2279send_stream_peers(const struct ClientContext *cli_ctx,
2280 uint64_t num_peers, 2280 uint64_t num_peers,
2281 const struct GNUNET_PeerIdentity *peers) 2281 const struct GNUNET_PeerIdentity *peers)
2282{ 2282{
2283 struct GNUNET_MQ_Envelope *ev; 2283 struct GNUNET_MQ_Envelope *ev;
2284 struct GNUNET_RPS_CS_DEBUG_StreamReply *out_msg; 2284 struct GNUNET_RPS_CS_DEBUG_StreamReply *out_msg;
2285 2285
2286 GNUNET_assert (NULL != peers); 2286 GNUNET_assert(NULL != peers);
2287 2287
2288 ev = GNUNET_MQ_msg_extra (out_msg, 2288 ev = GNUNET_MQ_msg_extra(out_msg,
2289 num_peers * sizeof (struct GNUNET_PeerIdentity), 2289 num_peers * sizeof(struct GNUNET_PeerIdentity),
2290 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY); 2290 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY);
2291 out_msg->num_peers = htonl (num_peers); 2291 out_msg->num_peers = htonl(num_peers);
2292 2292
2293 GNUNET_memcpy (&out_msg[1], 2293 GNUNET_memcpy(&out_msg[1],
2294 peers, 2294 peers,
2295 num_peers * sizeof (struct GNUNET_PeerIdentity)); 2295 num_peers * sizeof(struct GNUNET_PeerIdentity));
2296 GNUNET_MQ_send (cli_ctx->mq, ev); 2296 GNUNET_MQ_send(cli_ctx->mq, ev);
2297} 2297}
2298 2298
2299 2299
@@ -2303,43 +2303,46 @@ send_stream_peers (const struct ClientContext *cli_ctx,
2303 * @param sub Sub for which to notify clients 2303 * @param sub Sub for which to notify clients
2304 */ 2304 */
2305static void 2305static void
2306clients_notify_view_update (const struct Sub *sub) 2306clients_notify_view_update(const struct Sub *sub)
2307{ 2307{
2308 struct ClientContext *cli_ctx_iter; 2308 struct ClientContext *cli_ctx_iter;
2309 uint64_t num_peers; 2309 uint64_t num_peers;
2310 const struct GNUNET_PeerIdentity *view_array; 2310 const struct GNUNET_PeerIdentity *view_array;
2311 2311
2312 num_peers = View_size (sub->view); 2312 num_peers = View_size(sub->view);
2313 view_array = View_get_as_array(sub->view); 2313 view_array = View_get_as_array(sub->view);
2314 /* check size of view is small enough */ 2314 /* check size of view is small enough */
2315 if (GNUNET_MAX_MESSAGE_SIZE < num_peers) 2315 if (GNUNET_MAX_MESSAGE_SIZE < num_peers)
2316 { 2316 {
2317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2317 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2318 "View is too big to send\n"); 2318 "View is too big to send\n");
2319 return; 2319 return;
2320 } 2320 }
2321 2321
2322 for (cli_ctx_iter = cli_ctx_head; 2322 for (cli_ctx_iter = cli_ctx_head;
2323 NULL != cli_ctx_iter; 2323 NULL != cli_ctx_iter;
2324 cli_ctx_iter = cli_ctx_iter->next) 2324 cli_ctx_iter = cli_ctx_iter->next)
2325 {
2326 if (1 < cli_ctx_iter->view_updates_left)
2327 { 2325 {
2328 /* Client wants to receive limited amount of updates */ 2326 if (1 < cli_ctx_iter->view_updates_left)
2329 cli_ctx_iter->view_updates_left -= 1; 2327 {
2330 } else if (1 == cli_ctx_iter->view_updates_left) 2328 /* Client wants to receive limited amount of updates */
2331 { 2329 cli_ctx_iter->view_updates_left -= 1;
2332 /* Last update of view for client */ 2330 }
2333 cli_ctx_iter->view_updates_left = -1; 2331 else if (1 == cli_ctx_iter->view_updates_left)
2334 } else if (0 > cli_ctx_iter->view_updates_left) { 2332 {
2335 /* Client is not interested in updates */ 2333 /* Last update of view for client */
2336 continue; 2334 cli_ctx_iter->view_updates_left = -1;
2337 } 2335 }
2338 /* else _updates_left == 0 - infinite amount of updates */ 2336 else if (0 > cli_ctx_iter->view_updates_left)
2337 {
2338 /* Client is not interested in updates */
2339 continue;
2340 }
2341 /* else _updates_left == 0 - infinite amount of updates */
2339 2342
2340 /* send view */ 2343 /* send view */
2341 send_view (cli_ctx_iter, view_array, num_peers); 2344 send_view(cli_ctx_iter, view_array, num_peers);
2342 } 2345 }
2343} 2346}
2344 2347
2345 2348
@@ -2350,27 +2353,27 @@ clients_notify_view_update (const struct Sub *sub)
2350 * @param peers the array of peers to send 2353 * @param peers the array of peers to send
2351 */ 2354 */
2352static void 2355static void
2353clients_notify_stream_peer (const struct Sub *sub, 2356clients_notify_stream_peer(const struct Sub *sub,
2354 uint64_t num_peers, 2357 uint64_t num_peers,
2355 const struct GNUNET_PeerIdentity *peers) 2358 const struct GNUNET_PeerIdentity *peers)
2356 // TODO enum StreamPeerSource) 2359// TODO enum StreamPeerSource)
2357{ 2360{
2358 struct ClientContext *cli_ctx_iter; 2361 struct ClientContext *cli_ctx_iter;
2359 2362
2360 LOG (GNUNET_ERROR_TYPE_DEBUG, 2363 LOG(GNUNET_ERROR_TYPE_DEBUG,
2361 "Got peer (%s) from biased stream - update all clients\n", 2364 "Got peer (%s) from biased stream - update all clients\n",
2362 GNUNET_i2s (peers)); 2365 GNUNET_i2s(peers));
2363 2366
2364 for (cli_ctx_iter = cli_ctx_head; 2367 for (cli_ctx_iter = cli_ctx_head;
2365 NULL != cli_ctx_iter; 2368 NULL != cli_ctx_iter;
2366 cli_ctx_iter = cli_ctx_iter->next) 2369 cli_ctx_iter = cli_ctx_iter->next)
2367 {
2368 if (GNUNET_YES == cli_ctx_iter->stream_update &&
2369 (sub == cli_ctx_iter->sub || sub == msub))
2370 { 2370 {
2371 send_stream_peers (cli_ctx_iter, num_peers, peers); 2371 if (GNUNET_YES == cli_ctx_iter->stream_update &&
2372 (sub == cli_ctx_iter->sub || sub == msub))
2373 {
2374 send_stream_peers(cli_ctx_iter, num_peers, peers);
2375 }
2372 } 2376 }
2373 }
2374} 2377}
2375 2378
2376 2379
@@ -2382,34 +2385,34 @@ clients_notify_stream_peer (const struct Sub *sub,
2382 * @param cls Closure - The Sub for which this is to be done 2385 * @param cls Closure - The Sub for which this is to be done
2383 */ 2386 */
2384static void 2387static void
2385hist_update (const struct GNUNET_PeerIdentity *ids, 2388hist_update(const struct GNUNET_PeerIdentity *ids,
2386 uint32_t num_peers, 2389 uint32_t num_peers,
2387 void *cls) 2390 void *cls)
2388{ 2391{
2389 unsigned int i; 2392 unsigned int i;
2390 struct Sub *sub = cls; 2393 struct Sub *sub = cls;
2391 2394
2392 for (i = 0; i < num_peers; i++) 2395 for (i = 0; i < num_peers; i++)
2393 {
2394 int inserted;
2395 if (GNUNET_YES != check_peer_known (sub->peer_map, &ids[i]))
2396 { 2396 {
2397 LOG (GNUNET_ERROR_TYPE_WARNING, 2397 int inserted;
2398 "Peer in history update not known!\n"); 2398 if (GNUNET_YES != check_peer_known(sub->peer_map, &ids[i]))
2399 continue; 2399 {
2400 } 2400 LOG(GNUNET_ERROR_TYPE_WARNING,
2401 inserted = insert_in_view (sub, &ids[i]); 2401 "Peer in history update not known!\n");
2402 if (GNUNET_OK == inserted) 2402 continue;
2403 { 2403 }
2404 clients_notify_stream_peer (sub, 1, &ids[i]); 2404 inserted = insert_in_view(sub, &ids[i]);
2405 } 2405 if (GNUNET_OK == inserted)
2406 {
2407 clients_notify_stream_peer(sub, 1, &ids[i]);
2408 }
2406#ifdef TO_FILE_FULL 2409#ifdef TO_FILE_FULL
2407 to_file (sub->file_name_view_log, 2410 to_file(sub->file_name_view_log,
2408 "+%s\t(hist)", 2411 "+%s\t(hist)",
2409 GNUNET_i2s_full (ids)); 2412 GNUNET_i2s_full(ids));
2410#endif /* TO_FILE_FULL */ 2413#endif /* TO_FILE_FULL */
2411 } 2414 }
2412 clients_notify_view_update (sub); 2415 clients_notify_view_update(sub);
2413} 2416}
2414 2417
2415 2418
@@ -2423,22 +2426,22 @@ hist_update (const struct GNUNET_PeerIdentity *ids,
2423 * @param new_size New size to which to resize 2426 * @param new_size New size to which to resize
2424 */ 2427 */
2425static void 2428static void
2426resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size) 2429resize_wrapper(struct RPS_Sampler *sampler, uint32_t new_size)
2427{ 2430{
2428 unsigned int sampler_size; 2431 unsigned int sampler_size;
2429 2432
2430 // TODO statistics 2433 // TODO statistics
2431 // TODO respect the min, max 2434 // TODO respect the min, max
2432 sampler_size = RPS_sampler_get_size (sampler); 2435 sampler_size = RPS_sampler_get_size(sampler);
2433 if (sampler_size > new_size * 4) 2436 if (sampler_size > new_size * 4)
2434 { /* Shrinking */ 2437 { /* Shrinking */
2435 RPS_sampler_resize (sampler, sampler_size / 2); 2438 RPS_sampler_resize(sampler, sampler_size / 2);
2436 } 2439 }
2437 else if (sampler_size < new_size) 2440 else if (sampler_size < new_size)
2438 { /* Growing */ 2441 { /* Growing */
2439 RPS_sampler_resize (sampler, sampler_size * 2); 2442 RPS_sampler_resize(sampler, sampler_size * 2);
2440 } 2443 }
2441 LOG (GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size); 2444 LOG(GNUNET_ERROR_TYPE_DEBUG, "sampler_size is now %u\n", sampler_size);
2442} 2445}
2443 2446
2444 2447
@@ -2450,32 +2453,33 @@ resize_wrapper (struct RPS_Sampler *sampler, uint32_t new_size)
2450 * @param peer_map the peermap to use as set 2453 * @param peer_map the peermap to use as set
2451 */ 2454 */
2452static void 2455static void
2453add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array, 2456add_peer_array_to_set(const struct GNUNET_PeerIdentity *peer_array,
2454 unsigned int num_peers, 2457 unsigned int num_peers,
2455 struct GNUNET_CONTAINER_MultiPeerMap *peer_map) 2458 struct GNUNET_CONTAINER_MultiPeerMap *peer_map)
2456{ 2459{
2457 unsigned int i; 2460 unsigned int i;
2461
2458 if (NULL == peer_map) 2462 if (NULL == peer_map)
2459 { 2463 {
2460 LOG (GNUNET_ERROR_TYPE_WARNING, 2464 LOG(GNUNET_ERROR_TYPE_WARNING,
2461 "Trying to add peers to non-existing peermap.\n"); 2465 "Trying to add peers to non-existing peermap.\n");
2462 return; 2466 return;
2463 } 2467 }
2464 2468
2465 for (i = 0; i < num_peers; i++) 2469 for (i = 0; i < num_peers; i++)
2466 {
2467 GNUNET_CONTAINER_multipeermap_put (peer_map,
2468 &peer_array[i],
2469 NULL,
2470 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2471 if (msub->peer_map == peer_map)
2472 { 2470 {
2473 GNUNET_STATISTICS_set (stats, 2471 GNUNET_CONTAINER_multipeermap_put(peer_map,
2474 "# known peers", 2472 &peer_array[i],
2475 GNUNET_CONTAINER_multipeermap_size (peer_map), 2473 NULL,
2476 GNUNET_NO); 2474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2475 if (msub->peer_map == peer_map)
2476 {
2477 GNUNET_STATISTICS_set(stats,
2478 "# known peers",
2479 GNUNET_CONTAINER_multipeermap_size(peer_map),
2480 GNUNET_NO);
2481 }
2477 } 2482 }
2478 }
2479} 2483}
2480 2484
2481 2485
@@ -2487,17 +2491,17 @@ add_peer_array_to_set (const struct GNUNET_PeerIdentity *peer_array,
2487 * @param num_peer_ids the number of peers to send to @a peer_id 2491 * @param num_peer_ids the number of peers to send to @a peer_id
2488 */ 2492 */
2489static void 2493static void
2490send_pull_reply (struct PeerContext *peer_ctx, 2494send_pull_reply(struct PeerContext *peer_ctx,
2491 const struct GNUNET_PeerIdentity *peer_ids, 2495 const struct GNUNET_PeerIdentity *peer_ids,
2492 unsigned int num_peer_ids) 2496 unsigned int num_peer_ids)
2493{ 2497{
2494 uint32_t send_size; 2498 uint32_t send_size;
2495 struct GNUNET_MQ_Envelope *ev; 2499 struct GNUNET_MQ_Envelope *ev;
2496 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg; 2500 struct GNUNET_RPS_P2P_PullReplyMessage *out_msg;
2497 2501
2498 /* Compute actual size */ 2502 /* Compute actual size */
2499 send_size = sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) + 2503 send_size = sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) +
2500 num_peer_ids * sizeof (struct GNUNET_PeerIdentity); 2504 num_peer_ids * sizeof(struct GNUNET_PeerIdentity);
2501 2505
2502 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size) 2506 if (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < send_size)
2503 /* Compute number of peers to send 2507 /* Compute number of peers to send
@@ -2506,27 +2510,27 @@ send_pull_reply (struct PeerContext *peer_ctx,
2506 // or even better: do good protocol design 2510 // or even better: do good protocol design
2507 send_size = 2511 send_size =
2508 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE - 2512 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE -
2509 sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) / 2513 sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)) /
2510 sizeof (struct GNUNET_PeerIdentity); 2514 sizeof(struct GNUNET_PeerIdentity);
2511 else 2515 else
2512 send_size = num_peer_ids; 2516 send_size = num_peer_ids;
2513 2517
2514 LOG (GNUNET_ERROR_TYPE_DEBUG, 2518 LOG(GNUNET_ERROR_TYPE_DEBUG,
2515 "Going to send PULL REPLY with %u peers to %s\n", 2519 "Going to send PULL REPLY with %u peers to %s\n",
2516 send_size, GNUNET_i2s (&peer_ctx->peer_id)); 2520 send_size, GNUNET_i2s(&peer_ctx->peer_id));
2517 2521
2518 ev = GNUNET_MQ_msg_extra (out_msg, 2522 ev = GNUNET_MQ_msg_extra(out_msg,
2519 send_size * sizeof (struct GNUNET_PeerIdentity), 2523 send_size * sizeof(struct GNUNET_PeerIdentity),
2520 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY); 2524 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY);
2521 out_msg->num_peers = htonl (send_size); 2525 out_msg->num_peers = htonl(send_size);
2522 GNUNET_memcpy (&out_msg[1], peer_ids, 2526 GNUNET_memcpy(&out_msg[1], peer_ids,
2523 send_size * sizeof (struct GNUNET_PeerIdentity)); 2527 send_size * sizeof(struct GNUNET_PeerIdentity));
2524 2528
2525 send_message (peer_ctx, ev, "PULL REPLY"); 2529 send_message(peer_ctx, ev, "PULL REPLY");
2526 if (peer_ctx->sub == msub) 2530 if (peer_ctx->sub == msub)
2527 { 2531 {
2528 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO); 2532 GNUNET_STATISTICS_update(stats, "# pull reply send issued", 1, GNUNET_NO);
2529 } 2533 }
2530 // TODO check with send intention: as send_channel is used/opened we indicate 2534 // TODO check with send intention: as send_channel is used/opened we indicate
2531 // a sending intention without intending it. 2535 // a sending intention without intending it.
2532 // -> clean peer afterwards? 2536 // -> clean peer afterwards?
@@ -2543,12 +2547,12 @@ send_pull_reply (struct PeerContext *peer_ctx,
2543 * @param peer Peer to insert 2547 * @param peer Peer to insert
2544 */ 2548 */
2545static void 2549static void
2546insert_in_pull_map (void *cls, 2550insert_in_pull_map(void *cls,
2547 const struct GNUNET_PeerIdentity *peer) 2551 const struct GNUNET_PeerIdentity *peer)
2548{ 2552{
2549 struct Sub *sub = cls; 2553 struct Sub *sub = cls;
2550 2554
2551 CustomPeerMap_put (sub->pull_map, peer); 2555 CustomPeerMap_put(sub->pull_map, peer);
2552} 2556}
2553 2557
2554 2558
@@ -2562,17 +2566,17 @@ insert_in_pull_map (void *cls,
2562 * @param peer the peer to insert 2566 * @param peer the peer to insert
2563 */ 2567 */
2564static void 2568static void
2565insert_in_view_op (void *cls, 2569insert_in_view_op(void *cls,
2566 const struct GNUNET_PeerIdentity *peer) 2570 const struct GNUNET_PeerIdentity *peer)
2567{ 2571{
2568 struct Sub *sub = cls; 2572 struct Sub *sub = cls;
2569 int inserted; 2573 int inserted;
2570 2574
2571 inserted = insert_in_view (sub, peer); 2575 inserted = insert_in_view(sub, peer);
2572 if (GNUNET_OK == inserted) 2576 if (GNUNET_OK == inserted)
2573 { 2577 {
2574 clients_notify_stream_peer (sub, 1, peer); 2578 clients_notify_stream_peer(sub, 1, peer);
2575 } 2579 }
2576} 2580}
2577 2581
2578 2582
@@ -2584,49 +2588,49 @@ insert_in_view_op (void *cls,
2584 * @param peer Peer to insert 2588 * @param peer Peer to insert
2585 */ 2589 */
2586static void 2590static void
2587insert_in_sampler (void *cls, 2591insert_in_sampler(void *cls,
2588 const struct GNUNET_PeerIdentity *peer) 2592 const struct GNUNET_PeerIdentity *peer)
2589{ 2593{
2590 struct Sub *sub = cls; 2594 struct Sub *sub = cls;
2591 2595
2592 LOG (GNUNET_ERROR_TYPE_DEBUG, 2596 LOG(GNUNET_ERROR_TYPE_DEBUG,
2593 "Updating samplers with peer %s from insert_in_sampler()\n", 2597 "Updating samplers with peer %s from insert_in_sampler()\n",
2594 GNUNET_i2s (peer)); 2598 GNUNET_i2s(peer));
2595 RPS_sampler_update (sub->sampler, peer); 2599 RPS_sampler_update(sub->sampler, peer);
2596 if (0 < RPS_sampler_count_id (sub->sampler, peer)) 2600 if (0 < RPS_sampler_count_id(sub->sampler, peer))
2597 { 2601 {
2598 /* Make sure we 'know' about this peer */ 2602 /* Make sure we 'know' about this peer */
2599 (void) issue_peer_online_check (sub, peer); 2603 (void)issue_peer_online_check(sub, peer);
2600 /* Establish a channel towards that peer to indicate we are going to send 2604 /* Establish a channel towards that peer to indicate we are going to send
2601 * messages to it */ 2605 * messages to it */
2602 //indicate_sending_intention (peer); 2606 //indicate_sending_intention (peer);
2603 } 2607 }
2604 if (sub == msub) 2608 if (sub == msub)
2605 { 2609 {
2606 GNUNET_STATISTICS_update (stats, 2610 GNUNET_STATISTICS_update(stats,
2607 "# observed peers in gossip", 2611 "# observed peers in gossip",
2608 1, 2612 1,
2609 GNUNET_NO); 2613 GNUNET_NO);
2610 } 2614 }
2611#ifdef TO_FILE 2615#ifdef TO_FILE
2612 sub->num_observed_peers++; 2616 sub->num_observed_peers++;
2613 GNUNET_CONTAINER_multipeermap_put 2617 GNUNET_CONTAINER_multipeermap_put
2614 (sub->observed_unique_peers, 2618 (sub->observed_unique_peers,
2615 peer, 2619 peer,
2616 NULL, 2620 NULL,
2617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 2621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2618 uint32_t num_observed_unique_peers = 2622 uint32_t num_observed_unique_peers =
2619 GNUNET_CONTAINER_multipeermap_size (sub->observed_unique_peers); 2623 GNUNET_CONTAINER_multipeermap_size(sub->observed_unique_peers);
2620 GNUNET_STATISTICS_set (stats, 2624 GNUNET_STATISTICS_set(stats,
2621 "# unique peers in gossip", 2625 "# unique peers in gossip",
2622 num_observed_unique_peers, 2626 num_observed_unique_peers,
2623 GNUNET_NO); 2627 GNUNET_NO);
2624#ifdef TO_FILE_FULL 2628#ifdef TO_FILE_FULL
2625 to_file (sub->file_name_observed_log, 2629 to_file(sub->file_name_observed_log,
2626 "%" PRIu32 " %" PRIu32 " %f\n", 2630 "%" PRIu32 " %" PRIu32 " %f\n",
2627 sub->num_observed_peers, 2631 sub->num_observed_peers,
2628 num_observed_unique_peers, 2632 num_observed_unique_peers,
2629 1.0*num_observed_unique_peers/sub->num_observed_peers) 2633 1.0 * num_observed_unique_peers / sub->num_observed_peers)
2630#endif /* TO_FILE_FULL */ 2634#endif /* TO_FILE_FULL */
2631#endif /* TO_FILE */ 2635#endif /* TO_FILE */
2632} 2636}
@@ -2643,24 +2647,24 @@ insert_in_sampler (void *cls,
2643 * @param peer peer to insert/peer received 2647 * @param peer peer to insert/peer received
2644 */ 2648 */
2645static void 2649static void
2646got_peer (struct Sub *sub, 2650got_peer(struct Sub *sub,
2647 const struct GNUNET_PeerIdentity *peer) 2651 const struct GNUNET_PeerIdentity *peer)
2648{ 2652{
2649 /* If we did not know this peer already, insert it into sampler and view */ 2653 /* If we did not know this peer already, insert it into sampler and view */
2650 if (GNUNET_YES == issue_peer_online_check (sub, peer)) 2654 if (GNUNET_YES == issue_peer_online_check(sub, peer))
2651 { 2655 {
2652 schedule_operation (get_peer_ctx (sub->peer_map, peer), 2656 schedule_operation(get_peer_ctx(sub->peer_map, peer),
2653 &insert_in_sampler, sub); 2657 &insert_in_sampler, sub);
2654 schedule_operation (get_peer_ctx (sub->peer_map, peer), 2658 schedule_operation(get_peer_ctx(sub->peer_map, peer),
2655 &insert_in_view_op, sub); 2659 &insert_in_view_op, sub);
2656 } 2660 }
2657 if (sub == msub) 2661 if (sub == msub)
2658 { 2662 {
2659 GNUNET_STATISTICS_update (stats, 2663 GNUNET_STATISTICS_update(stats,
2660 "# learnd peers", 2664 "# learnd peers",
2661 1, 2665 1,
2662 GNUNET_NO); 2666 GNUNET_NO);
2663 } 2667 }
2664} 2668}
2665 2669
2666 2670
@@ -2672,32 +2676,32 @@ got_peer (struct Sub *sub,
2672 * GNUNET_NO otherwise 2676 * GNUNET_NO otherwise
2673 */ 2677 */
2674static int 2678static int
2675check_sending_channel_needed (const struct PeerContext *peer_ctx) 2679check_sending_channel_needed(const struct PeerContext *peer_ctx)
2676{ 2680{
2677 /* struct GNUNET_CADET_Channel *channel; */ 2681 /* struct GNUNET_CADET_Channel *channel; */
2678 if (GNUNET_NO == check_peer_known (peer_ctx->sub->peer_map, 2682 if (GNUNET_NO == check_peer_known(peer_ctx->sub->peer_map,
2679 &peer_ctx->peer_id)) 2683 &peer_ctx->peer_id))
2680 { 2684 {
2681 return GNUNET_NO; 2685 return GNUNET_NO;
2682 } 2686 }
2683 if (GNUNET_YES == check_sending_channel_exists (peer_ctx)) 2687 if (GNUNET_YES == check_sending_channel_exists(peer_ctx))
2684 { 2688 {
2685 if ( (0 < RPS_sampler_count_id (peer_ctx->sub->sampler, 2689 if ((0 < RPS_sampler_count_id(peer_ctx->sub->sampler,
2686 &peer_ctx->peer_id)) || 2690 &peer_ctx->peer_id)) ||
2687 (GNUNET_YES == View_contains_peer (peer_ctx->sub->view, 2691 (GNUNET_YES == View_contains_peer(peer_ctx->sub->view,
2688 &peer_ctx->peer_id)) || 2692 &peer_ctx->peer_id)) ||
2689 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->push_map, 2693 (GNUNET_YES == CustomPeerMap_contains_peer(peer_ctx->sub->push_map,
2690 &peer_ctx->peer_id)) || 2694 &peer_ctx->peer_id)) ||
2691 (GNUNET_YES == CustomPeerMap_contains_peer (peer_ctx->sub->pull_map, 2695 (GNUNET_YES == CustomPeerMap_contains_peer(peer_ctx->sub->pull_map,
2692 &peer_ctx->peer_id)) || 2696 &peer_ctx->peer_id)) ||
2693 (GNUNET_YES == check_peer_flag (peer_ctx->sub->peer_map, 2697 (GNUNET_YES == check_peer_flag(peer_ctx->sub->peer_map,
2694 &peer_ctx->peer_id, 2698 &peer_ctx->peer_id,
2695 Peers_PULL_REPLY_PENDING))) 2699 Peers_PULL_REPLY_PENDING)))
2696 { /* If we want to keep the connection to peer open */ 2700 { /* If we want to keep the connection to peer open */
2697 return GNUNET_YES; 2701 return GNUNET_YES;
2702 }
2703 return GNUNET_NO;
2698 } 2704 }
2699 return GNUNET_NO;
2700 }
2701 return GNUNET_NO; 2705 return GNUNET_NO;
2702} 2706}
2703 2707
@@ -2710,26 +2714,26 @@ check_sending_channel_needed (const struct PeerContext *peer_ctx)
2710 * @param peer the peer to remove 2714 * @param peer the peer to remove
2711 */ 2715 */
2712static void 2716static void
2713remove_peer (struct Sub *sub, 2717remove_peer(struct Sub *sub,
2714 const struct GNUNET_PeerIdentity *peer) 2718 const struct GNUNET_PeerIdentity *peer)
2715{ 2719{
2716 (void) View_remove_peer (sub->view, 2720 (void)View_remove_peer(sub->view,
2717 peer); 2721 peer);
2718 CustomPeerMap_remove_peer (sub->pull_map, 2722 CustomPeerMap_remove_peer(sub->pull_map,
2719 peer); 2723 peer);
2720 CustomPeerMap_remove_peer (sub->push_map, 2724 CustomPeerMap_remove_peer(sub->push_map,
2721 peer); 2725 peer);
2722 RPS_sampler_reinitialise_by_value (sub->sampler, 2726 RPS_sampler_reinitialise_by_value(sub->sampler,
2723 peer); 2727 peer);
2724 /* We want to destroy the peer now. 2728 /* We want to destroy the peer now.
2725 * Sometimes, it just seems that it's already been removed from the peer_map, 2729 * Sometimes, it just seems that it's already been removed from the peer_map,
2726 * so check the peer_map first. */ 2730 * so check the peer_map first. */
2727 if (GNUNET_YES == check_peer_known (sub->peer_map, 2731 if (GNUNET_YES == check_peer_known(sub->peer_map,
2728 peer)) 2732 peer))
2729 { 2733 {
2730 destroy_peer (get_peer_ctx (sub->peer_map, 2734 destroy_peer(get_peer_ctx(sub->peer_map,
2731 peer)); 2735 peer));
2732 } 2736 }
2733} 2737}
2734 2738
2735 2739
@@ -2742,49 +2746,49 @@ remove_peer (struct Sub *sub,
2742 * @param peer the peer whose data is about to be cleaned 2746 * @param peer the peer whose data is about to be cleaned
2743 */ 2747 */
2744static void 2748static void
2745clean_peer (struct Sub *sub, 2749clean_peer(struct Sub *sub,
2746 const struct GNUNET_PeerIdentity *peer) 2750 const struct GNUNET_PeerIdentity *peer)
2747{ 2751{
2748 if (GNUNET_NO == check_sending_channel_needed (get_peer_ctx (sub->peer_map, 2752 if (GNUNET_NO == check_sending_channel_needed(get_peer_ctx(sub->peer_map,
2749 peer))) 2753 peer)))
2750 { 2754 {
2751 LOG (GNUNET_ERROR_TYPE_DEBUG, 2755 LOG(GNUNET_ERROR_TYPE_DEBUG,
2752 "Going to remove send channel to peer %s\n", 2756 "Going to remove send channel to peer %s\n",
2753 GNUNET_i2s (peer)); 2757 GNUNET_i2s(peer));
2754 #if ENABLE_MALICIOUS 2758 #if ENABLE_MALICIOUS
2755 if (0 != GNUNET_memcmp (&attacked_peer, 2759 if (0 != GNUNET_memcmp(&attacked_peer,
2756 peer)) 2760 peer))
2757 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map, 2761 (void)destroy_sending_channel(get_peer_ctx(sub->peer_map,
2758 peer)); 2762 peer));
2759 #else /* ENABLE_MALICIOUS */ 2763 #else /* ENABLE_MALICIOUS */
2760 (void) destroy_sending_channel (get_peer_ctx (sub->peer_map, 2764 (void)destroy_sending_channel(get_peer_ctx(sub->peer_map,
2761 peer)); 2765 peer));
2762 #endif /* ENABLE_MALICIOUS */ 2766 #endif /* ENABLE_MALICIOUS */
2763 } 2767 }
2764 2768
2765 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (sub->peer_map, 2769 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(sub->peer_map,
2766 peer)) 2770 peer))
2767 { 2771 {
2768 /* Peer was already removed by callback on destroyed channel */ 2772 /* Peer was already removed by callback on destroyed channel */
2769 LOG (GNUNET_ERROR_TYPE_WARNING, 2773 LOG(GNUNET_ERROR_TYPE_WARNING,
2770 "Peer was removed from our knowledge during cleanup\n"); 2774 "Peer was removed from our knowledge during cleanup\n");
2771 return; 2775 return;
2772 } 2776 }
2773 2777
2774 if ( (GNUNET_NO == check_peer_send_intention (get_peer_ctx (sub->peer_map, 2778 if ((GNUNET_NO == check_peer_send_intention(get_peer_ctx(sub->peer_map,
2775 peer))) && 2779 peer))) &&
2776 (GNUNET_NO == View_contains_peer (sub->view, peer)) && 2780 (GNUNET_NO == View_contains_peer(sub->view, peer)) &&
2777 (GNUNET_NO == CustomPeerMap_contains_peer (sub->push_map, peer)) && 2781 (GNUNET_NO == CustomPeerMap_contains_peer(sub->push_map, peer)) &&
2778 (GNUNET_NO == CustomPeerMap_contains_peer (sub->push_map, peer)) && 2782 (GNUNET_NO == CustomPeerMap_contains_peer(sub->push_map, peer)) &&
2779 (0 == RPS_sampler_count_id (sub->sampler, peer)) && 2783 (0 == RPS_sampler_count_id(sub->sampler, peer)) &&
2780 (GNUNET_YES == check_removable (get_peer_ctx (sub->peer_map, peer))) ) 2784 (GNUNET_YES == check_removable(get_peer_ctx(sub->peer_map, peer))))
2781 { /* We can safely remove this peer */ 2785 { /* We can safely remove this peer */
2782 LOG (GNUNET_ERROR_TYPE_DEBUG, 2786 LOG(GNUNET_ERROR_TYPE_DEBUG,
2783 "Going to remove peer %s\n", 2787 "Going to remove peer %s\n",
2784 GNUNET_i2s (peer)); 2788 GNUNET_i2s(peer));
2785 remove_peer (sub, peer); 2789 remove_peer(sub, peer);
2786 return; 2790 return;
2787 } 2791 }
2788} 2792}
2789 2793
2790 2794
@@ -2800,31 +2804,32 @@ clean_peer (struct Sub *sub,
2800 * @param channel The channel being closed 2804 * @param channel The channel being closed
2801 */ 2805 */
2802static void 2806static void
2803cleanup_destroyed_channel (void *cls, 2807cleanup_destroyed_channel(void *cls,
2804 const struct GNUNET_CADET_Channel *channel) 2808 const struct GNUNET_CADET_Channel *channel)
2805{ 2809{
2806 struct ChannelCtx *channel_ctx = cls; 2810 struct ChannelCtx *channel_ctx = cls;
2807 struct PeerContext *peer_ctx = channel_ctx->peer_ctx; 2811 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
2808 (void) channel; 2812
2813 (void)channel;
2809 2814
2810 channel_ctx->channel = NULL; 2815 channel_ctx->channel = NULL;
2811 remove_channel_ctx (channel_ctx); 2816 remove_channel_ctx(channel_ctx);
2812 if (NULL != peer_ctx && 2817 if (NULL != peer_ctx &&
2813 peer_ctx->send_channel_ctx == channel_ctx && 2818 peer_ctx->send_channel_ctx == channel_ctx &&
2814 GNUNET_YES == check_sending_channel_needed (channel_ctx->peer_ctx)) 2819 GNUNET_YES == check_sending_channel_needed(channel_ctx->peer_ctx))
2815 { 2820 {
2816 remove_peer (peer_ctx->sub, &peer_ctx->peer_id); 2821 remove_peer(peer_ctx->sub, &peer_ctx->peer_id);
2817 } 2822 }
2818} 2823}
2819 2824
2820/*********************************************************************** 2825/***********************************************************************
2821 * /Util functions 2826* /Util functions
2822***********************************************************************/ 2827***********************************************************************/
2823 2828
2824 2829
2825 2830
2826/*********************************************************************** 2831/***********************************************************************
2827 * Sub 2832* Sub
2828***********************************************************************/ 2833***********************************************************************/
2829 2834
2830/** 2835/**
@@ -2838,119 +2843,119 @@ cleanup_destroyed_channel (void *cls,
2838 * @return Sub 2843 * @return Sub
2839 */ 2844 */
2840struct Sub * 2845struct Sub *
2841new_sub (const struct GNUNET_HashCode *hash, 2846new_sub(const struct GNUNET_HashCode *hash,
2842 uint32_t sampler_size, 2847 uint32_t sampler_size,
2843 struct GNUNET_TIME_Relative round_interval) 2848 struct GNUNET_TIME_Relative round_interval)
2844{ 2849{
2845 struct Sub *sub; 2850 struct Sub *sub;
2846 2851
2847 sub = GNUNET_new (struct Sub); 2852 sub = GNUNET_new(struct Sub);
2848 2853
2849 /* With the hash generated from the secret value this service only connects 2854 /* With the hash generated from the secret value this service only connects
2850 * to rps instances that share the value */ 2855 * to rps instances that share the value */
2851 struct GNUNET_MQ_MessageHandler cadet_handlers[] = { 2856 struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
2852 GNUNET_MQ_hd_fixed_size (peer_check, 2857 GNUNET_MQ_hd_fixed_size(peer_check,
2853 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE, 2858 GNUNET_MESSAGE_TYPE_RPS_PP_CHECK_LIVE,
2854 struct GNUNET_MessageHeader, 2859 struct GNUNET_MessageHeader,
2855 NULL), 2860 NULL),
2856 GNUNET_MQ_hd_fixed_size (peer_push, 2861 GNUNET_MQ_hd_fixed_size(peer_push,
2857 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH, 2862 GNUNET_MESSAGE_TYPE_RPS_PP_PUSH,
2858 struct GNUNET_MessageHeader, 2863 struct GNUNET_MessageHeader,
2859 NULL), 2864 NULL),
2860 GNUNET_MQ_hd_fixed_size (peer_pull_request, 2865 GNUNET_MQ_hd_fixed_size(peer_pull_request,
2861 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST, 2866 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST,
2862 struct GNUNET_MessageHeader, 2867 struct GNUNET_MessageHeader,
2863 NULL), 2868 NULL),
2864 GNUNET_MQ_hd_var_size (peer_pull_reply, 2869 GNUNET_MQ_hd_var_size(peer_pull_reply,
2865 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY, 2870 GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY,
2866 struct GNUNET_RPS_P2P_PullReplyMessage, 2871 struct GNUNET_RPS_P2P_PullReplyMessage,
2867 NULL), 2872 NULL),
2868 GNUNET_MQ_handler_end () 2873 GNUNET_MQ_handler_end()
2869 }; 2874 };
2870 sub->hash = *hash; 2875 sub->hash = *hash;
2871 sub->cadet_port = 2876 sub->cadet_port =
2872 GNUNET_CADET_open_port (cadet_handle, 2877 GNUNET_CADET_open_port(cadet_handle,
2873 &sub->hash, 2878 &sub->hash,
2874 &handle_inbound_channel, /* Connect handler */ 2879 &handle_inbound_channel, /* Connect handler */
2875 sub, /* cls */ 2880 sub, /* cls */
2876 NULL, /* WindowSize handler */ 2881 NULL, /* WindowSize handler */
2877 &cleanup_destroyed_channel, /* Disconnect handler */ 2882 &cleanup_destroyed_channel, /* Disconnect handler */
2878 cadet_handlers); 2883 cadet_handlers);
2879 if (NULL == sub->cadet_port) 2884 if (NULL == sub->cadet_port)
2880 { 2885 {
2881 LOG (GNUNET_ERROR_TYPE_ERROR, 2886 LOG(GNUNET_ERROR_TYPE_ERROR,
2882 "Cadet port `%s' is already in use.\n", 2887 "Cadet port `%s' is already in use.\n",
2883 GNUNET_APPLICATION_PORT_RPS); 2888 GNUNET_APPLICATION_PORT_RPS);
2884 GNUNET_assert (0); 2889 GNUNET_assert(0);
2885 } 2890 }
2886 2891
2887 /* Set up general data structure to keep track about peers */ 2892 /* Set up general data structure to keep track about peers */
2888 sub->valid_peers = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 2893 sub->valid_peers = GNUNET_CONTAINER_multipeermap_create(4, GNUNET_NO);
2889 if (GNUNET_OK != 2894 if (GNUNET_OK !=
2890 GNUNET_CONFIGURATION_get_value_filename (cfg, 2895 GNUNET_CONFIGURATION_get_value_filename(cfg,
2891 "rps", 2896 "rps",
2892 "FILENAME_VALID_PEERS", 2897 "FILENAME_VALID_PEERS",
2893 &sub->filename_valid_peers)) 2898 &sub->filename_valid_peers))
2894 { 2899 {
2895 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2900 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
2896 "rps", 2901 "rps",
2897 "FILENAME_VALID_PEERS"); 2902 "FILENAME_VALID_PEERS");
2898 } 2903 }
2899 if (0 != strncmp ("DISABLE", sub->filename_valid_peers, 7)) 2904 if (0 != strncmp("DISABLE", sub->filename_valid_peers, 7))
2900 { 2905 {
2901 char *tmp_filename_valid_peers; 2906 char *tmp_filename_valid_peers;
2902 char str_hash[105]; 2907 char str_hash[105];
2903 2908
2904 GNUNET_snprintf (str_hash, 2909 GNUNET_snprintf(str_hash,
2905 sizeof (str_hash), 2910 sizeof(str_hash),
2906 GNUNET_h2s_full (hash)); 2911 GNUNET_h2s_full(hash));
2907 tmp_filename_valid_peers = sub->filename_valid_peers; 2912 tmp_filename_valid_peers = sub->filename_valid_peers;
2908 GNUNET_asprintf (&sub->filename_valid_peers, 2913 GNUNET_asprintf(&sub->filename_valid_peers,
2909 "%s%s", 2914 "%s%s",
2910 tmp_filename_valid_peers, 2915 tmp_filename_valid_peers,
2911 str_hash); 2916 str_hash);
2912 GNUNET_free (tmp_filename_valid_peers); 2917 GNUNET_free(tmp_filename_valid_peers);
2913 } 2918 }
2914 sub->peer_map = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 2919 sub->peer_map = GNUNET_CONTAINER_multipeermap_create(4, GNUNET_NO);
2915 2920
2916 /* Set up the sampler */ 2921 /* Set up the sampler */
2917 sub->sampler_size_est_min = sampler_size; 2922 sub->sampler_size_est_min = sampler_size;
2918 sub->sampler_size_est_need = sampler_size;; 2923 sub->sampler_size_est_need = sampler_size;;
2919 LOG (GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sub->sampler_size_est_min); 2924 LOG(GNUNET_ERROR_TYPE_DEBUG, "MINSIZE is %u\n", sub->sampler_size_est_min);
2920 GNUNET_assert (0 != round_interval.rel_value_us); 2925 GNUNET_assert(0 != round_interval.rel_value_us);
2921 sub->round_interval = round_interval; 2926 sub->round_interval = round_interval;
2922 sub->sampler = RPS_sampler_init (sampler_size, 2927 sub->sampler = RPS_sampler_init(sampler_size,
2923 round_interval); 2928 round_interval);
2924 2929
2925 /* Logging of internals */ 2930 /* Logging of internals */
2926#ifdef TO_FILE_FULL 2931#ifdef TO_FILE_FULL
2927 sub->file_name_view_log = store_prefix_file_name (&own_identity, "view"); 2932 sub->file_name_view_log = store_prefix_file_name(&own_identity, "view");
2928#endif /* TO_FILE_FULL */ 2933#endif /* TO_FILE_FULL */
2929#ifdef TO_FILE 2934#ifdef TO_FILE
2930#ifdef TO_FILE_FULL 2935#ifdef TO_FILE_FULL
2931 sub->file_name_observed_log = store_prefix_file_name (&own_identity, 2936 sub->file_name_observed_log = store_prefix_file_name(&own_identity,
2932 "observed"); 2937 "observed");
2933#endif /* TO_FILE_FULL */ 2938#endif /* TO_FILE_FULL */
2934 sub->num_observed_peers = 0; 2939 sub->num_observed_peers = 0;
2935 sub->observed_unique_peers = GNUNET_CONTAINER_multipeermap_create (1, 2940 sub->observed_unique_peers = GNUNET_CONTAINER_multipeermap_create(1,
2936 GNUNET_NO); 2941 GNUNET_NO);
2937#endif /* TO_FILE */ 2942#endif /* TO_FILE */
2938 2943
2939 /* Set up data structures for gossip */ 2944 /* Set up data structures for gossip */
2940 sub->push_map = CustomPeerMap_create (4); 2945 sub->push_map = CustomPeerMap_create(4);
2941 sub->pull_map = CustomPeerMap_create (4); 2946 sub->pull_map = CustomPeerMap_create(4);
2942 sub->view_size_est_min = sampler_size;; 2947 sub->view_size_est_min = sampler_size;;
2943 sub->view = View_create (sub->view_size_est_min); 2948 sub->view = View_create(sub->view_size_est_min);
2944 if (sub == msub) 2949 if (sub == msub)
2945 { 2950 {
2946 GNUNET_STATISTICS_set (stats, 2951 GNUNET_STATISTICS_set(stats,
2947 "view size aim", 2952 "view size aim",
2948 sub->view_size_est_min, 2953 sub->view_size_est_min,
2949 GNUNET_NO); 2954 GNUNET_NO);
2950 } 2955 }
2951 2956
2952 /* Start executing rounds */ 2957 /* Start executing rounds */
2953 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub); 2958 sub->do_round_task = GNUNET_SCHEDULER_add_now(&do_round, sub);
2954 2959
2955 return sub; 2960 return sub;
2956} 2961}
@@ -2966,36 +2971,36 @@ new_sub (const struct GNUNET_HashCode *hash,
2966 * @param file_name file to dump into 2971 * @param file_name file to dump into
2967 */ 2972 */
2968static void 2973static void
2969write_histogram_to_file (const uint32_t hist_array[], 2974write_histogram_to_file(const uint32_t hist_array[],
2970 const char *file_name) 2975 const char *file_name)
2971{ 2976{
2972 char collect_str[SIZE_DUMP_FILE + 1] = ""; 2977 char collect_str[SIZE_DUMP_FILE + 1] = "";
2973 char *recv_str_iter; 2978 char *recv_str_iter;
2974 char *file_name_full; 2979 char *file_name_full;
2975 2980
2976 recv_str_iter = collect_str; 2981 recv_str_iter = collect_str;
2977 file_name_full = store_prefix_file_name (&own_identity, 2982 file_name_full = store_prefix_file_name(&own_identity,
2978 file_name); 2983 file_name);
2979 for (uint32_t i = 0; i < HISTOGRAM_FILE_SLOTS; i++) 2984 for (uint32_t i = 0; i < HISTOGRAM_FILE_SLOTS; i++)
2980 { 2985 {
2981 char collect_str_tmp[8]; 2986 char collect_str_tmp[8];
2982 2987
2983 GNUNET_snprintf (collect_str_tmp, 2988 GNUNET_snprintf(collect_str_tmp,
2984 sizeof (collect_str_tmp), 2989 sizeof(collect_str_tmp),
2985 "%" PRIu32 "\n", 2990 "%" PRIu32 "\n",
2986 hist_array[i]); 2991 hist_array[i]);
2987 recv_str_iter = stpncpy (recv_str_iter, 2992 recv_str_iter = stpncpy(recv_str_iter,
2988 collect_str_tmp, 2993 collect_str_tmp,
2989 6); 2994 6);
2990 } 2995 }
2991 (void) stpcpy (recv_str_iter, 2996 (void)stpcpy(recv_str_iter,
2992 "\n"); 2997 "\n");
2993 LOG (GNUNET_ERROR_TYPE_DEBUG, 2998 LOG(GNUNET_ERROR_TYPE_DEBUG,
2994 "Writing push stats to disk\n"); 2999 "Writing push stats to disk\n");
2995 to_file_w_len (file_name_full, 3000 to_file_w_len(file_name_full,
2996 SIZE_DUMP_FILE, 3001 SIZE_DUMP_FILE,
2997 collect_str); 3002 collect_str);
2998 GNUNET_free (file_name_full); 3003 GNUNET_free(file_name_full);
2999} 3004}
3000#endif /* TO_FILE */ 3005#endif /* TO_FILE */
3001 3006
@@ -3006,66 +3011,66 @@ write_histogram_to_file (const uint32_t hist_array[],
3006 * @param sub Sub to destroy 3011 * @param sub Sub to destroy
3007 */ 3012 */
3008static void 3013static void
3009destroy_sub (struct Sub *sub) 3014destroy_sub(struct Sub *sub)
3010{ 3015{
3011 GNUNET_assert (NULL != sub); 3016 GNUNET_assert(NULL != sub);
3012 GNUNET_assert (NULL != sub->do_round_task); 3017 GNUNET_assert(NULL != sub->do_round_task);
3013 GNUNET_SCHEDULER_cancel (sub->do_round_task); 3018 GNUNET_SCHEDULER_cancel(sub->do_round_task);
3014 sub->do_round_task = NULL; 3019 sub->do_round_task = NULL;
3015 3020
3016 /* Disconnect from cadet */ 3021 /* Disconnect from cadet */
3017 GNUNET_CADET_close_port (sub->cadet_port); 3022 GNUNET_CADET_close_port(sub->cadet_port);
3018 sub->cadet_port= NULL; 3023 sub->cadet_port = NULL;
3019 3024
3020 /* Clean up data structures for peers */ 3025 /* Clean up data structures for peers */
3021 RPS_sampler_destroy (sub->sampler); 3026 RPS_sampler_destroy(sub->sampler);
3022 sub->sampler = NULL; 3027 sub->sampler = NULL;
3023 View_destroy (sub->view); 3028 View_destroy(sub->view);
3024 sub->view = NULL; 3029 sub->view = NULL;
3025 CustomPeerMap_destroy (sub->push_map); 3030 CustomPeerMap_destroy(sub->push_map);
3026 sub->push_map = NULL; 3031 sub->push_map = NULL;
3027 CustomPeerMap_destroy (sub->pull_map); 3032 CustomPeerMap_destroy(sub->pull_map);
3028 sub->pull_map = NULL; 3033 sub->pull_map = NULL;
3029 peers_terminate (sub); 3034 peers_terminate(sub);
3030 3035
3031 /* Free leftover data structures */ 3036 /* Free leftover data structures */
3032#ifdef TO_FILE_FULL 3037#ifdef TO_FILE_FULL
3033 GNUNET_free (sub->file_name_view_log); 3038 GNUNET_free(sub->file_name_view_log);
3034 sub->file_name_view_log = NULL; 3039 sub->file_name_view_log = NULL;
3035#endif /* TO_FILE_FULL */ 3040#endif /* TO_FILE_FULL */
3036#ifdef TO_FILE 3041#ifdef TO_FILE
3037#ifdef TO_FILE_FULL 3042#ifdef TO_FILE_FULL
3038 GNUNET_free (sub->file_name_observed_log); 3043 GNUNET_free(sub->file_name_observed_log);
3039 sub->file_name_observed_log = NULL; 3044 sub->file_name_observed_log = NULL;
3040#endif /* TO_FILE_FULL */ 3045#endif /* TO_FILE_FULL */
3041 3046
3042 /* Write push frequencies to disk */ 3047 /* Write push frequencies to disk */
3043 write_histogram_to_file (sub->push_recv, 3048 write_histogram_to_file(sub->push_recv,
3044 "push_recv"); 3049 "push_recv");
3045 3050
3046 /* Write push deltas to disk */ 3051 /* Write push deltas to disk */
3047 write_histogram_to_file (sub->push_delta, 3052 write_histogram_to_file(sub->push_delta,
3048 "push_delta"); 3053 "push_delta");
3049 3054
3050 /* Write pull delays to disk */ 3055 /* Write pull delays to disk */
3051 write_histogram_to_file (sub->pull_delays, 3056 write_histogram_to_file(sub->pull_delays,
3052 "pull_delays"); 3057 "pull_delays");
3053 3058
3054 GNUNET_CONTAINER_multipeermap_destroy (sub->observed_unique_peers); 3059 GNUNET_CONTAINER_multipeermap_destroy(sub->observed_unique_peers);
3055 sub->observed_unique_peers = NULL; 3060 sub->observed_unique_peers = NULL;
3056#endif /* TO_FILE */ 3061#endif /* TO_FILE */
3057 3062
3058 GNUNET_free (sub); 3063 GNUNET_free(sub);
3059} 3064}
3060 3065
3061 3066
3062/*********************************************************************** 3067/***********************************************************************
3063 * /Sub 3068* /Sub
3064***********************************************************************/ 3069***********************************************************************/
3065 3070
3066 3071
3067/*********************************************************************** 3072/***********************************************************************
3068 * Core handlers 3073* Core handlers
3069***********************************************************************/ 3074***********************************************************************/
3070 3075
3071/** 3076/**
@@ -3075,13 +3080,13 @@ destroy_sub (struct Sub *sub)
3075 * @param my_identity - unused 3080 * @param my_identity - unused
3076 */ 3081 */
3077void 3082void
3078core_init (void *cls, 3083core_init(void *cls,
3079 const struct GNUNET_PeerIdentity *my_identity) 3084 const struct GNUNET_PeerIdentity *my_identity)
3080{ 3085{
3081 (void) cls; 3086 (void)cls;
3082 (void) my_identity; 3087 (void)my_identity;
3083 3088
3084 map_single_hop = GNUNET_CONTAINER_multipeermap_create (4, GNUNET_NO); 3089 map_single_hop = GNUNET_CONTAINER_multipeermap_create(4, GNUNET_NO);
3085} 3090}
3086 3091
3087 3092
@@ -3094,18 +3099,18 @@ core_init (void *cls,
3094 * @return closure given to #core_disconnects as peer_cls 3099 * @return closure given to #core_disconnects as peer_cls
3095 */ 3100 */
3096void * 3101void *
3097core_connects (void *cls, 3102core_connects(void *cls,
3098 const struct GNUNET_PeerIdentity *peer, 3103 const struct GNUNET_PeerIdentity *peer,
3099 struct GNUNET_MQ_Handle *mq) 3104 struct GNUNET_MQ_Handle *mq)
3100{ 3105{
3101 (void) cls; 3106 (void)cls;
3102 (void) mq; 3107 (void)mq;
3103 3108
3104 GNUNET_assert (GNUNET_YES == 3109 GNUNET_assert(GNUNET_YES ==
3105 GNUNET_CONTAINER_multipeermap_put (map_single_hop, 3110 GNUNET_CONTAINER_multipeermap_put(map_single_hop,
3106 peer, 3111 peer,
3107 NULL, 3112 NULL,
3108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 3113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
3109 return NULL; 3114 return NULL;
3110} 3115}
3111 3116
@@ -3119,18 +3124,18 @@ core_connects (void *cls,
3119 * @param peer_cls closure given in #core_connects - unused 3124 * @param peer_cls closure given in #core_connects - unused
3120 */ 3125 */
3121void 3126void
3122core_disconnects (void *cls, 3127core_disconnects(void *cls,
3123 const struct GNUNET_PeerIdentity *peer, 3128 const struct GNUNET_PeerIdentity *peer,
3124 void *peer_cls) 3129 void *peer_cls)
3125{ 3130{
3126 (void) cls; 3131 (void)cls;
3127 (void) peer_cls; 3132 (void)peer_cls;
3128 3133
3129 GNUNET_CONTAINER_multipeermap_remove_all (map_single_hop, peer); 3134 GNUNET_CONTAINER_multipeermap_remove_all(map_single_hop, peer);
3130} 3135}
3131 3136
3132/*********************************************************************** 3137/***********************************************************************
3133 * /Core handlers 3138* /Core handlers
3134***********************************************************************/ 3139***********************************************************************/
3135 3140
3136 3141
@@ -3140,18 +3145,18 @@ core_disconnects (void *cls,
3140 * @param cli_ctx Context to destroy 3145 * @param cli_ctx Context to destroy
3141 */ 3146 */
3142static void 3147static void
3143destroy_cli_ctx (struct ClientContext *cli_ctx) 3148destroy_cli_ctx(struct ClientContext *cli_ctx)
3144{ 3149{
3145 GNUNET_assert (NULL != cli_ctx); 3150 GNUNET_assert(NULL != cli_ctx);
3146 GNUNET_CONTAINER_DLL_remove (cli_ctx_head, 3151 GNUNET_CONTAINER_DLL_remove(cli_ctx_head,
3147 cli_ctx_tail, 3152 cli_ctx_tail,
3148 cli_ctx); 3153 cli_ctx);
3149 if (NULL != cli_ctx->sub) 3154 if (NULL != cli_ctx->sub)
3150 { 3155 {
3151 destroy_sub (cli_ctx->sub); 3156 destroy_sub(cli_ctx->sub);
3152 cli_ctx->sub = NULL; 3157 cli_ctx->sub = NULL;
3153 } 3158 }
3154 GNUNET_free (cli_ctx); 3159 GNUNET_free(cli_ctx);
3155} 3160}
3156 3161
3157 3162
@@ -3163,42 +3168,44 @@ destroy_cli_ctx (struct ClientContext *cli_ctx)
3163 * @param std_dev standard deviation for the estimate 3168 * @param std_dev standard deviation for the estimate
3164 */ 3169 */
3165static void 3170static void
3166adapt_sizes (struct Sub *sub, double logestimate, double std_dev) 3171adapt_sizes(struct Sub *sub, double logestimate, double std_dev)
3167{ 3172{
3168 double estimate; 3173 double estimate;
3174
3169 //double scale; // TODO this might go gloabal/config 3175 //double scale; // TODO this might go gloabal/config
3170 3176
3171 LOG (GNUNET_ERROR_TYPE_DEBUG, 3177 LOG(GNUNET_ERROR_TYPE_DEBUG,
3172 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n", 3178 "Received a ns estimate - logest: %f, std_dev: %f (old_size: %u)\n",
3173 logestimate, std_dev, RPS_sampler_get_size (sub->sampler)); 3179 logestimate, std_dev, RPS_sampler_get_size(sub->sampler));
3174 //scale = .01; 3180 //scale = .01;
3175 estimate = GNUNET_NSE_log_estimate_to_n (logestimate); 3181 estimate = GNUNET_NSE_log_estimate_to_n(logestimate);
3176 // GNUNET_NSE_log_estimate_to_n (logestimate); 3182 // GNUNET_NSE_log_estimate_to_n (logestimate);
3177 estimate = pow (estimate, 1.0 / 3); 3183 estimate = pow(estimate, 1.0 / 3);
3178 // TODO add if std_dev is a number 3184 // TODO add if std_dev is a number
3179 // estimate += (std_dev * scale); 3185 // estimate += (std_dev * scale);
3180 if (sub->view_size_est_min < ceil (estimate)) 3186 if (sub->view_size_est_min < ceil(estimate))
3181 { 3187 {
3182 LOG (GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate); 3188 LOG(GNUNET_ERROR_TYPE_DEBUG, "Changing estimate to %f\n", estimate);
3183 sub->sampler_size_est_need = estimate; 3189 sub->sampler_size_est_need = estimate;
3184 sub->view_size_est_need = estimate; 3190 sub->view_size_est_need = estimate;
3185 } else 3191 }
3186 { 3192 else
3187 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate); 3193 {
3188 //sub->sampler_size_est_need = sub->view_size_est_min; 3194 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not using estimate %f\n", estimate);
3189 sub->view_size_est_need = sub->view_size_est_min; 3195 //sub->sampler_size_est_need = sub->view_size_est_min;
3190 } 3196 sub->view_size_est_need = sub->view_size_est_min;
3197 }
3191 if (sub == msub) 3198 if (sub == msub)
3192 { 3199 {
3193 GNUNET_STATISTICS_set (stats, 3200 GNUNET_STATISTICS_set(stats,
3194 "view size aim", 3201 "view size aim",
3195 sub->view_size_est_need, 3202 sub->view_size_est_need,
3196 GNUNET_NO); 3203 GNUNET_NO);
3197 } 3204 }
3198 3205
3199 /* If the NSE has changed adapt the lists accordingly */ 3206 /* If the NSE has changed adapt the lists accordingly */
3200 resize_wrapper (sub->sampler, sub->sampler_size_est_need); 3207 resize_wrapper(sub->sampler, sub->sampler_size_est_need);
3201 View_change_len (sub->view, sub->view_size_est_need); 3208 View_change_len(sub->view, sub->view_size_est_need);
3202} 3209}
3203 3210
3204 3211
@@ -3216,24 +3223,24 @@ adapt_sizes (struct Sub *sub, double logestimate, double std_dev)
3216 * @param std_dev standard deviation for the estimate 3223 * @param std_dev standard deviation for the estimate
3217 */ 3224 */
3218static void 3225static void
3219nse_callback (void *cls, 3226nse_callback(void *cls,
3220 struct GNUNET_TIME_Absolute timestamp, 3227 struct GNUNET_TIME_Absolute timestamp,
3221 double logestimate, double std_dev) 3228 double logestimate, double std_dev)
3222{ 3229{
3223 (void) cls; 3230 (void)cls;
3224 (void) timestamp; 3231 (void)timestamp;
3225 struct ClientContext *cli_ctx_iter; 3232 struct ClientContext *cli_ctx_iter;
3226 3233
3227 adapt_sizes (msub, logestimate, std_dev); 3234 adapt_sizes(msub, logestimate, std_dev);
3228 for (cli_ctx_iter = cli_ctx_head; 3235 for (cli_ctx_iter = cli_ctx_head;
3229 NULL != cli_ctx_iter; 3236 NULL != cli_ctx_iter;
3230 cli_ctx_iter = cli_ctx_iter->next) 3237 cli_ctx_iter = cli_ctx_iter->next)
3231 {
3232 if (NULL != cli_ctx_iter->sub)
3233 { 3238 {
3234 adapt_sizes (cli_ctx_iter->sub, logestimate, std_dev); 3239 if (NULL != cli_ctx_iter->sub)
3240 {
3241 adapt_sizes(cli_ctx_iter->sub, logestimate, std_dev);
3242 }
3235 } 3243 }
3236 }
3237} 3244}
3238 3245
3239 3246
@@ -3247,24 +3254,24 @@ nse_callback (void *cls,
3247 * #GNUNET_SYSERR otherwise 3254 * #GNUNET_SYSERR otherwise
3248 */ 3255 */
3249static int 3256static int
3250check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg) 3257check_client_seed(void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
3251{ 3258{
3252 struct ClientContext *cli_ctx = cls; 3259 struct ClientContext *cli_ctx = cls;
3253 uint16_t msize = ntohs (msg->header.size); 3260 uint16_t msize = ntohs(msg->header.size);
3254 uint32_t num_peers = ntohl (msg->num_peers); 3261 uint32_t num_peers = ntohl(msg->num_peers);
3255 3262
3256 msize -= sizeof (struct GNUNET_RPS_CS_SeedMessage); 3263 msize -= sizeof(struct GNUNET_RPS_CS_SeedMessage);
3257 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) || 3264 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
3258 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) ) 3265 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
3259 { 3266 {
3260 LOG (GNUNET_ERROR_TYPE_ERROR, 3267 LOG(GNUNET_ERROR_TYPE_ERROR,
3261 "message says it sends %" PRIu32 " peers, have space for %lu peers\n", 3268 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3262 ntohl (msg->num_peers), 3269 ntohl(msg->num_peers),
3263 (msize / sizeof (struct GNUNET_PeerIdentity))); 3270 (msize / sizeof(struct GNUNET_PeerIdentity)));
3264 GNUNET_break (0); 3271 GNUNET_break(0);
3265 GNUNET_SERVICE_client_drop (cli_ctx->client); 3272 GNUNET_SERVICE_client_drop(cli_ctx->client);
3266 return GNUNET_SYSERR; 3273 return GNUNET_SYSERR;
3267 } 3274 }
3268 return GNUNET_OK; 3275 return GNUNET_OK;
3269} 3276}
3270 3277
@@ -3276,32 +3283,34 @@ check_client_seed (void *cls, const struct GNUNET_RPS_CS_SeedMessage *msg)
3276 * @param message the actual message 3283 * @param message the actual message
3277 */ 3284 */
3278static void 3285static void
3279handle_client_seed (void *cls, 3286handle_client_seed(void *cls,
3280 const struct GNUNET_RPS_CS_SeedMessage *msg) 3287 const struct GNUNET_RPS_CS_SeedMessage *msg)
3281{ 3288{
3282 struct ClientContext *cli_ctx = cls; 3289 struct ClientContext *cli_ctx = cls;
3283 struct GNUNET_PeerIdentity *peers; 3290 struct GNUNET_PeerIdentity *peers;
3284 uint32_t num_peers; 3291 uint32_t num_peers;
3285 uint32_t i; 3292 uint32_t i;
3286 3293
3287 num_peers = ntohl (msg->num_peers); 3294 num_peers = ntohl(msg->num_peers);
3288 peers = (struct GNUNET_PeerIdentity *) &msg[1]; 3295 peers = (struct GNUNET_PeerIdentity *)&msg[1];
3289 3296
3290 LOG (GNUNET_ERROR_TYPE_DEBUG, 3297 LOG(GNUNET_ERROR_TYPE_DEBUG,
3291 "Client seeded peers:\n"); 3298 "Client seeded peers:\n");
3292 print_peer_list (peers, num_peers); 3299 print_peer_list(peers, num_peers);
3293 3300
3294 for (i = 0; i < num_peers; i++) 3301 for (i = 0; i < num_peers; i++)
3295 { 3302 {
3296 LOG (GNUNET_ERROR_TYPE_DEBUG, 3303 LOG(GNUNET_ERROR_TYPE_DEBUG,
3297 "Updating samplers with seed %" PRIu32 ": %s\n", 3304 "Updating samplers with seed %" PRIu32 ": %s\n",
3298 i, 3305 i,
3299 GNUNET_i2s (&peers[i])); 3306 GNUNET_i2s(&peers[i]));
3300 3307
3301 if (NULL != msub) got_peer (msub, &peers[i]); /* Condition needed? */ 3308 if (NULL != msub)
3302 if (NULL != cli_ctx->sub) got_peer (cli_ctx->sub, &peers[i]); 3309 got_peer(msub, &peers[i]); /* Condition needed? */
3303 } 3310 if (NULL != cli_ctx->sub)
3304 GNUNET_SERVICE_client_continue (cli_ctx->client); 3311 got_peer(cli_ctx->sub, &peers[i]);
3312 }
3313 GNUNET_SERVICE_client_continue(cli_ctx->client);
3305} 3314}
3306 3315
3307 3316
@@ -3313,22 +3322,22 @@ handle_client_seed (void *cls,
3313 * receive 3322 * receive
3314 */ 3323 */
3315static void 3324static void
3316handle_client_view_request (void *cls, 3325handle_client_view_request(void *cls,
3317 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg) 3326 const struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg)
3318{ 3327{
3319 struct ClientContext *cli_ctx = cls; 3328 struct ClientContext *cli_ctx = cls;
3320 uint64_t num_updates; 3329 uint64_t num_updates;
3321 3330
3322 num_updates = ntohl (msg->num_updates); 3331 num_updates = ntohl(msg->num_updates);
3323 3332
3324 LOG (GNUNET_ERROR_TYPE_DEBUG, 3333 LOG(GNUNET_ERROR_TYPE_DEBUG,
3325 "Client requested %" PRIu64 " updates of view.\n", 3334 "Client requested %" PRIu64 " updates of view.\n",
3326 num_updates); 3335 num_updates);
3327 3336
3328 GNUNET_assert (NULL != cli_ctx); 3337 GNUNET_assert(NULL != cli_ctx);
3329 cli_ctx->view_updates_left = num_updates; 3338 cli_ctx->view_updates_left = num_updates;
3330 send_view (cli_ctx, NULL, 0); 3339 send_view(cli_ctx, NULL, 0);
3331 GNUNET_SERVICE_client_continue (cli_ctx->client); 3340 GNUNET_SERVICE_client_continue(cli_ctx->client);
3332} 3341}
3333 3342
3334 3343
@@ -3339,22 +3348,23 @@ handle_client_view_request (void *cls,
3339 * @param msg Unused 3348 * @param msg Unused
3340 */ 3349 */
3341static void 3350static void
3342handle_client_view_cancel (void *cls, 3351handle_client_view_cancel(void *cls,
3343 const struct GNUNET_MessageHeader *msg) 3352 const struct GNUNET_MessageHeader *msg)
3344{ 3353{
3345 struct ClientContext *cli_ctx = cls; 3354 struct ClientContext *cli_ctx = cls;
3346 (void) msg;
3347 3355
3348 LOG (GNUNET_ERROR_TYPE_DEBUG, 3356 (void)msg;
3349 "Client does not want to receive updates of view any more.\n");
3350 3357
3351 GNUNET_assert (NULL != cli_ctx); 3358 LOG(GNUNET_ERROR_TYPE_DEBUG,
3359 "Client does not want to receive updates of view any more.\n");
3360
3361 GNUNET_assert(NULL != cli_ctx);
3352 cli_ctx->view_updates_left = 0; 3362 cli_ctx->view_updates_left = 0;
3353 GNUNET_SERVICE_client_continue (cli_ctx->client); 3363 GNUNET_SERVICE_client_continue(cli_ctx->client);
3354 if (GNUNET_YES == cli_ctx->stream_update) 3364 if (GNUNET_YES == cli_ctx->stream_update)
3355 { 3365 {
3356 destroy_cli_ctx (cli_ctx); 3366 destroy_cli_ctx(cli_ctx);
3357 } 3367 }
3358} 3368}
3359 3369
3360 3370
@@ -3365,18 +3375,19 @@ handle_client_view_cancel (void *cls,
3365 * @param message unused 3375 * @param message unused
3366 */ 3376 */
3367static void 3377static void
3368handle_client_stream_request (void *cls, 3378handle_client_stream_request(void *cls,
3369 const struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg) 3379 const struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg)
3370{ 3380{
3371 struct ClientContext *cli_ctx = cls; 3381 struct ClientContext *cli_ctx = cls;
3372 (void) msg;
3373 3382
3374 LOG (GNUNET_ERROR_TYPE_DEBUG, 3383 (void)msg;
3375 "Client requested peers from biased stream.\n"); 3384
3385 LOG(GNUNET_ERROR_TYPE_DEBUG,
3386 "Client requested peers from biased stream.\n");
3376 cli_ctx->stream_update = GNUNET_YES; 3387 cli_ctx->stream_update = GNUNET_YES;
3377 3388
3378 GNUNET_assert (NULL != cli_ctx); 3389 GNUNET_assert(NULL != cli_ctx);
3379 GNUNET_SERVICE_client_continue (cli_ctx->client); 3390 GNUNET_SERVICE_client_continue(cli_ctx->client);
3380} 3391}
3381 3392
3382 3393
@@ -3387,18 +3398,19 @@ handle_client_stream_request (void *cls,
3387 * @param msg unused 3398 * @param msg unused
3388 */ 3399 */
3389static void 3400static void
3390handle_client_stream_cancel (void *cls, 3401handle_client_stream_cancel(void *cls,
3391 const struct GNUNET_MessageHeader *msg) 3402 const struct GNUNET_MessageHeader *msg)
3392{ 3403{
3393 struct ClientContext *cli_ctx = cls; 3404 struct ClientContext *cli_ctx = cls;
3394 (void) msg;
3395 3405
3396 LOG (GNUNET_ERROR_TYPE_DEBUG, 3406 (void)msg;
3397 "Client canceled receiving peers from biased stream.\n"); 3407
3408 LOG(GNUNET_ERROR_TYPE_DEBUG,
3409 "Client canceled receiving peers from biased stream.\n");
3398 cli_ctx->stream_update = GNUNET_NO; 3410 cli_ctx->stream_update = GNUNET_NO;
3399 3411
3400 GNUNET_assert (NULL != cli_ctx); 3412 GNUNET_assert(NULL != cli_ctx);
3401 GNUNET_SERVICE_client_continue (cli_ctx->client); 3413 GNUNET_SERVICE_client_continue(cli_ctx->client);
3402} 3414}
3403 3415
3404 3416
@@ -3409,25 +3421,25 @@ handle_client_stream_cancel (void *cls,
3409 * @param msg Message containing the necessary information 3421 * @param msg Message containing the necessary information
3410 */ 3422 */
3411static void 3423static void
3412handle_client_start_sub (void *cls, 3424handle_client_start_sub(void *cls,
3413 const struct GNUNET_RPS_CS_SubStartMessage *msg) 3425 const struct GNUNET_RPS_CS_SubStartMessage *msg)
3414{ 3426{
3415 struct ClientContext *cli_ctx = cls; 3427 struct ClientContext *cli_ctx = cls;
3416 3428
3417 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client requested start of a new sub.\n"); 3429 LOG(GNUNET_ERROR_TYPE_DEBUG, "Client requested start of a new sub.\n");
3418 if (NULL != cli_ctx->sub && 3430 if (NULL != cli_ctx->sub &&
3419 0 != memcmp (&cli_ctx->sub->hash, 3431 0 != memcmp(&cli_ctx->sub->hash,
3420 &msg->hash, 3432 &msg->hash,
3421 sizeof (struct GNUNET_HashCode))) 3433 sizeof(struct GNUNET_HashCode)))
3422 { 3434 {
3423 LOG (GNUNET_ERROR_TYPE_WARNING, "Already have a Sub with different share for this client. Remove old one, add new.\n"); 3435 LOG(GNUNET_ERROR_TYPE_WARNING, "Already have a Sub with different share for this client. Remove old one, add new.\n");
3424 destroy_sub (cli_ctx->sub); 3436 destroy_sub(cli_ctx->sub);
3425 cli_ctx->sub = NULL; 3437 cli_ctx->sub = NULL;
3426 } 3438 }
3427 cli_ctx->sub = new_sub (&msg->hash, 3439 cli_ctx->sub = new_sub(&msg->hash,
3428 msub->sampler_size_est_min, // TODO make api input? 3440 msub->sampler_size_est_min, // TODO make api input?
3429 GNUNET_TIME_relative_ntoh (msg->round_interval)); 3441 GNUNET_TIME_relative_ntoh(msg->round_interval));
3430 GNUNET_SERVICE_client_continue (cli_ctx->client); 3442 GNUNET_SERVICE_client_continue(cli_ctx->client);
3431} 3443}
3432 3444
3433 3445
@@ -3438,19 +3450,19 @@ handle_client_start_sub (void *cls,
3438 * @param msg Message containing the hash that identifies the Sub 3450 * @param msg Message containing the hash that identifies the Sub
3439 */ 3451 */
3440static void 3452static void
3441handle_client_stop_sub (void *cls, 3453handle_client_stop_sub(void *cls,
3442 const struct GNUNET_RPS_CS_SubStopMessage *msg) 3454 const struct GNUNET_RPS_CS_SubStopMessage *msg)
3443{ 3455{
3444 struct ClientContext *cli_ctx = cls; 3456 struct ClientContext *cli_ctx = cls;
3445 3457
3446 GNUNET_assert (NULL != cli_ctx->sub); 3458 GNUNET_assert(NULL != cli_ctx->sub);
3447 if (0 != memcmp (&cli_ctx->sub->hash, &msg->hash, sizeof (struct GNUNET_HashCode))) 3459 if (0 != memcmp(&cli_ctx->sub->hash, &msg->hash, sizeof(struct GNUNET_HashCode)))
3448 { 3460 {
3449 LOG (GNUNET_ERROR_TYPE_WARNING, "Share of current sub and request differ!\n"); 3461 LOG(GNUNET_ERROR_TYPE_WARNING, "Share of current sub and request differ!\n");
3450 } 3462 }
3451 destroy_sub (cli_ctx->sub); 3463 destroy_sub(cli_ctx->sub);
3452 cli_ctx->sub = NULL; 3464 cli_ctx->sub = NULL;
3453 GNUNET_SERVICE_client_continue (cli_ctx->client); 3465 GNUNET_SERVICE_client_continue(cli_ctx->client);
3454} 3466}
3455 3467
3456 3468
@@ -3464,24 +3476,25 @@ handle_client_stop_sub (void *cls,
3464 * @param msg Message - unused 3476 * @param msg Message - unused
3465 */ 3477 */
3466static void 3478static void
3467handle_peer_check (void *cls, 3479handle_peer_check(void *cls,
3468 const struct GNUNET_MessageHeader *msg) 3480 const struct GNUNET_MessageHeader *msg)
3469{ 3481{
3470 const struct ChannelCtx *channel_ctx = cls; 3482 const struct ChannelCtx *channel_ctx = cls;
3471 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id; 3483 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3472 (void) msg;
3473 3484
3474 LOG (GNUNET_ERROR_TYPE_DEBUG, 3485 (void)msg;
3475 "Received CHECK_LIVE (%s)\n", GNUNET_i2s (peer)); 3486
3487 LOG(GNUNET_ERROR_TYPE_DEBUG,
3488 "Received CHECK_LIVE (%s)\n", GNUNET_i2s(peer));
3476 if (channel_ctx->peer_ctx->sub == msub) 3489 if (channel_ctx->peer_ctx->sub == msub)
3477 { 3490 {
3478 GNUNET_STATISTICS_update (stats, 3491 GNUNET_STATISTICS_update(stats,
3479 "# pending online checks", 3492 "# pending online checks",
3480 -1, 3493 -1,
3481 GNUNET_NO); 3494 GNUNET_NO);
3482 } 3495 }
3483 3496
3484 GNUNET_CADET_receive_done (channel_ctx->channel); 3497 GNUNET_CADET_receive_done(channel_ctx->channel);
3485} 3498}
3486 3499
3487 3500
@@ -3495,69 +3508,70 @@ handle_peer_check (void *cls,
3495 * @param msg Message - unused 3508 * @param msg Message - unused
3496 */ 3509 */
3497static void 3510static void
3498handle_peer_push (void *cls, 3511handle_peer_push(void *cls,
3499 const struct GNUNET_MessageHeader *msg) 3512 const struct GNUNET_MessageHeader *msg)
3500{ 3513{
3501 const struct ChannelCtx *channel_ctx = cls; 3514 const struct ChannelCtx *channel_ctx = cls;
3502 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id; 3515 const struct GNUNET_PeerIdentity *peer = &channel_ctx->peer_ctx->peer_id;
3503 (void) msg; 3516
3517 (void)msg;
3504 3518
3505 // (check the proof of work (?)) 3519 // (check the proof of work (?))
3506 3520
3507 LOG (GNUNET_ERROR_TYPE_DEBUG, 3521 LOG(GNUNET_ERROR_TYPE_DEBUG,
3508 "Received PUSH (%s)\n", 3522 "Received PUSH (%s)\n",
3509 GNUNET_i2s (peer)); 3523 GNUNET_i2s(peer));
3510 if (channel_ctx->peer_ctx->sub == msub) 3524 if (channel_ctx->peer_ctx->sub == msub)
3511 {
3512 GNUNET_STATISTICS_update(stats, "# push message received", 1, GNUNET_NO);
3513 if (NULL != map_single_hop &&
3514 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3515 peer))
3516 { 3525 {
3517 GNUNET_STATISTICS_update (stats, 3526 GNUNET_STATISTICS_update(stats, "# push message received", 1, GNUNET_NO);
3518 "# push message received (multi-hop peer)", 3527 if (NULL != map_single_hop &&
3519 1, 3528 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
3520 GNUNET_NO); 3529 peer))
3530 {
3531 GNUNET_STATISTICS_update(stats,
3532 "# push message received (multi-hop peer)",
3533 1,
3534 GNUNET_NO);
3535 }
3521 } 3536 }
3522 }
3523 3537
3524 #if ENABLE_MALICIOUS 3538 #if ENABLE_MALICIOUS
3525 struct AttackedPeer *tmp_att_peer; 3539 struct AttackedPeer *tmp_att_peer;
3526 3540
3527 if ( (1 == mal_type) || 3541 if ((1 == mal_type) ||
3528 (3 == mal_type) ) 3542 (3 == mal_type))
3529 { /* Try to maximise representation */ 3543 { /* Try to maximise representation */
3530 tmp_att_peer = GNUNET_new (struct AttackedPeer); 3544 tmp_att_peer = GNUNET_new(struct AttackedPeer);
3531 tmp_att_peer->peer_id = *peer; 3545 tmp_att_peer->peer_id = *peer;
3532 if (NULL == att_peer_set) 3546 if (NULL == att_peer_set)
3533 att_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO); 3547 att_peer_set = GNUNET_CONTAINER_multipeermap_create(1, GNUNET_NO);
3534 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set, 3548 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(att_peer_set,
3535 peer)) 3549 peer))
3536 { 3550 {
3537 GNUNET_CONTAINER_DLL_insert (att_peers_head, 3551 GNUNET_CONTAINER_DLL_insert(att_peers_head,
3538 att_peers_tail, 3552 att_peers_tail,
3539 tmp_att_peer); 3553 tmp_att_peer);
3540 add_peer_array_to_set (peer, 1, att_peer_set); 3554 add_peer_array_to_set(peer, 1, att_peer_set);
3541 } 3555 }
3542 else 3556 else
3543 { 3557 {
3544 GNUNET_free (tmp_att_peer); 3558 GNUNET_free(tmp_att_peer);
3559 }
3545 } 3560 }
3546 }
3547 3561
3548 3562
3549 else if (2 == mal_type) 3563 else if (2 == mal_type)
3550 { 3564 {
3551 /* We attack one single well-known peer - simply ignore */ 3565 /* We attack one single well-known peer - simply ignore */
3552 } 3566 }
3553 #endif /* ENABLE_MALICIOUS */ 3567 #endif /* ENABLE_MALICIOUS */
3554 3568
3555 /* Add the sending peer to the push_map */ 3569 /* Add the sending peer to the push_map */
3556 CustomPeerMap_put (channel_ctx->peer_ctx->sub->push_map, peer); 3570 CustomPeerMap_put(channel_ctx->peer_ctx->sub->push_map, peer);
3557 3571
3558 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map, 3572 GNUNET_break_op(check_peer_known(channel_ctx->peer_ctx->sub->peer_map,
3559 &channel_ctx->peer_ctx->peer_id)); 3573 &channel_ctx->peer_ctx->peer_id));
3560 GNUNET_CADET_receive_done (channel_ctx->channel); 3574 GNUNET_CADET_receive_done(channel_ctx->channel);
3561} 3575}
3562 3576
3563 3577
@@ -3570,56 +3584,57 @@ handle_peer_push (void *cls,
3570 * @param msg Message - unused 3584 * @param msg Message - unused
3571 */ 3585 */
3572static void 3586static void
3573handle_peer_pull_request (void *cls, 3587handle_peer_pull_request(void *cls,
3574 const struct GNUNET_MessageHeader *msg) 3588 const struct GNUNET_MessageHeader *msg)
3575{ 3589{
3576 const struct ChannelCtx *channel_ctx = cls; 3590 const struct ChannelCtx *channel_ctx = cls;
3577 struct PeerContext *peer_ctx = channel_ctx->peer_ctx; 3591 struct PeerContext *peer_ctx = channel_ctx->peer_ctx;
3578 const struct GNUNET_PeerIdentity *peer = &peer_ctx->peer_id; 3592 const struct GNUNET_PeerIdentity *peer = &peer_ctx->peer_id;
3579 const struct GNUNET_PeerIdentity *view_array; 3593 const struct GNUNET_PeerIdentity *view_array;
3580 (void) msg;
3581 3594
3582 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s (peer)); 3595 (void)msg;
3596
3597 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received PULL REQUEST (%s)\n", GNUNET_i2s(peer));
3583 if (peer_ctx->sub == msub) 3598 if (peer_ctx->sub == msub)
3584 {
3585 GNUNET_STATISTICS_update(stats,
3586 "# pull request message received",
3587 1,
3588 GNUNET_NO);
3589 if (NULL != map_single_hop &&
3590 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3591 &peer_ctx->peer_id))
3592 { 3599 {
3593 GNUNET_STATISTICS_update (stats, 3600 GNUNET_STATISTICS_update(stats,
3594 "# pull request message received (multi-hop peer)", 3601 "# pull request message received",
3595 1, 3602 1,
3596 GNUNET_NO); 3603 GNUNET_NO);
3604 if (NULL != map_single_hop &&
3605 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
3606 &peer_ctx->peer_id))
3607 {
3608 GNUNET_STATISTICS_update(stats,
3609 "# pull request message received (multi-hop peer)",
3610 1,
3611 GNUNET_NO);
3612 }
3597 } 3613 }
3598 }
3599 3614
3600 #if ENABLE_MALICIOUS 3615 #if ENABLE_MALICIOUS
3601 if (1 == mal_type 3616 if (1 == mal_type
3602 || 3 == mal_type) 3617 || 3 == mal_type)
3603 { /* Try to maximise representation */ 3618 { /* Try to maximise representation */
3604 send_pull_reply (peer_ctx, mal_peers, num_mal_peers); 3619 send_pull_reply(peer_ctx, mal_peers, num_mal_peers);
3605 } 3620 }
3606 3621
3607 else if (2 == mal_type) 3622 else if (2 == mal_type)
3608 { /* Try to partition network */ 3623 { /* Try to partition network */
3609 if (0 == GNUNET_memcmp (&attacked_peer, peer)) 3624 if (0 == GNUNET_memcmp(&attacked_peer, peer))
3610 { 3625 {
3611 send_pull_reply (peer_ctx, mal_peers, num_mal_peers); 3626 send_pull_reply(peer_ctx, mal_peers, num_mal_peers);
3627 }
3612 } 3628 }
3613 }
3614 #endif /* ENABLE_MALICIOUS */ 3629 #endif /* ENABLE_MALICIOUS */
3615 3630
3616 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map, 3631 GNUNET_break_op(check_peer_known(channel_ctx->peer_ctx->sub->peer_map,
3617 &channel_ctx->peer_ctx->peer_id)); 3632 &channel_ctx->peer_ctx->peer_id));
3618 GNUNET_CADET_receive_done (channel_ctx->channel); 3633 GNUNET_CADET_receive_done(channel_ctx->channel);
3619 view_array = View_get_as_array (channel_ctx->peer_ctx->sub->view); 3634 view_array = View_get_as_array(channel_ctx->peer_ctx->sub->view);
3620 send_pull_reply (peer_ctx, 3635 send_pull_reply(peer_ctx,
3621 view_array, 3636 view_array,
3622 View_size (channel_ctx->peer_ctx->sub->view)); 3637 View_size(channel_ctx->peer_ctx->sub->view));
3623} 3638}
3624 3639
3625 3640
@@ -3631,45 +3646,45 @@ handle_peer_pull_request (void *cls,
3631 * @param msg Message containing the replied peers 3646 * @param msg Message containing the replied peers
3632 */ 3647 */
3633static int 3648static int
3634check_peer_pull_reply (void *cls, 3649check_peer_pull_reply(void *cls,
3635 const struct GNUNET_RPS_P2P_PullReplyMessage *msg) 3650 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3636{ 3651{
3637 struct ChannelCtx *channel_ctx = cls; 3652 struct ChannelCtx *channel_ctx = cls;
3638 struct PeerContext *sender_ctx = channel_ctx->peer_ctx; 3653 struct PeerContext *sender_ctx = channel_ctx->peer_ctx;
3639 3654
3640 if (sizeof (struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs (msg->header.size)) 3655 if (sizeof(struct GNUNET_RPS_P2P_PullReplyMessage) > ntohs(msg->header.size))
3641 {
3642 GNUNET_break_op (0);
3643 return GNUNET_SYSERR;
3644 }
3645
3646 if ((ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3647 sizeof (struct GNUNET_PeerIdentity) != ntohl (msg->num_peers))
3648 {
3649 LOG (GNUNET_ERROR_TYPE_ERROR,
3650 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3651 ntohl (msg->num_peers),
3652 (ntohs (msg->header.size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
3653 sizeof (struct GNUNET_PeerIdentity));
3654 GNUNET_break_op (0);
3655 return GNUNET_SYSERR;
3656 }
3657
3658 if (GNUNET_YES != check_peer_flag (sender_ctx->sub->peer_map,
3659 &sender_ctx->peer_id,
3660 Peers_PULL_REPLY_PENDING))
3661 {
3662 LOG (GNUNET_ERROR_TYPE_WARNING,
3663 "Received a pull reply from a peer (%s) we didn't request one from!\n",
3664 GNUNET_i2s (&sender_ctx->peer_id));
3665 if (sender_ctx->sub == msub)
3666 { 3656 {
3667 GNUNET_STATISTICS_update (stats, 3657 GNUNET_break_op(0);
3668 "# unrequested pull replies", 3658 return GNUNET_SYSERR;
3669 1, 3659 }
3670 GNUNET_NO); 3660
3661 if ((ntohs(msg->header.size) - sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)) /
3662 sizeof(struct GNUNET_PeerIdentity) != ntohl(msg->num_peers))
3663 {
3664 LOG(GNUNET_ERROR_TYPE_ERROR,
3665 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3666 ntohl(msg->num_peers),
3667 (ntohs(msg->header.size) - sizeof(struct GNUNET_RPS_P2P_PullReplyMessage)) /
3668 sizeof(struct GNUNET_PeerIdentity));
3669 GNUNET_break_op(0);
3670 return GNUNET_SYSERR;
3671 }
3672
3673 if (GNUNET_YES != check_peer_flag(sender_ctx->sub->peer_map,
3674 &sender_ctx->peer_id,
3675 Peers_PULL_REPLY_PENDING))
3676 {
3677 LOG(GNUNET_ERROR_TYPE_WARNING,
3678 "Received a pull reply from a peer (%s) we didn't request one from!\n",
3679 GNUNET_i2s(&sender_ctx->peer_id));
3680 if (sender_ctx->sub == msub)
3681 {
3682 GNUNET_STATISTICS_update(stats,
3683 "# unrequested pull replies",
3684 1,
3685 GNUNET_NO);
3686 }
3671 } 3687 }
3672 }
3673 return GNUNET_OK; 3688 return GNUNET_OK;
3674} 3689}
3675 3690
@@ -3681,107 +3696,108 @@ check_peer_pull_reply (void *cls,
3681 * @param msg The message header 3696 * @param msg The message header
3682 */ 3697 */
3683static void 3698static void
3684handle_peer_pull_reply (void *cls, 3699handle_peer_pull_reply(void *cls,
3685 const struct GNUNET_RPS_P2P_PullReplyMessage *msg) 3700 const struct GNUNET_RPS_P2P_PullReplyMessage *msg)
3686{ 3701{
3687 const struct ChannelCtx *channel_ctx = cls; 3702 const struct ChannelCtx *channel_ctx = cls;
3688 const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id; 3703 const struct GNUNET_PeerIdentity *sender = &channel_ctx->peer_ctx->peer_id;
3689 const struct GNUNET_PeerIdentity *peers; 3704 const struct GNUNET_PeerIdentity *peers;
3690 struct Sub *sub = channel_ctx->peer_ctx->sub; 3705 struct Sub *sub = channel_ctx->peer_ctx->sub;
3691 uint32_t i; 3706 uint32_t i;
3707
3692#if ENABLE_MALICIOUS 3708#if ENABLE_MALICIOUS
3693 struct AttackedPeer *tmp_att_peer; 3709 struct AttackedPeer *tmp_att_peer;
3694#endif /* ENABLE_MALICIOUS */ 3710#endif /* ENABLE_MALICIOUS */
3695 3711
3696 sub->pull_delays[sub->num_rounds - channel_ctx->peer_ctx->round_pull_req]++; 3712 sub->pull_delays[sub->num_rounds - channel_ctx->peer_ctx->round_pull_req]++;
3697 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s (sender)); 3713 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received PULL REPLY (%s)\n", GNUNET_i2s(sender));
3698 if (channel_ctx->peer_ctx->sub == msub) 3714 if (channel_ctx->peer_ctx->sub == msub)
3699 {
3700 GNUNET_STATISTICS_update (stats,
3701 "# pull reply messages received",
3702 1,
3703 GNUNET_NO);
3704 if (NULL != map_single_hop &&
3705 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3706 &channel_ctx->peer_ctx->peer_id))
3707 { 3715 {
3708 GNUNET_STATISTICS_update (stats, 3716 GNUNET_STATISTICS_update(stats,
3709 "# pull reply messages received (multi-hop peer)", 3717 "# pull reply messages received",
3710 1, 3718 1,
3711 GNUNET_NO); 3719 GNUNET_NO);
3720 if (NULL != map_single_hop &&
3721 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
3722 &channel_ctx->peer_ctx->peer_id))
3723 {
3724 GNUNET_STATISTICS_update(stats,
3725 "# pull reply messages received (multi-hop peer)",
3726 1,
3727 GNUNET_NO);
3728 }
3712 } 3729 }
3713 }
3714 3730
3715 #if ENABLE_MALICIOUS 3731 #if ENABLE_MALICIOUS
3716 // We shouldn't even receive pull replies as we're not sending 3732 // We shouldn't even receive pull replies as we're not sending
3717 if (2 == mal_type) 3733 if (2 == mal_type)
3718 { 3734 {
3719 } 3735 }
3720 #endif /* ENABLE_MALICIOUS */ 3736 #endif /* ENABLE_MALICIOUS */
3721 3737
3722 /* Do actual logic */ 3738 /* Do actual logic */
3723 peers = (const struct GNUNET_PeerIdentity *) &msg[1]; 3739 peers = (const struct GNUNET_PeerIdentity *)&msg[1];
3724 3740
3725 LOG (GNUNET_ERROR_TYPE_DEBUG, 3741 LOG(GNUNET_ERROR_TYPE_DEBUG,
3726 "PULL REPLY received, got following %u peers:\n", 3742 "PULL REPLY received, got following %u peers:\n",
3727 ntohl (msg->num_peers)); 3743 ntohl(msg->num_peers));
3728 3744
3729 for (i = 0; i < ntohl (msg->num_peers); i++) 3745 for (i = 0; i < ntohl(msg->num_peers); i++)
3730 { 3746 {
3731 LOG (GNUNET_ERROR_TYPE_DEBUG, 3747 LOG(GNUNET_ERROR_TYPE_DEBUG,
3732 "%u. %s\n", 3748 "%u. %s\n",
3733 i, 3749 i,
3734 GNUNET_i2s (&peers[i])); 3750 GNUNET_i2s(&peers[i]));
3735 3751
3736 #if ENABLE_MALICIOUS 3752 #if ENABLE_MALICIOUS
3737 if ((NULL != att_peer_set) && 3753 if ((NULL != att_peer_set) &&
3738 (1 == mal_type || 3 == mal_type)) 3754 (1 == mal_type || 3 == mal_type))
3739 { /* Add attacked peer to local list */ 3755 { /* Add attacked peer to local list */
3740 // TODO check if we sent a request and this was the first reply 3756 // TODO check if we sent a request and this was the first reply
3741 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (att_peer_set, 3757 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(att_peer_set,
3742 &peers[i]) 3758 &peers[i])
3743 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (mal_peer_set, 3759 && GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(mal_peer_set,
3744 &peers[i])) 3760 &peers[i]))
3745 { 3761 {
3746 tmp_att_peer = GNUNET_new (struct AttackedPeer); 3762 tmp_att_peer = GNUNET_new(struct AttackedPeer);
3747 tmp_att_peer->peer_id = peers[i]; 3763 tmp_att_peer->peer_id = peers[i];
3748 GNUNET_CONTAINER_DLL_insert (att_peers_head, 3764 GNUNET_CONTAINER_DLL_insert(att_peers_head,
3749 att_peers_tail, 3765 att_peers_tail,
3750 tmp_att_peer); 3766 tmp_att_peer);
3751 add_peer_array_to_set (&peers[i], 1, att_peer_set); 3767 add_peer_array_to_set(&peers[i], 1, att_peer_set);
3752 } 3768 }
3753 continue; 3769 continue;
3754 } 3770 }
3755 #endif /* ENABLE_MALICIOUS */ 3771 #endif /* ENABLE_MALICIOUS */
3756 /* Make sure we 'know' about this peer */ 3772 /* Make sure we 'know' about this peer */
3757 (void) insert_peer (channel_ctx->peer_ctx->sub, 3773 (void)insert_peer(channel_ctx->peer_ctx->sub,
3758 &peers[i]); 3774 &peers[i]);
3759 3775
3760 if (GNUNET_YES == check_peer_valid (channel_ctx->peer_ctx->sub->valid_peers, 3776 if (GNUNET_YES == check_peer_valid(channel_ctx->peer_ctx->sub->valid_peers,
3761 &peers[i])) 3777 &peers[i]))
3762 { 3778 {
3763 CustomPeerMap_put (channel_ctx->peer_ctx->sub->pull_map, 3779 CustomPeerMap_put(channel_ctx->peer_ctx->sub->pull_map,
3764 &peers[i]); 3780 &peers[i]);
3781 }
3782 else
3783 {
3784 schedule_operation(channel_ctx->peer_ctx,
3785 insert_in_pull_map,
3786 channel_ctx->peer_ctx->sub); /* cls */
3787 (void)issue_peer_online_check(channel_ctx->peer_ctx->sub,
3788 &peers[i]);
3789 }
3765 } 3790 }
3766 else 3791
3767 { 3792 UNSET_PEER_FLAG(get_peer_ctx(channel_ctx->peer_ctx->sub->peer_map,
3768 schedule_operation (channel_ctx->peer_ctx, 3793 sender),
3769 insert_in_pull_map, 3794 Peers_PULL_REPLY_PENDING);
3770 channel_ctx->peer_ctx->sub); /* cls */ 3795 clean_peer(channel_ctx->peer_ctx->sub,
3771 (void) issue_peer_online_check (channel_ctx->peer_ctx->sub, 3796 sender);
3772 &peers[i]); 3797
3773 } 3798 GNUNET_break_op(check_peer_known(channel_ctx->peer_ctx->sub->peer_map,
3774 } 3799 sender));
3775 3800 GNUNET_CADET_receive_done(channel_ctx->channel);
3776 UNSET_PEER_FLAG (get_peer_ctx (channel_ctx->peer_ctx->sub->peer_map,
3777 sender),
3778 Peers_PULL_REPLY_PENDING);
3779 clean_peer (channel_ctx->peer_ctx->sub,
3780 sender);
3781
3782 GNUNET_break_op (check_peer_known (channel_ctx->peer_ctx->sub->peer_map,
3783 sender));
3784 GNUNET_CADET_receive_done (channel_ctx->channel);
3785} 3801}
3786 3802
3787 3803
@@ -3796,8 +3812,8 @@ handle_peer_pull_reply (void *cls,
3796 * @param spread the inverse amount of deviation from the mean 3812 * @param spread the inverse amount of deviation from the mean
3797 */ 3813 */
3798static struct GNUNET_TIME_Relative 3814static struct GNUNET_TIME_Relative
3799compute_rand_delay (struct GNUNET_TIME_Relative mean, 3815compute_rand_delay(struct GNUNET_TIME_Relative mean,
3800 unsigned int spread) 3816 unsigned int spread)
3801{ 3817{
3802 struct GNUNET_TIME_Relative half_interval; 3818 struct GNUNET_TIME_Relative half_interval;
3803 struct GNUNET_TIME_Relative ret; 3819 struct GNUNET_TIME_Relative ret;
@@ -3805,30 +3821,30 @@ compute_rand_delay (struct GNUNET_TIME_Relative mean,
3805 unsigned int max_rand_delay; 3821 unsigned int max_rand_delay;
3806 3822
3807 if (0 == spread) 3823 if (0 == spread)
3808 { 3824 {
3809 LOG (GNUNET_ERROR_TYPE_WARNING, 3825 LOG(GNUNET_ERROR_TYPE_WARNING,
3810 "Not accepting spread of 0\n"); 3826 "Not accepting spread of 0\n");
3811 GNUNET_break (0); 3827 GNUNET_break(0);
3812 GNUNET_assert (0); 3828 GNUNET_assert(0);
3813 } 3829 }
3814 GNUNET_assert (0 != mean.rel_value_us); 3830 GNUNET_assert(0 != mean.rel_value_us);
3815 3831
3816 /* Compute random time value between spread * mean and spread * mean */ 3832 /* Compute random time value between spread * mean and spread * mean */
3817 half_interval = GNUNET_TIME_relative_divide (mean, spread); 3833 half_interval = GNUNET_TIME_relative_divide(mean, spread);
3818 3834
3819 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2/spread); 3835 max_rand_delay = GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us / mean.rel_value_us * (2 / spread);
3820 /** 3836 /**
3821 * Compute random value between (0 and 1) * round_interval 3837 * Compute random value between (0 and 1) * round_interval
3822 * via multiplying round_interval with a 'fraction' (0 to value)/value 3838 * via multiplying round_interval with a 'fraction' (0 to value)/value
3823 */ 3839 */
3824 rand_delay = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay); 3840 rand_delay = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max_rand_delay);
3825 ret = GNUNET_TIME_relative_saturating_multiply (mean, rand_delay); 3841 ret = GNUNET_TIME_relative_saturating_multiply(mean, rand_delay);
3826 ret = GNUNET_TIME_relative_divide (ret, max_rand_delay); 3842 ret = GNUNET_TIME_relative_divide(ret, max_rand_delay);
3827 ret = GNUNET_TIME_relative_add (ret, half_interval); 3843 ret = GNUNET_TIME_relative_add(ret, half_interval);
3828 3844
3829 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us) 3845 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == ret.rel_value_us)
3830 LOG (GNUNET_ERROR_TYPE_WARNING, 3846 LOG(GNUNET_ERROR_TYPE_WARNING,
3831 "Returning FOREVER_REL\n"); 3847 "Returning FOREVER_REL\n");
3832 3848
3833 return ret; 3849 return ret;
3834} 3850}
@@ -3840,41 +3856,41 @@ compute_rand_delay (struct GNUNET_TIME_Relative mean,
3840 * @param peer_ctx Context to the peer to send request to 3856 * @param peer_ctx Context to the peer to send request to
3841 */ 3857 */
3842static void 3858static void
3843send_pull_request (struct PeerContext *peer_ctx) 3859send_pull_request(struct PeerContext *peer_ctx)
3844{ 3860{
3845 struct GNUNET_MQ_Envelope *ev; 3861 struct GNUNET_MQ_Envelope *ev;
3846 3862
3847 GNUNET_assert (GNUNET_NO == check_peer_flag (peer_ctx->sub->peer_map, 3863 GNUNET_assert(GNUNET_NO == check_peer_flag(peer_ctx->sub->peer_map,
3848 &peer_ctx->peer_id, 3864 &peer_ctx->peer_id,
3849 Peers_PULL_REPLY_PENDING)); 3865 Peers_PULL_REPLY_PENDING));
3850 SET_PEER_FLAG (peer_ctx, 3866 SET_PEER_FLAG(peer_ctx,
3851 Peers_PULL_REPLY_PENDING); 3867 Peers_PULL_REPLY_PENDING);
3852 peer_ctx->round_pull_req = peer_ctx->sub->num_rounds; 3868 peer_ctx->round_pull_req = peer_ctx->sub->num_rounds;
3853 3869
3854 LOG (GNUNET_ERROR_TYPE_DEBUG, 3870 LOG(GNUNET_ERROR_TYPE_DEBUG,
3855 "Going to send PULL REQUEST to peer %s.\n", 3871 "Going to send PULL REQUEST to peer %s.\n",
3856 GNUNET_i2s (&peer_ctx->peer_id)); 3872 GNUNET_i2s(&peer_ctx->peer_id));
3857 3873
3858 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST); 3874 ev = GNUNET_MQ_msg_header(GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REQUEST);
3859 send_message (peer_ctx, 3875 send_message(peer_ctx,
3860 ev, 3876 ev,
3861 "PULL REQUEST"); 3877 "PULL REQUEST");
3862 if (peer_ctx->sub) 3878 if (peer_ctx->sub)
3863 {
3864 GNUNET_STATISTICS_update (stats,
3865 "# pull request send issued",
3866 1,
3867 GNUNET_NO);
3868 if (NULL != map_single_hop &&
3869 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3870 &peer_ctx->peer_id))
3871 { 3879 {
3872 GNUNET_STATISTICS_update (stats, 3880 GNUNET_STATISTICS_update(stats,
3873 "# pull request send issued (multi-hop peer)", 3881 "# pull request send issued",
3874 1, 3882 1,
3875 GNUNET_NO); 3883 GNUNET_NO);
3884 if (NULL != map_single_hop &&
3885 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
3886 &peer_ctx->peer_id))
3887 {
3888 GNUNET_STATISTICS_update(stats,
3889 "# pull request send issued (multi-hop peer)",
3890 1,
3891 GNUNET_NO);
3892 }
3876 } 3893 }
3877 }
3878} 3894}
3879 3895
3880 3896
@@ -3884,32 +3900,32 @@ send_pull_request (struct PeerContext *peer_ctx)
3884 * @param peer_ctx Context of peer to send push to 3900 * @param peer_ctx Context of peer to send push to
3885 */ 3901 */
3886static void 3902static void
3887send_push (struct PeerContext *peer_ctx) 3903send_push(struct PeerContext *peer_ctx)
3888{ 3904{
3889 struct GNUNET_MQ_Envelope *ev; 3905 struct GNUNET_MQ_Envelope *ev;
3890 3906
3891 LOG (GNUNET_ERROR_TYPE_DEBUG, 3907 LOG(GNUNET_ERROR_TYPE_DEBUG,
3892 "Going to send PUSH to peer %s.\n", 3908 "Going to send PUSH to peer %s.\n",
3893 GNUNET_i2s (&peer_ctx->peer_id)); 3909 GNUNET_i2s(&peer_ctx->peer_id));
3894 3910
3895 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_PP_PUSH); 3911 ev = GNUNET_MQ_msg_header(GNUNET_MESSAGE_TYPE_RPS_PP_PUSH);
3896 send_message (peer_ctx, ev, "PUSH"); 3912 send_message(peer_ctx, ev, "PUSH");
3897 if (peer_ctx->sub) 3913 if (peer_ctx->sub)
3898 {
3899 GNUNET_STATISTICS_update (stats,
3900 "# push send issued",
3901 1,
3902 GNUNET_NO);
3903 if (NULL != map_single_hop &&
3904 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (map_single_hop,
3905 &peer_ctx->peer_id))
3906 { 3914 {
3907 GNUNET_STATISTICS_update (stats, 3915 GNUNET_STATISTICS_update(stats,
3908 "# push send issued (multi-hop peer)", 3916 "# push send issued",
3909 1, 3917 1,
3910 GNUNET_NO); 3918 GNUNET_NO);
3919 if (NULL != map_single_hop &&
3920 GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(map_single_hop,
3921 &peer_ctx->peer_id))
3922 {
3923 GNUNET_STATISTICS_update(stats,
3924 "# push send issued (multi-hop peer)",
3925 1,
3926 GNUNET_NO);
3927 }
3911 } 3928 }
3912 }
3913} 3929}
3914 3930
3915 3931
@@ -3925,25 +3941,25 @@ send_push (struct PeerContext *peer_ctx)
3925 * @return #GNUNET_OK if @a msg is well-formed 3941 * @return #GNUNET_OK if @a msg is well-formed
3926 */ 3942 */
3927static int 3943static int
3928check_client_act_malicious (void *cls, 3944check_client_act_malicious(void *cls,
3929 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg) 3945 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3930{ 3946{
3931 struct ClientContext *cli_ctx = cls; 3947 struct ClientContext *cli_ctx = cls;
3932 uint16_t msize = ntohs (msg->header.size); 3948 uint16_t msize = ntohs(msg->header.size);
3933 uint32_t num_peers = ntohl (msg->num_peers); 3949 uint32_t num_peers = ntohl(msg->num_peers);
3934 3950
3935 msize -= sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage); 3951 msize -= sizeof(struct GNUNET_RPS_CS_ActMaliciousMessage);
3936 if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) || 3952 if ((msize / sizeof(struct GNUNET_PeerIdentity) != num_peers) ||
3937 (msize % sizeof (struct GNUNET_PeerIdentity) != 0) ) 3953 (msize % sizeof(struct GNUNET_PeerIdentity) != 0))
3938 { 3954 {
3939 LOG (GNUNET_ERROR_TYPE_ERROR, 3955 LOG(GNUNET_ERROR_TYPE_ERROR,
3940 "message says it sends %" PRIu32 " peers, have space for %lu peers\n", 3956 "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
3941 ntohl (msg->num_peers), 3957 ntohl(msg->num_peers),
3942 (msize / sizeof (struct GNUNET_PeerIdentity))); 3958 (msize / sizeof(struct GNUNET_PeerIdentity)));
3943 GNUNET_break (0); 3959 GNUNET_break(0);
3944 GNUNET_SERVICE_client_drop (cli_ctx->client); 3960 GNUNET_SERVICE_client_drop(cli_ctx->client);
3945 return GNUNET_SYSERR; 3961 return GNUNET_SYSERR;
3946 } 3962 }
3947 return GNUNET_OK; 3963 return GNUNET_OK;
3948} 3964}
3949 3965
@@ -3955,8 +3971,8 @@ check_client_act_malicious (void *cls,
3955 * @param msg The message header 3971 * @param msg The message header
3956 */ 3972 */
3957static void 3973static void
3958handle_client_act_malicious (void *cls, 3974handle_client_act_malicious(void *cls,
3959 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg) 3975 const struct GNUNET_RPS_CS_ActMaliciousMessage *msg)
3960{ 3976{
3961 struct ClientContext *cli_ctx = cls; 3977 struct ClientContext *cli_ctx = cls;
3962 struct GNUNET_PeerIdentity *peers; 3978 struct GNUNET_PeerIdentity *peers;
@@ -3964,102 +3980,103 @@ handle_client_act_malicious (void *cls,
3964 uint32_t num_mal_peers_old; 3980 uint32_t num_mal_peers_old;
3965 struct Sub *sub = cli_ctx->sub; 3981 struct Sub *sub = cli_ctx->sub;
3966 3982
3967 if (NULL == sub) sub = msub; 3983 if (NULL == sub)
3984 sub = msub;
3968 /* Do actual logic */ 3985 /* Do actual logic */
3969 peers = (struct GNUNET_PeerIdentity *) &msg[1]; 3986 peers = (struct GNUNET_PeerIdentity *)&msg[1];
3970 mal_type = ntohl (msg->type); 3987 mal_type = ntohl(msg->type);
3971 if (NULL == mal_peer_set) 3988 if (NULL == mal_peer_set)
3972 mal_peer_set = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO); 3989 mal_peer_set = GNUNET_CONTAINER_multipeermap_create(1, GNUNET_NO);
3973 3990
3974 LOG (GNUNET_ERROR_TYPE_DEBUG, 3991 LOG(GNUNET_ERROR_TYPE_DEBUG,
3975 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n", 3992 "Now acting malicious type %" PRIu32 ", got %" PRIu32 " peers.\n",
3976 mal_type, 3993 mal_type,
3977 ntohl (msg->num_peers)); 3994 ntohl(msg->num_peers));
3978 3995
3979 if (1 == mal_type) 3996 if (1 == mal_type)
3980 { /* Try to maximise representation */ 3997 { /* Try to maximise representation */
3981 /* Add other malicious peers to those we already know */ 3998 /* Add other malicious peers to those we already know */
3982 3999
3983 num_mal_peers_sent = ntohl (msg->num_peers); 4000 num_mal_peers_sent = ntohl(msg->num_peers);
3984 num_mal_peers_old = num_mal_peers; 4001 num_mal_peers_old = num_mal_peers;
3985 GNUNET_array_grow (mal_peers, 4002 GNUNET_array_grow(mal_peers,
3986 num_mal_peers, 4003 num_mal_peers,
3987 num_mal_peers + num_mal_peers_sent); 4004 num_mal_peers + num_mal_peers_sent);
3988 GNUNET_memcpy (&mal_peers[num_mal_peers_old], 4005 GNUNET_memcpy(&mal_peers[num_mal_peers_old],
3989 peers, 4006 peers,
3990 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity)); 4007 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
3991
3992 /* Add all mal peers to mal_peer_set */
3993 add_peer_array_to_set (&mal_peers[num_mal_peers_old],
3994 num_mal_peers_sent,
3995 mal_peer_set);
3996
3997 /* Substitute do_round () with do_mal_round () */
3998 GNUNET_assert (NULL != sub->do_round_task);
3999 GNUNET_SCHEDULER_cancel (sub->do_round_task);
4000 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub);
4001 }
4002
4003 else if ( (2 == mal_type) ||
4004 (3 == mal_type) )
4005 { /* Try to partition the network */
4006 /* Add other malicious peers to those we already know */
4007
4008 num_mal_peers_sent = ntohl (msg->num_peers) - 1;
4009 num_mal_peers_old = num_mal_peers;
4010 GNUNET_assert (GNUNET_MAX_MALLOC_CHECKED > num_mal_peers_sent);
4011 GNUNET_array_grow (mal_peers,
4012 num_mal_peers,
4013 num_mal_peers + num_mal_peers_sent);
4014 if (NULL != mal_peers &&
4015 0 != num_mal_peers)
4016 {
4017 GNUNET_memcpy (&mal_peers[num_mal_peers_old],
4018 peers,
4019 num_mal_peers_sent * sizeof (struct GNUNET_PeerIdentity));
4020 4008
4021 /* Add all mal peers to mal_peer_set */ 4009 /* Add all mal peers to mal_peer_set */
4022 add_peer_array_to_set (&mal_peers[num_mal_peers_old], 4010 add_peer_array_to_set(&mal_peers[num_mal_peers_old],
4023 num_mal_peers_sent, 4011 num_mal_peers_sent,
4024 mal_peer_set); 4012 mal_peer_set);
4013
4014 /* Substitute do_round () with do_mal_round () */
4015 GNUNET_assert(NULL != sub->do_round_task);
4016 GNUNET_SCHEDULER_cancel(sub->do_round_task);
4017 sub->do_round_task = GNUNET_SCHEDULER_add_now(&do_mal_round, sub);
4025 } 4018 }
4026 4019
4027 /* Store the one attacked peer */ 4020 else if ((2 == mal_type) ||
4028 GNUNET_memcpy (&attacked_peer, 4021 (3 == mal_type))
4029 &msg->attacked_peer, 4022 { /* Try to partition the network */
4030 sizeof (struct GNUNET_PeerIdentity)); 4023 /* Add other malicious peers to those we already know */
4031 /* Set the flag of the attacked peer to valid to avoid problems */ 4024
4032 if (GNUNET_NO == check_peer_known (sub->peer_map, &attacked_peer)) 4025 num_mal_peers_sent = ntohl(msg->num_peers) - 1;
4033 { 4026 num_mal_peers_old = num_mal_peers;
4034 (void) issue_peer_online_check (sub, &attacked_peer); 4027 GNUNET_assert(GNUNET_MAX_MALLOC_CHECKED > num_mal_peers_sent);
4035 } 4028 GNUNET_array_grow(mal_peers,
4029 num_mal_peers,
4030 num_mal_peers + num_mal_peers_sent);
4031 if (NULL != mal_peers &&
4032 0 != num_mal_peers)
4033 {
4034 GNUNET_memcpy(&mal_peers[num_mal_peers_old],
4035 peers,
4036 num_mal_peers_sent * sizeof(struct GNUNET_PeerIdentity));
4037
4038 /* Add all mal peers to mal_peer_set */
4039 add_peer_array_to_set(&mal_peers[num_mal_peers_old],
4040 num_mal_peers_sent,
4041 mal_peer_set);
4042 }
4036 4043
4037 LOG (GNUNET_ERROR_TYPE_DEBUG, 4044 /* Store the one attacked peer */
4038 "Attacked peer is %s\n", 4045 GNUNET_memcpy(&attacked_peer,
4039 GNUNET_i2s (&attacked_peer)); 4046 &msg->attacked_peer,
4047 sizeof(struct GNUNET_PeerIdentity));
4048 /* Set the flag of the attacked peer to valid to avoid problems */
4049 if (GNUNET_NO == check_peer_known(sub->peer_map, &attacked_peer))
4050 {
4051 (void)issue_peer_online_check(sub, &attacked_peer);
4052 }
4040 4053
4041 /* Substitute do_round () with do_mal_round () */ 4054 LOG(GNUNET_ERROR_TYPE_DEBUG,
4042 if (NULL != sub->do_round_task) 4055 "Attacked peer is %s\n",
4043 { 4056 GNUNET_i2s(&attacked_peer));
4044 /* Probably in shutdown */ 4057
4045 GNUNET_SCHEDULER_cancel (sub->do_round_task); 4058 /* Substitute do_round () with do_mal_round () */
4046 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_mal_round, sub); 4059 if (NULL != sub->do_round_task)
4060 {
4061 /* Probably in shutdown */
4062 GNUNET_SCHEDULER_cancel(sub->do_round_task);
4063 sub->do_round_task = GNUNET_SCHEDULER_add_now(&do_mal_round, sub);
4064 }
4047 } 4065 }
4048 }
4049 else if (0 == mal_type) 4066 else if (0 == mal_type)
4050 { /* Stop acting malicious */ 4067 { /* Stop acting malicious */
4051 GNUNET_array_grow (mal_peers, num_mal_peers, 0); 4068 GNUNET_array_grow(mal_peers, num_mal_peers, 0);
4052 4069
4053 /* Substitute do_mal_round () with do_round () */ 4070 /* Substitute do_mal_round () with do_round () */
4054 GNUNET_SCHEDULER_cancel (sub->do_round_task); 4071 GNUNET_SCHEDULER_cancel(sub->do_round_task);
4055 sub->do_round_task = GNUNET_SCHEDULER_add_now (&do_round, sub); 4072 sub->do_round_task = GNUNET_SCHEDULER_add_now(&do_round, sub);
4056 } 4073 }
4057 else 4074 else
4058 { 4075 {
4059 GNUNET_break (0); 4076 GNUNET_break(0);
4060 GNUNET_SERVICE_client_continue (cli_ctx->client); 4077 GNUNET_SERVICE_client_continue(cli_ctx->client);
4061 } 4078 }
4062 GNUNET_SERVICE_client_continue (cli_ctx->client); 4079 GNUNET_SERVICE_client_continue(cli_ctx->client);
4063} 4080}
4064 4081
4065 4082
@@ -4071,7 +4088,7 @@ handle_client_act_malicious (void *cls,
4071 * @param cls Closure - Sub 4088 * @param cls Closure - Sub
4072 */ 4089 */
4073static void 4090static void
4074do_mal_round (void *cls) 4091do_mal_round(void *cls)
4075{ 4092{
4076 uint32_t num_pushes; 4093 uint32_t num_pushes;
4077 uint32_t i; 4094 uint32_t i;
@@ -4079,121 +4096,119 @@ do_mal_round (void *cls)
4079 struct AttackedPeer *tmp_att_peer; 4096 struct AttackedPeer *tmp_att_peer;
4080 struct Sub *sub = cls; 4097 struct Sub *sub = cls;
4081 4098
4082 LOG (GNUNET_ERROR_TYPE_DEBUG, 4099 LOG(GNUNET_ERROR_TYPE_DEBUG,
4083 "Going to execute next round maliciously type %" PRIu32 ".\n", 4100 "Going to execute next round maliciously type %" PRIu32 ".\n",
4084 mal_type); 4101 mal_type);
4085 sub->do_round_task = NULL; 4102 sub->do_round_task = NULL;
4086 GNUNET_assert (mal_type <= 3); 4103 GNUNET_assert(mal_type <= 3);
4087 /* Do malicious actions */ 4104 /* Do malicious actions */
4088 if (1 == mal_type) 4105 if (1 == mal_type)
4089 { /* Try to maximise representation */ 4106 { /* Try to maximise representation */
4090 4107 /* The maximum of pushes we're going to send this round */
4091 /* The maximum of pushes we're going to send this round */ 4108 num_pushes = GNUNET_MIN(GNUNET_MIN(push_limit,
4092 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit,
4093 num_attacked_peers), 4109 num_attacked_peers),
4094 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE); 4110 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4095 4111
4096 LOG (GNUNET_ERROR_TYPE_DEBUG, 4112 LOG(GNUNET_ERROR_TYPE_DEBUG,
4097 "Going to send %" PRIu32 " pushes\n", 4113 "Going to send %" PRIu32 " pushes\n",
4098 num_pushes); 4114 num_pushes);
4099 4115
4100 /* Send PUSHes to attacked peers */ 4116 /* Send PUSHes to attacked peers */
4101 for (i = 0 ; i < num_pushes ; i++) 4117 for (i = 0; i < num_pushes; i++)
4102 { 4118 {
4103 if (att_peers_tail == att_peer_index) 4119 if (att_peers_tail == att_peer_index)
4104 att_peer_index = att_peers_head; 4120 att_peer_index = att_peers_head;
4105 else 4121 else
4106 att_peer_index = att_peer_index->next; 4122 att_peer_index = att_peer_index->next;
4107 4123
4108 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id)); 4124 send_push(get_peer_ctx(sub->peer_map, &att_peer_index->peer_id));
4109 } 4125 }
4110 4126
4111 /* Send PULLs to some peers to learn about additional peers to attack */ 4127 /* Send PULLs to some peers to learn about additional peers to attack */
4112 tmp_att_peer = att_peer_index; 4128 tmp_att_peer = att_peer_index;
4113 for (i = 0 ; i < num_pushes * alpha ; i++) 4129 for (i = 0; i < num_pushes * alpha; i++)
4114 { 4130 {
4115 if (att_peers_tail == tmp_att_peer) 4131 if (att_peers_tail == tmp_att_peer)
4116 tmp_att_peer = att_peers_head; 4132 tmp_att_peer = att_peers_head;
4117 else 4133 else
4118 att_peer_index = tmp_att_peer->next; 4134 att_peer_index = tmp_att_peer->next;
4119 4135
4120 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id)); 4136 send_pull_request(get_peer_ctx(sub->peer_map, &tmp_att_peer->peer_id));
4137 }
4121 } 4138 }
4122 }
4123 4139
4124 4140
4125 else if (2 == mal_type) 4141 else if (2 == mal_type)
4126 { /** 4142 { /**
4127 * Try to partition the network 4143 * Try to partition the network
4128 * Send as many pushes to the attacked peer as possible 4144 * Send as many pushes to the attacked peer as possible
4129 * That is one push per round as it will ignore more. 4145 * That is one push per round as it will ignore more.
4130 */ 4146 */
4131 (void) issue_peer_online_check (sub, &attacked_peer); 4147 (void)issue_peer_online_check(sub, &attacked_peer);
4132 if (GNUNET_YES == check_peer_flag (sub->peer_map, 4148 if (GNUNET_YES == check_peer_flag(sub->peer_map,
4133 &attacked_peer, 4149 &attacked_peer,
4134 Peers_ONLINE)) 4150 Peers_ONLINE))
4135 send_push (get_peer_ctx (sub->peer_map, &attacked_peer)); 4151 send_push(get_peer_ctx(sub->peer_map, &attacked_peer));
4136 } 4152 }
4137 4153
4138 4154
4139 if (3 == mal_type) 4155 if (3 == mal_type)
4140 { /* Combined attack */ 4156 { /* Combined attack */
4157 /* Send PUSH to attacked peers */
4158 if (GNUNET_YES == check_peer_known(sub->peer_map, &attacked_peer))
4159 {
4160 (void)issue_peer_online_check(sub, &attacked_peer);
4161 if (GNUNET_YES == check_peer_flag(sub->peer_map,
4162 &attacked_peer,
4163 Peers_ONLINE))
4164 {
4165 LOG(GNUNET_ERROR_TYPE_DEBUG,
4166 "Goding to send push to attacked peer (%s)\n",
4167 GNUNET_i2s(&attacked_peer));
4168 send_push(get_peer_ctx(sub->peer_map, &attacked_peer));
4169 }
4170 }
4171 (void)issue_peer_online_check(sub, &attacked_peer);
4141 4172
4142 /* Send PUSH to attacked peers */ 4173 /* The maximum of pushes we're going to send this round */
4143 if (GNUNET_YES == check_peer_known (sub->peer_map, &attacked_peer)) 4174 num_pushes = GNUNET_MIN(GNUNET_MIN(push_limit - 1,
4144 {
4145 (void) issue_peer_online_check (sub, &attacked_peer);
4146 if (GNUNET_YES == check_peer_flag (sub->peer_map,
4147 &attacked_peer,
4148 Peers_ONLINE))
4149 {
4150 LOG (GNUNET_ERROR_TYPE_DEBUG,
4151 "Goding to send push to attacked peer (%s)\n",
4152 GNUNET_i2s (&attacked_peer));
4153 send_push (get_peer_ctx (sub->peer_map, &attacked_peer));
4154 }
4155 }
4156 (void) issue_peer_online_check (sub, &attacked_peer);
4157
4158 /* The maximum of pushes we're going to send this round */
4159 num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
4160 num_attacked_peers), 4175 num_attacked_peers),
4161 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE); 4176 GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE);
4162 4177
4163 LOG (GNUNET_ERROR_TYPE_DEBUG, 4178 LOG(GNUNET_ERROR_TYPE_DEBUG,
4164 "Going to send %" PRIu32 " pushes\n", 4179 "Going to send %" PRIu32 " pushes\n",
4165 num_pushes); 4180 num_pushes);
4166 4181
4167 for (i = 0; i < num_pushes; i++) 4182 for (i = 0; i < num_pushes; i++)
4168 { 4183 {
4169 if (att_peers_tail == att_peer_index) 4184 if (att_peers_tail == att_peer_index)
4170 att_peer_index = att_peers_head; 4185 att_peer_index = att_peers_head;
4171 else 4186 else
4172 att_peer_index = att_peer_index->next; 4187 att_peer_index = att_peer_index->next;
4173 4188
4174 send_push (get_peer_ctx (sub->peer_map, &att_peer_index->peer_id)); 4189 send_push(get_peer_ctx(sub->peer_map, &att_peer_index->peer_id));
4175 } 4190 }
4176 4191
4177 /* Send PULLs to some peers to learn about additional peers to attack */ 4192 /* Send PULLs to some peers to learn about additional peers to attack */
4178 tmp_att_peer = att_peer_index; 4193 tmp_att_peer = att_peer_index;
4179 for (i = 0; i < num_pushes * alpha; i++) 4194 for (i = 0; i < num_pushes * alpha; i++)
4180 { 4195 {
4181 if (att_peers_tail == tmp_att_peer) 4196 if (att_peers_tail == tmp_att_peer)
4182 tmp_att_peer = att_peers_head; 4197 tmp_att_peer = att_peers_head;
4183 else 4198 else
4184 att_peer_index = tmp_att_peer->next; 4199 att_peer_index = tmp_att_peer->next;
4185 4200
4186 send_pull_request (get_peer_ctx (sub->peer_map, &tmp_att_peer->peer_id)); 4201 send_pull_request(get_peer_ctx(sub->peer_map, &tmp_att_peer->peer_id));
4202 }
4187 } 4203 }
4188 }
4189 4204
4190 /* Schedule next round */ 4205 /* Schedule next round */
4191 time_next_round = compute_rand_delay (sub->round_interval, 2); 4206 time_next_round = compute_rand_delay(sub->round_interval, 2);
4192 4207
4193 GNUNET_assert (NULL == sub->do_round_task); 4208 GNUNET_assert(NULL == sub->do_round_task);
4194 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, 4209 sub->do_round_task = GNUNET_SCHEDULER_add_delayed(time_next_round,
4195 &do_mal_round, sub); 4210 &do_mal_round, sub);
4196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n"); 4211 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4197} 4212}
4198#endif /* ENABLE_MALICIOUS */ 4213#endif /* ENABLE_MALICIOUS */
4199 4214
@@ -4206,7 +4221,7 @@ do_mal_round (void *cls)
4206 * @param cls Closure - Sub 4221 * @param cls Closure - Sub
4207 */ 4222 */
4208static void 4223static void
4209do_round (void *cls) 4224do_round(void *cls)
4210{ 4225{
4211 unsigned int i; 4226 unsigned int i;
4212 const struct GNUNET_PeerIdentity *view_array; 4227 const struct GNUNET_PeerIdentity *view_array;
@@ -4220,306 +4235,308 @@ do_round (void *cls)
4220 struct Sub *sub = cls; 4235 struct Sub *sub = cls;
4221 4236
4222 sub->num_rounds++; 4237 sub->num_rounds++;
4223 LOG (GNUNET_ERROR_TYPE_DEBUG, 4238 LOG(GNUNET_ERROR_TYPE_DEBUG,
4224 "Going to execute next round.\n"); 4239 "Going to execute next round.\n");
4225 if (sub == msub) 4240 if (sub == msub)
4226 { 4241 {
4227 GNUNET_STATISTICS_update (stats, "# rounds", 1, GNUNET_NO); 4242 GNUNET_STATISTICS_update(stats, "# rounds", 1, GNUNET_NO);
4228 } 4243 }
4229 sub->do_round_task = NULL; 4244 sub->do_round_task = NULL;
4230#ifdef TO_FILE_FULL 4245#ifdef TO_FILE_FULL
4231 to_file (sub->file_name_view_log, 4246 to_file(sub->file_name_view_log,
4232 "___ new round ___"); 4247 "___ new round ___");
4233#endif /* TO_FILE_FULL */ 4248#endif /* TO_FILE_FULL */
4234 view_array = View_get_as_array (sub->view); 4249 view_array = View_get_as_array(sub->view);
4235 for (i = 0; i < View_size (sub->view); i++) 4250 for (i = 0; i < View_size(sub->view); i++)
4236 { 4251 {
4237 LOG (GNUNET_ERROR_TYPE_DEBUG, 4252 LOG(GNUNET_ERROR_TYPE_DEBUG,
4238 "\t%s\n", GNUNET_i2s (&view_array[i])); 4253 "\t%s\n", GNUNET_i2s(&view_array[i]));
4239#ifdef TO_FILE_FULL 4254#ifdef TO_FILE_FULL
4240 to_file (sub->file_name_view_log, 4255 to_file(sub->file_name_view_log,
4241 "=%s\t(do round)", 4256 "=%s\t(do round)",
4242 GNUNET_i2s_full (&view_array[i])); 4257 GNUNET_i2s_full(&view_array[i]));
4243#endif /* TO_FILE_FULL */ 4258#endif /* TO_FILE_FULL */
4244 } 4259 }
4245 4260
4246 4261
4247 /* Send pushes and pull requests */ 4262 /* Send pushes and pull requests */
4248 if (0 < View_size (sub->view)) 4263 if (0 < View_size(sub->view))
4249 {
4250 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
4251 View_size (sub->view));
4252
4253 /* Send PUSHes */
4254 a_peers = ceil (alpha * View_size (sub->view));
4255
4256 LOG (GNUNET_ERROR_TYPE_DEBUG,
4257 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
4258 a_peers, alpha, View_size (sub->view));
4259 for (i = 0; i < a_peers; i++)
4260 { 4264 {
4261 peer = view_array[permut[i]]; 4265 permut = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG,
4262 // FIXME if this fails schedule/loop this for later 4266 View_size(sub->view));
4263 send_push (get_peer_ctx (sub->peer_map, &peer));
4264 }
4265 4267
4266 /* Send PULL requests */ 4268 /* Send PUSHes */
4267 b_peers = ceil (beta * View_size (sub->view)); 4269 a_peers = ceil(alpha * View_size(sub->view));
4268 first_border = a_peers; 4270
4269 second_border = a_peers + b_peers; 4271 LOG(GNUNET_ERROR_TYPE_DEBUG,
4270 if (second_border > View_size (sub->view)) 4272 "Going to send pushes to %u (ceil (%f * %u)) peers.\n",
4271 { 4273 a_peers, alpha, View_size(sub->view));
4272 first_border = View_size (sub->view) - b_peers; 4274 for (i = 0; i < a_peers; i++)
4273 second_border = View_size (sub->view); 4275 {
4274 } 4276 peer = view_array[permut[i]];
4275 LOG (GNUNET_ERROR_TYPE_DEBUG, 4277 // FIXME if this fails schedule/loop this for later
4276 "Going to send pulls to %u (ceil (%f * %u)) peers.\n", 4278 send_push(get_peer_ctx(sub->peer_map, &peer));
4277 b_peers, beta, View_size (sub->view)); 4279 }
4278 for (i = first_border; i < second_border; i++) 4280
4279 { 4281 /* Send PULL requests */
4280 peer = view_array[permut[i]]; 4282 b_peers = ceil(beta * View_size(sub->view));
4281 if ( GNUNET_NO == check_peer_flag (sub->peer_map, 4283 first_border = a_peers;
4282 &peer, 4284 second_border = a_peers + b_peers;
4283 Peers_PULL_REPLY_PENDING)) 4285 if (second_border > View_size(sub->view))
4284 { // FIXME if this fails schedule/loop this for later 4286 {
4285 send_pull_request (get_peer_ctx (sub->peer_map, &peer)); 4287 first_border = View_size(sub->view) - b_peers;
4286 } 4288 second_border = View_size(sub->view);
4287 } 4289 }
4290 LOG(GNUNET_ERROR_TYPE_DEBUG,
4291 "Going to send pulls to %u (ceil (%f * %u)) peers.\n",
4292 b_peers, beta, View_size(sub->view));
4293 for (i = first_border; i < second_border; i++)
4294 {
4295 peer = view_array[permut[i]];
4296 if (GNUNET_NO == check_peer_flag(sub->peer_map,
4297 &peer,
4298 Peers_PULL_REPLY_PENDING))
4299 { // FIXME if this fails schedule/loop this for later
4300 send_pull_request(get_peer_ctx(sub->peer_map, &peer));
4301 }
4302 }
4288 4303
4289 GNUNET_free (permut); 4304 GNUNET_free(permut);
4290 permut = NULL; 4305 permut = NULL;
4291 } 4306 }
4292 4307
4293 4308
4294 /* Update view */ 4309 /* Update view */
4295 /* TODO see how many peers are in push-/pull- list! */ 4310 /* TODO see how many peers are in push-/pull- list! */
4296 4311
4297 if ((CustomPeerMap_size (sub->push_map) <= alpha * sub->view_size_est_need) && 4312 if ((CustomPeerMap_size(sub->push_map) <= alpha * sub->view_size_est_need) &&
4298 (0 < CustomPeerMap_size (sub->push_map)) && 4313 (0 < CustomPeerMap_size(sub->push_map)) &&
4299 (0 < CustomPeerMap_size (sub->pull_map))) 4314 (0 < CustomPeerMap_size(sub->pull_map)))
4300 { /* If conditions for update are fulfilled, update */ 4315 { /* If conditions for update are fulfilled, update */
4301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n"); 4316 LOG(GNUNET_ERROR_TYPE_DEBUG, "Update of the view.\n");
4302 4317
4303 uint32_t final_size; 4318 uint32_t final_size;
4304 uint32_t peers_to_clean_size; 4319 uint32_t peers_to_clean_size;
4305 struct GNUNET_PeerIdentity *peers_to_clean; 4320 struct GNUNET_PeerIdentity *peers_to_clean;
4306 4321
4307 peers_to_clean = NULL; 4322 peers_to_clean = NULL;
4308 peers_to_clean_size = 0; 4323 peers_to_clean_size = 0;
4309 GNUNET_array_grow (peers_to_clean, 4324 GNUNET_array_grow(peers_to_clean,
4310 peers_to_clean_size, 4325 peers_to_clean_size,
4311 View_size (sub->view)); 4326 View_size(sub->view));
4312 GNUNET_memcpy (peers_to_clean, 4327 GNUNET_memcpy(peers_to_clean,
4313 view_array, 4328 view_array,
4314 View_size (sub->view) * sizeof (struct GNUNET_PeerIdentity)); 4329 View_size(sub->view) * sizeof(struct GNUNET_PeerIdentity));
4315 4330
4316 /* Seems like recreating is the easiest way of emptying the peermap */ 4331 /* Seems like recreating is the easiest way of emptying the peermap */
4317 View_clear (sub->view); 4332 View_clear(sub->view);
4318#ifdef TO_FILE_FULL 4333#ifdef TO_FILE_FULL
4319 to_file (sub->file_name_view_log, 4334 to_file(sub->file_name_view_log,
4320 "--- emptied ---"); 4335 "--- emptied ---");
4321#endif /* TO_FILE_FULL */ 4336#endif /* TO_FILE_FULL */
4322 4337
4323 first_border = GNUNET_MIN (ceil (alpha * sub->view_size_est_need), 4338 first_border = GNUNET_MIN(ceil(alpha * sub->view_size_est_need),
4324 CustomPeerMap_size (sub->push_map)); 4339 CustomPeerMap_size(sub->push_map));
4325 second_border = first_border + 4340 second_border = first_border +
4326 GNUNET_MIN (floor (beta * sub->view_size_est_need), 4341 GNUNET_MIN(floor(beta * sub->view_size_est_need),
4327 CustomPeerMap_size (sub->pull_map)); 4342 CustomPeerMap_size(sub->pull_map));
4328 final_size = second_border + 4343 final_size = second_border +
4329 ceil ((1 - (alpha + beta)) * sub->view_size_est_need); 4344 ceil((1 - (alpha + beta)) * sub->view_size_est_need);
4330 LOG (GNUNET_ERROR_TYPE_DEBUG, 4345 LOG(GNUNET_ERROR_TYPE_DEBUG,
4331 "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %"PRIu32 "\n", 4346 "first border: %" PRIu32 ", second border: %" PRIu32 ", final size: %" PRIu32 "\n",
4332 first_border, 4347 first_border,
4333 second_border, 4348 second_border,
4334 final_size); 4349 final_size);
4335 4350
4336 /* Update view with peers received through PUSHes */ 4351 /* Update view with peers received through PUSHes */
4337 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, 4352 permut = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG,
4338 CustomPeerMap_size (sub->push_map)); 4353 CustomPeerMap_size(sub->push_map));
4339 for (i = 0; i < first_border; i++) 4354 for (i = 0; i < first_border; i++)
4340 { 4355 {
4341 int inserted; 4356 int inserted;
4342 inserted = insert_in_view (sub, 4357 inserted = insert_in_view(sub,
4343 CustomPeerMap_get_peer_by_index (sub->push_map, 4358 CustomPeerMap_get_peer_by_index(sub->push_map,
4344 permut[i])); 4359 permut[i]));
4345 if (GNUNET_OK == inserted) 4360 if (GNUNET_OK == inserted)
4346 { 4361 {
4347 clients_notify_stream_peer (sub, 4362 clients_notify_stream_peer(sub,
4348 1, 4363 1,
4349 CustomPeerMap_get_peer_by_index (sub->push_map, permut[i])); 4364 CustomPeerMap_get_peer_by_index(sub->push_map, permut[i]));
4350 } 4365 }
4351#ifdef TO_FILE_FULL 4366#ifdef TO_FILE_FULL
4352 to_file (sub->file_name_view_log, 4367 to_file(sub->file_name_view_log,
4353 "+%s\t(push list)", 4368 "+%s\t(push list)",
4354 GNUNET_i2s_full (&view_array[i])); 4369 GNUNET_i2s_full(&view_array[i]));
4355#endif /* TO_FILE_FULL */ 4370#endif /* TO_FILE_FULL */
4356 // TODO change the peer_flags accordingly 4371 // TODO change the peer_flags accordingly
4357 } 4372 }
4358 GNUNET_free (permut); 4373 GNUNET_free(permut);
4359 permut = NULL; 4374 permut = NULL;
4360 4375
4361 /* Update view with peers received through PULLs */ 4376 /* Update view with peers received through PULLs */
4362 permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG, 4377 permut = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_STRONG,
4363 CustomPeerMap_size (sub->pull_map)); 4378 CustomPeerMap_size(sub->pull_map));
4364 for (i = first_border; i < second_border; i++) 4379 for (i = first_border; i < second_border; i++)
4365 { 4380 {
4366 int inserted; 4381 int inserted;
4367 inserted = insert_in_view (sub, 4382 inserted = insert_in_view(sub,
4368 CustomPeerMap_get_peer_by_index (sub->pull_map, 4383 CustomPeerMap_get_peer_by_index(sub->pull_map,
4369 permut[i - first_border])); 4384 permut[i - first_border]));
4370 if (GNUNET_OK == inserted) 4385 if (GNUNET_OK == inserted)
4371 { 4386 {
4372 clients_notify_stream_peer (sub, 4387 clients_notify_stream_peer(sub,
4373 1, 4388 1,
4374 CustomPeerMap_get_peer_by_index (sub->pull_map, 4389 CustomPeerMap_get_peer_by_index(sub->pull_map,
4375 permut[i - first_border])); 4390 permut[i - first_border]));
4376 } 4391 }
4377#ifdef TO_FILE_FULL 4392#ifdef TO_FILE_FULL
4378 to_file (sub->file_name_view_log, 4393 to_file(sub->file_name_view_log,
4379 "+%s\t(pull list)", 4394 "+%s\t(pull list)",
4380 GNUNET_i2s_full (&view_array[i])); 4395 GNUNET_i2s_full(&view_array[i]));
4381#endif /* TO_FILE_FULL */ 4396#endif /* TO_FILE_FULL */
4397 // TODO change the peer_flags accordingly
4398 }
4399 GNUNET_free(permut);
4400 permut = NULL;
4401
4402 /* Update view with peers from history */
4403 RPS_sampler_get_n_rand_peers(sub->sampler,
4404 final_size - second_border,
4405 hist_update,
4406 sub);
4382 // TODO change the peer_flags accordingly 4407 // TODO change the peer_flags accordingly
4383 }
4384 GNUNET_free (permut);
4385 permut = NULL;
4386 4408
4387 /* Update view with peers from history */ 4409 for (i = 0; i < View_size(sub->view); i++)
4388 RPS_sampler_get_n_rand_peers (sub->sampler, 4410 rem_from_list(&peers_to_clean, &peers_to_clean_size, &view_array[i]);
4389 final_size - second_border,
4390 hist_update,
4391 sub);
4392 // TODO change the peer_flags accordingly
4393 4411
4394 for (i = 0; i < View_size (sub->view); i++) 4412 /* Clean peers that were removed from the view */
4395 rem_from_list (&peers_to_clean, &peers_to_clean_size, &view_array[i]); 4413 for (i = 0; i < peers_to_clean_size; i++)
4396 4414 {
4397 /* Clean peers that were removed from the view */
4398 for (i = 0; i < peers_to_clean_size; i++)
4399 {
4400#ifdef TO_FILE_FULL 4415#ifdef TO_FILE_FULL
4401 to_file (sub->file_name_view_log, 4416 to_file(sub->file_name_view_log,
4402 "-%s", 4417 "-%s",
4403 GNUNET_i2s_full (&peers_to_clean[i])); 4418 GNUNET_i2s_full(&peers_to_clean[i]));
4404#endif /* TO_FILE_FULL */ 4419#endif /* TO_FILE_FULL */
4405 clean_peer (sub, &peers_to_clean[i]); 4420 clean_peer(sub, &peers_to_clean[i]);
4406 } 4421 }
4407 4422
4408 GNUNET_array_grow (peers_to_clean, peers_to_clean_size, 0); 4423 GNUNET_array_grow(peers_to_clean, peers_to_clean_size, 0);
4409 clients_notify_view_update (sub); 4424 clients_notify_view_update(sub);
4410 } else { 4425 }
4411 LOG (GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n"); 4426 else
4412 if (sub == msub)
4413 { 4427 {
4414 GNUNET_STATISTICS_update(stats, "# rounds blocked", 1, GNUNET_NO); 4428 LOG(GNUNET_ERROR_TYPE_DEBUG, "No update of the view.\n");
4415 if (CustomPeerMap_size (sub->push_map) > alpha * sub->view_size_est_need && 4429 if (sub == msub)
4416 !(0 >= CustomPeerMap_size (sub->pull_map))) 4430 {
4417 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes", 1, GNUNET_NO); 4431 GNUNET_STATISTICS_update(stats, "# rounds blocked", 1, GNUNET_NO);
4418 if (CustomPeerMap_size (sub->push_map) > alpha * sub->view_size_est_need && 4432 if (CustomPeerMap_size(sub->push_map) > alpha * sub->view_size_est_need &&
4419 (0 >= CustomPeerMap_size (sub->pull_map))) 4433 !(0 >= CustomPeerMap_size(sub->pull_map)))
4420 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes, no pull replies", 1, GNUNET_NO); 4434 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes", 1, GNUNET_NO);
4421 if (0 >= CustomPeerMap_size (sub->push_map) && 4435 if (CustomPeerMap_size(sub->push_map) > alpha * sub->view_size_est_need &&
4422 !(0 >= CustomPeerMap_size (sub->pull_map))) 4436 (0 >= CustomPeerMap_size(sub->pull_map)))
4423 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes", 1, GNUNET_NO); 4437 GNUNET_STATISTICS_update(stats, "# rounds blocked - too many pushes, no pull replies", 1, GNUNET_NO);
4424 if (0 >= CustomPeerMap_size (sub->push_map) && 4438 if (0 >= CustomPeerMap_size(sub->push_map) &&
4425 (0 >= CustomPeerMap_size (sub->pull_map))) 4439 !(0 >= CustomPeerMap_size(sub->pull_map)))
4426 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes, no pull replies", 1, GNUNET_NO); 4440 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes", 1, GNUNET_NO);
4427 if (0 >= CustomPeerMap_size (sub->pull_map) && 4441 if (0 >= CustomPeerMap_size(sub->push_map) &&
4428 CustomPeerMap_size (sub->push_map) > alpha * sub->view_size_est_need && 4442 (0 >= CustomPeerMap_size(sub->pull_map)))
4429 0 >= CustomPeerMap_size (sub->push_map)) 4443 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pushes, no pull replies", 1, GNUNET_NO);
4430 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pull replies", 1, GNUNET_NO); 4444 if (0 >= CustomPeerMap_size(sub->pull_map) &&
4431 } 4445 CustomPeerMap_size(sub->push_map) > alpha * sub->view_size_est_need &&
4432 } 4446 0 >= CustomPeerMap_size(sub->push_map))
4447 GNUNET_STATISTICS_update(stats, "# rounds blocked - no pull replies", 1, GNUNET_NO);
4448 }
4449 }
4433 // TODO independent of that also get some peers from CADET_get_peers()? 4450 // TODO independent of that also get some peers from CADET_get_peers()?
4434 if (CustomPeerMap_size (sub->push_map) < HISTOGRAM_FILE_SLOTS) 4451 if (CustomPeerMap_size(sub->push_map) < HISTOGRAM_FILE_SLOTS)
4435 { 4452 {
4436 sub->push_recv[CustomPeerMap_size (sub->push_map)]++; 4453 sub->push_recv[CustomPeerMap_size(sub->push_map)]++;
4437 } 4454 }
4438 else 4455 else
4439 { 4456 {
4440 LOG (GNUNET_ERROR_TYPE_WARNING, 4457 LOG(GNUNET_ERROR_TYPE_WARNING,
4441 "Push map size too big for histogram (%u, %u)\n", 4458 "Push map size too big for histogram (%u, %u)\n",
4442 CustomPeerMap_size (sub->push_map), 4459 CustomPeerMap_size(sub->push_map),
4443 HISTOGRAM_FILE_SLOTS); 4460 HISTOGRAM_FILE_SLOTS);
4444 } 4461 }
4445 // FIXME check bounds of histogram 4462 // FIXME check bounds of histogram
4446 sub->push_delta[(int32_t) (CustomPeerMap_size (sub->push_map) - 4463 sub->push_delta[(int32_t)(CustomPeerMap_size(sub->push_map) -
4447 (alpha * sub->view_size_est_need)) + 4464 (alpha * sub->view_size_est_need)) +
4448 (HISTOGRAM_FILE_SLOTS/2)]++; 4465 (HISTOGRAM_FILE_SLOTS / 2)]++;
4449 if (sub == msub) 4466 if (sub == msub)
4450 { 4467 {
4451 GNUNET_STATISTICS_set (stats, 4468 GNUNET_STATISTICS_set(stats,
4452 "# peers in push map at end of round", 4469 "# peers in push map at end of round",
4453 CustomPeerMap_size (sub->push_map), 4470 CustomPeerMap_size(sub->push_map),
4454 GNUNET_NO); 4471 GNUNET_NO);
4455 GNUNET_STATISTICS_set (stats, 4472 GNUNET_STATISTICS_set(stats,
4456 "# peers in pull map at end of round", 4473 "# peers in pull map at end of round",
4457 CustomPeerMap_size (sub->pull_map), 4474 CustomPeerMap_size(sub->pull_map),
4458 GNUNET_NO); 4475 GNUNET_NO);
4459 GNUNET_STATISTICS_set (stats, 4476 GNUNET_STATISTICS_set(stats,
4460 "# peers in view at end of round", 4477 "# peers in view at end of round",
4461 View_size (sub->view), 4478 View_size(sub->view),
4462 GNUNET_NO); 4479 GNUNET_NO);
4463 GNUNET_STATISTICS_set (stats, 4480 GNUNET_STATISTICS_set(stats,
4464 "# expected pushes", 4481 "# expected pushes",
4465 alpha * sub->view_size_est_need, 4482 alpha * sub->view_size_est_need,
4466 GNUNET_NO); 4483 GNUNET_NO);
4467 GNUNET_STATISTICS_set (stats, 4484 GNUNET_STATISTICS_set(stats,
4468 "delta expected - received pushes", 4485 "delta expected - received pushes",
4469 CustomPeerMap_size (sub->push_map) - (alpha * sub->view_size_est_need), 4486 CustomPeerMap_size(sub->push_map) - (alpha * sub->view_size_est_need),
4470 GNUNET_NO); 4487 GNUNET_NO);
4471 } 4488 }
4472 4489
4473 LOG (GNUNET_ERROR_TYPE_DEBUG, 4490 LOG(GNUNET_ERROR_TYPE_DEBUG,
4474 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (sub->view%u) = %.2f)\n", 4491 "Received %u pushes and %u pulls last round (alpha (%.2f) * view_size (sub->view%u) = %.2f)\n",
4475 CustomPeerMap_size (sub->push_map), 4492 CustomPeerMap_size(sub->push_map),
4476 CustomPeerMap_size (sub->pull_map), 4493 CustomPeerMap_size(sub->pull_map),
4477 alpha, 4494 alpha,
4478 View_size (sub->view), 4495 View_size(sub->view),
4479 alpha * View_size (sub->view)); 4496 alpha * View_size(sub->view));
4480 4497
4481 /* Update samplers */ 4498 /* Update samplers */
4482 for (i = 0; i < CustomPeerMap_size (sub->push_map); i++) 4499 for (i = 0; i < CustomPeerMap_size(sub->push_map); i++)
4483 { 4500 {
4484 update_peer = CustomPeerMap_get_peer_by_index (sub->push_map, i); 4501 update_peer = CustomPeerMap_get_peer_by_index(sub->push_map, i);
4485 LOG (GNUNET_ERROR_TYPE_DEBUG, 4502 LOG(GNUNET_ERROR_TYPE_DEBUG,
4486 "Updating with peer %s from push list\n", 4503 "Updating with peer %s from push list\n",
4487 GNUNET_i2s (update_peer)); 4504 GNUNET_i2s(update_peer));
4488 insert_in_sampler (sub, update_peer); 4505 insert_in_sampler(sub, update_peer);
4489 clean_peer (sub, update_peer); /* This cleans only if it is not in the view */ 4506 clean_peer(sub, update_peer); /* This cleans only if it is not in the view */
4490 } 4507 }
4491 4508
4492 for (i = 0; i < CustomPeerMap_size (sub->pull_map); i++) 4509 for (i = 0; i < CustomPeerMap_size(sub->pull_map); i++)
4493 { 4510 {
4494 LOG (GNUNET_ERROR_TYPE_DEBUG, 4511 LOG(GNUNET_ERROR_TYPE_DEBUG,
4495 "Updating with peer %s from pull list\n", 4512 "Updating with peer %s from pull list\n",
4496 GNUNET_i2s (CustomPeerMap_get_peer_by_index (sub->pull_map, i))); 4513 GNUNET_i2s(CustomPeerMap_get_peer_by_index(sub->pull_map, i)));
4497 insert_in_sampler (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i)); 4514 insert_in_sampler(sub, CustomPeerMap_get_peer_by_index(sub->pull_map, i));
4498 /* This cleans only if it is not in the view */ 4515 /* This cleans only if it is not in the view */
4499 clean_peer (sub, CustomPeerMap_get_peer_by_index (sub->pull_map, i)); 4516 clean_peer(sub, CustomPeerMap_get_peer_by_index(sub->pull_map, i));
4500 } 4517 }
4501 4518
4502 4519
4503 /* Empty push/pull lists */ 4520 /* Empty push/pull lists */
4504 CustomPeerMap_clear (sub->push_map); 4521 CustomPeerMap_clear(sub->push_map);
4505 CustomPeerMap_clear (sub->pull_map); 4522 CustomPeerMap_clear(sub->pull_map);
4506 4523
4507 if (sub == msub) 4524 if (sub == msub)
4508 { 4525 {
4509 GNUNET_STATISTICS_set (stats, 4526 GNUNET_STATISTICS_set(stats,
4510 "view size", 4527 "view size",
4511 View_size(sub->view), 4528 View_size(sub->view),
4512 GNUNET_NO); 4529 GNUNET_NO);
4513 } 4530 }
4514 4531
4515 struct GNUNET_TIME_Relative time_next_round; 4532 struct GNUNET_TIME_Relative time_next_round;
4516 4533
4517 time_next_round = compute_rand_delay (sub->round_interval, 2); 4534 time_next_round = compute_rand_delay(sub->round_interval, 2);
4518 4535
4519 /* Schedule next round */ 4536 /* Schedule next round */
4520 sub->do_round_task = GNUNET_SCHEDULER_add_delayed (time_next_round, 4537 sub->do_round_task = GNUNET_SCHEDULER_add_delayed(time_next_round,
4521 &do_round, sub); 4538 &do_round, sub);
4522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Finished round\n"); 4539 LOG(GNUNET_ERROR_TYPE_DEBUG, "Finished round\n");
4523} 4540}
4524 4541
4525 4542
@@ -4539,25 +4556,26 @@ do_round (void *cls)
4539 * (0 = unknown, 1 = ourselves, 2 = neighbor) 4556 * (0 = unknown, 1 = ourselves, 2 = neighbor)
4540 */ 4557 */
4541void 4558void
4542init_peer_cb (void *cls, 4559init_peer_cb(void *cls,
4543 const struct GNUNET_PeerIdentity *peer, 4560 const struct GNUNET_PeerIdentity *peer,
4544 int tunnel, /* "Do we have a tunnel towards this peer?" */ 4561 int tunnel, /* "Do we have a tunnel towards this peer?" */
4545 unsigned int n_paths, /* "Number of known paths towards this peer" */ 4562 unsigned int n_paths, /* "Number of known paths towards this peer" */
4546 unsigned int best_path) /* "How long is the best path? 4563 unsigned int best_path) /* "How long is the best path?
4547 * (0 = unknown, 1 = ourselves, 2 = neighbor)" */ 4564 * (0 = unknown, 1 = ourselves, 2 = neighbor)" */
4548{ 4565{
4549 struct Sub *sub = cls; 4566 struct Sub *sub = cls;
4550 (void) tunnel; 4567
4551 (void) n_paths; 4568 (void)tunnel;
4552 (void) best_path; 4569 (void)n_paths;
4570 (void)best_path;
4553 4571
4554 if (NULL != peer) 4572 if (NULL != peer)
4555 { 4573 {
4556 LOG (GNUNET_ERROR_TYPE_DEBUG, 4574 LOG(GNUNET_ERROR_TYPE_DEBUG,
4557 "Got peer_id %s from cadet\n", 4575 "Got peer_id %s from cadet\n",
4558 GNUNET_i2s (peer)); 4576 GNUNET_i2s(peer));
4559 got_peer (sub, peer); 4577 got_peer(sub, peer);
4560 } 4578 }
4561} 4579}
4562 4580
4563 4581
@@ -4573,18 +4591,18 @@ init_peer_cb (void *cls,
4573 * #GNUNET_NO if not. 4591 * #GNUNET_NO if not.
4574 */ 4592 */
4575static int 4593static int
4576valid_peers_iterator (void *cls, 4594valid_peers_iterator(void *cls,
4577 const struct GNUNET_PeerIdentity *peer) 4595 const struct GNUNET_PeerIdentity *peer)
4578{ 4596{
4579 struct Sub *sub = cls; 4597 struct Sub *sub = cls;
4580 4598
4581 if (NULL != peer) 4599 if (NULL != peer)
4582 { 4600 {
4583 LOG (GNUNET_ERROR_TYPE_DEBUG, 4601 LOG(GNUNET_ERROR_TYPE_DEBUG,
4584 "Got stored, valid peer %s\n", 4602 "Got stored, valid peer %s\n",
4585 GNUNET_i2s (peer)); 4603 GNUNET_i2s(peer));
4586 got_peer (sub, peer); 4604 got_peer(sub, peer);
4587 } 4605 }
4588 return GNUNET_YES; 4606 return GNUNET_YES;
4589} 4607}
4590 4608
@@ -4598,22 +4616,23 @@ valid_peers_iterator (void *cls,
4598 * @param error message 4616 * @param error message
4599 */ 4617 */
4600void 4618void
4601process_peerinfo_peers (void *cls, 4619process_peerinfo_peers(void *cls,
4602 const struct GNUNET_PeerIdentity *peer, 4620 const struct GNUNET_PeerIdentity *peer,
4603 const struct GNUNET_HELLO_Message *hello, 4621 const struct GNUNET_HELLO_Message *hello,
4604 const char *err_msg) 4622 const char *err_msg)
4605{ 4623{
4606 struct Sub *sub = cls; 4624 struct Sub *sub = cls;
4607 (void) hello; 4625
4608 (void) err_msg; 4626 (void)hello;
4627 (void)err_msg;
4609 4628
4610 if (NULL != peer) 4629 if (NULL != peer)
4611 { 4630 {
4612 LOG (GNUNET_ERROR_TYPE_DEBUG, 4631 LOG(GNUNET_ERROR_TYPE_DEBUG,
4613 "Got peer_id %s from peerinfo\n", 4632 "Got peer_id %s from peerinfo\n",
4614 GNUNET_i2s (peer)); 4633 GNUNET_i2s(peer));
4615 got_peer (sub, peer); 4634 got_peer(sub, peer);
4616 } 4635 }
4617} 4636}
4618 4637
4619 4638
@@ -4623,67 +4642,67 @@ process_peerinfo_peers (void *cls,
4623 * @param cls Closure - unused 4642 * @param cls Closure - unused
4624 */ 4643 */
4625static void 4644static void
4626shutdown_task (void *cls) 4645shutdown_task(void *cls)
4627{ 4646{
4628 (void) cls; 4647 (void)cls;
4629 struct ClientContext *client_ctx; 4648 struct ClientContext *client_ctx;
4630 4649
4631 LOG (GNUNET_ERROR_TYPE_DEBUG, 4650 LOG(GNUNET_ERROR_TYPE_DEBUG,
4632 "RPS service is going down\n"); 4651 "RPS service is going down\n");
4633 4652
4634 /* Clean all clients */ 4653 /* Clean all clients */
4635 for (client_ctx = cli_ctx_head; 4654 for (client_ctx = cli_ctx_head;
4636 NULL != cli_ctx_head; 4655 NULL != cli_ctx_head;
4637 client_ctx = cli_ctx_head) 4656 client_ctx = cli_ctx_head)
4638 { 4657 {
4639 destroy_cli_ctx (client_ctx); 4658 destroy_cli_ctx(client_ctx);
4640 } 4659 }
4641 if (NULL != msub) 4660 if (NULL != msub)
4642 { 4661 {
4643 destroy_sub (msub); 4662 destroy_sub(msub);
4644 msub = NULL; 4663 msub = NULL;
4645 } 4664 }
4646 4665
4647 /* Disconnect from other services */ 4666 /* Disconnect from other services */
4648 GNUNET_PEERINFO_notify_cancel (peerinfo_notify_handle); 4667 GNUNET_PEERINFO_notify_cancel(peerinfo_notify_handle);
4649 GNUNET_PEERINFO_disconnect (peerinfo_handle); 4668 GNUNET_PEERINFO_disconnect(peerinfo_handle);
4650 peerinfo_handle = NULL; 4669 peerinfo_handle = NULL;
4651 GNUNET_NSE_disconnect (nse); 4670 GNUNET_NSE_disconnect(nse);
4652 if (NULL != map_single_hop) 4671 if (NULL != map_single_hop)
4653 { 4672 {
4654 /* core_init was called - core was initialised */ 4673 /* core_init was called - core was initialised */
4655 /* disconnect first, so no callback tries to access missing peermap */ 4674 /* disconnect first, so no callback tries to access missing peermap */
4656 GNUNET_CORE_disconnect (core_handle); 4675 GNUNET_CORE_disconnect(core_handle);
4657 core_handle = NULL; 4676 core_handle = NULL;
4658 GNUNET_CONTAINER_multipeermap_destroy (map_single_hop); 4677 GNUNET_CONTAINER_multipeermap_destroy(map_single_hop);
4659 map_single_hop = NULL; 4678 map_single_hop = NULL;
4660 } 4679 }
4661 4680
4662 if (NULL != stats) 4681 if (NULL != stats)
4663 { 4682 {
4664 GNUNET_STATISTICS_destroy (stats, 4683 GNUNET_STATISTICS_destroy(stats,
4665 GNUNET_NO); 4684 GNUNET_NO);
4666 stats = NULL; 4685 stats = NULL;
4667 } 4686 }
4668 GNUNET_CADET_disconnect (cadet_handle); 4687 GNUNET_CADET_disconnect(cadet_handle);
4669 cadet_handle = NULL; 4688 cadet_handle = NULL;
4670#if ENABLE_MALICIOUS 4689#if ENABLE_MALICIOUS
4671 struct AttackedPeer *tmp_att_peer; 4690 struct AttackedPeer *tmp_att_peer;
4672 GNUNET_array_grow (mal_peers, 4691 GNUNET_array_grow(mal_peers,
4673 num_mal_peers, 4692 num_mal_peers,
4674 0); 4693 0);
4675 if (NULL != mal_peer_set) 4694 if (NULL != mal_peer_set)
4676 GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set); 4695 GNUNET_CONTAINER_multipeermap_destroy(mal_peer_set);
4677 if (NULL != att_peer_set) 4696 if (NULL != att_peer_set)
4678 GNUNET_CONTAINER_multipeermap_destroy (att_peer_set); 4697 GNUNET_CONTAINER_multipeermap_destroy(att_peer_set);
4679 while (NULL != att_peers_head) 4698 while (NULL != att_peers_head)
4680 { 4699 {
4681 tmp_att_peer = att_peers_head; 4700 tmp_att_peer = att_peers_head;
4682 GNUNET_CONTAINER_DLL_remove (att_peers_head, 4701 GNUNET_CONTAINER_DLL_remove(att_peers_head,
4683 att_peers_tail, 4702 att_peers_tail,
4684 tmp_att_peer); 4703 tmp_att_peer);
4685 GNUNET_free (tmp_att_peer); 4704 GNUNET_free(tmp_att_peer);
4686 } 4705 }
4687#endif /* ENABLE_MALICIOUS */ 4706#endif /* ENABLE_MALICIOUS */
4688 close_all_files(); 4707 close_all_files();
4689} 4708}
@@ -4698,25 +4717,26 @@ shutdown_task (void *cls)
4698 * @return @a client 4717 * @return @a client
4699 */ 4718 */
4700static void * 4719static void *
4701client_connect_cb (void *cls, 4720client_connect_cb(void *cls,
4702 struct GNUNET_SERVICE_Client *client, 4721 struct GNUNET_SERVICE_Client *client,
4703 struct GNUNET_MQ_Handle *mq) 4722 struct GNUNET_MQ_Handle *mq)
4704{ 4723{
4705 struct ClientContext *cli_ctx; 4724 struct ClientContext *cli_ctx;
4706 (void) cls;
4707 4725
4708 LOG (GNUNET_ERROR_TYPE_DEBUG, 4726 (void)cls;
4709 "Client connected\n"); 4727
4728 LOG(GNUNET_ERROR_TYPE_DEBUG,
4729 "Client connected\n");
4710 if (NULL == client) 4730 if (NULL == client)
4711 return client; /* Server was destroyed before a client connected. Shutting down */ 4731 return client; /* Server was destroyed before a client connected. Shutting down */
4712 cli_ctx = GNUNET_new (struct ClientContext); 4732 cli_ctx = GNUNET_new(struct ClientContext);
4713 cli_ctx->mq = mq; 4733 cli_ctx->mq = mq;
4714 cli_ctx->view_updates_left = -1; 4734 cli_ctx->view_updates_left = -1;
4715 cli_ctx->stream_update = GNUNET_NO; 4735 cli_ctx->stream_update = GNUNET_NO;
4716 cli_ctx->client = client; 4736 cli_ctx->client = client;
4717 GNUNET_CONTAINER_DLL_insert (cli_ctx_head, 4737 GNUNET_CONTAINER_DLL_insert(cli_ctx_head,
4718 cli_ctx_tail, 4738 cli_ctx_tail,
4719 cli_ctx); 4739 cli_ctx);
4720 return cli_ctx; 4740 return cli_ctx;
4721} 4741}
4722 4742
@@ -4728,25 +4748,25 @@ client_connect_cb (void *cls,
4728 * @param internal_cls should be equal to @a c 4748 * @param internal_cls should be equal to @a c
4729 */ 4749 */
4730static void 4750static void
4731client_disconnect_cb (void *cls, 4751client_disconnect_cb(void *cls,
4732 struct GNUNET_SERVICE_Client *client, 4752 struct GNUNET_SERVICE_Client *client,
4733 void *internal_cls) 4753 void *internal_cls)
4734{ 4754{
4735 struct ClientContext *cli_ctx = internal_cls; 4755 struct ClientContext *cli_ctx = internal_cls;
4736 4756
4737 (void) cls; 4757 (void)cls;
4738 GNUNET_assert (client == cli_ctx->client); 4758 GNUNET_assert(client == cli_ctx->client);
4739 if (NULL == client) 4759 if (NULL == client)
4740 {/* shutdown task - destroy all clients */ 4760 {/* shutdown task - destroy all clients */
4741 while (NULL != cli_ctx_head) 4761 while (NULL != cli_ctx_head)
4742 destroy_cli_ctx (cli_ctx_head); 4762 destroy_cli_ctx(cli_ctx_head);
4743 } 4763 }
4744 else 4764 else
4745 { /* destroy this client */ 4765 { /* destroy this client */
4746 LOG (GNUNET_ERROR_TYPE_DEBUG, 4766 LOG(GNUNET_ERROR_TYPE_DEBUG,
4747 "Client disconnected. Destroy its context.\n"); 4767 "Client disconnected. Destroy its context.\n");
4748 destroy_cli_ctx (cli_ctx); 4768 destroy_cli_ctx(cli_ctx);
4749 } 4769 }
4750} 4770}
4751 4771
4752 4772
@@ -4758,104 +4778,104 @@ client_disconnect_cb (void *cls,
4758 * @param service the initialized service 4778 * @param service the initialized service
4759 */ 4779 */
4760static void 4780static void
4761run (void *cls, 4781run(void *cls,
4762 const struct GNUNET_CONFIGURATION_Handle *c, 4782 const struct GNUNET_CONFIGURATION_Handle *c,
4763 struct GNUNET_SERVICE_Handle *service) 4783 struct GNUNET_SERVICE_Handle *service)
4764{ 4784{
4765 struct GNUNET_TIME_Relative round_interval; 4785 struct GNUNET_TIME_Relative round_interval;
4766 long long unsigned int sampler_size; 4786 long long unsigned int sampler_size;
4767 char hash_port_string[] = GNUNET_APPLICATION_PORT_RPS; 4787 char hash_port_string[] = GNUNET_APPLICATION_PORT_RPS;
4768 struct GNUNET_HashCode hash; 4788 struct GNUNET_HashCode hash;
4769 4789
4770 (void) cls; 4790 (void)cls;
4771 (void) service; 4791 (void)service;
4772 4792
4773 GNUNET_log_setup ("rps", 4793 GNUNET_log_setup("rps",
4774 GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), 4794 GNUNET_error_type_to_string(GNUNET_ERROR_TYPE_DEBUG),
4775 NULL); 4795 NULL);
4776 cfg = c; 4796 cfg = c;
4777 /* Get own ID */ 4797 /* Get own ID */
4778 GNUNET_CRYPTO_get_peer_identity (cfg, 4798 GNUNET_CRYPTO_get_peer_identity(cfg,
4779 &own_identity); // TODO check return value 4799 &own_identity); // TODO check return value
4780 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 4800 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
4781 "STARTING SERVICE (rps) for peer [%s]\n", 4801 "STARTING SERVICE (rps) for peer [%s]\n",
4782 GNUNET_i2s (&own_identity)); 4802 GNUNET_i2s(&own_identity));
4783#if ENABLE_MALICIOUS 4803#if ENABLE_MALICIOUS
4784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 4804 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
4785 "Malicious execution compiled in.\n"); 4805 "Malicious execution compiled in.\n");
4786#endif /* ENABLE_MALICIOUS */ 4806#endif /* ENABLE_MALICIOUS */
4787 4807
4788 /* Get time interval from the configuration */ 4808 /* Get time interval from the configuration */
4789 if (GNUNET_OK != 4809 if (GNUNET_OK !=
4790 GNUNET_CONFIGURATION_get_value_time (cfg, 4810 GNUNET_CONFIGURATION_get_value_time(cfg,
4791 "RPS", 4811 "RPS",
4792 "ROUNDINTERVAL", 4812 "ROUNDINTERVAL",
4793 &round_interval)) 4813 &round_interval))
4794 { 4814 {
4795 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 4815 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
4796 "RPS", "ROUNDINTERVAL"); 4816 "RPS", "ROUNDINTERVAL");
4797 GNUNET_SCHEDULER_shutdown (); 4817 GNUNET_SCHEDULER_shutdown();
4798 return; 4818 return;
4799 } 4819 }
4800 4820
4801 /* Get initial size of sampler/view from the configuration */ 4821 /* Get initial size of sampler/view from the configuration */
4802 if (GNUNET_OK != 4822 if (GNUNET_OK !=
4803 GNUNET_CONFIGURATION_get_value_number (cfg, 4823 GNUNET_CONFIGURATION_get_value_number(cfg,
4804 "RPS", 4824 "RPS",
4805 "MINSIZE", 4825 "MINSIZE",
4806 &sampler_size)) 4826 &sampler_size))
4807 { 4827 {
4808 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 4828 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
4809 "RPS", "MINSIZE"); 4829 "RPS", "MINSIZE");
4810 GNUNET_SCHEDULER_shutdown (); 4830 GNUNET_SCHEDULER_shutdown();
4811 return; 4831 return;
4812 } 4832 }
4813 4833
4814 cadet_handle = GNUNET_CADET_connect (cfg); 4834 cadet_handle = GNUNET_CADET_connect(cfg);
4815 GNUNET_assert (NULL != cadet_handle); 4835 GNUNET_assert(NULL != cadet_handle);
4816 core_handle = GNUNET_CORE_connect (cfg, 4836 core_handle = GNUNET_CORE_connect(cfg,
4817 NULL, /* cls */ 4837 NULL, /* cls */
4818 core_init, /* init */ 4838 core_init, /* init */
4819 core_connects, /* connects */ 4839 core_connects, /* connects */
4820 core_disconnects, /* disconnects */ 4840 core_disconnects, /* disconnects */
4821 NULL); /* handlers */ 4841 NULL); /* handlers */
4822 GNUNET_assert (NULL != core_handle); 4842 GNUNET_assert(NULL != core_handle);
4823 4843
4824 4844
4825 alpha = 0.45; 4845 alpha = 0.45;
4826 beta = 0.45; 4846 beta = 0.45;
4827 4847
4828 4848
4829 /* Set up main Sub */ 4849 /* Set up main Sub */
4830 GNUNET_CRYPTO_hash (hash_port_string, 4850 GNUNET_CRYPTO_hash(hash_port_string,
4831 strlen (hash_port_string), 4851 strlen(hash_port_string),
4832 &hash); 4852 &hash);
4833 msub = new_sub (&hash, 4853 msub = new_sub(&hash,
4834 sampler_size, /* Will be overwritten by config */ 4854 sampler_size, /* Will be overwritten by config */
4835 round_interval); 4855 round_interval);
4836 4856
4837 4857
4838 peerinfo_handle = GNUNET_PEERINFO_connect (cfg); 4858 peerinfo_handle = GNUNET_PEERINFO_connect(cfg);
4839 4859
4840 /* connect to NSE */ 4860 /* connect to NSE */
4841 nse = GNUNET_NSE_connect (cfg, nse_callback, NULL); 4861 nse = GNUNET_NSE_connect(cfg, nse_callback, NULL);
4842 4862
4843 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n"); 4863 //LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
4844 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, msub); 4864 //GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, msub);
4845 // TODO send push/pull to each of those peers? 4865 // TODO send push/pull to each of those peers?
4846 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n"); 4866 LOG(GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
4847 restore_valid_peers (msub); 4867 restore_valid_peers(msub);
4848 get_valid_peers (msub->valid_peers, valid_peers_iterator, msub); 4868 get_valid_peers(msub->valid_peers, valid_peers_iterator, msub);
4849 4869
4850 peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg, 4870 peerinfo_notify_handle = GNUNET_PEERINFO_notify(cfg,
4851 GNUNET_NO, 4871 GNUNET_NO,
4852 process_peerinfo_peers, 4872 process_peerinfo_peers,
4853 msub); 4873 msub);
4854 4874
4855 LOG (GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n"); 4875 LOG(GNUNET_ERROR_TYPE_INFO, "Ready to receive requests from clients\n");
4856 4876
4857 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 4877 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
4858 stats = GNUNET_STATISTICS_create ("rps", cfg); 4878 stats = GNUNET_STATISTICS_create("rps", cfg);
4859} 4879}
4860 4880
4861 4881
@@ -4863,46 +4883,46 @@ run (void *cls,
4863 * Define "main" method using service macro. 4883 * Define "main" method using service macro.
4864 */ 4884 */
4865GNUNET_SERVICE_MAIN 4885GNUNET_SERVICE_MAIN
4866("rps", 4886 ("rps",
4867 GNUNET_SERVICE_OPTION_NONE, 4887 GNUNET_SERVICE_OPTION_NONE,
4868 &run, 4888 &run,
4869 &client_connect_cb, 4889 &client_connect_cb,
4870 &client_disconnect_cb, 4890 &client_disconnect_cb,
4871 NULL, 4891 NULL,
4872 GNUNET_MQ_hd_var_size (client_seed, 4892 GNUNET_MQ_hd_var_size(client_seed,
4873 GNUNET_MESSAGE_TYPE_RPS_CS_SEED, 4893 GNUNET_MESSAGE_TYPE_RPS_CS_SEED,
4874 struct GNUNET_RPS_CS_SeedMessage, 4894 struct GNUNET_RPS_CS_SeedMessage,
4875 NULL), 4895 NULL),
4876#if ENABLE_MALICIOUS 4896#if ENABLE_MALICIOUS
4877 GNUNET_MQ_hd_var_size (client_act_malicious, 4897 GNUNET_MQ_hd_var_size(client_act_malicious,
4878 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS, 4898 GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS,
4879 struct GNUNET_RPS_CS_ActMaliciousMessage, 4899 struct GNUNET_RPS_CS_ActMaliciousMessage,
4880 NULL), 4900 NULL),
4881#endif /* ENABLE_MALICIOUS */ 4901#endif /* ENABLE_MALICIOUS */
4882 GNUNET_MQ_hd_fixed_size (client_view_request, 4902 GNUNET_MQ_hd_fixed_size(client_view_request,
4883 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST, 4903 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST,
4884 struct GNUNET_RPS_CS_DEBUG_ViewRequest, 4904 struct GNUNET_RPS_CS_DEBUG_ViewRequest,
4885 NULL), 4905 NULL),
4886 GNUNET_MQ_hd_fixed_size (client_view_cancel, 4906 GNUNET_MQ_hd_fixed_size(client_view_cancel,
4887 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL, 4907 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL,
4888 struct GNUNET_MessageHeader, 4908 struct GNUNET_MessageHeader,
4889 NULL), 4909 NULL),
4890 GNUNET_MQ_hd_fixed_size (client_stream_request, 4910 GNUNET_MQ_hd_fixed_size(client_stream_request,
4891 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST, 4911 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST,
4892 struct GNUNET_RPS_CS_DEBUG_StreamRequest, 4912 struct GNUNET_RPS_CS_DEBUG_StreamRequest,
4893 NULL), 4913 NULL),
4894 GNUNET_MQ_hd_fixed_size (client_stream_cancel, 4914 GNUNET_MQ_hd_fixed_size(client_stream_cancel,
4895 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL, 4915 GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL,
4896 struct GNUNET_MessageHeader, 4916 struct GNUNET_MessageHeader,
4897 NULL), 4917 NULL),
4898 GNUNET_MQ_hd_fixed_size (client_start_sub, 4918 GNUNET_MQ_hd_fixed_size(client_start_sub,
4899 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START, 4919 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_START,
4900 struct GNUNET_RPS_CS_SubStartMessage, 4920 struct GNUNET_RPS_CS_SubStartMessage,
4901 NULL), 4921 NULL),
4902 GNUNET_MQ_hd_fixed_size (client_stop_sub, 4922 GNUNET_MQ_hd_fixed_size(client_stop_sub,
4903 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP, 4923 GNUNET_MESSAGE_TYPE_RPS_CS_SUB_STOP,
4904 struct GNUNET_RPS_CS_SubStopMessage, 4924 struct GNUNET_RPS_CS_SubStopMessage,
4905 NULL), 4925 NULL),
4906 GNUNET_MQ_handler_end()); 4926 GNUNET_MQ_handler_end());
4907 4927
4908/* end of gnunet-service-rps.c */ 4928/* end of gnunet-service-rps.c */