VPNDetection/Bogon.pm

filter: Statementstmt 100% 62 / 62 Branchbran 100% 20 / 20 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 92.4% 110 / 119 CC 19 SCAR 14.1
CC
19
Cov
97%
is_bogon
8.0
unmapped
5.1
bogon_result
4.0
CRAP 4.1
« VPNDetection/Cache.pm ↑ Summary VPNDetection/Database.pm »
1 package VPNDetection::Bogon;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Socket qw(AF_INET AF_INET6 inet_pton);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use VPNDetection::Bogons;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use VPNDetection::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our $VERSION = '3.16.0';
12  
13 # Whether an address is private, loopback, link-local, documentation, multicast
14 # or otherwise not routable on the public internet, including the IPv6
15 # equivalents and the 6to4 and Teredo ranges that wrap them.
16 #
17 # These can never be VPN or proxy infrastructure, so the client answers them
18 # itself and they never cost a request.
19 28496 ▶sub is_bogon {
Subroutine is_bogon: called CC=8 SCAR=20.8
Pod undocumented
20 28496 my ($ip) = @_;
21 28496 ▶ return 0 unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 1 28495 28496
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
0128495
MC/DC50%1/2
defined $ip and length $ip
defined $ip length $ip
Truth table66%2/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
22  
23 28495 $ip = unmapped($ip);
24 28495 my $v6 = index($ip, ':') >= 0;
25 28495 ▶ my $addr = _pton($v6 ? AF_INET6 : AF_INET, $ip);
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 417 28078 28495
26 28495 ▶ return 0 unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 3 28492 28495
27  
28 28492 ▶ for my $range (@{ $v6 ? _v6() : _v4() }) {
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 417 28075 28492
29 466718 ▶ return 1 if ($addr & $range->[1]) eq $range->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ($addr & $range->[1]) eq $range->[0] 376 466342 466718
30 }
31 28116 return 0;
32 }
33  
34 # The IPv4 address an IPv4-mapped IPv6 address (::ffff:a.b.c.d, in any spelling)
35 # carries, dotted, and any other string as given. A server listening on :: sees
36 # every IPv4 visitor in that form, which read whole is inside ::ffff:0:0/96, so
37 # judging it whole would answer every such visitor locally as a bogon. ::a.b.c.d
38 # is IPv4-compatible rather than mapped, and stays IPv6.
39 83981 ▶sub unmapped {
Subroutine unmapped: called CC=5 SCAR=16.2
Pod undocumented
40 83981 my ($ip) = @_;
41 83981 ▶ return $ip unless defined $ip && index($ip, ':') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and index($ip, ":") != -1 83541 440 83981
Condition66%2/3
defined $ip and index($ip, ":") != -1
!ll&&!rl&&r
083541440
MC/DC50%1/2
defined $ip and index($ip, ":") != -1
defined $ip index($ip, ":") != -1
Truth table66%2/3
A and B
A= defined $ipB= index($ip, ":") != -1
ABresult
0X0
100
111
42 440 my $addr = _pton(AF_INET6, $ip);
43 440 ▶ return $ip unless defined $addr && substr($addr, 0, 12) eq ("\0" x 10) . "\xff\xff";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377" 419 21 440
Condition66%2/3
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
!ll&&!rl&&r
041921
MC/DC50%1/2
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
defined $addr substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
Truth table66%2/3
A and B
A= defined $addrB= substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
ABresult
0X0
100
111
44 21 return join '.', unpack 'C4', substr($addr, 12);
45 }
46  
47 # The answer a bogon gets, in the full shape the API serves at its widest plan:
48 # every flag present and false, every detail object present and empty, and a
49 # location and AS of empty strings and AS 0.
50 6 ▶sub bogon_result {
Subroutine bogon_result: called CC=4 SCAR=13.9
Pod undocumented
51 6 my ($ip) = @_;
52 6 my %answer = (ip => $ip, is_bogon => 1);
53 6 $answer{$_} = 0 for @VPNDetection::Result::FLAGS;
54 6 $answer{$_} = {} for @VPNDetection::Result::DETAILS;
55 6 $answer{location} = { map { $_ => '' } @VPNDetection::Result::LOCATION };
56 6 ▶ $answer{asn} = { map { $_ => $_ eq 'asn' ? 0 : '' } @VPNDetection::Result::ASN };
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'asn' ? : 6 24 30
57 6 return VPNDetection::Result->_new(\%answer, {});
58 }
59  
60 # Parsed on first use rather than at load: a program that never looks an address
61 # up should not pay for the table.
62 7 my ($v4, $v6);
63  
64 28075 ▶sub _v4 {
Subroutine _v4: called CC=2 SCAR=6.9
65 28075 ▶ $v4 ||= [map { _range(AF_INET, $_, 4) } @VPNDetection::Bogons::V4];
Condition100%2/2
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
l!l
280723
MC/DC100%1/1
$v4 ||= [map({_range(2, $_, 4);} @VPNDetection::Bogons::V4)]
$v4
Truth table100%2/2
A
A= $v4
Aresult
11
00
66 28075 return $v4;
67 }
68  
69 417 ▶sub _v6 {
Subroutine _v6: called CC=2 SCAR=6.9
70 417 ▶ $v6 ||= [map { _range(AF_INET6, $_, 16) } @VPNDetection::Bogons::V6];
Condition100%2/2
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
l!l
4143
MC/DC100%1/1
$v6 ||= [map({_range(10, $_, 16);} @VPNDetection::Bogons::V6)]
$v6
Truth table100%2/2
A
A= $v6
Aresult
11
00
71 417 return $v6;
72 }
73  
74 # A range is [masked network bytes, mask bytes], both packed strings, so
75 # membership is one bitwise string AND and one string comparison. Perl has no
76 # 128 bit integer, and it does not need one: `&` on two byte strings is a
77 # per-byte AND, so a /36 IPv6 mask is just a 16 byte string.
78 #
79 # That behavior is why no file in this distribution says `use v5.28` or later:
80 # those enable the `bitwise` feature, under which `&` becomes numeric-only, both
81 # operands numify to 0, and EVERY address would match EVERY range. The isBogon
82 # corpus is what catches it.
83 240 ▶sub _range {
Subroutine _range: called CC=1 SCAR=0.0
84 240 my ($family, $cidr, $width) = @_;
85 240 my ($net, $bits) = split m{/}, $cidr, 2;
86 240 my $mask = _mask($bits, $width);
87 240 return [inet_pton($family, $net) & $mask, $mask];
88 }
89  
90 240 ▶sub _mask {
Subroutine _mask: called CC=2 SCAR=6.9
91 240 my ($bits, $width) = @_;
92 240 my $mask = "\xff" x int($bits / 8);
93 240 my $partial = $bits % 8;
94 240 ▶ $mask .= chr((0xff << (8 - $partial)) & 0xff) if $partial;
Branch: 1 branch
BranchTrueFalseTotal
if $partial 99 141 240
95 240 return $mask . "\x00" x ($width - length $mask);
96 }
97  
98 # inet_pton is strict: it rejects a prefix, a shortened dotted quad and junk, so
99 # no separate validation is needed. It dies rather than returning undef on some
100 # platforms, hence the eval.
101 28935 ▶sub _pton {
Subroutine _pton: called CC=2 SCAR=6.9
102 28935 my ($family, $ip) = @_;
103 28935 my $packed = eval { inet_pton($family, $ip) };
104 28935 ▶ return undef unless defined $packed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed 3 28932 28935
105 28932 return $packed;
106 }
107  
108 7 1;
109  
110 __END__
« VPNDetection/Cache.pm ↑ Summary VPNDetection/Database.pm »