| 1 | package Net::CIDR::Set::IPv4; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Encode / decode IPv4 addresses | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 9 | our $VERSION = '0.23'; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 97 | ▶ | sub new { bless \my $x, shift } | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 9 | my $DEC = "(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})"; | |||||||||||||||||||||||||||||||
| 16 | 9 | my $IP = "(?:${DEC}\.){3}${DEC}"; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 207 | ▶ | sub _pack { | ||||||||||||||||||||||||||||||
|
Subroutine
_pack: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||
| 19 | 207 | my @nums = split /[.]/, shift, -1; | |||||||||||||||||||||||||||||||
| 20 | 207 | ▶ | return unless @nums == 4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 207 | for ( @nums ) { | |||||||||||||||||||||||||||||||
| 22 | 825 | ▶ | return unless /\A${DEC}\z/ and !/\A0[1-9]+/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/\A$DEC\z/ and not /\A0[1-9]+/
MC/DC50%1/2
/\A$DEC\z/ and not /\A0[1-9]+/
/\A$DEC\z/
not /\A0[1-9]+/
Truth table66%2/3
A and not(B)
A= /\A$DEC\z/B= not /\A0[1-9]+/
| |||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | 206 | return pack "CC*", 0, @nums; | |||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 75 | ▶ | sub _unpack { join ".", unpack "xC*", shift } | ||||||||||||||||||||||||||||||
|
Subroutine
_unpack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 110 | ▶ | sub _width2bits { | ||||||||||||||||||||||||||||||
|
Subroutine
_width2bits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 110 | my ( $width, $size ) = @_; | |||||||||||||||||||||||||||||||
| 31 | 110 | return pack 'B*', | |||||||||||||||||||||||||||||||
| 32 | ( '1' x ( $width + 8 ) ) . ( '0' x ( $size - $width ) ); | ||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | sub _ip2bits { | ||||||||||||||||||||||||||||||
|
Subroutine
_ip2bits: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my $ip = shift or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 2 | vec( $ip, 0, 8 ) = 255; | |||||||||||||||||||||||||||||||
| 38 | 2 | my $bits = unpack 'B*', $ip; | |||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | return unless $bits =~ /\A1*0*\z/; # Valid mask? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 1 | return $ip; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 52 | ▶ | sub _is_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_cidr: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 44 | 52 | my ( $lo, $hi ) = @_; | |||||||||||||||||||||||||||||||
| 45 | 52 | my $mask = ~( $lo ^ $hi ); | |||||||||||||||||||||||||||||||
| 46 | 52 | my $bits = unpack 'B*', $mask; | |||||||||||||||||||||||||||||||
| 47 | 52 | ▶ | return unless $hi eq ($lo | $hi); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 47 | ▶ | return unless $bits =~ /\A(1*)0*\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 36 | return length( $1 ) - 8; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 207 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||
| 53 | 207 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 54 | 207 | ▶ | if ( $ip =~ m{\A(${IP})/((?:3[0-2]|[12]?[0-9])|${IP})\z} ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 55 | 113 | my $mask = $2; | |||||||||||||||||||||||||||||||
| 56 | 113 | ▶ | return unless my $addr = _pack( $1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | return | ||||||||||||||||||||||||||||||||
| 58 | 112 | ▶ | unless my $bits | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 59 | = ( $mask =~ /\A[0-9]+\z/ ) | ||||||||||||||||||||||||||||||||
| 60 | ? _width2bits( $mask, 32 ) | ||||||||||||||||||||||||||||||||
| 61 | : _ip2bits( _pack( $mask ) ); | ||||||||||||||||||||||||||||||||
| 62 | 111 | return ( $addr & $bits, Net::CIDR::Set::_inc( $addr | ~$bits ) ); | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | elsif ( $ip =~ m{\A(${IP})-(${IP})\z} ) { | ||||||||||||||||||||||||||||||||
| 65 | 46 | ▶ | return unless my $lo = _pack( $1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 46 | ▶ | return unless my $hi = _pack( $2 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 46 | return ( $lo, Net::CIDR::Set::_inc( $hi ) ); | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | elsif ( $ip =~ m{\A${IP}\z} ) { | ||||||||||||||||||||||||||||||||
| 70 | 5 | return $self->_encode( "$ip/32" ); | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||||||
| 73 | 43 | return; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 202 | ▶ | sub encode { | ||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 78 | 202 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 79 | 202 | ▶ | my @r = $self->_encode( $ip ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | or croak "Can't decode $ip as an IPv4 address"; | ||||||||||||||||||||||||||||||||
| 81 | 157 | return @r; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 57 | ▶ | sub decode { | ||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 85 | 57 | my $self = shift; | |||||||||||||||||||||||||||||||
| 86 | 57 | my $lo = shift; | |||||||||||||||||||||||||||||||
| 87 | 57 | my $hi = Net::CIDR::Set::_dec( shift ); | |||||||||||||||||||||||||||||||
| 88 | 57 | ▶ | my $generic = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 89 | 57 | ▶ | if ( $generic < 1 && $lo eq $hi ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$generic < 1 and $lo eq $hi
MC/DC100%2/2
$generic < 1 and $lo eq $hi
$generic < 1
$lo eq $hi
Truth table100%3/3
A and B
A= $generic < 1B= $lo eq $hi
| |||||||||||||||||||||||||||||||||
| 90 | # Single address | ||||||||||||||||||||||||||||||||
| 91 | 3 | return _unpack( $lo ); | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | ▶ | elsif ( $generic < 2 && defined( my $w = _is_cidr( $lo, $hi ) ) ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$generic < 2 and defined(my $w = _is_cidr($lo, $hi))
MC/DC100%2/2
$generic < 2 and defined(my $w = _is_cidr($lo, $hi))
$generic < 2
defined(my $w = _is_cidr($lo, $hi))
Truth table100%3/3
A and B
A= $generic < 2B= defined(my $w = _is_cidr($lo, $hi))
| |||||||||||||||||||||||||||||||||
| 94 | # Valid CIDR range | ||||||||||||||||||||||||||||||||
| 95 | 36 | return join '/', _unpack( $lo ), $w; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||
| 98 | # General range | ||||||||||||||||||||||||||||||||
| 99 | 18 | return join '-', _unpack( $lo ), _unpack( $hi ); | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 38 | ▶ | sub nbits { 32 } | ||||||||||||||||||||||||||||||
|
Subroutine
nbits: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 9 | 1; | |||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||||||