Farly/IPv4/Range.pm

filter: Statementstmt 92.4% 61 / 66 Branchbran 77.7% 14 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 100% 9 / 9 totaltotal 90.8% 99 / 109 CC 10 SCAR 9.8
CC
10
Cov
89%
as_network
6.0
new
3.0
adjacent
2.1
CRAP 2.7
« Farly/Remove/Rule.pm ↑ Summary Farly/ASA/Generator.pm »
1 package Farly::IPv4::Range;
2
3 19 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Farly::IPv4::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 require Farly::IPv4::Network;
9 19 ▶use Farly::IPv4::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 19 our @ISA = qw(Farly::IPv4::Object);
12 19 our $VERSION = '0.26';
13
14 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
15 13 my ( $class, $first, $last ) = @_;
16
17 13 my $self = {
18 FIRST => undef, #::IPv4::Address
19 LAST => undef, #::IPv4::Address
20 };
21 13 bless( $self, $class );
22
23 13 ▶ if ( defined $last ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $last) { } 1 12 13
elsif (defined $first) { } 12 0 12
24 1 $self->_init( $first, $last );
25 }
26 elsif ( defined $first ) {
27 12 my ( $f, $l ) = split( /-|\s+/, $first );
28 12 $self->_init( $f, $l );
29 }
30 else {
31 0 confess "first last address range required";
32 }
33
34 13 return $self;
35 }
36
37 13 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
38 13 my ( $self, $first, $last ) = @_;
39
40 13 $self->{FIRST} = Farly::IPv4::Address->new($first);
41 13 $self->{LAST} = Farly::IPv4::Address->new($last);
42
43 13 ▶ confess "First must be less than last"
Branch: 1 branch
BranchTrueFalseTotal
if $self->first > $self->last 0 13 13
44 if ( $self->first() > $self->last() );
45 }
46
47 53 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
48 53 return $_[0]->{FIRST}->address();
49 }
50
51 39 ▶sub last {
Subroutine last: called CC=1 SCAR=0.0
Pod documented
52 39 return $_[0]->{LAST}->address();
53 }
54
55 8 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
56 8 my ($self) = @_;
57 8 return join( " ", $self->{FIRST}->as_string(), $self->{LAST}->as_string() );
58 }
59
60 1 ▶sub adjacent {
Subroutine adjacent: called CC=2 SCAR=7.6
Pod documented
61 1 my ( $self, $other ) = @_;
62 1 ▶ if ( ( $self->last() + 1 ) == $other->first() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last + 1 == $other->first) 1 0 1
63 1 return 1;
64 }
65 0 return 0;
66 }
67
68 1 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
69 1 return $_[0]->{FIRST};
70 }
71
72 1 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
73 1 return $_[0]->{LAST};
74 }
75
76 1 ▶sub as_network {
Subroutine as_network: called CC=6 SCAR=17.9
Pod documented
77 1 my ($self) = @_;
78 1 my $first = $self->first();
79 1 my $last = $self->last();
80 1 my @list;
81 my $mask;
82 1 my $host_mask = 4294967295; #0XFFFFFFFF
83
84 #start from the first address in the range
85 1 ▶ while ( $first < $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first < $last) 4 1 5
86
87 # start from the /32 mask and work downwards
88 4 my $current_mask = $host_mask;
89
90 # check if the current mask results in first being a network number
91 4 ▶ while ( $first == ( $first & $current_mask ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == ($first & $current_mask)) 10 4 14
92
93 # if the current mask is still in the range try a bigger mask
94 10 my $inverse_mask = ~$current_mask & $host_mask;
95 10 ▶ if ( ( $first + $inverse_mask ) <= $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first + $inverse_mask <= $last) { } 10 0 10
96 10 $mask = $current_mask;
97 10 $current_mask = ( $current_mask << 1 ) & $host_mask;
98 }
99 else {
100 0 last;
101 }
102 }
103
104 4 ▶ if ( $mask == 4294967295 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask == 4294967295) { } 1 3 4
105 1 push @list, Farly::IPv4::Address->new($first);
106 }
107 else {
108 3 push @list, Farly::IPv4::Network->new("$first $mask");
109 }
110
111 4 $first = $first + ( ~$mask & $host_mask ) + 1;
112
113 4 ▶ if ( $first == $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == $last) 1 3 4
114 1 push @list, Farly::IPv4::Address->new($first);
115 }
116
117 }
118
119 1 return @list;
120 }
121
122 0 ▶sub iter {
Subroutine iter: not called CC=1 SCAR=6.9
Pod documented
123 0 my @iter = ( Farly::IPv4::Range->new( $_[0]->first(), $_[0]->last() ) );
124 0 return @iter;
125 }
126
127 19 1;
128 __END__
« Farly/Remove/Rule.pm ↑ Summary Farly/ASA/Generator.pm »