Identifier/Net.pm

filter: Statementstmt 41.6% 45 / 108 Branchbran 19.0% 8 / 42 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 61.1% 11 / 18 Podpod 30.0% 3 / 10 totaltotal 31.1% 67 / 215 CC 30 SCAR 34.3
CC
30
Cov
31%
split_range
76.0
int_to_ip
72.0
new
14.8
CRAP 30.9
« Identifier/WhoisParser.pm ↑ Summary Identifier_Role.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.106'; # VERSION
21  
22 use overload (
23 3 '""' => 'print',
24 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Accept any of:
27 # Net::IP::Identifier::Net object or class
28 # Net::IP object
29 sub new {
30 6 ▶ my ($class, $net) = @_;
Subroutine new: called CC=6 SCAR=26.9
Pod undocumented
31  
32 6 ▶ croak "Must have exactly one argument to 'new'\n" if (@_ != 2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 6 6
33 6 ▶ if (ref $net) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $net) 0 6 6
34 0 ▶ return $net if (ref $net eq __PACKAGE__); # already correct
Branch: 1 branch
BranchTrueFalseTotal
if ref $net eq __PACKAGE__ 0 0 0
35  
36 # it's an object, but wrong kind
37 0 my $src_str;
38 0 ▶ $src_str = $net->src_str if ($net->can('src_str'));
Branch: 1 branch
BranchTrueFalseTotal
if $net->can("src_str") 0 0 0
39 0 bless $net, $class; # rebless to this package
40 # make sure we have a source string
41 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
42 0 return $net;
43 }
44 6 my $self = $class->SUPER::new($net);
45 6 bless $self, $class; # rebless to this package
46 6 $self->src_str($net); # set the source string
47  
48 6 return $self;
49 }
50  
51 sub src_str {
52 6 ▶ my ($self, $new) = @_;
Subroutine src_str: called CC=2 SCAR=7.0
Pod undocumented
53  
54 6 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 6 0 6
55 6 $self->{src_str} = $new;
56 }
57 6 return $self->{src_str};
58 }
59  
60 # print an int as a dotted decimal quad
61 sub int_to_ip {
62 0 ▶ my ($self, $ip, $version) = @_;
Subroutine int_to_ip: not called CC=8 SCAR=42.8
Pod undocumented
63  
64 0 ▶ $version ||= $self->version || 4;
Condition0%0/5
$version ||= $self->version || 4
l!l&&r!l&&!r
000
$self->version || 4
l!l
00
MC/DC0%0/2
$version ||= $self->version || 4
$version $self->version
Truth table0%0/3
A ||= B
A= $versionB= $self->version
ABresult
1X1
011
000
65 0 my @parts;
66 0 ▶ if ($version eq '6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 6) 0 0 0
67 0 ▶ while ($ip or @parts < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip or @parts < 8) 0 0 0
Condition0%0/3
$ip or @parts < 8
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip or @parts < 8
$ip @parts < 8
Truth table0%0/3
A or B
A= $ipB= @parts < 8
ABresult
1X1
011
000
68 0 unshift @parts, $ip & 0xffff;
69 0 $ip >>= 16;
70 }
71 0 my $addr = join(':', map { sprintf "%x", $_ } @parts);
72 0 return Net::IP::ip_compress_address($addr, 6);
73 }
74  
75 # else IPv4
76 0 ▶ while ($ip or @parts < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip or @parts < 4) 0 0 0
Condition0%0/3
$ip or @parts < 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$ip or @parts < 4
$ip @parts < 4
Truth table0%0/3
A or B
A= $ipB= @parts < 4
ABresult
1X1
011
000
77 0 unshift @parts, $ip & 0xff;
78 0 $ip >>= 8;
79 }
80 0 return join('.', @parts);
81 }
82  
83 # split a range at the binary changeover point
84 sub split_range {
85 0 ▶ my ($self, $idx, $low, $high) = @_;
Subroutine split_range: not called CC=9 SCAR=43.3
Pod undocumented
86  
87 3 ▶ no warnings 'recursion'; # IPv6 requires at least 128 levels
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 0 if (0) {
90 printf "idx $idx, low %s, high %s\n",
91 $self->int_to_ip($low),
92 $self->int_to_ip($high);
93 }
94  
95 0 ▶ if ($high <= $low) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high <= $low) 0 0 0
96 0 return Net::IP::Identifier::Net->new($self->int_to_ip($low));
97 }
98 0 my $mask = Math::BigInt->new(2);
99 0 $mask->bpow($idx);
100 0 $mask--;
101 0 ▶ 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) 0 0 0
Condition0%0/3
$idx and ($high ^ $low) <= $mask
!ll&&!rl&&r
000
MC/DC0%0/2
$idx and ($high ^ $low) <= $mask
$idx ($high ^ $low) <= $mask
Truth table0%0/3
A and B
A= $idxB= ($high ^ $low) <= $mask
ABresult
0X0
100
111
102 0 $mask->brsft(1);
103 0 $idx--;
104 }
105 0 $mask->blsft(1); # undo one shift
106 0 $mask += 1;
107 0 $idx++;
108 0 ▶ if (($mask & $low) == 0 and
Branch: 1 branch
BranchTrueFalseTotal
if (($mask & $low) == 0 and ($mask & $high) == $mask) 0 0 0
Condition0%0/3
($mask & $low) == 0 and ($mask & $high) == $mask
!ll&&!rl&&r
000
MC/DC0%0/2
($mask & $low) == 0 and ($mask & $high) == $mask
($mask & $low) == 0 ($mask & $high) == $mask
Truth table0%0/3
A and B
A= ($mask & $low) == 0B= ($mask & $high) == $mask
ABresult
0X0
100
111
109 ($mask & $high) == $mask) {
110 0 $low = $self->int_to_ip($low);
111 0 $high = $self->int_to_ip($high);
112 0 return Net::IP::Identifier::Net->new("$low - $high");
113 }
114  
115 0 ▶ croak sprintf "ran out of indexes: 0x%x-0x%x\n", $low, $high if($idx <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $idx <= 0 0 0 0
116  
117 # need to split
118 0 my $new_split;
119 0 ▶ 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 0 0
Condition0%0/3
$new_split > $high and $idx >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$new_split > $high and $idx >= 0
$new_split > $high $idx >= 0
Truth table0%0/3
A and B
A= $new_split > $highB= $idx >= 0
ABresult
0X0
100
111
120 0 $new_split = ($low & ~$mask);
121 0 $mask >>= 1;
122 0 $new_split += $mask + 1;
123 0 $idx--;
124 } while ($new_split > $high and $idx >= 0);
125  
126 0 my @low = $self->split_range($idx, $low, $new_split - 1);
127 0 my @high = $self->split_range($idx, $new_split, $high);
128  
129 0 return(@low, @high);
130 }
131  
132 # method to convert range into cidrs
133 sub range_to_cidrs {
134 0 ▶ my ($self) = @_;
Subroutine range_to_cidrs: not called CC=2 SCAR=17.9
Pod documented
135  
136 0 ▶ return $self if ($self->prefixlen); # don't need to split
Branch: 1 branch
BranchTrueFalseTotal
if $self->prefixlen 0 0 0
137  
138 0 return $self->split_range(
139 $self->masklen - 1,
140 $self->intip,
141 $self->last_int);
142 }
143  
144 3 my $zero_v4 = '0' x 32;
145 3 my $zero_v6 = '0' x 128;
146 # return the XOR of the first and last IP addresses
147 sub differ {
148 0 ▶ my ($self) = @_;
Subroutine differ: not called CC=2 SCAR=17.9
Pod undocumented
149  
150 # xor with zero mask also to ensure proper upper bits
151 0 ▶ return $self->binip ^ $self->last_bin ^ ($self->version == 6 ? $zero_v6 : $zero_v4);
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 0 0 0
152 }
153  
154 # return the inverse of the prefixlen, the length of the 1s in the mask
155 # always returns a value, unlike prefixlen. If not on an even binary
156 # boundary, the masklen represents a big enough CIDR that the range fits
157 # in it
158 sub masklen {
159 0 ▶ my ($self) = @_;
Subroutine masklen: not called CC=3 SCAR=24.8
Pod undocumented
160  
161 0 ▶ my $len = $self->version == 6 ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 0 0 0
162 0 my $differ = $self->differ;
163 0 my $idx = index($differ, '1');
164 0 ▶ return 0 if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 0 0 0
165 0 return $len - $idx;
166 }
167  
168 # return the masked portion of the IP (upper part)
169 sub masked_ip {
170 0 ▶ my ($self) = @_;
Subroutine masked_ip: not called CC=2 SCAR=17.9
Pod documented
171  
172 0 ▶ my $len = $self->version == 6 ? 128 : 32;
Branch: 1 branch
BranchTrueFalseTotal
$self->version == 6 ? : 0 0 0
173 0 return substr($self->binip, 0, $len - $self->masklen);
174 }
175  
176 # override print: bare IP (without prefixlen) for single IPs or network
177 # with prefixlength if CIDR, or as range if not single and not CIDR
178 sub print {
179 6 ▶ my ($self) = @_;
Subroutine print: called CC=4 SCAR=13.9
Pod documented
180  
181 6 ▶ return $self->ip if ($self->ip eq $self->last_ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ip eq $self->last_ip 0 6 6
182 6 ▶ if (defined $self->prefixlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->prefixlen) 4 2 6
183 4 ▶ if ($self->version eq 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version eq 6) 1 3 4
184 1 return $self->SUPER::print;
185 }
186 3 return $self->ip . '/' . $self->prefixlen;
187 }
188 2 return $self->ip . '-' . $self->last_ip;
189 }
190  
191 # return ->ip, but compress if IPv6
192 sub compressed_ip {
193 0 ▶ my ($self) = @_;
Subroutine compressed_ip: not called CC=1 SCAR=6.9
Pod undocumented
194  
195 0 return Net::IP::ip_compress_address($self->ip, $self->version);
196 }
197  
198 3 1;
199  
200 __END__
« Identifier/WhoisParser.pm ↑ Summary Identifier_Role.pm »