| 1 | #=============================================================================== | ||||||||||||||||||||||||||||
| 2 | # PODNAME: Net::IP::Identifier::Net | ||||||||||||||||||||||||||||
| 3 | # ABSTRACT: subclass Net::IP to add some functionality | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # AUTHOR: Reid Augustin (REID) | ||||||||||||||||||||||||||||
| 6 | # EMAIL: reid@hellosix.com | ||||||||||||||||||||||||||||
| 7 | # CREATED: Sun Jul 20 17:48:21 PDT 2014 | ||||||||||||||||||||||||||||
| 8 | #=============================================================================== | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use 5.002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | package Net::IP::Identifier::Net; | ||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use parent 'Net::IP'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | our $VERSION = '0.110'; # VERSION | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use overload ( | ||||||||||||||||||||||||||||
| 23 | 3 | '""' => 'print', | |||||||||||||||||||||||||||
| 24 | 3 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Accept any of: | ||||||||||||||||||||||||||||
| 27 | # Net::IP::Identifier::Net object or class | ||||||||||||||||||||||||||||
| 28 | # Net::IP object | ||||||||||||||||||||||||||||
| 29 | sub new { | ||||||||||||||||||||||||||||
| 30 | 1710 | ▶ | my ($class, $net) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1710 | ▶ | croak "Must have exactly one argument to 'new'\n" if (@_ != 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1710 | ▶ | if (ref $net) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return $net if (ref $net eq __PACKAGE__); # already correct | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # it's an object, but wrong kind | ||||||||||||||||||||||||||||
| 37 | 0 | my $src_str; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $src_str = $net->src_str if ($net->can('src_str')); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | bless $net, $class; # rebless to this package | |||||||||||||||||||||||||||
| 40 | # make sure we have a source string | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $net->src_str($src_str || $net->print); | ||||||||||||||||||||||||||
|
Condition0%0/3
$src_str || $net->print
MC/DC0%0/2
$src_str || $net->print
$src_str
$net->print
Truth table0%0/3
A || B
A= $src_strB= $net->print
| |||||||||||||||||||||||||||||
| 42 | 0 | return $net; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 1710 | my $self = $class->SUPER::new($net); | |||||||||||||||||||||||||||
| 45 | 1710 | bless $self, $class; # rebless to this package | |||||||||||||||||||||||||||
| 46 | 1710 | $self->src_str($net); # set the source string | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1710 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub src_str { | ||||||||||||||||||||||||||||
| 52 | 1710 | ▶ | my ($self, $new) = @_; | ||||||||||||||||||||||||||
|
Subroutine
src_str: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1710 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1710 | $self->{src_str} = $new; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 1710 | return $self->{src_str}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # print an int as a dotted decimal quad | ||||||||||||||||||||||||||||
| 61 | sub int_to_ip { | ||||||||||||||||||||||||||||
| 62 | 758 | ▶ | my ($self, $int, $version) = @_; | ||||||||||||||||||||||||||
|
Subroutine
int_to_ip: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 758 | ▶ | $version ||= $self->version || 4; | ||||||||||||||||||||||||||
|
Condition40%2/5
$version ||= $self->version || 4
$self->version || 4
MC/DC0%0/2
$version ||= $self->version || 4
$version
$self->version
Truth table33%1/3
A ||= B
A= $versionB= $self->version
| |||||||||||||||||||||||||||||
| 65 | 758 | my @parts; | |||||||||||||||||||||||||||
| 66 | 758 | ▶ | if ($version eq '6') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 58 | ▶ | while ($int or @parts < 8) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$int or @parts < 8
MC/DC50%1/2
$int or @parts < 8
$int
@parts < 8
Truth table66%2/3
A or B
A= $intB= @parts < 8
| |||||||||||||||||||||||||||||
| 68 | 464 | unshift @parts, $int & 0xffff; | |||||||||||||||||||||||||||
| 69 | 464 | $int >>= 16; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 58 | my $addr = join(':', map { sprintf "%x", $_ } @parts); | |||||||||||||||||||||||||||
| 72 | 58 | return Net::IP::ip_compress_address($addr, 6); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # else IPv4 | ||||||||||||||||||||||||||||
| 76 | 700 | ▶ | while ($int or @parts < 4) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$int or @parts < 4
MC/DC50%1/2
$int or @parts < 4
$int
@parts < 4
Truth table66%2/3
A or B
A= $intB= @parts < 4
| |||||||||||||||||||||||||||||
| 77 | 2800 | unshift @parts, $int & 0xff; | |||||||||||||||||||||||||||
| 78 | 2800 | $int >>= 8; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 700 | return join('.', @parts); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # split a range at the binary changeover point | ||||||||||||||||||||||||||||
| 84 | sub _split_range { | ||||||||||||||||||||||||||||
| 85 | 620 | ▶ | my ($self, $idx, $low, $high) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_split_range: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | no warnings 'recursion'; # IPv6 requires at least 128 levels | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 620 | if (0) { | |||||||||||||||||||||||||||
| 90 | printf "idx $idx, low %s, high %s\n", | ||||||||||||||||||||||||||||
| 91 | $self->int_to_ip($low), | ||||||||||||||||||||||||||||
| 92 | $self->int_to_ip($high); | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 620 | ▶ | if ($high <= $low) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 4 | return Net::IP::Identifier::Net->new($self->int_to_ip($low)); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 616 | my $mask = Math::BigInt->new(2); | |||||||||||||||||||||||||||
| 99 | 616 | $mask->bpow($idx); | |||||||||||||||||||||||||||
| 100 | 616 | $mask--; | |||||||||||||||||||||||||||
| 101 | 616 | ▶ | while ($idx and ($high ^ $low) <= $mask) { # find first mask where different bit is outside it | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$idx and ($high ^ $low) <= $mask
MC/DC100%2/2
$idx and ($high ^ $low) <= $mask
$idx
($high ^ $low) <= $mask
Truth table100%3/3
A and B
A= $idxB= ($high ^ $low) <= $mask
| |||||||||||||||||||||||||||||
| 102 | 889 | $mask->brsft(1); | |||||||||||||||||||||||||||
| 103 | 889 | $idx--; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 616 | $mask->blsft(1); # undo one shift | |||||||||||||||||||||||||||
| 106 | 616 | $mask += 1; | |||||||||||||||||||||||||||
| 107 | 616 | $idx++; | |||||||||||||||||||||||||||
| 108 | 616 | ▶ | if (($mask & $low) == 0 and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
($mask & $low) == 0 and ($mask & $high) == $mask
MC/DC100%2/2
($mask & $low) == 0 and ($mask & $high) == $mask
($mask & $low) == 0
($mask & $high) == $mask
Truth table100%3/3
A and B
A= ($mask & $low) == 0B= ($mask & $high) == $mask
| |||||||||||||||||||||||||||||
| 109 | ($mask & $high) == $mask) { | ||||||||||||||||||||||||||||
| 110 | 377 | $low = $self->int_to_ip($low); | |||||||||||||||||||||||||||
| 111 | 377 | $high = $self->int_to_ip($high); | |||||||||||||||||||||||||||
| 112 | 377 | return Net::IP::Identifier::Net->new("$low - $high"); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 239 | ▶ | croak sprintf "ran out of indexes: 0x%x-0x%x\n", $low, $high if($idx <= 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # need to split | ||||||||||||||||||||||||||||
| 118 | 239 | my $new_split; | |||||||||||||||||||||||||||
| 119 | 239 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$new_split > $high and $idx >= 0
MC/DC0%0/2
$new_split > $high and $idx >= 0
$new_split > $high
$idx >= 0
Truth table33%1/3
A and B
A= $new_split > $highB= $idx >= 0
| |||||||||||||||||||||||||||||
| 120 | 239 | $new_split = ($low & ~$mask); | |||||||||||||||||||||||||||
| 121 | 239 | $mask >>= 1; | |||||||||||||||||||||||||||
| 122 | 239 | $new_split += $mask + 1; | |||||||||||||||||||||||||||
| 123 | 239 | $idx--; | |||||||||||||||||||||||||||
| 124 | } while ($new_split > $high and $idx >= 0); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 239 | my @low = $self->_split_range($idx, $low, $new_split - 1); | |||||||||||||||||||||||||||
| 127 | 239 | my @high = $self->_split_range($idx, $new_split, $high); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 239 | return(@low, @high); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # method to convert range into cidrs | ||||||||||||||||||||||||||||
| 133 | sub range_to_cidrs { | ||||||||||||||||||||||||||||
| 134 | 142 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
range_to_cidrs: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 142 | ▶ | return $self if ($self->prefixlen); # don't need to split | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 142 | return $self->_split_range( | |||||||||||||||||||||||||||
| 139 | $self->masklen - 1, | ||||||||||||||||||||||||||||
| 140 | $self->intip, | ||||||||||||||||||||||||||||
| 141 | $self->last_int); | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | my $zero_v4 = '0' x 32; | |||||||||||||||||||||||||||
| 145 | 3 | my $zero_v6 = '0' x 128; | |||||||||||||||||||||||||||
| 146 | # return the length of inverse of the prefixlen, the length of the 1s in the mask | ||||||||||||||||||||||||||||
| 147 | # always returns a value, unlike prefixlen. If not on an even binary | ||||||||||||||||||||||||||||
| 148 | # boundary, the masklen represents a big enough CIDR that the range fits | ||||||||||||||||||||||||||||
| 149 | # in it | ||||||||||||||||||||||||||||
| 150 | sub masklen { | ||||||||||||||||||||||||||||
| 151 | 1710 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
masklen: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 1710 | ▶ | my $len = $self->version == 6 ? 128 : 32; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | # xor with zero mask also to ensure proper upper bits | ||||||||||||||||||||||||||||
| 155 | 1710 | ▶ | my $differ = $self->binip ^ $self->last_bin ^ ($self->version == 6 ? $zero_v6 : $zero_v4); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 1710 | my $idx = index($differ, '1'); | |||||||||||||||||||||||||||
| 157 | 1710 | ▶ | return 0 if $idx < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 1662 | return $len - $idx; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # return the masked portion of the IP (upper part) | ||||||||||||||||||||||||||||
| 162 | sub masked_ip { | ||||||||||||||||||||||||||||
| 163 | 1568 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
masked_ip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1568 | ▶ | my $len = $self->version == 6 ? 128 : 32; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 1568 | return substr($self->binip, 0, $len - $self->masklen); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # override print: bare IP (without prefixlen) for single IPs or network | ||||||||||||||||||||||||||||
| 170 | # with prefixlength if CIDR, or as range if not single and not CIDR | ||||||||||||||||||||||||||||
| 171 | sub print { | ||||||||||||||||||||||||||||
| 172 | 1303 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1303 | ▶ | return $self->ip if ($self->ip eq $self->last_ip); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1303 | ▶ | if (defined $self->prefixlen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1161 | ▶ | if ($self->version eq 6) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 96 | return $self->SUPER::print; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | 1065 | return $self->ip . '/' . $self->prefixlen; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | 142 | return $self->ip . '-' . $self->last_ip; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # return ->ip, but compress if IPv6 | ||||||||||||||||||||||||||||
| 185 | sub compressed_ip { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compressed_ip: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | return Net::IP::ip_compress_address($self->ip, $self->version); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||