lib/Net/BitTorrent/Peer.pm

filter: Statementstmt 68.9% 275 / 399 Branchbran 52.6% 160 / 304 Conditioncond 45.9% 74 / 161 MC/DCmcdc 25.5% 24 / 94 Subroutinesub 61.2% 30 / 49 Podpod 66.6% 22 / 33 totaltotal 56.2% 585 / 1040 CC 212 SCAR 43.5
CC
212
Cov
59%
handle_hashes
240.0
handle_hash_request
156.0
handle_hp_connect
156.0
CRAP 77.2
« lib/Net/BitTorrent/Transport/TCP.pm ↑ Summary lib/Net/BitTorrent/Tracker.pm »
1 25 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 class Net::BitTorrent::Peer v2.1.1 : isa(Net::BitTorrent::Emitter) {
6 25 ▶ use Net::BitTorrent::Types qw[:encryption :state];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶ use Net::BitTorrent::SSRF qw[is_safe_ip];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 field $protocol : param;
9  
10 # Instance of Net::BitTorrent::Protocol::BEP03 or subclass
11 25 field $torrent : param : reader; # Parent Net::BitTorrent::Torrent object
12 25 field $transport : param : reader; # Net::BitTorrent::Transport::*
13 25 field $ip : param : reader = undef;
14 25 field $port : param : reader = undef;
15 25 field $am_choking : reader = 1;
16 25 field $am_interested : reader = 0;
17 25 field $peer_choking : reader = 1;
18 25 field $peer_interested : reader = 0;
19 25 field $blocks_inflight : reader = 0;
20 25 field $bitfield_status : reader : writer = undef; # 'all', 'none', or raw data
21 25 field $offered_piece = undef;
22 25 field $bytes_down = 0;
23 25 field $bytes_up = 0;
24 25 field $rate_down : reader = 0;
25 25 field $rate_up : reader = 0;
26 25 field $reputation : reader = 100; # Start at 100
27 25 field $debug : param : reader = 0;
28 25 field $encryption : param : reader = ENCRYPTION_PREFERRED; # none, preferred, required
29 25 field $mse : param = undef;
30 25 field @allowed_fast_set; # Pieces we are allowed to request even if choked
31 25 field @suggested_pieces;
32 25 field $pwp_handshake_sent = 0;
33 25 field $requested_blocks : reader = {}; # Track pending requests "index,begin" => 1
34 25 field $last_activity : reader = 0; # Timestamp of last data received
35 25 field $connected_at : reader = 0; # Timestamp of connection
36 25 field $_disconnected = 0; # Guard: prevent repeated disconnect
37 25 ▶ use constant REQUEST_TIMEOUT => 30; # Seconds without response before disconnect
Subroutine BEGIN: called CC=1 SCAR=0.0
38 25 ▶ use constant IDLE_TIMEOUT => 120; # Seconds without any data before disconnect
Subroutine BEGIN: called CC=1 SCAR=0.0
39 14 ▶ method protocol () {$protocol}
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
40 49 ▶ method connected () { !$_disconnected }
Subroutine connected: called CC=1 SCAR=0.0
Pod documented
41 0 ▶ method is_encrypted () { defined $mse && $mse->state eq 'PAYLOAD' }
Subroutine is_encrypted: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $mse and $mse->state eq "PAYLOAD" 0 0 0
42 49 ▶ method is_seeder () { defined $bitfield_status && $bitfield_status eq 'all' }
Subroutine is_seeder: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $bitfield_status and $bitfield_status eq "all" 26 23 49
43  
44 0 ▶ method flags () {
Subroutine flags: not called CC=3 SCAR=24.8
Pod documented
45 0 my $f = 0;
46 0 ▶ $f |= 0x01 if $self->is_encrypted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_encrypted 0 0 0
47 0 ▶ $f |= 0x02 if $self->is_seeder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_seeder 0 0 0
48 0 return $f;
49 }
50 55 ▶ ADJUST {
Subroutine __ANON__: called CC=9 SCAR=22.7
51 55 $connected_at = time();
52 55 $last_activity = $connected_at;
53 55 $self->set_parent_emitter($torrent);
54 55 ▶ builtin::weaken($torrent) if defined $torrent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $torrent 55 0 55
55 55 ▶ if ( $protocol->can('set_peer') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol->can("set_peer")) 55 0 55
56 55 $protocol->set_peer($self);
57 }
58 55 ▶ if ( !$mse && $encryption != ENCRYPTION_NONE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $mse and $encryption != 0) 13 42 55
Condition66%2/3
not $mse and $encryption != 0
!ll&&!rl&&r
04213
MC/DC50%1/2
not $mse and $encryption != 0
not $mse $encryption != 0
Truth table66%2/3
not(A) and B
A= not $mseB= $encryption != 0
ABresult
0X0
100
111
59 25 ▶ use Net::BitTorrent::Protocol::MSE;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 13 ▶ $mse = Net::BitTorrent::Protocol::MSE->new(
Branch: 2 branches
BranchTrueFalseTotal
$torrent ? : 13 0 13
$encryption == 1 ? : 12 1 13
Condition33%1/3
$torrent->infohash_v1 // $torrent->infohash_v2
l!l&&r!l&&!r
1300
MC/DC0%0/2
$torrent->infohash_v1 // $torrent->infohash_v2
$torrent->infohash_v1 $torrent->infohash_v2
Truth table33%1/3
A // B
A= $torrent->infohash_v1B= $torrent->infohash_v2
ABresult
1X1
011
000
61 infohash => $torrent ? ( $torrent->infohash_v1 // $torrent->infohash_v2 ) : undef,
62 is_initiator => 1, # Outgoing
63 allow_plaintext => ( $encryption == ENCRYPTION_PREFERRED ? 1 : 0 ),
64 );
65 13 ▶ if ( $mse->supported ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mse->supported) { } 13 0 13
66 13 $transport->set_filter($mse);
67 }
68 else {
69 0 $mse = undef;
70 }
71 }
72 55 my $weak_self = $self;
73 55 builtin::weaken($weak_self);
74 $transport->on(
75 'data',
76 sub ( $emitter, $data ) {
77 $weak_self->receive_data($data) if $weak_self;
78 }
79 55 );
80 $transport->on(
81 'disconnected',
82 sub ( $emitter, @args ) {
83 $weak_self->disconnected() if $weak_self;
84 }
85 55 );
86 $transport->on(
87 'filter_failed',
88 sub ( $emitter, $leftover ) {
89 return unless $weak_self;
90 return if $weak_self->encryption == ENCRYPTION_REQUIRED;
91 $weak_self->_emit_log( 'debug', 'Falling back to plaintext handshake...' ) if $weak_self->debug;
92  
93 # We can't easily change $mse from here because it's a field
94 # but we can call a method or just use it.
95 # Actually $mse is in scope but it's a field.
96 # In ADJUST, we can access fields.
97 $mse = undef;
98 $protocol->send_handshake();
99 $pwp_handshake_sent = 1;
100 }
101 55 );
102 $transport->on(
103 'connected',
104 sub ( $emitter, @args ) {
105 return unless $weak_self;
106 if ($mse) {
107 $weak_self->_emit_log( 'debug', 'Starting MSE handshake...' ) if $weak_self->debug;
108  
109 # Handshake is driven by transport filter's write_buffer in tick()
110 }
111 else {
112 $protocol->send_handshake();
113 $pwp_handshake_sent = 1;
114 }
115 }
116 55 );
117 $self->on(
118 'handshake_complete',
119 sub ( $emitter, @args ) {
120 return unless $weak_self;
121  
122 # Some peers need us to be unchoked/interested to talk to us
123 # but we'll stay choked until we have metadata if we want to be safe.
124 # However, we MUST send bitfield/have_none to be protocol compliant.
125 # BEP 03: Send bitfield if we have one
126 if ( $torrent && $torrent->bitfield ) {
127 $protocol->send_bitfield( $torrent->bitfield->data );
128 }
129  
130 # BEP 06: Send HAVE_NONE ONLY if remote supports Fast Extension
131 elsif ( ord( substr( $protocol->reserved, 7, 1 ) ) & 0x04 ) {
132 if ( $protocol->can('send_have_none') ) {
133 $protocol->send_have_none();
134 }
135 }
136  
137 # If in METADATA mode, we don't send unchoke/interested yet
138 return if $torrent && $torrent->state == STATE_METADATA;
139 $weak_self->unchoke();
140 $weak_self->_check_interest();
141  
142 # BEP 06: Send Allowed Fast set immediately after handshake
143 if ( $protocol->isa('Net::BitTorrent::Protocol::BEP06') ) {
144 my $set = $torrent->get_allowed_fast_set( $weak_self->ip );
145 for my $idx (@$set) {
146 $protocol->send_allowed_fast($idx);
147 }
148 }
149 if ( $torrent && $torrent->client ) {
150 $torrent->client->_emit( 'peer_connected', $weak_self );
151 }
152 }
153 55 );
154 }
155  
156 0 ▶ method send_suggest ($index) {
Subroutine send_suggest: not called CC=2 SCAR=17.9
Pod documented
157 0 ▶ $protocol->send_suggest($index) if $protocol->can('send_suggest');
Branch: 1 branch
BranchTrueFalseTotal
if $protocol->can("send_suggest") 0 0 0
158 }
159  
160 0 ▶ method send_allowed_fast ($index) {
Subroutine send_allowed_fast: not called CC=2 SCAR=17.9
Pod documented
161 0 ▶ $protocol->send_allowed_fast($index) if $protocol->can('send_allowed_fast');
Branch: 1 branch
BranchTrueFalseTotal
if $protocol->can("send_allowed_fast") 0 0 0
162 }
163  
164 0 ▶ method on_data ($data) {
Subroutine on_data: not called CC=1 SCAR=6.9
Pod documented
165 0 $self->receive_data($data);
166 }
167  
168 0 ▶ method set_protocol ($p) {
Subroutine set_protocol: not called CC=1 SCAR=6.9
Pod documented
169 0 $protocol = $p;
170 }
171  
172 0 ▶ method set_torrent ($t) {
Subroutine set_torrent: not called CC=1 SCAR=6.9
Pod documented
173 0 $torrent = $t;
174 }
175  
176 21 ▶ method receive_data ($data) {
Subroutine receive_data: called CC=2 SCAR=7.0
Pod documented
177 21 $last_activity = time();
178 21 ▶ $self->_emit_log( 'debug', 'Peer received ' . length($data) . ' bytes of data' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 21 21
179 21 $torrent->can_read( length $data );
180 21 $protocol->receive_data($data);
181 }
182  
183 66 ▶ method write_buffer () {
Subroutine write_buffer: called CC=5 SCAR=17.8
Pod documented
184 66 my $raw = $protocol->write_buffer();
185 66 ▶ return '' unless length $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless length $raw 43 23 66
186  
187 # Rate limiting logic
188 23 my $allowed = length $raw;
189 23 ▶ if ($torrent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($torrent) 23 0 23
190 23 $allowed = $torrent->can_write( length $raw );
191 }
192 23 ▶ if ( $allowed <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($allowed <= 0) { } 0 23 23
elsif ($allowed < length $raw) { } 0 23 23
193 0 return 0; # Rate limit: send nothing
194 }
195 elsif ( $allowed < length $raw ) {
196 0 my $chunk = substr( $raw, 0, $allowed, '' );
197 0 return $transport->send_data($chunk);
198 }
199 23 return $transport->send_data($raw);
200 }
201  
202 0 ▶ method handle_hash_request ( $root, $proof_layer, $base_layer, $index, $length ) {
Subroutine handle_hash_request: not called CC=12 SCAR=50.5
Pod undocumented
203 0 ▶ if ( !defined $root || length($root) != 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $root or length $root != 32) 0 0 0
Condition0%0/3
not defined $root or length $root != 32
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $root or length $root != 32
not defined $root length $root != 32
Truth table0%0/3
not(A) or B
A= not defined $rootB= length $root != 32
ABresult
1X1
011
000
204 0 ▶ $self->_emit_log( 'debug', 'HASH_REQUEST with invalid root' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
205 0 return;
206 }
207 0 ▶ if ( $base_layer > 63 || $length == 0 || $length > 65536 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_layer > 63 or $length == 0 or $length > 65536) 0 0 0
Condition0%0/6
$base_layer > 63 or $length == 0 or $length > 65536
l!l&&r!l&&!r
000
$base_layer > 63 or $length == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$base_layer > 63 or $length == 0 or $length > 65536
$base_layer > 63 $length == 0 $length > 65536
Truth table0%0/4
A or B or C
A= $base_layer > 63B= $length == 0C= $length > 65536
ABCresult
1XX1
01X1
0011
0000
208 0 ▶ $self->_emit_log( 'debug', "HASH_REQUEST with invalid params: layer=$base_layer index=$index length=$length" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
209 0 $self->adjust_reputation(-5);
210 0 return;
211 }
212 0 my $file = $torrent->storage->get_file_by_root($root);
213 0 ▶ if ( !$file || !$file->merkle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file and $file->merkle) 0 0 0
Condition0%0/3
$file and $file->merkle
!ll&&!rl&&r
000
MC/DC0%0/2
$file and $file->merkle
$file $file->merkle
Truth table0%0/3
A and B
A= $fileB= $file->merkle
ABresult
0X0
100
111
214 0 ▶ $protocol->send_hash_reject( $root, $proof_layer, $base_layer, $index, $length ) if $protocol->can('send_hash_reject');
Branch: 1 branch
BranchTrueFalseTotal
if $protocol->can("send_hash_reject") 0 0 0
215 0 return;
216 }
217 0 my $hashes = $file->merkle->get_hashes( $base_layer, $index, $length );
218  
219 # Simplified: no proof nodes added yet
220 0 ▶ $protocol->send_hashes( $root, $proof_layer, $base_layer, $index, $length, $hashes ) if $protocol->can('send_hashes');
Branch: 1 branch
BranchTrueFalseTotal
if $protocol->can("send_hashes") 0 0 0
221 }
222  
223 0 ▶ method handle_hashes ( $root, $proof_layer, $base_layer, $index, $length, $hashes ) {
Subroutine handle_hashes: not called CC=15 SCAR=54.8
Pod undocumented
224 0 ▶ if ( !defined $root || length($root) != 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $root or length $root != 32) 0 0 0
Condition0%0/3
not defined $root or length $root != 32
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $root or length $root != 32
not defined $root length $root != 32
Truth table0%0/3
not(A) or B
A= not defined $rootB= length $root != 32
ABresult
1X1
011
000
225 0 ▶ $self->_emit_log( 'debug', 'HASHES with invalid root' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
226 0 return;
227 }
228 0 ▶ if ( $base_layer > 63 || $length == 0 || $length > 65536 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_layer > 63 or $length == 0 or $length > 65536) 0 0 0
Condition0%0/6
$base_layer > 63 or $length == 0 or $length > 65536
l!l&&r!l&&!r
000
$base_layer > 63 or $length == 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$base_layer > 63 or $length == 0 or $length > 65536
$base_layer > 63 $length == 0 $length > 65536
Truth table0%0/4
A or B or C
A= $base_layer > 63B= $length == 0C= $length > 65536
ABCresult
1XX1
01X1
0011
0000
229 0 ▶ $self->_emit_log( 'debug', "HASHES with invalid params: layer=$base_layer index=$index length=$length" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
230 0 $self->adjust_reputation(-5);
231 0 return;
232 }
233 0 my $file = $torrent->storage->get_file_by_root($root);
234 0 ▶ return unless $file && $file->merkle;
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $file->merkle 0 0 0
Condition0%0/3
$file and $file->merkle
!ll&&!rl&&r
000
MC/DC0%0/2
$file and $file->merkle
$file $file->merkle
Truth table0%0/3
A and B
A= $fileB= $file->merkle
ABresult
0X0
100
111
235 0 my $node_size = 32;
236 0 ▶ if ( length($hashes) % $node_size != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($hashes) % $node_size != 0) 0 0 0
237 0 $self->_emit_log( 'warning', 'Invalid hashes length: ' . length($hashes) . ' not a multiple of ' . $node_size );
238 0 $self->adjust_reputation(-5);
239 0 return;
240 }
241 0 my $num_hashes = length($hashes) / $node_size;
242 0 ▶ if ( $num_hashes > $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_hashes > $length) 0 0 0
243 0 ▶ $self->_emit_log( 'debug', "HASHES contains $num_hashes hashes but claimed length is $length, truncating" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
244 0 $num_hashes = $length;
245 }
246  
247 # BEP 52: index and length refer to the range of nodes at base_layer.
248 # The hashes string contains these nodes concatenated.
249 0 ▶ for ( my $i = 0; $i < $num_hashes; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_hashes) 0 0 0
250 0 my $hash = substr( $hashes, $i * $node_size, $node_size );
251 0 $file->merkle->set_node( $base_layer, $index + $i, $hash );
252 }
253 0 ▶ $self->_emit_log( 'debug', "Received and stored $num_hashes hashes for root " . unpack( 'H*', $root ) . " at layer $base_layer" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
254 }
255  
256 0 ▶ method handle_hash_reject ( $root, $proof_layer, $base_layer, $index, $length ) {
Subroutine handle_hash_reject: not called CC=2 SCAR=17.9
Pod undocumented
257 0 ▶ $self->_emit_log( 'debug', 'Peer rejected hash request for root ' . unpack( 'H*', $root ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
258 }
259  
260 2 ▶ method handle_metadata_request ($piece) {
Subroutine handle_metadata_request: called CC=1 SCAR=0.0
Pod undocumented
261 2 $torrent->handle_metadata_request( $self, $piece );
262 }
263  
264 2 ▶ method handle_metadata_data ( $piece, $total_size, $data ) {
Subroutine handle_metadata_data: called CC=1 SCAR=0.0
Pod undocumented
265 2 $torrent->handle_metadata_data( $self, $piece, $total_size, $data );
266 }
267  
268 0 ▶ method handle_metadata_reject ($piece) {
Subroutine handle_metadata_reject: not called CC=1 SCAR=6.9
Pod undocumented
269 0 $torrent->handle_metadata_reject( $self, $piece );
270 }
271  
272 2 ▶ method handle_pex ( $added, $dropped, $added6, $dropped6 ) {
Subroutine handle_pex: called CC=16 SCAR=34.1
Pod undocumented
273 2 for my $list ( $added, $dropped, $added6, $dropped6 ) {
274 8 ▶ next unless defined $list && ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list and ref $list eq "ARRAY" 0 8 8
Condition33%1/3
defined $list and ref $list eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
defined $list and ref $list eq "ARRAY"
defined $list ref $list eq "ARRAY"
Truth table33%1/3
A and B
A= defined $listB= ref $list eq "ARRAY"
ABresult
0X0
100
111
275  
276 # Limit PEX entries to prevent resource exhaustion
277 8 ▶ splice( @$list, 100 ) if @$list > 100;
Branch: 1 branch
BranchTrueFalseTotal
if @$list > 100 0 8 8
278 }
279 2 ▶ $added //= [];
Condition50%1/2
$added //= []
l!l
20
MC/DC0%0/1
$added //= []
$added
Truth table50%1/2
A
A= $added
Aresult
11
00
280 2 ▶ $dropped //= [];
Condition50%1/2
$dropped //= []
l!l
20
MC/DC0%0/1
$dropped //= []
$dropped
Truth table50%1/2
A
A= $dropped
Aresult
11
00
281 2 ▶ $added6 //= [];
Condition50%1/2
$added6 //= []
l!l
20
MC/DC0%0/1
$added6 //= []
$added6
Truth table50%1/2
A
A= $added6
Aresult
11
00
282 2 ▶ $dropped6 //= [];
Condition50%1/2
$dropped6 //= []
l!l
20
MC/DC0%0/1
$dropped6 //= []
$dropped6
Truth table50%1/2
A
A= $dropped6
Aresult
11
00
283 2 for my $p ( @$added, @$added6 ) {
284 101 ▶ next unless ref $p eq 'HASH' && defined $p->{ip} && defined $p->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p eq "HASH" and defined $p->{"ip"} and defined $p->{"port"} 0 101 101
Condition33%2/6
ref $p eq "HASH" and defined $p->{"ip"} and defined $p->{"port"}
!ll&&!rl&&r
00101
ref $p eq "HASH" and defined $p->{"ip"}
!ll&&!rl&&r
00101
MC/DC0%0/3
ref $p eq "HASH" and defined $p->{"ip"} and defined $p->{"port"}
ref $p eq "HASH" defined $p->{"ip"} defined $p->{"port"}
Truth table25%1/4
A and B and C
A= ref $p eq "HASH"B= defined $p->{"ip"}C= defined $p->{"port"}
ABCresult
0XX0
10X0
1100
1111
285 101 ▶ next if $p->{port} < 1 || $p->{port} > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"port"} < 1 or $p->{"port"} > 65535 0 101 101
Condition33%1/3
$p->{"port"} < 1 or $p->{"port"} > 65535
l!l&&r!l&&!r
00101
MC/DC0%0/2
$p->{"port"} < 1 or $p->{"port"} > 65535
$p->{"port"} < 1 $p->{"port"} > 65535
Truth table33%1/3
A or B
A= $p->{"port"} < 1B= $p->{"port"} > 65535
ABresult
1X1
011
000
286 101 ▶ next unless is_safe_ip( $p->{ip} );
Branch: 1 branch
BranchTrueFalseTotal
unless is_safe_ip($p->{"ip"}) 100 1 101
287 1 $torrent->add_peer($p);
288 }
289 }
290  
291 0 ▶ method handle_hp_rendezvous ($id) {
Subroutine handle_hp_rendezvous: not called CC=7 SCAR=40.3
Pod undocumented
292  
293 # Remote wants to connect to a node with $id via us.
294 # Find node in our swarm.
295 0 my $target;
296 0 for my $p ( values $torrent->peer_objects_hash->%* ) {
297 0 ▶ if ( $p->protocol->can('peer_id') && $p->protocol->peer_id eq $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->protocol->can("peer_id") and $p->protocol->peer_id eq $id) 0 0 0
Condition0%0/3
$p->protocol->can("peer_id") and $p->protocol->peer_id eq $id
!ll&&!rl&&r
000
MC/DC0%0/2
$p->protocol->can("peer_id") and $p->protocol->peer_id eq $id
$p->protocol->can("peer_id") $p->protocol->peer_id eq $id
Truth table0%0/3
A and B
A= $p->protocol->can("peer_id")B= $p->protocol->peer_id eq $id
ABresult
0X0
100
111
298 0 $target = $p;
299 0 last;
300 }
301 }
302 0 ▶ if ( $target && exists $target->protocol->remote_extensions->{ut_holepunch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target and exists $target->protocol->remote_extensions->{'ut_holepunch'}) { } 0 0 0
Condition0%0/3
$target and exists $target->protocol->remote_extensions->{"ut_holepunch"}
!ll&&!rl&&r
000
MC/DC0%0/2
$target and exists $target->protocol->remote_extensions->{"ut_holepunch"}
$target exists $target->protocol->remote_extensions->{"ut_holepunch"}
Truth table0%0/3
A and B
A= $targetB= exists $target->protocol->remote_extensions->{"ut_holepunch"}
ABresult
0X0
100
111
303  
304 # Relay connect instruction to target
305 0 $target->protocol->send_hp_connect( $self->ip, $self->port );
306  
307 # Acknowledge to source (optional, BEP 55 says relay then ack?)
308 # Actually, BEP says relay 'connect' to target.
309 }
310 else {
311 0 ▶ $protocol->send_hp_error(0x01) if $protocol->can('send_hp_error'); # 0x01 = peer not found
Branch: 1 branch
BranchTrueFalseTotal
if $protocol->can("send_hp_error") 0 0 0
312 }
313 }
314  
315 0 ▶ method handle_hp_connect ( $ip, $port ) {
Subroutine handle_hp_connect: not called CC=12 SCAR=50.5
Pod undocumented
316 0 ▶ unless ( is_safe_ip($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_safe_ip($ip)) 0 0 0
317 0 ▶ $self->_emit_log( 'warn', "HP_CONNECT blocked by SSRF policy: $ip:$port" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
318 0 return;
319 }
320 0 ▶ unless ( defined $port && $port >= 1 && $port <= 65535 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port and $port >= 1 and $port <= 65535) 0 0 0
Condition0%0/6
defined $port and $port >= 1 and $port <= 65535
!ll&&!rl&&r
000
defined $port and $port >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
defined $port and $port >= 1 and $port <= 65535
defined $port $port >= 1 $port <= 65535
Truth table0%0/4
A and B and C
A= defined $portB= $port >= 1C= $port <= 65535
ABCresult
0XX0
10X0
1100
1111
321 0 ▶ $self->_emit_log( 'warn', "HP_CONNECT rejected: invalid port $port" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
322 0 return;
323 }
324 0 state $hp_connect_count = 0;
325 0 state $hp_connect_reset = time();
326 0 my $now = time();
327 0 ▶ if ( $now - $hp_connect_reset > 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $hp_connect_reset > 60) 0 0 0
328 0 $hp_connect_count = 0;
329 0 $hp_connect_reset = $now;
330 }
331 0 $hp_connect_count++;
332 0 ▶ if ( $hp_connect_count > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hp_connect_count > 10) 0 0 0
333 0 ▶ $self->_emit_log( 'warn', "HP_CONNECT rate limit exceeded, ignoring" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
334 0 $self->adjust_reputation(-20);
335 0 return;
336 }
337 0 ▶ $self->_emit_log( 'info', "Instructed to connect to $ip:$port" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
338  
339 # Trigger uTP connection
340 0 ▶ $torrent->client->connect_to_peer( $ip, $port, $torrent->infohash_v2 || $torrent->infohash_v1 );
Condition0%0/3
$torrent->infohash_v2 || $torrent->infohash_v1
l!l&&r!l&&!r
000
MC/DC0%0/2
$torrent->infohash_v2 || $torrent->infohash_v1
$torrent->infohash_v2 $torrent->infohash_v1
Truth table0%0/3
A || B
A= $torrent->infohash_v2B= $torrent->infohash_v1
ABresult
1X1
011
000
341 }
342  
343 0 ▶ method handle_hp_error ($err) {
Subroutine handle_hp_error: not called CC=2 SCAR=17.9
Pod undocumented
344 0 ▶ $self->_emit_log( 'error', 'Received holepunch error: ' . $err ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
345 }
346  
347 281 ▶ method handle_message ( $id, $payload ) {
Subroutine handle_message: called CC=76 SCAR=49.1
Pod documented
348 281 my $plen = length($payload);
349  
350 # Validate payload length per message type (BEP 03)
351 281 ▶ if ( $id == 0 || $id == 1 || $id == 2 || $id == 3 || $id == 14 || $id == 15 )
Branch: 4 branches
BranchTrueFalseTotal
if ($id == 0 or $id == 1 or $id == 2 or $id == 3 or $id == 14 or $id == 15) { } 22 259 281
elsif ($id == 4 or $id == 13 or $id == 17) { } 180 79 259
elsif ($id == 6 or $id == 16) { } 25 54 79
elsif ($id == 7) { } 31 23 54
Condition93%14/15
$id == 0 or $id == 1 or $id == 2 or $id == 3 or $id == 14 or $id == 15
l!l&&r!l&&!r
193259
$id == 0 or $id == 1 or $id == 2 or $id == 3 or $id == 14
l!l&&r!l&&!r
181262
$id == 0 or $id == 1 or $id == 2 or $id == 3
l!l&&r!l&&!r
180263
$id == 0 or $id == 1 or $id == 2
l!l&&r!l&&!r
99263
$id == 0 or $id == 1
l!l&&r!l&&!r
18272
MC/DC83%5/6
$id == 0 or $id == 1 or $id == 2 or $id == 3 or $id == 14 or $id == 15
$id == 0 $id == 1 $id == 2 $id == 3 $id == 14 $id == 15
Truth table85%6/7
A or B or C or D or E or F
A= $id == 0B= $id == 1C= $id == 2D= $id == 3E= $id == 14F= $id == 15
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
352 { # CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED, HAVE_ALL, HAVE_NONE
353 22 ▶ if ( $plen != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen != 0) 5 17 22
354 5 ▶ $self->_emit_log( 'debug', "Peer message $id expected 0-byte payload, got $plen" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
355 5 $self->adjust_reputation(-2);
356 5 return;
357 }
358 }
359 ▶ elsif ( $id == 4 || $id == 13 || $id == 17 ) { # HAVE, SUGGEST_PIECE, ALLOWED_FAST
Condition100%6/6
$id == 4 or $id == 13 or $id == 17
l!l&&r!l&&!r
1582279
$id == 4 or $id == 13
l!l&&r!l&&!r
5153101
MC/DC100%3/3
$id == 4 or $id == 13 or $id == 17
$id == 4 $id == 13 $id == 17
Truth table100%4/4
A or B or C
A= $id == 4B= $id == 13C= $id == 17
ABCresult
1XX1
01X1
0011
0000
360 180 ▶ if ( $plen != 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen != 4) 4 176 180
361 4 ▶ $self->_emit_log( 'debug', "Peer message $id expected 4-byte payload, got $plen" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
362 4 $self->adjust_reputation(-2);
363 4 return;
364 }
365 }
366 ▶ elsif ( $id == 6 || $id == 16 ) { # REQUEST, REJECT
Condition100%3/3
$id == 6 or $id == 16
l!l&&r!l&&!r
23254
MC/DC100%2/2
$id == 6 or $id == 16
$id == 6 $id == 16
Truth table100%3/3
A or B
A= $id == 6B= $id == 16
ABresult
1X1
011
000
367 25 ▶ if ( $plen != 12 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen != 12) 2 23 25
368 2 ▶ $self->_emit_log( 'debug', "Peer message $id expected 12-byte payload, got $plen" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
369 2 $self->adjust_reputation(-2);
370 2 return;
371 }
372 }
373 elsif ( $id == 7 ) { # PIECE
374 31 ▶ if ( $plen < 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plen < 8) 1 30 31
375 1 ▶ $self->_emit_log( 'debug', "Peer PIECE message too short ($plen bytes)" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
376 1 $self->adjust_reputation(-2);
377 1 return;
378 }
379 }
380  
381 # warn ' [DEBUG] Peer ' . ($socket ? $socket->peerhost : 'sim') . " sent message ID $id (len " . length($payload) . ")\n";
382 269 ▶ if ( $id == 0 ) { # CHOKE
Branch: 13 branches
BranchTrueFalseTotal
if ($id == 0) { } 0 269 269
elsif ($id == 1) { } 7 262 269
elsif ($id == 2) { } 8 254 262
elsif ($id == 3) { } 0 254 254
elsif ($id == 4) { } 4 250 254
elsif ($id == 5) { } 7 243 250
elsif ($id == 6) { } 22 221 243
elsif ($id == 7) { } 30 191 221
elsif ($id == 13) { } 151 40 191
elsif ($id == 14) { } 0 40 40
elsif ($id == 15) { } 2 38 40
elsif ($id == 16) { } 1 37 38
elsif ($id == 17) { } 21 16 37
383 0 $peer_choking = 1;
384 0 $self->_emit('choked');
385 }
386 elsif ( $id == 1 ) { # UNCHOKE
387 7 $peer_choking = 0;
388 7 $self->_emit('unchoked');
389 7 $self->_request_next_block();
390 }
391 elsif ( $id == 2 ) { # INTERESTED
392 8 $peer_interested = 1;
393 8 $self->_emit('interested');
394 }
395 elsif ( $id == 3 ) { # NOT_INTERESTED
396 0 $peer_interested = 0;
397 0 $self->_emit('not_interested');
398 }
399 elsif ( $id == 4 ) { # HAVE
400 4 my $index = unpack( 'N', $payload );
401 4 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 4 0 4
402 4 ▶ if ( !defined $num_pieces || $num_pieces == 0 || $index >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $num_pieces or $num_pieces == 0 or $index >= $num_pieces) 1 3 4
Condition50%3/6
not defined $num_pieces or $num_pieces == 0 or $index >= $num_pieces
l!l&&r!l&&!r
013
not defined $num_pieces or $num_pieces == 0
l!l&&r!l&&!r
004
MC/DC33%1/3
not defined $num_pieces or $num_pieces == 0 or $index >= $num_pieces
not defined $num_pieces $num_pieces == 0 $index >= $num_pieces
Truth table50%2/4
not(A) or B or C
A= not defined $num_piecesB= $num_pieces == 0C= $index >= $num_pieces
ABCresult
1XX1
01X1
0011
0000
403 1 ▶ $self->_emit_log( 'debug', "Peer sent HAVE with out-of-range index $index (num_pieces=$num_pieces)" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
404 1 $self->adjust_reputation(-2);
405 1 return;
406 }
407 3 $torrent->update_peer_have( $self, $index );
408  
409 # BEP 16: If we see this peer (or others) have our offered piece,
410 # we can offer a new one. (Simplified global check)
411 3 ▶ if ( defined $offered_piece && $index == $offered_piece ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offered_piece and $index == $offered_piece) 1 2 3
Condition66%2/3
defined $offered_piece and $index == $offered_piece
!ll&&!rl&&r
201
MC/DC50%1/2
defined $offered_piece and $index == $offered_piece
defined $offered_piece $index == $offered_piece
Truth table66%2/3
A and B
A= defined $offered_pieceB= $index == $offered_piece
ABresult
0X0
100
111
412 1 $offered_piece = undef;
413 }
414 3 $self->_check_interest();
415 }
416 elsif ( $id == 5 ) { # BITFIELD
417 7 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 5 2 7
418 7 ▶ my $expected = $num_pieces ? int( ( $num_pieces + 7 ) / 8 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$num_pieces ? : 5 2 7
419 7 ▶ if ( $expected == 0 || $plen != $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected == 0 or $plen != $expected) 2 5 7
Condition66%2/3
$expected == 0 or $plen != $expected
l!l&&r!l&&!r
205
MC/DC50%1/2
$expected == 0 or $plen != $expected
$expected == 0 $plen != $expected
Truth table66%2/3
A or B
A= $expected == 0B= $plen != $expected
ABresult
1X1
011
000
420 2 ▶ $self->_emit_log( 'debug', "Peer BITFIELD with invalid length $plen (expected $expected)" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
421 2 $self->adjust_reputation(-5);
422 2 return;
423 }
424 5 $bitfield_status = $payload;
425 5 $torrent->set_peer_bitfield( $self, $payload );
426 5 $self->_emit( bitfield => $torrent->peer_bitfields->{$self} );
427  
428 # BEP 16: If superseeding, we don't send our real bitfield.
429 # Instead, we wait for interest and then offer pieces.
430 5 $self->_check_interest();
431 }
432 elsif ( $id == 6 ) { # REQUEST
433 22 my ( $index, $begin, $len ) = unpack( 'N N N', $payload );
434 22 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 22 0 22
435 22 ▶ my $piece_len = $torrent->metadata->{info}{'piece length'} // 16384;
Condition50%1/2
$torrent->metadata->{"info"}{"piece length"} // 16384
l!l
220
MC/DC0%0/1
$torrent->metadata->{"info"}{"piece length"} // 16384
$torrent->metadata->{"info"}{"piece length"}
Truth table50%1/2
A
A= $torrent->metadata->{"info"}{"piece length"}
Aresult
11
00
436 22 ▶ if ( $num_pieces == 0 || $index >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_pieces == 0 or $index >= $num_pieces) 2 20 22
Condition66%2/3
$num_pieces == 0 or $index >= $num_pieces
l!l&&r!l&&!r
0220
MC/DC50%1/2
$num_pieces == 0 or $index >= $num_pieces
$num_pieces == 0 $index >= $num_pieces
Truth table66%2/3
A or B
A= $num_pieces == 0B= $index >= $num_pieces
ABresult
1X1
011
000
437 2 ▶ $self->_emit_log( 'debug', "Peer REQUEST with out-of-range index $index" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
438 2 return;
439 }
440 20 ▶ if ( $len == 0 || $len > 131072 ) { # 2^17 = 128 KiB max block
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0 or $len > 131072) 2 18 20
Condition100%3/3
$len == 0 or $len > 131072
l!l&&r!l&&!r
1118
MC/DC100%2/2
$len == 0 or $len > 131072
$len == 0 $len > 131072
Truth table100%3/3
A or B
A= $len == 0B= $len > 131072
ABresult
1X1
011
000
441 2 ▶ $self->_emit_log( 'debug', "Peer REQUEST with invalid len $len" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
442 2 return;
443 }
444 18 ▶ if ( $begin + $len > $piece_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin + $len > $piece_len) 1 17 18
445 1 ▶ $self->_emit_log( 'debug', 'Peer REQUEST extends beyond piece boundary' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
446 1 return;
447 }
448 17 $self->_handle_request( $index, $begin, $len );
449 }
450 elsif ( $id == 7 ) { # PIECE
451 30 my ( $index, $begin ) = unpack( 'N N', substr( $payload, 0, 8, '' ) );
452 30 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 30 0 30
453 30 ▶ my $piece_len = $torrent->metadata->{info}{'piece length'} // 16384;
Condition50%1/2
$torrent->metadata->{"info"}{"piece length"} // 16384
l!l
300
MC/DC0%0/1
$torrent->metadata->{"info"}{"piece length"} // 16384
$torrent->metadata->{"info"}{"piece length"}
Truth table50%1/2
A
A= $torrent->metadata->{"info"}{"piece length"}
Aresult
11
00
454 30 ▶ if ( $num_pieces == 0 || $index >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_pieces == 0 or $index >= $num_pieces) 0 30 30
Condition33%1/3
$num_pieces == 0 or $index >= $num_pieces
l!l&&r!l&&!r
0030
MC/DC0%0/2
$num_pieces == 0 or $index >= $num_pieces
$num_pieces == 0 $index >= $num_pieces
Truth table33%1/3
A or B
A= $num_pieces == 0B= $index >= $num_pieces
ABresult
1X1
011
000
455 0 ▶ $self->_emit_log( 'debug', "Peer PIECE with out-of-range index $index" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
456 0 $self->adjust_reputation(-5);
457 0 return;
458 }
459 30 ▶ if ( $begin + length($payload) > $piece_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($begin + length($payload) > $piece_len) 0 30 30
460 0 ▶ $self->_emit_log( 'debug', "Peer PIECE block extends beyond piece boundary: $index:$begin+" . length($payload) . " > $piece_len" )
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
461 if $debug;
462 0 $self->adjust_reputation(-5);
463 0 return;
464 }
465 30 $self->_handle_piece_data( $index, $begin, $payload );
466 }
467 elsif ( $id == 13 ) { # SUGGEST_PIECE
468 151 my $index = unpack( 'N', $payload );
469 151 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 151 0 151
470 151 ▶ if ( $num_pieces == 0 || $index >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_pieces == 0 or $index >= $num_pieces) 149 2 151
Condition66%2/3
$num_pieces == 0 or $index >= $num_pieces
l!l&&r!l&&!r
01492
MC/DC50%1/2
$num_pieces == 0 or $index >= $num_pieces
$num_pieces == 0 $index >= $num_pieces
Truth table66%2/3
A or B
A= $num_pieces == 0B= $index >= $num_pieces
ABresult
1X1
011
000
471 149 ▶ $self->_emit_log( 'debug', "Peer SUGGEST_PIECE with out-of-range index $index" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 149 149
472 149 $self->adjust_reputation(-2);
473 149 return;
474 }
475 0 ▶ push @suggested_pieces, $index if scalar @suggested_pieces < 100 && !grep { $_ == $index } @suggested_pieces;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @suggested_pieces < 100 and not grep({$_ == $index;} @suggested_pieces) 2 0 2
Condition33%1/3
scalar @suggested_pieces < 100 and not grep({$_ == $index;} @suggested_pieces)
!ll&&!rl&&r
002
MC/DC0%0/2
scalar @suggested_pieces < 100 and not grep({$_ == $index;} @suggested_pieces)
scalar @suggested_pieces < 100 not grep({$_ == $index;} @suggested_pieces)
Truth table33%1/3
A and not(B)
A= scalar @suggested_pieces < 100B= not grep({$_ == $index;} @suggested_pieces)
ABresult
0X0
100
111
476 2 $self->_check_interest();
477 }
478 elsif ( $id == 14 ) { # HAVE_ALL
479 0 $bitfield_status = 'all';
480 0 $torrent->set_peer_have_all($self);
481 0 $self->_emit('have_all');
482 0 $self->_check_interest();
483 }
484 elsif ( $id == 15 ) { # HAVE_NONE
485 2 $bitfield_status = 'none';
486 2 $torrent->set_peer_have_none($self);
487 2 $self->_emit('have_none');
488 }
489 elsif ( $id == 16 ) { # REJECT
490 1 my ( $index, $begin, $len ) = unpack( 'N N N', $payload );
491 1 $self->_handle_reject( $index, $begin, $len );
492 }
493 elsif ( $id == 17 ) { # ALLOWED_FAST
494 21 my $index = unpack( 'N', $payload );
495 21 ▶ my $num_pieces = $torrent->bitfield ? $torrent->bitfield->size : 0;
Branch: 1 branch
BranchTrueFalseTotal
$torrent->bitfield ? : 19 2 21
496 21 ▶ if ( $num_pieces == 0 || $index >= $num_pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_pieces == 0 or $index >= $num_pieces) 16 5 21
Condition100%3/3
$num_pieces == 0 or $index >= $num_pieces
l!l&&r!l&&!r
2145
MC/DC100%2/2
$num_pieces == 0 or $index >= $num_pieces
$num_pieces == 0 $index >= $num_pieces
Truth table100%3/3
A or B
A= $num_pieces == 0B= $index >= $num_pieces
ABresult
1X1
011
000
497 16 ▶ $self->_emit_log( 'debug', "Peer ALLOWED_FAST with out-of-range index $index" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 16 16
498 16 $self->adjust_reputation(-2);
499 16 return;
500 }
501 5 ▶ my $max_allowed = $num_pieces < 10 ? $num_pieces : 10;
Branch: 1 branch
BranchTrueFalseTotal
$num_pieces < 10 ? : 5 0 5
502 0 ▶ push @allowed_fast_set, $index if scalar @allowed_fast_set < $max_allowed && !grep { $_ == $index } @allowed_fast_set;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @allowed_fast_set < $max_allowed and not grep({$_ == $index;} @allowed_fast_set) 5 0 5
Condition33%1/3
scalar @allowed_fast_set < $max_allowed and not grep({$_ == $index;} @allowed_fast_set)
!ll&&!rl&&r
005
MC/DC0%0/2
scalar @allowed_fast_set < $max_allowed and not grep({$_ == $index;} @allowed_fast_set)
scalar @allowed_fast_set < $max_allowed not grep({$_ == $index;} @allowed_fast_set)
Truth table33%1/3
A and not(B)
A= scalar @allowed_fast_set < $max_allowedB= not grep({$_ == $index;} @allowed_fast_set)
ABresult
0X0
100
111
503 5 $self->_check_interest();
504 }
505 }
506  
507 1 ▶ method _handle_reject ( $index, $begin, $len ) {
Subroutine _handle_reject: called CC=3 SCAR=12.0
508 1 my $key = "$index,$begin";
509 1 ▶ $blocks_inflight-- if delete $self->requested_blocks->{$key} && $blocks_inflight > 0;
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->requested_blocks->{$key} and $blocks_inflight > 0 0 1 1
Condition33%1/3
delete $self->requested_blocks->{$key} and $blocks_inflight > 0
!ll&&!rl&&r
100
MC/DC0%0/2
delete $self->requested_blocks->{$key} and $blocks_inflight > 0
delete $self->requested_blocks->{$key} $blocks_inflight > 0
Truth table33%1/3
A and B
A= delete $self->requested_blocks->{$key}B= $blocks_inflight > 0
ABresult
0X0
100
111
510 1 delete $torrent->blocks_pending->{$index}{$begin};
511 1 $self->_request_next_block();
512 }
513  
514 22 ▶ method _check_interest () {
Subroutine _check_interest: called CC=8 SCAR=20.8
515 22 ▶ if ( $torrent->is_superseed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($torrent->is_superseed) 2 20 22
516 2 $self->_check_superseed();
517 }
518 22 ▶ if ( !$am_interested ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $am_interested) 20 2 22
519 20 my $bitfield = $torrent->bitfield;
520 20 my $p_bfs = $torrent->peer_bitfields;
521 20 my $p_bf = $p_bfs->{$self};
522 20 my $has_new = 0;
523 20 ▶ if ($p_bf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_bf) 10 10 20
524 10 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 12 6 18
525 12 ▶ if ( !$bitfield->get($i) && $p_bf->get($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $bitfield->get($i) and $p_bf->get($i)) 4 8 12
Condition66%2/3
not $bitfield->get($i) and $p_bf->get($i)
!ll&&!rl&&r
804
MC/DC50%1/2
not $bitfield->get($i) and $p_bf->get($i)
not $bitfield->get($i) $p_bf->get($i)
Truth table66%2/3
not(A) and B
A= not $bitfield->get($i)B= $p_bf->get($i)
ABresult
0X0
100
111
526 4 $has_new = 1;
527 4 last;
528 }
529 }
530 }
531 20 ▶ if ($has_new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_new) 4 16 20
532 4 $am_interested = 1;
533 4 $protocol->send_message(2); # INTERESTED
534 }
535 }
536 }
537  
538 2 ▶ method _check_superseed () {
Subroutine _check_superseed: called CC=6 SCAR=18.3
539 2 ▶ return if defined $offered_piece;
Branch: 1 branch
BranchTrueFalseTotal
if defined $offered_piece 0 2 2
540  
541 # Pick a piece to offer
542 2 my $bitfield = $torrent->bitfield;
543 2 my $p_bfs = $torrent->peer_bitfields;
544 2 my $p_bf = $p_bfs->{$self};
545 2 ▶ return unless $p_bf;
Branch: 1 branch
BranchTrueFalseTotal
unless $p_bf 0 2 2
546 2 ▶ for ( my $i = 0; $i < $bitfield->size; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bitfield->size) 3 0 3
547 3 ▶ if ( $bitfield->get($i) && !$p_bf->get($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bitfield->get($i) and not $p_bf->get($i)) 2 1 3
Condition66%2/3
$bitfield->get($i) and not $p_bf->get($i)
!ll&&!rl&&r
012
MC/DC50%1/2
$bitfield->get($i) and not $p_bf->get($i)
$bitfield->get($i) not $p_bf->get($i)
Truth table66%2/3
A and not(B)
A= $bitfield->get($i)B= not $p_bf->get($i)
ABresult
0X0
100
111
548 2 $offered_piece = $i;
549 2 $protocol->send_message( 4, pack( 'N', $i ) ); # HAVE
550 2 last;
551 }
552 }
553 }
554  
555 38 ▶ method _request_next_block () {
Subroutine _request_next_block: called CC=5 SCAR=17.0
556 38 ▶ while ( $blocks_inflight < 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks_inflight < 5) 50 3 53
557 50 my $req = $torrent->get_next_request($self);
558 50 ▶ if ($req) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req) { } 15 35 50
559  
560 # BEP 06: Can request if not choked OR if piece is in allowed_fast_set
561 15 ▶ if ( !$peer_choking || $self->is_allowed_fast( $req->{index} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $peer_choking or $self->is_allowed_fast($req->{'index'})) { } 15 0 15
Condition33%1/3
not $peer_choking or $self->is_allowed_fast($req->{"index"})
l!l&&r!l&&!r
1500
MC/DC0%0/2
not $peer_choking or $self->is_allowed_fast($req->{"index"})
not $peer_choking $self->is_allowed_fast($req->{"index"})
Truth table33%1/3
not(A) or B
A= not $peer_chokingB= $self->is_allowed_fast($req->{"index"})
ABresult
1X1
011
000
562 15 $protocol->send_message( 6, pack( 'N N N', $req->{index}, $req->{begin}, $req->{length} ) );
563 15 $blocks_inflight++;
564 15 $self->requested_blocks->{"$req->{index},$req->{begin}"} = 1;
565 }
566 else {
567 # We picked a piece but we are choked and it's not fast-allowed.
568 # We must un-pending it so others can pick it.
569 0 delete $torrent->blocks_pending->{ $req->{index} }{ $req->{begin} };
570 0 last;
571 }
572 }
573 else {
574 35 last;
575 }
576 }
577 }
578  
579 0 ▶ method is_allowed_fast ($index) {
Subroutine is_allowed_fast: not called CC=1 SCAR=6.9
Pod documented
580 0 return grep { $_ == $index } @allowed_fast_set;
581 }
582  
583 17 ▶ method _handle_request ( $index, $begin, $len ) {
Subroutine _handle_request: called CC=7 SCAR=19.7
584 17 ▶ return if $am_choking;
Branch: 1 branch
BranchTrueFalseTotal
if $am_choking 0 17 17
585  
586 # Reputation checks
587 # Do we even have this piece?
588 17 ▶ if ( !$torrent->bitfield->get($index) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($torrent->bitfield->get($index)) 1 16 17
589 1 $self->adjust_reputation(-5);
590 1 return;
591 }
592  
593 # Does the peer already have this piece?
594 16 my $p_bf = $torrent->peer_bitfields->{$self};
595 16 ▶ if ( $p_bf && $p_bf->get($index) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_bf and $p_bf->get($index)) 1 15 16
Condition66%2/3
$p_bf and $p_bf->get($index)
!ll&&!rl&&r
0151
MC/DC50%1/2
$p_bf and $p_bf->get($index)
$p_bf $p_bf->get($index)
Truth table66%2/3
A and B
A= $p_bfB= $p_bf->get($index)
ABresult
0X0
100
111
596 1 $self->adjust_reputation(-5);
597 1 return;
598 }
599 15 ▶ my $piece_len = $torrent->metadata->{info}{'piece length'} // 16384;
Condition50%1/2
$torrent->metadata->{"info"}{"piece length"} // 16384
l!l
150
MC/DC0%0/1
$torrent->metadata->{"info"}{"piece length"} // 16384
$torrent->metadata->{"info"}{"piece length"}
Truth table50%1/2
A
A= $torrent->metadata->{"info"}{"piece length"}
Aresult
11
00
600 15 my $abs_offset = ( $index * $piece_len ) + $begin;
601 15 my $data = $torrent->storage->read_global( $abs_offset, $len );
602 15 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 15 0 15
603 15 $bytes_up += length($data);
604 15 $protocol->send_message( 7, pack( 'N N', $index, $begin ) . $data );
605 }
606 }
607  
608 30 ▶ method _handle_piece_data ( $index, $begin, $data ) {
Subroutine _handle_piece_data: called CC=10 SCAR=27.0
609 30 ▶ $self->_emit_log( 'debug', 'Received ' . length($data) . " bytes for piece $index at $begin" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 30 30
610 30 ▶ if ( length($data) == 0 || length($data) > 131072 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data == 0 or length $data > 131072) 0 30 30
Condition33%1/3
length $data == 0 or length $data > 131072
l!l&&r!l&&!r
0030
MC/DC0%0/2
length $data == 0 or length $data > 131072
length $data == 0 length $data > 131072
Truth table33%1/3
A or B
A= length $data == 0B= length $data > 131072
ABresult
1X1
011
000
611 0 ▶ $self->_emit_log( 'debug', "Invalid PIECE data length: " . length($data) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
612 0 $self->adjust_reputation(-5);
613 0 return;
614 }
615 30 $bytes_down += length($data);
616 30 my $key = "$index,$begin";
617 30 ▶ if ( delete $self->requested_blocks->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->requested_blocks->{$key}) { } 3 27 30
618 3 ▶ $blocks_inflight-- if $blocks_inflight > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $blocks_inflight > 0 3 0 3
619 }
620 else {
621 27 ▶ $self->_emit_log( 'warning', "Received unsolicited PIECE for $key" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 27 27
622 27 $self->adjust_reputation(-10);
623 }
624 30 my $status = $torrent->receive_block( $self, $index, $begin, $data );
625 30 ▶ if ( $status == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($status == 1) { } 3 27 30
elsif ($status == -1) { } 0 27 27
626  
627 # Verified and saved
628 }
629 elsif ( $status == -1 ) {
630  
631 # Failed verification
632 }
633 30 $self->_request_next_block();
634 }
635  
636 32 ▶ method disconnected () {
Subroutine disconnected: called CC=5 SCAR=16.6
Pod documented
637 32 ▶ return if $_disconnected;
Branch: 1 branch
BranchTrueFalseTotal
if $_disconnected 0 32 32
638 32 $_disconnected = 1;
639 32 ▶ $torrent->peer_disconnected($self) if $torrent;
Branch: 1 branch
BranchTrueFalseTotal
if $torrent 32 0 32
640 32 ▶ $transport->close() if $transport && $transport->can('close');
Branch: 1 branch
BranchTrueFalseTotal
if $transport and $transport->can("close") 23 9 32
Condition66%2/3
$transport and $transport->can("close")
!ll&&!rl&&r
0923
MC/DC50%1/2
$transport and $transport->can("close")
$transport $transport->can("close")
Truth table66%2/3
A and B
A= $transportB= $transport->can("close")
ABresult
0X0
100
111
641 32 $self->_emit('disconnected');
642 }
643  
644 16 ▶ method unchoke () {
Subroutine unchoke: called CC=1 SCAR=0.0
Pod documented
645 16 $am_choking = 0;
646 16 $protocol->send_message(1); # UNCHOKE
647 }
648  
649 0 ▶ method choke () {
Subroutine choke: not called CC=1 SCAR=6.9
Pod documented
650 0 $am_choking = 1;
651 0 $protocol->send_message(0); # CHOKE
652 }
653  
654 0 ▶ method interested () {
Subroutine interested: not called CC=1 SCAR=6.9
Pod documented
655 0 $am_interested = 1;
656 0 $protocol->send_message(2); # INTERESTED
657 }
658  
659 2 ▶ method not_interested () {
Subroutine not_interested: called CC=1 SCAR=0.0
Pod documented
660 2 $am_interested = 0;
661 2 $protocol->send_message(3); # NOT_INTERESTED
662 }
663  
664 0 ▶ method request ( $index, $begin, $len ) {
Subroutine request: not called CC=1 SCAR=6.9
Pod documented
665 0 $blocks_inflight++;
666 0 $self->requested_blocks->{"$index,$begin"} = 1;
667 0 $protocol->send_message( 6, pack( 'N N N', $index, $begin, $len ) );
668 }
669  
670 0 ▶ method cancel_request ( $index, $begin ) {
Subroutine cancel_request: not called CC=3 SCAR=24.8
Pod undocumented
671 0 my $key = "$index,$begin";
672 0 ▶ if ( delete $self->requested_blocks->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->requested_blocks->{$key}) 0 0 0
673 0 ▶ $blocks_inflight-- if $blocks_inflight > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $blocks_inflight > 0 0 0 0
674 }
675 }
676  
677 28 ▶ method tick () {
Subroutine tick: called CC=15 SCAR=39.7
Pod documented
678 28 ▶ return if $_disconnected;
Branch: 1 branch
BranchTrueFalseTotal
if $_disconnected 0 28 28
679  
680 # Zombie peer detection: disconnect if no activity for too long
681 28 my $idle = time() - $last_activity;
682 28 ▶ if ( $blocks_inflight > 0 && $idle > REQUEST_TIMEOUT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks_inflight > 0 and $idle > 30) 0 28 28
Condition66%2/3
$blocks_inflight > 0 and $idle > 30
!ll&&!rl&&r
2620
MC/DC0%0/2
$blocks_inflight > 0 and $idle > 30
$blocks_inflight > 0 $idle > 30
Truth table66%2/3
A and B
A= $blocks_inflight > 0B= $idle > 30
ABresult
0X0
100
111
683 0 ▶ $self->_emit_log( 'warn', "Request timeout: $ip:$port silent for ${idle}s with $blocks_inflight inflight" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
684 0 $self->disconnected();
685 0 return;
686 }
687 28 ▶ if ( $idle > IDLE_TIMEOUT && $protocol->state eq 'OPEN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idle > 120 and $protocol->state eq "OPEN") 0 28 28
Condition33%1/3
$idle > 120 and $protocol->state eq "OPEN"
!ll&&!rl&&r
2800
MC/DC0%0/2
$idle > 120 and $protocol->state eq "OPEN"
$idle > 120 $protocol->state eq "OPEN"
Truth table33%1/3
A and B
A= $idle > 120B= $protocol->state eq "OPEN"
ABresult
0X0
100
111
688 0 ▶ $self->_emit_log( 'debug', "Idle timeout: $ip:$port silent for ${idle}s" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
689 0 $self->disconnected();
690 0 return;
691 }
692  
693 # Simple moving average / decay
694 28 $rate_down = ( $rate_down * 0.8 ) + ( $bytes_down * 0.2 );
695 28 $rate_up = ( $rate_up * 0.8 ) + ( $bytes_up * 0.2 );
696 28 $bytes_down = 0;
697 28 $bytes_up = 0;
698 28 ▶ $transport->tick() if $transport->can('tick');
Branch: 1 branch
BranchTrueFalseTotal
if $transport->can("tick") 17 11 28
699  
700 # MSE Transition Check
701 28 ▶ if ( $mse && $mse->state eq 'PAYLOAD' && !$pwp_handshake_sent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mse and $mse->state eq "PAYLOAD" and not $pwp_handshake_sent) 0 28 28
Condition50%3/6
$mse and $mse->state eq "PAYLOAD" and not $pwp_handshake_sent
!ll&&!rl&&r
2800
$mse and $mse->state eq "PAYLOAD"
!ll&&!rl&&r
10180
MC/DC0%0/3
$mse and $mse->state eq "PAYLOAD" and not $pwp_handshake_sent
$mse $mse->state eq "PAYLOAD" not $pwp_handshake_sent
Truth table50%2/4
A and B and not(C)
A= $mseB= $mse->state eq "PAYLOAD"C= not $pwp_handshake_sent
ABCresult
0XX0
10X0
1100
1111
702 0 ▶ $self->_emit_log( 'debug', 'MSE handshake complete, sending protocol handshake...' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
703 0 $protocol->send_handshake();
704 0 $pwp_handshake_sent = 1;
705 }
706  
707 # Fatal Protocol Error Check
708 28 ▶ if ( $protocol->state eq 'CLOSED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol->state eq "CLOSED") 0 28 28
709 0 ▶ $self->_emit_log( 'error', "Fatal protocol error from $ip:$port. Disconnecting." ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
710 0 $self->disconnected();
711 0 return;
712 }
713 28 $self->write_buffer();
714 }
715  
716 215 ▶ method adjust_reputation ($delta) {
Subroutine adjust_reputation: called CC=6 SCAR=18.0
Pod documented
717 215 ▶ return if $_disconnected;
Branch: 1 branch
BranchTrueFalseTotal
if $_disconnected 109 106 215
718 106 $reputation += $delta;
719 106 ▶ $reputation = 0 if $reputation < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $reputation < 0 0 106 106
720 106 ▶ $reputation = 100 if $reputation > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $reputation > 100 1 105 106
721 106 ▶ if ( $reputation <= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reputation <= 20) 2 104 106
722 2 ▶ $self->_emit_log( 'error', "Blacklisting peer $ip:$port due to low reputation ($reputation)" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
723 2 $self->disconnected();
724 }
725 }
726 };
727 25 1;
« lib/Net/BitTorrent/Transport/TCP.pm ↑ Summary lib/Net/BitTorrent/Tracker.pm »