| 1 | package Net::CIDR::Lite; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | $VERSION = '0.23'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | my %masks; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | my @fields = qw(PACK UNPACK NBITS MASKS); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # Preloaded methods go here. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 26 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 26 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 26 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 26 | $self->add_any($_) for @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 21 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | sub add_any { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 22 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_any: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 22 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 22 | ▶ | tr|/|| && do { $self->add($_); next }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | tr|-|| && do { $self->add_range($_); next }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | UNIVERSAL::isa($_, 'Net::CIDR::Lite') && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | $self->add_cidr($_); next | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | $self->add_ip($_), next; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 17 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | sub add { | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 27 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 27 | my ($ip, $mask) = split "/", shift; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 27 | ▶ | $self->_init($ip) || confess "Can't determine ip format" unless %$self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
$self->_init($ip) or confess("Can't determine ip format")
MC/DC0%0/3
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
%$self
$self->_init($ip)
confess("Can't determine ip format")
Truth table0%0/4
A || B or C
A= %$selfB= $self->_init($ip)C= confess("Can't determine ip format")
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | confess "Bad mask $mask" | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 21 | ▶ | unless $mask =~ /^\d+$/ and $mask <= $self->{NBITS}-8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8
MC/DC50%1/2
$mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8
$mask =~ /^\d+$/
$mask <= $self->{"NBITS"} - 8
Truth table66%2/3
A and B
A= $mask =~ /^\d+$/B= $mask <= $self->{"NBITS"} - 8
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 20 | $mask += 8; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 20 | ▶ | my $start = $self->{PACK}->($ip) & $self->{MASKS}[$mask] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | or confess "Bad ip address: $ip"; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 20 | my $end = $self->_add_bit($start, $mask); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 20 | ▶ | --$$self{RANGES}{$end} || delete $$self{RANGES}{$end}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 20 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub clean { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clean: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 9 | ▶ | return $self unless $self->{RANGES}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | my $ranges = $$self{RANGES}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | my $total; | |||||||||||||||||||||||||||||||||||||||||
| 55 | $$self{RANGES} = { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 6 | map { $total ? ($total+=$$ranges{$_})? () : ($_=>-1) | |||||||||||||||||||||||||||||||||||||||||
| 57 | 16 | ▶ | : do { $total+=$$ranges{$_}; ($_=>1) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | } sort keys %$ranges | ||||||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub list { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | return unless $self->{NBITS}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | my $nbits = $$self{NBITS}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | my ($start, $total); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | my @results; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | for my $ip (sort keys %{$$self{RANGES}}) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | $start = $ip unless $total; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | $total += $$self{RANGES}{$ip}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | unless ($total) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | while ($start lt $ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | my ($end, $bits); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | my $sbit = $nbits-1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | # Find the position of the last 1 bit | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | $sbit-- while !vec($start, $sbit^7, 1) and $sbit>0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not vec($start, $sbit ^ 7, 1) and $sbit > 0
MC/DC100%2/2
not vec($start, $sbit ^ 7, 1) and $sbit > 0
not vec($start, $sbit ^ 7, 1)
$sbit > 0
Truth table100%3/3
not(A) and B
A= not vec($start, $sbit ^ 7, 1)B= $sbit > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | for my $pos ($sbit+1..$nbits) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | 117 | $end = $self->_add_bit($start, $pos); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 117 | ▶ | $bits = $pos-8, last if $end le $ip; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | push @results, $self->{UNPACK}->($start) . "/$bits"; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | $start = $end; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | wantarray ? @results : \@results; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | sub list_range { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_range: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | my ($start, $total); | |||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | my @results; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | for my $ip (sort keys %{$$self{RANGES}}) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | ▶ | $start = $ip unless $total; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | $total += $$self{RANGES}{$ip}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | ▶ | unless ($total) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | $ip = $self->_minus_one($ip); | |||||||||||||||||||||||||||||||||||||||||
| 99 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | $self->{UNPACK}->($start) . "-" . $self->{UNPACK}->($ip); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | ▶ | wantarray ? @results : \@results; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | sub list_short_range { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_short_range: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | my $start; | |||||||||||||||||||||||||||||||||||||||||
| 110 | my $total; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | my @results; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | for my $ip (sort keys %{$$self{RANGES}}) { | |||||||||||||||||||||||||||||||||||||||||
| 114 | # we begin new range when $total is zero | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 12 | ▶ | $start = $ip if not $total; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # add to total (1 for start of the range or -1 for end of the range) | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 12 | $total += $$self{RANGES}{$ip}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # in case of end of range | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 12 | ▶ | if (not $total) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | while ($ip gt $start) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 10 | $ip = $self->_minus_one($ip); | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # in case of single ip not a range | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 10 | ▶ | if ($ip eq $start) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | $self->{UNPACK}->($start); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | # get the last ip octet number | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 7 | my $to_octet = ( unpack('C5', $ip) )[4]; | |||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # next ip end will be current end masked by c subnet mask 255.255.255.0 - /24 | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 7 | $ip = $ip & $self->{MASKS}[32]; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # if the ip range is in the same c subnet | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 7 | ▶ | if ($ip eq ($start & $self->{MASKS}[32])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 3 | $self->{UNPACK}->($start) . "-" . $to_octet; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # otherwise the range start is .0 (end of range masked by c subnet mask) | ||||||||||||||||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | push @results, | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | $self->{UNPACK}->($ip & $self->{MASKS}[32]) . "-" . $to_octet; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | wantarray ? @results : \@results; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | sub _init { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 25 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 25 | my $ip = shift; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 25 | my ($nbits, $pack, $unpack); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 25 | ▶ | if (_pack_ipv4($ip)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 13 | $nbits = 40; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 13 | $pack = \&_pack_ipv4; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 13 | $unpack = \&_unpack_ipv4; | |||||||||||||||||||||||||||||||||||||||||
| 162 | } elsif (_pack_ipv6($ip)) { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 5 | $nbits = 136; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | $pack = \&_pack_ipv6; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 5 | $unpack = \&_unpack_ipv6; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 18 | $$self{PACK} = $pack; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 18 | $$self{UNPACK} = $unpack; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 18 | $$self{NBITS} = $nbits; | |||||||||||||||||||||||||||||||||||||||||
| 172 | $$self{MASKS} = $masks{$nbits} ||= [ | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 18 | ▶ | map { pack("B*", substr("1" x $_ . "0" x $nbits, 0, $nbits)) | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$masks{$nbits} ||= [map({pack "B*", substr(1 x $_ . 0 x $nbits, 0, $nbits);} 0 .. $nbits)]
MC/DC100%1/1
$masks{$nbits} ||= [map({pack "B*", substr(1 x $_ . 0 x $nbits, 0, $nbits);} 0 .. $nbits)]
$masks{$nbits}
Truth table100%2/2
A
A= $masks{$nbits}
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | } 0..$nbits | ||||||||||||||||||||||||||||||||||||||||||
| 175 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 18 | $$self{RANGES} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 18 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | sub _pack_ipv4 { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 72 | ▶ | my @nums = split /\./, shift(), -1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_ipv4: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 72 | ▶ | return unless @nums == 4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 65 | for (@nums) { | |||||||||||||||||||||||||||||||||||||||||
| 184 | 238 | ▶ | return unless /^\d{1,3}$/ and !/^0\d{1,2}$/ and $_ <= 255; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
/^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255
/^\d{1,3}$/ and not /^0\d{1,2}$/
MC/DC66%2/3
/^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255
/^\d{1,3}$/
not /^0\d{1,2}$/
$_ <= 255
Truth table75%3/4
A and not(B) and C
A= /^\d{1,3}$/B= not /^0\d{1,2}$/C= $_ <= 255
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 57 | pack("CC*", 0, @nums); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | sub _unpack_ipv4 { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 22 | ▶ | join(".", unpack("xC*", shift)); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unpack_ipv4: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | sub _pack_ipv6 { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 25 | ▶ | my $ip = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_ipv6: called CC=19 SCAR=30.7
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 25 | $ip =~ s/^::$/::0/; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 25 | ▶ | return if $ip =~ /^:/ and $ip !~ s/^::/:/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ip =~ /^:/ and not $ip =~ s/^::/:/
MC/DC0%0/2
$ip =~ /^:/ and not $ip =~ s/^::/:/
$ip =~ /^:/
not $ip =~ s/^::/:/
Truth table66%2/3
A and not(B)
A= $ip =~ /^:/B= not $ip =~ s/^::/:/
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 25 | ▶ | return if $ip =~ /:$/ and $ip !~ s/::$/:/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ip =~ /:$/ and not $ip =~ s/::$/:/
MC/DC0%0/2
$ip =~ /:$/ and not $ip =~ s/::$/:/
$ip =~ /:$/
not $ip =~ s/::$/:/
Truth table33%1/3
A and not(B)
A= $ip =~ /:$/B= not $ip =~ s/::$/:/
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 25 | my @nums = split /:/, $ip, -1; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 25 | ▶ | return unless @nums <= 8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 25 | my ($empty, $ipv4, $str) = (0,'',''); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 25 | for (@nums) { | |||||||||||||||||||||||||||||||||||||||||
| 202 | 101 | ▶ | return if $ipv4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 101 | ▶ | $str .= "0" x (4-length) . $_, next if /^[a-fA-F\d]{1,4}$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 23 | ▶ | do { return if $empty++ }, $str .= "X", next if $_ eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 12 | ▶ | next if $ipv4 = _pack_ipv4($_); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 22 | ▶ | return if $ipv4 and @nums > 6; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ipv4 and @nums > 6
MC/DC0%0/2
$ipv4 and @nums > 6
$ipv4
@nums > 6
Truth table66%2/3
A and B
A= $ipv4B= @nums > 6
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 22 | ▶ | return unless $empty or @nums == ($ipv4 ? 6 : 8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$empty or @nums == ($ipv4 ? 6 : 8)
MC/DC100%2/2
$empty or @nums == ($ipv4 ? 6 : 8)
$empty
@nums == ($ipv4 ? 6 : 8)
Truth table100%3/3
A or B
A= $emptyB= @nums == ($ipv4 ? 6 : 8)
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 18 | ▶ | $str =~ s/X/"0" x (($ipv4 ? 25 : 33)-length($str))/e if $empty; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 18 | pack("H*", "00" . $str).substr($ipv4, 1); | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | sub _unpack_ipv6 { | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 3 | ▶ | _compress_ipv6(join(":", unpack("xH*", shift) =~ /..../g)), | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unpack_ipv6: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | # Replace longest run of null blocks with a double colon | ||||||||||||||||||||||||||||||||||||||||||
| 219 | sub _compress_ipv6 { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | ▶ | my $ip = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compress_ipv6: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | if (my @runs = $ip =~ /((?:(?:^|:)(?:0000))+:?)/g ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | my $max = $runs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | for (@runs[1..$#runs]) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $max = $_ if length($max) < length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | $ip =~ s/$max/::/; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | $ip =~ s/:0{1,3}/:/g; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | $ip; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | # Add a single IP address | ||||||||||||||||||||||||||||||||||||||||||
| 233 | sub add_ip { | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_ip: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 6 | my $ip = shift; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 6 | ▶ | $self->_init($ip) || confess "Can't determine ip format" unless %$self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
$self->_init($ip) or confess("Can't determine ip format")
MC/DC0%0/3
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
%$self
$self->_init($ip)
confess("Can't determine ip format")
Truth table0%0/4
A || B or C
A= %$selfB= $self->_init($ip)C= confess("Can't determine ip format")
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | ▶ | my $start = $self->{PACK}->($ip) or confess "Bad ip address: $ip"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 5 | my $end = $self->_add_bit($start, $self->{NBITS}); | |||||||||||||||||||||||||||||||||||||||||
| 239 | 5 | ▶ | ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 5 | ▶ | --$$self{RANGES}{$end} || delete $$self{RANGES}{$end}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 5 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # Add a hyphenated range of IP addresses | ||||||||||||||||||||||||||||||||||||||||||
| 245 | sub add_range { | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_range: called CC=9 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 3 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 3 | my ($ip_start, $ip_end, $crud) = split /\s*-\s*/; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 3 | ▶ | confess "Only one hyphen allowed in range" if defined $crud; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | $self->_init($ip_start) || confess "Can't determine ip format" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
%$self || ($self->_init($ip_start) || confess(q[Can't determine ip format]))
$self->_init($ip_start) or confess("Can't determine ip format")
MC/DC0%0/3
%$self || ($self->_init($ip_start) || confess(q[Can't determine ip format]))
%$self
$self->_init($ip_start)
confess("Can't determine ip format")
Truth table0%0/4
A || B or C
A= %$selfB= $self->_init($ip_start)C= confess("Can't determine ip format")
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | unless %$self; | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 3 | ▶ | my $start = $self->{PACK}->($ip_start) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | or confess "Bad ip address: $ip_start"; | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | ▶ | my $end = $self->{PACK}->($ip_end) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | or confess "Bad ip address: $ip_end"; | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | confess "Start IP is greater than end IP" if $start gt $end; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | $end = $self->_add_bit($end, $$self{NBITS}); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | ▶ | ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | ▶ | --$$self{RANGES}{$end} || delete $$self{RANGES}{$end}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 3 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # Add ranges from another Net::CIDR::Lite object | ||||||||||||||||||||||||||||||||||||||||||
| 264 | sub add_cidr { | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_cidr: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $cidr = shift; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | confess "Not a CIDR object" unless UNIVERSAL::isa($cidr, 'Net::CIDR::Lite'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | unless (%$self) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | @$self{@fields} = @$cidr{@fields}; | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $$self{RANGES}{$_} += $$cidr{RANGES}{$_} for keys %{$$cidr{RANGES}}; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | # Increment the ip address at the given bit position | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # bit position is in range 1 to # of bits in ip | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # where 1 is high order bit, # of bits is low order bit | ||||||||||||||||||||||||||||||||||||||||||
| 278 | sub _add_bit { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 184 | ▶ | my $self= shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_bit: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 184 | my $base= shift(); | |||||||||||||||||||||||||||||||||||||||||
| 281 | 184 | my $bits= shift()-1; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 184 | ▶ | while (vec($base, $bits^7, 1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 383 | vec($base, $bits^7, 1) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 383 | $bits--; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 383 | ▶ | return $base if $bits < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 180 | vec($base, $bits^7, 1) = 1; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 180 | return $base; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Subtract one from an ip address | ||||||||||||||||||||||||||||||||||||||||||
| 292 | sub _minus_one { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_minus_one: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 13 | my $nbits = $self->{NBITS}; | |||||||||||||||||||||||||||||||||||||||||
| 295 | 13 | my $ip = shift; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 13 | $ip = ~$ip; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 13 | $ip = $self->_add_bit($ip, $nbits); | |||||||||||||||||||||||||||||||||||||||||
| 298 | 13 | $ip = $self->_add_bit($ip, $nbits); | |||||||||||||||||||||||||||||||||||||||||
| 299 | 13 | $self->_add_bit(~$ip, $nbits); | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | sub find { | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 10 | ▶ | $self->prep_find unless $self->{FIND}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 10 | ▶ | return $self->bin_find(@_) unless @{$self->{FIND}} < $self->{PCT}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 10 | ▶ | return 0 unless $self->{PACK}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 9 | my $this_ip = $self->{PACK}->(shift); | |||||||||||||||||||||||||||||||||||||||||
| 308 | 9 | my $ranges = $self->{RANGES}; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 9 | my $last = -1; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 9 | for my $ip (@{$self->{FIND}}) { | |||||||||||||||||||||||||||||||||||||||||
| 311 | 17 | ▶ | last if $this_ip lt $ip; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 11 | $last = $ranges->{$ip}; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 9 | $last > 0; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | sub bin_find { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bin_find: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 4 | my $ip = $self->{PACK}->(shift); | |||||||||||||||||||||||||||||||||||||||||
| 320 | 4 | ▶ | $self->prep_find unless $self->{FIND}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 4 | my $find = $self->{FIND}; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 4 | my ($start, $end) = (0, $#$find); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 4 | ▶ | return unless $ip ge $find->[$start] and $ip lt $find->[$end]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ip ge $find->[$start] and $ip lt $find->[$end]
MC/DC100%2/2
$ip ge $find->[$start] and $ip lt $find->[$end]
$ip ge $find->[$start]
$ip lt $find->[$end]
Truth table100%3/3
A and B
A= $ip ge $find->[$start]B= $ip lt $find->[$end]
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | ▶ | while ($end - $start > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 4 | my $mid = int(($start+$end)/2); | |||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | ▶ | if ($start == $mid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | ▶ | if ($find->[$end] eq $ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | $start = $end; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 1 | } else { $end = $start } | |||||||||||||||||||||||||||||||||||||||||
| 330 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 2 | ▶ | ($find->[$mid] lt $ip ? $start : $end) = $mid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 2 | $self->{RANGES}{$find->[$start]} > 0; | |||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | sub prep_find { | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prep_find: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 5 | $self->clean; | |||||||||||||||||||||||||||||||||||||||||
| 340 | 5 | ▶ | $self->{PCT} = shift || 20; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 20
MC/DC0%0/1
shift() || 20
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 5 | my $aref = $self->{FIND} = []; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 5 | push @$aref, $_ for sort keys %{$self->{RANGES}}; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 5 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | sub spanner { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 2 | ▶ | Net::CIDR::Lite::Span->new(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spanner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | sub _ranges { | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 3 | ▶ | sort keys %{shift->{RANGES}}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ranges: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 2 | ▶ | sub _packer { shift->{PACK} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_packer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 2 | ▶ | sub _unpacker { shift->{UNPACK} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unpacker: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | package Net::CIDR::Lite::Span; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 3 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 2 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 2 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 2 | my $self = bless {RANGES=>{}}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 2 | $self->add(@_); | |||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | sub add { | ||||||||||||||||||||||||||||||||||||||||||
| 368 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 3 | my $ranges = $self->{RANGES}; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 3 | ▶ | if (@_ && !$self->{PACK}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and not $self->{"PACK"}
MC/DC50%1/2
@_ and not $self->{"PACK"}
@_
not $self->{"PACK"}
Truth table66%2/3
A and not(B)
A= @_B= not $self->{"PACK"}
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 2 | my $cidr = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 2 | ▶ | $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | $self->{PACK} = $cidr->_packer; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | $self->{UNPACK} = $cidr->_unpacker; | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 3 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 3 | my ($cidr, $label) = (shift, shift); | |||||||||||||||||||||||||||||||||||||||||
| 378 | 3 | ▶ | $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 3 | $cidr->clean; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 3 | for my $ip ($cidr->_ranges) { | |||||||||||||||||||||||||||||||||||||||||
| 381 | 4 | push @{$ranges->{$ip}}, $label; | |||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 3 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | sub find { | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 4 | my $pack = $self->{PACK}; | |||||||||||||||||||||||||||||||||||||||||
| 390 | 4 | my $unpack = $self->{UNPACK}; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 4 | my %results; | |||||||||||||||||||||||||||||||||||||||||
| 392 | my $in_range; | ||||||||||||||||||||||||||||||||||||||||||
| 393 | 4 | ▶ | $self->prep_find unless $self->{FIND}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 394 | 4 | ▶ | return {} unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 4 | ▶ | return { map { $_ => {} } @_ } unless @{$self->{FIND}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 3 | ▶ | return $self->bin_find(@_) if @_/@{$self->{FIND}} < $self->{PCT}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 2 | ▶ | my @ips = sort map { $pack->($_) || confess "Bad IP: $_" } @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 2 | my $last; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 2 | for my $ip (@{$self->{FIND}}) { | |||||||||||||||||||||||||||||||||||||||||
| 400 | 5 | ▶ | if ($ips[0] lt $ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | ▶ | $results{$unpack->(shift @ips)} = $self->_in_range($last) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@ips and $ips[0] lt $ip
MC/DC100%2/2
@ips and $ips[0] lt $ip
@ips
$ips[0] lt $ip
Truth table100%3/3
A and B
A= @ipsB= $ips[0] lt $ip
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | while @ips and $ips[0] lt $ip; | ||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 5 | ▶ | last unless @ips; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 3 | $last = $ip; | |||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 2 | ▶ | if (@ips) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my $no_range = $self->_in_range({}); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | $results{$unpack->(shift @ips)} = $no_range while @ips; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 2 | \%results; | |||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | sub bin_find { | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bin_find: called CC=12 SCAR=26.6
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | 1 | ▶ | return {} unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 1 | ▶ | $self->prep_find unless $self->{FIND}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 1 | ▶ | return { map { $_ => {} } @_ } unless @{$self->{FIND}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | my $pack = $self->{PACK}; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | my $unpack = $self->{UNPACK}; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | my $find = $self->{FIND}; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 1 | my %results; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 1 | ▶ | for my $ip ( map { $pack->($_) || confess "Bad IP: $_" } @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 424 | 1 | my ($start, $end) = (0, $#$find); | |||||||||||||||||||||||||||||||||||||||||
| 425 | 1 | ▶ | $results{$unpack->($ip)} = $self->_in_range, next | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ip ge $find->[$start] and $ip lt $find->[$end]
MC/DC0%0/2
$ip ge $find->[$start] and $ip lt $find->[$end]
$ip ge $find->[$start]
$ip lt $find->[$end]
Truth table33%1/3
A and B
A= $ip ge $find->[$start]B= $ip lt $find->[$end]
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | unless $ip ge $find->[$start] and $ip lt $find->[$end]; | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 1 | ▶ | while ($start < $end) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 2 | my $mid = int(($start+$end)/2); | |||||||||||||||||||||||||||||||||||||||||
| 429 | 2 | ▶ | if ($start == $mid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | ▶ | if ($find->[$end] eq $ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | $start = $end; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | } else { $end = $start } | |||||||||||||||||||||||||||||||||||||||||
| 433 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | ▶ | ($find->[$mid] lt $ip ? $start : $end) = $mid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 437 | 1 | $results{$unpack->($ip)} = $self->_in_range($find->[$start]); | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | \%results; | |||||||||||||||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | sub _in_range { | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_range: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 4 | ▶ | my $ip = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ''
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 4 | ▶ | my $aref = $self->{PREPPED}{$ip} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'PREPPED'}{$ip} || []
MC/DC100%1/1
$self->{'PREPPED'}{$ip} || []
$self->{'PREPPED'}{$ip}
Truth table100%2/2
A
A= $self->{'PREPPED'}{$ip}
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 4 | my $key = join "|", sort @$aref; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 4 | ▶ | $self->{CACHE}{$key} ||= { map { $_ => 1 } @$aref }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"CACHE"}{$key} ||= {map({$_, 1;} @$aref)}
MC/DC0%0/1
$self->{"CACHE"}{$key} ||= {map({$_, 1;} @$aref)}
$self->{"CACHE"}{$key}
Truth table50%1/2
A
A= $self->{"CACHE"}{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | sub prep_find { | ||||||||||||||||||||||||||||||||||||||||||
| 451 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prep_find: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 4 | ▶ | my $pct = shift || 4; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 4
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 4 | $self->{PCT} = $pct/100; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 4 | $self->{FIND} = [ sort keys %{$self->{RANGES}} ]; | |||||||||||||||||||||||||||||||||||||||||
| 455 | 4 | $self->{PREPPED} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 4 | $self->{CACHE} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 4 | my %cache; | |||||||||||||||||||||||||||||||||||||||||
| 458 | my %in_range; | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 4 | for my $ip (@{$self->{FIND}}) { | |||||||||||||||||||||||||||||||||||||||||
| 460 | 10 | my $keys = $self->{RANGES}{$ip}; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 10 | $_ = !$_ for @in_range{@$keys}; | |||||||||||||||||||||||||||||||||||||||||
| 462 | 10 | my @keys = grep $in_range{$_}, keys %in_range; | |||||||||||||||||||||||||||||||||||||||||
| 463 | 10 | my $key_str = join "|", @keys; | |||||||||||||||||||||||||||||||||||||||||
| 464 | 10 | ▶ | $self->{PREPPED}{$ip} = $cache{$key_str} ||= \@keys; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$cache{$key_str} ||= \@keys
MC/DC100%1/1
$cache{$key_str} ||= \@keys
$cache{$key_str}
Truth table100%2/2
A
A= $cache{$key_str}
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 4 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | sub clean { | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clean: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | unless ($self->{PACK}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | my $ip = shift; | |||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | my $cidr = Net::CIDR::Lite->new($ip); | |||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | return $cidr->clean($ip); | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | ▶ | my $ip = $self->{PACK}->(shift) || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'PACK'}->(shift()) || (return)
MC/DC0%0/1
$self->{'PACK'}->(shift()) || (return)
$self->{'PACK'}->(shift())
Truth table0%0/2
A
A= $self->{'PACK'}->(shift())
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | $self->{UNPACK}->($ip); | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 481 | __END__ | ||||||||||||||||||||||||||||||||||||||||||