aboutsummaryrefslogtreecommitdiff
path: root/src/rps
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2015-07-28 14:11:17 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2015-07-28 14:11:17 +0000
commit387ce05fe798b8baa07c04af2750d97b571d2460 (patch)
treeb9b815670040d7cf926d8c7838cca9eb148edb52 /src/rps
parent6cc5c45875bffd111a7c6161d1857433ea420cc2 (diff)
downloadgnunet-387ce05fe798b8baa07c04af2750d97b571d2460.tar.gz
gnunet-387ce05fe798b8baa07c04af2750d97b571d2460.zip
code cleanup
Diffstat (limited to 'src/rps')
-rw-r--r--src/rps/gnunet-service-rps.c233
1 files changed, 112 insertions, 121 deletions
diff --git a/src/rps/gnunet-service-rps.c b/src/rps/gnunet-service-rps.c
index 8ff3755e2..f0393bdf0 100644
--- a/src/rps/gnunet-service-rps.c
+++ b/src/rps/gnunet-service-rps.c
@@ -141,11 +141,6 @@ struct PeerOutstandingOp
141struct PeerContext 141struct PeerContext
142{ 142{
143 /** 143 /**
144 * Flags indicating status of peer
145 */
146 uint32_t peer_flags;
147
148 /**
149 * Message queue open to client 144 * Message queue open to client
150 */ 145 */
151 struct GNUNET_MQ_Handle *mq; 146 struct GNUNET_MQ_Handle *mq;
@@ -166,22 +161,26 @@ struct PeerContext
166 struct PeerOutstandingOp *outstanding_ops; 161 struct PeerOutstandingOp *outstanding_ops;
167 162
168 /** 163 /**
169 * Number of outstanding operations.
170 */
171 unsigned int num_outstanding_ops;
172 //size_t num_outstanding_ops;
173
174 /**
175 * Handle to the callback given to cadet_ntfy_tmt_rdy() 164 * Handle to the callback given to cadet_ntfy_tmt_rdy()
176 * 165 *
177 * To be canceled on shutdown. 166 * To be canceled on shutdown.
178 */ 167 */
179 struct GNUNET_CADET_TransmitHandle *is_live_task; 168 struct GNUNET_CADET_TransmitHandle *transmit_handle;
169
170 /**
171 * Number of outstanding operations.
172 */
173 unsigned int num_outstanding_ops;
180 174
181 /** 175 /**
182 * Identity of the peer 176 * Identity of the peer
183 */ 177 */
184 struct GNUNET_PeerIdentity peer_id; 178 struct GNUNET_PeerIdentity peer_id;
179
180 /**
181 * Flags indicating status of peer
182 */
183 uint32_t peer_flags;
185 184
186 /** 185 /**
187 * This is pobably followed by 'statistical' data (when we first saw 186 * This is pobably followed by 'statistical' data (when we first saw
@@ -651,29 +650,32 @@ get_rand_peer_ignore_list (const struct GNUNET_PeerIdentity *peer_list,
651 * Get the context of a peer. If not existing, create. 650 * Get the context of a peer. If not existing, create.
652 */ 651 */
653 struct PeerContext * 652 struct PeerContext *
654get_peer_ctx (struct GNUNET_CONTAINER_MultiPeerMap *peer_map, 653get_peer_ctx (const struct GNUNET_PeerIdentity *peer)
655 const struct GNUNET_PeerIdentity *peer)
656{ 654{
657 struct PeerContext *ctx; 655 struct PeerContext *ctx;
656 int ret;
658 657
659 if ( GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) 658 ret = GNUNET_CONTAINER_multipeermap_contains (peer_map, peer);
660 { 659 GNUNET_assert (GNUNET_YES == ret);
661 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer); 660 ctx = GNUNET_CONTAINER_multipeermap_get (peer_map, peer);
662 } 661 GNUNET_assert (NULL != ctx);
663 else 662 return ctx;
664 { 663}
665 ctx = GNUNET_new (struct PeerContext); 664
666 ctx->peer_flags = 0; 665/**
667 ctx->mq = NULL; 666 * Create a new peer context and insert it into the peer map
668 ctx->send_channel = NULL; 667 */
669 ctx->recv_channel = NULL; 668struct PeerContext *
670 ctx->outstanding_ops = NULL; 669create_peer_ctx (const struct GNUNET_PeerIdentity *peer)
671 ctx->num_outstanding_ops = 0; 670{
672 ctx->is_live_task = NULL; 671 struct PeerContext *ctx;
673 ctx->peer_id = *peer; 672 int ret;
674 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx, 673
675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 674 ctx = GNUNET_new (struct PeerContext);
676 } 675 ctx->peer_id = *peer;
676 ret = GNUNET_CONTAINER_multipeermap_put (peer_map, peer, ctx,
677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
678 GNUNET_assert (GNUNET_OK == ret);
677 return ctx; 679 return ctx;
678} 680}
679 681
@@ -724,11 +726,11 @@ peer_is_live (struct PeerContext *peer_ctx)
724{ 726{
725 struct GNUNET_PeerIdentity *peer; 727 struct GNUNET_PeerIdentity *peer;
726 728
727 /* Cancle is_live_task if still scheduled */ 729 /* Cancle transmit_handle if still scheduled */
728 if (NULL != peer_ctx->is_live_task) 730 if (NULL != peer_ctx->transmit_handle)
729 { 731 {
730 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task); 732 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
731 peer_ctx->is_live_task = NULL; 733 peer_ctx->transmit_handle = NULL;
732 } 734 }
733 735
734 peer = &peer_ctx->peer_id; 736 peer = &peer_ctx->peer_id;
@@ -759,9 +761,9 @@ cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
759{ 761{
760 struct PeerContext *peer_ctx = (struct PeerContext *) cls; 762 struct PeerContext *peer_ctx = (struct PeerContext *) cls;
761 763
762 peer_ctx->is_live_task = NULL; 764 peer_ctx->transmit_handle = NULL;
763 LOG (GNUNET_ERROR_TYPE_DEBUG, 765 LOG (GNUNET_ERROR_TYPE_DEBUG,
764 "Set ->is_live_task = NULL for peer %s\n", 766 "Set ->transmit_handle = NULL for peer %s\n",
765 GNUNET_i2s (&peer_ctx->peer_id)); 767 GNUNET_i2s (&peer_ctx->peer_id));
766 768
767 if (NULL != buf 769 if (NULL != buf
@@ -777,13 +779,13 @@ cadet_ntfy_tmt_rdy_cb (void *cls, size_t size, void *buf)
777 // TODO reschedule? cleanup? 779 // TODO reschedule? cleanup?
778 } 780 }
779 781
780 //if (NULL != peer_ctx->is_live_task) 782 //if (NULL != peer_ctx->transmit_handle)
781 //{ 783 //{
782 // LOG (GNUNET_ERROR_TYPE_DEBUG, 784 // LOG (GNUNET_ERROR_TYPE_DEBUG,
783 // "Trying to cancle is_live_task for peer %s\n", 785 // "Trying to cancle transmit_handle for peer %s\n",
784 // GNUNET_i2s (&peer_ctx->peer_id)); 786 // GNUNET_i2s (&peer_ctx->peer_id));
785 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task); 787 // GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
786 // peer_ctx->is_live_task = NULL; 788 // peer_ctx->transmit_handle = NULL;
787 //} 789 //}
788 790
789 return 0; 791 return 0;
@@ -799,8 +801,7 @@ get_channel (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
799{ 801{
800 struct PeerContext *peer_ctx; 802 struct PeerContext *peer_ctx;
801 803
802 peer_ctx = get_peer_ctx (peer_map, peer); 804 peer_ctx = get_peer_ctx (peer);
803
804 if (NULL == peer_ctx->send_channel) 805 if (NULL == peer_ctx->send_channel)
805 { 806 {
806 LOG (GNUNET_ERROR_TYPE_DEBUG, 807 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -831,9 +832,9 @@ get_mq (struct GNUNET_CONTAINER_MultiPeerMap *peer_map,
831{ 832{
832 struct PeerContext *peer_ctx; 833 struct PeerContext *peer_ctx;
833 834
834 peer_ctx = get_peer_ctx (peer_map, peer_id); 835 peer_ctx = get_peer_ctx (peer_id);
835 836
836 GNUNET_assert (NULL == peer_ctx->is_live_task); 837 GNUNET_assert (NULL == peer_ctx->transmit_handle);
837 838
838 if (NULL == peer_ctx->mq) 839 if (NULL == peer_ctx->mq)
839 { 840 {
@@ -859,11 +860,11 @@ check_peer_live (struct PeerContext *peer_ctx)
859 "Get informed about peer %s getting live\n", 860 "Get informed about peer %s getting live\n",
860 GNUNET_i2s (&peer_ctx->peer_id)); 861 GNUNET_i2s (&peer_ctx->peer_id));
861 862
862 if (NULL == peer_ctx->is_live_task && 863 if (NULL == peer_ctx->transmit_handle &&
863 NULL == peer_ctx->send_channel) 864 NULL == peer_ctx->send_channel)
864 { 865 {
865 (void) get_channel (peer_map, &peer_ctx->peer_id); 866 (void) get_channel (peer_map, &peer_ctx->peer_id);
866 peer_ctx->is_live_task = 867 peer_ctx->transmit_handle =
867 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel, 868 GNUNET_CADET_notify_transmit_ready (peer_ctx->send_channel,
868 GNUNET_NO, 869 GNUNET_NO,
869 GNUNET_TIME_UNIT_FOREVER_REL, 870 GNUNET_TIME_UNIT_FOREVER_REL,
@@ -871,7 +872,7 @@ check_peer_live (struct PeerContext *peer_ctx)
871 cadet_ntfy_tmt_rdy_cb, 872 cadet_ntfy_tmt_rdy_cb,
872 peer_ctx); 873 peer_ctx);
873 } 874 }
874 else if (NULL != peer_ctx->is_live_task) 875 else if (NULL != peer_ctx->transmit_handle)
875 LOG (GNUNET_ERROR_TYPE_DEBUG, 876 LOG (GNUNET_ERROR_TYPE_DEBUG,
876 "Already waiting for notification\n"); 877 "Already waiting for notification\n");
877 else if (NULL != peer_ctx->send_channel) 878 else if (NULL != peer_ctx->send_channel)
@@ -1185,43 +1186,47 @@ new_peer_id (const struct GNUNET_PeerIdentity *peer_id)
1185 struct PeerOutstandingOp out_op; 1186 struct PeerOutstandingOp out_op;
1186 struct PeerContext *peer_ctx; 1187 struct PeerContext *peer_ctx;
1187 1188
1188 if (NULL != peer_id && 1189 if ((NULL == peer_id) ||
1189 0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)) 1190 (0 == GNUNET_CRYPTO_cmp_peer_identity (&own_identity, peer_id)))
1190 { 1191 return;
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, 1192 LOG (GNUNET_ERROR_TYPE_DEBUG,
1192 "Got peer_id %s (at %p, view size: %u)\n", 1193 "Got peer_id %s (at %p, view size: %u)\n",
1193 GNUNET_i2s (peer_id), 1194 GNUNET_i2s (peer_id),
1194 peer_id, 1195 peer_id,
1195 GNUNET_CONTAINER_multipeermap_size (view)); 1196 GNUNET_CONTAINER_multipeermap_size (view));
1197
1198 /* if the seed peer is already know, skip context creation */
1199 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer_id))
1200 peer_ctx = create_peer_ctx (peer_id);
1201 else
1202 peer_ctx = get_peer_ctx (peer_id);
1196 1203
1197 peer_ctx = get_peer_ctx (peer_map, peer_id); 1204 if (GNUNET_NO == get_peer_flag (peer_ctx, VALID))
1198 if (GNUNET_YES != get_peer_flag (peer_ctx, VALID)) 1205 {
1206 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx))
1199 { 1207 {
1200 if (GNUNET_NO == insert_in_sampler_scheduled (peer_ctx)) 1208 out_op.op = insert_in_sampler;
1201 { 1209 out_op.op_cls = NULL;
1202 out_op.op = insert_in_sampler; 1210 GNUNET_array_append (peer_ctx->outstanding_ops,
1203 out_op.op_cls = NULL; 1211 peer_ctx->num_outstanding_ops,
1204 GNUNET_array_append (peer_ctx->outstanding_ops, 1212 out_op);
1205 peer_ctx->num_outstanding_ops, 1213 }
1206 out_op);
1207 }
1208
1209 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1210 {
1211 out_op.op = insert_in_view;
1212 out_op.op_cls = NULL;
1213 GNUNET_array_append (peer_ctx->outstanding_ops,
1214 peer_ctx->num_outstanding_ops,
1215 out_op);
1216 }
1217 1214
1218 /* Trigger livelyness test on peer */ 1215 if (GNUNET_NO == insert_in_view_scheduled (peer_ctx))
1219 check_peer_live (peer_ctx); 1216 {
1217 out_op.op = insert_in_view;
1218 out_op.op_cls = NULL;
1219 GNUNET_array_append (peer_ctx->outstanding_ops,
1220 peer_ctx->num_outstanding_ops,
1221 out_op);
1220 } 1222 }
1221 // else...?
1222 1223
1223 // send push/pull to each of those peers? 1224 /* Trigger livelyness test on peer */
1225 check_peer_live (peer_ctx);
1224 } 1226 }
1227 // else...?
1228
1229 // send push/pull to each of those peers?
1225} 1230}
1226 1231
1227 1232
@@ -1659,11 +1664,11 @@ handle_peer_pull_reply (void *cls,
1659 return GNUNET_SYSERR; 1664 return GNUNET_SYSERR;
1660 } 1665 }
1661 1666
1662 sender = (struct GNUNET_PeerIdentity *) GNUNET_CADET_channel_get_info ( 1667 // Guess simply casting isn't the nicest way...
1663 (struct GNUNET_CADET_Channel *) channel, GNUNET_CADET_OPTION_PEER); 1668 // FIXME wait for cadet to change this function
1664 // Guess simply casting isn't the nicest way... 1669 sender = (struct GNUNET_PeerIdentity *)
1665 // FIXME wait for cadet to change this function 1670 GNUNET_CADET_channel_get_info (channel, GNUNET_CADET_OPTION_PEER);
1666 sender_ctx = get_peer_ctx (peer_map, sender); 1671 sender_ctx = get_peer_ctx (sender);
1667 1672
1668 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender)); 1673 LOG (GNUNET_ERROR_TYPE_DEBUG, "PULL REPLY received (%s)\n", GNUNET_i2s (sender));
1669 1674
@@ -1721,7 +1726,7 @@ handle_peer_pull_reply (void *cls,
1721 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, 1726 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity,
1722 &peers[i])) 1727 &peers[i]))
1723 { 1728 {
1724 peer_ctx = get_peer_ctx (peer_map, &peers[i]); 1729 peer_ctx = get_peer_ctx (&peers[i]);
1725 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)) 1730 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
1726 { 1731 {
1727 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i])) 1732 if (GNUNET_NO == in_arr (pull_list, pull_list_size, &peers[i]))
@@ -1804,7 +1809,7 @@ send_pull_request (struct GNUNET_PeerIdentity *peer_id)
1804 struct GNUNET_MQ_Handle *mq; 1809 struct GNUNET_MQ_Handle *mq;
1805 struct PeerContext *peer_ctx; 1810 struct PeerContext *peer_ctx;
1806 1811
1807 peer_ctx = get_peer_ctx (peer_map, peer_id); 1812 peer_ctx = get_peer_ctx (peer_id);
1808 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)); 1813 GNUNET_assert (GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING));
1809 set_peer_flag (peer_ctx, PULL_REPLY_PENDING); 1814 set_peer_flag (peer_ctx, PULL_REPLY_PENDING);
1810 1815
@@ -2036,7 +2041,7 @@ do_mal_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2036 { /* Combined attack */ 2041 { /* Combined attack */
2037 2042
2038 /* Send PUSH to attacked peers */ 2043 /* Send PUSH to attacked peers */
2039 peer_ctx = get_peer_ctx (peer_map, &attacked_peer); 2044 peer_ctx = get_peer_ctx (&attacked_peer);
2040 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID)) 2045 if (GNUNET_YES == get_peer_flag (peer_ctx, VALID))
2041 { 2046 {
2042 LOG (GNUNET_ERROR_TYPE_DEBUG, 2047 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -2161,7 +2166,7 @@ do_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2161 for (i = first_border; i < second_border; i++) 2166 for (i = first_border; i < second_border; i++)
2162 { 2167 {
2163 peer = view_array[permut[i]]; 2168 peer = view_array[permut[i]];
2164 peer_ctx = get_peer_ctx (peer_map, &peer); 2169 peer_ctx = get_peer_ctx (&peer);
2165 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) && 2170 if (0 != GNUNET_CRYPTO_cmp_peer_identity (&own_identity, &peer) &&
2166 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO 2171 GNUNET_NO == get_peer_flag (peer_ctx, PULL_REPLY_PENDING)) // TODO
2167 { // FIXME if this fails schedule/loop this for later 2172 { // FIXME if this fails schedule/loop this for later
@@ -2398,13 +2403,13 @@ peer_remove_cb (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2398 2403
2399 /* If we are still waiting for notification whether this peer is live 2404 /* If we are still waiting for notification whether this peer is live
2400 * cancel the according task */ 2405 * cancel the according task */
2401 if (NULL != peer_ctx->is_live_task) 2406 if (NULL != peer_ctx->transmit_handle)
2402 { 2407 {
2403 LOG (GNUNET_ERROR_TYPE_DEBUG, 2408 LOG (GNUNET_ERROR_TYPE_DEBUG,
2404 "Trying to cancle is_live_task for peer %s\n", 2409 "Trying to cancle transmit_handle for peer %s\n",
2405 GNUNET_i2s (key)); 2410 GNUNET_i2s (key));
2406 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->is_live_task); 2411 GNUNET_CADET_notify_transmit_ready_cancel (peer_ctx->transmit_handle);
2407 peer_ctx->is_live_task = NULL; 2412 peer_ctx->transmit_handle = NULL;
2408 } 2413 }
2409 2414
2410 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING); 2415 unset_peer_flag (peer_ctx, PULL_REPLY_PENDING);
@@ -2466,7 +2471,7 @@ peer_clean (const struct GNUNET_PeerIdentity *peer)
2466 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) && 2471 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains (view, peer) &&
2467 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer)) 2472 GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (peer_map, peer))
2468 { 2473 {
2469 peer_ctx = get_peer_ctx (peer_map, peer); 2474 peer_ctx = get_peer_ctx (peer);
2470 2475
2471 if (NULL == peer_ctx->recv_channel) 2476 if (NULL == peer_ctx->recv_channel)
2472 { 2477 {
@@ -2564,38 +2569,24 @@ handle_inbound_channel (void *cls,
2564 enum GNUNET_CADET_ChannelOption options) 2569 enum GNUNET_CADET_ChannelOption options)
2565{ 2570{
2566 struct PeerContext *peer_ctx; 2571 struct PeerContext *peer_ctx;
2567 struct GNUNET_PeerIdentity peer;
2568 2572
2569 peer = *initiator;
2570 LOG (GNUNET_ERROR_TYPE_DEBUG, 2573 LOG (GNUNET_ERROR_TYPE_DEBUG,
2571 "New channel was established to us (Peer %s).\n", 2574 "New channel was established to us (Peer %s).\n",
2572 GNUNET_i2s (&peer)); 2575 GNUNET_i2s (initiator));
2573 2576 GNUNET_assert (NULL != channel); /* according to cadet API */
2574 GNUNET_assert (NULL != channel); 2577 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (peer_map, initiator))
2575 2578 peer_ctx = create_peer_ctx (initiator);
2576 // we might not even store the recv_channel 2579 else
2577 2580 peer_ctx = get_peer_ctx (initiator);
2578 peer_ctx = get_peer_ctx (peer_map, &peer); 2581 /* We only accept one incoming channel from peers */
2579 // FIXME what do we do if a channel is established twice? 2582 if (NULL != peer_ctx->recv_channel)
2580 // overwrite? Clean old channel? ...? 2583 {
2581 //if (NULL != peer_ctx->recv_channel) 2584 GNUNET_CADET_channel_destroy (channel);
2582 //{ 2585 return NULL;
2583 // peer_ctx->recv_channel = channel; 2586 }
2584 //}
2585 peer_ctx->recv_channel = channel; 2587 peer_ctx->recv_channel = channel;
2586
2587 (void) GNUNET_CONTAINER_multipeermap_put (peer_map, &peer, peer_ctx,
2588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
2589
2590 /* This would make the push-message unnecessary */
2591 LOG (GNUNET_ERROR_TYPE_DEBUG,
2592 "Got peer_id %s from peerinfo\n",
2593 GNUNET_i2s (&peer));
2594 new_peer_id (&peer);
2595
2596 peer_is_live (peer_ctx); 2588 peer_is_live (peer_ctx);
2597 2589 return NULL;
2598 return NULL; // TODO
2599} 2590}
2600 2591
2601 2592