blib/lib/Net/CIDR/Lite.pm

filter: Statementstmt 93.3% 310 / 332 Branchbran 75.2% 140 / 186 Conditioncond 63.0% 46 / 73 MC/DCmcdc 33.3% 14 / 42 Subroutinesub 94.4% 34 / 36 Podpod 100% 14 / 14 totaltotal 81.7% 558 / 683 CC 135 SCAR 22.1
CC
135
Cov
84%
_pack_ipv6
20.8
add_cidr
20.0
bin_find
14.2
CRAP 9.1
↑ Summary
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.22';
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 20 ▶ my $proto = shift;
Subroutine new: called CC=3 SCAR=11.4
Pod documented
16 20 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
17 20 my $self = bless {}, $class;
18 20 $self->add_any($_) for @_;
19 19 $self;
20 }
21  
22 sub add_any {
23 16 ▶ my $self = shift;
Subroutine add_any: called CC=5 SCAR=16.3
Pod documented
24 16 for (@_) {
25 16 ▶ tr|/|| && do { $self->add($_); next };
Branch: 1 branch
BranchTrueFalseTotal
tr[/][] and do { $self->add($_); next } 9 7 16
26 7 ▶ tr|-|| && do { $self->add_range($_); next };
Branch: 1 branch
BranchTrueFalseTotal
tr/\-// and do { $self->add_range($_); next } 2 5 7
27 5 ▶ UNIVERSAL::isa($_, 'Net::CIDR::Lite') && do {
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "Net::CIDR::Lite") and do { $self->add_cidr($_); next } 0 5 5
28 0 $self->add_cidr($_); next
29 0 };
30 5 $self->add_ip($_), next;
31 }
32 15 $self;
33 }
34  
35 sub add {
36 22 ▶ my $self = shift;
Subroutine add: called CC=8 SCAR=21.2
Pod documented
37 22 my ($ip, $mask) = split "/", shift;
38 22 ▶ $self->_init($ip) || confess "Can't determine ip format" unless %$self;
Branch: 1 branch
BranchTrueFalseTotal
unless %$self 15 7 22
Condition66%4/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
l!l
7150
$self->_init($ip) or confess("Can't determine ip format")
l!l
1230
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")
ABCresult
1XX1
01X1
0011
0000
39 confess "Bad mask $mask"
40 19 ▶ unless $mask =~ /^\d+$/ and $mask <= $self->{NBITS}-8;
Branch: 1 branch
BranchTrueFalseTotal
unless $mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8 1 18 19
Condition66%2/3
$mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8
!ll&&!rl&&r
0118
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
ABresult
0X0
100
111
41 18 $mask += 8;
42 18 ▶ my $start = $self->{PACK}->($ip) & $self->{MASKS}[$mask]
Branch: 1 branch
BranchTrueFalseTotal
my $start = $self->{"PACK"}->($ip) & $self->{"MASKS"}[$mask] or confess("Bad ip address: $ip") 0 18 18
43 or confess "Bad ip address: $ip";
44 18 my $end = $self->_add_bit($start, $mask);
45 18 ▶ ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 4 14 18
46 18 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 18 18
47 18 $self;
48 }
49  
50 sub clean {
51 7 ▶ my $self = shift;
Subroutine clean: called CC=4 SCAR=13.9
Pod documented
52 7 ▶ return $self unless $self->{RANGES};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"RANGES"} 3 4 7
53 4 my $ranges = $$self{RANGES};
54 4 my $total;
55 $$self{RANGES} = {
56 4 map { $total ? ($total+=$$ranges{$_})? () : ($_=>-1)
57 12 ▶ : do { $total+=$$ranges{$_}; ($_=>1) }
Branch: 2 branches
BranchTrueFalseTotal
$total ? : 6 6 12
($total += $ranges->{$_}) ? : 0 6 6
58 } sort keys %$ranges
59 };
60 4 $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
BranchTrueFalseTotal
unless $self->{"NBITS"} 1 4 5
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
BranchTrueFalseTotal
unless $total 4 4 8
71 8 $total += $$self{RANGES}{$ip};
72 8 ▶ unless ($total) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total) 4 4 8
73 4 ▶ while ($start lt $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start lt $ip) 5 4 9
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
BranchTrueFalseTotal
not vec($start, $sbit ^ 7, 1) and $sbit > 0 and do { --$sbit } 142 5 147
Condition100%3/3
not vec($start, $sbit ^ 7, 1) and $sbit > 0
!ll&&!rl&&r
41142
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $end le $ip 5 112 117
81 }
82 5 push @results, $self->{UNPACK}->($start) . "/$bits";
83 5 $start = $end;
84 }
85 }
86 }
87 4 ▶ wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
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
BranchTrueFalseTotal
unless $total 3 3 6
96 6 $total += $$self{RANGES}{$ip};
97 6 ▶ unless ($total) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total) 3 3 6
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
BranchTrueFalseTotal
wantarray ? : 3 0 3
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
BranchTrueFalseTotal
unless $total 6 6 12
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
BranchTrueFalseTotal
unless ($total) 6 6 12
122 6 ▶ while ($ip gt $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip gt $start) 10 6 16
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
BranchTrueFalseTotal
if ($ip eq $start) 3 7 10
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
BranchTrueFalseTotal
if ($ip eq ($start & $self->{'MASKS'}[32])) { } 3 4 7
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
BranchTrueFalseTotal
wantarray ? : 3 0 3
152 }
153  
154 sub _init {
155 19 ▶ my $self = shift;
Subroutine _init: called CC=4 SCAR=13.9
156 19 my $ip = shift;
157 19 my ($nbits, $pack, $unpack);
158 19 ▶ if (_pack_ipv4($ip)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_pack_ipv4($ip)) { } 13 6 19
elsif (_pack_ipv6($ip)) { } 3 3 6
159 13 $nbits = 40;
160 13 $pack = \&_pack_ipv4;
161 13 $unpack = \&_unpack_ipv4;
162 } elsif (_pack_ipv6($ip)) {
163 3 $nbits = 136;
164 3 $pack = \&_pack_ipv6;
165 3 $unpack = \&_unpack_ipv6;
166 } else {
167 3 return;
168 }
169 16 $$self{PACK} = $pack;
170 16 $$self{UNPACK} = $unpack;
171 16 $$self{NBITS} = $nbits;
172 $$self{MASKS} = $masks{$nbits} ||= [
173 16 ▶ 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)]
l!l
124
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}
Aresult
11
00
174 } 0..$nbits
175 ];
176 16 $$self{RANGES} = {};
177 16 $self;
178 }
179  
180 sub _pack_ipv4 {
181 57 ▶ my @nums = split /\./, shift(), -1;
Subroutine _pack_ipv4: called CC=6 SCAR=18.1
182 57 ▶ return unless @nums == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums == 4 3 54 57
183 54 for (@nums) {
184 200 ▶ return unless /^\d{1,3}$/ and !/^0\d{1,2}$/ and $_ <= 255;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255 6 194 200
Condition66%4/6
/^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255
!ll&&!rl&&r
60194
/^\d{1,3}$/ and not /^0\d{1,2}$/
!ll&&!rl&&r
06194
MC/DC33%1/3
/^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255
/^\d{1,3}$/ not /^0\d{1,2}$/ $_ <= 255
Truth table50%2/4
A and not(B) and C
A= /^\d{1,3}$/B= not /^0\d{1,2}$/C= $_ <= 255
ABCresult
0XX0
10X0
1100
1111
185 }
186 48 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 12 ▶ my $ip = shift;
Subroutine _pack_ipv6: called CC=16 SCAR=30.3
195 12 $ip =~ s/^::$/::0/;
196 12 ▶ return if $ip =~ /^:/ and $ip !~ s/^::/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /^:/ and not $ip =~ s/^::/:/ 0 12 12
Condition66%2/3
$ip =~ /^:/ and not $ip =~ s/^::/:/
!ll&&!rl&&r
1020
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/^::/:/
ABresult
0X0
100
111
197 12 ▶ return if $ip =~ /:$/ and $ip !~ s/::$/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /:$/ and not $ip =~ s/::$/:/ 0 12 12
Condition33%1/3
$ip =~ /:$/ and not $ip =~ s/::$/:/
!ll&&!rl&&r
1200
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/::$/:/
ABresult
0X0
100
111
198 12 my @nums = split /:/, $ip, -1;
199 12 ▶ return unless @nums <= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums <= 8 0 12 12
200 12 my ($empty, $ipv4, $str) = (0,'','');
201 12 for (@nums) {
202 63 ▶ return if $ipv4;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 0 63 63
203 63 ▶ $str .= "0" x (4-length) . $_, next if /^[a-fA-F\d]{1,4}$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[a-fA-F\d]{1,4}$/ 58 5 63
204 5 ▶ do { return if $empty++ }, $str .= "X", next if $_ eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $_ eq "" 2 3 5
if $empty++ 0 2 2
205 3 ▶ next if $ipv4 = _pack_ipv4($_);
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 = _pack_ipv4($_) 0 3 3
206 3 return;
207 }
208 9 ▶ return if $ipv4 and @nums > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 and @nums > 6 0 9 9
Condition33%1/3
$ipv4 and @nums > 6
!ll&&!rl&&r
900
MC/DC0%0/2
$ipv4 and @nums > 6
$ipv4 @nums > 6
Truth table33%1/3
A and B
A= $ipv4B= @nums > 6
ABresult
0X0
100
111
209 9 ▶ $str =~ s/X/"0" x (($ipv4 ? 25 : 33)-length($str))/e if $empty;
Branch: 2 branches
BranchTrueFalseTotal
if $empty 2 7 9
$ipv4 ? : 0 2 2
210 9 pack("H*", "00" . $str).$ipv4;
211 }
212  
213 sub _unpack_ipv6 {
214 3 ▶ _compress_ipv6(join(":", unpack("xH*", shift) =~ /..../g)),
Subroutine _unpack_ipv6: called CC=1 SCAR=0.0
215 }
216  
217 # Replace longest run of null blocks with a double colon
218 sub _compress_ipv6 {
219 3 ▶ my $ip = shift;
Subroutine _compress_ipv6: called CC=4 SCAR=15.2
220 3 ▶ if (my @runs = $ip =~ /((?:(?:^|:)(?:0000))+:?)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@runs) = $ip =~ /((?:(?:^|:)(?:0000))+:?)/g) 3 0 3
221 3 my $max = $runs[0];
222 3 for (@runs[1..$#runs]) {
223 0 ▶ $max = $_ if length($max) < length;
Branch: 1 branch
BranchTrueFalseTotal
if length $max < length $_ 0 0 0
224 }
225 3 $ip =~ s/$max/::/;
226 }
227 3 $ip =~ s/:0{1,3}/:/g;
228 3 $ip;
229 }
230  
231 # Add a single IP address
232 sub add_ip {
233 5 ▶ my $self = shift;
Subroutine add_ip: called CC=6 SCAR=18.6
Pod documented
234 5 my $ip = shift;
235 5 ▶ $self->_init($ip) || confess "Can't determine ip format" unless %$self;
Branch: 1 branch
BranchTrueFalseTotal
unless %$self 2 3 5
Condition50%3/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
l!l
320
$self->_init($ip) or confess("Can't determine ip format")
l!l
200
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")
ABCresult
1XX1
01X1
0011
0000
236 5 ▶ my $start = $self->{PACK}->($ip) or confess "Bad ip address: $ip";
Branch: 1 branch
BranchTrueFalseTotal
my $start = $self->{"PACK"}->($ip) or confess("Bad ip address: $ip") 0 5 5
237 5 my $end = $self->_add_bit($start, $self->{NBITS});
238 5 ▶ ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 1 4 5
239 5 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 5 5
240 5 $self;
241 }
242  
243 # Add a hyphenated range of IP addresses
244 sub add_range {
245 3 ▶ my $self = shift;
Subroutine add_range: called CC=9 SCAR=23.8
Pod documented
246 3 local $_ = shift;
247 3 my ($ip_start, $ip_end, $crud) = split /\s*-\s*/;
248 3 ▶ confess "Only one hyphen allowed in range" if defined $crud;
Branch: 1 branch
BranchTrueFalseTotal
if defined $crud 0 3 3
249 3 ▶ $self->_init($ip_start) || confess "Can't determine ip format"
Branch: 1 branch
BranchTrueFalseTotal
unless %$self 2 1 3
Condition50%3/6
%$self || ($self->_init($ip_start) || confess(q[Can't determine ip format]))
l!l
120
$self->_init($ip_start) or confess("Can't determine ip format")
l!l
200
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")
ABCresult
1XX1
01X1
0011
0000
250 unless %$self;
251 3 ▶ my $start = $self->{PACK}->($ip_start)
Branch: 1 branch
BranchTrueFalseTotal
my $start = $self->{"PACK"}->($ip_start) or confess("Bad ip address: $ip_start") 0 3 3
252 or confess "Bad ip address: $ip_start";
253 3 ▶ my $end = $self->{PACK}->($ip_end)
Branch: 1 branch
BranchTrueFalseTotal
my $end = $self->{"PACK"}->($ip_end) or confess("Bad ip address: $ip_end") 0 3 3
254 or confess "Bad ip address: $ip_end";
255 3 ▶ confess "Start IP is greater than end IP" if $start gt $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start gt $end 0 3 3
256 3 $end = $self->_add_bit($end, $$self{NBITS});
257 3 ▶ ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 0 3 3
258 3 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 3 3
259 3 $self;
260 }
261  
262 # Add ranges from another Net::CIDR::Lite object
263 sub add_cidr {
264 0 ▶ my $self = shift;
Subroutine add_cidr: not called CC=4 SCAR=30.0
Pod documented
265 0 my $cidr = shift;
266 0 ▶ confess "Not a CIDR object" unless UNIVERSAL::isa($cidr, 'Net::CIDR::Lite');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($cidr, "Net::CIDR::Lite") 0 0 0
267 0 ▶ unless (%$self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$self) 0 0 0
268 0 @$self{@fields} = @$cidr{@fields};
269 }
270 0 $$self{RANGES}{$_} += $$cidr{RANGES}{$_} for keys %{$$cidr{RANGES}};
271 0 $self;
272 }
273  
274 # Increment the ip address at the given bit position
275 # bit position is in range 1 to # of bits in ip
276 # where 1 is high order bit, # of bits is low order bit
277 sub _add_bit {
278 182 ▶ my $self= shift;
Subroutine _add_bit: called CC=3 SCAR=11.0
279 182 my $base= shift();
280 182 my $bits= shift()-1;
281 182 ▶ while (vec($base, $bits^7, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $base, $bits ^ 7, 1) 382 178 560
282 382 vec($base, $bits^7, 1) = 0;
283 382 $bits--;
284 382 ▶ return $base if $bits < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bits < 0 4 378 382
285 }
286 178 vec($base, $bits^7, 1) = 1;
287 178 return $base;
288 }
289  
290 # Subtract one from an ip address
291 sub _minus_one {
292 13 ▶ my $self = shift;
Subroutine _minus_one: called CC=1 SCAR=0.0
293 13 my $nbits = $self->{NBITS};
294 13 my $ip = shift;
295 13 $ip = ~$ip;
296 13 $ip = $self->_add_bit($ip, $nbits);
297 13 $ip = $self->_add_bit($ip, $nbits);
298 13 $self->_add_bit(~$ip, $nbits);
299 }
300  
301 sub find {
302 5 ▶ my $self = shift;
Subroutine find: called CC=6 SCAR=17.9
Pod documented
303 5 ▶ $self->prep_find unless $self->{FIND};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FIND"} 2 3 5
304 5 ▶ return $self->bin_find(@_) unless @{$self->{FIND}} < $self->{PCT};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} < $self->{"PCT"} 0 5 5
305 5 ▶ return 0 unless $self->{PACK};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PACK"} 1 4 5
306 4 my $this_ip = $self->{PACK}->(shift);
307 4 my $ranges = $self->{RANGES};
308 4 my $last = -1;
309 4 for my $ip (@{$self->{FIND}}) {
310 7 ▶ last if $this_ip lt $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $this_ip lt $ip 3 4 7
311 4 $last = $ranges->{$ip};
312 }
313 4 $last > 0;
314 }
315  
316 sub bin_find {
317 4 ▶ my $self = shift;
Subroutine bin_find: called CC=8 SCAR=20.8
Pod documented
318 4 my $ip = $self->{PACK}->(shift);
319 4 ▶ $self->prep_find unless $self->{FIND};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FIND"} 1 3 4
320 4 my $find = $self->{FIND};
321 4 my ($start, $end) = (0, $#$find);
322 4 ▶ return unless $ip ge $find->[$start] and $ip lt $find->[$end];
Branch: 1 branch
BranchTrueFalseTotal
unless $ip ge $find->[$start] and $ip lt $find->[$end] 2 2 4
Condition100%3/3
$ip ge $find->[$start] and $ip lt $find->[$end]
!ll&&!rl&&r
112
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]
ABresult
0X0
100
111
323 2 ▶ while ($end - $start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $start > 0) 4 2 6
324 4 my $mid = int(($start+$end)/2);
325 4 ▶ if ($start == $mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == $mid) { } 2 2 4
326 2 ▶ if ($find->[$end] eq $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find->[$end] eq $ip) { } 1 1 2
327 1 $start = $end;
328 1 } else { $end = $start }
329 } else {
330 2 ▶ ($find->[$mid] lt $ip ? $start : $end) = $mid;
Branch: 1 branch
BranchTrueFalseTotal
$find->[$mid] lt $ip ? : 1 1 2
331 }
332 }
333 2 $self->{RANGES}{$find->[$start]} > 0;
334 }
335  
336 sub prep_find {
337 3 ▶ my $self = shift;
Subroutine prep_find: called CC=3 SCAR=11.0
Pod documented
338 3 $self->clean;
339 3 ▶ $self->{PCT} = shift || 20;
Condition50%1/2
shift() || 20
l!l
03
MC/DC0%0/1
shift() || 20
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
340 3 my $aref = $self->{FIND} = [];
341 3 push @$aref, $_ for sort keys %{$self->{RANGES}};
342 3 $self;
343 }
344  
345 sub spanner {
346 2 ▶ Net::CIDR::Lite::Span->new(@_);
Subroutine spanner: called CC=1 SCAR=0.0
Pod documented
347 }
348  
349 sub _ranges {
350 3 ▶ sort keys %{shift->{RANGES}};
Subroutine _ranges: called CC=1 SCAR=0.0
351 }
352  
353 2 ▶sub _packer { shift->{PACK} }
Subroutine _packer: called CC=1 SCAR=0.0
354 2 ▶sub _unpacker { shift->{UNPACK} }
Subroutine _unpacker: called CC=1 SCAR=0.0
355  
356 package Net::CIDR::Lite::Span;
357 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
358  
359 sub new {
360 2 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.4
361 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
362 2 my $self = bless {RANGES=>{}}, $class;
363 2 $self->add(@_);
364 }
365  
366 sub add {
367 3 ▶ my $self = shift;
Subroutine add: called CC=7 SCAR=19.5
368 3 my $ranges = $self->{RANGES};
369 3 ▶ if (@_ && !$self->{PACK}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not $self->{"PACK"}) 2 1 3
Condition66%2/3
@_ and not $self->{"PACK"}
!ll&&!rl&&r
012
MC/DC50%1/2
@_ and not $self->{"PACK"}
@_ not $self->{"PACK"}
Truth table66%2/3
A and not(B)
A= @_B= not $self->{"PACK"}
ABresult
0X0
100
111
370 2 my $cidr = $_[0];
371 2 ▶ $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cidr 0 2 2
372 2 $self->{PACK} = $cidr->_packer;
373 2 $self->{UNPACK} = $cidr->_unpacker;
374 }
375 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
376 3 my ($cidr, $label) = (shift, shift);
377 3 ▶ $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cidr 1 2 3
378 3 $cidr->clean;
379 3 for my $ip ($cidr->_ranges) {
380 4 push @{$ranges->{$ip}}, $label;
381 }
382 }
383 3 $self;
384 }
385  
386 sub find {
387 4 ▶ my $self = shift;
Subroutine find: called CC=13 SCAR=26.1
388 4 my $pack = $self->{PACK};
389 4 my $unpack = $self->{UNPACK};
390 4 my %results;
391 my $in_range;
392 4 ▶ $self->prep_find unless $self->{FIND};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FIND"} 2 2 4
393 4 ▶ return {} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
394 4 ▶ return { map { $_ => {} } @_ } unless @{$self->{FIND}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} 1 3 4
395 3 ▶ return $self->bin_find(@_) if @_/@{$self->{FIND}} < $self->{PCT};
Branch: 1 branch
BranchTrueFalseTotal
if @_ / @{$self->{"FIND"};} < $self->{"PCT"} 1 2 3
396 2 ▶ my @ips = sort map { $pack->($_) || confess "Bad IP: $_" } @_;
Branch: 1 branch
BranchTrueFalseTotal
&$pack($_) or confess("Bad IP: $_") 0 3 3
397 2 my $last;
398 2 for my $ip (@{$self->{FIND}}) {
399 5 ▶ if ($ips[0] lt $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ips[0] lt $ip) 3 2 5
400 3 ▶ $results{$unpack->(shift @ips)} = $self->_in_range($last)
Branch: 1 branch
BranchTrueFalseTotal
@ips and $ips[0] lt $ip and do { $results{&$unpack(shift @ips)} = $self->_in_range($last) } 3 3 6
Condition100%3/3
@ips and $ips[0] lt $ip
!ll&&!rl&&r
213
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
ABresult
0X0
100
111
401 while @ips and $ips[0] lt $ip;
402 }
403 5 ▶ last unless @ips;
Branch: 1 branch
BranchTrueFalseTotal
unless @ips 2 3 5
404 3 $last = $ip;
405 }
406 2 ▶ if (@ips) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ips) 0 2 2
407 0 my $no_range = $self->_in_range({});
408 0 ▶ $results{$unpack->(shift @ips)} = $no_range while @ips;
Branch: 1 branch
BranchTrueFalseTotal
@ips and do { $results{&$unpack(shift @ips)} = $no_range } 0 0 0
409 }
410 2 \%results;
411 }
412  
413 sub bin_find {
414 1 ▶ my $self = shift;
Subroutine bin_find: called CC=12 SCAR=26.6
415 1 ▶ return {} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
416 1 ▶ $self->prep_find unless $self->{FIND};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FIND"} 0 1 1
417 1 ▶ return { map { $_ => {} } @_ } unless @{$self->{FIND}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} 0 1 1
418 1 my $pack = $self->{PACK};
419 1 my $unpack = $self->{UNPACK};
420 1 my $find = $self->{FIND};
421 1 my %results;
422 1 ▶ for my $ip ( map { $pack->($_) || confess "Bad IP: $_" } @_) {
Branch: 1 branch
BranchTrueFalseTotal
&$pack($_) or confess("Bad IP: $_") 0 1 1
423 1 my ($start, $end) = (0, $#$find);
424 1 ▶ $results{$unpack->($ip)} = $self->_in_range, next
Branch: 1 branch
BranchTrueFalseTotal
unless $ip ge $find->[$start] and $ip lt $find->[$end] 0 1 1
Condition33%1/3
$ip ge $find->[$start] and $ip lt $find->[$end]
!ll&&!rl&&r
001
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]
ABresult
0X0
100
111
425 unless $ip ge $find->[$start] and $ip lt $find->[$end];
426 1 ▶ while ($start < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < $end) 2 1 3
427 2 my $mid = int(($start+$end)/2);
428 2 ▶ if ($start == $mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == $mid) { } 1 1 2
429 1 ▶ if ($find->[$end] eq $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find->[$end] eq $ip) { } 1 0 1
430 1 $start = $end;
431 0 } else { $end = $start }
432 } else {
433 1 ▶ ($find->[$mid] lt $ip ? $start : $end) = $mid;
Branch: 1 branch
BranchTrueFalseTotal
$find->[$mid] lt $ip ? : 0 1 1
434 }
435 }
436 1 $results{$unpack->($ip)} = $self->_in_range($find->[$start]);
437 }
438 1 \%results;
439 }
440  
441 sub _in_range {
442 4 ▶ my $self = shift;
Subroutine _in_range: called CC=4 SCAR=13.9
443 4 ▶ my $ip = shift || '';
Condition100%2/2
shift() || ''
l!l
31
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
444 4 ▶ my $aref = $self->{PREPPED}{$ip} || [];
Condition100%2/2
$self->{'PREPPED'}{$ip} || []
l!l
31
MC/DC100%1/1
$self->{'PREPPED'}{$ip} || []
$self->{'PREPPED'}{$ip}
Truth table100%2/2
A
A= $self->{'PREPPED'}{$ip}
Aresult
11
00
445 4 my $key = join "|", sort @$aref;
446 4 ▶ $self->{CACHE}{$key} ||= { map { $_ => 1 } @$aref };
Condition50%1/2
$self->{"CACHE"}{$key} ||= {map({$_, 1;} @$aref)}
l!l
04
MC/DC0%0/1
$self->{"CACHE"}{$key} ||= {map({$_, 1;} @$aref)}
$self->{"CACHE"}{$key}
Truth table50%1/2
A
A= $self->{"CACHE"}{$key}
Aresult
11
00
447 }
448  
449 sub prep_find {
450 4 ▶ my $self = shift;
Subroutine prep_find: called CC=5 SCAR=16.1
451 4 ▶ my $pct = shift || 4;
Condition100%2/2
shift() || 4
l!l
13
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
452 4 $self->{PCT} = $pct/100;
453 4 $self->{FIND} = [ sort keys %{$self->{RANGES}} ];
454 4 $self->{PREPPED} = {};
455 4 $self->{CACHE} = {};
456 4 my %cache;
457 my %in_range;
458 4 for my $ip (@{$self->{FIND}}) {
459 10 my $keys = $self->{RANGES}{$ip};
460 10 $_ = !$_ for @in_range{@$keys};
461 10 my @keys = grep $in_range{$_}, keys %in_range;
462 10 my $key_str = join "|", @keys;
463 10 ▶ $self->{PREPPED}{$ip} = $cache{$key_str} ||= \@keys;
Condition100%2/2
$cache{$key_str} ||= \@keys
l!l
28
MC/DC100%1/1
$cache{$key_str} ||= \@keys
$cache{$key_str}
Truth table100%2/2
A
A= $cache{$key_str}
Aresult
11
00
464 }
465 4 $self;
466 }
467  
468 sub clean {
469 0 ▶ my $self = shift;
Subroutine clean: not called CC=3 SCAR=24.8
470 0 ▶ unless ($self->{PACK}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"PACK"}) 0 0 0
471 0 my $ip = shift;
472 0 my $cidr = Net::CIDR::Lite->new($ip);
473 0 return $cidr->clean($ip);
474 }
475 0 ▶ my $ip = $self->{PACK}->(shift) || return;
Condition0%0/2
$self->{'PACK'}->(shift()) || (return)
l!l
00
MC/DC0%0/1
$self->{'PACK'}->(shift()) || (return)
$self->{'PACK'}->(shift())
Truth table0%0/2
A
A= $self->{'PACK'}->(shift())
Aresult
11
00
476 0 $self->{UNPACK}->($ip);
477 }
478  
479 3 1;
480 __END__
↑ Summary