Replace the deprecated module Net::Subnets with Net::CIDR::Lite.

This commit is contained in:
Colin Kuskie 2009-11-17 18:34:42 -08:00
parent 5df15c6307
commit fae8b162fa
4 changed files with 39 additions and 34 deletions

View file

@ -19,7 +19,7 @@ package WebGUI::Utility;
use Exporter;
use strict;
use Tie::IxHash;
use Net::Subnets;
use Net::CIDR::Lite;
our @ISA = qw(Exporter);
our @EXPORT = qw(&isBetween &makeTabSafe &makeArrayTabSafe &randomizeHash &commify &randomizeArray &isInSubnet
@ -176,7 +176,9 @@ sub isIn {
=head2 isInSubnet ( ipAddress, subnets )
Verifies whether an IP address is in a given subnet. Returns a 1 if it is, undef if there's a formatting error, or 0 if the IP is not in the list of subnets.
Verifies whether an IP address is in a given subnet. Returns a 1 if it
is, undef if there's a formatting error, or 0 if the IP is not in the
list of subnets.
=head3 ipAddress
@ -191,22 +193,21 @@ An array reference containing subnets in CIDR format. Example: 127.0.0.1/32
sub isInSubnet {
my $ip = shift;
my $subnets = shift;
# some validation
return 0 unless @{ $subnets };
for my $cidr ( @{ $subnets } ) {
my @parts = $cidr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)$/;
unless ( 5 == @parts ) { # cidr has 5 parts
return undef;
}
unless ( 4 == grep { $_ <= 255 } @parts[0..3] ) { # each octet needs to be between 0 and 255
return undef;
}
unless ( $parts[4] <= 32 ) { # the subnet needs to be less than or equal to 32, as 32 represents only 1 ip address
return undef;
}
my @parts = $cidr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)$/;
unless ( 5 == @parts ) { # cidr has 5 parts
return undef;
}
unless ( 4 == grep { $_ <= 255 } @parts[0..3] ) { # each octet needs to be between 0 and 255
return undef;
}
unless ( $parts[4] <= 32 ) { # the subnet needs to be less than or equal to 32, as 32 represents only 1 ip address
return undef;
}
}
my $net = Net::Subnets->new;
$net->subnets($subnets);
if ($net->check(\$ip)) {
my $net = Net::CIDR::Lite->new(@{ $subnets });
if ($net->find($ip)) {
return 1;
} else {
return 0;