lib/Net/BitTorrent/DHT.pm

filter: Statementstmt 81.9% 513 / 626 Branchbran 62.3% 248 / 398 Conditioncond 47.9% 172 / 359 MC/DCmcdc 20.2% 40 / 198 Subroutinesub 85.7% 48 / 56 Podpod 95.0% 19 / 20 totaltotal 62.7% 1040 / 1657 CC 366 SCAR 61.3
CC
366
Cov
67%
_handle_query
1248.8
import_state
63.7
_send
58.8
CRAP 457.9
↑ Summary lib/Net/BitTorrent.pm »
1 38 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 38 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 38 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 38 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #
6 38 class Net::BitTorrent::DHT::Peer v2.1.1 {
7 38 field $ip : param : reader;
8 38 field $port : param : reader;
9 38 field $family : param : reader;
10 1 ▶ method to_string () {"$ip:$port"}
Subroutine to_string: called CC=1 SCAR=0.0
11 };
12 #
13 38 class Net::BitTorrent::DHT v2.1.1 : isa(Net::BitTorrent::Emitter) {
14 38 ▶ use Algorithm::Kademlia;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 38 ▶ use Net::BitTorrent::DHT::Security;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 38 ▶ use Net::BitTorrent::Protocol::BEP03::Bencode qw[bencode bdecode];
Subroutine BEGIN: called CC=1 SCAR=0.0
17 38 ▶ use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Socket
19 38 ▶ qw[sockaddr_family pack_sockaddr_in unpack_sockaddr_in inet_aton inet_ntoa AF_INET AF_INET6 pack_sockaddr_in6 unpack_sockaddr_in6 inet_pton inet_ntop getaddrinfo getnameinfo NI_NUMERICHOST SOCK_DGRAM];
Subroutine BEGIN: called CC=1 SCAR=0.0
20 38 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 38 ▶ use Digest::SHA qw[sha1];
Subroutine BEGIN: called CC=1 SCAR=0.0
22 38 ▶ use Crypt::URandom qw[urandom];
Subroutine BEGIN: called CC=1 SCAR=0.0
23 38 ▶ use Net::BitTorrent::SSRF qw[is_safe_ip is_safe_host];
Subroutine BEGIN: called CC=1 SCAR=0.0
24 #
25 38 ▶ use constant MAX_STORAGE_ENTRIES => 10000; # Max total entries in peer+data storage
Subroutine BEGIN: called CC=1 SCAR=0.0
26 38 ▶ use constant MAX_IMMUTABLE_VALUE_SIZE => 1024 * 1024; # 1 MB max for BEP44 immutable data values
Subroutine BEGIN: called CC=1 SCAR=0.0
27 38 ▶ use constant MAX_UNPACK_NODES => 200; # Max nodes unpacked from a single response
Subroutine BEGIN: called CC=1 SCAR=0.0
28 38 ▶ use constant MAX_IMPORT_NODES => 2000;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 38 ▶ use constant MAX_IMPORT_ENTRIES => 5000;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 38 field $node_id_bin : param : reader //= urandom(20);
31 38 field $port : param : reader = 6881;
32 38 field $address : param //= undef;
33 38 field $want_v4 : param : reader //= 1;
34 38 field $want_v6 : param : reader //= 1;
35 38 field $bep32 : param : reader //= 1;
36 38 field $bep42 : param : reader //= 1;
37 38 field $bep33 : param : reader //= 1;
38 38 field $bep44 : param : reader //= 1;
39 38 field $bep51 : param : reader //= 1;
40 38 field $read_only : param = 0;
41 38 field $ssrf_bypass : param = 0;
42 38 field $security : reader = Net::BitTorrent::DHT::Security->new();
43 38 field $routing_table_v4 : reader = Algorithm::Kademlia::RoutingTable->new( local_id_bin => $node_id_bin, k => 8 );
44 38 field $routing_table_v6 : reader = Algorithm::Kademlia::RoutingTable->new( local_id_bin => $node_id_bin, k => 8 );
45 38 field $peer_storage : reader = Algorithm::Kademlia::Storage->new( ttl => 7200 );
46 38 field $data_storage : reader = Algorithm::Kademlia::Storage->new( ttl => 7200 );
47 38 field $socket : param : reader //= IO::Socket::IP->new( LocalAddr => $address, LocalPort => $port, Proto => 'udp', Blocking => 0 );
48 38 field $select //= IO::Select->new($socket);
49 38 field $token_secret = urandom(32);
50 38 field $token_old_secret = $token_secret;
51 38 field $last_rotation = time;
52 38 field $node_id_rotation_interval : param = 7200; # 2 hours
53 38 field $last_node_id_rotation = time;
54 38 field $last_external_ip_change = 0;
55 field $boot_nodes : param : reader : writer //= [ [ 'router.bittorrent.com', 6881 ], [ 'router.utorrent.com', 6881 ],
56 38 [ 'dht.transmissionbt.com', 6881 ] ];
57 38 field @_resolved_boot_nodes;
58 38 field $v : param : reader //= ();
59 38 field $debug : param = 0;
60 38 field $_ed25519_backend = ();
61 38 field $running = 0;
62 38 field %_blacklist;
63 38 field %_query_rate; # ip => [ timestamps ] for per-IP rate limiting
64 38 field %ip_votes; # external_ip => count
65 38 field $external_ip : reader = undef;
66 38 field %_pending_queries;
67 38 field $_tid_counter = 0;
68  
69 9 ▶ method set_node_id ($new_id) {
Subroutine set_node_id: called CC=3 SCAR=11.0
Pod documented
70 9 ▶ return unless defined $new_id && length($new_id) == 20;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_id and length $new_id == 20 3 6 9
Condition100%3/3
defined $new_id and length $new_id == 20
!ll&&!rl&&r
126
MC/DC100%2/2
defined $new_id and length $new_id == 20
defined $new_id length $new_id == 20
Truth table100%3/3
A and B
A= defined $new_idB= length $new_id == 20
ABresult
0X0
100
111
71 6 $node_id_bin = $new_id;
72 6 $routing_table_v4->set_local_id_bin($new_id);
73 6 $routing_table_v6->set_local_id_bin($new_id);
74 }
75 122 ▶ ADJUST {
Subroutine __ANON__: called CC=5 SCAR=19.0
76 122 ▶ $socket // $self->_emit_log( 'fatal', 'Could not create UDP socket: ' . $! );
Condition33%1/3
$socket // $self->_emit_log("fatal", 'Could not create UDP socket: ' . $!)
l!l
12200
MC/DC0%0/2
$socket // $self->_emit_log("fatal", 'Could not create UDP socket: ' . $!)
$socket $self->_emit_log("fatal", 'Could not create UDP socket: ' . $!)
Truth table33%1/3
A // B
A= $socketB= $self->_emit_log("fatal", 'Could not create UDP socket: ' . $!)
ABresult
1X1
011
000
77  
78 # Pre-resolve bootstrap nodes
79 122 for my $r (@$boot_nodes) {
80 360 my ( $err, @res ) = getaddrinfo( $r->[0], $r->[1], { socktype => SOCK_DGRAM } );
81 360 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 360 360
82 0 $self->_emit_log( 'warn', "Could not resolve bootstrap node $r->[0]:$r->[1]: $err" );
83 0 next;
84 }
85 360 push @_resolved_boot_nodes, $res[0]{addr};
86 }
87 $self->on(
88 external_ip_detected => sub ( $emitter, $ip ) {
89 $external_ip = $ip;
90 return unless $bep42;
91 my $new_id = $security->generate_node_id($ip);
92 $self->set_node_id($new_id);
93 }
94 122 );
95 122 ▶ if ($bep44) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep44) 122 0 122
96 122 try {
97 122 require Crypt::PK::Ed25519;
98 $_ed25519_backend = method( $sig, $msg, $key ) {
99 my $ed = Crypt::PK::Ed25519->new();
100 try { $ed->import_key_raw( $key, 'public' )->verify_message( $sig, $msg ); }
101 catch ($e) { return 0; }
102 }
103 122 }
104 catch ($e) {
105 0 try {
106 0 require Crypt::Perl::Ed25519::PublicKey;
107 $_ed25519_backend = method( $sig, $msg, $key ) {
108 try {
109 # Crypt::Perl might throw if key or sig length is invalid
110 return 0 unless length($key) == 32;
111 return 0 unless length($sig) == 64;
112 my $pk = Crypt::Perl::Ed25519::PublicKey->new($key);
113 return $pk->verify( $msg, $sig );
114 }
115 catch ($e2) {
116 return 0;
117 }
118 }
119 0 }
120 catch ($e2) { }
121 }
122 }
123 }
124 56 ▶ method routing_table () {$routing_table_v4} # Backward compatibility
Subroutine routing_table: called CC=1 SCAR=0.0
Pod undocumented
125  
126 0 ▶ method routing_table_stats () {
Subroutine routing_table_stats: not called CC=3 SCAR=24.8
Pod documented
127 0 my $stats = { v4 => [], v6 => [] };
128 0 my $idx = 0;
129 0 push $stats->{v4}->@*, { index => $idx++, count => scalar @$_ } for $routing_table_v4->buckets;
130 0 $idx = 0;
131 0 push $stats->{v6}->@*, { index => $idx++, count => scalar @$_ } for $routing_table_v6->buckets;
132 0 return $stats;
133 }
134  
135 4 ▶ method export_state () {
Subroutine export_state: called CC=3 SCAR=11.2
Pod documented
136 4 my @nodes_v4;
137 4 for my $bucket ( $routing_table_v4->buckets ) {
138 0 push @nodes_v4, map { { id => $_->{id}, ip => $_->{data}{ip}, port => $_->{data}{port} } } @$bucket;
139 }
140 4 my @nodes_v6;
141 4 for my $bucket ( $routing_table_v6->buckets ) {
142 0 push @nodes_v6, map { { id => $_->{id}, ip => $_->{data}{ip}, port => $_->{data}{port} } } @$bucket;
143 }
144 4 my %peers = $peer_storage->entries;
145 4 my %data = $data_storage->entries;
146 4 return { id => $node_id_bin, nodes => \@nodes_v4, nodes6 => \@nodes_v6, peers => \%peers, data => \%data, };
147 }
148  
149 8 ▶ method import_state ($state) {
Subroutine import_state: called CC=33 SCAR=41.5
Pod documented
150 8 ▶ return unless ref $state eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $state eq "HASH" 1 7 8
151 7 try {
152 7 ▶ $node_id_bin = $state->{id} if defined $state->{id} && length( $state->{id} ) == 20;
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"id"} and length $state->{"id"} == 20 2 5 7
Condition100%3/3
defined $state->{"id"} and length $state->{"id"} == 20
!ll&&!rl&&r
412
MC/DC100%2/2
defined $state->{"id"} and length $state->{"id"} == 20
defined $state->{"id"} length $state->{"id"} == 20
Truth table100%3/3
A and B
A= defined $state->{"id"}B= length $state->{"id"} == 20
ABresult
0X0
100
111
153 7 ▶ if ( $state->{nodes} && ref $state->{nodes} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"nodes"} and ref $state->{"nodes"} eq "ARRAY") 3 4 7
Condition100%3/3
$state->{"nodes"} and ref $state->{"nodes"} eq "ARRAY"
!ll&&!rl&&r
313
MC/DC100%2/2
$state->{"nodes"} and ref $state->{"nodes"} eq "ARRAY"
$state->{"nodes"} ref $state->{"nodes"} eq "ARRAY"
Truth table100%3/3
A and B
A= $state->{"nodes"}B= ref $state->{"nodes"} eq "ARRAY"
ABresult
0X0
100
111
154 3 my @to_import;
155 3 for my $n ( $state->{nodes}->@* ) {
156 2004 ▶ next unless ref $n eq 'HASH' && defined $n->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n eq "HASH" and defined $n->{"id"} 3 2001 2004
Condition66%2/3
ref $n eq "HASH" and defined $n->{"id"}
!ll&&!rl&&r
302001
MC/DC50%1/2
ref $n eq "HASH" and defined $n->{"id"}
ref $n eq "HASH" defined $n->{"id"}
Truth table66%2/3
A and B
A= ref $n eq "HASH"B= defined $n->{"id"}
ABresult
0X0
100
111
157 2001 ▶ last if scalar @to_import >= MAX_IMPORT_NODES;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @to_import >= 2000 1 2000 2001
158 2000 ▶ push @to_import, { id => $n->{id}, data => { ip => $n->{ip} // '', port => $n->{port} // 0 } };
Condition50%2/4
$n->{"ip"} // ""
l!l
20000
$n->{"port"} // 0
l!l
20000
MC/DC0%0/1
$n->{"ip"} // ""
$n->{"ip"}
MC/DC0%0/1
$n->{"port"} // 0
$n->{"port"}
Truth table50%1/2
A
A= $n->{"ip"}
Aresult
11
00
Truth table50%1/2
A
A= $n->{"port"}
Aresult
11
00
159 }
160 3 ▶ $routing_table_v4->import_peers( \@to_import ) if @to_import;
Branch: 1 branch
BranchTrueFalseTotal
if @to_import 1 2 3
161 }
162 7 ▶ if ( $state->{nodes6} && ref $state->{nodes6} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"nodes6"} and ref $state->{"nodes6"} eq "ARRAY") 1 6 7
Condition66%2/3
$state->{"nodes6"} and ref $state->{"nodes6"} eq "ARRAY"
!ll&&!rl&&r
601
MC/DC50%1/2
$state->{"nodes6"} and ref $state->{"nodes6"} eq "ARRAY"
$state->{"nodes6"} ref $state->{"nodes6"} eq "ARRAY"
Truth table66%2/3
A and B
A= $state->{"nodes6"}B= ref $state->{"nodes6"} eq "ARRAY"
ABresult
0X0
100
111
163 1 my @to_import;
164 1 for my $n ( $state->{nodes6}->@* ) {
165 0 ▶ next unless ref $n eq 'HASH' && defined $n->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n eq "HASH" and defined $n->{"id"} 0 0 0
Condition0%0/3
ref $n eq "HASH" and defined $n->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $n eq "HASH" and defined $n->{"id"}
ref $n eq "HASH" defined $n->{"id"}
Truth table0%0/3
A and B
A= ref $n eq "HASH"B= defined $n->{"id"}
ABresult
0X0
100
111
166 0 ▶ last if scalar @to_import >= MAX_IMPORT_NODES;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @to_import >= 2000 0 0 0
167 0 ▶ push @to_import, { id => $n->{id}, data => { ip => $n->{ip} // '', port => $n->{port} // 0 } };
Condition0%0/4
$n->{"ip"} // ""
l!l
00
$n->{"port"} // 0
l!l
00
MC/DC0%0/1
$n->{"ip"} // ""
$n->{"ip"}
MC/DC0%0/1
$n->{"port"} // 0
$n->{"port"}
Truth table0%0/2
A
A= $n->{"ip"}
Aresult
11
00
Truth table0%0/2
A
A= $n->{"port"}
Aresult
11
00
168 }
169 1 ▶ $routing_table_v6->import_peers( \@to_import ) if @to_import;
Branch: 1 branch
BranchTrueFalseTotal
if @to_import 0 1 1
170 }
171 7 my $count = 0;
172 7 ▶ if ( $state->{peers} && ref $state->{peers} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"peers"} and ref $state->{"peers"} eq "HASH") 2 5 7
Condition66%2/3
$state->{"peers"} and ref $state->{"peers"} eq "HASH"
!ll&&!rl&&r
502
MC/DC50%1/2
$state->{"peers"} and ref $state->{"peers"} eq "HASH"
$state->{"peers"} ref $state->{"peers"} eq "HASH"
Truth table66%2/3
A and B
A= $state->{"peers"}B= ref $state->{"peers"} eq "HASH"
ABresult
0X0
100
111
173 2 for my $hash ( keys $state->{peers}->%* ) {
174 1 ▶ last if ++$count > MAX_IMPORT_ENTRIES;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count > 5000 0 1 1
175 1 my $entry = $state->{peers}{$hash};
176 1 ▶ $peer_storage->put( $hash, $entry->{value} ) if ref $entry eq 'HASH' && defined $entry->{value};
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry eq "HASH" and defined $entry->{"value"} 0 1 1
Condition33%1/3
ref $entry eq "HASH" and defined $entry->{"value"}
!ll&&!rl&&r
100
MC/DC0%0/2
ref $entry eq "HASH" and defined $entry->{"value"}
ref $entry eq "HASH" defined $entry->{"value"}
Truth table33%1/3
A and B
A= ref $entry eq "HASH"B= defined $entry->{"value"}
ABresult
0X0
100
111
177 }
178 }
179 7 ▶ if ( $state->{data} && ref $state->{data} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"data"} and ref $state->{"data"} eq "HASH") 2 5 7
Condition66%2/3
$state->{"data"} and ref $state->{"data"} eq "HASH"
!ll&&!rl&&r
502
MC/DC50%1/2
$state->{"data"} and ref $state->{"data"} eq "HASH"
$state->{"data"} ref $state->{"data"} eq "HASH"
Truth table66%2/3
A and B
A= $state->{"data"}B= ref $state->{"data"} eq "HASH"
ABresult
0X0
100
111
180 2 for my $hash ( keys $state->{data}->%* ) {
181 1 ▶ last if ++$count > MAX_IMPORT_ENTRIES;
Branch: 1 branch
BranchTrueFalseTotal
if ++$count > 5000 0 1 1
182 1 ▶ $data_storage->put( $hash, $state->{data}{$hash} ) if defined $state->{data}{$hash};
Branch: 1 branch
BranchTrueFalseTotal
if defined $state->{"data"}{$hash} 0 1 1
183 }
184 }
185 }
186 catch ($e) {
187 0 $self->_emit_log( 'warn', "Failed to import state: $e" );
188 }
189 }
190  
191 688 ▶ method _rotate_tokens () {
Subroutine _rotate_tokens: called CC=2 SCAR=11.6
192 688 ▶ if ( time - $last_rotation > 300 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $last_rotation > 300) 0 688 688
193 0 $token_old_secret = $token_secret;
194 0 $token_secret = urandom(32);
195 0 $last_rotation = time;
196 }
197 }
198  
199 1 ▶ method _rotate_node_id () {
Subroutine _rotate_node_id: called CC=4 SCAR=15.0
200 1 ▶ if ( $external_ip && $bep42 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($external_ip and $bep42) 1 0 1
Condition33%1/3
$external_ip and $bep42
!ll&&!rl&&r
001
MC/DC0%0/2
$external_ip and $bep42
$external_ip $bep42
Truth table33%1/3
A and B
A= $external_ipB= $bep42
ABresult
0X0
100
111
201 1 my $new_id = $security->generate_node_id($external_ip);
202 1 ▶ if ( $new_id ne $node_id_bin ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_id ne $node_id_bin) 1 0 1
203 1 $self->_emit_log( 'debug', 'Rotating Node ID for ' . $external_ip );
204 1 $self->set_node_id($new_id);
205 }
206 }
207 1 $last_node_id_rotation = time;
208 }
209  
210 380 ▶ method _generate_token ( $ip, $secret //= undef ) {
Subroutine _generate_token: called CC=3 SCAR=11.1
211 380 ▶ $secret //= $token_secret;
Condition66%2/3
$secret //= $token_secret
l!l&&r!l&&!r
2281520
MC/DC0%0/2
$secret //= $token_secret
$secret $token_secret
Truth table66%2/3
A //= B
A= $secretB= $token_secret
ABresult
1X1
011
000
212 380 return sha1( $ip . $secret );
213 }
214  
215 115 ▶ method _verify_token ( $ip, $token ) {
Subroutine _verify_token: called CC=5 SCAR=16.2
216  
217 # Use constant-time comparison to prevent timing attacks on tokens
218 115 ▶ return 0 unless defined $token && length($token) == 20;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token == 20 1 114 115
Condition66%2/3
defined $token and length $token == 20
!ll&&!rl&&r
01114
MC/DC50%1/2
defined $token and length $token == 20
defined $token length $token == 20
Truth table66%2/3
A and B
A= defined $tokenB= length $token == 20
ABresult
0X0
100
111
219 114 my $t1 = $self->_generate_token( $ip, $token_secret );
220 114 my $t2 = $self->_generate_token( $ip, $token_old_secret );
221 114 my $ok1 = 0;
222 114 my $ok2 = 0;
223 114 for my $i ( 0 .. 19 ) {
224 2280 $ok1 |= ord( substr( $t1, $i, 1 ) ) ^ ord( substr( $token, $i, 1 ) );
225 2280 $ok2 |= ord( substr( $t2, $i, 1 ) ) ^ ord( substr( $token, $i, 1 ) );
226 }
227 114 ▶ return $ok1 == 0 || $ok2 == 0;
Condition66%2/3
$ok1 == 0 || $ok2 == 0
l!l&&r!l&&!r
11301
MC/DC50%1/2
$ok1 == 0 || $ok2 == 0
$ok1 == 0 $ok2 == 0
Truth table66%2/3
A || B
A= $ok1 == 0B= $ok2 == 0
ABresult
1X1
011
000
228 }
229  
230 49 ▶ method bootstrap () {
Subroutine bootstrap: called CC=2 SCAR=6.9
231 49 for my $addr (@_resolved_boot_nodes) {
232 147 $self->_send( { t => 'pn', y => 'q', q => 'ping', a => { id => $node_id_bin } }, $addr );
233 147 $self->_send( { t => 'fn', y => 'q', q => 'find_node', a => { id => $node_id_bin, target => $node_id_bin } }, $addr );
234 }
235 }
236  
237 18 ▶ method ping ( $addr, $port = undef ) {
Subroutine ping: called CC=2 SCAR=6.9
Pod documented
238 18 my $tid = $self->_next_tid();
239 18 $_pending_queries{$tid} = { q => 'ping', time => time };
240 18 $self->_send( { t => $tid, y => 'q', q => 'ping', a => { id => $node_id_bin } }, $addr, $port );
241 }
242  
243 334 ▶ method _next_tid () {
Subroutine _next_tid: called CC=1 SCAR=0.0
244 334 $_tid_counter = ( $_tid_counter + 1 ) % 0xFFFF;
245 334 pack 'n', $_tid_counter;
246 }
247  
248 0 ▶ method find_node_remote ( $target_id, $addr, $port = undef ) {
Subroutine find_node_remote: not called CC=2 SCAR=17.9
Pod documented
249 0 my $tid = $self->_next_tid();
250 0 $_pending_queries{$tid} = { q => 'find_node', target => $target_id, time => time };
251 0 $self->_send( { t => $tid, y => 'q', q => 'find_node', a => { id => $node_id_bin, target => $target_id } }, $addr, $port );
252 }
253  
254 309 ▶ method get_peers ( $info_hash, $addr, $port = undef ) {
Subroutine get_peers: called CC=2 SCAR=6.9
Pod documented
255 309 my $tid = $self->_next_tid();
256 309 $_pending_queries{$tid} = { q => 'get_peers', target => $info_hash, time => time };
257 309 $self->_send( { t => $tid, y => 'q', q => 'get_peers', a => { id => $node_id_bin, info_hash => $info_hash } }, $addr, $port );
258 }
259  
260 0 ▶ method get_remote ( $target, $addr, $port = undef ) {
Subroutine get_remote: not called CC=3 SCAR=24.8
Pod documented
261 0 ▶ return unless $bep44;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep44 0 0 0
262 0 my $tid = $self->_next_tid();
263 0 $_pending_queries{$tid} = { q => 'get', target => $target, time => time };
264 0 $self->_send( { t => $tid, y => 'q', q => 'get', a => { id => $node_id_bin, target => $target } }, $addr, $port );
265 }
266  
267 0 ▶ method put_remote ( $args, $addr, $port = undef ) {
Subroutine put_remote: not called CC=3 SCAR=24.8
Pod documented
268 0 ▶ return unless $bep44;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep44 0 0 0
269 0 my $tid = $self->_next_tid();
270 0 $_pending_queries{$tid} = { q => 'put', time => time };
271  
272 # $args should contain 'v' and optionally 'k', 'sig', 'seq', 'salt', 'cas'
273 0 $self->_send( { t => $tid, y => 'q', q => 'put', a => { id => $node_id_bin, %$args } }, $addr, $port );
274 }
275  
276 0 ▶ method announce_peer ( $info_hash, $token, $announce_port, $addr, $port = undef, $is_seed //= 0 ) {
Subroutine announce_peer: not called CC=5 SCAR=34.0
Pod documented
277 0 my $tid = $self->_next_tid();
278 0 $_pending_queries{$tid} = { q => 'announce_peer', target => $info_hash, time => time };
279 0 ▶ my $msg = {
Branch: 1 branch
BranchTrueFalseTotal
$bep33 && $is_seed ? : 0 0 0
Condition0%0/3
$bep33 and $is_seed
!ll&&!rl&&r
000
MC/DC0%0/2
$bep33 and $is_seed
$bep33 $is_seed
Truth table0%0/3
A and B
A= $bep33B= $is_seed
ABresult
0X0
100
111
280 t => $tid,
281 y => 'q',
282 q => 'announce_peer',
283 a => { id => $node_id_bin, info_hash => $info_hash, port => $announce_port, token => $token, ( $bep33 && $is_seed ? ( seed => 1 ) : () ) }
284 };
285 0 $self->_send( $msg, $addr, $port );
286 }
287  
288 0 ▶ method announce_infohash ( $ih, $port ) {
Subroutine announce_infohash: not called CC=5 SCAR=34.0
Pod documented
289 0 my @learned; # High level: find closest nodes and announce
290 0 ▶ push @learned, $routing_table_v4->find_closest($ih) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 0 0 0
291 0 ▶ push @learned, $routing_table_v6->find_closest($ih) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 0 0 0
Condition0%0/3
$want_v6 and $bep32
!ll&&!rl&&r
000
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table0%0/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
292  
293 # get_peers first to get tokens
294 0 $self->get_peers( $ih, $_->{data}{ip}, $_->{data}{port} ) for @learned;
295 }
296  
297 5 ▶ method scrape_peers_remote ( $info_hash, $addr, $port = undef ) {
Subroutine scrape_peers_remote: called CC=3 SCAR=11.1
Pod documented
298 5 ▶ return unless $bep33;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep33 0 5 5
299 5 my $tid = $self->_next_tid();
300 5 $_pending_queries{$tid} = { q => 'scrape_peers', target => $info_hash, time => time };
301 5 $self->_send( { t => $tid, y => 'q', q => 'scrape_peers', a => { id => $node_id_bin, info_hash => $info_hash } }, $addr, $port );
302 }
303  
304 82 ▶ method find_peers ($info_hash) {
Subroutine find_peers: called CC=5 SCAR=18.2
Pod documented
305 82 my @learned;
306 82 ▶ push @learned, $routing_table_v4->find_closest($info_hash) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 82 0 82
307 82 ▶ push @learned, $routing_table_v6->find_closest($info_hash) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 82 0 82
Condition33%1/3
$want_v6 and $bep32
!ll&&!rl&&r
0082
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table33%1/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
308 82 $self->get_peers( $info_hash, $_->{data}{ip}, $_->{data}{port} ) for @learned;
309 }
310  
311 46 ▶ method scrape ($info_hash) {
Subroutine scrape: called CC=6 SCAR=20.3
Pod documented
312 46 ▶ return unless $bep33;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep33 0 46 46
313 46 my @learned;
314 46 ▶ push @learned, $routing_table_v4->find_closest($info_hash) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 46 0 46
315 46 ▶ push @learned, $routing_table_v6->find_closest($info_hash) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 46 0 46
Condition33%1/3
$want_v6 and $bep32
!ll&&!rl&&r
0046
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table33%1/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
316 46 $self->scrape_peers_remote( $info_hash, $_->{data}{ip}, $_->{data}{port} ) for @learned;
317 }
318  
319 0 ▶ method sample ($target) {
Subroutine sample: not called CC=6 SCAR=37.4
Pod documented
320 0 ▶ return unless $bep51;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep51 0 0 0
321 0 my @learned;
322 0 ▶ push @learned, $routing_table_v4->find_closest($target) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 0 0 0
323 0 ▶ push @learned, $routing_table_v6->find_closest($target) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 0 0 0
Condition0%0/3
$want_v6 and $bep32
!ll&&!rl&&r
000
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table0%0/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
324 0 $self->sample_infohashes_remote( $target, $_->{data}{ip}, $_->{data}{port} ) for @learned;
325 }
326  
327 2 ▶ method sample_infohashes_remote ( $target, $addr, $port = undef ) {
Subroutine sample_infohashes_remote: called CC=3 SCAR=11.1
Pod documented
328 2 ▶ return unless $bep51;
Branch: 1 branch
BranchTrueFalseTotal
unless $bep51 0 2 2
329 2 my $tid = $self->_next_tid();
330 2 $_pending_queries{$tid} = { q => 'sample_infohashes', target => $target, time => time };
331 2 $self->_send( { t => $tid, y => 'q', q => 'sample_infohashes', a => { id => $node_id_bin, target => $target } }, $addr, $port );
332 }
333  
334 688 ▶ method tick ( $timeout //= 0 ) {
Subroutine tick: called CC=4 SCAR=13.9
Pod documented
335 688 $self->_rotate_tokens();
336 688 ▶ $self->_rotate_node_id() if time - $last_node_id_rotation >= $node_id_rotation_interval;
Branch: 1 branch
BranchTrueFalseTotal
if time - $last_node_id_rotation >= $node_id_rotation_interval 1 687 688
337 688 ▶ return $self->handle_incoming() if $select->can_read(0); # Non-blocking: _run_one_tick already reads this socket
Branch: 1 branch
BranchTrueFalseTotal
if $select->can_read(0) 69 619 688
338 619 return ( [], [], undef );
339 }
340  
341 321 ▶ method handle_incoming ( $data //= undef, $sender //= undef ) {
Subroutine handle_incoming: called CC=22 SCAR=38.7
Pod documented
342 321 ▶ $sender = $socket->recv( $data, 4096 ) unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 69 252 321
343 321 ▶ return ( [], [], undef ) unless defined $data && length $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 321 321
Condition33%1/3
defined $data and length $data
!ll&&!rl&&r
00321
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table33%1/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
344 321 my $msg;
345 321 try { $msg = bdecode($data) }
346 0 catch ($e) { return ( [], [], undef ) }
347 321 ▶ return ( [], [], undef ) if ref($msg) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $msg ne "HASH" 1 320 321
348 320 my ( $port, $ip ) = $self->_unpack_address($sender);
349 320 ▶ return ( [], [], undef ) unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 2 318 320
350  
351 318 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 1 317 318
352 1 ▶ my $qname = $msg->{q} // '';
Condition50%1/2
$msg->{"q"} // ""
l!l
10
MC/DC0%0/1
$msg->{"q"} // ""
$msg->{"q"}
Truth table50%1/2
A
A= $msg->{"q"}
Aresult
11
00
353 1 $qname =~ s/[^\x20-\x7E]/./g;
354 1 ▶ my $type = ( $msg->{y} // '' ) eq 'q' ? "QUERY ($qname)" : 'RESPONSE';
Branch: 1 branch
BranchTrueFalseTotal
($msg->{'y'} // '') eq 'q' ? : 1 0 1
Condition50%1/2
$msg->{"y"} // ""
l!l
10
MC/DC0%0/1
$msg->{"y"} // ""
$msg->{"y"}
Truth table50%1/2
A
A= $msg->{"y"}
Aresult
11
00
355 1 $self->_emit_log( 'debug', "RECV $type from $ip:$port" );
356 }
357 318 ▶ if ( ( $msg->{y} // '' ) eq 'q' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($msg->{"y"} // "") eq "q") 239 79 318
Condition50%1/2
$msg->{"y"} // ""
l!l
3180
MC/DC0%0/1
$msg->{"y"} // ""
$msg->{"y"}
Truth table50%1/2
A
A= $msg->{"y"}
Aresult
11
00
358 239 my $node;
359 239 try { $node = $self->_handle_query( $msg, $sender, $ip, $port ); }
360 0 catch ($e) { $self->_emit_log( 'warning', "Query handler error from $ip:$port: $e" ); }
361 239 ▶ return ( $node ? [$node] : [], [], undef ); # Return flat format
Branch: 1 branch
BranchTrueFalseTotal
$node ? : 128 111 239
362 }
363 79 ▶ if ( ( $msg->{y} // '' ) eq 'e' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($msg->{"y"} // "") eq "e") 0 79 79
Condition50%1/2
$msg->{"y"} // ""
l!l
790
MC/DC0%0/1
$msg->{"y"} // ""
$msg->{"y"}
Truth table50%1/2
A
A= $msg->{"y"}
Aresult
11
00
364 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
365 0 ▶ my $code = $msg->{e}->[0] // 'unknown';
Condition0%0/2
$msg->{"e"}[0] // "unknown"
l!l
00
MC/DC0%0/1
$msg->{"e"}[0] // "unknown"
$msg->{"e"}[0]
Truth table0%0/2
A
A= $msg->{"e"}[0]
Aresult
11
00
366 0 ▶ my $text = $msg->{e}->[1] // 'no message';
Condition0%0/2
$msg->{"e"}[1] // "no message"
l!l
00
MC/DC0%0/1
$msg->{"e"}[1] // "no message"
$msg->{"e"}[1]
Truth table0%0/2
A
A= $msg->{"e"}[1]
Aresult
11
00
367 0 $text = substr( "$text", 0, 200 ); # Truncate
368 0 $text =~ s/[^\x20-\x7E]/./g; # Sanitize non-printable
369 0 $self->_emit_log( 'debug', "RECV ERROR $code: $text from $ip:$port" );
370 }
371 0 return ( [], [], undef );
372 }
373 79 ▶ if ( ( $msg->{y} // '' ) eq 'r' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($msg->{"y"} // "") eq "r") 79 0 79
Condition50%1/2
$msg->{"y"} // ""
l!l
790
MC/DC0%0/1
$msg->{"y"} // ""
$msg->{"y"}
Truth table50%1/2
A
A= $msg->{"y"}
Aresult
11
00
374 79 my @result;
375 79 try { @result = $self->_handle_response( $msg, $sender, $ip, $port ); }
376 0 catch ($e) { $self->_emit_log( 'warning', "Response handler error from $ip:$port: $e" ); return ( [], [], undef ) }
377 79 return @result;
378 }
379 0 return ( [], [], undef );
380 }
381  
382 320 ▶ method _unpack_address ($sockaddr) {
Subroutine _unpack_address: called CC=4 SCAR=16.3
383 320 ▶ return () unless defined $sockaddr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sockaddr 2 318 320
384 318 my $family;
385 318 try { $family = sockaddr_family($sockaddr) }
386 0 catch ($e) { return () }
387 318 ▶ if ( $family == AF_INET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($family == 2) { } 318 0 318
elsif ($family == 10) { } 0 0 0
388 318 my ( $port, $ip_bin ) = unpack_sockaddr_in($sockaddr);
389 318 return ( $port, inet_ntoa($ip_bin) );
390 }
391 elsif ( $family == AF_INET6 ) {
392 0 my ( $port, $ip_bin, $scope, $flow ) = unpack_sockaddr_in6($sockaddr);
393 0 return ( $port, inet_ntop( AF_INET6, $ip_bin ) );
394 }
395 0 return ();
396 }
397  
398 285 ▶ method _handle_query ( $msg, $sender, $ip, $port ) {
Subroutine _handle_query: called CC=148 SCAR=71.3
399 285 ▶ return if $_blacklist{$ip};
Branch: 1 branch
BranchTrueFalseTotal
if $_blacklist{$ip} 1 284 285
400  
401 # Cap blacklist size to prevent memory exhaustion from spoofed IPs
402 284 ▶ if ( keys %_blacklist > 10000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %_blacklist > 10000) 0 284 284
403 0 ▶ my @oldest = ( sort { ( $_blacklist{$a} // 0 ) <=> ( $_blacklist{$b} // 0 ) } keys %_blacklist )[ 0 .. 999 ];
Condition0%0/4
$_blacklist{$a} // 0
l!l
00
$_blacklist{$b} // 0
l!l
00
MC/DC0%0/1
$_blacklist{$a} // 0
$_blacklist{$a}
MC/DC0%0/1
$_blacklist{$b} // 0
$_blacklist{$b}
Truth table0%0/2
A
A= $_blacklist{$a}
Aresult
11
00
Truth table0%0/2
A
A= $_blacklist{$b}
Aresult
11
00
404 0 delete @_blacklist{@oldest};
405 }
406  
407 # Max 20 queries per IP in a given 10s window
408 284 my $now = time;
409 284 ▶ $_query_rate{$ip} //= [];
Condition100%2/2
$_query_rate{$ip} //= []
l!l
43241
MC/DC100%1/1
$_query_rate{$ip} //= []
$_query_rate{$ip}
Truth table100%2/2
A
A= $_query_rate{$ip}
Aresult
11
00
410 284 @{ $_query_rate{$ip} } = grep { $_ > $now - 10 } @{ $_query_rate{$ip} };
411 284 ▶ if ( scalar @{ $_query_rate{$ip} } >= 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$_query_rate{$ip};} >= 20) 1 283 284
412 1 $_blacklist{$ip} = $now;
413 1 ▶ $self->_emit_log( 'debug', "Rate-limited DHT query from $ip (exceeded 20/10s)" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
414 1 return;
415 }
416 283 push @{ $_query_rate{$ip} }, $now;
417  
418 # Cap _query_rate to prevent memory exhaustion from spoofed IPs
419 283 ▶ if ( keys %_query_rate > 10000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %_query_rate > 10000) 0 283 283
420 0 my @stale = grep { $_query_rate{$_}[-1] < $now - 60 } keys %_query_rate;
421 0 ▶ if (@stale) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stale) 0 0 0
422 0 ▶ my $delete = @stale > 1000 ? 1000 : scalar @stale;
Branch: 1 branch
BranchTrueFalseTotal
@stale > 1000 ? : 0 0 0
423 0 delete @_query_rate{ @stale[ 0 .. $delete - 1 ] };
424 }
425 }
426 283 ▶ my $q = $msg->{q} // return;
Condition50%1/2
$msg->{"q"} // (return)
l!l
2830
MC/DC0%0/1
$msg->{"q"} // (return)
$msg->{"q"}
Truth table50%1/2
A
A= $msg->{"q"}
Aresult
11
00
427 283 ▶ my $a = $msg->{a} // return;
Condition50%1/2
$msg->{"a"} // (return)
l!l
2830
MC/DC0%0/1
$msg->{"a"} // (return)
$msg->{"a"}
Truth table50%1/2
A
A= $msg->{"a"}
Aresult
11
00
428 283 ▶ my $id = $a->{id} // return;
Condition50%1/2
$a->{"id"} // (return)
l!l
2830
MC/DC0%0/1
$a->{"id"} // (return)
$a->{"id"}
Truth table50%1/2
A
A= $a->{"id"}
Aresult
11
00
429 283 ▶ return unless defined $msg->{t} && !ref $msg->{t} && length( $msg->{t} ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg->{"t"} and not ref $msg->{"t"} and length $msg->{"t"} > 0 0 283 283
Condition33%2/6
defined $msg->{"t"} and not ref $msg->{"t"} and length $msg->{"t"} > 0
!ll&&!rl&&r
00283
defined $msg->{"t"} and not ref $msg->{"t"}
!ll&&!rl&&r
00283
MC/DC0%0/3
defined $msg->{"t"} and not ref $msg->{"t"} and length $msg->{"t"} > 0
defined $msg->{"t"} not ref $msg->{"t"} length $msg->{"t"} > 0
Truth table25%1/4
A and not(B) and C
A= defined $msg->{"t"}B= not ref $msg->{"t"}C= length $msg->{"t"} > 0
ABCresult
0XX0
10X0
1100
1111
430  
431 # BEP 42: Reject nodes with invalid IDs
432 283 ▶ return if $bep42 && !$security->validate_node_id( $id, $ip );
Branch: 1 branch
BranchTrueFalseTotal
if $bep42 and not $security->validate_node_id($id, $ip) 112 171 283
Condition100%3/3
$bep42 and not $security->validate_node_id($id, $ip)
!ll&&!rl&&r
12348112
MC/DC100%2/2
$bep42 and not $security->validate_node_id($id, $ip)
$bep42 not $security->validate_node_id($id, $ip)
Truth table100%3/3
A and not(B)
A= $bep42B= not $security->validate_node_id($id, $ip)
ABresult
0X0
100
111
433 171 ▶ my $table = ( $ip =~ /:/ ) ? $routing_table_v6 : $routing_table_v4;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /:/u ? : 0 171 171
434 171 ▶ unless ( $a->{ro} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($a->{"ro"}) 171 0 171
435 171 my $stale = $table->add_peer( $id, { ip => $ip, port => $port } );
436 171 ▶ $self->ping( $stale->{data}{ip}, $stale->{data}{port} ) if $stale;
Branch: 1 branch
BranchTrueFalseTotal
if $stale 6 165 171
437 }
438 171 my $res = { t => $msg->{t}, y => 'r', r => { id => $node_id_bin } };
439 171 ▶ $res->{v} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 171 171
440 171 ▶ if ( my $ip_bin = ( $ip =~ /:/ ) ? inet_pton( AF_INET6, $ip ) : inet_aton($ip) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ip_bin = $ip =~ /:/u ? inet_pton(10, $ip) : inet_aton($ip)) 171 0 171
$ip =~ /:/u ? : 0 171 171
441 171 $res->{ip} = $ip_bin;
442 }
443 171 ▶ my $w = $a->{want} // [];
Condition100%2/2
$a->{"want"} // []
l!l
2169
MC/DC100%1/1
$a->{"want"} // []
$a->{"want"}
Truth table100%2/2
A
A= $a->{"want"}
Aresult
11
00
444 171 ▶ $w = [$w] if defined $w && !ref $w;
Branch: 1 branch
BranchTrueFalseTotal
if defined $w and not ref $w 0 171 171
Condition33%1/3
defined $w and not ref $w
!ll&&!rl&&r
01710
MC/DC0%0/2
defined $w and not ref $w
defined $w not ref $w
Truth table33%1/3
A and not(B)
A= defined $wB= not ref $w
ABresult
0X0
100
111
445 171 ▶ $w = [] unless ref $w eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $w eq "ARRAY" 0 171 171
446 171 my %want = map { $_ => 1 } @$w;
447 171 ▶ if ( !@$w ) { # Default: same family as query
Branch: 1 branch
BranchTrueFalseTotal
unless (@$w) 169 2 171
448 0 ▶ if ( $ip =~ /:/ ) { $want{n6} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:/u) { } 0 169 169
449 169 else { $want{n4} = 1 }
450 }
451 171 ▶ if ( $q eq 'ping' ) { }
Branch: 8 branches
BranchTrueFalseTotal
if ($q eq 'ping') { } 29 142 171
elsif ($q eq 'find_node') { } 2 140 142
elsif ($q eq 'get_peers') { } 21 119 140
elsif ($q eq 'announce_peer') { } 103 16 119
elsif ($q eq 'scrape_peers') { } 1 15 16
elsif ($q eq 'get') { } 6 9 15
elsif ($q eq 'put') { } 7 2 9
elsif ($q eq 'sample_infohashes') { } 2 0 2
452 elsif ( $q eq 'find_node' ) {
453 2 my @closest;
454 2 ▶ push @closest, $routing_table_v4->find_closest( $a->{target} ) if $want_v4 && $want{n4};
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 and $want{"n4"} 2 0 2
Condition33%1/3
$want_v4 and $want{"n4"}
!ll&&!rl&&r
002
MC/DC0%0/2
$want_v4 and $want{"n4"}
$want_v4 $want{"n4"}
Truth table33%1/3
A and B
A= $want_v4B= $want{"n4"}
ABresult
0X0
100
111
455 2 ▶ push @closest, $routing_table_v6->find_closest( $a->{target} ) if $want_v6 && $bep32 && $want{n6};
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 and $want{"n6"} 1 1 2
Condition66%4/6
$want_v6 and $bep32 and $want{"n6"}
!ll&&!rl&&r
101
$want_v6 and $bep32
!ll&&!rl&&r
011
MC/DC33%1/3
$want_v6 and $bep32 and $want{"n6"}
$want_v6 $bep32 $want{"n6"}
Truth table50%2/4
A and B and C
A= $want_v6B= $bep32C= $want{"n6"}
ABCresult
0XX0
10X0
1100
1111
456 2 my ( $v4, $v6 ) = $self->_pack_nodes( \@closest );
457 2 ▶ $res->{r}{nodes} = $v4 if $v4 && $want_v4 && $want{n4};
Branch: 1 branch
BranchTrueFalseTotal
if $v4 and $want_v4 and $want{"n4"} 2 0 2
Condition33%2/6
$v4 and $want_v4 and $want{"n4"}
!ll&&!rl&&r
002
$v4 and $want_v4
!ll&&!rl&&r
002
MC/DC0%0/3
$v4 and $want_v4 and $want{"n4"}
$v4 $want_v4 $want{"n4"}
Truth table25%1/4
A and B and C
A= $v4B= $want_v4C= $want{"n4"}
ABCresult
0XX0
10X0
1100
1111
458 2 ▶ $res->{r}{nodes6} = $v6 if $v6 && $want_v6 && $bep32 && $want{n6};
Branch: 1 branch
BranchTrueFalseTotal
if $v6 and $want_v6 and $bep32 and $want{"n6"} 1 1 2
Condition66%6/9
$v6 and $want_v6 and $bep32 and $want{"n6"}
!ll&&!rl&&r
101
$v6 and $want_v6 and $bep32
!ll&&!rl&&r
101
$v6 and $want_v6
!ll&&!rl&&r
101
MC/DC25%1/4
$v6 and $want_v6 and $bep32 and $want{"n6"}
$v6 $want_v6 $bep32 $want{"n6"}
Truth table40%2/5
A and B and C and D
A= $v6B= $want_v6C= $bep32D= $want{"n6"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
459 }
460 elsif ( $q eq 'get_peers' ) {
461 21 my $info_hash = $a->{info_hash};
462  
463 # info_hash must be 20 or 32 bytes
464 21 ▶ return if !defined $info_hash || ( length($info_hash) != 20 && length($info_hash) != 32 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $info_hash or length $info_hash != 20 and length $info_hash != 32 0 21 21
Condition33%2/6
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
l!l&&r!l&&!r
0021
length $info_hash != 20 and length $info_hash != 32
!ll&&!rl&&r
2100
MC/DC0%0/3
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
not defined $info_hash length $info_hash != 20 length $info_hash != 32
Truth table25%1/4
not(A) or B and C
A= not defined $info_hashB= length $info_hash != 20C= length $info_hash != 32
ABCresult
1XX1
0111
00X0
0100
465 21 $res->{r}{token} = $self->_generate_token($ip);
466 21 my $peers_obj = $peer_storage->get($info_hash);
467 21 ▶ if ( $peers_obj && @{ $peers_obj->value } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peers_obj and @{$peers_obj->value;}) { } 1 20 21
Condition66%2/3
$peers_obj and @{$peers_obj->value;}
!ll&&!rl&&r
2001
MC/DC50%1/2
$peers_obj and @{$peers_obj->value;}
$peers_obj @{$peers_obj->value;}
Truth table66%2/3
A and B
A= $peers_objB= @{$peers_obj->value;}
ABresult
0X0
100
111
468 1 ▶ my @filtered = grep { ( $_->{ip} =~ /:/ ) ? $want_v6 : $want_v4 } @{ $peers_obj->value };
Branch: 1 branch
BranchTrueFalseTotal
$_->{'ip'} =~ /:/u ? : 0 1 1
469 1 $res->{r}{values} = $self->_pack_peers_raw( \@filtered );
470 }
471 else {
472 20 my @closest;
473 20 ▶ push @closest, $routing_table_v4->find_closest($info_hash) if $want_v4 && $want{n4};
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 and $want{"n4"} 20 0 20
Condition33%1/3
$want_v4 and $want{"n4"}
!ll&&!rl&&r
0020
MC/DC0%0/2
$want_v4 and $want{"n4"}
$want_v4 $want{"n4"}
Truth table33%1/3
A and B
A= $want_v4B= $want{"n4"}
ABresult
0X0
100
111
474 20 ▶ push @closest, $routing_table_v6->find_closest($info_hash) if $want_v6 && $bep32 && $want{n6};
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 and $want{"n6"} 0 20 20
Condition33%2/6
$want_v6 and $bep32 and $want{"n6"}
!ll&&!rl&&r
0200
$want_v6 and $bep32
!ll&&!rl&&r
0020
MC/DC0%0/3
$want_v6 and $bep32 and $want{"n6"}
$want_v6 $bep32 $want{"n6"}
Truth table25%1/4
A and B and C
A= $want_v6B= $bep32C= $want{"n6"}
ABCresult
0XX0
10X0
1100
1111
475 20 my ( $v4, $v6 ) = $self->_pack_nodes( \@closest );
476 20 ▶ $res->{r}{nodes} = $v4 if $v4 && $want_v4 && $want{n4};
Branch: 1 branch
BranchTrueFalseTotal
if $v4 and $want_v4 and $want{"n4"} 20 0 20
Condition33%2/6
$v4 and $want_v4 and $want{"n4"}
!ll&&!rl&&r
0020
$v4 and $want_v4
!ll&&!rl&&r
0020
MC/DC0%0/3
$v4 and $want_v4 and $want{"n4"}
$v4 $want_v4 $want{"n4"}
Truth table25%1/4
A and B and C
A= $v4B= $want_v4C= $want{"n4"}
ABCresult
0XX0
10X0
1100
1111
477 20 ▶ $res->{r}{nodes6} = $v6 if $v6 && $want_v6 && $bep32 && $want{n6};
Branch: 1 branch
BranchTrueFalseTotal
if $v6 and $want_v6 and $bep32 and $want{"n6"} 0 20 20
Condition33%3/9
$v6 and $want_v6 and $bep32 and $want{"n6"}
!ll&&!rl&&r
2000
$v6 and $want_v6 and $bep32
!ll&&!rl&&r
2000
$v6 and $want_v6
!ll&&!rl&&r
2000
MC/DC0%0/4
$v6 and $want_v6 and $bep32 and $want{"n6"}
$v6 $want_v6 $bep32 $want{"n6"}
Truth table20%1/5
A and B and C and D
A= $v6B= $want_v6C= $bep32D= $want{"n6"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
478 }
479 }
480 elsif ( $q eq 'announce_peer' ) {
481 103 my $info_hash = $a->{info_hash};
482  
483 # Validate info_hash length
484 103 ▶ return if !defined $info_hash || ( length($info_hash) != 20 && length($info_hash) != 32 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $info_hash or length $info_hash != 20 and length $info_hash != 32 0 103 103
Condition33%2/6
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
l!l&&r!l&&!r
00103
length $info_hash != 20 and length $info_hash != 32
!ll&&!rl&&r
10300
MC/DC0%0/3
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
not defined $info_hash length $info_hash != 20 length $info_hash != 32
Truth table25%1/4
not(A) or B and C
A= not defined $info_hashB= length $info_hash != 20C= length $info_hash != 32
ABCresult
1XX1
0111
00X0
0100
485 103 ▶ if ( $self->_verify_token( $ip, $a->{token} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_verify_token($ip, $a->{"token"})) 103 0 103
486 103 my $peers_obj = $peer_storage->get($info_hash);
487 103 ▶ my @peers = $peers_obj ? @{ $peers_obj->value } : ();
Branch: 1 branch
BranchTrueFalseTotal
$peers_obj ? : 100 3 103
488 103 ▶ my $peer_port = $a->{implied_port} ? $port : $a->{port};
Branch: 1 branch
BranchTrueFalseTotal
$a->{'implied_port'} ? : 0 103 103
489  
490 # Validate peer port is defined and within range
491 103 ▶ return if ( !defined $peer_port || $peer_port < 1 || $peer_port > 65535 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $peer_port or $peer_port < 1 or $peer_port > 65535 0 103 103
Condition33%2/6
not defined $peer_port or $peer_port < 1 or $peer_port > 65535
l!l&&r!l&&!r
00103
not defined $peer_port or $peer_port < 1
l!l&&r!l&&!r
00103
MC/DC0%0/3
not defined $peer_port or $peer_port < 1 or $peer_port > 65535
not defined $peer_port $peer_port < 1 $peer_port > 65535
Truth table25%1/4
not(A) or B or C
A= not defined $peer_portB= $peer_port < 1C= $peer_port > 65535
ABCresult
1XX1
01X1
0011
0000
492 103 ▶ my $new_peer = { ip => $ip, port => $peer_port, ( $bep33 && defined $a->{seed} ? ( seed => $a->{seed} ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
$bep33 && defined $a->{'seed'} ? : 1 102 103
Condition66%2/3
$bep33 and defined $a->{"seed"}
!ll&&!rl&&r
01021
MC/DC50%1/2
$bep33 and defined $a->{"seed"}
$bep33 defined $a->{"seed"}
Truth table66%2/3
A and B
A= $bep33B= defined $a->{"seed"}
ABresult
0X0
100
111
493 103 @peers = grep { $_->{ip} ne $ip } @peers;
494 103 push @peers, $new_peer;
495 103 ▶ splice( @peers, 0, @peers - 50 ) if @peers > 50; # Cap peers per info_hash
Branch: 1 branch
BranchTrueFalseTotal
if @peers > 50 50 53 103
496 103 $peer_storage->put( $info_hash, \@peers );
497  
498 # Cap total storage entries to prevent memory exhaustion
499 103 my %peers_entries = $peer_storage->entries;
500 103 ▶ if ( keys %peers_entries > MAX_STORAGE_ENTRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %peers_entries > 10000) 0 103 103
501 0 my @oldest = ( sort { $peers_entries{$a} cmp $peers_entries{$b} } keys %peers_entries )[ 0 .. 999 ];
502 0 $peer_storage->delete($_) for @oldest;
503 }
504 }
505 }
506 elsif ( $q eq 'scrape_peers' ) {
507 1 ▶ if ($bep33) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep33) { } 1 0 1
508 1 my $info_hash = $a->{info_hash};
509  
510 # Validate info_hash length for scrape_peers
511 1 ▶ return if !defined $info_hash || ( length($info_hash) != 20 && length($info_hash) != 32 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $info_hash or length $info_hash != 20 and length $info_hash != 32 0 1 1
Condition33%2/6
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
l!l&&r!l&&!r
001
length $info_hash != 20 and length $info_hash != 32
!ll&&!rl&&r
100
MC/DC0%0/3
not defined $info_hash or length $info_hash != 20 and length $info_hash != 32
not defined $info_hash length $info_hash != 20 length $info_hash != 32
Truth table25%1/4
not(A) or B and C
A= not defined $info_hashB= length $info_hash != 20C= length $info_hash != 32
ABCresult
1XX1
0111
00X0
0100
512 1 my $peers_obj = $peer_storage->get($info_hash);
513 1 ▶ my $peers = $peers_obj ? $peers_obj->value : [];
Branch: 1 branch
BranchTrueFalseTotal
$peers_obj ? : 1 0 1
514 1 my $seeders = grep { $_->{seed} } @$peers;
515 1 my $leechers = @$peers - $seeders;
516 1 $res->{r}{sn} = $seeders;
517 1 $res->{r}{ln} = $leechers;
518 }
519 else {
520 # If BEP 33 is disabled, we might want to return an error or just ignore.
521 # Standard is to just return 'id'.
522 }
523 }
524 elsif ( $q eq 'get' ) {
525 6 ▶ if ($bep44) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep44) 6 0 6
526 6 my $target = $a->{target};
527  
528 # Validate target length (must be 20 or 32 bytes)
529 6 ▶ return if !defined $target || ( length($target) != 20 && length($target) != 32 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $target or length $target != 20 and length $target != 32 0 6 6
Condition33%2/6
not defined $target or length $target != 20 and length $target != 32
l!l&&r!l&&!r
006
length $target != 20 and length $target != 32
!ll&&!rl&&r
600
MC/DC0%0/3
not defined $target or length $target != 20 and length $target != 32
not defined $target length $target != 20 length $target != 32
Truth table25%1/4
not(A) or B and C
A= not defined $targetB= length $target != 20C= length $target != 32
ABCresult
1XX1
0111
00X0
0100
530 6 my $data_obj = $data_storage->get($target);
531 6 ▶ if ($data_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_obj) { } 6 0 6
532 6 $res->{r} = { %{ $res->{r} }, %{ $data_obj->value } };
533 }
534 else {
535 0 my @closest;
536 0 ▶ push @closest, $routing_table_v4->find_closest($target) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 0 0 0
537 0 ▶ push @closest, $routing_table_v6->find_closest($target) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 0 0 0
Condition0%0/3
$want_v6 and $bep32
!ll&&!rl&&r
000
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table0%0/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
538 0 my ( $v4, $v6 ) = $self->_pack_nodes( \@closest );
539 0 ▶ $res->{r}{nodes} = $v4 if $v4 && $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $v4 and $want_v4 0 0 0
Condition0%0/3
$v4 and $want_v4
!ll&&!rl&&r
000
MC/DC0%0/2
$v4 and $want_v4
$v4 $want_v4
Truth table0%0/3
A and B
A= $v4B= $want_v4
ABresult
0X0
100
111
540 0 ▶ $res->{r}{nodes6} = $v6 if $v6 && $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $v6 and $want_v6 and $bep32 0 0 0
Condition0%0/6
$v6 and $want_v6 and $bep32
!ll&&!rl&&r
000
$v6 and $want_v6
!ll&&!rl&&r
000
MC/DC0%0/3
$v6 and $want_v6 and $bep32
$v6 $want_v6 $bep32
Truth table0%0/4
A and B and C
A= $v6B= $want_v6C= $bep32
ABCresult
0XX0
10X0
1100
1111
541 }
542 6 $res->{r}{token} = $self->_generate_token($ip);
543 }
544 }
545 elsif ( $q eq 'put' ) {
546 7 ▶ if ( $bep44 && $self->_verify_token( $ip, $a->{token} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep44 and $self->_verify_token($ip, $a->{"token"})) 7 0 7
Condition33%1/3
$bep44 and $self->_verify_token($ip, $a->{"token"})
!ll&&!rl&&r
007
MC/DC0%0/2
$bep44 and $self->_verify_token($ip, $a->{"token"})
$bep44 $self->_verify_token($ip, $a->{"token"})
Truth table33%1/3
A and B
A= $bep44B= $self->_verify_token($ip, $a->{"token"})
ABresult
0X0
100
111
547 7 my $v = $a->{v};
548  
549 # BEP44 mutable values must be <= 1000 bytes
550 7 ▶ return unless defined $v && !ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v 0 7 7
Condition33%1/3
defined $v and not ref $v
!ll&&!rl&&r
007
MC/DC0%0/2
defined $v and not ref $v
defined $v not ref $v
Truth table33%1/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
551 7 ▶ if ( defined $a->{k} && length($v) > 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a->{"k"} and length $v > 1000) 0 7 7
Condition66%2/3
defined $a->{"k"} and length $v > 1000
!ll&&!rl&&r
160
MC/DC0%0/2
defined $a->{"k"} and length $v > 1000
defined $a->{"k"} length $v > 1000
Truth table66%2/3
A and B
A= defined $a->{"k"}B= length $v > 1000
ABresult
0X0
100
111
552 0 $_blacklist{$ip} = time;
553 0 return;
554 }
555 7 my $target = sha1($v);
556 7 my $is_mutable = defined $a->{k};
557 7 ▶ if ($is_mutable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_mutable) { } 6 1 7
558  
559 # Validate key length and seq upper bound
560 6 ▶ my $seq_val = $a->{seq} // 0;
Condition50%1/2
$a->{"seq"} // 0
l!l
60
MC/DC0%0/1
$a->{"seq"} // 0
$a->{"seq"}
Truth table50%1/2
A
A= $a->{"seq"}
Aresult
11
00
561 6 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64 or not $a->{"seq"} =~ /^\d+$/u or $seq_val > '9007199254740992' or defined $a->{"cas"} and not $a->{"cas"} =~ /^\d+$/u) 1 5 6
Condition54%13/24
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64 or not $a->{"seq"} =~ /^\d+$/u or $seq_val > '9007199254740992' or defined $a->{"cas"} and not $a->{"cas"} =~ /^\d+$/u
l!l&&r!l&&!r
105
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64 or not $a->{"seq"} =~ /^\d+$/u or $seq_val > '9007199254740992'
l!l&&r!l&&!r
105
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64 or not $a->{"seq"} =~ /^\d+$/u
l!l&&r!l&&!r
105
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64
l!l&&r!l&&!r
015
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32
l!l&&r!l&&!r
006
defined $a->{'sig'} && defined $a->{'k'} && defined $a->{'seq'}
!ll&&!rl&&r
006
defined $a->{"sig"} and defined $a->{"k"}
!ll&&!rl&&r
006
defined $a->{"cas"} and not $a->{"cas"} =~ /^\d+$/u
!ll&&!rl&&r
320
MC/DC11%1/9
not defined $a->{"sig"} && defined $a->{"k"} && defined $a->{"seq"} or length $a->{"k"} != 32 or length $a->{"sig"} != 64 or not $a->{"seq"} =~ /^\d+$/u or $seq_val > '9007199254740992' or defined $a->{"cas"} and not $a->{"cas"} =~ /^\d+$/u
defined $a->{"sig"} defined $a->{"k"} defined $a->{'seq'} length $a->{"k"} != 32 length $a->{"sig"} != 64 not $a->{"seq"} =~ /^\d+$/u $seq_val > '9007199254740992' defined $a->{"cas"} not $a->{"cas"} =~ /^\d+$/u
Truth table30%3/10
not(A and B && C) or D or E or not(F) or G or H and not(I)
A= defined $a->{"sig"}B= defined $a->{"k"}C= defined $a->{'seq'}D= length $a->{"k"} != 32E= length $a->{"sig"} != 64F= not $a->{"seq"} =~ /^\d+$/uG= $seq_val > '9007199254740992'H= defined $a->{"cas"}I= not $a->{"cas"} =~ /^\d+$/u
ABCDEFGHIresult
0XXXXXXXX1
10XXXXXXX1
110XXXXXX1
1111XXXXX1
11101XXXX1
111001XXX1
1110001XX1
1110000111
11100000X0
1110000100
562 !defined $a->{sig} ||
563 !defined $a->{k} ||
564 !defined $a->{seq} ||
565 length( $a->{k} ) != 32 ||
566 length( $a->{sig} ) != 64 ||
567 $a->{seq} !~ /^\d+$/ ||
568 $seq_val > ( 1 << 53 ) || # L4: Reject seq > 2^53 (lossy float precision)
569 ( defined $a->{cas} && $a->{cas} !~ /^\d+$/ )
570 ) {
571 1 $_blacklist{$ip} = time;
572 1 return;
573 }
574 5 ▶ $target = sha1( $a->{k} . ( $a->{salt} // '' ) );
Condition50%1/2
$a->{"salt"} // ""
l!l
50
MC/DC0%0/1
$a->{"salt"} // ""
$a->{"salt"}
Truth table50%1/2
A
A= $a->{"salt"}
Aresult
11
00
575  
576 # Validate signature
577 5 my $to_sign = '';
578 5 ▶ $to_sign .= '3:cas' . bencode( $a->{cas} ) if defined $a->{cas};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->{"cas"} 2 3 5
579 5 ▶ $to_sign .= '4:salt' . bencode( $a->{salt} ) if defined $a->{salt} && length $a->{salt};
Branch: 1 branch
BranchTrueFalseTotal
if defined $a->{"salt"} and length $a->{"salt"} 5 0 5
Condition33%1/3
defined $a->{"salt"} and length $a->{"salt"}
!ll&&!rl&&r
005
MC/DC0%0/2
defined $a->{"salt"} and length $a->{"salt"}
defined $a->{"salt"} length $a->{"salt"}
Truth table33%1/3
A and B
A= defined $a->{"salt"}B= length $a->{"salt"}
ABresult
0X0
100
111
580 5 $to_sign .= '3:seq' . bencode( $a->{seq} );
581 5 $to_sign .= '1:v' . bencode($v);
582 5 ▶ if ( defined $_ed25519_backend && $_ed25519_backend->( $self, $a->{sig}, $to_sign, $a->{k} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ed25519_backend and &$_ed25519_backend($self, $a->{'sig'}, $to_sign, $a->{'k'})) { } 4 1 5
Condition66%2/3
defined $_ed25519_backend and &$_ed25519_backend($self, $a->{"sig"}, $to_sign, $a->{"k"})
!ll&&!rl&&r
014
MC/DC50%1/2
defined $_ed25519_backend and &$_ed25519_backend($self, $a->{"sig"}, $to_sign, $a->{"k"})
defined $_ed25519_backend &$_ed25519_backend($self, $a->{"sig"}, $to_sign, $a->{"k"})
Truth table66%2/3
A and B
A= defined $_ed25519_backendB= &$_ed25519_backend($self, $a->{"sig"}, $to_sign, $a->{"k"})
ABresult
0X0
100
111
583 4 my $existing_obj = $data_storage->get($target);
584 4 ▶ if ( !defined $existing_obj || $a->{seq} > $existing_obj->value->{seq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $existing_obj or $a->{"seq"} > $existing_obj->value->{"seq"}) 4 0 4
Condition66%2/3
not defined $existing_obj or $a->{"seq"} > $existing_obj->value->{"seq"}
l!l&&r!l&&!r
220
MC/DC0%0/2
not defined $existing_obj or $a->{"seq"} > $existing_obj->value->{"seq"}
not defined $existing_obj $a->{"seq"} > $existing_obj->value->{"seq"}
Truth table66%2/3
not(A) or B
A= not defined $existing_objB= $a->{"seq"} > $existing_obj->value->{"seq"}
ABresult
1X1
011
000
585 4 ▶ if ( !defined $a->{cas} || ( $existing_obj && $existing_obj->value->{seq} == $a->{cas} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a->{"cas"} or $existing_obj and $existing_obj->value->{"seq"} == $a->{"cas"}) 3 1 4
Condition83%5/6
not defined $a->{"cas"} or $existing_obj and $existing_obj->value->{"seq"} == $a->{"cas"}
l!l&&r!l&&!r
211
$existing_obj and $existing_obj->value->{"seq"} == $a->{"cas"}
!ll&&!rl&&r
011
MC/DC66%2/3
not defined $a->{"cas"} or $existing_obj and $existing_obj->value->{"seq"} == $a->{"cas"}
not defined $a->{"cas"} $existing_obj $existing_obj->value->{"seq"} == $a->{"cas"}
Truth table75%3/4
not(A) or B and C
A= not defined $a->{"cas"}B= $existing_objC= $existing_obj->value->{"seq"} == $a->{"cas"}
ABCresult
1XX1
0111
00X0
0100
586 $data_storage->put(
587 $target,
588 { v => $v,
589 k => $a->{k},
590 sig => $a->{sig},
591 seq => $a->{seq},
592 3 ▶ ( defined $a->{salt} ? ( salt => $a->{salt} ) : () )
Branch: 1 branch
BranchTrueFalseTotal
defined $a->{'salt'} ? : 3 0 3
593 }
594 );
595 }
596 }
597 }
598 else {
599 # BEP 44: "If the signature is invalid, the request MUST be rejected."
600 # Additionally, we blacklist the peer for attempting a malicious update.
601 1 $_blacklist{$ip} = time;
602 1 return;
603 }
604 }
605 else { # Immutable
606  
607 # Validate immutable data size to prevent memory exhaustion
608 1 ▶ if ( length($v) > MAX_IMMUTABLE_VALUE_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v > 1048576) 0 1 1
609 0 $self->_emit_log( 'warn', "Immutable data value too large (" . length($v) . " bytes), rejecting" );
610 0 return;
611 }
612 1 $data_storage->put( $target, { v => $v } );
613  
614 # Cap total data storage entries
615 1 my %data_entries = $data_storage->entries;
616 1 ▶ if ( keys %data_entries > MAX_STORAGE_ENTRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %data_entries > 10000) 0 1 1
617 0 my @oldest = ( sort { $data_entries{$a} cmp $data_entries{$b} } keys %data_entries )[ 0 .. 999 ];
618 0 $data_storage->delete($_) for @oldest;
619 }
620 }
621 }
622 }
623 elsif ( $q eq 'sample_infohashes' ) {
624 2 ▶ if ($bep51) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep51) 1 1 2
625 1 my $target = $a->{target};
626  
627 # Validate target length
628 1 ▶ return if !defined $target || ( length($target) != 20 && length($target) != 32 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $target or length $target != 20 and length $target != 32 0 1 1
Condition33%2/6
not defined $target or length $target != 20 and length $target != 32
l!l&&r!l&&!r
001
length $target != 20 and length $target != 32
!ll&&!rl&&r
100
MC/DC0%0/3
not defined $target or length $target != 20 and length $target != 32
not defined $target length $target != 20 length $target != 32
Truth table25%1/4
not(A) or B and C
A= not defined $targetB= length $target != 20C= length $target != 32
ABCresult
1XX1
0111
00X0
0100
629 1 my %entries = $peer_storage->entries;
630 1 my @all_keys = keys %entries;
631 1 my $num = scalar @all_keys;
632  
633 # BEP 51: return up to 20 samples closest to target
634 my @sorted = sort {
635 1 my ( $a_dist, $b_dist ) = ( 0, 0 );
636 1 for my $i ( 0 .. 19 ) {
637 1 my $ab = ord( substr( $a, $i, 1 ) ) ^ ord( substr( $target, $i, 1 ) );
638 1 my $bb = ord( substr( $b, $i, 1 ) ) ^ ord( substr( $target, $i, 1 ) );
639 1 ▶ if ( $ab != $bb ) { $a_dist = $ab; $b_dist = $bb; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($ab != $bb) 1 0 1
640 }
641 1 $a_dist <=> $b_dist;
642 } @all_keys;
643 1 my @samples = splice( @sorted, 0, 20 );
644 1 $res->{r}{samples} = join( '', @samples );
645 1 $res->{r}{num} = $num;
646 1 $res->{r}{interval} = 21600; # 6 hours default
647 1 my @closest;
648 1 ▶ push @closest, $routing_table_v4->find_closest($target) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 1 0 1
649 1 ▶ push @closest, $routing_table_v6->find_closest($target) if $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 and $bep32 1 0 1
Condition33%1/3
$want_v6 and $bep32
!ll&&!rl&&r
001
MC/DC0%0/2
$want_v6 and $bep32
$want_v6 $bep32
Truth table33%1/3
A and B
A= $want_v6B= $bep32
ABresult
0X0
100
111
650 1 my ( $v4, $v6 ) = $self->_pack_nodes( \@closest );
651 1 ▶ $res->{r}{nodes} = $v4 if $v4 && $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $v4 and $want_v4 1 0 1
Condition33%1/3
$v4 and $want_v4
!ll&&!rl&&r
001
MC/DC0%0/2
$v4 and $want_v4
$v4 $want_v4
Truth table33%1/3
A and B
A= $v4B= $want_v4
ABresult
0X0
100
111
652 1 ▶ $res->{r}{nodes6} = $v6 if $v6 && $want_v6 && $bep32;
Branch: 1 branch
BranchTrueFalseTotal
if $v6 and $want_v6 and $bep32 0 1 1
Condition33%2/6
$v6 and $want_v6 and $bep32
!ll&&!rl&&r
100
$v6 and $want_v6
!ll&&!rl&&r
100
MC/DC0%0/3
$v6 and $want_v6 and $bep32
$v6 $want_v6 $bep32
Truth table25%1/4
A and B and C
A= $v6B= $want_v6C= $bep32
ABCresult
0XX0
10X0
1100
1111
653 }
654 }
655 169 ▶ $self->_check_external_ip( $msg->{ip}, $ip ) if exists $msg->{ip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $msg->{"ip"} 0 169 169
656 169 $self->_send_raw( bencode($res), $sender );
657 149 return { id => $id, ip => $ip, port => $port };
658 }
659  
660 81 ▶ method _handle_response ( $msg, $sender, $ip, $port ) {
Subroutine _handle_response: called CC=34 SCAR=36.5
661 81 ▶ $self->_check_external_ip( $msg->{ip}, $ip ) if exists $msg->{ip};
Branch: 1 branch
BranchTrueFalseTotal
if exists $msg->{"ip"} 41 40 81
662 81 ▶ return ( [], [], undef ) if $_blacklist{$ip};
Branch: 1 branch
BranchTrueFalseTotal
if $_blacklist{$ip} 0 81 81
663 81 my $r = $msg->{r};
664 81 ▶ return ( [], [], undef ) unless $r && $r->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $r and $r->{"id"} 0 81 81
Condition33%1/3
$r and $r->{"id"}
!ll&&!rl&&r
0081
MC/DC0%0/2
$r and $r->{"id"}
$r $r->{"id"}
Truth table33%1/3
A and B
A= $rB= $r->{"id"}
ABresult
0X0
100
111
665 81 ▶ my $tid = $msg->{t} // '';
Condition50%1/2
$msg->{"t"} // ""
l!l
810
MC/DC0%0/1
$msg->{"t"} // ""
$msg->{"t"}
Truth table50%1/2
A
A= $msg->{"t"}
Aresult
11
00
666 81 my $pending = delete $_pending_queries{$tid};
667  
668 # Deterministic cleanup of old pending queries (older than 30s)
669 {
670 81 my $now = time;
671 81 for my $k ( keys %_pending_queries ) {
672 324 ▶ delete $_pending_queries{$k} if $now - $_pending_queries{$k}{time} > 30;
Branch: 1 branch
BranchTrueFalseTotal
if $now - $_pending_queries{$k}{"time"} > 30 0 324 324
673 }
674 }
675 81 ▶ if ( $bep42 && !$security->validate_node_id( $r->{id}, $ip ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bep42 and not $security->validate_node_id($r->{"id"}, $ip)) 5 76 81
Condition100%3/3
$bep42 and not $security->validate_node_id($r->{"id"}, $ip)
!ll&&!rl&&r
47295
MC/DC100%2/2
$bep42 and not $security->validate_node_id($r->{"id"}, $ip)
$bep42 not $security->validate_node_id($r->{"id"}, $ip)
Truth table100%3/3
A and not(B)
A= $bep42B= not $security->validate_node_id($r->{"id"}, $ip)
ABresult
0X0
100
111
676 5 return ( [], [], undef );
677 }
678 76 ▶ my $table = ( $ip =~ /:/ ) ? $routing_table_v6 : $routing_table_v4;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /:/u ? : 0 76 76
679 76 my $stale = $table->add_peer( $r->{id}, { ip => $ip, port => $port } );
680 76 ▶ $self->ping( $stale->{data}{ip}, $stale->{data}{port} ) if $stale;
Branch: 1 branch
BranchTrueFalseTotal
if $stale 10 66 76
681 76 my $peers = [];
682 76 ▶ $peers = $self->_unpack_peers( $r->{values} ) if $r->{values};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"values"} 2 74 76
683 76 my @learned;
684 76 ▶ push @learned, $self->_unpack_nodes( $r->{nodes}, AF_INET )->@* if $r->{nodes};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"nodes"} 59 17 76
685 76 ▶ push @learned, $self->_unpack_nodes( $r->{nodes6}, AF_INET6 )->@* if $r->{nodes6};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"nodes6"} 0 76 76
686  
687 76 for my $node (@learned) {
688 369 ▶ next if $bep42 && !$security->validate_node_id( $node->{id}, $node->{ip} );
Branch: 1 branch
BranchTrueFalseTotal
if $bep42 and not $security->validate_node_id($node->{"id"}, $node->{"ip"}) 0 369 369
Condition66%2/3
$bep42 and not $security->validate_node_id($node->{"id"}, $node->{"ip"})
!ll&&!rl&&r
312570
MC/DC0%0/2
$bep42 and not $security->validate_node_id($node->{"id"}, $node->{"ip"})
$bep42 not $security->validate_node_id($node->{"id"}, $node->{"ip"})
Truth table66%2/3
A and not(B)
A= $bep42B= not $security->validate_node_id($node->{"id"}, $node->{"ip"})
ABresult
0X0
100
111
689 369 ▶ my $ntable = ( $node->{ip} =~ /:/ ) ? $routing_table_v6 : $routing_table_v4;
Branch: 1 branch
BranchTrueFalseTotal
$node->{'ip'} =~ /:/u ? : 0 369 369
690 369 $ntable->add_peer( $node->{id}, { ip => $node->{ip}, port => $node->{port} } );
691 }
692  
693 # Always include the responding node itself
694 76 push @learned, { id => $r->{id}, ip => $ip, port => $port };
695 76 my $scrape;
696 76 ▶ $scrape = { id => $r->{id}, ip => $ip, port => $port, sn => $r->{sn}, ln => $r->{ln} } if $pending && $pending->{q} eq 'scrape_peers';
Branch: 1 branch
BranchTrueFalseTotal
if $pending and $pending->{"q"} eq "scrape_peers" 2 74 76
Condition100%3/3
$pending and $pending->{"q"} eq "scrape_peers"
!ll&&!rl&&r
24502
MC/DC100%2/2
$pending and $pending->{"q"} eq "scrape_peers"
$pending $pending->{"q"} eq "scrape_peers"
Truth table100%3/3
A and B
A= $pendingB= $pending->{"q"} eq "scrape_peers"
ABresult
0X0
100
111
697 76 my $data;
698 76 ▶ if ( defined $r->{v} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"v"}) 0 76 76
699 $data = {
700 id => $r->{id},
701 ip => $ip,
702 port => $port,
703 v => $r->{v},
704 k => $r->{k},
705 sig => $r->{sig},
706 seq => $r->{seq},
707 salt => $r->{salt},
708 token => $r->{token}
709 0 };
710 }
711 76 my $sample;
712 76 ▶ if ( $pending && $pending->{q} eq 'sample_infohashes' && defined $r->{samples} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending and $pending->{"q"} eq "sample_infohashes" and defined $r->{"samples"}) 1 75 76
Condition83%5/6
$pending and $pending->{"q"} eq "sample_infohashes" and defined $r->{"samples"}
!ll&&!rl&&r
7501
$pending and $pending->{"q"} eq "sample_infohashes"
!ll&&!rl&&r
24511
MC/DC66%2/3
$pending and $pending->{"q"} eq "sample_infohashes" and defined $r->{"samples"}
$pending $pending->{"q"} eq "sample_infohashes" defined $r->{"samples"}
Truth table75%3/4
A and B and C
A= $pendingB= $pending->{"q"} eq "sample_infohashes"C= defined $r->{"samples"}
ABCresult
0XX0
10X0
1100
1111
713 1 my @samples;
714 1 my $blob = $r->{samples};
715 1 ▶ push @samples, substr( $blob, 0, 20, '' ) while length($blob) >= 20;
Branch: 1 branch
BranchTrueFalseTotal
length $blob >= 20 and do { push @samples, substr($blob, 0, 20, '') } 2 1 3
716 1 $sample = { id => $r->{id}, ip => $ip, port => $port, samples => \@samples, num => $r->{num}, interval => $r->{interval} };
717 }
718 76 my $token_only;
719 76 ▶ $token_only = { id => $r->{id}, ip => $ip, port => $port, token => $r->{token} } if defined $r->{token} && !$data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"token"} and not $data 29 47 76
Condition66%2/3
defined $r->{"token"} and not $data
!ll&&!rl&&r
47029
MC/DC50%1/2
defined $r->{"token"} and not $data
defined $r->{"token"} not $data
Truth table66%2/3
A and not(B)
A= defined $r->{"token"}B= not $data
ABresult
0X0
100
111
720 76 ▶ my $result = $scrape // $data // $sample // $token_only;
Condition88%8/9
(($scrape // $data) // $sample) // $token_only
l!l&&r!l&&!r
32944
($scrape // $data) // $sample
l!l&&r!l&&!r
2173
$scrape // $data
l!l&&r!l&&!r
2074
MC/DC75%3/4
(($scrape // $data) // $sample) // $token_only
$scrape $data $sample $token_only
Truth table80%4/5
A // B // C // D
A= $scrapeB= $dataC= $sampleD= $token_only
ABCDresult
1XXX1
01XX1
001X1
00011
00000
721 76 ▶ $result->{queried_target} = $pending->{target} if $result && $pending && $pending->{target};
Branch: 1 branch
BranchTrueFalseTotal
if $result and $pending and $pending->{"target"} 32 44 76
Condition66%4/6
$result and $pending and $pending->{"target"}
!ll&&!rl&&r
44032
$result and $pending
!ll&&!rl&&r
44032
MC/DC33%1/3
$result and $pending and $pending->{"target"}
$result $pending $pending->{"target"}
Truth table50%2/4
A and B and C
A= $resultB= $pendingC= $pending->{"target"}
ABCresult
0XX0
10X0
1100
1111
722 76 return ( \@learned, $peers, $result );
723 }
724  
725 628 ▶ method _send ( $msg, $addr, $port //= undef ) {
Subroutine _send: called CC=26 SCAR=40.7
726 628 ▶ $msg->{v} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 628 628
727 628 ▶ $msg->{a}{ro} = 1 if $read_only && $msg->{y} eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $read_only and $msg->{"y"} eq "q" 0 628 628
Condition33%1/3
$read_only and $msg->{"y"} eq "q"
!ll&&!rl&&r
62800
MC/DC0%0/2
$read_only and $msg->{"y"} eq "q"
$read_only $msg->{"y"} eq "q"
Truth table33%1/3
A and B
A= $read_onlyB= $msg->{"y"} eq "q"
ABresult
0X0
100
111
728 628 ▶ if ( !defined $port && !ref $addr && length($addr) >= 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $port || ref $addr and length $addr >= 16) 294 334 628
Condition66%4/6
not defined $port || ref $addr and length $addr >= 16
!ll&&!rl&&r
3340294
defined $port || ref $addr
l!l&&r!l&&!r
3340294
MC/DC33%1/3
not defined $port || ref $addr and length $addr >= 16
defined $port ref $addr length $addr >= 16
Truth table50%2/4
not(A || B) and C
A= defined $portB= ref $addrC= length $addr >= 16
ABCresult
1XX0
01X0
0000
0011
729 294 ▶ if ( !$ssrf_bypass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssrf_bypass) 288 6 294
730 288 my ( $gerr, $ip ) = getnameinfo( $addr, NI_NUMERICHOST );
731 288 ▶ if ( !$gerr && defined $ip && !is_safe_ip($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $gerr and defined $ip and not is_safe_ip($ip)) 0 288 288
Condition33%2/6
not $gerr and defined $ip and not is_safe_ip($ip)
!ll&&!rl&&r
02880
not $gerr and defined $ip
!ll&&!rl&&r
00288
MC/DC0%0/3
not $gerr and defined $ip and not is_safe_ip($ip)
not $gerr defined $ip not is_safe_ip($ip)
Truth table25%1/4
not(A) and B and not(C)
A= not $gerrB= defined $ipC= not is_safe_ip($ip)
ABCresult
0XX0
10X0
1100
1111
732 0 $self->_emit_log( 'warn', "DHT send blocked by SSRF policy: $ip" );
733 0 return;
734 }
735 }
736 294 $self->_send_raw( bencode($msg), $addr );
737 294 return;
738 }
739 334 ▶ ( $addr, $port ) = @$addr if ref $addr eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $addr eq "ARRAY" 0 334 334
740 334 ▶ if ( !$ssrf_bypass && !is_safe_host($addr) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssrf_bypass or is_safe_host($addr)) 1 333 334
Condition100%3/3
$ssrf_bypass or is_safe_host($addr)
l!l&&r!l&&!r
362971
MC/DC100%2/2
$ssrf_bypass or is_safe_host($addr)
$ssrf_bypass is_safe_host($addr)
Truth table100%3/3
A or B
A= $ssrf_bypassB= is_safe_host($addr)
ABresult
1X1
011
000
741 1 $self->_emit_log( 'debug', 'DHT send blocked by SSRF policy: ' . $addr );
742 1 return;
743 }
744 333 my ( $err, @res ) = getaddrinfo( $addr, $port, { socktype => SOCK_DGRAM } );
745 333 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 333 333
746 0 ▶ $self->_emit_log( 'debug', 'getaddrinfo failed for ' . $addr . ( defined $port ? ":$port" : '' ) . ': ' . $err );
Branch: 1 branch
BranchTrueFalseTotal
defined $port ? : 0 0 0
747 0 return;
748 }
749 333 for my $res (@res) {
750 497 my $family = sockaddr_family( $res->{addr} );
751 497 ▶ next unless ( $family == AF_INET && $want_v4 ) || ( $family == AF_INET6 && $want_v6 );
Branch: 1 branch
BranchTrueFalseTotal
unless $family == 2 and $want_v4 or $family == 10 and $want_v6 0 497 497
Condition55%5/9
$family == 2 and $want_v4 or $family == 10 and $want_v6
l!l&&r!l&&!r
415820
$family == 2 and $want_v4
!ll&&!rl&&r
820415
$family == 10 and $want_v6
!ll&&!rl&&r
0082
MC/DC0%0/4
$family == 2 and $want_v4 or $family == 10 and $want_v6
$family == 2 $want_v4 $family == 10 $want_v6
Truth table28%2/7
A and B or C and D
A= $family == 2B= $want_v4C= $family == 10D= $want_v6
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
752 497 ▶ if ( !$ssrf_bypass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssrf_bypass) 461 36 497
753 461 my ( $gerr, $ip ) = getnameinfo( $res->{addr}, NI_NUMERICHOST );
754 461 ▶ if ( !$gerr && defined $ip && !is_safe_ip($ip) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $gerr and defined $ip and not is_safe_ip($ip)) 0 461 461
Condition33%2/6
not $gerr and defined $ip and not is_safe_ip($ip)
!ll&&!rl&&r
04610
not $gerr and defined $ip
!ll&&!rl&&r
00461
MC/DC0%0/3
not $gerr and defined $ip and not is_safe_ip($ip)
not $gerr defined $ip not is_safe_ip($ip)
Truth table25%1/4
not(A) and B and not(C)
A= not $gerrB= defined $ipC= not is_safe_ip($ip)
ABCresult
0XX0
10X0
1100
1111
755 0 $self->_emit_log( 'debug', 'DHT send blocked by SSRF policy: ' . $ip );
756 0 next;
757 }
758 }
759 497 $self->_send_raw( bencode($msg), $res->{addr} );
760 }
761 }
762  
763 833 ▶ method _send_raw ( $data, $dest ) {
Subroutine _send_raw: called CC=4 SCAR=15.0
764 833 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 833 833
765 0 my ( $port, $ip ) = $self->_unpack_address($dest);
766 0 $self->_emit_log( 'debug', "SEND to $ip:$port" );
767 }
768 833 my $sent = $socket->send( $data, 0, $dest );
769 813 ▶ if ( !defined $sent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sent) 88 725 813
770 88 ▶ $self->_emit_log( 'warn', "DHT UDP send failed: $!" ) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 88 88
771 }
772 813 return $sent;
773 }
774  
775 24 ▶ method _pack_nodes ($peers) {
Subroutine _pack_nodes: called CC=8 SCAR=21.4
776 24 my $v4 = '';
777 24 my $v6 = '';
778 24 for my $p (@$peers) {
779 62 my $ip = $p->{data}{ip};
780 62 ▶ my $port = $p->{data}{port} // 0;
Condition50%1/2
$p->{"data"}{"port"} // 0
l!l
620
MC/DC0%0/1
$p->{"data"}{"port"} // 0
$p->{"data"}{"port"}
Truth table50%1/2
A
A= $p->{"data"}{"port"}
Aresult
11
00
781 62 ▶ if ( $ip =~ /:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:/u) { } 2 60 62
782 2 ▶ next unless $want_v6;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_v6 0 2 2
783 2 my $ip_bin = inet_pton( AF_INET6, $ip );
784 2 ▶ $v6 .= $p->{id} . $ip_bin . pack( 'n', $port ) if $ip_bin;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_bin 2 0 2
785 }
786 else {
787 60 ▶ next unless $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_v4 0 60 60
788 60 my $ip_bin = inet_aton($ip);
789 60 ▶ $v4 .= $p->{id} . $ip_bin . pack( 'n', $port ) if $ip_bin;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_bin 60 0 60
790 }
791 }
792 24 return ( $v4, $v6 );
793 }
794  
795 62 ▶ method _unpack_nodes ( $blob, $family //= AF_INET ) {
Subroutine _unpack_nodes: called CC=8 SCAR=20.8
796 62 my @nodes;
797 62 ▶ my $stride = ( $family == AF_INET ) ? 26 : 38;
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 ? : 61 1 62
798 62 ▶ my $ip_len = ( $family == AF_INET ) ? 4 : 16;
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 ? : 61 1 62
799 62 ▶ while ( length($blob) >= $stride && @nodes < MAX_UNPACK_NODES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $blob >= $stride and @nodes < 200) 571 62 633
Condition100%3/3
length $blob >= $stride and @nodes < 200
!ll&&!rl&&r
611571
MC/DC100%2/2
length $blob >= $stride and @nodes < 200
length $blob >= $stride @nodes < 200
Truth table100%3/3
A and B
A= length $blob >= $strideB= @nodes < 200
ABresult
0X0
100
111
800 571 my $chunk = substr( $blob, 0, $stride, '' );
801 571 my $id = substr( $chunk, 0, 20 );
802 571 my $ip_bin = substr( $chunk, 20, $ip_len );
803 571 my $port = unpack( 'n', substr( $chunk, 20 + $ip_len, 2 ) );
804 571 ▶ next if $port == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $port == 0 1 570 571
805 570 ▶ my $ip = ( $family == AF_INET ) ? inet_ntoa($ip_bin) : inet_ntop( AF_INET6, $ip_bin );
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 ? : 569 1 570
806 570 push @nodes, { id => $id, ip => $ip, port => $port };
807 }
808 62 return \@nodes;
809 }
810  
811 3 ▶ method _unpack_peers ($list) {
Subroutine _unpack_peers: called CC=12 SCAR=36.9
812 3 my @peers;
813 3 ▶ my @blobs = ( ref($list) eq 'ARRAY' ) ? @$list : ($list);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'ARRAY' ? : 2 1 3
814 3 for my $blob (@blobs) {
815 3 ▶ if ( length($blob) == 18 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $blob == 18) { } 1 2 3
elsif (length $blob == 6) { } 2 0 2
816 1 my ( $ip_bin, $port ) = unpack( 'a16 n', $blob );
817 1 ▶ push @peers, Net::BitTorrent::DHT::Peer->new( ip => inet_ntop( AF_INET6, $ip_bin ), port => $port, family => 6 ) if $want_v6;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 1 0 1
818 }
819 elsif ( length($blob) == 6 ) {
820 2 my ( $ip_bin, $port ) = unpack( 'a4 n', $blob );
821 2 ▶ push @peers, Net::BitTorrent::DHT::Peer->new( ip => inet_ntoa($ip_bin), port => $port, family => 4 ) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 2 0 2
822 }
823 else { # Fallback for non-standard implementations that pack multiple peers into one string
824 0 ▶ while ( length($blob) >= 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $blob >= 6) 0 0 0
825 0 ▶ if ( length($blob) >= 18 && ( length($blob) % 18 == 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $blob >= 18 and length($blob) % 18 == 0) { } 0 0 0
Condition0%0/3
length $blob >= 18 and length($blob) % 18 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $blob >= 18 and length($blob) % 18 == 0
length $blob >= 18 length($blob) % 18 == 0
Truth table0%0/3
A and B
A= length $blob >= 18B= length($blob) % 18 == 0
ABresult
0X0
100
111
826 0 my $chunk = substr( $blob, 0, 18, '' );
827 0 my ( $ip_bin, $port ) = unpack( 'a16 n', $chunk );
828 0 ▶ push @peers, Net::BitTorrent::DHT::Peer->new( ip => inet_ntop( AF_INET6, $ip_bin ), port => $port, family => 6 ) if $want_v6;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v6 0 0 0
829 }
830 else {
831 0 my $chunk = substr( $blob, 0, 6, '' );
832 0 my ( $ip_bin, $port ) = unpack( 'a4 n', $chunk );
833 0 ▶ push @peers, Net::BitTorrent::DHT::Peer->new( ip => inet_ntoa($ip_bin), port => $port, family => 4 ) if $want_v4;
Branch: 1 branch
BranchTrueFalseTotal
if $want_v4 0 0 0
834 }
835 }
836 }
837 }
838 3 return \@peers;
839 }
840  
841 1 ▶ method _pack_peers_raw ($peers) {
Subroutine _pack_peers_raw: called CC=2 SCAR=7.2
842 return [
843 map {
844 1 ( $_->{ip} =~ /:/ ) ? ( inet_pton( AF_INET6, $_->{ip} ) . pack( 'n', $_->{port} ) ) :
845 1 ▶ ( inet_aton( $_->{ip} ) . pack( 'n', $_->{port} ) )
Branch: 1 branch
BranchTrueFalseTotal
$_->{'ip'} =~ /:/u ? : 0 1 1
846 } @$peers
847 ];
848 }
849  
850 556 ▶ method _check_external_ip ( $ip_bin, $source_ip = undef ) {
Subroutine _check_external_ip: called CC=18 SCAR=31.0
851 556 ▶ if ( length($ip_bin) == 6 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $ip_bin == 6) { } 8 548 556
elsif (length $ip_bin == 18) { } 0 548 548
852 8 $ip_bin = substr( $ip_bin, 0, 4 );
853 }
854 elsif ( length($ip_bin) == 18 ) {
855 0 $ip_bin = substr( $ip_bin, 0, 16 );
856 }
857 556 ▶ my $ip = length($ip_bin) == 4 ? inet_ntoa($ip_bin) : length($ip_bin) == 16 ? inet_ntop( AF_INET6, $ip_bin ) : undef;
Branch: 2 branches
BranchTrueFalseTotal
length $ip_bin == 4 ? : 556 0 556
length $ip_bin == 16 ? : 0 0 0
858 556 ▶ return unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 556 556
859  
860 # Only accept public IPs as external IP
861 556 ▶ return unless is_safe_ip($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless is_safe_ip($ip) 48 508 556
862 508 my $consensus = 0;
863 508 ▶ if ($source_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_ip) { } 13 495 508
864  
865 # Track votes by source IP to prevent colluding-node manipulation.
866 # Each source IP can only contribute one vote for one candidate IP.
867 13 ▶ return if exists $ip_votes{$source_ip} && $ip_votes{$source_ip} eq $ip;
Branch: 1 branch
BranchTrueFalseTotal
if exists $ip_votes{$source_ip} and $ip_votes{$source_ip} eq $ip 0 13 13
Condition33%1/3
exists $ip_votes{$source_ip} and $ip_votes{$source_ip} eq $ip
!ll&&!rl&&r
1300
MC/DC0%0/2
exists $ip_votes{$source_ip} and $ip_votes{$source_ip} eq $ip
exists $ip_votes{$source_ip} $ip_votes{$source_ip} eq $ip
Truth table33%1/3
A and B
A= exists $ip_votes{$source_ip}B= $ip_votes{$source_ip} eq $ip
ABresult
0X0
100
111
868 13 $ip_votes{$source_ip} = $ip;
869 13 my @matching = grep { $_ eq $ip } values %ip_votes;
870 13 ▶ $consensus = 1 if scalar @matching >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matching >= 5 2 11 13
871 }
872 else {
873 # Fallback for direct/internal calls without source IP
874 495 $ip_votes{$ip}++;
875 495 ▶ $consensus = 1 if $ip_votes{$ip} >= 5;
Branch: 1 branch
BranchTrueFalseTotal
if $ip_votes{$ip} >= 5 3 492 495
876 }
877 508 ▶ if ($consensus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($consensus) 5 503 508
878 5 ▶ if ( !defined $external_ip || $external_ip ne $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $external_ip or $external_ip ne $ip) 5 0 5
Condition66%2/3
not defined $external_ip or $external_ip ne $ip
l!l&&r!l&&!r
410
MC/DC0%0/2
not defined $external_ip or $external_ip ne $ip
not defined $external_ip $external_ip ne $ip
Truth table66%2/3
not(A) or B
A= not defined $external_ipB= $external_ip ne $ip
ABresult
1X1
011
000
879 5 ▶ if ( time - $last_external_ip_change >= 300 ) { # 5-min cooldown between changes
Branch: 1 branch
BranchTrueFalseTotal
if (time - $last_external_ip_change >= 300) 4 1 5
880 4 $external_ip = $ip;
881 4 $last_external_ip_change = time;
882 4 $self->_emit( external_ip_detected => $ip );
883 }
884 }
885 5 %ip_votes = (); # Reset votes after consensus
886 }
887  
888 # Cap ip_votes hash
889 508 ▶ if ( keys %ip_votes > 500 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %ip_votes > 500) 0 508 508
890 0 my @keys = keys %ip_votes;
891 0 delete @ip_votes{ @keys[ 0 .. 49 ] };
892 }
893 }
894  
895 0 ▶ method run () {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
896 0 $running = 1;
897 0 $self->bootstrap();
898 0 ▶ $self->tick(1) while $running;
Branch: 1 branch
BranchTrueFalseTotal
$running and do { $self->tick(1) } 0 0 0
899 }
900 };
901 38 1;
↑ Summary lib/Net/BitTorrent.pm »