/usr/share/perl5/Paranoid/Network/IPv4.pm is in libparanoid-perl 0.36-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | # Paranoid::Network::IPv4 -- IPv4-specific network functions
#
# (c) 2012, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: IPv4.pm,v 0.2 2012/09/24 22:47:01 acorliss Exp $
#
# This software is licensed under the same terms as Perl, itself.
# Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid::Network::IPv4;
use 5.006;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Paranoid::Debug qw(:all);
use Paranoid::Network::Socket;
use Carp;
my @base = qw(ipv4NetConvert ipv4NetIntersect);
my @constants = qw(MAXIPV4CIDR IPV4REGEX IPV4CIDRRGX IPV4BASE IPV4BRDCST
IPV4MASK);
my @ipv4sort = qw(ipv4NumSort ipv4StrSort ipv4PackedSort);
($VERSION) = ( q$Revision: 0.2 $ =~ /(\d+(?:\.(\d+))+)/sm );
@EXPORT = @base;
@EXPORT_OK = ( @base, @constants, @ipv4sort );
%EXPORT_TAGS = (
all => [@EXPORT_OK],
base => [@base],
constants => [@constants],
ipv4Sort => [@ipv4sort],
);
use constant MAXIPV4CIDR => 32;
use constant IPV4REGEX => qr/(?:\d{1,3}\.){3}\d{1,3}/sm;
use constant IPV4CIDRRGX =>
qr#(@{[ IPV4REGEX ]})(?:/(\d+|@{[ IPV4REGEX ]}))?#sm;
use constant FULLMASK => 0xffffffff;
use constant IPV4BASE => 0;
use constant IPV4BRDCST => 1;
use constant IPV4MASK => 2;
#####################################################################
#
# Module code follows
#
#####################################################################
sub ipv4NetConvert ($) {
# Purpose: Takes a string representation of an IPv4 network
# address and returns a list containing the binary
# network address, broadcast address, and netmask.
# Also allows for a plain IP being passed, in which
# case it only returns the binary IP.
# Returns: Array, empty on errors
# Usage: @network = ipv4NetConvert($netAddr);
my $netAddr = shift;
my $n = defined $netAddr ? $netAddr : 'undef';
my ( $bnet, $bmask, @rv );
pdebug( "entering w/$n", PDLEVEL1 );
pIn();
# Extract net address, mask
if ( defined $netAddr ) {
( $bnet, $bmask ) = ( $netAddr =~ m#^@{[ IPV4CIDRRGX ]}$#sm );
}
if ( defined $bnet and length $bnet ) {
# First, convert $bnet to see if we have a valid IP address
$bnet = unpack 'N', inet_aton($bnet);
if ( defined $bnet and length $bnet ) {
# Save our network address
push @rv, $bnet;
if ( defined $bmask and length $bmask ) {
# Convert netmask
$bmask =
$bmask !~ /^\d+$/sm ? unpack 'N', inet_aton($bmask)
: $bmask <= MAXIPV4CIDR
? FULLMASK - ( ( 2**( MAXIPV4CIDR - $bmask ) ) - 1 )
: undef;
if ( defined $bmask and length $bmask ) {
# Apply the mask to the base address
$rv[IPV4BASE] = $rv[IPV4BASE] & $bmask;
# Calculate and save our broadcast address
push @rv, $bnet | ( $bmask ^ FULLMASK );
# Save our mask
push @rv, $bmask;
} else {
pdebug( 'invalid netmask passed', PDLEVEL1 );
}
}
} else {
pdebug( 'failed to convert IPv4 address', PDLEVEL1 );
}
} else {
pdebug( 'failed to extract an IPv4 address', PDLEVEL1 );
}
pOut();
pdebug( "leaving w/rv: @rv", PDLEVEL1 );
return @rv;
}
sub ipv4NetIntersect (@) {
# Purpose: Tests whether network address ranges intersect
# Returns: Integer, denoting whether an intersection exists, and what
# kind:
#
# -1: destination range encompasses target range
# 0: both ranges do not intersect at all
# 1: target range encompasses destination range
#
# Usage: $rv = ipv4NetIntersect($net1, $net2);
my $tgt = shift;
my $dest = shift;
my $t = defined $tgt ? $tgt : 'undef';
my $d = defined $dest ? $dest : 'undef';
my $rv = 0;
my ( @tnet, @dnet );
pdebug( "entering w/$t, $d", PDLEVEL1 );
pIn();
# Bypas if one or both isn't defined -- obviously no intersection
unless ( !defined $tgt or !defined $dest ) {
# Convert addresses (also allows for raw IPs (32bit integers) to be
# passed)
@tnet = $tgt =~ /^\d+$/sm ? ($tgt) : ipv4NetConvert($tgt);
@dnet = $dest =~ /^\d+$/sm ? ($dest) : ipv4NetConvert($dest);
# insert bogus numbers for non IP-address info
@tnet = (-1) unless scalar @tnet;
@dnet = (-2) unless scalar @dnet;
# Dummy up broadcast address for those single IPs passed (in lieu of
# network ranges)
$tnet[IPV4BRDCST] = $tnet[IPV4BASE] if $#tnet == 0;
$dnet[IPV4BRDCST] = $dnet[IPV4BASE] if $#dnet == 0;
if ( $tnet[IPV4BASE] <= $dnet[IPV4BASE]
and $tnet[IPV4BRDCST] >= $dnet[IPV4BRDCST] ) {
# Target fully encapsulates dest
$rv = 1;
} elsif ( $tnet[IPV4BASE] >= $dnet[IPV4BASE]
and $tnet[IPV4BRDCST] <= $dnet[IPV4BRDCST] ) {
# Dest fully encapsulates target
$rv = -1;
}
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
{
no strict 'refs';
sub ipv4NumSort {
# Purpose: Sorts IPv4 addresses represented in numeric form
# Returns: -1, 0, 1
# Usage: @sorted = sort &ipv4NumSort @ipv4;
my ($pkg) = caller;
return ${"${pkg}::a"} <=> ${"${pkg}::b"};
}
sub ipv4PackedSort {
# Purpose: Sorts IPv4 addresses represented in packed strings
# Returns: -1, 0, 1
# Usage: @sorted = sort &ipv4PackedSort @ipv4;
my ($pkg) = caller;
my $a1 = unpack 'N', ${"${pkg}::a"};
my $b1 = unpack 'N', ${"${pkg}::b"};
return $a1 <=> $b1;
}
sub ipv4StrSort {
# Purpose: Sorts IPv4 addresses represented in string form
# Returns: -1, 0, 1
# Usage: @sorted = sort &ipv4StrSort @ipv4;
my ($pkg) = caller;
my $a1 = ${"${pkg}::a"};
my $b1 = ${"${pkg}::b"};
$a1 =~ s#/.+##sm;
$a1 = unpack 'N', inet_aton($a1);
$b1 =~ s#/.+##sm;
$b1 = unpack 'N', inet_aton($b1);
return $a1 <=> $b1;
}
}
1;
__END__
=head1 NAME
Paranoid::Network::IPv4 - IPv4-related functions
=head1 VERSION
$Id: IPv4.pm,v 0.2 2012/09/24 22:47:01 acorliss Exp $
=head1 SYNOPSIS
use Paranoid::Network::IPv4;
@net = ipv4NetConvert($netAddr);
$rv = ipv4NetIntersect($net1, $net2);
or
use Paranoid::Network::IPv4 qw(:all);
print "Valid IP address\n" if $netAddr =~ /^@{[ IPV4REGEX ]}$/;
@net = ipv4NetConvert($netAddr);
$broadcast = $net[IPV4BRDCST];
use Paranoid::Network::IPv4 qw(:ipv4Sort);
@nets = sort ipv4StrSort @nets;
@nets = sort ipv4PackedSort @nets;
@nets = sort ipv4NumSort @nets;
=head1 DESCRIPTION
This module contains a few convenience functions for working with IPv4
addresses.
By default only B<ipv4NetConvert> and B<ipv4NetIntersect> are imported.
Other symbol sets are:
Name Description
---------------------------------------------
all all functions/constants
base default exported functions
constants constants
ipv4Sort sort functions
=head1 SUBROUTINES/METHODS
=head2 ipv4NetConvert
@net = ipv4NetConvert($netAddr);
This function takes an IPv4 network address in string format and converts it
into an array containing the base network address, the broadcast address, and
the netmask, in integer format. The network address can have the netmask in
either CIDR format or dotted quads.
In the case of a single IP address, the array with only have one element, that
of the IP in integer format.
Passing any argument to this function that is not a string representation of
an IP address (including undef values) will cause this function to return an
empty array.
=head2 ipv4NetIntersect
$rv = ipv4NetIntersect($net1, $net2);
This function tests whether an IP or subnet intersects with another IP or
subnet. The return value is essentially boolean, but the true value can vary
to indicate which is a subset of the other:
-1: destination range encompasses target range
0: both ranges do not intersect at all
1: target range encompasses destination range
The function handles the same string formats as B<ipv4NetConvert>, but will
allow you to test single IPs in integer format as well.
=head2 ipv4StrSort
@sorted = sort ipv4StrSort @nets;
This function allows IPv4 addresses and networks to be passed in string
format. Networks can be in CIDR format. Sorts in ascending order.
=head2 ipv4PackedSort
@sorted = sort ipv4PackedSort @nets;
This function sorts IPv4 addresses as returned by L<inet_aton>. Sorts in
ascending order.
=head2 ipv4NumSort
@sorted = sort ipv4NumSort @nets;
This function is rather pointless, but is included merely for completeness.
Addresses are in unpacked, native integer format, such as one gets from:
$ip = unpack 'N', inet_aton($ipAddr);
Sorts in ascending order.
=head1 CONSTANTS
These are only imported if explicity requested or with the B<:all> tag.
=head2 MAXIPV4CIDR
Simply put: 32. This is the largest CIDR notation supported in IPv4.
=head2 IPV4REGEX
Regular expression: B<qr/(?:\d{1,3}\.){3}\d{1,3}/sm>
You can use this for validating IP addresses as such:
$ip =~ m#^@{[ IPV4REGEX ]}$#;
or to extract potential IPs from extraneous text:
(@ips) = ( $string =~ m#(@{[ IPV4REGEX ]})#gsm);
=head2 IPV4CIDRRGX
Regular expression:
qr#(@{[ IPV4REGEX ]})(?:/(\d+|@{[ IPV4REGEX ]}))?#sm
By default this will extract an IP or CIDR notation network address:
($net, $mask) = ( $ip =~ m#^@{[ IPV4CIDRRGX ]}$# );
In the case of a simple IP address B<$mask> will be undefined.
=head2 IPV4BASE
This is the ordinal index of the base network address as returned by
B<ipv4NetConvert>.
=head2 IPV4BRDCST
This is the ordinal index of the broadcast address as returned by
B<ipv4NetConvert>.
=head2 IPV4MASK
This is the ordinal index of the network mask as returned by
B<ipv4NetConvert>.
=head1 DEPENDENCIES
=over
=item o
L<Paranoid>
=item o
L<Paranoid::Network::Socket>
=back
=head1 BUGS AND LIMITATIONS
=head1 AUTHOR
Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl, itself.
Please see http://dev.perl.org/licenses/ for more information.
(c) 2012, Arthur Corliss (corliss@digitalmages.com)
|