| 1 | package Net::IP::Util; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our @ISA = qw/Exporter/; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT = qw/isClassAddrA | |||||||||||||||||||||||||||||||||||||||||
| 12 | isClassAddrB | ||||||||||||||||||||||||||||||||||||||||||
| 13 | isClassAddrC | ||||||||||||||||||||||||||||||||||||||||||
| 14 | isClassAddrD | ||||||||||||||||||||||||||||||||||||||||||
| 15 | isClassAddrE | ||||||||||||||||||||||||||||||||||||||||||
| 16 | dec2binIpAddr | ||||||||||||||||||||||||||||||||||||||||||
| 17 | bin2decIpAddr/; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | our @EXPORT_OK = qw/getAddrMaskDefault | |||||||||||||||||||||||||||||||||||||||||
| 20 | getAddrClass | ||||||||||||||||||||||||||||||||||||||||||
| 21 | isValidMask | ||||||||||||||||||||||||||||||||||||||||||
| 22 | extendMaskByBits | ||||||||||||||||||||||||||||||||||||||||||
| 23 | calcSubnet | ||||||||||||||||||||||||||||||||||||||||||
| 24 | calcSubnetCIDR | ||||||||||||||||||||||||||||||||||||||||||
| 25 | calcSubnetExt | ||||||||||||||||||||||||||||||||||||||||||
| 26 | getNetworkAddr/; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | our %EXPORT_TAGS = ('class' => [qw/isClassAddrA isClassAddrB isClassAddrC | |||||||||||||||||||||||||||||||||||||||||
| 29 | isClassAddrD isClassAddrE getAddrClass/], | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 'convert' => [qw/dec2binIpAddr bin2decIpAddr/]); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use constant { 'A' => qr'^0', | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 'B' => qr'^10', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 'C' => qr'^110', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 'D' => qr'^1110', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 'E' => qr'^11110', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 'MASKA' => '255.0.0.0', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 'MASKB' => '255.255.0.0', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 'MASKC' => '255.255.255.0', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 'IPREGEXP' => qr'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub isClassAddrA { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isClassAddrA: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | return 1 if($addr =~ A); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub isClassAddrB { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isClassAddrB: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return 1 if($addr =~ B); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub isClassAddrC { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isClassAddrC: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return 1 if($addr =~ C); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub isClassAddrD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isClassAddrD: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return 1 if($addr =~ D); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub isClassAddrE { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isClassAddrE: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 1 if($addr =~ E); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub getAddrMaskDefault { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getAddrMaskDefault: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | (! isBinIpAddr($addr)) && ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my ($class) = getAddrClass($addr); | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $mask = eval 'MASK'.$class; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return $mask; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub getAddrClass { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getAddrClass: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp(&bin2decIpAddr($addr)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp(&bin2decIpAddr($addr))
isBinIpAddr($addr)
_validateIp(&bin2decIpAddr($addr))
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp(&bin2decIpAddr($addr))
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $class = (($addr =~ A)?'A' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | :($addr =~ B)?'B' | ||||||||||||||||||||||||||||||||||||||||||
| 103 | :($addr =~ C)?'C' | ||||||||||||||||||||||||||||||||||||||||||
| 104 | :($addr =~ D)?'D' | ||||||||||||||||||||||||||||||||||||||||||
| 105 | :($addr =~ E)?'E' | ||||||||||||||||||||||||||||||||||||||||||
| 106 | :undef); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return $class; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub dec2binIpAddr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dec2binIpAddr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | _validateIp($addr); | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my @octets = split /\./,$addr; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | map {$_ = sprintf '%08b', $_} @octets; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | return join '.',@octets; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub bin2decIpAddr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bin2decIpAddr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my @octets = split /\./, $addr; | |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | map {$_ = oct "0b$_"} @octets; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my $decAddr = join '.',@octets; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | _validateIp($decAddr); | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return $decAddr; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub isBinIpAddr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isBinIpAddr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | return 1 if ($addr =~ /^(([0|1]){8}\.){3}([0|1]){8}$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _validateIp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validateIp: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | ($addr =~ /^[01.]*$/) && ($addr = &bin2decIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $validate = sub { map { return 0 if ( $_ > 255);}@_; return 1;}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if(($addr =~ IPREGEXP) && $validate->($1,$2,$3,$4)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$addr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ and &$validate($1, $2, $3, $4)
MC/DC0%0/2
$addr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/ and &$validate($1, $2, $3, $4)
$addr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
&$validate($1, $2, $3, $4)
Truth table0%0/3
A and B
A= $addr =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/B= &$validate($1, $2, $3, $4)
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | croak "$addr is not a valid IP address"; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub isValidMask { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
isValidMask: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | (!isBinIpAddr($addr)) && ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my ($prefix) = ($addr =~ /(.*1)/); | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | ($prefix =~ /0/)?return 0:return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub extendMaskByBits { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extendMaskByBits: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my ($mask,$noBits) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | (! isBinIpAddr($mask)) && ($mask = &dec2binIpAddr($mask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return $mask if(!$noBits); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | croak "Bits $noBits is invalid!!" if ($noBits !~ /^\d+$/ || $noBits > 24); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $noBits =~ /^\d+$/ or $noBits > 24
MC/DC0%0/2
not $noBits =~ /^\d+$/ or $noBits > 24
not $noBits =~ /^\d+$/
$noBits > 24
Truth table0%0/3
not(A) or B
A= not $noBits =~ /^\d+$/B= $noBits > 24
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | croak "Mask $mask invalid!!" if (! isValidMask($mask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | for (1..$noBits) { | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $mask =~ s/0/1/; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | return $mask; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub calcSubnet { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calcSubnet: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my ($addr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp($addr) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp($addr)
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp($addr)
isBinIpAddr($addr)
_validateIp($addr)
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp($addr)
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $mask = getAddrMaskDefault($addr); | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | ($mask && ! isBinIpAddr($mask)) && ($mask = dec2binIpAddr($mask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mask and not isBinIpAddr($mask)
MC/DC0%0/2
$mask and not isBinIpAddr($mask)
$mask
not isBinIpAddr($mask)
Truth table0%0/3
A and not(B)
A= $maskB= not isBinIpAddr($mask)
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $numZeros = ($mask =~ s/0//g) || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$mask =~ s/0//g || 0
MC/DC0%0/1
$mask =~ s/0//g || 0
$mask =~ s/0//g
Truth table0%0/2
A
A= $mask =~ s/0//g
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | my $numHosts = 2 ** $numZeros - 2; | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | return (0, $numHosts); | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub calcSubnetCIDR { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calcSubnetCIDR: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | my ($addr, $mask) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | my ($ip, $maskBits) = split (/\//, $addr); | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | (isBinIpAddr($ip)) && _validateIp(&bin2decIpAddr($ip)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($ip) and _validateIp(&bin2decIpAddr($ip))
MC/DC0%0/2
isBinIpAddr($ip) and _validateIp(&bin2decIpAddr($ip))
isBinIpAddr($ip)
_validateIp(&bin2decIpAddr($ip))
Truth table0%0/3
A and B
A= isBinIpAddr($ip)B= _validateIp(&bin2decIpAddr($ip))
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | || ($ip = &dec2binIpAddr($ip)); | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | ($maskBits !~ /^\d+$/ || $maskBits < 0 || $maskBits > 32) && croak "Given mask bits $maskBits is invalid!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $maskBits =~ /^\d+$/ or $maskBits < 0 or $maskBits > 32
not $maskBits =~ /^\d+$/ or $maskBits < 0
MC/DC0%0/3
not $maskBits =~ /^\d+$/ or $maskBits < 0 or $maskBits > 32
not $maskBits =~ /^\d+$/
$maskBits < 0
$maskBits > 32
Truth table0%0/4
not(A) or B or C
A= not $maskBits =~ /^\d+$/B= $maskBits < 0C= $maskBits > 32
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $numOnes; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if(!$mask) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $mask = &dec2binIpAddr(getAddrMaskDefault($ip)); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $temp = $mask; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $numOnes = ($temp =~ s/1//g) ||0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$temp =~ s/1//g || 0
MC/DC0%0/1
$temp =~ s/1//g || 0
$temp =~ s/1//g
Truth table0%0/2
A
A= $temp =~ s/1//g
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | (! isValidMask($mask)) && croak "Given mask $mask is not valid!!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | (! isBinIpAddr($mask)) && ($mask = &dec2binIpAddr($mask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my $temp = $mask; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | $numOnes = ($temp =~ s/1//g) ||0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$temp =~ s/1//g || 0
MC/DC0%0/1
$temp =~ s/1//g || 0
$temp =~ s/1//g
Truth table0%0/2
A
A= $temp =~ s/1//g
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $diffBits = $maskBits - $numOnes; | |||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | ($diffBits < 0) && croak "Given masking bits $maskBits should be >= $numOnes (no. of default mask 1 bits"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my $extendedMask = extendMaskByBits($mask,$diffBits); | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | $diffBits = 0 if(!defined $diffBits); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my $numSubnets = 2 ** $diffBits; | |||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | my $numZeros = ($extendedMask =~ s/0//g) || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$extendedMask =~ s/0//g || 0
MC/DC0%0/1
$extendedMask =~ s/0//g || 0
$extendedMask =~ s/0//g
Truth table0%0/2
A
A= $extendedMask =~ s/0//g
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my $numHosts = 2 ** $numZeros - 2; | |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return ($numSubnets, $numHosts); | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub calcSubnetExt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
calcSubnetExt: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my ($addr,$borrow) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | (isBinIpAddr($addr)) && _validateIp($addr) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
isBinIpAddr($addr) and _validateIp($addr)
MC/DC0%0/2
isBinIpAddr($addr) and _validateIp($addr)
isBinIpAddr($addr)
_validateIp($addr)
Truth table0%0/3
A and B
A= isBinIpAddr($addr)B= _validateIp($addr)
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | || ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | my $defaultMask = getAddrMaskDefault($addr); | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my $extendedMask = extendMaskByBits($defaultMask,$borrow); | |||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | $borrow = 0 if(!defined $borrow); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $numSubnets = 2 ** $borrow; | |||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $numZeros = ($extendedMask =~ s/0//g) || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$extendedMask =~ s/0//g || 0
MC/DC0%0/1
$extendedMask =~ s/0//g || 0
$extendedMask =~ s/0//g
Truth table0%0/2
A
A= $extendedMask =~ s/0//g
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | my $numHosts = 2 ** $numZeros - 2; | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | return ($numSubnets, $numHosts); | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | sub getNetworkAddr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getNetworkAddr: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my ($addr, $defaultMask, $subnetMask, $bc) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $defaultMask && isValidMask($defaultMask); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | !$defaultMask && ($defaultMask = getAddrMaskDefault($addr)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $subnetMask && isValidMask($subnetMask); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | (! isBinIpAddr($addr)) && ($addr = &dec2binIpAddr($addr)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | (! isBinIpAddr($defaultMask)) && ($defaultMask = &dec2binIpAddr($defaultMask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | (! isBinIpAddr($subnetMask)) && ($subnetMask = &dec2binIpAddr($subnetMask)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | my $subnetMaskOnBits = ($subnetMask =~ s/1/1/g); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $defaultMaskOnBits = ($defaultMask =~ s/1/1/g); | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my $numSubnetBits = ($subnetMaskOnBits - $defaultMaskOnBits); | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | croak "Default mask : $defaultMask and/or Subnet mask : $subnetMask incorrect!!" if($numSubnetBits !~/^\d+$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | my $numHostBits = 32 - $subnetMaskOnBits; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my $numHostAddrs = 2 ** $numHostBits; | |||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $numSubnets = 2 ** $numSubnetBits; | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my @nwAddrs; | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ($bc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | for (0..($numSubnets-1)) { | |||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | push @nwAddrs, bin2decIpAddr(_incrIpAddr($addr, $numHostAddrs-1)); | |||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $addr = _incrIpAddr($addr, $numHostAddrs); | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | for (0..($numSubnets-1)) { | |||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | push @nwAddrs, bin2decIpAddr ($addr); | |||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $addr = _incrIpAddr($addr, $numHostAddrs); | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | return (@nwAddrs); | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub _incrIpAddr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_incrIpAddr: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my ($addr, $incr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | $addr = bin2decIpAddr($addr); | |||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my ($o4,$o3,$o2,$o1) = split /\./,$addr; | |||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $o1 += $incr; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | if($o1 <= 255) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | return dec2binIpAddr("$o4.$o3.$o2.$o1"); | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | $incr = $o1 - 255; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $o1 = 255; | |||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | $o2 += $incr; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | if ($o2 <= 255) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | return dec2binIpAddr("$o4.$o3.$o2.$o1"); | |||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $incr = $o2 - 255; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | $o2 = 255; | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | $o3 += $incr; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | if($o3 <= 255) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | return dec2binIpAddr("$o4.$o3.$o2.$o1"); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | $incr = $o3 - 255; | |||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | $o3 = 255; | |||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $o4 += $incr; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | return dec2binIpAddr("$o4.$o3.$o2.$o1"); | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | Net::IP::Util - Common useful routines like converting decimal address to binary and vice versa, determining address class, | ||||||||||||||||||||||||||||||||||||||||||
| 337 | determining default mask, subnets and hosts and broadcast addresses for hosts in subnet. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | use Net::IP::Util; ## subroutines isClassAddrA-E, bin2decIpAddr, dec2binIpAddr | ||||||||||||||||||||||||||||||||||||||||||
| 342 | use Net::IP::Util qw/:class/; ## subroutines isClassAddrA-E, getAddrClass | ||||||||||||||||||||||||||||||||||||||||||
| 343 | use Net::IP::Util qw/:convert/; ## subroutines bin2decIpAddr, dec2binIpAddr | ||||||||||||||||||||||||||||||||||||||||||
| 344 | use Net::IP::Util qw/getAddrMaskDefault | ||||||||||||||||||||||||||||||||||||||||||
| 345 | getAddrClass | ||||||||||||||||||||||||||||||||||||||||||
| 346 | isValidMask | ||||||||||||||||||||||||||||||||||||||||||
| 347 | extendMaskByBits | ||||||||||||||||||||||||||||||||||||||||||
| 348 | calcSubnet | ||||||||||||||||||||||||||||||||||||||||||
| 349 | calcSubnetCIDR | ||||||||||||||||||||||||||||||||||||||||||
| 350 | calcSubnetExt | ||||||||||||||||||||||||||||||||||||||||||
| 351 | getNetworkAddr ## Explicit inclusions | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | isClassAddrA('127.0.32.45'); | ||||||||||||||||||||||||||||||||||||||||||
| 354 | isClassAddrA('00001111.11110010.00100100.10000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | dec2binIpAddr('128.0.0.56'); | ||||||||||||||||||||||||||||||||||||||||||
| 357 | bin2decIpAddr('10001000.10100001.00010101.00000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | getAddrMaskDefault('124.45.0.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 360 | getAddrMaskDefault('10000000.00000001.01010101.10000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | getAddrClass('124.45.0.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 363 | getAddrClass('00001111.11110010.00100100.10000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | isValidMask('255.255.252.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 366 | isValidMask('11111111.00000000.00000000.00000000'); | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | extendMaskByBits('255.255.0.0',2); | ||||||||||||||||||||||||||||||||||||||||||
| 369 | extendMaskByBits('11111111.00000000.00000000.00000000',2); | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | calcSubnet('128.8.9.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 372 | calcSubnet('10001000.10100001.00010101.00000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | calcSubnetCIDR('128.9.0.218/24'); | ||||||||||||||||||||||||||||||||||||||||||
| 375 | calcSubnetCIDR('128.9.0.218/28', '255.255.255.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | calcSubnetExt('128.0.0.1',4); | ||||||||||||||||||||||||||||||||||||||||||
| 378 | calcSubnetExt('10001000.10100001.00010101.00000001',4); | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252'); | ||||||||||||||||||||||||||||||||||||||||||
| 381 | getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252', 1); | ||||||||||||||||||||||||||||||||||||||||||
| 382 | getNetworkAddr('10000000.00000001.01010101.10000001', | ||||||||||||||||||||||||||||||||||||||||||
| 383 | '11111111.11111111.11111111.11110000', | ||||||||||||||||||||||||||||||||||||||||||
| 384 | '11111111.11111111.11111111.11111100',); | ||||||||||||||||||||||||||||||||||||||||||
| 385 | getNetworkAddr('10000000.00000001.01010101.10000001', | ||||||||||||||||||||||||||||||||||||||||||
| 386 | '11111111.11111111.11111111.11110000', | ||||||||||||||||||||||||||||||||||||||||||
| 387 | '11111111.11111111.11111111.11111100', 1); | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | =head1 ABSTRACT | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | This module tries provide the basic functionalities related to IPv4 addresses. | ||||||||||||||||||||||||||||||||||||||||||
| 392 | Address class, subnet masks, subnet addresses, broadcast addresses can be deduced | ||||||||||||||||||||||||||||||||||||||||||
| 393 | using the given methods. Ip addresses passed are also validated implicitly. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | Provision has been given to specify IP addresses in either dotted decimal notation | ||||||||||||||||||||||||||||||||||||||||||
| 396 | or dotted binary notation, methods have been provided for conversion to-from these | ||||||||||||||||||||||||||||||||||||||||||
| 397 | to notations which are internally used by other methods too. | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | =head2 isClassAddrA,isClassAddrB,isClassAddrC,isClassAddrD,isClassAddrE | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | isClassAddrA(<addr in decimal/binary>) : returns 1 if true | ||||||||||||||||||||||||||||||||||||||||||
| 404 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 405 | isClassAddrA('127.0.32.45'); | ||||||||||||||||||||||||||||||||||||||||||
| 406 | isClassAddrA('00001111.11110010.00100100.10000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | =head2 dec2binIpAddr | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | dec2binIpAddr(<ip addr in dotted decimal notation>) : returns ip in binary dotted notation | ||||||||||||||||||||||||||||||||||||||||||
| 411 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 412 | dec2binIpAddr('128.0.0.56'); | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | =head2 bin2decIpAddr | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | bin2decIpAddr(<ip addr in dotted binary notation>) : returns ip in decimal dotted notation | ||||||||||||||||||||||||||||||||||||||||||
| 417 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | bin2decIpAddr('10001000.10100001.00010101.00000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | =head2 getAddrMaskDefault | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | getAddrMaskDefault(<ip addr in decimal/binary notation>) : returns default subnet mask in dotted decimal notation | ||||||||||||||||||||||||||||||||||||||||||
| 423 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | getAddrMaskDefault('124.45.0.0'); >> 255.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||
| 425 | getAddrMaskDefault('10000000.00000001.01010101.10000001'); >> 255.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =head2 getAddrClass | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | getAddrClass(<ip addr in decimal/binary notation>) : returns class (A/B/C/D/E) of ip address | ||||||||||||||||||||||||||||||||||||||||||
| 430 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | getAddrClass('124.45.0.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 432 | getAddrClass('00001111.11110010.00100100.10000001'); | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | =head2 isValidMask | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | isValidMask(<ip addr in decimal/binary notation>) : returns 1 if valid mask | ||||||||||||||||||||||||||||||||||||||||||
| 437 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 438 | isValidMask('255.255.252.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 439 | isValidMask('11111111.00000000.00000000.00000000'); | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | =head2 extendMaskByBits | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | extendMaskByBits(<ip addr in decimal/binary notation>,<no.of bits to extend>) | ||||||||||||||||||||||||||||||||||||||||||
| 444 | : returns mask after extending/turning on given no. of bits after the already on bits of the mask | ||||||||||||||||||||||||||||||||||||||||||
| 445 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | extendMaskByBits('255.255.0.0',2); >> 255.255.192.0 | ||||||||||||||||||||||||||||||||||||||||||
| 447 | extendMaskByBits('11111111.00000000.00000000.00000000',2); >> 11111111.11000000.00000000.00000000 | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | =head2 calcSubnet | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | calcSubnet(<ip addr in decimal/binary notation>) : returns (no. of subnets, no. of hosts) | ||||||||||||||||||||||||||||||||||||||||||
| 452 | calcSubnet('128.90.80.12'); | ||||||||||||||||||||||||||||||||||||||||||
| 453 | calcSubnet('11000000.00000000.11000000.01011100'); | ||||||||||||||||||||||||||||||||||||||||||
| 454 | - These always assumes Default Mask in calculation - hence no of subnets returned is always 0 | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =head2 calcSubnetCIDR | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | calcSubnetCIDR(<ip addr in decimal/binary CIDR notation>, [<mask in decimal/binary notation>]) | ||||||||||||||||||||||||||||||||||||||||||
| 459 | : returns (no. of subnets, no. of hosts) | ||||||||||||||||||||||||||||||||||||||||||
| 460 | calcSubnetCIDR('128.87.56.26/28'); | ||||||||||||||||||||||||||||||||||||||||||
| 461 | calcSubnetCIDR('128.87.56.26/28','255.255.252.0'); | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | =head2 calcSubnetExt | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | calcSubnetExt(ip addr in decimal/binary notation>, no. of bits to extend in default mask OR no. of borrowed bits) | ||||||||||||||||||||||||||||||||||||||||||
| 466 | : returns (no. of subnets, no. of hosts) | ||||||||||||||||||||||||||||||||||||||||||
| 467 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 468 | calcSubnetExt('128.0.0.1',4); | ||||||||||||||||||||||||||||||||||||||||||
| 469 | calcSubnetExt('10001000.10100001.00010101.00000001',4); | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | Expln : no. of borrowed bits is added to the default subnet mask of ip addr to subnet mask | ||||||||||||||||||||||||||||||||||||||||||
| 472 | and subnetting is done so : | ||||||||||||||||||||||||||||||||||||||||||
| 473 | *************************************************** | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 127.0.40.1 = ip addr | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 255.0.0.0 = default subnet mask | ||||||||||||||||||||||||||||||||||||||||||
| 476 | no. of borrowed bits = 4 | ||||||||||||||||||||||||||||||||||||||||||
| 477 | => 255.240.0.0 = extended mask | ||||||||||||||||||||||||||||||||||||||||||
| 478 | *************************************************** | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 getNetworkAddr | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | getNetworkAddr(<ip addr in decimal/binary notation>, | ||||||||||||||||||||||||||||||||||||||||||
| 483 | <default mask in decimal/binary notation>, | ||||||||||||||||||||||||||||||||||||||||||
| 484 | <subnet mask in decimal/binary notation>, | ||||||||||||||||||||||||||||||||||||||||||
| 485 | <true flag - if you want broadcast addresses instead of n/w addresses | ||||||||||||||||||||||||||||||||||||||||||
| 486 | ) : returns network/broadcast addresses of the subnets after subnetting as a list | ||||||||||||||||||||||||||||||||||||||||||
| 487 | eg. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252'); >> ('198.23.16.0','198.23.16.4','198.23.16.8','198.23.16.12') | ||||||||||||||||||||||||||||||||||||||||||
| 489 | getNetworkAddr('198.23.16.0','255.255.255.240','255.255.255.252',1); >> ('198.23.16.3','198.23.16.7','198.23.16.11','198.23.16.15') | ||||||||||||||||||||||||||||||||||||||||||
| 490 | getNetworkAddr('10000000.00000001.01010101.10000001', | ||||||||||||||||||||||||||||||||||||||||||
| 491 | '11111111.11111111.11111111.11110000', | ||||||||||||||||||||||||||||||||||||||||||
| 492 | '11111111.11111111.11111111.11111100',); >> Always returns n/w addresses in dotted decimal irrespective of binary/decimal | ||||||||||||||||||||||||||||||||||||||||||
| 493 | address parameter passed | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | =head1 CAVEAT | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | IPv4 only | ||||||||||||||||||||||||||||||||||||||||||
| 498 | Validation of IP addresses are done, but because of conversions here and there it may not show the IP address properly in the error message | ||||||||||||||||||||||||||||||||||||||||||
| 499 | as passed earlier by the user. | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | =head1 Similar Modules | ||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | Net::IP, Net::IpAddr etc. | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | debashish@cpan.org | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | Copyright 2013 Debashish Parasar, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 516 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =cut | ||||||||||||||||||||||||||||||||||||||||||