lib/Net/BitTorrent/Protocol/BEP03.pm

filter: Statementstmt 83.8% 109 / 130 Branchbran 68.5% 37 / 54 Conditioncond 75.0% 9 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 77.2% 17 / 22 Podpod 100% 12 / 12 totaltotal 78.4% 186 / 237 CC 33 SCAR 19.1
CC
33
Cov
79%
_process_handshake
15.2
send_handshake
8.2
_process_buffer
7.0
CRAP 6.8
« lib/Net/BitTorrent/Torrent/Generator.pm ↑ Summary lib/Net/BitTorrent/DHT/Security.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.1 : 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 31 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
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 };
38  
39 166 ▶ 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 20 ▶ 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 81 ▶ method write_buffer () {
Subroutine write_buffer: called CC=1 SCAR=0.0
Pod documented
64 81 my $tmp = $buffer_out;
65 81 $buffer_out = '';
66 81 return $tmp;
67 }
68 31 field $processing = 0;
69  
70 65 ▶ method receive_data ($data) {
Subroutine receive_data: called CC=4 SCAR=15.0
Pod documented
71 65 ▶ return if $state eq 'CLOSED';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "CLOSED" 0 65 65
72 65 $buffer_in .= $data;
73 65 ▶ if ( length($buffer_in) > MAX_MESSAGE_SIZE + 65536 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in > 16842752) 0 65 65
74 0 $state = 'CLOSED';
75 0 $self->_emit_log( 'fatal', 'Inbound buffer exceeded hard limit, disconnecting' );
76 0 return;
77 }
78 65 ▶ return if $processing;
Branch: 1 branch
BranchTrueFalseTotal
if $processing 0 65 65
79 65 $processing = 1;
80 65 try { $self->_process_buffer(); }
81 catch ($e) {
82 3 $self->_emit_log( 'error', "Protocol processing error: $e" );
83 3 $state = 'CLOSED';
84 }
85 65 $processing = 0;
86 }
87  
88 65 ▶ method _process_buffer () {
Subroutine _process_buffer: called CC=7 SCAR=19.5
89 65 ▶ if ( $state eq 'OPEN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "OPEN") 36 29 65
90 36 $self->_process_messages();
91 34 return;
92 }
93 29 my $old_state;
94 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
95 57 $old_state = $state;
96 57 ▶ if ( $state eq 'HANDSHAKE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "HANDSHAKE") 29 28 57
97 29 $self->_process_handshake();
98 }
99 56 ▶ if ( $state eq 'OPEN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "OPEN") 56 0 56
100 56 $self->_process_messages();
101 }
102 }
103 }
104  
105 29 ▶ method _process_handshake () {
Subroutine _process_handshake: called CC=12 SCAR=27.2
106 29 ▶ return if length($buffer_in) < 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < 1 0 29 29
107 29 my $pstrlen = ord( substr( $buffer_in, 0, 1 ) );
108 29 ▶ if ( $pstrlen != 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstrlen != 19) 1 28 29
109 1 $state = 'CLOSED';
110 1 $self->_emit_log( 'fatal',
111 'Invalid protocol string (expected 19, got ' . $pstrlen . ') hex: ' . unpack( 'H*', substr( $buffer_in, 0, 20 ) ) );
112 0 return;
113 }
114 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
115 28 my $pstr = substr( $buffer_in, 1, $pstrlen );
116 28 ▶ if ( $pstr ne 'BitTorrent protocol' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstr ne "BitTorrent protocol") 0 28 28
117 0 $state = 'CLOSED';
118 0 $self->_emit_log( 'fatal', 'Invalid protocol string' );
119 0 return;
120 }
121  
122 # We have at least 68 bytes.
123 28 my $ih_len;
124 28 ▶ if ( defined $infohash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $infohash) { } 26 2 28
125 26 $ih_len = length($infohash);
126 }
127 else {
128 # If we have exactly 80 bytes, or more than 80, it MIGHT be a v2 handshake.
129 # But it could also be a v1 handshake (68) followed by a small message.
130 # A v2 handshake MUST be exactly 80 bytes if nothing else follows it.
131 # For now, let's look at the actual length of the buffer.
132 # If it's 68-79, it must be v1.
133 # If it's exactly 80, we assume v2 if we don't know yet?
134 # Actually, most swarms are still v1.
135 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
136 1 $ih_len = 32;
137 }
138 elsif ( length($buffer_in) > 80 ) {
139  
140 # Could be v2 + messages OR v1 + messages.
141 # This is ambiguous without knowing which IH the peer is using.
142 # Standard practice: if we didn't specify, assume v1 first.
143 0 $ih_len = 20;
144 }
145 else {
146 1 $ih_len = 20;
147 }
148 }
149 28 my $handshake_len = 1 + $pstrlen + 8 + $ih_len + 20;
150 28 ▶ return if length($buffer_in) < $handshake_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer_in < $handshake_len 0 28 28
151 28 my $remote_res = substr( $buffer_in, 1 + $pstrlen, 8 );
152 28 my $remote_ih = substr( $buffer_in, 1 + $pstrlen + 8, $ih_len );
153 28 my $remote_id = substr( $buffer_in, 1 + $pstrlen + 8 + $ih_len, 20 );
154 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
155  
156 # If we were expecting v2 but got v1, it might fail here.
157 0 $state = 'CLOSED';
158 0 $self->_emit_log( 'fatal', 'Info hash mismatch' );
159 0 return;
160 }
161 28 substr( $buffer_in, 0, $handshake_len, '' );
162 28 $state = 'OPEN';
163 28 $detected_ih = $remote_ih;
164 28 $reserved = $remote_res;
165 28 ▶ $self->_emit_log( 'debug', "Received handshake from " . unpack( 'H*', $remote_id ) ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 28 28
166 28 $self->_emit( handshake => $remote_ih, $remote_id );
167 }
168  
169 92 ▶ method _process_messages () {
Subroutine _process_messages: called CC=6 SCAR=17.9
170 92 ▶ while ( length($buffer_in) >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in >= 4) 88 89 177
171 88 my $msg_len = unpack( 'N', substr( $buffer_in, 0, 4 ) );
172 88 ▶ if ( $msg_len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_len == 0) 1 87 88
173 1 substr( $buffer_in, 0, 4, '' ); # Keep-alive
174 1 next;
175 }
176 87 ▶ if ( $msg_len > MAX_MESSAGE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_len > 16777216) 2 85 87
177 2 $state = 'CLOSED';
178 2 $self->_emit_log( 'fatal', "Message too large ($msg_len bytes, max " . MAX_MESSAGE_SIZE . ')' );
179 0 return;
180 }
181 85 ▶ if ( length($buffer_in) < 4 + $msg_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer_in < 4 + $msg_len) 1 84 85
182 1 return;
183 }
184 84 my $raw_msg = substr( $buffer_in, 0, 4 + $msg_len, '' );
185 84 my $id = unpack( 'C', substr( $raw_msg, 4, 1 ) );
186 84 my $payload = substr( $raw_msg, 5 );
187 84 ▶ $self->_emit_log( 'debug', "Received message ID $id (len " . length($payload) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 84 84
188 84 $self->_handle_message( $id, $payload );
189 }
190 }
191 51 ▶ method _handle_message ( $id, $payload ) { }
Subroutine _handle_message: called CC=1 SCAR=0.0
192  
193 88 ▶ method send_message ( $id, $payload = '' ) {
Subroutine send_message: called CC=3 SCAR=11.2
Pod documented
194 88 ▶ $self->_emit_log( 'debug', "Sending message ID $id (len " . length($payload) . ')' ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 88 88
195 88 $buffer_out .= pack( 'N C a*', 1 + length($payload), $id, $payload );
196 }
197 1 ▶ method send_keepalive () { $buffer_out .= pack( 'N', 0 ) }
Subroutine send_keepalive: called CC=1 SCAR=0.0
Pod documented
198 0 ▶ method send_choke () { $self->send_message(CHOKE) }
Subroutine send_choke: not called CC=1 SCAR=6.9
Pod documented
199 0 ▶ method send_unchoke () { $self->send_message(UNCHOKE) }
Subroutine send_unchoke: not called CC=1 SCAR=6.9
Pod documented
200 0 ▶ method send_interested () { $self->send_message(INTERESTED) }
Subroutine send_interested: not called CC=1 SCAR=6.9
Pod documented
201 0 ▶ method send_not_interested () { $self->send_message(NOT_INTERESTED) }
Subroutine send_not_interested: not called CC=1 SCAR=6.9
Pod documented
202 0 ▶ method send_have ($index) { $self->send_message( HAVE, pack( 'N', $index ) ) }
Subroutine send_have: not called CC=1 SCAR=6.9
Pod documented
203  
204 7 ▶ method send_bitfield ($data) {
Subroutine send_bitfield: called CC=1 SCAR=0.0
Pod documented
205 7 $self->send_message( BITFIELD, $data );
206 }
207 };
208 31 1;
« lib/Net/BitTorrent/Torrent/Generator.pm ↑ Summary lib/Net/BitTorrent/DHT/Security.pm »