| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | #=============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # PODNAME: Logwatch::RecordTree::IPv4 | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: a subclass of Logwatch::RecordTree for IPv4 addresses | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # AUTHOR: Reid Augustin (REID) | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # EMAIL: reid@hellosix.com | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # CREATED: Thu Mar 12 18:41:04 PDT 2015 | ||||||||||||||||||||||||||||||||||||||||||
| 9 | #=============================================================================== | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | package Logwatch::RecordTree::IPv4; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use parent 'Logwatch::RecordTree'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use UNIVERSAL::require; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | # use Net::IP::Identifier 0.111 | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Sort::Key::IPv4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Sort::Key::Natural qw( natsort natkeysort ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | our $VERSION = '2.056'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | has identify => ( | |||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | has snowshoe => ( # number indicating width of mask to consider. 1 => 24 | |||||||||||||||||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $identifier; # class variable | |||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 12 | ▶ | sub identifier { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
identifier: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 12 | ▶ | if (not $self->{identifier}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | if (not $identifier) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | Net::IP::Identifier->require(0.111) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | or croak($@); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $identifier = Net::IP::Identifier->new; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | $self->{identifier} = $identifier; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 12 | return $self->{identifier}; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 31 | ▶ | sub create_child { # override | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_child: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 31 | my ($self, $name, $type, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 31 | my $child = $self->SUPER::create_child($name, $type, $opts); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | # this is why we're overriding parent's create_child method. we want | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # to do these when child is created so caller can make changes | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 17 | ▶ | $child->sprint_name(sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 17 | my ($child) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 17 | my $ip = $child->name; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 17 | ▶ | if ($self->identify and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->identify and $ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$] || $ip =~ m[^[\d:]+(/\d+)?$]
$ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$] or $ip =~ m[^[\d:]+(/\d+)?$]
MC/DC66%2/3
$self->identify and $ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$] || $ip =~ m[^[\d:]+(/\d+)?$]
$self->identify
$ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$]
$ip =~ m[^[\d:]+(/\d+)?$]
Truth table75%3/4
A and B or C
A= $self->identifyB= $ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$]C= $ip =~ m[^[\d:]+(/\d+)?$]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | ($ip =~ m/^\d+\.\d+\.\d+\.\d+(\/\d+)?$/ or | ||||||||||||||||||||||||||||||||||||||||||
| 63 | $ip =~ m/^[\d:]+(\/\d+)?$/)) { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 12 | my $id = $self->identifier->identify($ip); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 12 | ▶ | if ($id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 12 | ▶ | $id = substr($id, 0, 8) if (length $id > 8); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 12 | $ip = "$id-$ip"; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 17 | return $ip; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 31 | }); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 31 | return $child; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # the IP list may contain non-IP addresses, split into two lists: | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 5 | ▶ | sub split_ips { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_ips: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 5 | my ($self, $ips_orig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | my (@non_ips, @ips); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 5 | for my $ip (@{$ips_orig}) { | |||||||||||||||||||||||||||||||||||||||||
| 82 | 34 | ▶ | if ($ip =~ m/^\d+\.\d+\.\d+\.\d+(\/\d+)?$/ or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$] or $ip =~ m[^[\d:]+(/\d+)?$]
MC/DC50%1/2
$ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$] or $ip =~ m[^[\d:]+(/\d+)?$]
$ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$]
$ip =~ m[^[\d:]+(/\d+)?$]
Truth table66%2/3
A or B
A= $ip =~ m[^\d+\.\d+\.\d+\.\d+(/\d+)?$]B= $ip =~ m[^[\d:]+(/\d+)?$]
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | $ip =~ m/^[\d:]+(\/\d+)?$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 32 | push @ips, $ip; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | push @non_ips, $ip; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | return (\@non_ips, \@ips); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # sort a list of hosts which may include non-IP addresses | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | sub ipv4sort { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ipv4sort: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | my ($self, @ips_orig) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my ($non_ips, $ips) = $self->split_ips(\@ips_orig); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | my $case_sensitive = ref $self ? $self->case_sensitive : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | @{$non_ips} = $case_sensitive | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ? natsort @{$non_ips} | |||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | ▶ | : natkeysort { lc $_ } @{$non_ips}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | my %ips; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | for my $ip (@{$ips}) { | |||||||||||||||||||||||||||||||||||||||||
| 105 | 7 | my ($key) = $ip =~ m/([^\/]+)/; # key on just the IP part without range | |||||||||||||||||||||||||||||||||||||||||
| 106 | 7 | $ips{$key} = $ip; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 3 | my @sorted_keys = Sort::Key::IPv4::ipv4sort(keys %ips); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 3 | my @ips = map { $ips{$_} } @sorted_keys; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | return (@{$non_ips}, @ips); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | ▶ | sub sort_children { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_children: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | my %keys = map { (defined($_->sort_key) ? $_->sort_key : $_->name) => $_ } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | values %{$self->children}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | my @children = map { $keys{$_} } $self->ipv4sort(keys %keys); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | return wantarray | ||||||||||||||||||||||||||||||||||||||||||
| 122 | ? @children | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | : \@children; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | sub sprint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sprint: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | ▶ | if ($self->snowshoe) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | # create new child list and replace the old list | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | $self->children($self->condense_snowshoes); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | return $self->SUPER::sprint(@args); | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # convert decimal dotted quad to binary IP | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 54 | ▶ | sub ip_to_bin { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ip_to_bin: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 54 | my ($self, $ip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 54 | my $bin = Math::BigInt->new(0); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 54 | for my $part (split '\.', $ip) { | |||||||||||||||||||||||||||||||||||||||||
| 142 | 216 | $bin <<= 8; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 216 | $bin |= $part; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 54 | return $bin | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # convert binary IP to decimal dotted quad | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | ▶ | sub bin_to_ip { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bin_to_ip: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | my ($self, $bin) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 3 | my @parts; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | ▶ | while (@parts < 4) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 12 | unshift @parts, $bin & 0xff; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 12 | $bin >>= 8; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 3 | return join('.', @parts); | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | # return a mask of $width | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | ▶ | sub mask { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mask: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 5 | my ($self, $width) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | return Math::BigInt->new(1)->blsft($width)->bsub(1)->blsft(32-$width); | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | sub min_range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
min_range: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | my ($self, $group) = @_; # group is ordered list of Logwatch::RecordTrees with IPs as names | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | my $width = 32; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | my $mask = $self->mask($width); # full width mask to start | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | my $masked_ip = $self->ip_to_bin($group->[0]->name); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 3 | for my $item (@{$group}) { | |||||||||||||||||||||||||||||||||||||||||
| 175 | 25 | my $ip = $self->ip_to_bin($item->name); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 25 | ▶ | while ($width) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 42 | ▶ | last if (($ip & $mask) == $masked_ip); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 17 | $mask &= $mask->blsft(1); | |||||||||||||||||||||||||||||||||||||||||
| 179 | 17 | $width--; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 17 | $masked_ip &= $mask; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | return $self->bin_to_ip($masked_ip). "/$width"; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | # hackers often rent IP blocks (/24 is common) so the source IP isn't | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # exactly duplicated. Collect IPs within a block into single child. | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 2 | ▶ | sub condense_snowshoes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
condense_snowshoes: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | my $mask_width = $self->snowshoe; | |||||||||||||||||||||||||||||||||||||||||
| 192 | # mask width of 1 is pretty useless, so we'll interpret it as /24: | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | $mask_width = 24 if ($mask_width == 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | my $mask = $self->mask($mask_width); | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | my ($non_ips, $ips) = $self->split_ips([keys %{$self->children}]); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 2 | @{$ips} = Sort::Key::IPv4::ipv4sort(@{$ips}); | |||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | my ($masked_ip, $count, @group, %new_children); | |||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | for my $ip (@{$ips}, '') { # add dummy at end to flush | |||||||||||||||||||||||||||||||||||||||||
| 201 | 27 | my $child; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 27 | ▶ | $child = $self->child_by_name($ip) if ($ip); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 27 | ▶ | if ($masked_ip) { # skip the first time through | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 25 | ▶ | if ($ip and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ip and $masked_ip == ($self->ip_to_bin($ip) & $mask)
MC/DC100%2/2
$ip and $masked_ip == ($self->ip_to_bin($ip) & $mask)
$ip
$masked_ip == ($self->ip_to_bin($ip) & $mask)
Truth table100%3/3
A and B
A= $ipB= $masked_ip == ($self->ip_to_bin($ip) & $mask)
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | $masked_ip == ($self->ip_to_bin($ip) & $mask)) { # in range? | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 22 | $count += $child->count; | |||||||||||||||||||||||||||||||||||||||||
| 207 | 22 | push @group, $child; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | else { # out of range (or last time through the loop with dummy) | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 3 | ▶ | if (@group < 3) { # require at least three before condensing | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | map { $new_children{$_->name} = $_ } @group; # copy to new list | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 3 | my $name = $self->min_range(\@group); | |||||||||||||||||||||||||||||||||||||||||
| 215 | my $new_child | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 3 | = $new_children{$name} | |||||||||||||||||||||||||||||||||||||||||
| 217 | = $group[0]->new( # clone first child | ||||||||||||||||||||||||||||||||||||||||||
| 218 | name => $name, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | sprint_name => $group[0]->sprint_name, | ||||||||||||||||||||||||||||||||||||||||||
| 220 | count_fields => [ '/', scalar @group ], | ||||||||||||||||||||||||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # transfer any children from group items to new parent | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | for my $item (@group) { | |||||||||||||||||||||||||||||||||||||||||
| 224 | 25 | my @g_children = values %{$item->children}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 25 | ▶ | if (@g_children) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 3 | for my $child (@g_children) { | |||||||||||||||||||||||||||||||||||||||||
| 227 | 4 | $new_child->adopt($child); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | else { # no children, count is entirely from item | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 22 | $new_child->count($new_child->count + $item->count); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | undef $masked_ip; # start a new range | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 27 | ▶ | if ($ip and not $masked_ip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ip and not $masked_ip
MC/DC100%2/2
$ip and not $masked_ip
$ip
not $masked_ip
Truth table100%3/3
A and not(B)
A= $ipB= not $masked_ip
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 3 | $masked_ip = $self->ip_to_bin($ip) & $mask; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 3 | @group = ( $self->child_by_name($ip) ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 3 | $count = $child->count; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | # rejoin the non-IP children | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | map { $new_children{$_} = $self->child_by_name($_) } @{$non_ips}; | |||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | $self->children(\%new_children); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | __END__ | ||||||||||||||||||||||||||||||||||||||||||