| 1 | # Paranoid::Network::IPv6 -- IPv6-specific network functions | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # $Id: lib/Paranoid/Network/IPv6.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ | ||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||
| 5 | # This software is free software. Similar to Perl, you can redistribute it | ||||||||||||||||||||||||||||||||
| 6 | # and/or modify it under the terms of either: | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | # a) the GNU General Public License | ||||||||||||||||||||||||||||||||
| 9 | # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the | ||||||||||||||||||||||||||||||||
| 10 | # Free Software Foundation <http://www.fsf.org/>; either version 1 | ||||||||||||||||||||||||||||||||
| 11 | # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version | ||||||||||||||||||||||||||||||||
| 12 | # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or | ||||||||||||||||||||||||||||||||
| 13 | # b) the Artistic License 2.0 | ||||||||||||||||||||||||||||||||
| 14 | # <https://opensource.org/licenses/Artistic-2.0>, | ||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||
| 16 | # subject to the following additional term: No trademark rights to | ||||||||||||||||||||||||||||||||
| 17 | # "Paranoid" have been or are conveyed under any of the above licenses. | ||||||||||||||||||||||||||||||||
| 18 | # However, "Paranoid" may be used fairly to describe this unmodified | ||||||||||||||||||||||||||||||||
| 19 | # software, in good faith, but not as a trademark. | ||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||
| 21 | # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) | ||||||||||||||||||||||||||||||||
| 22 | # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com) | ||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||
| 24 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||
| 28 | # Environment definitions | ||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||
| 30 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | package Paranoid::Network::IPv6; | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | use Paranoid; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use Paranoid::Debug qw(:all); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use Paranoid::Network::Socket; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 4 | my @base = qw(ipv6NetConvert ipv6NetPacked ipv6NetIntersect); | |||||||||||||||||||||||||||||||
| 45 | 4 | my @constants = qw(MAXIPV6CIDR IPV6REGEX IPV6CIDRRGX IPV6BASE IPV6BRDCST | |||||||||||||||||||||||||||||||
| 46 | IPV6MASK); | ||||||||||||||||||||||||||||||||
| 47 | 4 | my @ipv6sort = qw(ipv6StrSort ipv6PackedSort ipv6NumSort); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 4 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||||||||||||||||||||||||
| 50 | 4 | @EXPORT = @base; | |||||||||||||||||||||||||||||||
| 51 | 4 | @EXPORT_OK = ( @base, @constants, @ipv6sort ); | |||||||||||||||||||||||||||||||
| 52 | 4 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||
| 53 | all => [@EXPORT_OK], | ||||||||||||||||||||||||||||||||
| 54 | base => [@base], | ||||||||||||||||||||||||||||||||
| 55 | constants => [@constants], | ||||||||||||||||||||||||||||||||
| 56 | ipv6Sort => [@ipv6sort], | ||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | use constant MAXIPV6CIDR => 128; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | use constant IPV6REGEX => qr/ | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 61 | :(?::[abcdef\d]{1,4}){1,7} | | ||||||||||||||||||||||||||||||||
| 62 | \b[abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} | | ||||||||||||||||||||||||||||||||
| 63 | (?:\b[abcdef\d]{1,4}:){1,7}: | ||||||||||||||||||||||||||||||||
| 64 | 4 | /six; | |||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | use constant IPV6CIDRRGX => | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 66 | 4 | qr#@{[ IPV6REGEX ]}/(?:1(?:[01]\d|2[0-8])|\d\d?)#s; | |||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | use constant IPV6BASE => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | use constant IPV6BRDCST => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | use constant IPV6MASK => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | use constant CHUNKMASK => 0xffffffff; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | use constant CHUNK => 32; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | use constant IPV6CHUNKS => 4; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | use constant IPV6LENGTH => 16; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 76 | # | ||||||||||||||||||||||||||||||||
| 77 | # Module code follows | ||||||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||||||
| 79 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 43 | ▶ | sub ipv6NetConvert { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6NetConvert: called CC=18 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | # Purpose: Takes a string representation of an IPv6 network | ||||||||||||||||||||||||||||||||
| 84 | # address and returns a list of lists containing | ||||||||||||||||||||||||||||||||
| 85 | # the binary network address, broadcast address, | ||||||||||||||||||||||||||||||||
| 86 | # and netmask, each broken into 32bit chunks. | ||||||||||||||||||||||||||||||||
| 87 | # Also allows for a plain IP being passed, in which | ||||||||||||||||||||||||||||||||
| 88 | # case it only returns the binary IP. | ||||||||||||||||||||||||||||||||
| 89 | # Returns: Array, empty on errors | ||||||||||||||||||||||||||||||||
| 90 | # Usage: @network = ipv6NetConvert($netAddr); | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 43 | my $netAddr = shift; | |||||||||||||||||||||||||||||||
| 93 | 43 | my ( $bnet, $bmask, $t, @tmp, @rv ); | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 43 | subPreamble( PDLEVEL1, '$', $netAddr ); | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 43 | ▶ | if ( has_ipv6() or $] >= 5.012 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
has_ipv6() or $] >= "5.012"
MC/DC0%0/2
has_ipv6() or $] >= "5.012"
has_ipv6()
$] >= "5.012"
Truth table33%1/3
A or B
A= has_ipv6()B= $] >= "5.012"
| |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | # Extract net address, mask | ||||||||||||||||||||||||||||||||
| 100 | 43 | ▶ | if ( defined $netAddr ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 101 | 42 | ($t) = | |||||||||||||||||||||||||||||||
| 102 | 42 | ( $netAddr =~ m#^(@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})$#s ) | |||||||||||||||||||||||||||||||
| 103 | [0]; | ||||||||||||||||||||||||||||||||
| 104 | 42 | ▶ | ( $bnet, $bmask ) = split m#/#s, $t if defined $t; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 43 | ▶ | if ( defined $bnet and length $bnet ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bnet and length $bnet
MC/DC50%1/2
defined $bnet and length $bnet
defined $bnet
length $bnet
Truth table66%2/3
A and B
A= defined $bnetB= length $bnet
| |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # First, convert $bnet to see if we have a valid IP address | ||||||||||||||||||||||||||||||||
| 110 | 32 | $bnet = [ unpack 'NNNN', inet_pton( AF_INET6(), $bnet ) ]; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 32 | ▶ | if ( defined $bnet and length $bnet ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $bnet and length $bnet
MC/DC0%0/2
defined $bnet and length $bnet
defined $bnet
length $bnet
Truth table33%1/3
A and B
A= defined $bnetB= length $bnet
| |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | # Save our network address | ||||||||||||||||||||||||||||||||
| 115 | 32 | push @rv, $bnet; | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 32 | ▶ | if ( defined $bmask and length $bmask ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bmask and length $bmask
MC/DC50%1/2
defined $bmask and length $bmask
defined $bmask
length $bmask
Truth table66%2/3
A and B
A= defined $bmaskB= length $bmask
| |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | # Convert netmask | ||||||||||||||||||||||||||||||||
| 120 | 14 | ▶ | if ( $bmask <= MAXIPV6CIDR ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # Add the mask in 32-bit chunks | ||||||||||||||||||||||||||||||||
| 123 | 14 | @tmp = (); | |||||||||||||||||||||||||||||||
| 124 | 14 | ▶ | while ( $bmask >= CHUNK ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 24 | push @tmp, CHUNKMASK; | |||||||||||||||||||||||||||||||
| 126 | 24 | $bmask -= CHUNK; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | # Push the final segment if there's a remainder | ||||||||||||||||||||||||||||||||
| 130 | 14 | ▶ | if ($bmask) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 4 | push @tmp, | |||||||||||||||||||||||||||||||
| 132 | CHUNKMASK - ( ( 2**( CHUNK - $bmask ) ) - 1 ); | ||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | # Add zero'd chunks to fill it out | ||||||||||||||||||||||||||||||||
| 136 | 14 | ▶ | while ( @tmp < IPV6CHUNKS ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 28 | push @tmp, 0x0; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | # Finally, save the chunks | ||||||||||||||||||||||||||||||||
| 141 | 14 | $bmask = [@tmp]; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||||||
| 144 | 0 | $bmask = undef; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 14 | ▶ | if ( defined $bmask ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | # Apply the mask to the base address | ||||||||||||||||||||||||||||||||
| 150 | 14 | foreach ( 0 .. ( IPV6CHUNKS - 1 ) ) { | |||||||||||||||||||||||||||||||
| 151 | 56 | $$bnet[$_] &= $$bmask[$_]; | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | # Calculate and save our broadcast address | ||||||||||||||||||||||||||||||||
| 155 | 14 | @tmp = (); | |||||||||||||||||||||||||||||||
| 156 | 14 | foreach ( 0 .. ( IPV6CHUNKS - 1 ) ) { | |||||||||||||||||||||||||||||||
| 157 | 56 | $tmp[$_] = | |||||||||||||||||||||||||||||||
| 158 | $$bnet[$_] | ( $$bmask[$_] ^ CHUNKMASK ); | ||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | 14 | push @rv, [@tmp]; | |||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | # Save our mask | ||||||||||||||||||||||||||||||||
| 163 | 14 | push @rv, $bmask; | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | } else { | ||||||||||||||||||||||||||||||||
| 166 | 0 | pdebug( 'invalid netmask passed', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||||||
| 171 | 0 | pdebug( 'failed to convert IPv6 address', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||||||
| 174 | 11 | pdebug( 'failed to extract an IPv6 address', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||||||
| 178 | 0 | pdebug( 'IPv6 support not present', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 43 | subPostamble( PDLEVEL1, '@', @rv ); | |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 43 | return @rv; | |||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | sub ipv6NetPacked { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6NetPacked: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | # Purpose: Wrapper script for ipv6NetConvert that repacks all of its | ||||||||||||||||||||||||||||||||
| 189 | # 32bit chunks into opaque strings in network-byte order. | ||||||||||||||||||||||||||||||||
| 190 | # Returns: Array | ||||||||||||||||||||||||||||||||
| 191 | # Usage: @network = ipv6NetPacked($netAddr); | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 1 | my $netAddr = shift; | |||||||||||||||||||||||||||||||
| 194 | 1 | my @rv; | |||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | 1 | subPreamble( PDLEVEL1, '$', $netAddr ); | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 1 | @rv = ipv6NetConvert($netAddr); | |||||||||||||||||||||||||||||||
| 199 | 1 | foreach (@rv) { | |||||||||||||||||||||||||||||||
| 200 | 1 | $_ = pack 'NNNN', @$_; | |||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | 1 | subPostamble( PDLEVEL1, '@', @rv ); | |||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 1 | return @rv; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 50 | ▶ | sub _cmpArrays { | ||||||||||||||||||||||||||||||
|
Subroutine
_cmpArrays: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | # Purpose: Compares IPv6 chunked address arrays | ||||||||||||||||||||||||||||||||
| 211 | # Returns: -1: net1 < net 2 | ||||||||||||||||||||||||||||||||
| 212 | # 0: net1 == net2 | ||||||||||||||||||||||||||||||||
| 213 | # 1: net1 > net2 | ||||||||||||||||||||||||||||||||
| 214 | # Usage: $rv = _cmpArrays( $aref1, $aref2 ); | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 50 | my $aref1 = shift; | |||||||||||||||||||||||||||||||
| 217 | 50 | my $aref2 = shift; | |||||||||||||||||||||||||||||||
| 218 | 50 | my $rv = 0; | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 50 | subPreamble( PDLEVEL2, '\@\@', $aref1, $aref2 ); | |||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 50 | ▶ | while ( scalar @$aref1 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | 67 | ▶ | unless ( $$aref1[0] == $$aref2[0] ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | 46 | ▶ | $rv = $$aref1[0] > $$aref2[0] ? 1 : -1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 46 | last; | |||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||
| 227 | 21 | shift @$aref1; | |||||||||||||||||||||||||||||||
| 228 | 21 | shift @$aref2; | |||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | 50 | subPostamble( PDLEVEL2, '$', $rv ); | |||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 50 | return $rv; | |||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 18 | ▶ | sub ipv6NetIntersect { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6NetIntersect: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | # Purpose: Tests whether network address ranges intersect | ||||||||||||||||||||||||||||||||
| 239 | # Returns: Integer, denoting whether an intersection exists, and what | ||||||||||||||||||||||||||||||||
| 240 | # kind: | ||||||||||||||||||||||||||||||||
| 241 | # | ||||||||||||||||||||||||||||||||
| 242 | # -1: destination range encompasses target range | ||||||||||||||||||||||||||||||||
| 243 | # 0: both ranges do not intersect at all | ||||||||||||||||||||||||||||||||
| 244 | # 1: target range encompasses destination range | ||||||||||||||||||||||||||||||||
| 245 | # | ||||||||||||||||||||||||||||||||
| 246 | # Usage: $rv = ipv6NetIntersect($net1, $net2); | ||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||
| 248 | 18 | my $tgt = shift; | |||||||||||||||||||||||||||||||
| 249 | 18 | my $dest = shift; | |||||||||||||||||||||||||||||||
| 250 | 18 | my $rv = 0; | |||||||||||||||||||||||||||||||
| 251 | 18 | my ( @tnet, @dnet ); | |||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 18 | subPreamble( PDLEVEL1, '$$', $tgt, $dest ); | |||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | # Bypas if one or both isn't defined -- obviously no intersection | ||||||||||||||||||||||||||||||||
| 256 | 18 | ▶ | unless ( !defined $tgt or !defined $dest ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $tgt and defined $dest
MC/DC0%0/2
defined $tgt and defined $dest
defined $tgt
defined $dest
Truth table33%1/3
A and B
A= defined $tgtB= defined $dest
| |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | # Treat any array references as IPv6 addresses already translated into | ||||||||||||||||||||||||||||||||
| 259 | # 32bit integer chunks | ||||||||||||||||||||||||||||||||
| 260 | 18 | ▶ | @tnet = ref($tgt) eq 'ARRAY' ? $tgt : ipv6NetConvert($tgt); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | 18 | ▶ | @dnet = ref($dest) eq 'ARRAY' ? $dest : ipv6NetConvert($dest); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | # insert bogus numbers for non IP-address info | ||||||||||||||||||||||||||||||||
| 264 | 18 | ▶ | @tnet = ( [ -1, 0, 0, 0 ] ) unless scalar @tnet; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | 18 | ▶ | @dnet = ( [ -2, 0, 0, 0 ] ) unless scalar @dnet; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | # Dummy up broadcast address for those single IPs passed (in lieu of | ||||||||||||||||||||||||||||||||
| 268 | # network ranges) | ||||||||||||||||||||||||||||||||
| 269 | 18 | ▶ | if ( $#tnet == 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 270 | 14 | $tnet[IPV6BRDCST] = $tnet[IPV6BASE]; | |||||||||||||||||||||||||||||||
| 271 | 14 | $tnet[IPV6MASK] = [ CHUNKMASK, CHUNKMASK, CHUNKMASK, CHUNKMASK ]; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | 18 | ▶ | if ( $#dnet == 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | 10 | $dnet[IPV6BRDCST] = $dnet[IPV6BASE]; | |||||||||||||||||||||||||||||||
| 275 | 10 | $dnet[IPV6MASK] = [ CHUNKMASK, CHUNKMASK, CHUNKMASK, CHUNKMASK ]; | |||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 18 | ▶ | if ( _cmpArrays( $tnet[IPV6BASE], $dnet[IPV6BASE] ) <= 0 | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
_cmpArrays($tnet[0], $dnet[0]) <= 0 and _cmpArrays($tnet[1], $dnet[1]) >= 0
MC/DC100%2/2
_cmpArrays($tnet[0], $dnet[0]) <= 0 and _cmpArrays($tnet[1], $dnet[1]) >= 0
_cmpArrays($tnet[0], $dnet[0]) <= 0
_cmpArrays($tnet[1], $dnet[1]) >= 0
Truth table100%3/3
A and B
A= _cmpArrays($tnet[0], $dnet[0]) <= 0B= _cmpArrays($tnet[1], $dnet[1]) >= 0
| |||||||||||||||||||||||||||||||||
| 279 | and _cmpArrays( $tnet[IPV6BRDCST], $dnet[IPV6BRDCST] ) >= 0 ) { | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | # Target fully encapsulates dest | ||||||||||||||||||||||||||||||||
| 282 | 4 | $rv = 1; | |||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | ▶ | } elsif ( _cmpArrays( $tnet[IPV6BASE], $dnet[IPV6BASE] ) >= 0 | |||||||||||||||||||||||||||||||
|
Condition100%3/3
_cmpArrays($tnet[0], $dnet[0]) >= 0 and _cmpArrays($tnet[1], $dnet[1]) <= 0
MC/DC100%2/2
_cmpArrays($tnet[0], $dnet[0]) >= 0 and _cmpArrays($tnet[1], $dnet[1]) <= 0
_cmpArrays($tnet[0], $dnet[0]) >= 0
_cmpArrays($tnet[1], $dnet[1]) <= 0
Truth table100%3/3
A and B
A= _cmpArrays($tnet[0], $dnet[0]) >= 0B= _cmpArrays($tnet[1], $dnet[1]) <= 0
| |||||||||||||||||||||||||||||||||
| 285 | and _cmpArrays( $tnet[IPV6BRDCST], $dnet[IPV6BRDCST] ) <= 0 ) { | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | # Dest fully encapsulates target | ||||||||||||||||||||||||||||||||
| 288 | 5 | $rv = -1; | |||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | 18 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 18 | return $rv; | |||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | { | ||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 4 | ▶ | sub ipv6NumSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6NumSort: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | # Purpose: Sorts IPv6 addresses represented in numeric form | ||||||||||||||||||||||||||||||||
| 305 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 306 | # Usage: @sorted = sort &ipv6NumSort @ipv4; | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 4 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 309 | 4 | my ( $i, $rv ); | |||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 4 | foreach $i ( 0 .. 3 ) { | |||||||||||||||||||||||||||||||
| 312 | 4 | $rv = $${"${pkg}::a"}[$i] <=> $${"${pkg}::b"}[$i]; | |||||||||||||||||||||||||||||||
| 313 | 4 | ▶ | last if $rv; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | 4 | return $rv; | |||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | 2 | ▶ | sub ipv6PackedSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6PackedSort: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | # Purpose: Sorts IPv6 addresses represented by packed strings | ||||||||||||||||||||||||||||||||
| 322 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 323 | # Usage: @sorted = sort &ipv6PackedSort @ipv6; | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 4 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | 2 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 328 | 2 | $a = [ unpack 'NNNN', ${"${pkg}::a"} ]; | |||||||||||||||||||||||||||||||
| 329 | 2 | $b = [ unpack 'NNNN', ${"${pkg}::b"} ]; | |||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 2 | return ipv6NumSort(); | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 2 | ▶ | sub ipv6StrSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv6StrSort: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | # Purpose: Sorts IPv6 addresses represented in string form | ||||||||||||||||||||||||||||||||
| 337 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 338 | # Usage: @sorted = sort &ipv4StrSort @ipv4; | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | 2 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 341 | 2 | my $a1 = ${"${pkg}::a"}; | |||||||||||||||||||||||||||||||
| 342 | 2 | my $b1 = ${"${pkg}::b"}; | |||||||||||||||||||||||||||||||
| 343 | 2 | my ( $i, $rv ); | |||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | 2 | $a1 =~ s#/.+##s; | |||||||||||||||||||||||||||||||
| 346 | 2 | $a1 = [ unpack 'NNNN', inet_pton( AF_INET6(), $a1 ) ]; | |||||||||||||||||||||||||||||||
| 347 | 2 | $b1 =~ s#/.+##s; | |||||||||||||||||||||||||||||||
| 348 | 2 | $b1 = [ unpack 'NNNN', inet_pton( AF_INET6(), $b1 ) ]; | |||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | 2 | foreach $i ( 0 .. 3 ) { | |||||||||||||||||||||||||||||||
| 351 | 2 | $rv = $$a1[$i] <=> $$b1[$i]; | |||||||||||||||||||||||||||||||
| 352 | 2 | ▶ | last if $rv; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | 2 | return $rv; | |||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | 4 | 1; | |||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | __END__ | ||||||||||||||||||||||||||||||||