InternetData/Bogon.pm

filter: Statementstmt 100% 71 / 71 Branchbran 96.4% 27 / 28 Conditioncond 75.0% 12 / 16 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 90.9% 131 / 144 CC 25 SCAR 15.0
CC
25
Cov
96%
is_bogon
8.0
bogon_result
6.0
is_ip
5.4
CRAP 4.5
« InternetData/Error.pm ↑ Summary InternetData/Cache.pm »
1 package InternetData::Bogon;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Mojo::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Socket qw(AF_INET AF_INET6 inet_pton);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use InternetData::Bogons;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use InternetData::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 our $VERSION = '1.15.0';
13  
14 # Whether an address is private, loopback, link-local, documentation, multicast
15 # or otherwise not routable on the public internet, including the IPv6
16 # equivalents and the 6to4 and Teredo ranges that wrap them.
17 #
18 # No database says anything about these, so the client answers them itself and
19 # they never cost a request.
20 27815 ▶sub is_bogon {
Subroutine is_bogon: called CC=8 SCAR=20.8
Pod undocumented
21 27815 my ($ip) = @_;
22 27815 ▶ return 0 unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 1 27814 27815
Condition66%2/3
defined $ip and length $ip
!ll&&!rl&&r
0127814
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
23  
24 27814 $ip = unmapped($ip);
25 27814 my $v6 = index($ip, ':') >= 0;
26 27814 ▶ my $addr = _pton($v6 ? AF_INET6 : AF_INET, $ip);
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 51 27763 27814
27 27814 ▶ return 0 unless defined $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $addr 4 27810 27814
28  
29 27810 ▶ for my $range (@{ $v6 ? _v6() : _v4() }) {
Branch: 1 branch
BranchTrueFalseTotal
$v6 ? : 51 27759 27810
30 445452 ▶ return 1 if ($addr & $range->[1]) eq $range->[0];
Branch: 1 branch
BranchTrueFalseTotal
if ($addr & $range->[1]) eq $range->[0] 78 445374 445452
31 }
32 27732 return 0;
33 }
34  
35 # Whether a string is an IPv4 or IPv6 address, as inet_pton reads one.
36 173 ▶sub is_ip {
Subroutine is_ip: called CC=5 SCAR=16.8
Pod undocumented
37 173 my ($ip) = @_;
38 173 ▶ return 0 unless defined $ip && length $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and length $ip 0 173 173
Condition33%1/3
defined $ip and length $ip
!ll&&!rl&&r
00173
MC/DC0%0/2
defined $ip and length $ip
defined $ip length $ip
Truth table33%1/3
A and B
A= defined $ipB= length $ip
ABresult
0X0
100
111
39 173 ▶ return defined _pton(index($ip, ':') >= 0 ? AF_INET6 : AF_INET, $ip) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined _pton(index($ip, ':') != -1 ? 10 : 2, $ip) ? : 157 16 173
index($ip, ':') != -1 ? : 4 169 173
40 }
41  
42 # The IPv4 address an IPv4-mapped IPv6 address (::ffff:a.b.c.d, in any spelling)
43 # carries, dotted, and any other string as given. A server listening on :: sees
44 # every IPv4 visitor in that form, which read whole is inside ::ffff:0:0/96, so
45 # judging it whole would answer every such visitor locally as a bogon. ::a.b.c.d
46 # is IPv4-compatible rather than mapped, and stays IPv6.
47 83083 ▶sub unmapped {
Subroutine unmapped: called CC=5 SCAR=16.1
Pod undocumented
48 83083 my ($ip) = @_;
49 83083 ▶ return $ip unless defined $ip && index($ip, ':') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip and index($ip, ":") != -1 83007 76 83083
Condition66%2/3
defined $ip and index($ip, ":") != -1
!ll&&!rl&&r
08300776
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
50 76 my $addr = _pton(AF_INET6, $ip);
51 76 ▶ 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" 55 21 76
Condition100%3/3
defined $addr and substr($addr, 0, 12) eq "\0\0\0\0\0\0\0\0\0\0\377\377"
!ll&&!rl&&r
25321
MC/DC100%2/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 table100%3/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
52 21 return join '.', unpack 'C4', substr($addr, 12);
53 }
54  
55 # The answer a bogon gets, in the full shape the API serves on a paid plan: every
56 # flag present and false, every detail object present and empty (Web IP's list
57 # empty), and location, asn, company, abuse, whois and rwhois with every member
58 # empty, hostname empty and hosted_domains 0 with no domains.
59 6 ▶sub bogon_result {
Subroutine bogon_result: called CC=6 SCAR=17.9
Pod undocumented
60 6 my ($ip) = @_;
61 6 my %answer = (ip => $ip, is_bogon => 1, hostname => '', hosted_domains => { total => 0, domains => [] });
62 6 $answer{$_} = 0 for @InternetData::Result::FLAGS;
63 6 ▶ $answer{$_} = $_ eq 'web' ? [] : {} for @InternetData::Result::DETAILS;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'web' ? : 6 90 96
64 6 my %numbers = map { $_ => 1 } qw(latitude longitude accuracy_radius asn);
65 6 for my $object (qw(location asn company abuse whois rwhois)) {
66 36 my $members = $InternetData::Result::MEMBERS{$object};
67 36 ▶ $answer{$object} = { map { $_ => $numbers{$_} ? 0 : '' } @$members };
Branch: 1 branch
BranchTrueFalseTotal
$numbers{$_} ? : 30 294 324
68 }
69 6 $answer{abuse}{validated} = Mojo::JSON::false;
70 6 return InternetData::Result->_new(\%answer, {});
71 }
72  
73 # Parsed on first use rather than at load: a program that never looks an address
74 # up should not pay for the table.
75 8 my ($v4, $v6);
76  
77 27759 ▶sub _v4 {
Subroutine _v4: called CC=2 SCAR=6.9
78 27759 ▶ $v4 ||= [map { _range(AF_INET, $_, 4) } @InternetData::Bogons::V4];
Condition100%2/2
$v4 ||= [map({_range(2, $_, 4);} @InternetData::Bogons::V4)]
l!l
277554
MC/DC100%1/1
$v4 ||= [map({_range(2, $_, 4);} @InternetData::Bogons::V4)]
$v4
Truth table100%2/2
A
A= $v4
Aresult
11
00
79 27759 return $v4;
80 }
81  
82 51 ▶sub _v6 {
Subroutine _v6: called CC=2 SCAR=6.9
83 51 ▶ $v6 ||= [map { _range(AF_INET6, $_, 16) } @InternetData::Bogons::V6];
Condition100%2/2
$v6 ||= [map({_range(10, $_, 16);} @InternetData::Bogons::V6)]
l!l
483
MC/DC100%1/1
$v6 ||= [map({_range(10, $_, 16);} @InternetData::Bogons::V6)]
$v6
Truth table100%2/2
A
A= $v6
Aresult
11
00
84 51 return $v6;
85 }
86  
87 # A range is [masked network bytes, mask bytes], both packed strings, so
88 # membership is one bitwise string AND and one string comparison. Perl has no
89 # 128 bit integer, and it does not need one: `&` on two byte strings is a
90 # per-byte AND, so a /36 IPv6 mask is just a 16 byte string.
91 #
92 # That behavior is why no file in this distribution says `use v5.28` or later:
93 # those enable the `bitwise` feature, under which `&` becomes numeric-only, both
94 # operands numify to 0, and EVERY address would match EVERY range. The isBogon
95 # corpus is what catches it.
96 256 ▶sub _range {
Subroutine _range: called CC=1 SCAR=0.0
97 256 my ($family, $cidr, $width) = @_;
98 256 my ($net, $bits) = split m{/}, $cidr, 2;
99 256 my $mask = _mask($bits, $width);
100 256 return [inet_pton($family, $net) & $mask, $mask];
101 }
102  
103 256 ▶sub _mask {
Subroutine _mask: called CC=2 SCAR=6.9
104 256 my ($bits, $width) = @_;
105 256 my $mask = "\xff" x int($bits / 8);
106 256 my $partial = $bits % 8;
107 256 ▶ $mask .= chr((0xff << (8 - $partial)) & 0xff) if $partial;
Branch: 1 branch
BranchTrueFalseTotal
if $partial 104 152 256
108 256 return $mask . "\x00" x ($width - length $mask);
109 }
110  
111 # inet_pton is strict: it rejects a prefix, a shortened dotted quad and junk, so
112 # no separate validation is needed. It dies rather than returning undef on some
113 # platforms, hence the eval.
114 28063 ▶sub _pton {
Subroutine _pton: called CC=2 SCAR=6.9
115 28063 my ($family, $ip) = @_;
116 28063 my $packed = eval { inet_pton($family, $ip) };
117 28063 ▶ return undef unless defined $packed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $packed 22 28041 28063
118 28041 return $packed;
119 }
120  
121 8 1;
122  
123 __END__
« InternetData/Error.pm ↑ Summary InternetData/Cache.pm »