« Paranoid/IO/FileMultiplexer/Block/BATHeader.pm
↑ Summary
Paranoid/IO/FileMultiplexer/Block/FileHeader.pm »
| 1 | # Paranoid::Network::IPv4 -- IPv4-specific network functions | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # $Id: lib/Paranoid/Network/IPv4.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::IPv4; | ||||||||||||||||||||||||||||||||
| 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(ipv4NetConvert ipv4NetIntersect); | |||||||||||||||||||||||||||||||
| 45 | 4 | my @constants = qw(MAXIPV4CIDR IPV4REGEX IPV4CIDRRGX IPV4BASE IPV4BRDCST | |||||||||||||||||||||||||||||||
| 46 | IPV4MASK); | ||||||||||||||||||||||||||||||||
| 47 | 4 | my @ipv4sort = qw(ipv4NumSort ipv4StrSort ipv4PackedSort); | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 4 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||||||||||||||||||||||||
| 50 | 4 | @EXPORT = @base; | |||||||||||||||||||||||||||||||
| 51 | 4 | @EXPORT_OK = ( @base, @constants, @ipv4sort ); | |||||||||||||||||||||||||||||||
| 52 | 4 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||
| 53 | all => [@EXPORT_OK], | ||||||||||||||||||||||||||||||||
| 54 | base => [@base], | ||||||||||||||||||||||||||||||||
| 55 | constants => [@constants], | ||||||||||||||||||||||||||||||||
| 56 | ipv4Sort => [@ipv4sort], | ||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | use constant MAXIPV4CIDR => 32; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | use constant IPV4REGEX => | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 61 | 4 | qr/(?:(?:25[0-5]|2[0-4][0-9]|1?\d\d?)\.){3}(?:25[0-5]|2[0-4][0-9]|1?\d\d?)/s; | |||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | use constant IPV4CIDRRGX => | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 63 | 4 | qr#@{[ IPV4REGEX ]}/(?:(?:3[0-2]|[12]?\d)|@{[ IPV4REGEX ]})#s; | |||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | use constant FULLMASK => 0xffffffff; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | use constant IPV4BASE => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | use constant IPV4BRDCST => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | use constant IPV4MASK => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||||||
| 71 | # Module code follows | ||||||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||||||
| 73 | ##################################################################### | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 41 | ▶ | sub ipv4NetConvert { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv4NetConvert: called CC=13 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | # Purpose: Takes a string representation of an IPv4 network | ||||||||||||||||||||||||||||||||
| 78 | # address and returns a list containing the binary | ||||||||||||||||||||||||||||||||
| 79 | # network address, broadcast address, and netmask. | ||||||||||||||||||||||||||||||||
| 80 | # Also allows for a plain IP being passed, in which | ||||||||||||||||||||||||||||||||
| 81 | # case it only returns the binary IP. | ||||||||||||||||||||||||||||||||
| 82 | # Returns: Array, empty on errors | ||||||||||||||||||||||||||||||||
| 83 | # Usage: @network = ipv4NetConvert($netAddr); | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 41 | my $netAddr = shift; | |||||||||||||||||||||||||||||||
| 86 | 41 | my ( $bnet, $bmask, $t, @rv ); | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 41 | subPreamble( PDLEVEL1, '$', $netAddr ); | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | # Extract net address, mask | ||||||||||||||||||||||||||||||||
| 91 | 41 | ▶ | if ( defined $netAddr ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 40 | ($t) = ( $netAddr =~ m#^(@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$#s )[0]; | |||||||||||||||||||||||||||||||
| 93 | 40 | ▶ | ( $bnet, $bmask ) = split m#/#s, $t if defined $t; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 41 | ▶ | 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
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | # First, convert $bnet to see if we have a valid IP address | ||||||||||||||||||||||||||||||||
| 99 | 33 | $bnet = unpack 'N', inet_aton($bnet); | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 33 | ▶ | 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
| |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # Save our network address | ||||||||||||||||||||||||||||||||
| 104 | 33 | push @rv, $bnet; | |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 33 | ▶ | 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
| |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | # Convert netmask | ||||||||||||||||||||||||||||||||
| 109 | 24 | ▶ | $bmask = | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 110 | $bmask !~ /^\d+$/s ? unpack 'N', inet_aton($bmask) | ||||||||||||||||||||||||||||||||
| 111 | : $bmask <= MAXIPV4CIDR | ||||||||||||||||||||||||||||||||
| 112 | ? FULLMASK - ( ( 2**( MAXIPV4CIDR - $bmask ) ) - 1 ) | ||||||||||||||||||||||||||||||||
| 113 | : undef; | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 24 | ▶ | if ( defined $bmask and length $bmask ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $bmask and length $bmask
MC/DC0%0/2
defined $bmask and length $bmask
defined $bmask
length $bmask
Truth table33%1/3
A and B
A= defined $bmaskB= length $bmask
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # Apply the mask to the base address | ||||||||||||||||||||||||||||||||
| 118 | 24 | $rv[IPV4BASE] = $rv[IPV4BASE] & $bmask; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # Calculate and save our broadcast address | ||||||||||||||||||||||||||||||||
| 121 | 24 | push @rv, $bnet | ( $bmask ^ FULLMASK ); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | # Save our mask | ||||||||||||||||||||||||||||||||
| 124 | 24 | push @rv, $bmask; | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||
| 127 | 0 | pdebug( 'invalid netmask passed', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | } else { | ||||||||||||||||||||||||||||||||
| 131 | 0 | pdebug( 'failed to convert IPv4 address', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||||||||||||||
| 134 | 8 | pdebug( 'failed to extract an IPv4 address', PDLEVEL1 ); | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 41 | subPostamble( PDLEVEL1, '@', @rv ); | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 41 | return @rv; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 18 | ▶ | sub ipv4NetIntersect { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv4NetIntersect: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | # Purpose: Tests whether network address ranges intersect | ||||||||||||||||||||||||||||||||
| 145 | # Returns: Integer, denoting whether an intersection exists, and what | ||||||||||||||||||||||||||||||||
| 146 | # kind: | ||||||||||||||||||||||||||||||||
| 147 | # | ||||||||||||||||||||||||||||||||
| 148 | # -1: destination range encompasses target range | ||||||||||||||||||||||||||||||||
| 149 | # 0: both ranges do not intersect at all | ||||||||||||||||||||||||||||||||
| 150 | # 1: target range encompasses destination range | ||||||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||||||
| 152 | # Usage: $rv = ipv4NetIntersect($net1, $net2); | ||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 18 | my $tgt = shift; | |||||||||||||||||||||||||||||||
| 155 | 18 | my $dest = shift; | |||||||||||||||||||||||||||||||
| 156 | 18 | my $rv = 0; | |||||||||||||||||||||||||||||||
| 157 | 18 | my ( @tnet, @dnet ); | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 18 | subPreamble( PDLEVEL1, '$$', $tgt, $dest ); | |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | # Bypas if one or both isn't defined -- obviously no intersection | ||||||||||||||||||||||||||||||||
| 162 | 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
| |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | # Convert addresses (also allows for raw IPs (32bit integers) to be | ||||||||||||||||||||||||||||||||
| 165 | # passed) | ||||||||||||||||||||||||||||||||
| 166 | 18 | ▶ | @tnet = $tgt =~ /^\d+$/s ? ($tgt) : ipv4NetConvert($tgt); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 18 | ▶ | @dnet = $dest =~ /^\d+$/s ? ($dest) : ipv4NetConvert($dest); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | # insert bogus numbers for non IP-address info | ||||||||||||||||||||||||||||||||
| 170 | 18 | ▶ | @tnet = (-1) unless scalar @tnet; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 171 | 18 | ▶ | @dnet = (-2) unless scalar @dnet; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | # Dummy up broadcast address for those single IPs passed (in lieu of | ||||||||||||||||||||||||||||||||
| 174 | # network ranges) | ||||||||||||||||||||||||||||||||
| 175 | 18 | ▶ | $tnet[IPV4BRDCST] = $tnet[IPV4BASE] if $#tnet == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 176 | 18 | ▶ | $dnet[IPV4BRDCST] = $dnet[IPV4BASE] if $#dnet == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 18 | ▶ | if ( $tnet[IPV4BASE] <= $dnet[IPV4BASE] | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$tnet[0] <= $dnet[0] and $tnet[1] >= $dnet[1]
MC/DC100%2/2
$tnet[0] <= $dnet[0] and $tnet[1] >= $dnet[1]
$tnet[0] <= $dnet[0]
$tnet[1] >= $dnet[1]
Truth table100%3/3
A and B
A= $tnet[0] <= $dnet[0]B= $tnet[1] >= $dnet[1]
| |||||||||||||||||||||||||||||||||
| 179 | and $tnet[IPV4BRDCST] >= $dnet[IPV4BRDCST] ) { | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | # Target fully encapsulates dest | ||||||||||||||||||||||||||||||||
| 182 | 4 | $rv = 1; | |||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | ▶ | } elsif ( $tnet[IPV4BASE] >= $dnet[IPV4BASE] | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$tnet[0] >= $dnet[0] and $tnet[1] <= $dnet[1]
MC/DC100%2/2
$tnet[0] >= $dnet[0] and $tnet[1] <= $dnet[1]
$tnet[0] >= $dnet[0]
$tnet[1] <= $dnet[1]
Truth table100%3/3
A and B
A= $tnet[0] >= $dnet[0]B= $tnet[1] <= $dnet[1]
| |||||||||||||||||||||||||||||||||
| 185 | and $tnet[IPV4BRDCST] <= $dnet[IPV4BRDCST] ) { | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | # Dest fully encapsulates target | ||||||||||||||||||||||||||||||||
| 188 | 7 | $rv = -1; | |||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 18 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 18 | return $rv; | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 2 | ▶ | sub ipv4NumSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv4NumSort: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | # Purpose: Sorts IPv4 addresses represented in numeric form | ||||||||||||||||||||||||||||||||
| 205 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 206 | # Usage: @sorted = sort &ipv4NumSort @ipv4; | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 2 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 2 | return ${"${pkg}::a"} <=> ${"${pkg}::b"}; | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 2 | ▶ | sub ipv4PackedSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv4PackedSort: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | # Purpose: Sorts IPv4 addresses represented in packed strings | ||||||||||||||||||||||||||||||||
| 216 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 217 | # Usage: @sorted = sort &ipv4PackedSort @ipv4; | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 2 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | 2 | my $a1 = unpack 'N', ${"${pkg}::a"}; | |||||||||||||||||||||||||||||||
| 222 | 2 | my $b1 = unpack 'N', ${"${pkg}::b"}; | |||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 2 | return $a1 <=> $b1; | |||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 2 | ▶ | sub ipv4StrSort { | ||||||||||||||||||||||||||||||
|
Subroutine
ipv4StrSort: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # Purpose: Sorts IPv4 addresses represented in string form | ||||||||||||||||||||||||||||||||
| 230 | # Returns: -1, 0, 1 | ||||||||||||||||||||||||||||||||
| 231 | # Usage: @sorted = sort &ipv4StrSort @ipv4; | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 2 | my ($pkg) = caller; | |||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 2 | my $a1 = ${"${pkg}::a"}; | |||||||||||||||||||||||||||||||
| 236 | 2 | my $b1 = ${"${pkg}::b"}; | |||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 2 | $a1 =~ s#/.+##s; | |||||||||||||||||||||||||||||||
| 239 | 2 | $a1 = unpack 'N', inet_aton($a1); | |||||||||||||||||||||||||||||||
| 240 | 2 | $b1 =~ s#/.+##s; | |||||||||||||||||||||||||||||||
| 241 | 2 | $b1 = unpack 'N', inet_aton($b1); | |||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 2 | return $a1 <=> $b1; | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 4 | 1; | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | __END__ | ||||||||||||||||||||||||||||||||