diff --git a/IP.pm b/IP.pm index 1119f9e..8a845df 100644 --- a/IP.pm +++ b/IP.pm @@ -139,7 +139,7 @@ ############################################# -our $VERSION = '3.07'; +our $VERSION = '3.08'; # Preloaded methods go here. @@ -258,6 +258,19 @@ vec($bmask, 3, 8) = 0; } elsif ($mask =~ m/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { + + for my $i ($1, $2, $3, $4) { + return undef + unless grep { $i == $_ } + (255, 254, 252, 248, 224, 192, 160, 128, 0); + } + + return undef if ($1 < $2 or $2 < $3 or $3 < $4); + + return undef if $2 != 0 and $1 != 255; + return undef if $3 != 0 and $2 != 255; + return undef if $4 != 0 and $3 != 255; + vec($bmask, 0, 8) = $1; vec($bmask, 1, 8) = $2; vec($bmask, 2, 8) = $3; @@ -485,13 +498,16 @@ if (defined $_[2]) { $mask = _parse_mask $_[2], 32; + return undef unless defined $mask; } elsif (defined $mask) { $mask = _parse_mask $mask, 32; + return undef unless defined $mask; } else { $hasmask = 0; $mask = _parse_mask 32, 32; + return undef unless defined $mask; } my $self = _v4($ip, $mask, $hasmask); @@ -850,6 +866,7 @@ } 1; + __END__ =head1 NAME @@ -1435,6 +1452,17 @@ =back +=item 3.08 + +=over + +=item * + +By popular request, C<-Enew()> now checks the sanity of the netmasks +it receives. If the netmask is invalid, C will be returned. + +=back + =back =head1 AUTHOR diff --git a/MANIFEST b/MANIFEST index 4c344b8..594627c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -16,6 +16,7 @@ t/v4-cidr.t t/over-arr.t t/v4-range.t +t/v4-badnm.t t/v4-basem.t t/v4-first.t t/wildcard.t diff --git a/README b/README index 2bbb5f2..fc5f4ef 100644 --- a/README +++ b/README @@ -1,5 +1,7 @@ NetAddr::IP - Manages IPv4 (your traditional IP) addresses and subnets + * * * * THIS MODULE REQUIRES PERL 5.6.0 OR NEWER. * * * * + This module is designed as a help for managing (ranges of) IP addresses. It includes efficient implementations for most common tasks done to subnets or ranges of IP addresses, namely verifying if an diff --git a/t/v4-badnm.t b/t/v4-badnm.t new file mode 100644 index 0000000..a69da61 --- /dev/null +++ b/t/v4-badnm.t @@ -0,0 +1,56 @@ +# I know this does not look like -*- perl -*-, but I swear it is... + +use NetAddr::IP; +use strict; + +$| = 1; + +our @badnets = ( + '10.10.10.10/255.255.0.255', + '10.10.10.10/255.0.255.255', + '10.10.10.10/0.255.255.255', + '10.10.10.10/128.255.0.255', + '10.10.10.10/255.128.0.255', + '10.10.10.10/255.255.255.129', + '10.10.10.10/255.255.129.0', + '10.10.10.10/255.255.255.130', + '10.10.10.10/255.255.130.0', + '10.10.10.10/255.0.0.1', + '10.10.10.10/255.129.0.1', + '10.10.10.10/0.255.0.255', + ); + +our @goodnets = (); + +push @goodnets, "10.0.0.1/$_" for (0 .. 32); +push @goodnets, "10.0.0.1/255.255.255.255"; + +print '1..', (scalar @badnets + scalar @goodnets) , "\n"; + +my $count = 1; + +for my $bad (@badnets) { + + if (defined NetAddr::IP->new($bad)) { + print "not ok $count # $bad should fail but succeeded\n"; + } + else { + print "ok $count # $bad must fail\n"; + } + + ++ $count; +} + +for my $good (@goodnets) { + + if (defined NetAddr::IP->new($good)) { + print "ok $count # $good should not fail\n"; + } + else { + print "not ok $count # $good must not fail\n"; + } + + ++ $count; +} + +