blib/lib/Net/IPAM/Block.pm

filter: Statementstmt 94.3% 149 / 158 Branchbran 85.7% 72 / 84 Conditioncond 94.4% 17 / 18 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 94.1% 32 / 34 Podpod 100% 21 / 21 totaltotal 92.0% 301 / 327 CC 51 SCAR 18.7
CC
51
Cov
92%
diff
17.0
iter
12.0
merge
9.0
CRAP 6.5
↑ Summary Build »
1 package Net::IPAM::Block;
2  
3 14 our $VERSION = '6.00';
4  
5 14 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Net::IPAM::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Net::IPAM::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Net::IPAM::Block::Private ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 our @EXPORT_OK = qw(sort_block merge aggregate);
18  
19 =head1 NAME
20  
21 Net::IPAM::Block - A library for reading, formatting, sorting, ... and much more for IP-blocks.
22  
23 =head1 SYNOPSIS
24  
25 use Net::IPAM::Block;
26  
27 # parse and normalize
28 $cidr = Net::IPAM::Block->new('10.0.0.0/255.0.0.0') // die 'wrong format,';
29 $cidr = Net::IPAM::Block->new('10.0.0.0/8') // die 'wrong format,';
30 $range = Net::IPAM::Block->new('fe80::2-fe80::e') // die 'wrong format,';
31 $host = Net::IPAM::Block->new('2001:db8::1') // die 'wrong format,';
32  
33 =head1 DESCRIPTION
34  
35 A block is an IP-network or IP-range, e.g.
36  
37 192.168.0.1/255.255.255.0 # network, with IP mask
38 192.168.0.1/24 # network, with CIDR mask
39 ::1/128 # network, with CIDR mask
40 10.0.0.3-10.0.17.134 # range
41 2001:db8::1-2001:db8::f6 # range
42  
43 The parsed block is represented as an object with:
44  
45 base
46 last
47  
48 This representation is fast sortable without conversions to/from the different IP versions.
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 $b = Net::IPAM::Block->new('fe80::/10');
55  
56 new() parses the input as CIDR, range or address (or IP object, see below) and returns the Net::IPAM::Block object.
57  
58 Example for valid input strings:
59  
60 2001:db8:dead::/38
61 10.0.0.0/8
62 10.0.0.0/255.0.0.0
63  
64 2001:db8::1-2001:db8::ff00:35
65 192.168.2.3-192.168.7.255
66  
67 If a begin-end range can be represented as a CIDR, new() calculates the netmask and returns the range as CIDR block with a proper mask.
68  
69 Plain IP addresses as input strings or objects are converted to /32 or /128 CIDRs.
70  
71 0.0.0.0 => 0.0.0.0/32
72 ::ffff:127.0.0.1 => 127.0.0.1/32
73 :: => ::/128
74 Net::IPAM::IP->new('1.2.3.4') => 1.2.3.4/32
75  
76  
77 $range = Net::IPAM::Block->new('10.2.0.17-10.3.67.255') // die 'wrong block format,';
78 $range = Net::IPAM::Block->new('fe80::-fe80::1234') // die 'wrong block format,';
79  
80 $cidr_24 = Net::IPAM::Block->new('10.0.0.0/24') // die 'wrong block format,';
81 $cidr_32 = Net::IPAM::Block->new('192.168.0.1') // die 'wrong block format,';
82 $cidr_128 = Net::IPAM::Block->new('2001:db8::1') // die 'wrong block format,';
83  
84 $cidr_128 = Net::IPAM::Block->new( Net::IPAM::IP->new('2001:db8::1') // die 'wrong IP format,' );
85  
86 Returns undef on illegal input.
87  
88 =cut
89  
90 sub new {
91 380 ▶ my $self = bless( {}, $_[0] );
Subroutine new: called CC=5 SCAR=16.1
Pod documented
92 380 ▶ my $input = $_[1] // Carp::croak 'missing argument';
Condition66%2/3
$_[1] // Carp::croak("missing argument")
l!l&&r!l&&!r
37820
MC/DC0%0/2
$_[1] // Carp::croak("missing argument")
$_[1] Carp::croak("missing argument")
Truth table33%1/3
A // B
A= $_[1]B= Carp::croak("missing argument")
ABresult
1X1
011
000
93  
94 378 ▶ return Net::IPAM::Block::Private::_fromIP( $self, $input ) if ref $input;
Branch: 1 branch
BranchTrueFalseTotal
if ref $input 4 374 378
95  
96 # handle mask: 2001:db8::/32, 10.0.0.0/8, 10.0.0.0/255.0.0.0
97 374 my $idx = index( $input, '/' );
98 374 ▶ return Net::IPAM::Block::Private::_fromMask( $self, $input, $idx )
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= 0 228 146 374
99 if $idx >= 0;
100  
101 # handle range: 192.168.1.17-192.168.1.35
102 146 $idx = index( $input, '-' );
103 146 ▶ return Net::IPAM::Block::Private::_fromRange( $self, $input, $idx )
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= 0 81 65 146
104 if $idx >= 0;
105  
106 # handle address: fe80::1
107 65 return Net::IPAM::Block::Private::_fromAddr( $self, $input );
108 }
109  
110 =head2 version
111  
112 $v = $b->version
113  
114 Returns 4 or 6.
115  
116 =cut
117  
118 # just return the version from the base IP
119 sub version {
120 253 ▶ return $_[0]->{base}->version;
Subroutine version: called CC=1 SCAR=0.0
Pod documented
121 }
122  
123 =head2 to_string
124  
125 Returns the block in canonical form.
126  
127 say Net::IPAM::Block->new('fe80::aa/10')->to_string; # fe80::/10
128 say Net::IPAM::Block->new('1.2.3.4-1.2.3.36')->to_string; # 1.2.3.4-1.2.3.36
129 say Net::IPAM::Block->new('127.0.0.1')->to_string; # 127.0.0.1/32
130  
131 Stringification is overloaded with L</"to_string">
132  
133 my $b = Net::IPAM::Block->new('fe80::/10');
134 say $b; # fe80::/10
135  
136 =cut
137  
138 sub to_string {
139 182 ▶ my $self = shift;
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
140  
141 182 ▶ if ( $self->is_cidr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_cidr) 156 26 182
142 return $self->{base}->to_string . '/'
143 156 . Net::IPAM::Block::Private::_common_prefix( $self->{base}->bytes, $self->{last}->bytes );
144 }
145  
146 26 return $self->{base}->to_string . '-' . $self->{last}->to_string;
147 }
148  
149 =head2 TO_JSON
150  
151 helper method for JSON serialization, just calls $block->to_string.
152 See also L<JSON/OBJECT SERIALISATION>.
153  
154 =cut
155  
156 sub TO_JSON {
157 0 ▶ $_[0]->to_string;
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
158 }
159  
160 =head2 is_cidr
161  
162 $b->is_cidr
163  
164 Returns true if the block is a CIDR.
165  
166 Net::IPAM::Block->new('fe80::aa/10')->is_cidr # true
167 Net::IPAM::Block->new('1.2.3.1-1.2.3.2')->is_cidr # false
168  
169 =cut
170  
171 sub is_cidr {
172 226 ▶ return Net::IPAM::Block::Private::_is_cidr( $_[0] );
Subroutine is_cidr: called CC=1 SCAR=0.0
Pod documented
173 }
174  
175 =head2 cidrsplit
176  
177 @cidrs = $b->cidrsplit
178  
179 Returns the next 2 cidrs splitted from block.
180  
181 Net::IPAM::Block->new('0.0.0.0/7')->cidrsplit # 0.0.0.0/8 1.0.0.0/8
182 Net::IPAM::Block->new('fe80::/12')->cidrsplit # fe80::/13 fe88::/13
183  
184 Returns undef if cidr mask is at maximum or if block is no CIDR.
185  
186 =cut
187  
188 sub cidrsplit {
189 15 ▶ my $self = shift;
Subroutine cidrsplit: called CC=5 SCAR=16.1
Pod documented
190  
191 # return if block is no CIDR)
192 15 ▶ return unless $self->is_cidr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_cidr 2 13 15
193  
194 13 my $bits = 32;
195 13 ▶ $bits = 128 if $self->version == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $self->version == 6 7 6 13
196  
197 # get the number of '1' in mask: 11111111_11111111_1110000_0000 => 19
198 13 my $n = Net::IPAM::Block::Private::_common_prefix( $self->{base}->bytes, $self->{last}->bytes );
199  
200 # can't split, n == maxbits (32 or 128)
201 13 ▶ return if $n == $bits;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $bits 4 9 13
202  
203 # make next mask, e.g. /19 -> /20
204 # 11111111_11111111_1110000_0000
205 # 11111111_11111111_1111000_0000
206 9 my $next_mask_n = Net::IPAM::Block::Private::_make_mask_n( $n + 1, $bits );
207  
208 # get original base_n from block
209 9 my $base_n = $self->{base}->bytes;
210  
211 # make new base and last with new mask
212 9 my $base1_n = Net::IPAM::Block::Private::_make_base_n( $base_n, $next_mask_n );
213 9 my $last1_n = Net::IPAM::Block::Private::_make_last_n( $base_n, $next_mask_n );
214  
215 # make next base by incrementing last
216 9 my $base2_n = Net::IPAM::Util::incr_n($last1_n);
217 9 my $last2_n = Net::IPAM::Block::Private::_make_last_n( $base2_n, $next_mask_n );
218  
219 # make new cidr blocks
220 9 my $cidr1 = bless( {}, ref $self );
221 9 my $cidr2 = bless( {}, ref $self );
222  
223 9 $cidr1->{base} = Net::IPAM::IP->new_from_bytes($base1_n);
224 9 $cidr1->{last} = Net::IPAM::IP->new_from_bytes($last1_n);
225  
226 9 $cidr2->{base} = Net::IPAM::IP->new_from_bytes($base2_n);
227 9 $cidr2->{last} = Net::IPAM::IP->new_from_bytes($last2_n);
228  
229 9 ▶ return wantarray ? ( $cidr1, $cidr2 ) : [ $cidr1, $cidr2 ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 1 9
230 }
231  
232 =head2 to_cidrs
233  
234 @cidrs = $b->to_cidrs
235  
236 Returns a list of Net::IPAM::Block objects as true CIDRs, representing the range.
237  
238 Net::IPAM::Block->new('17.0.0.1-17.0.0.2')->to_cidrs # 17.0.0.1/32 17.0.0.2/32
239 Net::IPAM::Block->new('fe80::aa-fe80::ac')->to_cidrs # fe80::aa/127 fe80::ac/128
240 Net::IPAM::Block->new('1.2.3.0-1.2.3.101')->to_cidrs # 1.2.3.0/26 1.2.3.64/27 1.2.3.96/30 1.2.3.100/31
241  
242 If the range is a CIDR, just returns the CIDR:
243  
244 Net::IPAM::Block->new('10.0.0.0/8')->to_cidrs # 10.0.0.0/8
245 Net::IPAM::Block->new('::1')->to_cidrs # ::1/128
246  
247 =cut
248  
249 sub to_cidrs {
250 10 ▶ my $cidrs = Net::IPAM::Block::Private::_to_cidrs_iter( $_[0] );
Subroutine to_cidrs: called CC=2 SCAR=6.9
Pod documented
251 10 ▶ return wantarray ? @$cidrs : $cidrs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 3 10
252 }
253  
254 =head2 base
255  
256 $ip = $b->base
257  
258 Returns the base IP, as Net::IPAM::IP object.
259  
260 $b = Net::IPAM::Block->new('fe80::ffff/10');
261 say $b->base; # fe80::/10
262  
263 =cut
264  
265 # just return the base slot
266 sub base {
267 2 ▶ return $_[0]->{base};
Subroutine base: called CC=1 SCAR=0.0
Pod documented
268 }
269  
270 =head2 last
271  
272 $ip = $b->last
273  
274 Returns the last IP, as Net::IPAM::IP object.
275  
276 $b = Net::IPAM::Block->new('10.0.0.0/30')
277 say $b->last; # 10.0.0.3
278  
279 =cut
280  
281 # just return the last slot
282 sub last {
283 2 ▶ return $_[0]->{last};
Subroutine last: called CC=1 SCAR=0.0
Pod documented
284 }
285  
286 =head2 mask
287  
288 $ip = $b->mask
289  
290 Returns the netmask as Net::IPAM::IP object.
291  
292 $b = Net::IPAM::Block->new('10.0.0.0/24')
293 say $b->mask if defined $b->mask; # 255.255.255.0
294  
295 The mask is only defined for real CIDR blocks.
296  
297 Example:
298  
299 1.2.3.4 => mask is /32 = 255.255.255.255
300 ::1 => mask is /128 = ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
301  
302 10.0.0.0-10.0.0.15 => mask is /28 = 255.255.255.240
303 ::-::f => mask is /124 = ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0
304  
305 10.0.0.0/24 => mask is /24 = 255.255.255.0
306 fe80::/10 => mask is /10 = ffc0:0000:0000:0000:0000:0000:0000:0000
307  
308 10.0.0.0-10.0.0.13 => mask is undef
309 ::-::5 => mask is undef
310  
311 =cut
312  
313 # calc the mask as IP, returns undef if not a CIDR
314 sub mask {
315 2 ▶ my $self = shift;
Subroutine mask: called CC=2 SCAR=6.9
Pod documented
316 2 ▶ return unless $self->is_cidr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_cidr 1 1 2
317  
318 1 my $mask_n = Net::IPAM::Block::Private::_get_mask_n( $self->{base}, $self->{last} );
319 1 return Net::IPAM::IP->new_from_bytes($mask_n);
320 }
321  
322 =head2 hostmask
323  
324 $ip = $b->hostmask
325  
326 Returns the hostmask as Net::IPAM::IP object.
327  
328 $b = Net::IPAM::Block->new('10.0.0.0/24')
329 say $b->mask; # 255.255.255.0
330 say $b->hostmask; # 0.0.0.255
331  
332 $b = Net::IPAM::Block->new('fe80::/10')
333 say $b->mask; # ffc0::
334 say $b->hostmask; # 3f:ffff:ffff:ffff:ffff:ffff:ffff:ffff
335  
336 The hostmask is only defined for real CIDR blocks.
337  
338 =cut
339  
340 sub hostmask {
341 2 ▶ my $self = shift;
Subroutine hostmask: called CC=2 SCAR=6.9
Pod documented
342 2 ▶ return unless $self->is_cidr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_cidr 1 1 2
343  
344 1 my $mask_n = Net::IPAM::Block::Private::_get_mask_n( $self->{base}, $self->{last} );
345 1 return Net::IPAM::IP->new_from_bytes( ~$mask_n );
346 }
347  
348 =head2 bitlen
349  
350 C<< bitlen >> returns the minimum number of bits to represent a range from base to last
351  
352 $n = $b->bitlen
353  
354 obvious for CIDR blocks:
355  
356 $b = Net::IPAM::Block->new('10.0.0.0/24')
357 say $b->bitlen; # 32 - 24 = 8 bit
358  
359 $b = Net::IPAM::Block->new('::/0');
360 say $b->bitlen; # 128 - 0 = 128 bit
361  
362 not so obvious for ranges:
363  
364 $b = Net::IPAM::Block->new('2001:db8::affe-2001:db8::cafe');
365 say $b->bitlen; # 15 bit (at least)
366  
367 =cut
368  
369 sub bitlen {
370 4 ▶ my $self = shift;
Subroutine bitlen: called CC=2 SCAR=6.9
Pod documented
371  
372 4 my $bits = 32;
373 4 ▶ $bits = 128 if $self->version == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $self->version == 6 2 2 4
374  
375 4 return $bits - Net::IPAM::Block::Private::_common_prefix( $self->{base}->bytes, $self->{last}->bytes );
376 }
377  
378 =head2 iter
379  
380 C<< iter >> returns the next IP in block, starting with base and stopping at last. Returns undef after last.
381  
382 $b = Net::IPAM::Block->new('2001:db8::affe-2001:db8::cafe');
383 while ( my $ip = $b->iter ) {
384 say $ip;
385 }
386  
387 OUTPUT:
388  
389 2001:db8::affe
390 2001:db8::afff
391 2001:db8::b000
392 2001:db8::b001
393 ...
394 2001:db8::cafb
395 2001:db8::cafc
396 2001:db8::cafd
397 2001:db8::cafe
398  
399 =cut
400  
401 sub iter {
402 0 ▶ my $self = shift;
Subroutine iter: not called CC=3 SCAR=24.8
Pod documented
403  
404 # init
405 0 ▶ unless ( defined $self->{iter} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"iter"}) 0 0 0
406  
407 # initialize state
408 0 return $self->{iter} = $self->{base};
409 }
410  
411 #next
412 0 ▶ if ( $self->{iter}->cmp( $self->{last} ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iter"}->cmp($self->{"last"}) < 0) 0 0 0
413 0 return $self->{iter} = $self->{iter}->incr;
414 }
415  
416 # over
417 0 return;
418 }
419  
420 =head2 cmp
421  
422 $a->cmp($b)
423  
424 Compares two IP blocks:
425  
426 print $this->cmp($other);
427 @sorted_blocks = sort { $a->cmp($b) } @unsorted_blocks;
428  
429 cmp() returns -1, 0, +1:
430  
431 0 if $a == $b,
432  
433 -1 if $a is v4 and $b is v6
434 +1 if $a is v6 and $b is v4
435  
436 -1 if $a->base < $b->base
437 +1 if $a->base > $b->base
438  
439 -1 if $a->base == $b->base and $a->last > $b->last # $a is super-set of $b
440 +1 if $a->base == $b->base and $a->last < $b->last # $a is sub-set of $b
441  
442 =cut
443  
444 sub cmp {
445 return ( $_[0]->{base}->cmp( $_[1]->{base} ) )
446 542 ▶ || ( $_[1]->{last}->cmp( $_[0]->{last} ) );
Subroutine cmp: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0]{'base'}->cmp($_[1]{'base'}) || $_[1]{'last'}->cmp($_[0]{'last'})
l!l&&r!l&&!r
43110110
MC/DC100%2/2
$_[0]{'base'}->cmp($_[1]{'base'}) || $_[1]{'last'}->cmp($_[0]{'last'})
$_[0]{'base'}->cmp($_[1]{'base'}) $_[1]{'last'}->cmp($_[0]{'last'})
Truth table100%3/3
A || B
A= $_[0]{'base'}->cmp($_[1]{'base'})B= $_[1]{'last'}->cmp($_[0]{'last'})
ABresult
1X1
011
000
447 }
448  
449 =head2 is_disjunct_with
450  
451 $a->is_disjunct_with($b)
452  
453 Returns true if the blocks are disjunct
454  
455 a |----------|
456 b |---|
457  
458 a |------|
459 b |---|
460  
461 print "a and b are disjunct" if $a->is_disjunct_with($b)
462  
463 =cut
464  
465 sub is_disjunct_with {
466  
467 # a |----------|
468 # b |---|
469 194 ▶ return 1 if $_[0]->{base}->cmp( $_[1]->{last} ) == 1;
Subroutine is_disjunct_with: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"base"}->cmp($_[1]{"last"}) == 1 6 188 194
470  
471 # a |---|
472 # b |----------|
473 188 ▶ return 1 if $_[0]->{last}->cmp( $_[1]->{base} ) == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"last"}->cmp($_[1]{"base"}) == -1 72 116 188
474  
475 116 return;
476 }
477  
478 =head2 overlaps_with
479  
480 $a->overlaps_with($b)
481  
482 Returns true if the blocks overlap.
483  
484 a |-------|
485 b |------|
486
487 a |------|
488 b |-------|
489
490 a |----|
491 b |---------|
492
493 a |---------|
494 b |----|
495  
496 =cut
497  
498 sub overlaps_with {
499  
500 # false if a == b
501 123 ▶ return if $_[0]->cmp( $_[1] ) == 0;
Subroutine overlaps_with: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->cmp($_[1]) == 0 4 119 123
502  
503 # false if a contains b or vice versa
504 119 ▶ return if $_[0]->contains( $_[1] ) || $_[1]->contains( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->contains($_[1]) or $_[1]->contains($_[0]) 56 63 119
Condition100%3/3
$_[0]->contains($_[1]) or $_[1]->contains($_[0])
l!l&&r!l&&!r
55163
MC/DC100%2/2
$_[0]->contains($_[1]) or $_[1]->contains($_[0])
$_[0]->contains($_[1]) $_[1]->contains($_[0])
Truth table100%3/3
A or B
A= $_[0]->contains($_[1])B= $_[1]->contains($_[0])
ABresult
1X1
011
000
505  
506 # false if a is_disjunct_with b
507 63 ▶ return if $_[0]->is_disjunct_with( $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_disjunct_with($_[1]) 58 5 63
508  
509 5 return 1;
510 }
511  
512 =head2 contains
513  
514 $a->contains($b)
515  
516 Returns true if block a contains block b. a and b may NOT coincide.
517  
518 if ( $a->contains($b) ) {
519 print "block a contains block b\n";
520 }
521  
522 a |-----------------| |-----------------| |-----------------|
523 b |------------| |------------| |------------|
524  
525 The argument may also be a Net::IPAM::IP address object.
526  
527 if ( $a->contains($ip) ) {
528 print "block a contains ip\n";
529 }
530  
531 =cut
532  
533 # polymorphic: arg may be block or ip
534 sub contains {
535 259 ▶ if ( ref $_[1] && $_[1]->isa('Net::IPAM::IP') ) {
Subroutine contains: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and $_[1]->isa("Net::IPAM::IP")) 8 251 259
Condition100%3/3
ref $_[1] and $_[1]->isa("Net::IPAM::IP")
!ll&&!rl&&r
12508
MC/DC100%2/2
ref $_[1] and $_[1]->isa("Net::IPAM::IP")
ref $_[1] $_[1]->isa("Net::IPAM::IP")
Truth table100%3/3
A and B
A= ref $_[1]B= $_[1]->isa("Net::IPAM::IP")
ABresult
0X0
100
111
536 8 return Net::IPAM::Block::Private::_contains_ip(@_);
537 }
538  
539 251 ▶ if ( ref $_[1] && $_[1]->isa('Net::IPAM::Block') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and $_[1]->isa("Net::IPAM::Block")) 249 2 251
Condition100%3/3
ref $_[1] and $_[1]->isa("Net::IPAM::Block")
!ll&&!rl&&r
11249
MC/DC100%2/2
ref $_[1] and $_[1]->isa("Net::IPAM::Block")
ref $_[1] $_[1]->isa("Net::IPAM::Block")
Truth table100%3/3
A and B
A= ref $_[1]B= $_[1]->isa("Net::IPAM::Block")
ABresult
0X0
100
111
540 249 return Net::IPAM::Block::Private::_contains_block(@_);
541 }
542  
543 2 Carp::croak 'wrong argument,';
544 }
545  
546 =head2 diff
547  
548 @diff = $outer->diff(@inner)
549  
550 Returns all blocks in outer block, minus the inner blocks.
551  
552 my $outer = Net::IPAM::Block->new("192.168.2.0/24");
553 my @inner = (
554 Net::IPAM::Block->new("192.168.2.0/26"),
555 Net::IPAM::Block->new("192.168.2.240-192.168.2.249"),
556 );
557  
558 my @diff = $outer->diff(@inner);
559  
560 # diff: [192.168.2.64-192.168.2.239, 192.168.2.250-192.168.2.255]
561  
562 =cut
563  
564 sub diff {
565 17 ▶ my $self = shift;
Subroutine diff: called CC=16 SCAR=28.3
Pod documented
566 17 my $outer = Net::IPAM::Block::Private::_clone($self);
567  
568 # no inner blocks, just return outer block
569 17 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 16 17
570 1 ▶ return wantarray ? ($outer) : [$outer];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
571 }
572  
573 16 my @result;
574  
575 # blocks must be sorted for this algo
576 16 LOOP: foreach my $b ( sort_block(@_) ) {
577  
578 SWITCH: {
579 # no-op
580 64 ▶ next LOOP if $outer->is_disjunct_with($b);
Branch: 1 branch
BranchTrueFalseTotal
if $outer->is_disjunct_with($b) 6 58 64
581  
582 # masks rest
583 58 ▶ if ( $outer->cmp($b) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outer->cmp($b) == 0) 4 54 58
584 4 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
585 }
586  
587 # masks rest
588 54 ▶ if ( $b->contains($outer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->contains($outer)) 2 52 54
589 2 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
590 }
591  
592 # move cursor forward
593 52 ▶ if ( $outer->{base}->cmp( $b->{base} ) >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outer->{"base"}->cmp($b->{"base"}) >= 0) 36 16 52
594 36 $outer->{base} = $b->{last}->incr;
595 36 last SWITCH;
596 }
597  
598 # save diff, move cursor forward
599 16 ▶ if ( $outer->{base}->cmp( $b->{base} ) < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outer->{"base"}->cmp($b->{"base"}) < 0) 16 0 16
600  
601 16 my $block = bless {}, ref $outer;
602 16 $block->{base} = $outer->{base};
603 16 $block->{last} = $b->{base}->decr;
604  
605 16 push @result, $block;
606  
607 16 $outer->{base} = $b->{last}->incr;
608 16 last SWITCH;
609 }
610  
611 0 die "logic error: rest=$outer, topic: $b,";
612  
613 } # end of SWITCH
614  
615 # overflow from last incr
616 52 ▶ if ( not defined $outer->{base} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outer->{"base"}) 0 52 52
617 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
618 }
619  
620 # cursor moved behind last
621 52 ▶ if ( $outer->{base}->cmp( $outer->{last} ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outer->{"base"}->cmp($outer->{"last"}) > 0) 2 50 52
622 2 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
623 }
624  
625 } # end of LOOP
626  
627 # save the rest
628 8 push @result, $outer;
629  
630 8 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 3 8
631 }
632  
633 =head1 FUNCTIONS
634  
635 =head2 sort_block
636  
637 use Net::IPAM::Block 'sort_block';
638  
639 @sorted_blocks = sort_block @unsorted_blocks;
640  
641 Faster sort implemention (Schwartzian transform) as explcit sort function:
642  
643 @sorted_blocks = sort { $a->cmp($b) } @unsorted_blocks;
644  
645 =cut
646  
647 # see also cmp()
648  
649 sub sort_block {
650 212 return map { $_->[0] }
651 572 ▶ sort { $a->[1]->cmp( $b->[1] ) || $b->[2]->cmp( $a->[2] ) }
Branch: 1 branch
BranchTrueFalseTotal
$Net::IPAM::Block::a->[1]->cmp($Net::IPAM::Block::b->[1]) or $Net::IPAM::Block::b->[2]->cmp($Net::IPAM::Block::a->[2]) 63 509 572
652 29 ▶ map { [ $_, $_->{base}, $_->{last} ] } @_;
Subroutine sort_block: called CC=2 SCAR=6.9
Pod documented
653 }
654  
655 =head2 merge
656  
657  
658 use Net::IPAM::Block 'merge';
659  
660 @merged = merge(@blocks)
661  
662 Returns the minimal number of blocks spanning the range of input blocks.
663  
664 If CIDRs are required, use the following idiom:
665  
666 @cidrs = map { $_->to_cidrs } merge(@blocks);
667  
668 =cut
669  
670 sub merge {
671 13 ▶ return @_ if @_ <= 1;
Subroutine merge: called CC=9 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ <= 1 1 12 13
672  
673 # sort blocks
674 12 my @sorted = sort_block(@_);
675  
676 # start with first [0] block as prev, see below
677 12 my @result = ( shift @sorted );
678  
679 # [1, ..
680 12 for (@sorted) {
681  
682 # skip rubbish
683 105 ▶ next unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 105 105
684  
685 105 my $prev = $result[-1];
686  
687 # expand prev
688 105 ▶ if ( $prev->overlaps_with($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->overlaps_with($_)) 3 102 105
689 3 $prev->{last} = $_->{last};
690 3 next;
691 }
692  
693 # expand prev
694 102 my $next = $prev->{last}->incr;
695 102 ▶ if ( defined $next && $next->cmp( $_->{base} ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next and $next->cmp($_->{"base"}) == 0) 44 58 102
Condition100%3/3
defined $next and $next->cmp($_->{"base"}) == 0
!ll&&!rl&&r
174144
MC/DC100%2/2
defined $next and $next->cmp($_->{"base"}) == 0
defined $next $next->cmp($_->{"base"}) == 0
Truth table100%3/3
A and B
A= defined $nextB= $next->cmp($_->{"base"}) == 0
ABresult
0X0
100
111
696 44 $prev->{last} = $_->{last};
697 44 next;
698 }
699  
700 # append block
701 58 ▶ if ( $prev->is_disjunct_with($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->is_disjunct_with($_)) 9 49 58
702 9 push @result, $_;
703 9 next;
704 }
705  
706 # skip contains and equals
707 }
708  
709 12 ▶ return wantarray ? @result : [@result];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 3 12
710 }
711  
712 =head2 aggregate
713  
714 *** DEPRECATED *** use merge in favor of
715  
716 =cut
717  
718 sub aggregate {
719 2 ▶ Carp::carp("DEPRECATED: use merge() in favor of aggregate(),");
Subroutine aggregate: called CC=1 SCAR=0.0
Pod documented
720 1 return merge(@_);
721 }
722  
723 =head1 OPERATORS
724  
725 L<Net::IPAM::Block> overloads the following operators.
726  
727 =head2 bool
728  
729 my $bool = !!$block;
730  
731 Always true.
732  
733 =head2 stringify
734  
735 my $str = "$block";
736  
737 Alias for L</"to_string">.
738  
739 =cut
740  
741 use overload
742 63 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
743 1 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
744 14 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
745  
746 =head1 AUTHOR
747  
748 Karl Gaissmaier, C<< <karl.gaissmaier(at)uni-ulm.de> >>
749  
750 =head1 BUGS
751  
752 Please report any bugs or feature requests to C<bug-net-ipam-block at rt.cpan.org>, or through
753 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-IPAM-Block>. I will be notified, and then you'll
754 automatically be notified of progress on your bug as I make changes.
755  
756 =head1 SUPPORT
757  
758 You can find documentation for this module with the perldoc command.
759  
760 perldoc Net::IPAM::Block
761  
762  
763 You can also look for information at:
764  
765 =over 4
766  
767 =item * on github
768  
769 TODO
770  
771 =back
772  
773 =head1 SEE ALSO
774  
775 L<Net::IPAM::IP>
776 L<Net::IPAM::Tree>
777  
778 =head1 LICENSE AND COPYRIGHT
779  
780 This software is copyright (c) 2020-2021 by Karl Gaissmaier.
781  
782 This is free software; you can redistribute it and/or modify it under
783 the same terms as the Perl 5 programming language system itself.
784  
785 =cut
786  
787 14 1; # End of Net::IPAM::Block
788  
789 # vim: ts=2 sw=2 sts=2 background=dark
↑ Summary Build »