Identifier.pm

filter: Statementstmt 78.0% 146 / 187 Branchbran 57.3% 47 / 82 Conditioncond 33.3% 11 / 33 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 89.6% 26 / 29 Podpod 57.1% 4 / 7 totaltotal 65.8% 235 / 357 SCAR 52.1
CC
60
Cov
68%
run
210.0
parse_fh
20.0
load_entities
14.9
CRAP 183.0
« Identifier/WhoisParser.pm ↑ Summary Identifier/Binode.pm »
1 #!/usr/bin/perl
2 #===============================================================================
3 # PODNAME: Net::IP::Identifier
4 # ABSTRACT: Identify IPs that fall within collections of network blocks
5 #
6 # AUTHOR: Reid Augustin (REID)
7 # EMAIL: reid@hellosix.com
8 # CREATED: Mon Oct 6 10:20:33 PDT 2014
9 #===============================================================================
10  
11 3 ▶use 5.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 {
16 3 package Local::Payload;
17 3 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has entity => (
20 is => 'rw',
21 1507 ▶ isa => sub { die "Not a Net::IP::Identifier::Plugin\n"
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0]->does("Net::IP::Identifier_Role") 0 1507 1507
22 if (not $_[0]->does('Net::IP::Identifier_Role')); },
23 3 );
24 has ip => (
25 is => 'rw',
26 1507 ▶ isa => sub { die "Not a Net::IP::Identifier::Net\n"
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0]->isa("Net::IP::Identifier::Net") 0 1507 1507
27 if (not $_[0]->isa('Net::IP::Identifier::Net')); },
28 3 );
29 }
30  
31  
32 package Net::IP::Identifier;
33 3 ▶use Getopt::Long qw(:config pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use Net::IP::Identifier::Net;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Net::IP::Identifier::Binode;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Net::IP::Identifier::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Module::Pluggable;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 3 our $VERSION = '0.110'; # VERSION
44  
45 has joiner => (
46 is => 'rw',
47 4 ▶ isa => sub { die "Not a string\n" if (ref $_[0]); },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 4 4
48 3 ▶ default => sub { ':' },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 3 );
50 3 has cidr => (
51 is => 'rw',
52 );
53 3 has parents => (
54 is => 'rw',
55 );
56 3 has overlaps => (
57 is => 'rw',
58 );
59 has re => ( # regular expressions for IP addresses
60 is => 'lazy',
61 0 ▶ default => sub { Net::IP::Identifier::Regex->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 3 );
63  
64 3 my $imports;
65  
66 3 my (undef, undef, $myName) = File::Spec->splitpath($0);
67  
68 3 my $help_msg = <<EO_HELP
69  
70 $myName [ options ] IP [ IP... ]
71  
72 If IP belongs to a known entity (a Net::IP::Identifier::Plugin),
73 print the entity.
74  
75 IP may be dotted decimal format: N.N.N.N, range format: N.N.N.N - N.N.N.N,
76 CIDR format: N.N.N.N/W, or a filename from which IPs will be extracted. If
77 no IP or filename is found on the command line, STDIN is opened.
78  
79 Options (may be abbreviated):
80 parents => prepend Net::IP::Identifier objects of parent entities
81 cidr => append Net::IP::Identifier::Net objects to entities
82 filename => read from file(s) instead of command line args
83 overlaps => show overlapping netblocks during binary tree construction
84 help => this message
85  
86 EO_HELP
87 ;
88  
89 3 ▶__PACKAGE__->run unless caller; # modulino
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 3 3
90  
91 sub run {
92 0 ▶ my ($class) = @_;
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
93  
94 0 my %opts;
95 my $overlaps;
96 0 my $filename;
97 0 my $help;
98  
99 exit 0 if (not
100 GetOptions(
101 'parents' => \$opts{parents},
102 'cidr' => \$opts{cidr},
103 0 ▶ 'overlaps' => \$overlaps,
Branch: 1 branch
BranchTrueFalseTotal
unless &GetOptions("parents", \$opts{"parents"}, "cidr", \$opts{"cidr"}, "overlaps", \$overlaps, "filename=s", \$filename, "help", \$help) 0 0 0
104 'filename=s' => \$filename,
105 'help' => \$help,
106 )
107 );
108  
109 0 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 0 0
110 0 print $help_msg;
111 0 exit;
112 }
113  
114 0 my $identifier = __PACKAGE__->new(%opts);
115  
116 0 ▶ unshift @ARGV, $filename if ($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $filename 0 0 0
117 0 ▶ if (not @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV) 0 0 0
118 0 $identifier->parse_fh(\*STDIN);
119 }
120  
121 0 ▶ while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 0 0
122 0 my $arg = shift @ARGV;
123 0 ▶ if (-f $arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $arg) { } 0 0 0
elsif ($ARGV[0] and $ARGV[0] eq '-' and $ARGV[1]) { } 0 0 0
124 0 open my $fh, '<', $arg;
125 0 ▶ croak "Can't open $arg for reading\n" if not $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
126 0 $identifier->parse_fh($fh);
127 0 close $fh;
128 next
129 0 }
130 ▶ elsif ($ARGV[0] and # accept N.N.N.N - N.N.N.N for network blocks too
Condition0%0/6
$ARGV[0] and $ARGV[0] eq "-" and $ARGV[1]
!ll&&!rl&&r
000
$ARGV[0] and $ARGV[0] eq "-"
!ll&&!rl&&r
000
MC/DC0%0/3
$ARGV[0] and $ARGV[0] eq "-" and $ARGV[1]
$ARGV[0] $ARGV[0] eq "-" $ARGV[1]
Truth table0%0/4
A and B and C
A= $ARGV[0]B= $ARGV[0] eq "-"C= $ARGV[1]
ABCresult
0XX0
10X0
1100
1111
131 $ARGV[0] eq '-' and
132 $ARGV[1]) {
133 0 $arg .= shift(@ARGV) . shift(@ARGV);
134 }
135  
136 0 ▶ print $identifier->identify($arg) || $arg, "\n";
Condition0%0/3
$identifier->identify($arg) || $arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$identifier->identify($arg) || $arg
$identifier->identify($arg) $arg
Truth table0%0/3
A || B
A= $identifier->identify($arg)B= $arg
ABresult
1X1
011
000
137 }
138  
139 0 ▶ if ($overlaps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overlaps) 0 0 0
140 0 for my $return (@{$identifier->tree_overlaps}) {
141 0 my @r = map { join $identifier->joiner, $_->payload->entity, $_->payload->ip; } @{$return};
142 0 warn join(' => ', @r), "\n";
143 }
144 }
145 }
146  
147 sub import {
148 3 ▶ my ($class, @imports) = @_;
Subroutine import: called CC=2 SCAR=7.2
149  
150 3 ▶ $imports = \@imports if (@imports); # save import list in class variable
Branch: 1 branch
BranchTrueFalseTotal
if @imports 3 0 3
151 }
152  
153 sub parse_fh {
154 0 ▶ my ($self, $fh) = @_;
Subroutine parse_fh: not called CC=4 SCAR=30.0
Pod undocumented
155  
156 0 my $ip_any = $self->re->IP_any;
157 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
158 0 my (@ips) = m/($ip_any)/;
159 0 for my $ip (@ips) {
160 0 ▶ print $self->identify($ip) || $ip, "\n";
Condition0%0/3
$self->identify($ip) || $ip
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->identify($ip) || $ip
$self->identify($ip) $ip
Truth table0%0/3
A || B
A= $self->identify($ip)B= $ip
ABresult
1X1
011
000
161 }
162 }
163 }
164  
165 sub load_entities {
166 4 ▶ my ($self, @plugins) = @_;
Subroutine load_entities: called CC=12 SCAR=27.0
Pod undocumented
167  
168 4 ▶ my $plugins = ref $plugins[0] eq 'ARRAY' # accept array or ref
Branch: 1 branch
BranchTrueFalseTotal
ref $plugins[0] eq 'ARRAY' ? : 3 1 4
169 ? $plugins[0] # a ref was passed in
170 : \@plugins; # convert array to ref
171 4 delete $self->{parent_of};
172 4 delete $self->{entities};
173 4 for my $plugin (@{$plugins}) {
174 #print "requiring $plugin\n";
175 37 ▶ if (not $plugin =~ m/::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin =~ /::/) 2 35 37
176 2 $plugin = __PACKAGE__ . "::Plugin::$plugin";
177 }
178 37 eval "CORE::require $plugin"; ## no critic # attempt to read in the plugin
179 37 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 37 37
180 37 ▶ my $p = $plugin && $plugin->new;
Condition33%1/3
$plugin && $plugin->new
!ll&&!rl&&r
0037
MC/DC0%0/2
$plugin && $plugin->new
$plugin $plugin->new
Truth table33%1/3
A && B
A= $pluginB= $plugin->new
ABresult
0X0
100
111
181 37 ▶ next if not $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 37 37
182 37 ▶ if (not $p->does('Net::IP::Identifier_Role')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->does("Net::IP::Identifier_Role")) 0 37 37
183 0 print "$plugin doesn't satisfy the Net::IP::Identifier_Role - skipping\n";
184 0 next;
185 }
186 37 push @{$self->{entities}}, $p;
187 37 for my $child ($p->children) {
188 2 $self->{parent_of}{$child} = $p;
189 }
190 }
191 4 ▶ if ( @$plugins and
Branch: 1 branch
BranchTrueFalseTotal
if (@$plugins and not $self->{"entities"} && @{$self->{"entities"};}) 0 4 4
Condition50%3/6
@$plugins and not $self->{"entities"} && @{$self->{"entities"};}
!ll&&!rl&&r
130
$self->{'entities'} && @{$self->{'entities'};}
!ll&&!rl&&r
003
MC/DC0%0/3
@$plugins and not $self->{"entities"} && @{$self->{"entities"};}
@$plugins $self->{'entities'} @{$self->{'entities'};}
Truth table50%2/4
A and not(B && C)
A= @$pluginsB= $self->{'entities'}C= @{$self->{'entities'};}
ABCresult
0XX0
1110
10X1
1101
192 (not $self->{entities} or
193 3 not @{$self->{entities}})) {
194 0 croak "No plugins installed\n";
195 }
196 4 delete $self->{ip_tree};
197 }
198  
199 sub entities {
200 17 ▶ my ($self, @plugins) = @_;
Subroutine entities: called CC=8 SCAR=21.1
Pod documented
201  
202 17 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 2 15 17
203 2 undef $imports; # override imports with @plugins
204 #print "load args: ", join(' ', @plugins), "\n";
205 2 $self->load_entities(@plugins);
206 }
207  
208 17 ▶ if (not $self->{entities} or
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"entities"} and @{$self->{"entities"};}) 2 15 17
Condition66%2/3
$self->{"entities"} and @{$self->{"entities"};}
!ll&&!rl&&r
2015
MC/DC50%1/2
$self->{"entities"} and @{$self->{"entities"};}
$self->{"entities"} @{$self->{"entities"};}
Truth table66%2/3
A and B
A= $self->{"entities"}B= @{$self->{"entities"};}
ABresult
0X0
100
111
209 15 not @{$self->{entities}}) {
210 # if no plugins yet loaded, check import list
211 # no import list? load everything we can find
212 2 ▶ if ($imports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imports) { } 1 1 2
213 #print "load imports ", join(' ', @{$imports}), "\n";
214 1 $self->load_entities($imports);
215 1 undef $imports; # only the first time
216 }
217 else {
218 #print "load imports ", join(' ', $self->plugins), "\n";
219 1 $self->load_entities([ $self->plugins ]);
220 }
221  
222 2 ▶ if (not $self->{entities} or
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"entities"} and @{$self->{"entities"};}) 0 2 2
Condition33%1/3
$self->{"entities"} and @{$self->{"entities"};}
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"entities"} and @{$self->{"entities"};}
$self->{"entities"} @{$self->{"entities"};}
Truth table33%1/3
A and B
A= $self->{"entities"}B= @{$self->{"entities"};}
ABresult
0X0
100
111
223 2 not @{$self->{entities}}) {
224 0 croak "No entity Plugins found\n";
225 }
226 }
227  
228 return wantarray
229 15 ? @{$self->{entities}}
230 17 ▶ : $self->{entities};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 2 17
231 }
232  
233 sub ip_tree {
234 57 ▶ my ($self, $version) = @_;
Subroutine ip_tree: called CC=8 SCAR=20.8
Pod undocumented
235  
236 57 ▶ croak "ip_tree(\$version) error: no version\n" if not $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 0 57 57
237  
238 57 ▶ if (not $self->{ip_tree}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ip_tree"}) 15 42 57
239 15 my $root_v6 = Net::IP::Identifier::Binode->new;
240 # Place the IPv4 block in the IPv6 tree (IPv4 mapped IPv6)
241 15 my $root_v4 = $root_v6->construct(Net::IP::Identifier::Net->new('::ffff:0:0/96')->masked_ip);
242  
243 15 for my $entity ($self->entities) {
244 66 for my $ip ($entity->ips) {
245 1268 my @ips = ($ip);
246 1268 ▶ if (not defined $ip->prefixlen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip->prefixlen) 142 1126 1268
247 142 @ips = $ip->range_to_cidrs;
248 }
249 1268 for my $ip (@ips) {
250 1507 ▶ my $root = ($ip->version == 6) ? $root_v6 : $root_v4;
Branch: 1 branch
BranchTrueFalseTotal
$ip->version == 6 ? : 124 1383 1507
251 1507 $root->construct($ip->masked_ip)->payload(
252 Local::Payload->new(
253 entity => $entity,
254 ip => $ip,
255 ),
256 );
257 }
258 }
259 }
260 15 $self->{ip_tree}{6} = $root_v6;
261 15 $self->{ip_tree}{4} = $root_v4;
262 }
263 57 return $self->{ip_tree}{$version};
264 }
265  
266 sub identify {
267 46 ▶ my ($self, $ip) = @_;
Subroutine identify: called CC=8 SCAR=20.9
Pod documented
268  
269 46 $ip = Net::IP::Identifier::Net->new($ip);
270 46 my @ips = ($ip);
271 46 ▶ if (not defined $ip->prefixlen) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip->prefixlen) 0 46 46
272 0 @ips = $ip->range_to_cidrs;
273 }
274  
275 46 my @return;
276 46 for my $ip (@ips) {
277 $self->ip_tree($ip->version)->follow($ip->masked_ip, sub {
278 2102 ▶ push @return, $_[0] if ($_[0]->payload);
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->payload 25 2077 2102
279 2102 return 0; # always continue
280 },
281 46 );
282 }
283 46 ▶ if (not @return) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@return) 21 25 46
284 21 return; # not found.
285 }
286  
287 25 ▶ if (not $self->parents) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parents) 25 0 25
288 25 @return = ($return[-1]); # just the last child
289 }
290  
291 25 @return = map { $_->payload } @return; # remove the Binode layer
292  
293 25 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 24 25
294 return $self->cidr
295 1 ▶ ? map { $_->entity, $_->ip } @return
Branch: 1 branch
BranchTrueFalseTotal
$self->cidr ? : 1 0 1
296 : @return;
297 }
298  
299 24 ▶ if ($self->cidr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cidr) 8 16 24
300 8 my @e = map { join ( $self->joiner, $_->entity, $_->ip) } @return;
301 8 return join ' => ', @e;
302 }
303 my $r = join (' => ', map {
304 16 $_->entity->name
305 } @return);
306 16 return $r;
307 }
308  
309 sub tree_overlaps {
310 11 ▶ my ($self) = @_;
Subroutine tree_overlaps: called CC=2 SCAR=7.0
Pod documented
311  
312 11 my @overlaps; # collect overlaps here. each overlap is an array
313 # starting with the parent, followed by children.
314  
315 $self->ip_tree(6)->traverse_width_first(
316 sub {
317 1179 ▶ my ($node, $level) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
318  
319 1179 my @overlap; # a single overlap array, parent then children
320 1179 ▶ if ($node->payload and
Branch: 1 branch
BranchTrueFalseTotal
if ($node->payload and $node->zero || $node->one) 10 1169 1179
Condition66%4/6
$node->payload and $node->zero || $node->one
!ll&&!rl&&r
1168110
$node->zero or $node->one
l!l&&r!l&&!r
1001
MC/DC0%0/3
$node->payload and $node->zero || $node->one
$node->payload $node->zero $node->one
Truth table50%2/4
A and B or C
A= $node->payloadB= $node->zeroC= $node->one
ABCresult
0XX0
1000
11X1
1011
321 ($node->zero or $node->one)) {
322 $node->traverse_width_first(
323 sub {
324 138 ▶ if ($_[0]->payload) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->payload) 26 112 138
325 26 push @overlap, $_[0];
326 }
327 138 return 0; # always continue
328 }
329 10 );
330 }
331 1179 ▶ push @overlaps, \@overlap if (@overlap > 1);
Branch: 1 branch
BranchTrueFalseTotal
if @overlap > 1 10 1169 1179
332 1179 return @overlap > 1; # stop if we found overlap
333 },
334 11 );
335  
336 return wantarray
337 ? @overlaps
338 11 ▶ : \@overlaps;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 11 0 11
339 }
340  
341 3 1;
342  
343 __END__
« Identifier/WhoisParser.pm ↑ Summary Identifier/Binode.pm »