| 1 | package Net::CIDR::Set::IPv6; | ||||||||||||||||||||||||||||||||
| 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 IPv6 addresses | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 9 | our $VERSION = '0.23'; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 42 | ▶ | 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 | |||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub _pack_ipv4 { | ||||||||||||||||||||||||||||||
|
Subroutine
_pack_ipv4: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 18 | 0 | my @nums = split /[.]/, shift, -1; | |||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | return unless @nums == 4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 0 | for ( @nums ) { | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | return unless /\A${DEC}\z/ and !/\A0[1-9]+/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/\A$DEC\z/ and not /\A0[1-9]+/
MC/DC0%0/2
/\A$DEC\z/ and not /\A0[1-9]+/
/\A$DEC\z/
not /\A0[1-9]+/
Truth table0%0/3
A and not(B)
A= /\A$DEC\z/B= not /\A0[1-9]+/
| |||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | 0 | return pack "CC*", 0, @nums; | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub _426 { | ||||||||||||||||||||||||||||||
|
Subroutine
_426: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 27 | 0 | my @nums = split /[.]/, shift, -1; | |||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | return if grep $_ > 255, @nums; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 0 | return join( ":", unpack( 'H*', pack 'C*', @nums ) =~ /..../g ); | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 112 | ▶ | sub _pack { | ||||||||||||||||||||||||||||||
|
Subroutine
_pack: called CC=17 SCAR=32.6
| |||||||||||||||||||||||||||||||||
| 33 | 112 | my $ip = shift; | |||||||||||||||||||||||||||||||
| 34 | 112 | ▶ | return pack( 'H*', '0' x 33 ) if $ip eq '::'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 109 | ▶ | return if $ip =~ /\A:/ and $ip !~ s/\A::/:/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ip =~ /\A:/ and not $ip =~ s/\A::/:/
MC/DC0%0/2
$ip =~ /\A:/ and not $ip =~ s/\A::/:/
$ip =~ /\A:/
not $ip =~ s/\A::/:/
Truth table66%2/3
A and not(B)
A= $ip =~ /\A:/B= not $ip =~ s/\A::/:/
| |||||||||||||||||||||||||||||||||
| 36 | 109 | ▶ | return if $ip =~ /:\z/ and $ip !~ s/::\z/:/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ip =~ /:\z/ and not $ip =~ s/::\z/:/
MC/DC0%0/2
$ip =~ /:\z/ and not $ip =~ s/::\z/:/
$ip =~ /:\z/
not $ip =~ s/::\z/:/
Truth table66%2/3
A and not(B)
A= $ip =~ /:\z/B= not $ip =~ s/::\z/:/
| |||||||||||||||||||||||||||||||||
| 37 | 109 | my @nums = split /:/, $ip, -1; | |||||||||||||||||||||||||||||||
| 38 | 109 | ▶ | return unless @nums <= 8; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 109 | my ( $empty, $ipv4, $str ) = ( 0, '', '' ); | |||||||||||||||||||||||||||||||
| 40 | 109 | for ( @nums ) { | |||||||||||||||||||||||||||||||
| 41 | 469 | ▶ | return if $ipv4; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 469 | ▶ | $str .= "0" x ( 4 - length ) . $_, next if /\A[A-Fa-f0-9]{1,4}\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 99 | ▶ | do { return if $empty++ }, $str .= "X", next if $_ eq ''; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | next if $ipv4 = _pack_ipv4( $_ ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 0 | return; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | 109 | ▶ | return if $ipv4 and @nums > 6; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ipv4 and @nums > 6
MC/DC0%0/2
$ipv4 and @nums > 6
$ipv4
@nums > 6
Truth table33%1/3
A and B
A= $ipv4B= @nums > 6
| |||||||||||||||||||||||||||||||||
| 48 | 109 | ▶ | $str =~ s/X/"0" x (($ipv4 ? 25 : 33)-length($str))/e if $empty; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 49 | 109 | return pack( "H*", "00" . $str ) . $ipv4; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 45 | ▶ | sub _unpack { | ||||||||||||||||||||||||||||||
|
Subroutine
_unpack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 53 | 45 | return _compress_ipv6( | |||||||||||||||||||||||||||||||
| 54 | join( ":", unpack( "xH*", shift ) =~ /..../g ) ); | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | # Replace longest run of null blocks with a double colon | ||||||||||||||||||||||||||||||||
| 58 | 45 | ▶ | sub _compress_ipv6 { | ||||||||||||||||||||||||||||||
|
Subroutine
_compress_ipv6: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 59 | 45 | my $ip = shift; | |||||||||||||||||||||||||||||||
| 60 | 45 | ▶ | if ( my @runs = $ip =~ /((?:(?:\A|:)(?:0000))+:?)/g ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 38 | my $max = $runs[0]; | |||||||||||||||||||||||||||||||
| 62 | 38 | for ( @runs[ 1 .. $#runs ] ) { | |||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | $max = $_ if length( $max ) < length; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 38 | $ip =~ s/$max/::/; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | 45 | $ip =~ s/:0{1,3}/:/g; | |||||||||||||||||||||||||||||||
| 68 | 45 | return $ip; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 60 | ▶ | sub _width2bits { | ||||||||||||||||||||||||||||||
|
Subroutine
_width2bits: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 72 | 60 | my ( $width, $size ) = @_; | |||||||||||||||||||||||||||||||
| 73 | 60 | return pack 'B*', | |||||||||||||||||||||||||||||||
| 74 | ( '1' x ( $width + 8 ) ) . ( '0' x ( $size - $width ) ); | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 33 | ▶ | sub _is_cidr { | ||||||||||||||||||||||||||||||
|
Subroutine
_is_cidr: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 78 | 33 | my ( $lo, $hi ) = @_; | |||||||||||||||||||||||||||||||
| 79 | 33 | my $mask = ~( $lo ^ $hi ); | |||||||||||||||||||||||||||||||
| 80 | 33 | my $bits = unpack 'B*', $mask; | |||||||||||||||||||||||||||||||
| 81 | 33 | ▶ | return unless $hi eq ($lo | $hi); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 27 | ▶ | return unless $bits =~ /\A(1*)0*\z/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 23 | return length( $1 ) - 8; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 97 | ▶ | sub _encode { | ||||||||||||||||||||||||||||||
|
Subroutine
_encode: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||
| 87 | 97 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 88 | 97 | ▶ | if ( $ip =~ m{\A([0-9A-Fa-f:]+)/(0|[1-9][0-9]*)\z} ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 89 | 61 | my $mask = $2; | |||||||||||||||||||||||||||||||
| 90 | 61 | ▶ | return if $mask > 128; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | 60 | ▶ | return unless my $addr = _pack( $1 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 60 | ▶ | return unless my $bits = _width2bits( $mask, 128 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 60 | return ( $addr & $bits, Net::CIDR::Set::_inc( $addr | ~$bits ) ); | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | elsif ( $ip =~ m{\A([0-9A-Fa-f:]+)-([0-9A-Fa-f:]+)\z} ) { | ||||||||||||||||||||||||||||||||
| 96 | 26 | my ( $from, $to ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||
| 97 | 26 | ▶ | return unless my $lo = _pack( $from ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 26 | ▶ | return unless my $hi = _pack( $to ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 26 | return ( $lo, Net::CIDR::Set::_inc( $hi ) ); | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | elsif ( $ip =~ m{\A[0-9A-Fa-f:]+\z} ) { | ||||||||||||||||||||||||||||||||
| 102 | 2 | return $self->_encode( "$ip/128" ); | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||
| 105 | 8 | return; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 95 | ▶ | sub encode { | ||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 110 | 95 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||
| 111 | 95 | ▶ | my @r = $self->_encode( $ip ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | or croak "Can't decode $ip as an IPv6 address"; | ||||||||||||||||||||||||||||||||
| 113 | 86 | return @r; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 34 | ▶ | sub decode { | ||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 117 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||
| 118 | 34 | my $lo = shift; | |||||||||||||||||||||||||||||||
| 119 | 34 | my $hi = Net::CIDR::Set::_dec( shift ); | |||||||||||||||||||||||||||||||
| 120 | 34 | ▶ | my $generic = shift || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 121 | 34 | ▶ | if ( $generic < 1 && $lo eq $hi ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$generic < 1 and $lo eq $hi
MC/DC0%0/2
$generic < 1 and $lo eq $hi
$generic < 1
$lo eq $hi
Truth table66%2/3
A and B
A= $generic < 1B= $lo eq $hi
| |||||||||||||||||||||||||||||||||
| 122 | # Single address | ||||||||||||||||||||||||||||||||
| 123 | 0 | return _unpack( $lo ); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | ▶ | 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))
| |||||||||||||||||||||||||||||||||
| 126 | # Valid CIDR range | ||||||||||||||||||||||||||||||||
| 127 | 23 | return join '/', _unpack( $lo ), $w; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||||||
| 130 | # General range | ||||||||||||||||||||||||||||||||
| 131 | 11 | return join '-', _unpack( $lo ), _unpack( $hi ); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 1 | ▶ | sub nbits { 128 } | ||||||||||||||||||||||||||||||
|
Subroutine
nbits: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 9 | 1; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||||||