blib/lib/Net/BitTorrent/Protocol/BEP23.pm

filter: Statementstmt 100% 48 / 48 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 97.3% 73 / 75 CC 9 SCAR 8.2
CC
9
Cov
97%
pack_peers_ipv6
3.0
unpack_peers_ipv6
3.0
pack_peers_ipv4
3.0
CRAP 2.3
« lib/Net/BitTorrent/Protocol/BEP09.pm ↑ Summary blib/lib/Net/BitTorrent/Protocol/Simulation.pm »
1 32 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 32 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use Net::BitTorrent::Emitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 class Net::BitTorrent::Protocol::BEP23 v2.1.1 : isa(Net::BitTorrent::Emitter) {
6 32 ▶ use Socket qw[inet_pton inet_ntop AF_INET];
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶ sub pack_peers_ipv4 (@peers) {
Subroutine pack_peers_ipv4: called CC=3 SCAR=11.0
Pod documented
9 5 my $packed = '';
10 5 for my $peer (@peers) {
11  
12 # $peer is { ip => '...', port => ... }
13 154 my $ip = inet_pton( AF_INET, $peer->{ip} );
14 154 ▶ die "Invalid IPv4 address: $peer->{ip}" unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 153 154
15 153 $packed .= $ip . pack( 'n', $peer->{port} );
16 }
17 4 return $packed;
18 }
19  
20 11 ▶ sub unpack_peers_ipv4 ($data) {
Subroutine unpack_peers_ipv4: called CC=3 SCAR=11.0
Pod documented
21 11 ▶ die 'Compact peer list must be a multiple of 6 bytes' if length($data) % 6 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if length($data) % 6 != 0 3 8 11
22 8 my @peers;
23 8 ▶ for ( my $i = 0; $i < length($data); $i += 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $data) 605 8 613
24 605 my $chunk = substr( $data, $i, 6 );
25 605 my ( $ip_raw, $port ) = unpack( 'a4 n', $chunk );
26 605 push @peers, { ip => inet_ntop( AF_INET, $ip_raw ), port => $port };
27 }
28 8 return \@peers;
29 }
30  
31 2 ▶ sub pack_peers_ipv6 (@peers) {
Subroutine pack_peers_ipv6: called CC=3 SCAR=11.0
Pod documented
32 32 ▶ use Socket qw[inet_pton AF_INET6];
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 my $packed = '';
34 2 for my $peer (@peers) {
35 1 my $ip = inet_pton( AF_INET6, $peer->{ip} );
36 1 ▶ die "Invalid IPv6 address: $peer->{ip}" unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 0 1 1
37 1 $packed .= $ip . pack( 'n', $peer->{port} );
38 }
39 2 return $packed;
40 }
41  
42 1 ▶ sub unpack_peers_ipv6 ($data) {
Subroutine unpack_peers_ipv6: called CC=3 SCAR=11.0
Pod documented
43 32 ▶ use Socket qw[inet_ntop AF_INET6];
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶ die 'Compact IPv6 peer list must be a multiple of 18 bytes' if length($data) % 18 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if length($data) % 18 != 0 0 1 1
45 1 my @peers;
46 1 ▶ for ( my $i = 0; $i < length($data); $i += 18 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $data) 1 1 2
47 1 my $chunk = substr( $data, $i, 18 );
48 1 my ( $ip_raw, $port ) = unpack( 'a16 n', $chunk );
49 1 push @peers, { ip => inet_ntop( AF_INET6, $ip_raw ), port => $port };
50 }
51 1 return \@peers;
52 }
53 32 } 1;
« lib/Net/BitTorrent/Protocol/BEP09.pm ↑ Summary blib/lib/Net/BitTorrent/Protocol/Simulation.pm »