/usr/share/perl5/Paranoid/Network.pm is in libparanoid-perl 2.04-2.
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | # Paranoid::Network -- Network functions for paranoid programs
#
# (c) 2005 - 2015, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: lib/Paranoid/Network.pm, 2.04 2016/09/19 15:00:25 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;
use 5.008;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Paranoid;
use Paranoid::Debug qw(:all);
use Paranoid::Network::Socket;
use Paranoid::Network::IPv4 qw(:all);
use Paranoid::Network::IPv6 qw(:all);
($VERSION) = ( q$Revision: 2.04 $ =~ /(\d+(?:\.\d+)+)/sm );
@EXPORT = qw(ipInNetworks hostInDomains extractIPs netIntersect);
@EXPORT_OK = ( @EXPORT, qw(NETMATCH HOSTNAME_REGEX) );
%EXPORT_TAGS = ( all => [@EXPORT_OK], );
use constant HOSTNAME_REGEX =>
qr#(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?#s;
#####################################################################
#
# Module code follows
#
#####################################################################
{
my $lmatch;
sub NETMATCH : lvalue {
$lmatch;
}
}
sub ipInNetworks {
# Purpose: Checks to see if the IP occurs in the passed list of IPs and
# networks
# Returns: True (1) if the IP occurs, False (0) otherwise
# Usage: $rv = ipInNetworks($ip, @networks);
my $ip = shift;
my @networks = grep {defined} @_;
my $rv = 0;
my ( $family, @tmp );
pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $ip, @networks );
pIn();
NETMATCH = undef;
# Validate arguments
if ( defined $ip ) {
# Extract IPv4 address from IPv6 encoding
$ip =~ s/^::ffff:(@{[ IPV4REGEX ]})$/$1/sio;
# Check for IPv6 support
if ( has_ipv6() or $] >= 5.012 ) {
pdebug( 'Found IPv4/IPv6 support', PDLEVEL2 );
$family =
$ip =~ m/^@{[ IPV4REGEX ]}$/so ? AF_INET()
: $ip =~ m/^@{[ IPV6REGEX ]}$/so ? AF_INET6()
: undef;
} else {
pdebug( 'Found only IPv4 support', PDLEVEL2 );
$family = AF_INET()
if $ip =~ m/^@{[ IPV4REGEX ]}$/so;
}
}
if ( defined $ip and defined $family ) {
# Filter out non-family data from @networks
@networks = grep {
$family == AF_INET()
? m#^(?:@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$#so
: m#^(?:@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})$#so
} @networks;
pdebug( 'networks to compare: %s', PDLEVEL2, @networks );
# Start comparisons
foreach (@networks) {
if ($family == AF_INET()
? ipv4NetIntersect( $ip, $_ )
: ipv6NetIntersect( $ip, $_ )
) {
NETMATCH = $_;
$rv = 1;
last;
}
}
}
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
return $rv;
}
sub hostInDomains {
# Purpose: Checks to see if the host occurs in the list of domains
# Returns: True (1) if the host occurs, False (0) otherwise
# Usage: $rv = hostInDomains($hostname, @domains);
my $host = shift;
my @domains = @_;
my $rv = 0;
my $domain;
pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $host, @domains );
pIn();
NETMATCH = undef;
if ( defined $host and $host =~ /^@{[ HOSTNAME_REGEX ]}$/so ) {
# Filter out non-domains
@domains =
grep { defined $_ && m/^@{[ HOSTNAME_REGEX ]}$/so } @domains;
# Start the comparison
if (@domains) {
foreach $domain (@domains) {
if ( $host =~ /^(?:[\w\-]+\.)*\Q$domain\E$/si ) {
NETMATCH = $domain;
$rv = 1;
last;
}
}
}
}
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
return $rv;
}
sub extractIPs {
# Purpose: Extracts IPv4/IPv6 addresses from arbitrary text.
# Returns: List containing extracted IP addresses
# Usage: @ips = extractIPs($string1, $string2);
my @strings = @_;
my ( $string, @ips, $ip, @tmp, @rv );
pdebug( 'entering w/%d strings', PDLEVEL1, scalar @strings );
pIn();
foreach $string (@strings) {
next unless defined $string;
# Look for IPv4 addresses
@ips = ( $string =~ /(@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})/sog );
# Validate them by filtering through inet_aton
foreach $ip (@ips) {
@tmp = split m#/#s, $ip;
push @rv, $ip if defined inet_aton( $tmp[0] );
}
# If Socket6 is present or we have Perl 5.14 or higher we'll check
# for IPv6 addresses
if ( has_ipv6() or $] >= 5.012 ) {
@ips = ( $string =~
m/(@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})/sogix );
# Filter out addresses with more than one ::
@ips = grep { scalar(m/(::)/sg) <= 1 } @ips;
# Validate remaining addresses with inet_pton
foreach $ip (@ips) {
@tmp = split m#/#s, $ip;
push @rv, $ip
if defined inet_pton( AF_INET6(), $tmp[0] );
}
}
}
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, @rv );
return @rv;
}
sub netIntersect {
# 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 = netIntersect( $cidr1, $cidr2 );
my $target = shift;
my $dest = shift;
my $rv = 0;
pdebug( 'entering w/(%s)(%s)', PDLEVEL1, $target, $dest );
pIn();
if ( defined $target and defined $dest ) {
if ( $target =~ m/^(?:@{[ IPV4CIDRRGX ]}|@{[ IPV4REGEX ]})$/s ) {
$rv = ipv4NetIntersect( $target, $dest );
} elsif ( $target =~ m/^(?:@{[ IPV6CIDRRGX ]}|@{[ IPV6REGEX ]})$/si )
{
$rv = ipv6NetIntersect( $target, $dest )
if has_ipv6()
or $] >= 5.012;
} else {
pdebug(
'target string (%s) doesn\'t seem to match '
. 'an IP/network address',
PDLEVEL1, $target
);
}
} else {
pdebug( 'one or both arguments are not defined', PDLEVEL1 );
}
pOut();
pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
return $rv;
}
1;
__END__
=head1 NAME
Paranoid::Network - Network functions for paranoid programs
=head1 VERSION
$Id: lib/Paranoid/Network.pm, 2.04 2016/09/19 15:00:25 acorliss Exp $
=head1 SYNOPSIS
use Paranoid::Network;
$rv = ipInNetworks($ip, @networks);
$rv = hostInDomains($host, @domains);
$cidr = Paranoid::Network::NETMATCH();
@ips = extractIPs($string1);
@ips = extractIPs(@lines);
$rv = netIntersect( $cidr1, $cidr2 );
=head1 DESCRIPTION
This modules contains functions that may be useful for working with network
data. It attempts to be IPv4/IPv6 agnostic, assuming IPv6 support is present.
Due to the gradual introduction of IPv6 support into Perl there may be
caveats. Please consult L<Paranoid::Network::Socket> for more details.
I<NETMATCH> and I<HOSTNAME_REGEX> are not exported by default.
=head1 SUBROUTINES/METHODS
=head2 ipInNetworks
$rv = ipInNetworks($ip, @networks);
This function checks the passed IP (in string format) against each of the
networks or IPs in the list and returns true if there's a match. The list of
networks can be either individual IP address or network addresses in CIDR
notation or with full netmasks:
@networks = qw(127.0.0.1
192.168.0.0/24
172.16.12.0/255.255.240.0);
You can safely comingle IPv4 & IPv6 addresses in the list to check against.
Addresses not belonging to the same address family as the IP being tested will
be ignored.
B<NOTE:> IPv4 addresses encoded as IPv6 addresses, e.g.:
::ffff:192.168.0.5
are supported, however an IP address submitted in this format as the IP to
test for will be converted to a pure IPv4 address and compared only against
the IPv4 networks. This is meant as a convenience to the developer supporting
dual-stack systems to avoid having to list IPv4 networks in the array twice
like so:
::ffff:192.168.0.0/120, 192.168.0.0/24
Just list IPv4 as IPv4, IPv6 as IPv6, and this routine will convert
IPv6-encoded IPv4 addresses automatically. This would make the following test
return a true value:
ipInNetworks( '::ffff:192.168.0.5', '192.168.0.0/24' );
but
ipInNetworks( '::ffff:192.168.0.5', '::ffff:192.168.0.0/120' );
return a false value. This may seem counter intuitive, but it simplifies
things in (my alternate) reality.
Please note that this automatic conversion only applies to the B<IP> argument,
not to any member of the network array.
=head2 hostInDomains
$rv = hostInDomains($host, @domains);
This function checks the passed hostname (fully qualified) against each
of the domains in the list and returns true if there's a match. None of the
domains should have the preceding '.' (i.e., 'foo.com' rather than
'.foo.com').
=head2 NETMATCH
$cidr = Paranoid::Network::NETMATCH();
This stores the IP, network address, or domain that matched in
I<ipInNetworks> or I<hostInDomains>. This returns B<undef> if any
function call fails to make a match.
=head2 HOSTNAME_REGEX
$rv = $hostname =~ /^@{[ HOSTNAME_REGEX ]}$/so;
This constant is just a regex meant to be a basic sanity check for appropriate
hostnames. It is probably overly strict in accordance with outdated RFCs.
=head2 extractIPs
@ips = extractIPs($string1);
@ips = extractIPs(@lines);
This function extracts IPv4/IPv6 addresses from arbitrary text. IPv6 support
is contingent upon the presence of proper support (please see
L<Paranoid::Network::Socket> for more details).
This extracts only IP addresses, not network addresses in CIDR or dotted octet
notation. In the case of the latter the netmask will be extracted as an
additional address.
B<NOTE:> in the interest of performance this function does only rough regex
extraction of IP-looking candidates, then runs them through B<inet_aton> (for
IPv4) and B<inet_pton> (for IPv6) to see if they successfully convert. Even
with the overhead of B<Paranoid> (with debugging and I<loadModule> calls for
Socket6 and what-not) it seems that this is an order of a magnitude faster
than doing a pure regex extraction & validation of IPv6 addresses.
B<NOTE:> Like the B<ipInNetworks> function we filter out IPv4 addresses encoded
as IPv6 addresses since that address is already returned as a pure IPv4
address.
=head2 netIntersect
$rv = netIntersect( $cidr1, $cidr2 );
This function is an IPv4/IPv6 agnostic wrapper for the B<ipv{4,6}NetIntersect>
functions provided by L<Paranoid::Network::IPv{4,6}> modules. The return
value from which ever function called is passed on directly. Passing this
function non-IP or undefined values simply returns a zero.
=head1 DEPENDENCIES
=over
=item o
L<Paranoid>
=item o
L<Paranoid::Debug>
=item o
L<Paranoid::Network::Socket>
=item o
L<Paranoid::Network::IPv4>
=item o
L<Paranoid::Network::IPv6>
=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) 2005 - 2015, Arthur Corliss (corliss@digitalmages.com)
|