IP.pm

filter: Statementstmt 94.8% 405 / 427 Branchbran 89.9% 214 / 238 Conditioncond 84.4% 65 / 77 MC/DCmcdc 77.0% 37 / 48 Subroutinesub 100% 48 / 48 Podpod 84.0% 21 / 25 totaltotal 91.5% 790 / 863 CC 160 SCAR 27.1
CC
160
Cov
92%
re6
43.4
_splitplan
27.0
coalesce
15.0
CRAP 15.1
« IP/Lite.pm ↑ Summary IP/InetBase.pm »
1 #!/bin/false
2 # ABSTRACT: Manages IPv4 and IPv6 addresses and subnets
3 # PODNAME: NetAddr::IP
4  
5 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package NetAddr::IP;
9  
10 44 our $VERSION = '4.080_09'; # TRIAL VERSION
11  
12 44 ▶use parent qw(Exporter NetAddr::IP::Lite);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 44 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 44 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 44 ▶use NetAddr::IP::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 $DEFAULT_NETLIMIT_EXP
18 $IPV4_BITS
19 $IPV4_OFFSET
20 $IPV6_BITS
21 $MAX_NETLIMIT_EXP
22 $OCTET_BITS
23 $OCTET_COUNT
24 $RFC3021_THRESHOLD
25 44 );
26 44 ▶use NetAddr::IP::Lite qw( Ones V4mask V4net Zero Zeros );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 44 ▶use NetAddr::IP::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 hasbits
29 ipv6_n2d
30 isIPv4
31 isNewIPv4
32 notcontiguous
33 packzeros
34 shiftleft
35 sub128
36 44 );
37  
38 44 our @EXPORT_OK = qw(Compact Coalesce Zero Zeros Ones V4mask V4net netlimit);
39 44 our $netlimit = $DEFAULT_NETLIMIT_EXP;
40  
41 44 my $ONE_128 = pack 'N4', 0, 0, 0, 1;
42  
43  
44 7 ▶sub netlimit($) {
Subroutine netlimit: called CC=5 SCAR=16.1
Pod documented
45 7 ▶ return undef unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 2 5 7
46 5 ▶ return undef if $_[0] =~ /[^0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[^0-9]/ 1 4 5
47 4 ▶ return undef if $_[0] < $DEFAULT_NETLIMIT_EXP;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] < $NetAddr::IP::DEFAULT_NETLIMIT_EXP 1 3 4
48 3 ▶ return undef if $_[0] > $MAX_NETLIMIT_EXP;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] > $NetAddr::IP::MAX_NETLIMIT_EXP 1 2 3
49 2 $netlimit = $_[0];
50 2 return 2**$netlimit;
51 }
52  
53  
54 #############################################
55 # These are the overload methods, placed here
56 # for convenience.
57 #############################################
58  
59 use overload
60  
61 44 ▶ '@{}' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
62 4 return [ $_[0]->hostenum ];
63 44 };
64  
65  
66 #############################################
67 # End of the overload methods.
68 #############################################
69  
70 # Preloaded methods go here.
71  
72  
73 44 my $full_format = "%04X:%04X:%04X:%04X:%04X:%04X:%D.%D.%D.%D";
74 44 my $full6_format = "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X";
75  
76 # the same formats in lowercase, used while NetAddr::IP::InetBase::lower() is in force
77 44 my $full_lower = lc $full_format;
78 44 my $full6_lower = lc $full6_format;
79  
80 # Storable hooks live at file scope so that "require NetAddr::IP" and
81 # "use NetAddr::IP" serialize the same way. The :old_storable tag removes them.
82 1 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
83 1 my $self = shift;
84 1 return $self->cidr(); # use stringification
85 }
86  
87 1 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
88 1 my ($self, undef, $serial) = @_;
89  
90 1 my $ip = NetAddr::IP->new($serial);
91 1 $self->{addr} = $ip->{addr};
92 1 $self->{mask} = $ip->{mask};
93 1 $self->{isv6} = $ip->{isv6};
94 1 return;
95 }
96  
97 16 ▶sub import {
Subroutine import: called CC=6 SCAR=17.9
98 16 ▶ if (grep { $_ eq ':old_storable' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_storable";} @_) 1 15 16
99 1 warnings::warnif(
100 'deprecated',
101 ':old_storable is deprecated and will be removed in version 5; without it Storable freezes an object as its CIDR text, and data frozen as a plain hash still thaws'
102 );
103 1 @_ = grep { $_ ne ':old_storable' } @_;
104 1 delete $NetAddr::IP::{STORABLE_freeze};
105 1 delete $NetAddr::IP::{STORABLE_thaw};
106 }
107  
108 16 ▶ if (grep { $_ eq ':aton' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":aton";} @_) 1 15 16
109 1 warnings::warnif(
110 'deprecated',
111 ':aton is deprecated and will be removed in version 5; new() accepts inet_aton notation without it, and new_from_aton() takes a packed IPv4 address'
112 );
113 1 $NetAddr::IP::Lite::Accept_Binary_IP = 1;
114 1 @_ = grep { $_ ne ':aton' } @_;
115 }
116 16 ▶ if (grep { $_ eq ':old_nth' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":old_nth";} @_) 2 14 16
117 2 warnings::warnif(
118 'deprecated',
119 ':old_nth is deprecated and will be removed in version 5; without it nth(0) is the first usable host and num() counts only usable hosts'
120 );
121 2 $NetAddr::IP::Lite::Old_nth = 1;
122 2 @_ = grep { $_ ne ':old_nth' } @_;
123 }
124 16 ▶ if (grep { $_ eq ':nofqdn' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":nofqdn";} @_) 1 15 16
125 1 $NetAddr::IP::Lite::NoFQDN = 1;
126 1 @_ = grep { $_ ne ':nofqdn' } @_;
127 }
128 16 NetAddr::IP::InetBase::_case_tags(\@_);
129 16 ▶ if (grep { $_ eq ':rfc3021' } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq ":rfc3021";} @_) 1 15 16
130 1 warnings::warnif(
131 'deprecated',
132 ':rfc3021 is deprecated and no longer needed; hostenum now returns two hosts for /31 and /127 unconditionally'
133 );
134 1 @_ = grep { $_ ne ':rfc3021' } @_;
135 }
136 16 NetAddr::IP->export_to_level(1, @_);
137 }
138  
139 47 ▶sub compact {
Subroutine compact: called CC=2 SCAR=6.9
Pod documented
140 47 _croak_on_class('compact', 'Compact', $_[0]);
141  
142 # a reference in, a reference out; a list in, a list out
143 45 ▶ return compactref($_[0]) if ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 2 43 45
144 43 return @{ compactref(\@_) };
145 }
146  
147 44 *Compact = \&compact;
148  
149 67 ▶sub Coalesce {
Subroutine Coalesce: called CC=1 SCAR=0.0
Pod documented
150 67 return &coalesce;
151 }
152  
153 # a NetAddr::IP::Lite object, so a NetAddr::IP and a subclass count too
154 5543 ▶sub _is_address {
Subroutine _is_address: called CC=2 SCAR=7.2
155 5543 ▶ return blessed $_[0] && $_[0]->isa('NetAddr::IP::Lite');
Condition66%2/3
builtin::blessed($_[0]) && $_[0]->isa('NetAddr::IP::Lite')
!ll&&!rl&&r
11405429
MC/DC50%1/2
builtin::blessed($_[0]) && $_[0]->isa('NetAddr::IP::Lite')
builtin::blessed($_[0]) $_[0]->isa('NetAddr::IP::Lite')
Truth table66%2/3
A && B
A= builtin::blessed($_[0])B= $_[0]->isa('NetAddr::IP::Lite')
ABresult
0X0
100
111
156 }
157  
158 # the class name is not an object: NetAddr::IP->compact(@list) is a mistake
159 192 ▶sub _croak_on_class {
Subroutine _croak_on_class: called CC=4 SCAR=13.9
160 192 my ($method, $function, $arg) = @_;
161 192 ▶ croak("$method: call it on an object or as the function $function, not on the class $arg")
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg and not ref $arg and UNIVERSAL::isa($arg, "NetAddr::IP::Lite") 6 186 192
Condition100%6/6
defined $arg and not ref $arg and UNIVERSAL::isa($arg, "NetAddr::IP::Lite")
!ll&&!rl&&r
120666
defined $arg and not ref $arg
!ll&&!rl&&r
111972
MC/DC100%3/3
defined $arg and not ref $arg and UNIVERSAL::isa($arg, "NetAddr::IP::Lite")
defined $arg not ref $arg UNIVERSAL::isa($arg, "NetAddr::IP::Lite")
Truth table100%4/4
A and not(B) and C
A= defined $argB= not ref $argC= UNIVERSAL::isa($arg, "NetAddr::IP::Lite")
ABCresult
0XX0
10X0
1100
1111
162 if defined $arg && !ref $arg && UNIVERSAL::isa($arg, 'NetAddr::IP::Lite');
163 186 return;
164 }
165  
166 15 ▶sub hostenumref($) {
Subroutine hostenumref: called CC=2 SCAR=6.9
Pod documented
167 15 my $r = _splitref(0, $_[0]);
168  
169 # a /32 or /128 is one host, a /31 or /127 is two (RFC 3021), matching
170 # first, last, nth and num in NetAddr::IP::Lite
171 13 ▶ unless ((notcontiguous($_[0]->{mask}))[1] >= $RFC3021_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((&notcontiguous($_[0]{"mask"}))[1] >= $NetAddr::IP::RFC3021_THRESHOLD) 8 5 13
172 8 splice(@$r, 0, 1);
173 8 splice(@$r, scalar @$r - 1, 1);
174 }
175 13 return $r;
176 }
177  
178 22 ▶sub splitref {
Subroutine splitref: called CC=1 SCAR=0.0
Pod documented
179 22 unshift @_, 0; # mark as no reverse
180 22 goto &_splitref;
181 }
182  
183 1 ▶sub rsplitref {
Subroutine rsplitref: called CC=1 SCAR=0.0
Pod documented
184 1 unshift @_, 1; # mark as reversed
185 1 goto &_splitref;
186 }
187  
188 31 ▶sub split {
Subroutine split: called CC=2 SCAR=7.1
Pod documented
189 31 unshift @_, 0; # mark as no reverse
190 31 my $rv = &_splitref;
191 19 ▶ return $rv ? @$rv : ();
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 19 0 19
192 }
193  
194 5 ▶sub rsplit {
Subroutine rsplit: called CC=2 SCAR=7.1
Pod documented
195 5 unshift @_, 1; # mark as reversed
196 5 my $rv = &_splitref;
197 3 ▶ return $rv ? @$rv : ();
Branch: 1 branch
BranchTrueFalseTotal
$rv ? : 3 0 3
198 }
199  
200 7 ▶sub full($) {
Subroutine full: called CC=4 SCAR=14.0
Pod documented
201 7 ▶ if (!$_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{'isv6'} and isIPv4($_[0]{'addr'})) { } 2 5 7
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
502
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
202 2 my @hex = (unpack('n8', $_[0]->{addr}));
203 2 $hex[9] = $hex[7] & 0xff;
204 2 $hex[8] = $hex[7] >> 8;
205 2 $hex[7] = $hex[6] & 0xff;
206 2 $hex[6] >>= 8;
207 2 ▶ return sprintf($NetAddr::IP::InetBase::Upper ? $full_format : $full_lower, @hex);
Branch: 1 branch
BranchTrueFalseTotal
$NetAddr::IP::InetBase::Upper ? : 2 0 2
208 }
209 else {
210 5 &full6;
211 }
212 }
213  
214 16 ▶sub full6($) {
Subroutine full6: called CC=2 SCAR=6.9
Pod documented
215 16 my @hex = (unpack('n8', $_[0]->{addr}));
216 16 ▶ return sprintf($NetAddr::IP::InetBase::Upper ? $full6_format : $full6_lower, @hex);
Branch: 1 branch
BranchTrueFalseTotal
$NetAddr::IP::InetBase::Upper ? : 14 2 16
217 }
218  
219 3 ▶sub full6m($) {
Subroutine full6m: called CC=2 SCAR=6.9
Pod documented
220 3 my @hex = (unpack('n8', $_[0]->{mask}));
221 3 ▶ return sprintf($NetAddr::IP::InetBase::Upper ? $full6_format : $full6_lower, @hex);
Branch: 1 branch
BranchTrueFalseTotal
$NetAddr::IP::InetBase::Upper ? : 2 1 3
222 }
223  
224 1 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
225  
226 44 1;
227  
228 16 ▶sub do_prefix ($$$) {
Subroutine do_prefix: called CC=7 SCAR=19.8
Pod undocumented
229 16 my $mask = shift;
230 16 my $faddr = shift;
231 16 my $laddr = shift;
232  
233 16 ▶ if ($mask > $OCTET_BITS * 3) {
Branch: 6 branches
BranchTrueFalseTotal
if ($mask > $NetAddr::IP::OCTET_BITS * 3) { } 0 16 16
elsif ($mask == $NetAddr::IP::OCTET_BITS * 3) { } 2 14 16
elsif ($mask > $NetAddr::IP::OCTET_BITS * 2) { } 2 12 14
elsif ($mask == $NetAddr::IP::OCTET_BITS * 2) { } 2 10 12
elsif ($mask > $NetAddr::IP::OCTET_BITS) { } 0 10 10
elsif ($mask == $NetAddr::IP::OCTET_BITS) { } 2 8 10
234 0 return "$faddr->[0].$faddr->[1].$faddr->[2].$faddr->[3]-$laddr->[3]";
235 }
236 elsif ($mask == $OCTET_BITS * 3) {
237 2 return "$faddr->[0].$faddr->[1].$faddr->[2].";
238 }
239 elsif ($mask > $OCTET_BITS * 2) {
240 2 return "$faddr->[0].$faddr->[1].$faddr->[2]-$laddr->[2].";
241 }
242 elsif ($mask == $OCTET_BITS * 2) {
243 2 return "$faddr->[0].$faddr->[1].";
244 }
245 elsif ($mask > $OCTET_BITS) {
246 0 return "$faddr->[0].$faddr->[1]-$laddr->[1].";
247 }
248 elsif ($mask == $OCTET_BITS) {
249 2 return "$faddr->[0].";
250 }
251 else {
252 8 return "$faddr->[0]-$laddr->[0]";
253 }
254 }
255  
256  
257 # RFC 5952 s4.2.2: a single zero group is never shortened to ::
258 44 my $MIN_ZERO_RUN = 2;
259  
260 # shortens the first of the longest runs of zero groups to ::, RFC 5952 s4.2
261 123 ▶sub _compV6 ($) {
Subroutine _compV6: called CC=5 SCAR=16.1
262 123 my @group = split /:/, shift;
263 123 my ($best_at, $best_len, $run_len) = (0, 0, 0);
264 123 for my $i (0 .. $#group) {
265 984 ▶ $run_len = $group[$i] eq '0' ? $run_len + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$group[$i] eq '0' ? : 594 390 984
266 984 ▶ ($best_at, $best_len) = ($i - $run_len + 1, $run_len) if $run_len > $best_len;
Branch: 1 branch
BranchTrueFalseTotal
if $run_len > $best_len 524 460 984
267 }
268 123 ▶ return join ':', @group if $best_len < $MIN_ZERO_RUN;
Branch: 1 branch
BranchTrueFalseTotal
if $best_len < $MIN_ZERO_RUN 12 111 123
269 111 return join(':', @group[ 0 .. $best_at - 1 ]) . '::' . join(':', @group[ $best_at + $best_len .. $#group ]);
270 }
271  
272 40 ▶sub short($) {
Subroutine short: called CC=5 SCAR=16.1
Pod documented
273 40 my $addr = $_[0]->addr;
274 40 ▶ if (!$_[0]->{isv6} && isIPv4($_[0]->{addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})) 3 37 40
Condition66%2/3
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
!ll&&!rl&&r
3703
MC/DC50%1/2
not $_[0]{"isv6"} and isIPv4($_[0]{"addr"})
not $_[0]{"isv6"} isIPv4($_[0]{"addr"})
Truth table66%2/3
not(A) and B
A= not $_[0]{"isv6"}B= isIPv4($_[0]{"addr"})
ABresult
0X0
100
111
275 3 my @o = split(/\./, $addr, $OCTET_COUNT);
276 3 ▶ splice(@o, 1, 2) if $o[1] == 0 and $o[2] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o[1] == 0 and $o[2] == 0 1 2 3
Condition100%3/3
$o[1] == 0 and $o[2] == 0
!ll&&!rl&&r
111
MC/DC100%2/2
$o[1] == 0 and $o[2] == 0
$o[1] == 0 $o[2] == 0
Truth table100%3/3
A and B
A= $o[1] == 0B= $o[2] == 0
ABresult
0X0
100
111
277 3 return join '.', @o;
278 }
279 37 return _compV6($addr);
280 }
281  
282  
283 98 ▶sub canon($) {
Subroutine canon: called CC=3 SCAR=11.0
Pod documented
284 98 my $packed = $_[0]->{addr};
285 98 ▶ return $_[0]->addr unless $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"isv6"} 1 97 98
286  
287 # RFC 5952 s5 mixed form for the mapped prefix only; isIPv4 is true of ::1
288 97 ▶ return packzeros(ipv6_n2d($packed)) if isNewIPv4($packed);
Branch: 1 branch
BranchTrueFalseTotal
if isNewIPv4($packed) 11 86 97
289 86 return lc _compV6($_[0]->addr);
290 }
291  
292  
293 # only applicable to ipV4
294 14 ▶sub prefix($) {
Subroutine prefix: called CC=3 SCAR=11.0
Pod documented
295 14 ▶ return undef if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 0 14 14
296 14 my $mask = (notcontiguous($_[0]->{mask}))[1];
297 14 ▶ return $_[0]->addr if $mask == $IPV6_BITS;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == $NetAddr::IP::IPV6_BITS 1 13 14
298 13 $mask -= $IPV4_OFFSET;
299 13 my @faddr = split(/\./, $_[0]->first->addr);
300 13 my @laddr = split(/\./, $_[0]->broadcast->addr);
301 13 return do_prefix $mask, \@faddr, \@laddr;
302 }
303  
304  
305 # only applicable to ipV4
306 4 ▶sub nprefix($) {
Subroutine nprefix: called CC=3 SCAR=11.0
Pod documented
307 4 ▶ return undef if $_[0]->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} 0 4 4
308 4 my $mask = (notcontiguous($_[0]->{mask}))[1];
309 4 ▶ return $_[0]->addr if $mask == $IPV6_BITS;
Branch: 1 branch
BranchTrueFalseTotal
if $mask == $NetAddr::IP::IPV6_BITS 1 3 4
310 3 $mask -= $IPV4_OFFSET;
311 3 my @faddr = split(/\./, $_[0]->first->addr);
312 3 my @laddr = split(/\./, $_[0]->last->addr);
313 3 return do_prefix $mask, \@faddr, \@laddr;
314 }
315  
316  
317 8 ▶sub wildcard($) {
Subroutine wildcard: called CC=3 SCAR=11.0
Pod documented
318 8 my $copy = $_[0]->copy;
319 8 $copy->{addr} = ~$copy->{mask};
320 8 ▶ $copy->{addr} &= V4net unless $copy->{isv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $copy->{"isv6"} 8 0 8
321 8 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 4 4 8
322 4 return ($_[0]->addr, $copy->addr);
323 }
324 4 return $copy->addr;
325 }
326  
327  
328 # A split plan counts space in units of its smallest piece, up to 2**128 for ::/0 cut into /128s.
329 # Each amount is [ $top, $bits128 ]: $top is the 2**128 bit, which no 128-bit string can hold.
330 577 ▶sub _plan_units {
Subroutine _plan_units: called CC=2 SCAR=6.9
331 577 my ($exponent) = @_;
332 577 ▶ return [ 1, Zeros ] if $exponent == $IPV6_BITS;
Branch: 1 branch
BranchTrueFalseTotal
if $exponent == $NetAddr::IP::IPV6_BITS 2 575 577
333 575 return [ 0, shiftleft($ONE_128, $exponent) ];
334 }
335  
336 945376 ▶sub _plan_units_minus { # undef when $piece is bigger than $space
Subroutine _plan_units_minus: called CC=3 SCAR=11.0
337 945376 my ($space, $piece) = @_;
338 945376 my ($no_borrow, $bits128) = sub128($space->[1], $piece->[1]);
339 945376 ▶ my $top = $space->[0] - $piece->[0] - ($no_borrow ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$no_borrow ? : 945244 132 945376
340 945376 ▶ return if $top < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $top < 0 130 945246 945376
341 945246 return [ $top, $bits128 ];
342 }
343  
344 945330 ▶sub _plan_units_left {
Subroutine _plan_units_left: called CC=2 SCAR=6.9
345 945330 my ($space) = @_;
346 945330 ▶ return $space->[0] || hasbits($space->[1]);
Condition100%3/3
$space->[0] || &hasbits($space->[1])
l!l&&r!l&&!r
294524484
MC/DC100%2/2
$space->[0] || &hasbits($space->[1])
$space->[0] &hasbits($space->[1])
Truth table100%3/3
A || B
A= $space->[0]B= &hasbits($space->[1])
ABresult
1X1
011
000
347 }
348  
349 # input: $naip,
350 # @bits, list of masks for splits
351 #
352 # returns: empty array request will not fit in submitted net
353 # (\@bits,undef) if there is just one plan item i.e. return original net
354 # (\@bits,\%masks) for a real plan
355 #
356 111 ▶sub _splitplan {
Subroutine _splitplan: called CC=27 SCAR=33.0
357 111 my ($ip, @bits) = @_;
358 111 my $addr = $ip->addr();
359 111 my $isV6 = $ip->{isv6};
360 111 ▶ unless (@bits) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@bits) 15 96 111
361 15 ▶ $bits[0] = $isV6 ? $IPV6_BITS : $IPV4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
$isV6 ? : 1 14 15
362 }
363 111 my $basem = $ip->masklen();
364  
365 111 my (%nets, $dif);
366 111 my $denom = 0;
367  
368 111 my ($x, $maddr);
369 111 for my $mask (@bits) {
370 550 ▶ if (ref $mask) { # is a NetAddr::IP
Branch: 2 branches
BranchTrueFalseTotal
if (ref $mask) { } 4 546 550
elsif ($mask = 'NetAddr::IP'->new($addr, $mask, $isV6)) { } 535 11 546
371 4 ▶ $x = $mask->{isv6} ? $mask->{addr} : $mask->{addr} | V4mask;
Branch: 1 branch
BranchTrueFalseTotal
$mask->{'isv6'} ? : 2 2 4
372 4 ($x, $maddr) = notcontiguous($x);
373 4 ▶ return () if $x; # spurious bits
Branch: 1 branch
BranchTrueFalseTotal
if $x 2 2 4
374 2 ▶ $mask = $isV6 ? $maddr : $maddr - $IPV4_OFFSET;
Branch: 1 branch
BranchTrueFalseTotal
$isV6 ? : 1 1 2
375 }
376 elsif ($mask = NetAddr::IP->new($addr, $mask, $isV6)) { # will be undefined if bad mask and will fall into oops!
377 535 $mask = $mask->masklen();
378 }
379 else {
380 11 return (); # oops!
381 }
382 537 $dif = $mask - $basem; # for normalization
383 537 ▶ return () if $dif < 0; # overange nets not allowed
Branch: 1 branch
BranchTrueFalseTotal
if $dif < 0 3 534 537
384 534 ▶ return (\@bits, undef) unless ($dif || $#bits); # return if original net = mask alone
Branch: 1 branch
BranchTrueFalseTotal
unless $dif or $#bits 13 521 534
Condition66%2/3
$dif or $#bits
l!l&&r!l&&!r
521013
MC/DC50%1/2
$dif or $#bits
$dif $#bits
Truth table66%2/3
A or B
A= $difB= $#bits
ABresult
1X1
011
000
385 521 ▶ $denom = $dif if $dif > $denom;
Branch: 1 branch
BranchTrueFalseTotal
if $dif > $denom 324 197 521
386 521 ▶ next if exists $nets{$mask};
Branch: 1 branch
BranchTrueFalseTotal
if exists $nets{$mask} 25 496 521
387 496 $nets{$mask} = $mask - $basem; # for normalization
388 }
389  
390 # $denom is the normalization denominator, since these are all exponents
391 # normalization can use add/subtract to accomplish normalization
392 #
393 # keys of %nets are the masks used by this split
394 # values of %nets are the normalized weighting for
395 # calculating when the split is "full" or complete
396 # %masks values contain the actual masks for each split subnet
397 # @bits contains the masks in the order the user actually wants them
398 #
399 82 my %masks; # calculate masks
400 82 ▶ my $maskbase = $isV6 ? $IPV6_BITS : $IPV4_BITS;
Branch: 1 branch
BranchTrueFalseTotal
$isV6 ? : 23 59 82
401 82 for (keys %nets) {
402 495 $nets{$_} = _plan_units($denom - $nets{$_});
403 495 $masks{$_} = shiftleft(Ones, $maskbase - $_);
404 }
405  
406 82 my @plan;
407 82 my $idx = 0;
408 82 my $space = _plan_units($denom);
409 82 my $limit = 2**$netlimit;
410 PLAN:
411 82 ▶ while (_plan_units_left($space)) { # make a net plan
Branch: 1 branch
BranchTrueFalseTotal
if (_plan_units_left($space)) 748642 68 748710
412 748642 ▶ my $nexmask = ($idx < $#bits) ? $bits[$idx] : $bits[$#bits];
Branch: 1 branch
BranchTrueFalseTotal
$idx < $#bits ? : 438 748204 748642
413 748642 ++$idx;
414 748642 ▶ if (my $left = _plan_units_minus($space, $nets{$nexmask})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $left = _plan_units_minus($space, $nets{$nexmask})) 748631 11 748642
415 748631 ▶ croak('netlimit exceeded') if (push @plan, $nexmask) > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if push(@plan, $nexmask) > $limit 11 748620 748631
416 748620 $space = $left;
417 748620 next;
418 }
419  
420 # a fractional net is needed that is not in the mask list or the replicant
421 TRY:
422 11 for my $try_mask (sort { $a <=> $b } keys %nets) {
423 130 ▶ next TRY if $nexmask > $try_mask;
Branch: 1 branch
BranchTrueFalseTotal
if $nexmask > $try_mask 0 130 130
424 130 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
_plan_units_left($space) and do { do { my $left = _plan_units_minus($space, $nets{$try_mask}) or next TRY; croak("netlimit exceeded") if push(@plan, $try_mask) > $limit; $space = $left } } 196604 8 196612
425 196734 ▶ my $left = _plan_units_minus($space, $nets{$try_mask}) or next TRY;
Branch: 1 branch
BranchTrueFalseTotal
my $left = _plan_units_minus($space, $nets{$try_mask}) or next TRY 196615 119 196734
426 196615 ▶ croak('netlimit exceeded') if (push @plan, $try_mask) > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if push(@plan, $try_mask) > $limit 3 196612 196615
427 196612 $space = $left;
428 } while _plan_units_left($space);
429 }
430 8 ▶ die 'ERROR: miscalculated weights' if _plan_units_left($space);
Branch: 1 branch
BranchTrueFalseTotal
if _plan_units_left($space) 0 8 8
431 }
432 68 ▶ return () if $idx < @bits; # overrange original subnet request
Branch: 1 branch
BranchTrueFalseTotal
if $idx < @bits 4 64 68
433 64 return (\@plan, \%masks);
434 }
435  
436 # input: $rev, # t/f
437 # $naip,
438 # @bits # list of masks for split
439 #
440 74 ▶sub _splitref {
Subroutine _splitref: called CC=7 SCAR=19.5
441 74 my $rev = shift;
442 74 my ($plan, $masks) = &_splitplan;
443  
444 # bug report 82719
445 67 ▶ croak('netmask error: overrange or spurious bits') unless defined $plan;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plan 5 62 67
446 62 my $net = $_[0]->network();
447 62 ▶ return [$net] unless $masks;
Branch: 1 branch
BranchTrueFalseTotal
unless $masks 7 55 62
448 55 my $addr = $net->{addr};
449 55 my $isV6 = $net->{isv6};
450 55 ▶ my @plan = $rev ? reverse @$plan : @$plan;
Branch: 1 branch
BranchTrueFalseTotal
$rev ? : 5 50 55
451  
452 # create splits
453 55 my @ret;
454 55 ▶ while (@plan) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plan) 92964 0 92964
455 92964 my $mask = $masks->{ shift @plan };
456 92964 my $piece = $net->_new($addr, $mask, $isV6);
457  
458 # a CIDR block starts on a multiple of its own size
459 92964 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if &hasbits($addr & ~$mask) 6 92958 92964
460 "netmask error: the piece $piece does not start on its own boundary; order the split plan from the largest piece to the smallest"
461 ) if hasbits($addr & ~$mask);
462 92958 push @ret, $piece;
463 92958 ▶ last unless @plan;
Branch: 1 branch
BranchTrueFalseTotal
unless @plan 49 92909 92958
464 92909 $addr = (sub128($addr, $mask))[1];
465 }
466 49 return \@ret;
467 }
468  
469  
470 61 ▶sub compactref($) {
Subroutine compactref: called CC=7 SCAR=19.5
Pod documented
471 61 my ($first, $rest) = @_;
472 61 _croak_on_class('compactref', 'Compact', $first);
473  
474 # with no list, a method call compacts the invocant alone
475 60 ▶ $rest //= [];
Condition100%2/2
$rest //= []
l!l
852
MC/DC100%1/1
$rest //= []
$rest
Truth table100%2/2
A
A= $rest
Aresult
11
00
476  
477 # a method call compacts the invocant with the list, a function call the list alone
478 60 ▶ my $unr =
Branch: 1 branch
BranchTrueFalseTotal
_is_address($first) && ref $rest eq 'ARRAY' ? : 15 45 60
Condition66%2/3
_is_address($first) and ref $rest eq "ARRAY"
!ll&&!rl&&r
45015
MC/DC50%1/2
_is_address($first) and ref $rest eq "ARRAY"
_is_address($first) ref $rest eq "ARRAY"
Truth table66%2/3
A and B
A= _is_address($first)B= ref $rest eq "ARRAY"
ABresult
0X0
100
111
479 _is_address($first) && ref $rest eq 'ARRAY'
480 ? [ $first, @$rest ]
481 : $first;
482 60 ▶ return [] unless @$unr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$unr 0 60 60
483  
484 # work on network copies so the caller's objects are not modified, and
485 # keep the address families apart: a 128 bit mask comparison would
486 # otherwise merge 0.0.0.0/24 with ::100/120
487 60 my (@v4, @v6);
488 60 for my $entry (@$unr) {
489 54382 my $net = $entry->network;
490 54382 ▶ if ($net->{isv6}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($net->{'isv6'}) { } 4 54378 54382
491 4 push @v6, $net;
492 }
493 else {
494 54378 push @v4, $net;
495 }
496 }
497 60 return [ _merge_sorted(sort @v4), _merge_sorted(sort @v6) ];
498 }
499  
500 # input: sorted list of network objects of one address family
501 # returns: the compacted list
502 #
503 120 ▶sub _merge_sorted {
Subroutine _merge_sorted: called CC=7 SCAR=19.6
504 120 my @r = @_;
505 120 my $changed;
506 120 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$changed and do { do { $changed = 0; for (my $i = 0; $i <= $#r - 1; ++$i) { if ($r[$i]->contains($r[$i + 1])) { splice @r, $i + 1, 1; ++$changed; --$i; } elsif ((&notcontiguous($r[$i]{"mask"}))[1] == (&notcontiguous($r[$i + 1]{"mask"}))[1]) { if (&hasbits($r[$i]{"addr"} ^ $r[$i + 1]{"addr"})) { my $upnet = $r[$i]->copy; $upnet->{"mask"} = &shiftleft($upnet->{"mask"}, 1); if ($upnet->contains($r[$i + 1])) { $r[$i] = $upnet; splice @r, $i + 1, 1; ++$changed; --$i; } ; } else { splice @r, $i + 1, 1; ++$changed; --$i; } ; } ; } } } 146 120 266
507 266 $changed = 0;
508 266 ▶ for (my $i = 0 ; $i <= $#r - 1 ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#r - 1) 108502 266 108768
509 108502 ▶ if ($r[$i]->contains($r[ $i + 1 ])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r[$i]->contains($r[$i + 1])) { } 46 108456 108502
elsif ((&notcontiguous($r[$i]{'mask'}))[1] == (&notcontiguous($r[$i + 1]{'mask'}))[1]) { } 54300 54156 108456
510 46 splice(@r, $i + 1, 1);
511 46 ++$changed;
512 46 --$i;
513 }
514 elsif ((notcontiguous($r[$i]->{mask}))[1] == (notcontiguous($r[ $i + 1 ]->{mask}))[1]) { # masks the same
515 54300 ▶ if (hasbits($r[$i]->{addr} ^ $r[ $i + 1 ]->{addr})) { # if not the same netblock
Branch: 1 branch
BranchTrueFalseTotal
if (&hasbits($r[$i]{'addr'} ^ $r[$i + 1]{'addr'})) { } 54300 0 54300
516 54300 my $upnet = $r[$i]->copy;
517 54300 $upnet->{mask} = shiftleft($upnet->{mask}, 1);
518 54300 ▶ if ($upnet->contains($r[ $i + 1 ])) { # adjacent nets in next net up
Branch: 1 branch
BranchTrueFalseTotal
if ($upnet->contains($r[$i + 1])) 54243 57 54300
519 54243 $r[$i] = $upnet;
520 54243 splice(@r, $i + 1, 1);
521 54243 ++$changed;
522 54243 --$i;
523 }
524 }
525 else { # identical nets
526 0 splice(@r, $i + 1, 1);
527 0 ++$changed;
528 0 --$i;
529 }
530 }
531 }
532 } while $changed;
533 120 return @r;
534 }
535  
536  
537 84 ▶sub coalesce {
Subroutine coalesce: called CC=15 SCAR=27.1
Pod documented
538 84 my $masklen = shift;
539 84 _croak_on_class('coalesce', 'Coalesce', $masklen);
540 81 ▶ if (_is_address($masklen)) { # if called as a method
Branch: 1 branch
BranchTrueFalseTotal
if (_is_address($masklen)) 14 67 81
541 14 push @_, $masklen;
542 14 $masklen = shift;
543 }
544  
545 81 my $number = shift;
546  
547 # Addresses are at @_
548 81 ▶ return [] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 80 81
549  
550 80 ▶ croak("coalesce: masklen must be an integer from 0 to $IPV6_BITS")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $masklen and $masklen =~ /^[0-9]{1,3}$/ and $masklen <= $NetAddr::IP::IPV6_BITS 5 75 80
Condition100%6/6
defined $masklen and $masklen =~ /^[0-9]{1,3}$/ and $masklen <= $NetAddr::IP::IPV6_BITS
!ll&&!rl&&r
4175
defined $masklen and $masklen =~ /^[0-9]{1,3}$/
!ll&&!rl&&r
1376
MC/DC100%3/3
defined $masklen and $masklen =~ /^[0-9]{1,3}$/ and $masklen <= $NetAddr::IP::IPV6_BITS
defined $masklen $masklen =~ /^[0-9]{1,3}$/ $masklen <= $NetAddr::IP::IPV6_BITS
Truth table100%4/4
A and B and C
A= defined $masklenB= $masklen =~ /^[0-9]{1,3}$/C= $masklen <= $NetAddr::IP::IPV6_BITS
ABCresult
0XX0
10X0
1100
1111
551 unless defined $masklen && $masklen =~ m|^[0-9]{1,3}$| && $masklen <= $IPV6_BITS;
552 75 ▶ croak("coalesce: number must be a non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number and $number =~ /^[0-9]+$/ 3 72 75
Condition100%3/3
defined $number and $number =~ /^[0-9]+$/
!ll&&!rl&&r
1272
MC/DC100%2/2
defined $number and $number =~ /^[0-9]+$/
defined $number $number =~ /^[0-9]+$/
Truth table100%3/3
A and B
A= defined $numberB= $number =~ /^[0-9]+$/
ABresult
0X0
100
111
553 unless defined $number && $number =~ m|^[0-9]+$|;
554 croak("coalesce: arguments must be NetAddr::IP objects")
555 72 ▶ if grep { !_is_address($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not _is_address($_);} @_ 2 70 72
556 croak("coalesce: masklen $masklen exceeds the $IPV4_BITS bits of the IPv4 arguments")
557 70 ▶ if $masklen > $IPV4_BITS && grep { !$_->{isv6} } @_;
Branch: 1 branch
BranchTrueFalseTotal
if $masklen > $NetAddr::IP::IPV4_BITS and grep {not $_->{"isv6"};} @_ 2 68 70
Condition100%3/3
$masklen > $NetAddr::IP::IPV4_BITS and grep {not $_->{"isv6"};} @_
!ll&&!rl&&r
5992
MC/DC100%2/2
$masklen > $NetAddr::IP::IPV4_BITS and grep {not $_->{"isv6"};} @_
$masklen > $NetAddr::IP::IPV4_BITS grep {not $_->{"isv6"};} @_
Truth table100%3/3
A and B
A= $masklen > $NetAddr::IP::IPV4_BITSB= grep {not $_->{"isv6"};} @_
ABresult
0X0
100
111
558 68 my (@v4, @v6);
559 68 for my $ip (@_) {
560 5388 ▶ if ($ip->{isv6}) { push @v6, $ip }
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->{'isv6'}) { } 32 5356 5388
561 5356 else { push @v4, $ip }
562 }
563  
564 return [
565 59 ▶ (@v4 ? @{ _coalesce_family($masklen, $number, \@v4) } : ()),
Branch: 1 branch
BranchTrueFalseTotal
@v6 ? : 12 56 68
566 68 ▶ (@v6 ? @{ _coalesce_family($masklen, $number, \@v6) } : ()),
Branch: 1 branch
BranchTrueFalseTotal
@v4 ? : 59 9 68
567 ];
568 }
569  
570 71 ▶sub _coalesce_family {
Subroutine _coalesce_family: called CC=8 SCAR=20.8
571 71 my ($masklen, $number, $list) = @_;
572 71 my $type = $list->[0]->{isv6};
573 71 my %ret = ();
574 71 for my $ip (@{ _drop_nested($masklen, $list) }) {
575 5360 my $n = NetAddr::IP->new($ip->addr . '/' . $masklen)->network;
576 5360 ▶ if ($ip->masklen > $masklen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->masklen > $masklen) 5347 13 5360
577  
578 # the size of the subnet, which is not ->num, since ->num
579 # excludes the network and broadcast addresses
580 5347 ▶ $ret{$n} += 2**(($type ? $IPV6_BITS : $IPV4_BITS) - $ip->masklen);
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 27 5320 5347
581 }
582 }
583  
584 71 my @ret = ();
585  
586 # Add to @ret any arguments with netmasks longer than our argument
587 71 for my $c (
588 2 sort { $a->masklen <=> $b->masklen }
589 5388 grep { $_->masklen <= $masklen } @$list
590 ) {
591 13 ▶ next if grep { $_->contains($c) } @ret;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->contains($c);} @ret 2 11 13
592 11 push @ret, $c->network;
593 }
594  
595 # Now add to @ret all the subnets with more than $number hits
596 71 for my $c (
597 1083 map { NetAddr::IP->new($_) }
598 1616 grep { $ret{$_} >= $number }
599 sort keys %ret
600 ) {
601 1083 ▶ next if grep { $_->contains($c) } @ret;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->contains($c);} @ret 7 1076 1083
602 1076 push @ret, $c;
603 }
604  
605 71 return [ sort @ret ];
606 }
607  
608 # input: mask length, reference to a list of one address family
609 # returns: the list in its order, less each subnet longer than the mask
610 # length that repeats or lies inside another such subnet
611 71 ▶sub _drop_nested {
Subroutine _drop_nested: called CC=5 SCAR=16.1
612 71 my ($masklen, $list) = @_;
613 71 my (%nested, @key);
614 71 my @longer = grep { $list->[$_]->masklen > $masklen } 0 .. $#$list;
615  
616 # network, then mask. The raw-address order used by cmp sorts a
617 # subnet with host bits after an address inside it
618 $key[$_] = ($list->[$_]->{addr} & $list->[$_]->{mask}) . $list->[$_]->{mask}
619 71 for @longer;
620  
621 # this order puts a subnet before those inside it, so checking each
622 # against the last subnet kept finds every repeat and nested subnet
623 71 my $outer;
624 71 for my $i (sort { $key[$a] cmp $key[$b] } @longer) {
625 5375 ▶ if (defined $outer && $outer->contains($list->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outer and $outer->contains($list->[$i])) { } 28 5347 5375
Condition100%3/3
defined $outer and $outer->contains($list->[$i])
!ll&&!rl&&r
67528028
MC/DC100%2/2
defined $outer and $outer->contains($list->[$i])
defined $outer $outer->contains($list->[$i])
Truth table100%3/3
A and B
A= defined $outerB= $outer->contains($list->[$i])
ABresult
0X0
100
111
626 28 $nested{$i} = 1;
627 }
628 else {
629 5347 $outer = $list->[$i];
630 }
631 }
632 71 return [ @{$list}[ grep { !$nested{$_} } 0 .. $#$list ] ];
633 }
634  
635  
636 13 ▶sub hostenum ($) {
Subroutine hostenum: called CC=1 SCAR=0.0
Pod documented
637 13 return @{ $_[0]->hostenumref };
638 }
639  
640  
641 14 ▶sub re ($) {
Subroutine re: called CC=8 SCAR=21.2
Pod documented
642 14 ▶ return &re6 if $_[0]->{isv6} || !isIPv4($_[0]->{addr});
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"isv6"} or not isIPv4($_[0]{"addr"}) 4 10 14
Condition66%2/3
$_[0]{"isv6"} or not isIPv4($_[0]{"addr"})
l!l&&r!l&&!r
4010
MC/DC50%1/2
$_[0]{"isv6"} or not isIPv4($_[0]{"addr"})
$_[0]{"isv6"} not isIPv4($_[0]{"addr"})
Truth table66%2/3
A or not(B)
A= $_[0]{"isv6"}B= not isIPv4($_[0]{"addr"})
ABresult
1X1
011
000
643 10 my $self = shift->network; # Insure a "zero" host part
644 10 my ($addr, $mlen) = ($self->addr, $self->masklen);
645 10 my @o = split('\.', $addr, $OCTET_COUNT);
646  
647 10 my $octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
648 10 my @r = @o;
649 10 my $d;
650  
651 10 ▶ if ($mlen != $IPV4_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mlen != $NetAddr::IP::IPV4_BITS) 8 2 10
652 8 ▶ if ($mlen > $OCTET_BITS * 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mlen > $NetAddr::IP::OCTET_BITS * 3) { } 3 5 8
653 3 $d = 2**($IPV4_BITS - $mlen) - 1;
654 3 $r[3] = '(?:' . join('|', ($o[3] .. $o[3] + $d)) . ')';
655 }
656 else {
657 5 $r[3] = $octet;
658 5 ▶ if ($mlen > $OCTET_BITS * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mlen > $NetAddr::IP::OCTET_BITS * 2) { } 4 1 5
659 4 $d = 2**($OCTET_BITS * 3 - $mlen) - 1;
660 4 $r[2] = '(?:' . join('|', ($o[2] .. $o[2] + $d)) . ')';
661 }
662 else {
663 1 $r[2] = $octet;
664 1 ▶ if ($mlen > $OCTET_BITS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mlen > $NetAddr::IP::OCTET_BITS) { } 0 1 1
665 0 $d = 2**($OCTET_BITS * 2 - $mlen) - 1;
666 0 $r[1] = '(?:' . join('|', ($o[1] .. $o[1] + $d)) . ')';
667 }
668 else {
669 1 $r[1] = $octet;
670 1 ▶ if ($mlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mlen > 0) { } 0 1 1
671 0 $d = 2**($OCTET_BITS - $mlen) - 1;
672 0 $r[0] = '(?:' . join('|', ($o[0] .. $o[0] + $d)) . ')';
673 }
674 1 else { $r[0] = $octet; }
675 }
676 }
677 }
678 }
679  
680 ### no digit, and no digit followed by a dot, before nor after
681 ### (look-behind, look-ahead) so that an address embedded in a longer
682 ### dotted string such as 1.10.1.2.3 does not match
683 10 return "(?:(?<![0-9])(?<![0-9]\\.)$r[0]\\.$r[1]\\.$r[2]\\.$r[3](?![0-9])(?!\\.[0-9]))";
684 }
685  
686  
687 160 ▶sub re6($) {
Subroutine re6: called CC=36 SCAR=37.7
Pod documented
688 160 my @net = split('', sprintf("%04X%04X%04X%04X%04X%04X%04X%04X", unpack('n8', $_[0]->network->{addr})));
689 160 my @brd = split('', sprintf("%04X%04X%04X%04X%04X%04X%04X%04X", unpack('n8', $_[0]->broadcast->{addr})));
690  
691 160 my @dig;
692  
693 160 for (0 .. $#net) {
694 5120 my $n = $net[$_];
695 5120 my $b = $brd[$_];
696 5120 my $m;
697 5120 ▶ if ($n . '' eq $b . '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($n . '' eq $b . '') { } 4782 338 5120
698 4782 ▶ if ($n =~ /[0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /[0-9]/) { } 4330 452 4782
699 4330 push @dig, $n;
700 }
701 else {
702 452 push @dig, '[' . (lc $n) . $n . ']';
703 }
704 }
705 else {
706 338 my $n = $net[$_];
707 338 my $b = $brd[$_];
708 338 ▶ if ($n . '' eq 0 && $b =~ /F/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($n . '' eq 0 and $b =~ /F/) { } 255 83 338
elsif ($n =~ /[0-9]/ and $b =~ /[0-9]/) { } 59 24 83
elsif ($n =~ /[A-F]/ and $b =~ /[A-F]/) { } 10 14 24
elsif ($n =~ /[0-9]/ and $b =~ /[A-F]/) { } 14 0 14
elsif ($n =~ /[A-F]/ and $b =~ /[0-9]/) { } 0 0 0
Condition100%3/3
$n . '' eq 0 and $b =~ /F/
!ll&&!rl&&r
3647255
MC/DC100%2/2
$n . '' eq 0 and $b =~ /F/
$n . '' eq 0 $b =~ /F/
Truth table100%3/3
A and B
A= $n . '' eq 0B= $b =~ /F/
ABresult
0X0
100
111
709 255 push @dig, 'x';
710 }
711 ▶ elsif ($n =~ /[0-9]/ && $b =~ /[0-9]/) {
Condition100%3/3
$n =~ /[0-9]/ and $b =~ /[0-9]/
!ll&&!rl&&r
101459
MC/DC100%2/2
$n =~ /[0-9]/ and $b =~ /[0-9]/
$n =~ /[0-9]/ $b =~ /[0-9]/
Truth table100%3/3
A and B
A= $n =~ /[0-9]/B= $b =~ /[0-9]/
ABresult
0X0
100
111
712 59 push @dig, '[' . $n . '-' . $b . ']';
713 }
714 ▶ elsif ($n =~ /[A-F]/ && $b =~ /[A-F]/) {
Condition66%2/3
$n =~ /[A-F]/ and $b =~ /[A-F]/
!ll&&!rl&&r
14010
MC/DC50%1/2
$n =~ /[A-F]/ and $b =~ /[A-F]/
$n =~ /[A-F]/ $b =~ /[A-F]/
Truth table66%2/3
A and B
A= $n =~ /[A-F]/B= $b =~ /[A-F]/
ABresult
0X0
100
111
715 10 $n .= '-' . $b;
716 10 push @dig, '[' . (lc $n) . $n . ']';
717 }
718 ▶ elsif ($n =~ /[0-9]/ && $b =~ /[A-F]/) {
Condition33%1/3
$n =~ /[0-9]/ and $b =~ /[A-F]/
!ll&&!rl&&r
0014
MC/DC0%0/2
$n =~ /[0-9]/ and $b =~ /[A-F]/
$n =~ /[0-9]/ $b =~ /[A-F]/
Truth table33%1/3
A and B
A= $n =~ /[0-9]/B= $b =~ /[A-F]/
ABresult
0X0
100
111
719 14 ▶ $m = ($n == 9) ? 9 : $n . '-9';
Branch: 1 branch
BranchTrueFalseTotal
$n == 9 ? : 0 14 14
720 14 ▶ if ($b =~ /A/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ /A/) { } 0 14 14
721 0 $m .= 'aA';
722 }
723 else {
724 14 $b = 'A-' . $b;
725 14 $m .= (lc $b) . $b;
726 }
727 14 push @dig, '[' . $m . ']';
728 }
729 ▶ elsif ($n =~ /[A-F]/ && $b =~ /[0-9]/) {
Condition0%0/3
$n =~ /[A-F]/ and $b =~ /[0-9]/
!ll&&!rl&&r
000
MC/DC0%0/2
$n =~ /[A-F]/ and $b =~ /[0-9]/
$n =~ /[A-F]/ $b =~ /[0-9]/
Truth table0%0/3
A and B
A= $n =~ /[A-F]/B= $b =~ /[0-9]/
ABresult
0X0
100
111
730 0 ▶ if ($n =~ /A/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /A/) { } 0 0 0
731 0 $m = 'aA';
732 }
733 else {
734 0 $n .= '-F';
735 0 $m = (lc $n) . $n;
736 }
737 0 ▶ if ($b == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 9) { } 0 0 0
738 0 $m .= 9;
739 }
740 else {
741 0 $m .= $b . '-9';
742 }
743 0 push @dig, '[' . $m . ']';
744 }
745 }
746 }
747 160 ▶ my @zok = map { join('', @net[ $_ * 4 .. $_ * 4 + 3 ]) eq '0000' ? 1 : 0 } 0 .. 7;
Branch: 1 branch
BranchTrueFalseTotal
join('', @net[$_ * 4 .. $_ * 4 + 3]) eq '0000' ? : 962 318 1280
748  
749 160 my @grp;
750 160 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@dig > 0 and do { do { my(@g) = splice(@dig, 0, 4); my $zeros = 0; while (@g and $g[0] eq 0) { shift @g; ++$zeros; } ; my $wild = 0; while (@g and $g[-1] eq "x") { pop @g; ++$wild; } ; my $grp; if (not @g) { $grp = $wild ? "[0-9a-fA-F]{1,$wild}" : "0{1,4}"; } elsif ($wild and @g == 1 and $g[0] =~ /^\[0/) { $grp = '(?:' . $g[0] . '[0-9a-fA-F]{' . $wild . '}|[0-9a-fA-F]{1,' . $wild . '})'; } else { $grp = join("", @g); $grp .= "[0-9a-fA-F]{$wild}" if $wild; } ; $grp = "0{0,$zeros}" . $grp if $zeros and $grp ne "0{1,4}"; push @grp, $grp } } 1120 160 1280
751 1280 my @g = splice(@dig, 0, 4);
752 1280 my $zeros = 0;
753 1280 ▶ while (@g and $g[0] eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if (@g and $g[0] eq 0) 3979 1280 5259
Condition100%3/3
@g and $g[0] eq 0
!ll&&!rl&&r
9223583979
MC/DC100%2/2
@g and $g[0] eq 0
@g $g[0] eq 0
Truth table100%3/3
A and B
A= @gB= $g[0] eq 0
ABresult
0X0
100
111
754 3979 shift @g;
755 3979 ++$zeros;
756 }
757 1280 my $wild = 0;
758 1280 ▶ while (@g and $g[-1] eq 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if (@g and $g[-1] eq "x") 255 1280 1535
Condition100%3/3
@g and $g[-1] eq "x"
!ll&&!rl&&r
961319255
MC/DC100%2/2
@g and $g[-1] eq "x"
@g $g[-1] eq "x"
Truth table100%3/3
A and B
A= @gB= $g[-1] eq "x"
ABresult
0X0
100
111
759 255 pop @g;
760 255 ++$wild;
761 }
762 1280 my $grp;
763 1280 ▶ if (!@g) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @g) { } 961 319 1280
elsif ($wild and @g == 1 and $g[0] =~ /^\[0/) { } 1 318 319
764 961 ▶ $grp = $wild ? "[0-9a-fA-F]{1,$wild}" : '0{1,4}';
Branch: 1 branch
BranchTrueFalseTotal
$wild ? : 39 922 961
765 }
766 ▶ elsif ($wild and @g == 1 and $g[0] =~ /^\[0/) {
Condition100%6/6
$wild and @g == 1 and $g[0] =~ /^\[0/
!ll&&!rl&&r
31621
$wild and @g == 1
!ll&&!rl&&r
236803
MC/DC100%3/3
$wild and @g == 1 and $g[0] =~ /^\[0/
$wild @g == 1 $g[0] =~ /^\[0/
Truth table100%4/4
A and B and C
A= $wildB= @g == 1C= $g[0] =~ /^\[0/
ABCresult
0XX0
10X0
1100
1111
767 1 $grp = '(?:' . $g[0] . '[0-9a-fA-F]{' . $wild . '}|[0-9a-fA-F]{1,' . $wild . '})';
768 }
769 else {
770 318 $grp = join('', @g);
771 318 ▶ $grp .= "[0-9a-fA-F]{$wild}" if $wild;
Branch: 1 branch
BranchTrueFalseTotal
if $wild 82 236 318
772 }
773 1280 ▶ $grp = "0{0,$zeros}" . $grp if $zeros and $grp ne '0{1,4}';
Branch: 1 branch
BranchTrueFalseTotal
if $zeros and $grp ne "0{1,4}" 158 1122 1280
Condition100%3/3
$zeros and $grp ne "0{1,4}"
!ll&&!rl&&r
200922158
MC/DC100%2/2
$zeros and $grp ne "0{1,4}"
$zeros $grp ne "0{1,4}"
Truth table100%3/3
A and B
A= $zerosB= $grp ne "0{1,4}"
ABresult
0X0
100
111
774 1280 push @grp, $grp;
775 } while @dig > 0;
776  
777 160 my @alt = (join(':', @grp));
778 160 for my $i (0 .. $#grp) {
779 1280 ▶ next unless $zok[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $zok[$i] 318 962 1280
780 962 for my $j ($i .. $#grp) {
781 4286 ▶ last unless $zok[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $zok[$j] 912 3374 4286
782 3374 push @alt, join(':', @grp[ 0 .. $i - 1 ]) . '::' . join(':', @grp[ $j + 1 .. $#grp ]);
783 }
784 }
785  
786 # look-arounds as in re(): no match inside a longer address
787 return
788 160 '(?:(?<![0-9a-fA-F])(?<![0-9a-fA-F]:)(?:'
789 . join('|', @alt)
790 . ')(?![0-9a-fA-F])(?!:[0-9a-fA-F:])(?!\\.[0-9]))';
791 }
792  
793 2 ▶sub mod_version {
Subroutine mod_version: called CC=1 SCAR=4.1
Pod undocumented
794 2 return $NetAddr::IP::VERSION;
795 0 &Compact; # suppress warnings about these symbols
796 0 &Coalesce;
797 0 &STORABLE_freeze;
798 0 &STORABLE_thaw;
799 }
800  
801  
802 44 1;
803  
804 __END__
« IP/Lite.pm ↑ Summary IP/InetBase.pm »