blib/lib/Net/CIDR/Lite.pm

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