blib/lib/Net/IPAM/Util.pm

filter: Statementstmt 100% 84 / 84 Branchbran 95.0% 38 / 40 Conditioncond 81.8% 9 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 95.0% 155 / 163 CC 25 SCAR 14.9
CC
25
Cov
97%
_inet_pton_v6_pp
10.0
_inet_ntop_v6_pp
4.0
incr_n
4.0
CRAP 4.4
« blib/lib/Net/IPAM/IP.pm ↑ Summary Build »
1 package Net::IPAM::Util;
2  
3 11 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 our @EXPORT_OK = qw(incr_n decr_n inet_ntop_pp inet_pton_pp);
13  
14 =head1 NAME
15  
16 Net::IPAM::Util - A selection of general utility subroutines for Net::IPAM
17  
18 =head1 SYNOPSIS
19  
20 use Net::IPAM::Util qw(incr_n inet_ntop_pp inet_pton_pp);
21  
22 $n = incr_n("\x0a\x00\x00\x01"); # 10.0.0.2
23 $n = incr_n( pack( 'n8', 0x2001, 0xdb8, 0, 0, 0, 0, 0, 1 ) ); # 2001:db8::2
24  
25 $n = decr_n("\x0a\x00\x00\x01"); # 10.0.0.0
26 $n = decr_n( pack( 'n8', 0x2001, 0xdb8, 0, 0, 0, 0, 0, 1 ) ); # 2001:db8::
27  
28 $n = inet_pton_pp( AF_INET6, '2001:db8::fe1' );
29 say inet_ntop_pp( AF_INET, "\x0a\x00\x00\x01" ); # 10.0.0.1
30  
31 =cut
32  
33 =head1 FUNCTIONS
34  
35 =head2 $address_plusplus = incr_n( $address )
36  
37 Increment a packed IPv4 or IPv6 address in network byte order. Returns undef on overflow.
38  
39 This increment function is needed in L<Net::IPAM::IP> and L<Net::IPAM::Block> for transparent handling
40 of IPv4 and IPv6 addresses and blocks.
41  
42 No need for L<Math::BigInt>, this pure perl algorithm works for all uint_n in network byte order,
43 where n is a multiple of 32: uint_32, uint_64, uint_96, uint_128, ...
44  
45 =cut
46  
47 sub incr_n {
48 13 ▶ my $n = shift // Carp::croak("missing argument");
Subroutine incr_n: called CC=4 SCAR=13.9
Pod documented
Condition66%2/3
shift() // Carp::croak("missing argument")
l!l&&r!l&&!r
1210
MC/DC0%0/2
shift() // Carp::croak("missing argument")
shift() Carp::croak("missing argument")
Truth table66%2/3
A // B
A= shift()B= Carp::croak("missing argument")
ABresult
1X1
011
000
49  
50 # split in individual 32 bit unsigned ints in network byte order
51 12 my @N = unpack( 'N*', $n );
52  
53 # start at least significant N
54 12 my $i = $#N;
55  
56 # carry?
57 12 ▶ while ( $N[$i] == 0xffff_ffff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N[$i] == 4294967295) 14 8 22
58  
59 # OVERFLOW, it's already the most significant N
60 14 ▶ return if $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 4 10 14
61  
62 # set this N to zero: 0xffff_ffff + 1 = 0x0000_0000 + carry
63 10 $N[$i] = 0;
64  
65 # carry on to next more significant N
66 10 $i--;
67 }
68  
69 # incr this N
70 8 $N[$i]++;
71  
72 # pack again the individual 32 bit integers in network byte order to one byte string
73 8 return pack( 'N*', @N );
74 }
75  
76 =head2 $address_minusminus = decr_n( $address )
77  
78 Decrement a packed IPv4 or IPv6 address in network byte order. Returns undef on underflow.
79  
80 This decrement function is needed in L<Net::IPAM::IP> and L<Net::IPAM::Block> for transparent handling
81 of IPv4 and IPv6 addresses and blocks.
82  
83 No need for L<Math::BigInt>, this pure perl algorithm works for all uint_n in network byte order,
84 where n is a multiple of 32: uint_32, uint_64, uint_96, uint_128, ...
85  
86 =cut
87  
88 sub decr_n {
89 12 ▶ my $n = shift // Carp::croak("missing argument");
Subroutine decr_n: called CC=4 SCAR=13.9
Pod documented
Condition66%2/3
shift() // Carp::croak("missing argument")
l!l&&r!l&&!r
1110
MC/DC0%0/2
shift() // Carp::croak("missing argument")
shift() Carp::croak("missing argument")
Truth table66%2/3
A // B
A= shift()B= Carp::croak("missing argument")
ABresult
1X1
011
000
90  
91 # split in individual 32 bit unsigned ints in network byte order
92 11 my @N = unpack( 'N*', $n );
93  
94 # start at least significant N
95 11 my $i = $#N;
96  
97 # carry?
98 11 ▶ while ( $N[$i] == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N[$i] == 0) 14 7 21
99  
100 # UNDERFLOW, it's already the most significant N
101 14 ▶ return if $i == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 4 10 14
102  
103 # set this N to ffff_ffff: 0 - 1 = 0xffff_ffff + carry
104 10 $N[$i] = 0xffff_ffff;
105  
106 # carry on to next more significant N
107 10 $i--;
108 }
109  
110 # decr this N
111 7 $N[$i]--;
112  
113 # pack again the individual 32 bit integers in network byte order to one byte string
114 7 return pack( 'N*', @N );
115 }
116  
117 =head2 $string = inet_ntop_pp( $family, $address )
118  
119 A pure perl implementation for (buggy) Socket::inet_ntop.
120  
121 Takes an address family (C<AF_INET> or C<AF_INET6>) and
122 a packed binary address structure and translates it
123 into a human-readable textual representation of the address.
124  
125 =cut
126  
127 sub inet_ntop_pp {
128  
129 # modify @_ = (AF_INETx, $ip) => @_ = ($ip)
130 19 ▶ my $v = shift;
Subroutine inet_ntop_pp: called CC=2 SCAR=6.9
Pod documented
131 19 ▶ goto &_inet_ntop_v4_pp if $v == Socket::AF_INET;
Branch: 1 branch
BranchTrueFalseTotal
if $v == 2 8 11 19
132 11 goto &_inet_ntop_v6_pp;
133 }
134  
135 =head2 $address = inet_pton_pp( $family, $string )
136  
137 A pure perl implementation for (buggy) Socket::inet_pton.
138  
139 Takes an address family (C<AF_INET> or C<AF_INET6>) and a string
140 containing a textual representation of an address in that family and
141 translates that to an packed binary address structure.
142  
143 =cut
144  
145 sub inet_pton_pp {
146  
147 # modify @_ = (AF_INETx, $ip) => @_ = ($ip)
148 78 ▶ my $v = shift;
Subroutine inet_pton_pp: called CC=3 SCAR=11.0
Pod documented
149 78 ▶ goto &_inet_pton_v4_pp if $v == Socket::AF_INET;
Branch: 1 branch
BranchTrueFalseTotal
if $v == 2 20 58 78
150 58 ▶ goto &_inet_pton_v4in6_pp if $_[0] =~ m/^::ffff:/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^::ffff:/i 10 48 58
151 48 goto &_inet_pton_v6_pp;
152 }
153  
154 # easy peasy
155 sub _inet_ntop_v4_pp {
156 8 ▶ return if length( $_[0] ) != 4;
Subroutine _inet_ntop_v4_pp: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] != 4 0 8 8
157 8 return join( '.', unpack( 'C4', $_[0] ) );
158 }
159  
160 # (1) Hexadecimal digits are expressed as lower-case letters.
161 # For example, 2001:db8::1 is preferred over 2001:DB8::1.
162 #
163 # (2) Leading zeros in each 16-bit field are suppressed.
164 # For example, 2001:0db8::0001 is rendered as 2001:db8::1,
165 # though any all-zero field that is explicitly presented is rendered as 0.
166 #
167 # (3) Representations are shortened as much as possible.
168 # The longest sequence of consecutive all-zero fields is replaced with double-colon.
169 # If there are multiple longest runs of all-zero fields, then it is the leftmost that is compressed.
170 # E.g., 2001:db8:0:0:1:0:0:1 is rendered as 2001:db8::1:0:0:1 rather than as 2001:db8:0:0:1::1.
171 #
172 # (4) "::" is not used to shorten just a single 0 field.
173 # For example, 2001:db8:0:0:0:0:2:1 is shortened to 2001:db8::2:1,
174 # but 2001:db8:0000:1:1:1:1:1 is rendered as 2001:db8:0:1:1:1:1:1.
175 #
176 sub _inet_ntop_v6_pp {
177 11 ▶ my $n = shift;
Subroutine _inet_ntop_v6_pp: called CC=4 SCAR=13.9
178 11 ▶ return if length($n) != 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $n != 16 0 11 11
179  
180 # expand binary to hex, lower case, rule (1), leading zeroes squashed
181 # add : at left and right for symmetric squashing algo, see below
182 # :2001:db8:85a3:0:0:8a2e:370:7334:
183 11 my $ip = sprintf( ':%x:%x:%x:%x:%x:%x:%x:%x:', unpack( 'n8', $n ) );
184  
185 # rule (3,4) # squash the longest sequence of consecutive all-zero fields
186 # e.g. :0:0: (?!not followed) :0\1
187 11 $ip =~ s/(:0[:0]+:) (?! .+ :0\1)/::/x;
188  
189 11 ▶ $ip =~ s/^:// unless $ip =~ /^::/; # trim additional left
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /^::/ 8 3 11
190 11 ▶ $ip =~ s/:$// unless $ip =~ /::$/; # trim additional right
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /::$/ 9 2 11
191 11 return $ip;
192 }
193  
194 sub _inet_pton_v4_pp {
195  
196 # 'C' may overflow for values > 255, check below
197 11 ▶ no warnings qw(pack numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
198 30 ▶ my $n = pack( 'C4', split( /\./, $_[0] ) );
Subroutine _inet_pton_v4_pp: called CC=2 SCAR=6.9
199  
200 # unpack(pack...) must be idempotent
201 # check for overflow errors or leading zeroes
202 30 ▶ return unless $_[0] eq join( '.', unpack( 'C4', $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq join(".", unpack("C4", $n)) 16 14 30
203  
204 14 return $n;
205 }
206  
207 sub _inet_pton_v4in6_pp {
208 # skip ::ffff:
209 10 ▶ my $n = _inet_pton_v4_pp(substr($_[0], 7)) // return;
Subroutine _inet_pton_v4in6_pp: called CC=2 SCAR=6.9
Condition100%2/2
_inet_pton_v4_pp(substr($_[0], 7)) // (return)
l!l
46
MC/DC100%1/1
_inet_pton_v4_pp(substr($_[0], 7)) // (return)
_inet_pton_v4_pp(substr($_[0], 7))
Truth table100%2/2
A
A= _inet_pton_v4_pp(substr($_[0], 7))
Aresult
11
00
210 # back ::ffff:
211 4 return pack('n6', 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff) . $n;
212 }
213  
214 sub _inet_pton_v6_pp {
215 48 ▶ my $ip = shift;
Subroutine _inet_pton_v6_pp: called CC=10 SCAR=23.0
216  
217 48 ▶ return if $ip =~ m/[^a-fA-F0-9:]/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /[^a-fA-F0-9:]/ 2 46 48
218 46 ▶ return if $ip =~ m/:::/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /:::/ 4 42 46
219  
220 # starts with just one colon: :cafe...
221 42 ▶ return if $ip =~ m/^:[^:]/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /^:[^:]/ 2 40 42
222  
223 # ends with just one colon: ..:cafe:affe:
224 40 ▶ return if $ip =~ m/[^:]:$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /[^:]:$/ 2 38 40
225  
226 38 my $col_count = $ip =~ tr/://;
227 38 my $dbl_col_count = $ip =~ s/::/::/g;
228  
229 38 ▶ return if $col_count > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $col_count > 7 4 34 38
230 34 ▶ return if $dbl_col_count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $dbl_col_count > 1 6 28 34
231 28 ▶ return if $dbl_col_count == 0 && $col_count != 7;
Branch: 1 branch
BranchTrueFalseTotal
if $dbl_col_count == 0 and $col_count != 7 4 24 28
Condition100%3/3
$dbl_col_count == 0 and $col_count != 7
!ll&&!rl&&r
2224
MC/DC100%2/2
$dbl_col_count == 0 and $col_count != 7
$dbl_col_count == 0 $col_count != 7
Truth table100%3/3
A and B
A= $dbl_col_count == 0B= $col_count != 7
ABresult
0X0
100
111
232  
233 # normalize for splitting, prepend or append 0
234 24 $ip =~ s/^:: /0::/x;
235 24 $ip =~ s/ ::$/::0/x;
236  
237 # expand ::
238 24 my $expand_dbl_col = ':0' x ( 8 - $col_count ) . ':';
239 24 $ip =~ s/::/$expand_dbl_col/;
240  
241 24 my @hextets = split( /:/, $ip );
242 24 ▶ return if grep { length > 4 } @hextets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {length $_ > 4;} @hextets 2 22 24
243  
244 22 my $n = pack( 'n8', map { hex } @hextets );
245 22 return $n;
246 }
247  
248 =head1 AUTHOR
249  
250 Karl Gaissmaier, C<< <karl.gaissmaier(at)uni-ulm.de> >>
251  
252 =head1 BUGS
253  
254 Please report any bugs or feature requests to C<bug-net-ipam-util at rt.cpan.org>, or through
255 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-IPAM-Util>.
256 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
257  
258 =head1 SUPPORT
259  
260 You can find documentation for this module with the perldoc command.
261  
262 perldoc Net::IPAM::Util
263  
264 You can also look for information at:
265  
266 =over 4
267  
268 =item * on github
269  
270 TODO
271  
272 =back
273  
274 =head1 SEE ALSO
275  
276 L<Net::IPAM::IP>
277 L<Net::IPAM::Block>
278 L<Net::IPAM::Tree>
279  
280 =head1 LICENSE AND COPYRIGHT
281  
282 This software is copyright (c) 2020-2022 by Karl Gaissmaier.
283  
284 This is free software; you can redistribute it and/or modify it under
285 the same terms as the Perl 5 programming language system itself.
286  
287 =cut
288  
289 11 1; # End of Net::IPAM::Util
« blib/lib/Net/IPAM/IP.pm ↑ Summary Build »