| 1 | package Net::Subnet; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use Socket; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=9.2
| |||||||||||||||||||
| 6 | 2 | ▶ | if (defined &Socket::inet_pton) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 7 | 2 | Socket->import(qw(inet_pton AF_INET6)); | |||||||||||||||||
| 8 | } else { | ||||||||||||||||||
| 9 | 0 | require Socket6; | |||||||||||||||||
| 10 | 0 | Socket6->import(qw(inet_pton AF_INET6)); | |||||||||||||||||
| 11 | } | ||||||||||||||||||
| 12 | }; | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 2 | our @EXPORT = qw(subnet_matcher subnet_classifier sort_subnets); | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 2 | our $VERSION = '1.03'; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 3 | ▶ | sub cidr2mask_v4 { | ||||||||||||||||
|
Subroutine
cidr2mask_v4: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 21 | 3 | my ($length) = @_; | |||||||||||||||||
| 22 | 3 | return pack "N", 0xffffffff << (32 - $length); | |||||||||||||||||
| 23 | } | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 12 | ▶ | sub cidr2mask_v6 { | ||||||||||||||||
|
Subroutine
cidr2mask_v6: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 26 | 12 | my ($length) = @_; | |||||||||||||||||
| 27 | 12 | return pack('B128', '1' x $length); | |||||||||||||||||
| 28 | } | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 11 | ▶ | sub subnet_matcher { | ||||||||||||||||
|
Subroutine
subnet_matcher: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 31 | 11 | ▶ | @_ > 1 and goto &multi_matcher; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 10 | my ($net, $mask) = split m[/], shift; | |||||||||||||||||
| 34 | 10 | ▶ | return $net =~ /:/ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | ? ipv6_matcher($net, $mask) | ||||||||||||||||||
| 36 | : ipv4_matcher($net, $mask); | ||||||||||||||||||
| 37 | } | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 2 | ▶ | sub ipv4_matcher { | ||||||||||||||||
|
Subroutine
ipv4_matcher: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||
| 40 | 2 | my ($net, $mask) = @_; | |||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 2 | $net = inet_aton($net); | |||||||||||||||||
| 43 | 2 | ▶ | $mask = $mask =~ /\./ ? inet_aton($mask) : cidr2mask_v4($mask); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | 2 | my $masked_net = $net & $mask; | |||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 2 | ▶ | return sub { ((inet_aton(shift) // return !1) & $mask) eq $masked_net }; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition50%1/2
inet_aton(shift()) //
MC/DC0%0/1
inet_aton(shift()) //
inet_aton(shift())
Truth table50%1/2
A
A= inet_aton(shift())
| |||||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 8 | ▶ | sub ipv6_matcher { | ||||||||||||||||
|
Subroutine
ipv6_matcher: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||
| 51 | 8 | my ($net, $mask) = @_; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 8 | $net = inet_pton(AF_INET6, $net); | |||||||||||||||||
| 54 | 8 | ▶ | $mask = $mask =~ /:/ ? inet_pton(AF_INET6, $mask) : cidr2mask_v6($mask); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 8 | my $masked_net = $net & $mask; | |||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 75 | ▶ | return sub { ((inet_pton(AF_INET6,shift)//return!1) & $mask) eq $masked_net} | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition50%1/2
inet_pton(10, shift()) //
MC/DC0%0/1
inet_pton(10, shift()) //
inet_pton(10, shift())
Truth table50%1/2
A
A= inet_pton(10, shift())
| |||||||||||||||||||
| 59 | 8 | } | |||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 1 | ▶ | sub multi_matcher { | ||||||||||||||||
|
Subroutine
multi_matcher: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 62 | 1 | my @v4 = map subnet_matcher($_), grep !/:/, @_; | |||||||||||||||||
| 63 | 1 | my @v6 = map subnet_matcher($_), grep /:/, @_; | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 19 | ▶ | return sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 66 | 19 | ▶ | $_->($_[0]) and return 1 for $_[0] =~ /:/ ? @v6 : @v4; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 67 | 9 | return !!0; | |||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | 1 | } | |||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 2 | ▶ | use constant MATCHER => 0; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 72 | 2 | ▶ | use constant SUBNET => 1; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 1 | ▶ | sub subnet_classifier { | ||||||||||||||||
|
Subroutine
subnet_classifier: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 75 | # MATCHER, SUBNET | ||||||||||||||||||
| 76 | 1 | my @v4 = map [ subnet_matcher($_), $_ ], grep !/:/, @_; | |||||||||||||||||
| 77 | 1 | my @v6 = map [ subnet_matcher($_), $_ ], grep /:/, @_; | |||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | 19 | ▶ | return sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 80 | $_->[MATCHER]->($_[0]) and return $_->[SUBNET] | ||||||||||||||||||
| 81 | 19 | ▶ | for $_[0] =~ /:/ ? @v6 : @v4; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 82 | 9 | return undef; | |||||||||||||||||
| 83 | } | ||||||||||||||||||
| 84 | 1 | } | |||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 1 | ▶ | sub sort_subnets { | ||||||||||||||||
|
Subroutine
sort_subnets: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||
| 87 | 1 | my @unsorted; | |||||||||||||||||
| 88 | 1 | for (@_) { | |||||||||||||||||
| 89 | 5 | my ($net, $mask) = split m[/]; | |||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | 5 | ▶ | $mask = $net =~ /:/ | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 92 | ? ($mask =~ /:/ ? inet_pton(AF_INET6, $mask) : cidr2mask_v6($mask)) | ||||||||||||||||||
| 93 | : ($mask =~ /\./ ? inet_aton($mask) : cidr2mask_v4($mask)); | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 5 | ▶ | $net = $net =~ /:/ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | ? inet_pton(AF_INET6, $net) | ||||||||||||||||||
| 97 | : inet_aton($net); | ||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 5 | push @unsorted, sprintf "%-16s%-16s%s", ($net & $mask), $mask, $_; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 1 | return map substr($_, 32), reverse sort @unsorted; | |||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 2 | 1; | |||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||