lib/Net/BitTorrent/Peer.pm

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