diff --git a/IP.pm b/IP.pm index efa1d8d..f7ef524 100644 --- a/IP.pm +++ b/IP.pm @@ -8,7 +8,7 @@ use strict; use warnings; -our $VERSION = '3.09'; +our $VERSION = '3.10'; ############################################# # These are the overload methods, placed here @@ -51,85 +51,109 @@ # one octet at a time. String comparison # is not portable because of endianness. '>' => sub { - return 0 if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{addr}, $b, 8) - > vec($_[1]->{addr}, $b, 8); - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{mask}, $b, 8) - > vec($_[1]->{mask}, $b, 8); - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) > scalar($_[1]->numeric()); + +# return 0 if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{addr}, $b, 8) +# > vec($_[1]->{addr}, $b, 8); +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{mask}, $b, 8) +# > vec($_[1]->{mask}, $b, 8); +# } +# return 0; }, '<' => sub { - return 0 if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{addr}, $b, 8) - < vec($_[1]->{addr}, $b, 8); - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{mask}, $b, 8) - < vec($_[1]->{mask}, $b, 8); - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) < scalar($_[1]->numeric()); + +# return 0 if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{addr}, $b, 8) +# < vec($_[1]->{addr}, $b, 8); +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{mask}, $b, 8) +# < vec($_[1]->{mask}, $b, 8); +# } +# return 0; }, '>=' => sub { - return 0 if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{addr}, $b, 8) - >= vec($_[1]->{addr}, $b, 8); - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{mask}, $b, 8) - >= vec($_[1]->{mask}, $b, 8); - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) >= scalar($_[1]->numeric()); + +# return 0 if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{addr}, $b, 8) +# >= vec($_[1]->{addr}, $b, 8); +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{mask}, $b, 8) +# >= vec($_[1]->{mask}, $b, 8); +# } +# return 0; }, '<=' => sub { - return 0 if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{addr}, $b, 8) - <= vec($_[1]->{addr}, $b, 8); - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - return 1 if vec($_[0]->{mask}, $b, 8) - <= vec($_[1]->{mask}, $b, 8); - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) <= scalar($_[1]->numeric()); + +# return 0 if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{addr}, $b, 8) +# <= vec($_[1]->{addr}, $b, 8); +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# return 1 if vec($_[0]->{mask}, $b, 8) +# <= vec($_[1]->{mask}, $b, 8); +# } +# return 0; }, '<=>' => sub { - return undef if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - my $r = vec($_[0]->{addr}, $b, 8) - <=> vec($_[1]->{addr}, $b, 8); - return $r if $r; - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - my $r = vec($_[0]->{mask}, $b, 8) - <=> vec($_[1]->{mask}, $b, 8); - return $r if $r; - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) <=> scalar($_[1]->numeric()); + +# return undef if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# my $r = vec($_[0]->{addr}, $b, 8) +# <=> vec($_[1]->{addr}, $b, 8); +# return $r if $r; +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# my $r = vec($_[0]->{mask}, $b, 8) +# <=> vec($_[1]->{mask}, $b, 8); +# return $r if $r; +# } +# return 0; }, 'cmp' => sub { - return undef if ($_[0]->{bits} != $_[1]->{bits}); - for my $b (0 .. $_[0]->{bits}/8 - 1) { - my $r = vec($_[0]->{addr}, $b, 8) - <=> vec($_[1]->{addr}, $b, 8); - return $r if $r; - } - for my $b (0 .. $_[0]->{bits}/8 - 1) { - my $r = vec($_[0]->{mask}, $b, 8) - <=> vec($_[1]->{mask}, $b, 8); - return $r if $r; - } - return 0; + + return undef unless $_[0]->{bits} == $_[1]->{bits}; + return scalar($_[0]->numeric()) <=> scalar($_[1]->numeric()); + +# return undef if ($_[0]->{bits} != $_[1]->{bits}); +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# my $r = vec($_[0]->{addr}, $b, 8) +# <=> vec($_[1]->{addr}, $b, 8); +# return $r if $r; +# } +# for my $b (0 .. $_[0]->{bits}/8 - 1) { +# my $r = vec($_[0]->{mask}, $b, 8) +# <=> vec($_[1]->{mask}, $b, 8); +# return $r if $r; +# } +# return 0; }, '@{}' => sub { @@ -1473,6 +1497,18 @@ =back +=item 3.10 + +=over + +=item * + +Fixed relops. Semantics where adjusted to remove the netmask from the +comparison. (ie, it does not make sense to say that 10.0.0.0/24 is > +10.0.0.0/16 or viceversa). + +=back + =back =head1 AUTHOR diff --git a/t/relops.t b/t/relops.t index 364c47b..be7fcf0 100644 --- a/t/relops.t +++ b/t/relops.t @@ -2,8 +2,8 @@ my @gt = ( [ '255.255.255.255/32', '0.0.0.0/0' ], - [ '10.0.0.0/16', '10.0.0.0/8' ], - [ '10.0.0.0/24', '10.0.0.0/8' ], + [ '10.0.1.0/16', '10.0.0.1/24' ], + [ '10.0.0.1/24', '10.0.0.0/24' ], ); my @ngt = ( @@ -13,9 +13,10 @@ my @cmp = ( [ '0.0.0.0/0', '255.255.255.255/32', -1 ], - [ '10.0.0.0/16', '10.0.0.0/8', 1 ], - [ '10.0.0.0/24', '10.0.0.0/8', 1 ], + [ '10.0.0.0/16', '10.0.0.0/8', 0 ], + [ '10.0.0.0/24', '10.0.0.0/8', 0 ], [ '255.255.255.255/32', '0.0.0.0/0', 1 ], + [ '142.52.5.87', '142.52.2.88', 1 ], [ '10.0.0.0/24', '10.0.0.0/24', 0 ], [ 'default', 'default', 0 ], [ 'broadcast', 'broadcast', 0], @@ -33,10 +34,10 @@ my $b_ip = new NetAddr::IP $a->[1]; if ($a_ip > $b_ip) { - print "ok $count\n"; + print "ok $count # $a_ip > $b_ip\n"; } else { - print "not ok $count\n"; + print "not ok $count # $a_ip > $b_ip\n"; } ++$count; } @@ -46,10 +47,10 @@ my $b_ip = new NetAddr::IP $a->[1]; if (not ($a_ip > $b_ip)) { - print "ok $count\n"; + print "ok $count # $a_ip !> $b_ip\n"; } else { - print "not ok $count\n"; + print "not ok $count # $a_ip !> $b_ip\n"; } ++$count; } @@ -59,10 +60,10 @@ my $b_ip = new NetAddr::IP $a->[1]; if (($a_ip <=> $b_ip) == $a->[2]) { - print "ok $count\n"; + print "ok $count # $a_ip <=> $b_ip\n"; } else { - print "not ok $count\n"; + print "not ok $count # $a_ip <=> $b_ip\n"; } ++$count; } @@ -72,10 +73,10 @@ my $b_ip = new NetAddr::IP $a->[1]; if (($a_ip cmp $b_ip) == $a->[2]) { - print "ok $count\n"; + print "ok $count # $a_ip cmp $b_ip\n"; } else { - print "not ok $count\n"; + print "not ok $count # $a_ip cmp $b_ip\n"; } ++$count; }