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