blib/lib/Ereshkigal/IP.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 88.8% 32 / 36 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 95.0% 134 / 141 CC 24 SCAR 23.8
CC
24
Cov
94%
normalize_cidr
16.1
normalize_ip
9.1
BEGIN
1.0
CRAP 10.8
« blib/lib/Ereshkigal/App/Command.pm ↑ Summary blib/lib/Ereshkigal/App/Command/add.pm »
1 package Ereshkigal::IP;
2  
3 19 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Regexp::IPv4 qw( $IPv4_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use Regexp::IPv6 qw( $IPv6_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Socket qw( AF_INET6 inet_ntop inet_pton );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =pod
12  
13 =head1 NAME
14  
15 Ereshkigal::IP - Exportable IP validation and normalization helper shared by the Ereshkigal modules.
16  
17 =head1 VERSION
18  
19 Version 0.0.1
20  
21 =cut
22  
23 19 our $VERSION = '0.0.1';
24  
25 19 our @EXPORT_OK = qw( normalize_ip normalize_cidr );
26  
27 =head1 SYNOPSIS
28  
29 use Ereshkigal::IP qw( normalize_ip normalize_cidr );
30  
31 my $ip = normalize_ip('2001:0DB8:0000:0000:0000:0000:0000:0001');
32 # $ip is now '2001:db8::1'
33  
34 if ( !defined( normalize_ip($raw_ip) ) ) {
35 die( '"' . $raw_ip . '" does not appear to be an IPv4 or IPv6 IP' );
36 }
37  
38 my $cidr = normalize_cidr('1.2.3.4/24');
39 # $cidr is now '1.2.3.0/24' with the host bits masked off
40  
41 =head1 DESCRIPTION
42  
43 This holds the C<normalize_ip> and C<normalize_cidr> subs used for validating
44 IPs and CIDR ranges and reducing them to a single canonical string form, so
45 variant spellings of the same IP, most notably IPv6 long form vs short form
46 as well as case, cannot be mistaken for differing IPs. C<normalize_cidr>
47 additionally masks the host bits off, so C<1.2.3.4/24> and C<1.2.3.0/24> are
48 one range rather than two. Anything unparseable comes back as undef, letting
49 garbage be bounced at the point of entry instead of being passed along for
50 something further down to bounce.
51  
52 =head1 EXPORTS
53  
54 Nothing is exported by default. L</normalize_ip> and L</normalize_cidr> are
55 available via C<@EXPORT_OK>.
56  
57 =head1 FUNCTIONS
58  
59 =head2 normalize_ip
60  
61 Returns the canonical string form of the passed IP. If it does not validate
62 as either an IPv4 or IPv6 IP, undef is returned. undef and refs also return
63 undef.
64  
65 my $ip = normalize_ip($raw_ip);
66  
67 Validation is done via L<Regexp::IPv4> and L<Regexp::IPv6>, the same as
68 L<Net::Firewall::BlockerHelper> uses, so anything accepted here is also
69 acceptable to the backends. On top of that IPv4 with leading zero octets,
70 such as 010.0.0.1, which the regex permits, is explicitly refused rather
71 than the octal vs decimal ambiguity being guessed at.
72  
73 Only IPv6 IPs the regex has already validated are handed to inet_pton and
74 inet_ntop, which are used purely for reducing them to the canonical form.
75 IPv4 IPs that validate are already in canonical form and are returned as is.
76  
77 =cut
78  
79 41 ▶sub normalize_ip {
Subroutine normalize_ip: called CC=9 SCAR=22.1
Pod documented
80 41 my ($ip) = @_;
81  
82 41 ▶ if ( !defined($ip) || ref($ip) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ip or ref $ip ne "") 2 39 41
Condition100%3/3
not defined $ip or ref $ip ne ""
l!l&&r!l&&!r
1139
MC/DC100%2/2
not defined $ip or ref $ip ne ""
not defined $ip ref $ip ne ""
Truth table100%3/3
not(A) or B
A= not defined $ipB= ref $ip ne ""
ABresult
1X1
011
000
83 2 return undef;
84 }
85  
86 39 ▶ if ( $ip =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\A$Ereshkigal::IP::IPv4_re\z/) 12 27 39
87 # the regex permits leading zero octets, but whether those are octal
88 # or decimal depends on what is reading them, so they are refused
89 # rather than guessed at
90 12 foreach my $octet ( split( /\./, $ip ) ) {
91 39 ▶ if ( $octet =~ /\A0[0-9]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octet =~ /\A0[0-9]/) 3 36 39
92 3 return undef;
93 }
94 }
95 # a valid dotted quad without leading zero octets is already
96 # canonical
97 9 return $ip;
98 } ## end if ( $ip =~ /\A$IPv4_re\z/ )
99  
100 27 ▶ if ( $ip =~ /\A$IPv6_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\A$Ereshkigal::IP::IPv6_re\z/) 13 14 27
101 13 my $packed = inet_pton( AF_INET6, $ip );
102 13 ▶ if ( defined($packed) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packed) 13 0 13
103 13 my $canonical = inet_ntop( AF_INET6, $packed );
104 13 ▶ if ( defined($canonical) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $canonical) 13 0 13
105 13 return $canonical;
106 }
107 }
108 # valid per the regex, but the inet functions could not round trip
109 # it... refusing it beats passing along a form that could not be
110 # canonicalized
111 0 return undef;
112 } ## end if ( $ip =~ /\A$IPv6_re\z/ )
113  
114 14 return undef;
115 } ## end sub normalize_ip
116  
117 =head2 normalize_cidr
118  
119 Returns the canonical string form of the passed IPv4 or IPv6 CIDR range. If it
120 does not validate, undef is returned. undef and refs also return undef.
121  
122 my $cidr = normalize_cidr($raw_cidr);
123  
124 A CIDR is an address, validated the same way L</normalize_ip> validates it,
125 followed by C</> and a prefix length that is a non-negative integer within the
126 range valid for its family, 0 to 32 for IPv4 and 0 to 128 for IPv6. A bare IP
127 with no prefix is refused, as are prefixes with leading zeros, since those are
128 not canonical. The prefix range matches what L<Net::Firewall::BlockerHelper>
129 accepts, so anything accepted here is also acceptable to the backends.
130  
131 The host bits below the prefix are masked off so the network address is
132 returned, meaning C<1.2.3.4/24> and C<1.2.3.0/24> both reduce to C<1.2.3.0/24>
133 and variant spellings of the same range cannot be mistaken for differing
134 ranges. The address portion is canonicalized the same as L</normalize_ip>, so
135 IPv6 long form and case variants reduce to the same short form.
136  
137 =cut
138  
139 27 ▶sub normalize_cidr {
Subroutine normalize_cidr: called CC=16 SCAR=27.8
Pod documented
140 27 my ($cidr) = @_;
141  
142 27 ▶ if ( !defined($cidr) || ref($cidr) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cidr or ref $cidr ne "") 2 25 27
Condition100%3/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
1125
MC/DC100%2/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table100%3/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
143 2 return undef;
144 }
145  
146 # exactly one slash, an address on the left and a run of digits on the
147 # right... a bare IP with no prefix does not match and is refused
148 25 ▶ if ( $cidr !~ m!\A([^/]+)/([0-9]+)\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cidr =~ m[\A([^/]+)/([0-9]+)\z]) 7 18 25
149 7 return undef;
150 }
151 18 my ( $raw_addr, $prefix ) = ( $1, $2 );
152  
153 # a leading zero prefix, such as /024, is not canonical and its intent is
154 # ambiguous, so it is refused rather than guessed at, the same as is done
155 # with leading zero IPv4 octets
156 18 ▶ if ( length($prefix) > 1 && $prefix =~ /\A0/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $prefix > 1 and $prefix =~ /\A0/) 1 17 18
Condition100%3/3
length $prefix > 1 and $prefix =~ /\A0/
!ll&&!rl&&r
4131
MC/DC100%2/2
length $prefix > 1 and $prefix =~ /\A0/
length $prefix > 1 $prefix =~ /\A0/
Truth table100%3/3
A and B
A= length $prefix > 1B= $prefix =~ /\A0/
ABresult
0X0
100
111
157 1 return undef;
158 }
159  
160 # validate and canonicalize the address portion via the same path a bare
161 # IP takes, bouncing leading zero octets and the like here too
162 17 my $addr = normalize_ip($raw_addr);
163 17 ▶ if ( !defined($addr) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $addr) 3 14 17
164 3 return undef;
165 }
166  
167 14 ▶ if ( $addr =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /\A$Ereshkigal::IP::IPv4_re\z/) 7 7 14
168 7 ▶ if ( $prefix > 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix > 32) 1 6 7
169 1 return undef;
170 }
171 6 my @octets = split( /\./, $addr );
172 6 my $int = ( $octets[0] << 24 ) | ( $octets[1] << 16 ) | ( $octets[2] << 8 ) | $octets[3];
173 # for a /0 the shift is by 32, which on Perl's wider ints leaves the
174 # high bits set, so the trailing mask brings it back to 32 bits and a
175 # all zero network
176 6 my $mask = ( 0xFFFFFFFF << ( 32 - $prefix ) ) & 0xFFFFFFFF;
177 6 my $network = $int & $mask;
178 return
179 6 ( ( $network >> 24 ) & 0xFF ) . '.'
180 . ( ( $network >> 16 ) & 0xFF ) . '.'
181 . ( ( $network >> 8 ) & 0xFF ) . '.'
182 . ( $network & 0xFF ) . '/'
183 . $prefix;
184 } ## end if ( $addr =~ /\A$IPv4_re\z/ )
185  
186 # IPv6, already canonicalized and validated by normalize_ip above
187 7 ▶ if ( $prefix > 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix > 128) 1 6 7
188 1 return undef;
189 }
190 6 my $packed = inet_pton( AF_INET6, $addr );
191 6 ▶ if ( !defined($packed) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $packed) 0 6 6
192 0 return undef;
193 }
194 6 my @bytes = unpack( 'C16', $packed );
195 6 my $full_bytes = int( $prefix / 8 );
196 6 my $rem_bits = $prefix % 8;
197 6 ▶ for ( my $index = 0; $index < 16; $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 16) 96 6 102
198 96 ▶ if ( $index < $full_bytes ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index < $full_bytes) { } 32 64 96
elsif ($index == $full_bytes and $rem_bits) { } 1 63 64
199 # wholly within the prefix, kept as is
200 ▶ } elsif ( $index == $full_bytes && $rem_bits ) {
Condition100%3/3
$index == $full_bytes and $rem_bits
!ll&&!rl&&r
5941
MC/DC100%2/2
$index == $full_bytes and $rem_bits
$index == $full_bytes $rem_bits
Truth table100%3/3
A and B
A= $index == $full_bytesB= $rem_bits
ABresult
0X0
100
111
201 # straddles the prefix, keep the leading $rem_bits of this byte
202 1 $bytes[$index] &= ( 0xFF << ( 8 - $rem_bits ) ) & 0xFF;
203 } else {
204 # wholly host bits, zeroed
205 63 $bytes[$index] = 0;
206 }
207 } ## end for ( my $index = 0; $index < 16; $index++ )
208 6 my $canonical = inet_ntop( AF_INET6, pack( 'C16', @bytes ) );
209 6 ▶ if ( !defined($canonical) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $canonical) 0 6 6
210 0 return undef;
211 }
212 6 return $canonical . '/' . $prefix;
213 } ## end sub normalize_cidr
214  
215 19 1;
« blib/lib/Ereshkigal/App/Command.pm ↑ Summary blib/lib/Ereshkigal/App/Command/add.pm »