lib/Net/BitTorrent/Tracker/UDP.pm

filter: Statementstmt 48.4% 92 / 190 Branchbran 17.1% 12 / 70 Conditioncond 29.0% 9 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 68.9% 20 / 29 Podpod 44.4% 4 / 9 totaltotal 39.4% 137 / 347 CC 53 SCAR 36.5
CC
53
Cov
39%
tick
132.0
receive_data
110.0
perform_announce
30.0
CRAP 38.5
« lib/Net/BitTorrent/Tracker.pm ↑ Summary lib/Net/BitTorrent/Tracker/HTTP.pm »
1 28 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 28 ▶use feature 'class', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 28 ▶no warnings 'experimental::class', 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶class Net::BitTorrent::Tracker::UDP v2.1.1 : isa(Net::BitTorrent::Tracker::Base) {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶ use Net::BitTorrent::Protocol::BEP23;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶ use Net::BitTorrent::SSRF qw[is_safe_ip resolve_and_pin];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶ use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶ use Crypt::URandom qw[urandom];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶ use Digest::SHA qw[sha1];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶ use constant HAS_64BIT => $Config{ivsize} >= 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶ use constant MAX_PENDING_TRANSACTIONS => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 field $connection_id = HAS_64BIT ? 0 : pack( 'NN', 0, 0 );
14 28 field $connection_id_time = 0;
15 28 field $transaction_id;
16 28 field $host;
17 28 field $port;
18 28 field $resolved_ip; # Cached resolved IP for SSRF-safe retransmissions
19 28 field $socket;
20 28 field %pending_transactions; # tid => { type => ..., cb => ..., payload => ..., retries => ..., timestamp => ... }
21  
22 3 ▶ sub _split64 ($val) {
Subroutine _split64: called CC=2 SCAR=9.2
23 3 ▶ $val //= 0;
Condition50%1/2
$val //= 0
l!l
30
MC/DC0%0/1
$val //= 0
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
24 3 if (HAS_64BIT) { return $val }
25 0 my $hi = int( $val / 4294967296 );
26 0 my $lo = $val - ( $hi * 4294967296 );
27 0 return ( $hi, $lo );
28 }
29 9 ▶ ADJUST {
Subroutine __ANON__: called CC=6 SCAR=20.2
30 9 ▶ if ( $self->url =~ m{^udp://([^:/]+):(\d+)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url =~ m[^udp://([^:/]+):(\d+)]u) { } 9 0 9
31 9 $host = $1;
32 9 $port = $2;
33 9 ▶ unless ( $self->ssrf_bypass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ssrf_bypass) { } 5 4 9
34 4 my ( $ip, $rp ) = resolve_and_pin( $host, $port );
35 4 ▶ unless ( defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 4 0 4
36 4 $self->_emit_log( 'error', "UDP tracker blocked by SSRF policy: $host:$port" );
37 4 return;
38 }
39 0 $resolved_ip = $ip;
40 0 ▶ $port = $rp if defined $rp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rp 0 0 0
41 }
42 else {
43 5 $resolved_ip = $host;
44 }
45 5 ▶ $socket = IO::Socket::IP->new( Proto => 'udp', Blocking => 0 ) or $self->_emit_log( 'error', "Could not create UDP socket: $!" );
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::IP"->new("Proto", "udp", "Blocking", 0) or $self->_emit_log("error", "Could not create UDP socket: $!") 5 0 5
46 }
47 else {
48 0 $self->_emit_log( 'error', 'Invalid UDP tracker URL: ' . $self->url );
49 }
50 }
51  
52 102 ▶ method _new_transaction_id () {
Subroutine _new_transaction_id: called CC=1 SCAR=0.0
53 102 return $transaction_id = unpack( 'N', urandom(4) ) & 0x7FFFFFFF;
54 }
55  
56 0 ▶ method _is_connected () {
Subroutine _is_connected: not called CC=2 SCAR=17.9
57 0 ▶ return defined $connection_id && ( time() - $connection_id_time < 60 );
Condition0%0/3
defined $connection_id && time - $connection_id_time < 60
!ll&&!rl&&r
000
MC/DC0%0/2
defined $connection_id && time - $connection_id_time < 60
defined $connection_id time - $connection_id_time < 60
Truth table0%0/3
A && B
A= defined $connection_idB= time - $connection_id_time < 60
ABresult
0X0
100
111
58 }
59  
60 0 ▶ method tick ( $delta = 0.1 ) {
Subroutine tick: not called CC=11 SCAR=48.8
Pod documented
61 0 ▶ return unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
62  
63 # Check for incoming data
64 0 ▶ while ( $socket->recv( my $buf, 4096 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->recv(my $buf, 4096)) 0 0 0
65 0 my $sender_addr = $socket->peeraddr();
66 0 ▶ if ( defined $sender_addr && defined $resolved_ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sender_addr and defined $resolved_ip) 0 0 0
Condition0%0/3
defined $sender_addr and defined $resolved_ip
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sender_addr and defined $resolved_ip
defined $sender_addr defined $resolved_ip
Truth table0%0/3
A and B
A= defined $sender_addrB= defined $resolved_ip
ABresult
0X0
100
111
67 0 my $sender_ip = unpack( 'N', $sender_addr );
68 0 my $expected_ip = unpack( 'N', inet_aton($resolved_ip) );
69 0 ▶ if ( defined $expected_ip && $sender_ip ne $expected_ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_ip and $sender_ip ne $expected_ip) 0 0 0
Condition0%0/3
defined $expected_ip and $sender_ip ne $expected_ip
!ll&&!rl&&r
000
MC/DC0%0/2
defined $expected_ip and $sender_ip ne $expected_ip
defined $expected_ip $sender_ip ne $expected_ip
Truth table0%0/3
A and B
A= defined $expected_ipB= $sender_ip ne $expected_ip
ABresult
0X0
100
111
70 0 $self->_emit_log( 'debug', 'UDP tracker response from unexpected sender, ignoring' );
71 0 next;
72 }
73 }
74 0 $self->receive_data($buf);
75 }
76  
77 # Handle retransmissions
78 0 my $now = time();
79 0 for my $tid ( keys %pending_transactions ) {
80 0 my $entry = $pending_transactions{$tid};
81 0 my $timeout = 15 * ( 2**$entry->{retries} );
82 0 ▶ if ( $now - $entry->{timestamp} > $timeout ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $entry->{"timestamp"} > $timeout) 0 0 0
83 0 ▶ if ( $entry->{retries} >= 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"retries"} >= 8) 0 0 0
84 0 $self->_emit_log( 'error', "UDP transaction $tid timed out after 8 retries" );
85 0 delete $pending_transactions{$tid};
86 0 next;
87 }
88 0 $entry->{retries}++;
89 0 $entry->{timestamp} = $now;
90 0 $self->_send_packet( $entry->{payload} );
91 }
92 }
93 }
94  
95 0 ▶ method receive_data ($data) {
Subroutine receive_data: not called CC=10 SCAR=47.0
Pod documented
96 0 ▶ return if length($data) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $data < 8 0 0 0
97 0 my ( $action, $tid ) = unpack( 'N N', $data );
98 0 my $entry = delete $pending_transactions{$tid};
99 0 ▶ if ( !$entry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry) 0 0 0
100 0 $self->_emit_log( 'debug', 'Received UDP packet with unknown transaction ID: ' . $tid );
101 0 return;
102 }
103 0 try {
104 0 ▶ if ( $action == 3 ) { # Error
Branch: 1 branch
BranchTrueFalseTotal
if ($action == 3) 0 0 0
105 0 my $msg = substr( $data, 8 );
106 0 $msg =~ s/[^\x20-\x7E]/./g; # Sanitize: replace non-printable with dot
107 0 $self->_emit_log( 'error', 'UDP Tracker error: ' . $msg );
108 0 return;
109 }
110 0 ▶ if ( $entry->{type} eq 'connect' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($entry->{'type'} eq 'connect') { } 0 0 0
elsif ($entry->{'type'} eq 'announce') { } 0 0 0
elsif ($entry->{'type'} eq 'scrape') { } 0 0 0
111 0 if (HAS_64BIT) {
112 0 ( undef, undef, $connection_id ) = unpack( 'N N Q>', $data );
113 }
114 else {
115 $connection_id = substr( $data, 8, 8 );
116 }
117 0 $connection_id_time = time();
118  
119 # Now that we are connected, trigger the original request
120 0 ▶ if ( $entry->{on_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"on_connect"}) 0 0 0
121 0 $entry->{on_connect}->();
122 }
123 }
124 elsif ( $entry->{type} eq 'announce' ) {
125 0 my $res = $self->parse_announce_response($data);
126 0 ▶ $entry->{cb}->($res) if $entry->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"cb"} 0 0 0
127 }
128 elsif ( $entry->{type} eq 'scrape' ) {
129 0 my $res = $self->parse_scrape_response( $data, $entry->{num_hashes} );
130 0 ▶ $entry->{cb}->($res) if $entry->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"cb"} 0 0 0
131 }
132 }
133 catch ($e) {
134 0 $self->_emit_log( 'error', 'Error parsing UDP tracker response: ' . $e );
135 }
136 }
137  
138 0 ▶ method _send_packet ($payload) {
Subroutine _send_packet: not called CC=4 SCAR=30.0
139 0 ▶ return unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 0 0
140 0 ▶ my $target_ip = $resolved_ip // $host;
Condition0%0/3
$resolved_ip // $host
l!l&&r!l&&!r
000
MC/DC0%0/2
$resolved_ip // $host
$resolved_ip $host
Truth table0%0/3
A // B
A= $resolved_ipB= $host
ABresult
1X1
011
000
141 0 my $dest = sockaddr_in( $port, inet_aton($target_ip) );
142 0 my $sent = $socket->send( $payload, 0, $dest );
143 0 ▶ $self->_emit_log( 'warn', "UDP send failed: $!" ) unless defined $sent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sent 0 0 0
144 0 return $sent;
145 }
146  
147 1 ▶ method build_connect_packet () {
Subroutine build_connect_packet: called CC=1 SCAR=0.0
Pod undocumented
148 1 my $tid = $self->_new_transaction_id();
149 1 if (HAS_64BIT) {
150 28 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 return ( $tid, pack( 'Q> N N', 0x41727101980, 0, $tid ) );
152 }
153 0 return ( $tid, pack( 'NN N N', 0x417, 0x27101980, 0, $tid ) );
154 }
155  
156 0 ▶ method perform_announce ( $params, $cb = undef ) {
Subroutine perform_announce: not called CC=5 SCAR=34.0
Pod documented
157 0 ▶ if ( scalar keys %pending_transactions >= MAX_PENDING_TRANSACTIONS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pending_transactions >= 100) 0 0 0
158 0 $self->_emit_log( 'warn', 'UDP tracker pending transaction limit reached' );
159 0 return;
160 }
161 0 ▶ if ( !$self->_is_connected() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_connected) 0 0 0
162 0 my ( $tid, $pkt ) = $self->build_connect_packet();
163 $pending_transactions{$tid} = {
164 type => 'connect',
165 payload => $pkt,
166 retries => 0,
167 timestamp => time(),
168 0 ▶ on_connect => sub { $self->perform_announce( $params, $cb ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 0 };
170 0 $self->_send_packet($pkt);
171 0 return;
172 }
173 0 my $pkt = $self->build_announce_packet($params);
174 0 ▶ return unless $pkt;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkt 0 0 0
175 0 my ($tid) = unpack( 'x8 N', $pkt ); # transaction_id is at offset 12 but after action(4)
176  
177 # Wait, action(4) tid(4). So offset 12 is correct for cid(8) + action(4).
178 0 $tid = unpack( 'N', substr( $pkt, 12, 4 ) );
179 0 $pending_transactions{$tid} = { type => 'announce', payload => $pkt, retries => 0, timestamp => time(), cb => $cb, };
180 0 $self->_send_packet($pkt);
181 }
182  
183 0 ▶ method perform_scrape ( $infohashes, $cb = undef ) {
Subroutine perform_scrape: not called CC=4 SCAR=30.0
Pod documented
184 0 ▶ if ( scalar keys %pending_transactions >= MAX_PENDING_TRANSACTIONS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %pending_transactions >= 100) 0 0 0
185 0 $self->_emit_log( 'warn', 'UDP tracker pending transaction limit reached' );
186 0 return;
187 }
188 0 ▶ if ( !$self->_is_connected() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_connected) 0 0 0
189 0 my ( $tid, $pkt ) = $self->build_connect_packet();
190 $pending_transactions{$tid} = {
191 type => 'connect',
192 payload => $pkt,
193 retries => 0,
194 timestamp => time(),
195 0 ▶ on_connect => sub { $self->perform_scrape( $infohashes, $cb ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 };
197 0 $self->_send_packet($pkt);
198 0 return;
199 }
200 0 my $pkt = $self->build_scrape_packet($infohashes);
201 0 my $tid = unpack( 'N', substr( $pkt, 12, 4 ) );
202 0 $pending_transactions{$tid}
203 = { type => 'scrape', payload => $pkt, retries => 0, timestamp => time(), cb => $cb, num_hashes => scalar @$infohashes, };
204 0 $self->_send_packet($pkt);
205 }
206  
207 1 ▶ method build_announce_packet ($params) {
Subroutine build_announce_packet: called CC=9 SCAR=24.6
Pod undocumented
208 1 $self->_new_transaction_id();
209 1 my %event_map = ( none => 0, completed => 1, started => 2, stopped => 3, );
210 1 ▶ my $event = $event_map{ $params->{event} // 'none' } // 0;
Condition50%2/4
$event_map{$params->{"event"} // "none"} // 0
l!l
10
$params->{"event"} // "none"
l!l
01
MC/DC0%0/1
$event_map{$params->{"event"} // "none"} // 0
$event_map{$params->{"event"} // "none"}
MC/DC0%0/1
$params->{"event"} // "none"
$params->{"event"}
Truth table50%1/2
A
A= $event_map{$params->{"event"} // "none"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"event"}
Aresult
11
00
211 1 my $ih = $params->{info_hash};
212 1 my $ih_len = length($ih);
213  
214 # Mandatory key for tracker identification
215 1 ▶ my $key = $params->{key} // ( unpack( 'N', urandom(4) ) & 0x7FFFFFFF );
Condition50%1/2
$params->{"key"} // unpack("N", urandom(4)) & 2147483647
l!l
01
MC/DC0%0/1
$params->{"key"} // unpack("N", urandom(4)) & 2147483647
$params->{"key"}
Truth table50%1/2
A
A= $params->{"key"}
Aresult
11
00
216  
217 # BEP 52: Support 32-byte infohashes
218 # For UDP trackers, we use the v1 infohash if available,
219 # or truncate/hash the v2 one as per common practice if 32 bytes provided.
220 # REAL BEP 52 UDP trackers expect a modified layout, but standard ones
221 # usually get the 20-byte 'info_hash' (v1 or truncated).
222 1 ▶ my $ih_20 = length($ih) == 32 ? sha1($ih) : $ih;
Branch: 1 branch
BranchTrueFalseTotal
length $ih == 32 ? : 0 1 1
223 1 my $tmpl = HAS_64BIT ? 'Q> N N a20 a20 Q> Q> Q> N N N l> n' : 'a8 N N a20 a20 NN NN NN N N N l> n';
224 return pack(
225 $tmpl, $connection_id, 1, $transaction_id, $ih_20, $params->{peer_id}, _split64( $params->{downloaded} // 0 ),
226 _split64( $params->{left} // 0 ), _split64( $params->{uploaded} // 0 ), $event, 0, # ip
227 $key, $params->{num_want} // -1, $params->{port}
228 1 ▶ );
Condition50%4/8
$params->{"downloaded"} // 0
l!l
01
$params->{"left"} // 0
l!l
01
$params->{"uploaded"} // 0
l!l
01
$params->{"num_want"} // -1
l!l
01
MC/DC0%0/1
$params->{"downloaded"} // 0
$params->{"downloaded"}
MC/DC0%0/1
$params->{"left"} // 0
$params->{"left"}
MC/DC0%0/1
$params->{"uploaded"} // 0
$params->{"uploaded"}
MC/DC0%0/1
$params->{"num_want"} // -1
$params->{"num_want"}
Truth table50%1/2
A
A= $params->{"downloaded"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"left"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"uploaded"}
Aresult
11
00
Truth table50%1/2
A
A= $params->{"num_want"}
Aresult
11
00
229 }
230  
231 1 ▶ method parse_announce_response ($data) {
Subroutine parse_announce_response: called CC=4 SCAR=15.5
Pod undocumented
232 1 ▶ return { interval => 0, leechers => 0, seeders => 0, peers => [] } if length($data) < 20;
Branch: 1 branch
BranchTrueFalseTotal
if length $data < 20 0 1 1
233 1 my ( $action, $tid, $interval, $leechers, $seeders ) = unpack( 'N N N N N', $data );
234 1 my $peers_raw = substr( $data, 20 );
235  
236 # Cap peer list: max 500 peers per response (UDP datagram is ~4096 bytes anyway)
237 1 my $peers;
238 1 ▶ if ( length($peers_raw) % 18 == 0 && length($peers_raw) % 6 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($peers_raw) % 18 == 0 and length($peers_raw) % 6 != 0) { } 0 1 1
Condition33%1/3
length($peers_raw) % 18 == 0 and length($peers_raw) % 6 != 0
!ll&&!rl&&r
010
MC/DC0%0/2
length($peers_raw) % 18 == 0 and length($peers_raw) % 6 != 0
length($peers_raw) % 18 == 0 length($peers_raw) % 6 != 0
Truth table33%1/3
A and B
A= length($peers_raw) % 18 == 0B= length($peers_raw) % 6 != 0
ABresult
0X0
100
111
239 0 $peers_raw = substr( $peers_raw, 0, 500 * 18 );
240 0 $peers = Net::BitTorrent::Protocol::BEP23::unpack_peers_ipv6($peers_raw);
241 }
242 else {
243 1 $peers_raw = substr( $peers_raw, 0, 500 * 6 );
244 1 $peers = Net::BitTorrent::Protocol::BEP23::unpack_peers_ipv4($peers_raw);
245 }
246 1 return { interval => $interval, leechers => $leechers, seeders => $seeders, peers => $peers, };
247 }
248  
249 0 ▶ method build_scrape_packet ($infohashes) {
Subroutine build_scrape_packet: not called CC=3 SCAR=24.8
Pod undocumented
250 0 $self->_new_transaction_id();
251 0 ▶ my @capped = @$infohashes[ 0 .. ( @$infohashes > 70 ? 69 : $#$infohashes ) ];
Branch: 1 branch
BranchTrueFalseTotal
@$infohashes > 70 ? : 0 0 0
252  
253 # Validate and truncate each hash to 20 bytes
254 0 ▶ my $ih_data = join( '', map { length($_) == 32 ? sha1($_) : substr( $_, 0, 20 ) } @capped );
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 32 ? : 0 0 0
255 0 my $tmpl = HAS_64BIT ? 'Q> N N a*' : 'a8 N N a*';
256 0 return pack( $tmpl, $connection_id, 2, $transaction_id, $ih_data );
257 }
258  
259 1 ▶ method parse_scrape_response ( $data, $num_hashes ) {
Subroutine parse_scrape_response: called CC=4 SCAR=13.9
Pod undocumented
260 1 ▶ return { files => [] } if length($data) < 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $data < 8 0 1 1
261 1 my ( $action, $tid ) = unpack( 'N N', $data );
262 1 my $results = { files => [] };
263 1 my $max_hashes = int( ( length($data) - 8 ) / 12 );
264 1 ▶ $num_hashes = $max_hashes if $num_hashes > $max_hashes;
Branch: 1 branch
BranchTrueFalseTotal
if $num_hashes > $max_hashes 1 0 1
265 1 ▶ for ( my $i = 0; $i < $num_hashes; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_hashes) 1 1 2
266 1 my ( $seeders, $completed, $leechers ) = unpack( 'N N N', substr( $data, 8 + ( $i * 12 ), 12 ) );
267 1 push @{ $results->{files} }, { seeders => $seeders, completed => $completed, leechers => $leechers };
268 }
269 1 return $results;
270 }
271 };
272 28 1;
« lib/Net/BitTorrent/Tracker.pm ↑ Summary lib/Net/BitTorrent/Tracker/HTTP.pm »