Identifier/Net.pm

filter: Statementstmt 92.4% 98 / 106 Branchbran 73.8% 31 / 42 Conditioncond 56.5% 13 / 23 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 94.1% 16 / 17 Podpod 100% 8 / 8 totaltotal 81.9% 172 / 210 CC 30 SCAR 18.4
CC
30
Cov
83%
new
14.8
_split_range
9.0
int_to_ip
8.3
CRAP 6.3
« Identifier/Binode.pm ↑ Summary Identifier/Regex.pm »
1 #===============================================================================
2 # PODNAME: Net::IP::Identifier::Net
3 # ABSTRACT: subclass Net::IP to add some functionality
4 #
5 # AUTHOR: Reid Augustin (REID)
6 # EMAIL: reid@hellosix.com
7 # CREATED: Sun Jul 20 17:48:21 PDT 2014
8 #===============================================================================
9  
10 3 ▶use 5.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package Net::IP::Identifier::Net;
15 3 ▶use parent 'Net::IP';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 our $VERSION = '0.111'; # VERSION
21  
22 3 ▶use overload '""' => \&print;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Accept any of:
25 # Net::IP::Identifier::Net object or class
26 # Net::IP object
27 1704 ▶sub new {
Subroutine new: called CC=6 SCAR=26.9
Pod documented
28 1704 my ($class, $net) = @_;
29  
30 1704 ▶ croak "Must have exactly one argument to 'new'\n" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 1704 1704
31 1704 ▶ if (ref $net) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $net) 0 1704 1704
32 0 ▶ return $net if (ref $net eq __PACKAGE__); # already correct
Branch: 1 branch
BranchTrueFalseTotal
if ref $net eq __PACKAGE__ 0 0 0
33  
34 # it's an object, but wrong kind
35 0 my $src_str;
36 0 ▶ $src_str = $net->src_str if ($net->can('src_str'));
Branch: 1 branch
BranchTrueFalseTotal
if $net->can("src_str") 0 0 0
37 0 bless $net, $class; # rebless to this package
38 # make sure we have a source string
39 0 ▶ $net->src_str($src_str || $net->print);
Condition0%0/3
$src_str || $net->print
l!l&&r!l&&!r
000
MC/DC0%0/2
$src_str || $net->print
$src_str $net->print
Truth table0%0/3
A || B
A= $src_strB= $net->print
ABresult
1X1
011
000
40 0 return $net;
41 }
42 1704 my $self = $class->SUPER::new($net);
43 1704 bless $self, $class; # rebless to this package
44 1704 $self->src_str($net); # set the source string
45  
46 1704 return $self;
47 }
48  
49 1704 ▶sub src_str {
Subroutine src_str: called CC=2 SCAR=7.0
Pod documented
50 1704 my ($self, $new) = @_;
51  
52 1704 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1704 0 1704
53 1704 $self->{src_str} = $new;
54 }
55 1704 return $self->{src_str};
56 }
57  
58 # print an int as a dotted decimal quad
59 752 ▶sub int_to_ip {
Subroutine int_to_ip: called CC=8 SCAR=21.1
Pod documented
60 752 my ($self, $int, $version) = @_;
61  
62 752 ▶ $version ||= $self->version || 4;
Condition40%2/5
$version ||= $self->version || 4
l!l&&r!l&&!r
07520
$self->version || 4
l!l
7520
MC/DC0%0/2
$version ||= $self->version || 4
$version $self->version
Truth table33%1/3
A ||= B
A= $versionB= $self->version
ABresult
1X1
011
000
63 752 my @parts;
64 752 ▶ if ($version eq '6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 6) 58 694 752
65 58 ▶ while ($int or @parts < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int or @parts < 8) 464 58 522
Condition66%2/3
$int or @parts < 8
l!l&&r!l&&!r
464058
MC/DC50%1/2
$int or @parts < 8
$int @parts < 8
Truth table66%2/3
A or B
A= $intB= @parts < 8
ABresult
1X1
011
000
66 464 unshift @parts, $int & 0xffff;
67 464 $int >>= 16;
68 }
69 58 my $addr = join(':', map { sprintf "%x", $_ } @parts);
70 58 return Net::IP::ip_compress_address($addr, 6);
71 }
72  
73 # else IPv4
74 694 ▶ while ($int or @parts < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int or @parts < 4) 2776 694 3470
Condition66%2/3
$int or @parts < 4
l!l&&r!l&&!r
27760694
MC/DC50%1/2
$int or @parts < 4
$int @parts < 4
Truth table66%2/3
A or B
A= $intB= @parts < 4
ABresult
1X1
011
000
75 2776 unshift @parts, $int & 0xff;
76 2776 $int >>= 8;
77 }
78 694 return join('.', @parts);
79 }
80  
81 # split a range at the binary changeover point
82 615 ▶sub _split_range {
Subroutine _split_range: called CC=9 SCAR=22.0
83 615 my ($self, $idx, $low, $high) = @_;
84  
85 3 ▶ no warnings 'recursion'; # IPv6 requires at least 128 levels
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 615 if (0) {
88 printf "idx $idx, low %s, high %s\n",
89 $self->int_to_ip($low),
90 $self->int_to_ip($high);
91 }
92  
93 615 ▶ if ($high <= $low) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high <= $low) 4 611 615
94 4 return Net::IP::Identifier::Net->new($self->int_to_ip($low));
95 }
96 611 my $mask = Math::BigInt->new(2);
97 611 $mask->bpow($idx);
98 611 $mask--;
99 611 ▶ while ($idx and ($high ^ $low) <= $mask) { # find first mask where different bit is outside it
Branch: 1 branch
BranchTrueFalseTotal
if ($idx and ($high ^ $low) <= $mask) 881 611 1492
Condition100%3/3
$idx and ($high ^ $low) <= $mask
!ll&&!rl&&r
2609881
MC/DC100%2/2
$idx and ($high ^ $low) <= $mask
$idx ($high ^ $low) <= $mask
Truth table100%3/3
A and B
A= $idxB= ($high ^ $low) <= $mask
ABresult
0X0
100
111
100 881 $mask->brsft(1);
101 881 $idx--;
102 }
103 611 $mask->blsft(1); # undo one shift
104 611 $mask += 1;
105 611 $idx++;
106 611 ▶ if (($mask & $low) == 0 and
Branch: 1 branch
BranchTrueFalseTotal
if (($mask & $low) == 0 and ($mask & $high) == $mask) 374 237 611
Condition100%3/3
($mask & $low) == 0 and ($mask & $high) == $mask
!ll&&!rl&&r
14790374
MC/DC100%2/2
($mask & $low) == 0 and ($mask & $high) == $mask
($mask & $low) == 0 ($mask & $high) == $mask
Truth table100%3/3
A and B
A= ($mask & $low) == 0B= ($mask & $high) == $mask
ABresult
0X0
100
111
107 ($mask & $high) == $mask) {
108 374 $low = $self->int_to_ip($low);
109 374 $high = $self->int_to_ip($high);
110 374 return Net::IP::Identifier::Net->new("$low - $high");
111 }
112  
113 237 ▶ croak sprintf "ran out of indexes: 0x%x-0x%x\n", $low, $high if($idx <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $idx <= 0 0 237 237
114  
115 # need to split
116 237 my $new_split;
117 237 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$new_split > $high and $idx >= 0 and do { do { $new_split = $low & ~$mask; $mask >>= 1; $new_split += $mask + 1; --$idx } } 0 237 237
Condition33%1/3
$new_split > $high and $idx >= 0
!ll&&!rl&&r
23700
MC/DC0%0/2
$new_split > $high and $idx >= 0
$new_split > $high $idx >= 0
Truth table33%1/3
A and B
A= $new_split > $highB= $idx >= 0
ABresult
0X0
100
111
118 237 $new_split = ($low & ~$mask);
119 237 $mask >>= 1;
120 237 $new_split += $mask + 1;
121 237 $idx--;
122 } while ($new_split > $high and $idx >= 0);
123  
124 237 my @low = $self->_split_range($idx, $low, $new_split - 1);
125 237 my @high = $self->_split_range($idx, $new_split, $high);
126  
127 237 return(@low, @high);
128 }
129  
130 # method to convert range into cidrs
131 141 ▶sub range_to_cidrs {
Subroutine range_to_cidrs: called CC=2 SCAR=7.1
Pod documented
132 141 my ($self) = @_;
133  
134 141 ▶ return $self if ($self->prefixlen); # don't need to split
Branch: 1 branch
BranchTrueFalseTotal
if $self->prefixlen 0 141 141
135  
136 141 return $self->_split_range(
137 $self->masklen - 1,
138 $self->intip,
139 $self->last_int);
140 }
141  
142 3 my $zero_v4 = '0' x 32;
143 3 my $zero_v6 = '0' x 128;
144 # return the length of inverse of the prefixlen, the length of the 1s in the mask
145 # always returns a value, unlike prefixlen. If not on an even binary
146 # boundary, the masklen represents a big enough CIDR that the range fits
147 # in it
148 1704 ▶sub masklen {
Subroutine masklen: called CC=4 SCAR=13.9
Pod documented
149 1704 my ($self) = @_;
150  
151 1704 ▶ my $len = $self->version == 6 ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 163 1541 1704
152 # xor with zero mask also to ensure proper upper bits
153 1704 ▶ my $differ = $self->binip ^ $self->last_bin ^ ($self->version == 6 ? $zero_v6 : $zero_v4);
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 163 1541 1704
154 1704 my $idx = index($differ, '1');
155 1704 ▶ return 0 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 49 1655 1704
156 1655 return $len - $idx;
157 }
158  
159 # return the masked portion of the IP (upper part)
160 1563 ▶sub masked_ip {
Subroutine masked_ip: called CC=2 SCAR=6.9
Pod documented
161 1563 my ($self) = @_;
162  
163 1563 ▶ my $len = $self->version == 6 ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 154 1409 1563
164 1563 return substr($self->binip, 0, $len - $self->masklen);
165 }
166  
167 # override print: bare IP (without prefixlen) for single IPs or network
168 # with prefixlength if CIDR, or as range if not single and not CIDR
169 1300 ▶sub print {
Subroutine print: called CC=4 SCAR=13.9
Pod documented
170 1300 my ($self) = @_;
171  
172 1300 ▶ return $self->ip if ($self->ip eq $self->last_ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ip eq $self->last_ip 0 1300 1300
173 1300 ▶ if (defined $self->prefixlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->prefixlen) 1159 141 1300
174 1159 ▶ if ($self->version eq 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version eq 6) 97 1062 1159
175 97 return $self->SUPER::print;
176 }
177 1062 return $self->ip . '/' . $self->prefixlen;
178 }
179 141 return $self->ip . '-' . $self->last_ip;
180 }
181  
182 # return ->ip, but compress if IPv6
183 0 ▶sub compressed_ip {
Subroutine compressed_ip: not called CC=1 SCAR=6.9
Pod documented
184 0 my ($self) = @_;
185  
186 0 return Net::IP::ip_compress_address($self->ip, $self->version);
187 }
188  
189 3 1;
190  
191 __END__
« Identifier/Binode.pm ↑ Summary Identifier/Regex.pm »