blib/lib/Net/CIDR/Lite.pm

filter: Statementstmt 93.3% 311 / 333 Branchbran 76.3% 145 / 190 Conditioncond 68.4% 52 / 76 MC/DCmcdc 38.6% 17 / 44 Subroutinesub 94.4% 34 / 36 Podpod 100% 14 / 14 totaltotal 82.6% 573 / 693 CC 138 SCAR 22.4
CC
138
Cov
85%
_pack_ipv6
21.6
add_cidr
20.0
bin_find
14.2
CRAP 9.4
↑ 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.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
l!l&&r!l&&!r
0260
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 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
BranchTrueFalseTotal
tr[/][] and do { $self->add($_); next } 14 8 22
26 8 ▶ tr|-|| && do { $self->add_range($_); next };
Branch: 1 branch
BranchTrueFalseTotal
tr/\-// and do { $self->add_range($_); next } 2 6 8
27 6 ▶ UNIVERSAL::isa($_, 'Net::CIDR::Lite') && do {
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, "Net::CIDR::Lite") and do { $self->add_cidr($_); next } 0 6 6
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
BranchTrueFalseTotal
unless %$self 20 7 27
Condition66%4/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
l!l
7200
$self->_init($ip) or confess("Can't determine ip format")
l!l
1460
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 21 ▶ unless $mask =~ /^\d+$/ and $mask <= $self->{NBITS}-8;
Branch: 1 branch
BranchTrueFalseTotal
unless $mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8 1 20 21
Condition66%2/3
$mask =~ /^\d+$/ and $mask <= $self->{"NBITS"} - 8
!ll&&!rl&&r
0120
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 20 $mask += 8;
42 20 ▶ 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 20 20
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
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 4 16 20
46 20 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 20 20
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
BranchTrueFalseTotal
unless $self->{"RANGES"} 3 6 9
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
BranchTrueFalseTotal
$total ? : 8 8 16
($total += $ranges->{$_}) ? : 0 8 8
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
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 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
BranchTrueFalseTotal
if (_pack_ipv4($ip)) { } 13 12 25
elsif (_pack_ipv6($ip)) { } 5 7 12
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)]
l!l
144
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 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
BranchTrueFalseTotal
unless @nums == 4 7 65 72
183 65 for (@nums) {
184 238 ▶ 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 230 236
Condition83%5/6
/^\d{1,3}$/ and not /^0\d{1,2}$/ and $_ <= 255
!ll&&!rl&&r
80230
/^\d{1,3}$/ and not /^0\d{1,2}$/
!ll&&!rl&&r
26230
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $ip =~ /^:/ and not $ip =~ s/^::/:/ 0 25 25
Condition66%2/3
$ip =~ /^:/ and not $ip =~ s/^::/:/
!ll&&!rl&&r
14110
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 25 ▶ return if $ip =~ /:$/ and $ip !~ s/::$/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $ip =~ /:$/ and not $ip =~ s/::$/:/ 0 25 25
Condition33%1/3
$ip =~ /:$/ and not $ip =~ s/::$/:/
!ll&&!rl&&r
2500
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 25 my @nums = split /:/, $ip, -1;
199 25 ▶ return unless @nums <= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @nums <= 8 0 25 25
200 25 my ($empty, $ipv4, $str) = (0,'','');
201 25 for (@nums) {
202 101 ▶ return if $ipv4;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 0 101 101
203 101 ▶ $str .= "0" x (4-length) . $_, next if /^[a-fA-F\d]{1,4}$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[a-fA-F\d]{1,4}$/ 78 23 101
204 23 ▶ do { return if $empty++ }, $str .= "X", next if $_ eq '';
Branch: 2 branches
BranchTrueFalseTotal
if $_ eq "" 11 12 23
if $empty++ 0 11 11
205 12 ▶ next if $ipv4 = _pack_ipv4($_);
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 = _pack_ipv4($_) 9 3 12
206 3 return;
207 }
208 22 ▶ return if $ipv4 and @nums > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $ipv4 and @nums > 6 0 22 22
Condition66%2/3
$ipv4 and @nums > 6
!ll&&!rl&&r
1390
MC/DC0%0/2
$ipv4 and @nums > 6
$ipv4 @nums > 6
Truth table66%2/3
A and B
A= $ipv4B= @nums > 6
ABresult
0X0
100
111
209 22 ▶ return unless $empty or @nums == ($ipv4 ? 6 : 8);
Branch: 2 branches
BranchTrueFalseTotal
unless $empty or @nums == ($ipv4 ? 6 : 8) 4 18 22
$ipv4 ? : 0 11 11
Condition100%3/3
$empty or @nums == ($ipv4 ? 6 : 8)
l!l&&r!l&&!r
1174
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)
ABresult
1X1
011
000
210 18 ▶ $str =~ s/X/"0" x (($ipv4 ? 25 : 33)-length($str))/e if $empty;
Branch: 2 branches
BranchTrueFalseTotal
if $empty 11 7 18
$ipv4 ? : 9 2 11
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
BranchTrueFalseTotal
if (my(@runs) = $ip =~ /((?:(?:^|:)(?:0000))+:?)/g) 3 0 3
222 3 my $max = $runs[0];
223 3 for (@runs[1..$#runs]) {
224 0 ▶ $max = $_ if length($max) < length;
Branch: 1 branch
BranchTrueFalseTotal
if length $max < length $_ 0 0 0
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
BranchTrueFalseTotal
unless %$self 3 3 6
Condition66%4/6
%$self || ($self->_init($ip) || confess(q[Can't determine ip format]))
l!l
330
$self->_init($ip) or confess("Can't determine ip format")
l!l
210
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
237 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
238 5 my $end = $self->_add_bit($start, $self->{NBITS});
239 5 ▶ ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 1 4 5
240 5 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 5 5
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
BranchTrueFalseTotal
if defined $crud 0 3 3
250 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
251 unless %$self;
252 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
253 or confess "Bad ip address: $ip_start";
254 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
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
BranchTrueFalseTotal
if $start gt $end 0 3 3
257 3 $end = $self->_add_bit($end, $$self{NBITS});
258 3 ▶ ++$$self{RANGES}{$start} || delete $$self{RANGES}{$start};
Branch: 1 branch
BranchTrueFalseTotal
++$self->{"RANGES"}{$start} or delete $self->{"RANGES"}{$start} 0 3 3
259 3 ▶ --$$self{RANGES}{$end} || delete $$self{RANGES}{$end};
Branch: 1 branch
BranchTrueFalseTotal
--$self->{"RANGES"}{$end} or delete $self->{"RANGES"}{$end} 0 3 3
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
BranchTrueFalseTotal
unless UNIVERSAL::isa($cidr, "Net::CIDR::Lite") 0 0 0
268 0 ▶ unless (%$self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$self) 0 0 0
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
BranchTrueFalseTotal
if (vec $base, $bits ^ 7, 1) 383 180 563
283 383 vec($base, $bits^7, 1) = 0;
284 383 $bits--;
285 383 ▶ return $base if $bits < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bits < 0 4 379 383
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
BranchTrueFalseTotal
unless $self->{"FIND"} 4 6 10
305 10 ▶ return $self->bin_find(@_) unless @{$self->{FIND}} < $self->{PCT};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} < $self->{"PCT"} 0 10 10
306 10 ▶ return 0 unless $self->{PACK};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PACK"} 1 9 10
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
BranchTrueFalseTotal
if $this_ip lt $ip 6 11 17
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
BranchTrueFalseTotal
unless $self->{"FIND"} 1 3 4
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
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
324 2 ▶ while ($end - $start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end - $start > 0) 4 2 6
325 4 my $mid = int(($start+$end)/2);
326 4 ▶ if ($start == $mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == $mid) { } 2 2 4
327 2 ▶ if ($find->[$end] eq $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find->[$end] eq $ip) { } 1 1 2
328 1 $start = $end;
329 1 } else { $end = $start }
330 } else {
331 2 ▶ ($find->[$mid] lt $ip ? $start : $end) = $mid;
Branch: 1 branch
BranchTrueFalseTotal
$find->[$mid] lt $ip ? : 1 1 2
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
l!l
05
MC/DC0%0/1
shift() || 20
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
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
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
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
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
371 2 my $cidr = $_[0];
372 2 ▶ $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cidr 0 2 2
373 2 $self->{PACK} = $cidr->_packer;
374 2 $self->{UNPACK} = $cidr->_unpacker;
375 }
376 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
377 3 my ($cidr, $label) = (shift, shift);
378 3 ▶ $cidr = Net::CIDR::Lite->new($cidr) unless ref($cidr);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cidr 1 2 3
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
BranchTrueFalseTotal
unless $self->{"FIND"} 2 2 4
394 4 ▶ return {} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
395 4 ▶ return { map { $_ => {} } @_ } unless @{$self->{FIND}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} 1 3 4
396 3 ▶ return $self->bin_find(@_) if @_/@{$self->{FIND}} < $self->{PCT};
Branch: 1 branch
BranchTrueFalseTotal
if @_ / @{$self->{"FIND"};} < $self->{"PCT"} 1 2 3
397 2 ▶ my @ips = sort map { $pack->($_) || confess "Bad IP: $_" } @_;
Branch: 1 branch
BranchTrueFalseTotal
&$pack($_) or confess("Bad IP: $_") 0 3 3
398 2 my $last;
399 2 for my $ip (@{$self->{FIND}}) {
400 5 ▶ if ($ips[0] lt $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ips[0] lt $ip) 3 2 5
401 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
402 while @ips and $ips[0] lt $ip;
403 }
404 5 ▶ last unless @ips;
Branch: 1 branch
BranchTrueFalseTotal
unless @ips 2 3 5
405 3 $last = $ip;
406 }
407 2 ▶ if (@ips) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ips) 0 2 2
408 0 my $no_range = $self->_in_range({});
409 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
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
BranchTrueFalseTotal
unless @_ 0 1 1
417 1 ▶ $self->prep_find unless $self->{FIND};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FIND"} 0 1 1
418 1 ▶ return { map { $_ => {} } @_ } unless @{$self->{FIND}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"FIND"};} 0 1 1
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
BranchTrueFalseTotal
&$pack($_) or confess("Bad IP: $_") 0 1 1
424 1 my ($start, $end) = (0, $#$find);
425 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
426 unless $ip ge $find->[$start] and $ip lt $find->[$end];
427 1 ▶ while ($start < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < $end) 2 1 3
428 2 my $mid = int(($start+$end)/2);
429 2 ▶ if ($start == $mid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start == $mid) { } 1 1 2
430 1 ▶ if ($find->[$end] eq $ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find->[$end] eq $ip) { } 1 0 1
431 1 $start = $end;
432 0 } else { $end = $start }
433 } else {
434 1 ▶ ($find->[$mid] lt $ip ? $start : $end) = $mid;
Branch: 1 branch
BranchTrueFalseTotal
$find->[$mid] lt $ip ? : 0 1 1
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() || ''
l!l
31
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
445 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
446 4 my $key = join "|", sort @$aref;
447 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
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
l!l
13
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
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
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
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
BranchTrueFalseTotal
unless ($self->{"PACK"}) 0 0 0
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)
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
477 0 $self->{UNPACK}->($ip);
478 }
479  
480 3 1;
481 __END__
↑ Summary