| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # return binary output for ipv6 addresses | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 35 | ▶ | $binary = unpack("B*", Socket::inet_pton(AF_INET6, "$ipv6")) if $ipv6; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | my ($ipv4, $ipv6, $cidr, $mask) = Mnet::IP::parse($address); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 9 | ▶ | return undef if $ipv4 and $ipv6; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ipv4 and $ipv6
MC/DC0%0/2
$ipv4 and $ipv6
$ipv4
$ipv6
Truth table66%2/3
A and B
A= $ipv4B= $ipv6
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 9 | ▶ | return $ipv4 // $ipv6; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$ipv4 // $ipv6
MC/DC100%2/2
$ipv4 // $ipv6
$ipv4
$ipv6
Truth table100%3/3
A // B
A= $ipv4B= $ipv6
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ipv4 and $cidr
MC/DC100%2/2
$ipv4 and $cidr
$ipv4
$cidr
Truth table100%3/3
A and B
A= $ipv4B= $cidr
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
$ipv6 and $cidr
$ipv6
$cidr
Truth table100%3/3
A and B
A= $ipv6B= $cidr
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 232 | ▶ | $mask = $1 if $address =~ s/\/(\S*)$//; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 65 | ▶ | return @error if $address =~ /\./ and defined $cidr and $cidr !~ 96; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$address =~ /\./u and defined $cidr and not $cidr =~ /96/u
$address =~ /\./u and defined $cidr
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
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 63 | ▶ | $ipv6 = $address if Socket::inet_pton(AF_INET6, $address); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # ipv4 addresses have dots | ||||||||||||||||||||||||||||||||||||||||||
| 310 | } elsif ($address =~ /\./) { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 62 | ▶ | $ipv4 = $address if Socket::inet_pton(AF_INET, $address); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 91 | ▶ | return @error if $ipv4 and $cidr > 32; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ipv4 and $cidr > 32
MC/DC100%2/2
$ipv4 and $cidr > 32
$ipv4
$cidr > 32
Truth table100%3/3
A and B
A= $ipv4B= $cidr > 32
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 90 | ▶ | return @error if $cidr > 128; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 67 | ▶ | return @error if $ipv6 or not Socket::inet_pton(AF_INET, $mask); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ipv6 or not Socket::inet_pton(2, $mask)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 47 | my $binary = unpack("B*", Socket::inet_aton($mask)); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 47 | ▶ | return @error if $binary =~ /0.*1/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not $ipv6 || defined $mask and defined $cidr
$ipv6 || defined $mask
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
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 63 | ▶ | if ($cidr <= 32) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $cidr and defined $mask
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
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 46 | my $binary = unpack("B*", Socket::inet_aton($mask)); | |||||||||||||||||||||||||||||||||||||||||
| 345 | 46 | ▶ | $cidr = length($1) if $binary =~ /^(1*)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
not $ipv6 and defined $cidr and $cidr <= 32
not $ipv6 and defined $cidr
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||