lib/Net/BitTorrent/Protocol/BEP03.pm

filter: Statementstmt 85.6% 107 / 125 Branchbran 70.0% 35 / 50 Conditioncond 75.0% 9 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 77.2% 17 / 22 Podpod 100% 12 / 12 totaltotal 79.8% 182 / 228 SCAR 36.3
CC
31
Cov
81%
_process_handshake
15.2
send_handshake
8.2
_process_buffer
7.0
CRAP 37.9
« lib/Net/BitTorrent/Protocol/BEP10.pm ↑ Summary lib/Net/BitTorrent/Torrent/Generator.pm »
1 31 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 31 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #
5 31 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 class Net::BitTorrent::Protocol::BEP03 v2.1.0 : isa(Net::BitTorrent::Emitter) {
7 #
8 31 field $infohash : param = undef;
9 31 field $peer_id : param : reader;
10 31 field $reserved : param : reader : writer = "\0" x 8;
11 31 field $debug : param : reader = 0;
12 31 field $state : reader = 'HANDSHAKE'; # HANDSHAKE, OPEN, CLOSED
13 31 field $buffer_in = '';
14 31 field $buffer_out = '';
15 31 field $handshake_sent = 0;
16 31 field $detected_ih : reader = undef;
17  
18 # Message IDs
19 use constant {
20 31 CHOKE => 0,
21 UNCHOKE => 1,
22 INTERESTED => 2,
23 NOT_INTERESTED => 3,
24 HAVE => 4,
25 BITFIELD => 5,
26 REQUEST => 6,
27 PIECE => 7,
28 CANCEL => 8,
29  
30 # BEP 52
31 HASH_REQUEST => 21,
32 HASHES => 22,
33 HASH_REJECT => 23,
34  
35 # Security limits
36 MAX_MESSAGE_SIZE => 16 * 1024 * 1024, # 16 MB
37 31 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 ▶ method set_reserved_bit ( $byte, $mask ) {
Subroutine set_reserved_bit: called CC=1 SCAR=0.0
Pod documented
40 31 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 166 my $val = ord( substr( $reserved, $byte, 1 ) );
42 166 $val |= $mask;
43 166 $reserved = substr( $reserved, 0, $byte ) . chr($val) . substr( $reserved, $byte + 1 );
44 }
45  
46 ▶ method send_handshake () {
Subroutine send_handshake: called CC=6 SCAR=21.0
Pod documented
47 20 ▶ if ( !defined $infohash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $infohash) 0 20 20
48 0 $self->_emit_log( 'fatal', 'infohash required to send handshake' );
49 0 return;
50 }
51 20 my $ih_len = CORE::length($infohash);
52 20 ▶ if ( $ih_len != 20 && $ih_len != 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ih_len != 20 and $ih_len != 32) 0 20 20
Condition66%2/3
$ih_len != 20 and $ih_len != 32
!ll&&!rl&&r
1640
MC/DC0%0/2
$ih_len != 20 and $ih_len != 32
$ih_len != 20 $ih_len != 32
Truth table66%2/3
A and B
A= $ih_len != 20B= $ih_len != 32
ABresult
0X0
100
111
53 0 $self->_emit_log( 'fatal', 'Info hash must be 20 or 32 bytes' );
54 0 return;
55 }
56 20 ▶ $self->_emit_log( 'debug', 'Sending handshake (' . unpack( 'H*', $infohash ) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 20 20
57 20 my $raw = pack( 'C A19 a8', 19, 'BitTorrent protocol', $reserved ) . $infohash . $peer_id;
58 20 ▶ $self->_emit_log( 'debug', 'Handshake hex: ' . unpack( 'H*', $raw ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 20 20
59 20 $buffer_out .= $raw;
60 20 $handshake_sent = 1;
61 }
62  
63 ▶ method write_buffer () {
Subroutine write_buffer: called CC=1 SCAR=0.0
Pod documented
64 113 my $tmp = $buffer_out;
65 113 $buffer_out = '';
66 113 return $tmp;
67 }
68 31 field $processing = 0;
69  
70 ▶ method receive_data ($data) {
Subroutine receive_data: called CC=2 SCAR=7.0
Pod documented
71 66 $buffer_in .= $data;
72 66 ▶ return if $processing;
Branch: 1 branch
BranchTrueFalseTotal
if $processing 0 66 66
73 66 $processing = 1;
74 66 try { $self->_process_buffer(); }
75 catch ($e) {
76 3 $self->_emit_log( 'error', "Protocol processing error: $e" );
77 3 $state = 'CLOSED';
78 }
79 66 $processing = 0;
80 }
81  
82 ▶ method _process_buffer () {
Subroutine _process_buffer: called CC=7 SCAR=19.5
83 66 ▶ if ( $state eq 'OPEN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "OPEN") 37 29 66
84 37 $self->_process_messages();
85 35 return;
86 }
87 29 my $old_state;
88 29 ▶ while ( $state ne 'CLOSED' && ( !$old_state || $old_state ne $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state ne "CLOSED" and !$old_state || $old_state ne $state) 57 28 85
Condition83%5/6
$state ne "CLOSED" and !$old_state || $old_state ne $state
!ll&&!rl&&r
02857
not $old_state or $old_state ne $state
l!l&&r!l&&!r
292828
MC/DC66%2/3
$state ne "CLOSED" and !$old_state || $old_state ne $state
$state ne "CLOSED" not $old_state $old_state ne $state
Truth table75%3/4
A and not(B) or C
A= $state ne "CLOSED"B= not $old_stateC= $old_state ne $state
ABCresult
0XX0
1000
11X1
1011
89 57 $old_state = $state;
90 57 ▶ if ( $state eq 'HANDSHAKE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "HANDSHAKE") 29 28 57
91 29 $self->_process_handshake();
92 }
93 56 ▶ if ( $state eq 'OPEN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "OPEN") 56 0 56
94 56 $self->_process_messages();
95 }
96 }
97 }
98  
99 ▶ method _process_handshake () {
Subroutine _process_handshake: called CC=12 SCAR=27.2
100 29 ▶ return if length($buffer_in) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 1 0 29 29
101 29 my $pstrlen = ord( substr( $buffer_in, 0, 1 ) );
102 29 ▶ if ( $pstrlen != 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstrlen != 19) 1 28 29
103 1 $state = 'CLOSED';
104 1 $self->_emit_log( 'fatal',
105 'Invalid protocol string (expected 19, got ' . $pstrlen . ') hex: ' . unpack( 'H*', substr( $buffer_in, 0, 20 ) ) );
106 0 return;
107 }
108 28 ▶ return if length($buffer_in) < 1 + $pstrlen + 8 + 20 + 20; # Min v1 handshake (68 bytes)
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 1 + $pstrlen + 8 + 20 + 20 0 28 28
109 28 my $pstr = substr( $buffer_in, 1, $pstrlen );
110 28 ▶ if ( $pstr ne 'BitTorrent protocol' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstr ne "BitTorrent protocol") 0 28 28
111 0 $state = 'CLOSED';
112 0 $self->_emit_log( 'fatal', 'Invalid protocol string' );
113 0 return;
114 }
115  
116 # We have at least 68 bytes.
117 28 my $ih_len;
118 28 ▶ if ( defined $infohash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $infohash) { } 26 2 28
119 26 $ih_len = length($infohash);
120 }
121 else {
122 # If we have exactly 80 bytes, or more than 80, it MIGHT be a v2 handshake.
123 # But it could also be a v1 handshake (68) followed by a small message.
124 # A v2 handshake MUST be exactly 80 bytes if nothing else follows it.
125 # For now, let's look at the actual length of the buffer.
126 # If it's 68-79, it must be v1.
127 # If it's exactly 80, we assume v2 if we don't know yet?
128 # Actually, most swarms are still v1.
129 2 ▶ if ( length($buffer_in) == 80 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $buffer_in == 80) { } 1 1 2
elsif (length $buffer_in > 80) { } 0 1 1
130 1 $ih_len = 32;
131 }
132 elsif ( length($buffer_in) > 80 ) {
133  
134 # Could be v2 + messages OR v1 + messages.
135 # This is ambiguous without knowing which IH the peer is using.
136 # Standard practice: if we didn't specify, assume v1 first.
137 0 $ih_len = 20;
138 }
139 else {
140 1 $ih_len = 20;
141 }
142 }
143 28 my $handshake_len = 1 + $pstrlen + 8 + $ih_len + 20;
144 28 ▶ return if length($buffer_in) < $handshake_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < $handshake_len 0 28 28
145 28 my $remote_res = substr( $buffer_in, 1 + $pstrlen, 8 );
146 28 my $remote_ih = substr( $buffer_in, 1 + $pstrlen + 8, $ih_len );
147 28 my $remote_id = substr( $buffer_in, 1 + $pstrlen + 8 + $ih_len, 20 );
148 28 ▶ if ( defined $infohash && $remote_ih ne $infohash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $infohash and $remote_ih ne $infohash) 0 28 28
Condition66%2/3
defined $infohash and $remote_ih ne $infohash
!ll&&!rl&&r
2260
MC/DC0%0/2
defined $infohash and $remote_ih ne $infohash
defined $infohash $remote_ih ne $infohash
Truth table66%2/3
A and B
A= defined $infohashB= $remote_ih ne $infohash
ABresult
0X0
100
111
149  
150 # If we were expecting v2 but got v1, it might fail here.
151 0 $state = 'CLOSED';
152 0 $self->_emit_log( 'fatal', 'Info hash mismatch' );
153 0 return;
154 }
155 28 substr( $buffer_in, 0, $handshake_len, '' );
156 28 $state = 'OPEN';
157 28 $detected_ih = $remote_ih;
158 28 $reserved = $remote_res;
159 28 ▶ $self->_emit_log( 'debug', "Received handshake from " . unpack( 'H*', $remote_id ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 28 28
160 28 $self->_emit( handshake => $remote_ih, $remote_id );
161 }
162  
163 ▶ method _process_messages () {
Subroutine _process_messages: called CC=6 SCAR=17.9
164 93 ▶ while ( length($buffer_in) >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in >= 4) 89 90 179
165 89 my $msg_len = unpack( 'N', substr( $buffer_in, 0, 4 ) );
166 89 ▶ if ( $msg_len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_len == 0) 1 88 89
167 1 substr( $buffer_in, 0, 4, '' ); # Keep-alive
168 1 next;
169 }
170 88 ▶ if ( $msg_len > MAX_MESSAGE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_len > 16777216) 2 86 88
171 2 $state = 'CLOSED';
172 2 $self->_emit_log( 'fatal', "Message too large ($msg_len bytes, max " . MAX_MESSAGE_SIZE . ')' );
173 0 return;
174 }
175 86 ▶ if ( length($buffer_in) < 4 + $msg_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in < 4 + $msg_len) 1 85 86
176 1 return;
177 }
178 85 my $raw_msg = substr( $buffer_in, 0, 4 + $msg_len, '' );
179 85 my $id = unpack( 'C', substr( $raw_msg, 4, 1 ) );
180 85 my $payload = substr( $raw_msg, 5 );
181 85 ▶ $self->_emit_log( 'debug', "Received message ID $id (len " . length($payload) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 85 85
182 85 $self->_handle_message( $id, $payload );
183 }
184 }
185 ▶ method _handle_message ( $id, $payload ) { }
Subroutine _handle_message: called CC=1 SCAR=0.0
186  
187 90 ▶ method send_message ( $id, $payload = '' ) {
Subroutine send_message: called CC=3 SCAR=11.2
Pod documented
188 90 ▶ $self->_emit_log( 'debug', "Sending message ID $id (len " . length($payload) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 90 90
189 90 $buffer_out .= pack( 'N C a*', 1 + length($payload), $id, $payload );
190 }
191 1 ▶ method send_keepalive () { $buffer_out .= pack( 'N', 0 ) }
Subroutine send_keepalive: called CC=1 SCAR=0.0
Pod documented
192 0 ▶ method send_choke () { $self->send_message(CHOKE) }
Subroutine send_choke: not called CC=1 SCAR=6.9
Pod documented
193 0 ▶ method send_unchoke () { $self->send_message(UNCHOKE) }
Subroutine send_unchoke: not called CC=1 SCAR=6.9
Pod documented
194 0 ▶ method send_interested () { $self->send_message(INTERESTED) }
Subroutine send_interested: not called CC=1 SCAR=6.9
Pod documented
195 0 ▶ method send_not_interested () { $self->send_message(NOT_INTERESTED) }
Subroutine send_not_interested: not called CC=1 SCAR=6.9
Pod documented
196 0 ▶ method send_have ($index) { $self->send_message( HAVE, pack( 'N', $index ) ) }
Subroutine send_have: not called CC=1 SCAR=6.9
Pod documented
197  
198 ▶ method send_bitfield ($data) {
Subroutine send_bitfield: called CC=1 SCAR=0.0
Pod documented
199 7 $self->send_message( BITFIELD, $data );
200 }
201 };
202 31 1;
« lib/Net/BitTorrent/Protocol/BEP10.pm ↑ Summary lib/Net/BitTorrent/Torrent/Generator.pm »