« lib/IO/K8s/Role/MiddlewareBuilder.pm
↑ Summary
lib/IO/K8s/ApiextensionsApiserver/Pkg/Apis/Apiextensions/V1/JSONSchemaPropsOrArray.pm »
| 1 | package IO::K8s::Types::Net; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Type::Tiny constraints for IP addresses and CIDR notation | ||||||||||||||||||||||||||||||||||
| 3 | 25 | our $VERSION = '1.112'; | |||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use v5.10; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Type::Library -base, -declare => qw( IPv4 IPv6 IPAddress CIDR NetIP ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use Type::Utils -all; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use Types::Standard -types; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use Net::IP (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 25 | our @EXPORT_OK = qw( parse_ip cidr_contains is_rfc1918 ); | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | declare IPv4, as Str, where { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | !/\// && do { my $ip = Net::IP->new($_, 4); defined $ip && $ip->version == 4 }; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | }, message { "'$_' is not a valid IPv4 address" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | declare IPv6, as Str, where { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 20 | 6 | ▶ | !/\// && do { my $ip = Net::IP->new($_, 6); defined $ip && $ip->version == 6 }; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | }, message { "'$_' is not a valid IPv6 address" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | declare IPAddress, as Str, where { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | !/\// && defined Net::IP->new($_); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | }, message { "'$_' is not a valid IP address" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | declare CIDR, as Str, where { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 30 | 6 | ▶ | /\// && defined Net::IP->new($_); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | }, message { "'$_' is not valid CIDR notation" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 25 | declare NetIP, as InstanceOf['Net::IP']; | |||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | coerce NetIP, from Str, via { Net::IP->new($_) }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub parse_ip { | ||||||||||||||||||||||||||||||||
|
Subroutine
parse_ip: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 39 | 3 | my ($str) = @_; | |||||||||||||||||||||||||||||||||
| 40 | 3 | return Net::IP->new($str); | |||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 27 | ▶ | sub cidr_contains { | ||||||||||||||||||||||||||||||||
|
Subroutine
cidr_contains: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 45 | 27 | my ($cidr_str, $ip_str) = @_; | |||||||||||||||||||||||||||||||||
| 46 | 27 | ▶ | my $cidr = Net::IP->new($cidr_str) or return 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 26 | ▶ | my $ip = Net::IP->new($ip_str) or return 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | 25 | my $overlap = $ip->overlaps($cidr); | |||||||||||||||||||||||||||||||||
| 49 | 25 | ▶ | return defined $overlap && ($overlap == $Net::IP::IP_A_IN_B_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL); | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
defined $overlap && ($overlap == $Net::IP::IP_A_IN_B_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL)
$overlap == $Net::IP::IP_A_IN_B_OVERLAP or $overlap == $Net::IP::IP_IDENTICAL
MC/DC33%1/3
defined $overlap && ($overlap == $Net::IP::IP_A_IN_B_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL)
defined $overlap
$overlap == $Net::IP::IP_A_IN_B_OVERLAP
$overlap == $Net::IP::IP_IDENTICAL
Truth table50%2/4
A && B or C
A= defined $overlapB= $overlap == $Net::IP::IP_A_IN_B_OVERLAPC= $overlap == $Net::IP::IP_IDENTICAL
| |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | sub is_rfc1918 { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_rfc1918: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 54 | 9 | my ($ip_str) = @_; | |||||||||||||||||||||||||||||||||
| 55 | 9 | ▶ | return cidr_contains('10.0.0.0/8', $ip_str) | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
cidr_contains('10.0.0.0/8', $ip_str) || cidr_contains('172.16.0.0/12', $ip_str) || cidr_contains('192.168.0.0/16', $ip_str)
cidr_contains("10.0.0.0/8", $ip_str) or cidr_contains("172.16.0.0/12", $ip_str)
MC/DC100%3/3
cidr_contains('10.0.0.0/8', $ip_str) || cidr_contains('172.16.0.0/12', $ip_str) || cidr_contains('192.168.0.0/16', $ip_str)
cidr_contains("10.0.0.0/8", $ip_str)
cidr_contains("172.16.0.0/12", $ip_str)
cidr_contains('192.168.0.0/16', $ip_str)
Truth table100%4/4
A or B || C
A= cidr_contains("10.0.0.0/8", $ip_str)B= cidr_contains("172.16.0.0/12", $ip_str)C= cidr_contains('192.168.0.0/16', $ip_str)
| |||||||||||||||||||||||||||||||||||
| 56 | || cidr_contains('172.16.0.0/12', $ip_str) | ||||||||||||||||||||||||||||||||||
| 57 | || cidr_contains('192.168.0.0/16', $ip_str); | ||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 25 | 1; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||||||||