Mnet/IP.pm

filter: Statementstmt 100% 88 / 88 Branchbran 96.1% 50 / 52 Conditioncond 98.1% 52 / 53 MC/DCmcdc 93.3% 28 / 30 Subroutinesub 100% 14 / 14 Podpod 77.7% 7 / 9 totaltotal 97.1% 239 / 246 CC 47 SCAR 26.5
CC
47
Cov
98%
parse
27.0
network
6.0
ip
5.1
CRAP 14.2
« Mnet/Expect/Cli.pm ↑ Summary Mnet/Report/Table.pm »
1 package Mnet::IP;
2  
3 =head1 NAME
4  
5 Mnet::IP - Parse ipv4 and ipv6 addresses
6  
7 =head1 SYNOPSIS
8  
9 # refer to Mnet::IP::parse for all valid address input formats
10 $address = "127.0.0.1"
11 $address = "127.0.0.1/32"
12 $address = "127.0.0.1/255.255.255.255"
13 $address = "::1"
14 $address = "::1/128"
15 $address = "::127.0.0.1/96"
16 $address = "/32"
17 $address = "/255.255.255.255"
18  
19 # return sting of binary digits for input ipv4 or ipv6 address
20 $binary = Mnet::IP::binary($address)
21  
22 # return cidr from input address, /mask is converted
23 $cidr = Mnet::IP::cidr($address)
24  
25 # return ipv4 or ipv6 portion of address, removing any /cidr or /mask
26 $ip = Mnet::IP::ip($address)
27  
28 # return ipv4 portion of input address, removing any /cidr or /mask
29 $ipv4 = Mnet::IP::ipv4($address)
30  
31 # return ipv6 portion of input address, removing any /cidr
32 $ipv6 = Mnet::IP::ipv6($address)
33  
34 # return mask from input address, /cidr is converted
35 $mask = Mnet::IP::mask($address)
36  
37 # return network ip/cidr for input address
38 $network = Mnet::IP::network($address)
39  
40 # return ipv4, ipv6, cidr and mask values for input address
41 ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address)
42  
43 # return wildcard mask for /mask or /cidr of input address
44 $wildcard = Mnet::IP::wildcard($address)
45  
46 =head1 DESCRIPTION
47  
48 The following terms are used in this module:
49  
50 address ipv4 or ipv6 address, see below
51 cidr count of network bits, such as 32 for an ipv4 host
52 ip ipv4 or ipv6 address
53 ipv4 ipv4 address, see below
54 ipv6 ipv6 address, see below
55 network network ip/cidr, such as 192.168.1.0/24
56 mask ipv4 dotted decimal mask, such as 255.255.255.0
57 wildcard ipv4 acl wildcard mask, such as 0.0.0.255
58  
59 Refer to the parse function below for examples of valid address values.
60  
61 The Socket::inet_pton function used in this module requires perl 5.12 or newer.
62  
63 =cut
64  
65 # required modules
66 # Socket::inet_pton requires perl 5.12
67 # update Mnet.pm and this perldoc if changed
68 225 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 225 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 225 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 225 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 225 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74  
75  
76 36 ▶sub binary {
Subroutine binary: called CC=4 SCAR=13.9
Pod documented
77  
78 =head2 $binary = Mnet::IP::binary($address)
79  
80 Return binary string of zeros and ones for a valid input ipv4 or ipv6 address,
81 or undefined.
82  
83 =cut
84  
85 # read input address
86 36 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
351
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
87  
88 # init binary output value
89 35 my $binary = undef;
90  
91 # parse input ipv4 or ipv6 address
92 35 my ($ipv4, $ipv6) = Mnet::IP::parse($address);
93  
94 # return binary output for ipv4 addresses
95 # inet_pton failed on linux 5.12 and 5.14 tests, returning ipv6 binary
96 35 ▶ $binary = unpack("B*", Socket::inet_aton("$ipv4")) if $ipv4;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 16 19 35
97  
98 # return binary output for ipv6 addresses
99 35 ▶ $binary = unpack("B*", Socket::inet_pton(AF_INET6, "$ipv6")) if $ipv6;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv6 14 21 35
100  
101 # finished binary function, return binary
102 35 return $binary;
103  
104 }
105  
106  
107  
108 12 ▶sub cidr {
Subroutine cidr: called CC=2 SCAR=6.9
Pod documented
109  
110 =head2 $cidr = Mnet::IP::cidr($address)
111  
112 Return cidr from a valid input address containing a /cidr or /mask, or
113 undefined if no /cidr or /mask was specified.
114  
115 =cut
116  
117 # read and parse address, return cidr value
118 12 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
111
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
119 11 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
120 11 return $cidr;
121  
122 }
123  
124  
125  
126 10 ▶sub ip {
Subroutine ip: called CC=5 SCAR=16.2
Pod documented
127  
128 =head2 $boolean = Mnet::IP::ip($address)
129  
130 Return ipv4 or ipv6 portion from a valid input address, or undefined.
131  
132 =cut
133  
134 # read and parse address, return ipv4 or ipv6 if present
135 10 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
91
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
136 9 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
137 9 ▶ return undef if $ipv4 and $ipv6;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 and $ipv6 0 9 9
Condition66%2/3
$ipv4 and $ipv6
!ll&&!rl&&r
630
MC/DC0%0/2
$ipv4 and $ipv6
$ipv4 $ipv6
Truth table66%2/3
A and B
A= $ipv4B= $ipv6
ABresult
0X0
100
111
138 9 ▶ return $ipv4 // $ipv6;
Condition100%3/3
$ipv4 // $ipv6
l!l&&r!l&&!r
342
MC/DC100%2/2
$ipv4 // $ipv6
$ipv4 $ipv6
Truth table100%3/3
A // B
A= $ipv4B= $ipv6
ABresult
1X1
011
000
139  
140 }
141  
142  
143  
144 7 ▶sub ipv4 {
Subroutine ipv4: called CC=2 SCAR=6.9
Pod undocumented
145  
146 =head2 $ipv4 = Mnet::IP::v4($address)
147  
148 Return ipv4 portion from a valid input the address, or undefined.
149  
150  
151  
152 =cut
153  
154 # read and parse address, return ipv4
155 7 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
61
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
156 6 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
157 6 return $ipv4;
158  
159 }
160  
161  
162  
163 9 ▶sub ipv6 {
Subroutine ipv6: called CC=2 SCAR=6.9
Pod undocumented
164  
165 =head2 $boolean = Mnet::IP::v6($address)
166  
167 Return ipv6 fportion from a valid input address, or undefined.
168  
169 =cut
170  
171 # read and parse address, return ipv6
172 9 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
81
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
173 8 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
174 8 return $ipv6;
175  
176 }
177  
178  
179  
180 12 ▶sub mask {
Subroutine mask: called CC=2 SCAR=6.9
Pod documented
181  
182 =head2 $mask = Mnet::IP::mask($address)
183  
184 Return dotted decimal mask from a valid input ipv4 address containing a /cidr
185 or /mask, or undefined.
186  
187 =cut
188  
189 # read and parse address, return mask
190 12 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
111
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
191 11 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
192 11 return $mask
193  
194 }
195  
196  
197  
198 22 ▶sub network {
Subroutine network: called CC=6 SCAR=17.9
Pod documented
199  
200 =head2 $network = Mnet::IP::network($address)
201  
202 Return ipv4/cidr or ipv6/cidr network address from a valid input ipv4/cidr,
203 ipv4/mask, or ipv6/cidr address, or undefined.
204  
205 =cut
206  
207 # read input address
208 22 my $address = shift;
209  
210 # parse input ipv4 or ipv6 address
211 # note that /cidr is calculated from input ipv4 /mask
212 22 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
213  
214 # init output ip/cidr network address
215 22 my $network = undef;
216  
217 # calculate output ipv4/cidr network
218 # convert ipv4 to binary, zero-out masked bits at end based on cidr
219 22 ▶ if ($ipv4 and $cidr) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ipv4 and $cidr) { } 7 15 22
elsif ($ipv6 and $cidr) { } 9 10 19
Condition100%3/3
$ipv4 and $cidr
!ll&&!rl&&r
1327
MC/DC100%2/2
$ipv4 and $cidr
$ipv4 $cidr
Truth table100%3/3
A and B
A= $ipv4B= $cidr
ABresult
0X0
100
111
220 7 my $binary = substr(Mnet::IP::binary($ipv4), 0, $cidr);
221 7 $binary .= "0" x (32 - $cidr);
222 7 $network = join('.', unpack("C4", pack("B*", $binary)));
223  
224 # calculate output ipv6/cidr network
225 ▶ } elsif ($ipv6 and $cidr) {
Condition100%3/3
$ipv6 and $cidr
!ll&&!rl&&r
429
MC/DC100%2/2
$ipv6 and $cidr
$ipv6 $cidr
Truth table100%3/3
A and B
A= $ipv6B= $cidr
ABresult
0X0
100
111
226  
227 # convert ipv6 to binary, zero-out masked bits at end based on cidr
228 9 my $binary = substr(Mnet::IP::binary($ipv6), 0, $cidr);
229 9 $binary .= "0" x (128 - $cidr);
230  
231 # convert ipv6 hextets to hex
232 # remove extra leading zeros in each hextet
233 # remove extra trailing colon when done with look
234 9 ▶ while ($binary =~ s/^(\d{16})//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binary =~ s/^(\d{16})//u) 72 9 81
235 72 my $hextet = unpack("H*", pack("B*", $1));
236 72 $hextet =~ s/^0+(\d)/$1/;
237 72 $network .= "$hextet:";
238 }
239 9 $network =~ s/:$//;
240  
241 # add double colon to eliminate extra hextets with only zeroes
242 9 $network =~ s/(^|:)0:(0:)+/::/;
243 9 $network =~ s/(:0)+:0$/::/;
244  
245 # zero in first and/or last hextet can be removed
246 9 $network =~ s/(^0:|:0$)/:/g;
247  
248 # finished setting output network address
249 }
250  
251 # finished network function
252 22 return $network;
253  
254 }
255  
256  
257  
258 234 ▶sub parse {
Subroutine parse: called CC=27 SCAR=33.0
Pod documented
259  
260 =head2 ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address)
261  
262 Parse input address and return separate ipv4, ipv6, cidr and mask values.
263  
264 The following are examples of valid ipv4 addresses:
265  
266 127.0.0.1
267 127.0.0.1/32
268 127.0.0.1/255.255.255.255
269  
270 The following are examples of valid ipv6 addresses:
271  
272 ::1
273 ::1/128
274  
275 The following are examples of valid ipv6 addresses having embedded dotted
276 decimal text ipv4 addresses, as per rfc 6502 section 2:
277  
278 ::127.0.0.1
279 ::127.0.0.1/96
280  
281 The following are examples of valid /cidr and /mask values by themselves:
282  
283 /32
284 /255.255.255.255
285  
286 All output values are undefined if there were any errors.
287  
288 =cut
289  
290 # read input address
291 234 my $address = shift;
292  
293 # init outputs, also undefined outputs for errors
294 # return an error if input address not defined
295 234 my ($ipv4, $ipv6, $cidr, $mask) = ();
296 234 my @error = (undef, undef, undef, undef);
297 234 ▶ return @error if not defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 2 232 234
298  
299 # parse optional /cidr or /mask from end of address, validate them later
300 232 ▶ $cidr = $1 if $address =~ s/\/(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ s[/(\d+)$][]u 94 138 232
301 232 ▶ $mask = $1 if $address =~ s/\/(\S*)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ s[/(\S*)$][]u 69 163 232
302  
303 # ipv6 addresses have colons
304 # error if embedded ipv4 used with invalid /cidr, see rfc 6502 section 2
305 232 ▶ if ($address =~ /:.*:/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($address =~ /:.*:/u) { } 65 167 232
elsif ($address =~ /\./u) { } 62 105 167
elsif ($address =~ /\S/u) { } 9 96 105
306 65 ▶ return @error if $address =~ /\./ and defined $cidr and $cidr !~ 96;
Branch: 1 branch
BranchTrueFalseTotal
if $address =~ /\./u and defined $cidr and not $cidr =~ /96/u 2 63 65
Condition100%6/6
$address =~ /\./u and defined $cidr and not $cidr =~ /96/u
!ll&&!rl&&r
6032
$address =~ /\./u and defined $cidr
!ll&&!rl&&r
50105
MC/DC100%3/3
$address =~ /\./u and defined $cidr and not $cidr =~ /96/u
$address =~ /\./u defined $cidr not $cidr =~ /96/u
Truth table100%4/4
A and B and not(C)
A= $address =~ /\./uB= defined $cidrC= not $cidr =~ /96/u
ABCresult
0XX0
10X0
1100
1111
307 63 ▶ $ipv6 = $address if Socket::inet_pton(AF_INET6, $address);
Branch: 1 branch
BranchTrueFalseTotal
if Socket::inet_pton(10, $address) 53 10 63
308  
309 # ipv4 addresses have dots
310 } elsif ($address =~ /\./) {
311 62 ▶ $ipv4 = $address if Socket::inet_pton(AF_INET, $address);
Branch: 1 branch
BranchTrueFalseTotal
if Socket::inet_pton(2, $address) 56 6 62
312  
313 # otherwise address is invalid
314 } elsif ($address =~ /\S/) {
315 9 return @error;
316 }
317  
318 # return error if /cidr input is invalid
319 # /cidr max value is 32 for ipv4 and 128 for ipv6
320 221 ▶ if (defined $cidr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cidr) 91 130 221
321 91 ▶ return @error if $ipv4 and $cidr > 32;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 and $cidr > 32 1 90 91
Condition100%3/3
$ipv4 and $cidr > 32
!ll&&!rl&&r
70201
MC/DC100%2/2
$ipv4 and $cidr > 32
$ipv4 $cidr > 32
Truth table100%3/3
A and B
A= $ipv4B= $cidr > 32
ABresult
0X0
100
111
322 90 ▶ return @error if $cidr > 128;
Branch: 1 branch
BranchTrueFalseTotal
if $cidr > 128 3 87 90
323 }
324  
325 # return error if /mask set for ipv6 or /mask is invalid
326 # binary version should not have 0's before 1's
327 217 ▶ if (defined $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mask) 67 150 217
328 67 ▶ return @error if $ipv6 or not Socket::inet_pton(AF_INET, $mask);
Branch: 1 branch
BranchTrueFalseTotal
if $ipv6 or not Socket::inet_pton(2, $mask) 20 47 67
Condition100%3/3
$ipv6 or not Socket::inet_pton(2, $mask)
l!l&&r!l&&!r
21847
MC/DC100%2/2
$ipv6 or not Socket::inet_pton(2, $mask)
$ipv6 not Socket::inet_pton(2, $mask)
Truth table100%3/3
A or not(B)
A= $ipv6B= not Socket::inet_pton(2, $mask)
ABresult
1X1
011
000
329 47 my $binary = unpack("B*", Socket::inet_aton($mask));
330 47 ▶ return @error if $binary =~ /0.*1/;
Branch: 1 branch
BranchTrueFalseTotal
if $binary =~ /0.*1/u 1 46 47
331 }
332  
333 # set mask from input ipv4/cidr or ipv4-sized /cidr only
334 # we don't want to try convert ipv6-sized /cidr to a mask
335 196 ▶ if (not $ipv6 and not defined $mask and defined $cidr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ipv6 || defined $mask and defined $cidr) 63 133 196
Condition100%6/6
not $ipv6 || defined $mask and defined $cidr
!ll&&!rl&&r
953863
$ipv6 || defined $mask
l!l&&r!l&&!r
4946101
MC/DC100%3/3
not $ipv6 || defined $mask and defined $cidr
$ipv6 defined $mask defined $cidr
Truth table100%4/4
not(A || B) and C
A= $ipv6B= defined $maskC= defined $cidr
ABCresult
1XX0
01X0
0000
0011
336 63 ▶ if ($cidr <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr <= 32) 55 8 63
337 55 my $binary = '1' x $cidr . '0' x (32 - $cidr);
338 55 $mask = Socket::inet_ntoa(pack("B*", $binary));
339 }
340 }
341  
342 # set cidr from input /mask
343 196 ▶ if (not defined $cidr and defined $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cidr and defined $mask) 46 150 196
Condition100%3/3
not defined $cidr and defined $mask
!ll&&!rl&&r
876346
MC/DC100%2/2
not defined $cidr and defined $mask
not defined $cidr defined $mask
Truth table100%3/3
not(A) and B
A= not defined $cidrB= defined $mask
ABresult
0X0
100
111
344 46 my $binary = unpack("B*", Socket::inet_aton($mask));
345 46 ▶ $cidr = length($1) if $binary =~ /^(1*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $binary =~ /^(1*)/u 46 0 46
346 }
347  
348 # finished _parse function, return outputs
349 196 return ($ipv4, $ipv6, $cidr, $mask);
350  
351 }
352  
353  
354  
355 37 ▶sub wildcard {
Subroutine wildcard: called CC=5 SCAR=16.1
Pod documented
356  
357 =head2 $wildcard = Mnet::IP::wildcard($mask)
358  
359 Return dotted decimal wildcard mask for a valid input ipv4 address contianing a
360 /cidr or /mask, , or undefined.
361  
362 =cut
363  
364 # read and parse address, return wildcard
365 37 ▶ my $address = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
361
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
366 36 my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address);
367 36 my $wildcard = undef;
368 36 ▶ if (not $ipv6 and defined $cidr and $cidr <= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ipv6 and defined $cidr and $cidr <= 32) 27 9 36
Condition100%6/6
not $ipv6 and defined $cidr and $cidr <= 32
!ll&&!rl&&r
7227
not $ipv6 and defined $cidr
!ll&&!rl&&r
3429
MC/DC100%3/3
not $ipv6 and defined $cidr and $cidr <= 32
not $ipv6 defined $cidr $cidr <= 32
Truth table100%4/4
not(A) and B and C
A= not $ipv6B= defined $cidrC= $cidr <= 32
ABCresult
0XX0
10X0
1100
1111
369 27 my $binary = '0' x $cidr . '1' x (32 - $cidr);
370 27 $wildcard = Socket::inet_ntoa(pack("B*", $binary));
371 }
372 36 return $wildcard;
373  
374 }
375  
376  
377  
378 =head1 SEE ALSO
379  
380 L<Mnet>
381  
382 =cut
383  
384  
385 # normal end of package
386 225 1;
387  
« Mnet/Expect/Cli.pm ↑ Summary Mnet/Report/Table.pm »