| 1 | package Net::Works::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw( confess ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Math::Int128 qw( net_to_uint128 uint128_to_net ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Socket qw( AF_INET AF_INET6 inet_pton inet_ntop ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 16 | _string_address_to_integer | ||||||||||||||||||||||||||||
| 17 | _integer_address_to_binary | ||||||||||||||||||||||||||||
| 18 | _binary_address_to_string | ||||||||||||||||||||||||||||
| 19 | _integer_address_to_string | ||||||||||||||||||||||||||||
| 20 | _validate_ip_string | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||||||||||||||||||||||||||||
| 24 | 900 | ▶ | sub _string_address_to_integer { | ||||||||||||||||||||||||||
|
Subroutine
_string_address_to_integer: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 25 | 900 | my $string = shift; | |||||||||||||||||||||||||||
| 26 | 900 | my $version = shift; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 900 | ▶ | my $binary = inet_pton( $version == 4 ? AF_INET : AF_INET6, $string ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | or return; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 882 | ▶ | return $version == 4 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | ? unpack( N => $binary ) | ||||||||||||||||||||||||||||
| 33 | : net_to_uint128($binary); | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2736 | ▶ | sub _integer_address_to_binary { | ||||||||||||||||||||||||||
|
Subroutine
_integer_address_to_binary: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 37 | 2736 | my $integer = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2736 | ▶ | if ( ref $integer && blessed $integer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $integer and builtin::blessed($integer)
MC/DC50%1/2
ref $integer and builtin::blessed($integer)
ref $integer
builtin::blessed($integer)
Truth table66%2/3
A and B
A= ref $integerB= builtin::blessed($integer)
| |||||||||||||||||||||||||||||
| 40 | 2259 | return uint128_to_net($integer); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||||||||||
| 43 | 477 | return pack( N => $integer ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2736 | ▶ | sub _binary_address_to_string { | ||||||||||||||||||||||||||
|
Subroutine
_binary_address_to_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 48 | 2736 | my $binary = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2736 | ▶ | my $family = length($binary) == 4 ? AF_INET : AF_INET6; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2736 | my $string = inet_ntop( $family, $binary ); | |||||||||||||||||||||||||||
| 53 | 2736 | ▶ | return $string eq '::' ? '::0' : $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2736 | ▶ | sub _integer_address_to_string { | ||||||||||||||||||||||||||
|
Subroutine
_integer_address_to_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 2736 | _binary_address_to_string( _integer_address_to_binary( $_[0] ) ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 84 | ▶ | sub _validate_ip_string { | ||||||||||||||||||||||||||
|
Subroutine
_validate_ip_string: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 61 | 84 | my $str = shift; | |||||||||||||||||||||||||||
| 62 | 84 | my $version = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 84 | ▶ | my $str_val = defined $str ? $str : 'undef'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 84 | ▶ | if ( $version == 4 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | confess("$str_val is not a valid IPv4 address") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $str and defined inet_pton(2, $str)
MC/DC0%0/2
defined $str and defined inet_pton(2, $str)
defined $str
defined inet_pton(2, $str)
Truth table66%2/3
A and B
A= defined $strB= defined inet_pton(2, $str)
| |||||||||||||||||||||||||||||
| 67 | unless defined $str && defined inet_pton( AF_INET, $str ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 77 | ▶ | confess("$str_val is not a valid IPv6 address") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $str and defined inet_pton(10, $str)
MC/DC100%2/2
defined $str and defined inet_pton(10, $str)
defined $str
defined inet_pton(10, $str)
Truth table100%3/3
A and B
A= defined $strB= defined inet_pton(10, $str)
| |||||||||||||||||||||||||||||
| 71 | unless defined $str && defined inet_pton( AF_INET6, $str ); | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # ABSTRACT: Utility subroutines for Net-Works | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||