| 1 | # | ||||||||||||
| 2 | # $Id: Splitsubnet.pm,v cd68184c46bd 2023/03/23 09:32:23 gomor $ | ||||||||||||
| 3 | # | ||||||||||||
| 4 | package OPP::Proc::Splitsubnet; | ||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use base qw(OPP::Proc); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | __PACKAGE__->cgBuildIndices; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | our $VERSION = '1.00'; | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | use Regexp::IPv4; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Regexp::IPv6; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | ▶ | use Net::IPv4Addr qw(ipv4_broadcast); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 1 | ▶ | use Socket qw(inet_aton inet_ntoa); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | my $ipv4_re = qr/^${Regexp::IPv4::IPv4_re}$/; | |||||||||||
| 18 | 1 | my $ipv6_re = qr/^${Regexp::IPv6::IPv6_re}$/; | |||||||||||
| 19 | |||||||||||||
| 20 | # | ||||||||||||
| 21 | # | splitsubnet | ||||||||||||
| 22 | # | splitsubnet subnetfield | ||||||||||||
| 23 | # | ||||||||||||
| 24 | sub process { | ||||||||||||
| 25 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
process: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 26 | 0 | my ($input) = @_; | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | my $options = $self->options; | |||||||||||
| 29 | 0 | ▶ | my $field = $options->{0} || 'subnet'; | ||||||||||
|
Condition0%0/2
$options->{'0'} || 'subnet'
MC/DC0%0/1
$options->{'0'} || 'subnet'
$options->{'0'}
Truth table0%0/2
A
A= $options->{'0'}
| |||||||||||||
| 30 | |||||||||||||
| 31 | 0 | my $values = $self->value($input, $field); | |||||||||||
| 32 | 0 | ▶ | unless ($values) { # No field by that name, don't touch | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 0 | $self->output->add($input); | |||||||||||
| 34 | 0 | return 1; | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 0 | ▶ | if (@$values > 1) { # Don't handle that, don't touch | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 0 | $self->output->add($input); | |||||||||||
| 39 | 0 | return 1; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 0 | my $v = $values->[0]; | |||||||||||
| 43 | 0 | my ($subnet, $cidr) = $v =~ m{^([^/]+)/(\d+)$}; | |||||||||||
| 44 | 0 | ▶ | unless ($subnet =~ $ipv4_re) { # IPv6 not handled yet, don't touch | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | $self->output->add($input); | |||||||||||
| 46 | 0 | return 1; | |||||||||||
| 47 | } | ||||||||||||
| 48 | 0 | ▶ | unless ($cidr < 16) { # Mask is ok, don't touch | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | $self->output->add($input); | |||||||||||
| 50 | 0 | return 1; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | # Mask is < 16, we split into multiple docs: | ||||||||||||
| 54 | 0 | my $count = 2 ** (32 - $cidr); | |||||||||||
| 55 | 0 | my $chunks = $count / 65536; # Chunk by X number of /16 | |||||||||||
| 56 | 0 | my $this_subnet = $subnet.'/16'; | |||||||||||
| 57 | 0 | for (1..$chunks) { | |||||||||||
| 58 | 0 | my $clone = $self->clone($input); | |||||||||||
| 59 | 0 | $self->set($clone, $field, $this_subnet); | |||||||||||
| 60 | 0 | $self->output->add($clone); | |||||||||||
| 61 | # Prepare for next round: | ||||||||||||
| 62 | 0 | my $last = ipv4_broadcast($this_subnet); | |||||||||||
| 63 | 0 | my $last_int = CORE::unpack('N', inet_aton($last)) + 1; | |||||||||||
| 64 | 0 | $this_subnet = inet_ntoa(pack('N', $last_int)).'/16'; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | return 1; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 1 | 1; | |||||||||||
| 71 | |||||||||||||
| 72 | __END__ | ||||||||||||