X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Faddr_range.pm;h=1a8484fb697e72b2b3333cfa5d1d088118324349;hp=18ae1e2ea9432ef2da82dcaa7e5cb5c6ecb81abf;hb=5372897f367498972c96f5494e142e6e11b29eb8;hpb=430b2c784d2ee9ea5be00b821d2dbd27279ef132 diff --git a/FS/FS/addr_range.pm b/FS/FS/addr_range.pm index 18ae1e2ea..1a8484fb6 100644 --- a/FS/FS/addr_range.pm +++ b/FS/FS/addr_range.pm @@ -149,6 +149,9 @@ sub end { $self->set('start', $end); ($end, $start) = ($start, $end); } + # fails if $end - $start > 2^31 + # so don't do that + # (fixed in NetAddr::IP 4.050 but we can't rely on that, apparently) $self->set('length', $end - $start + 1); return $end->addr; } @@ -165,13 +168,13 @@ Checks whether IPADDR (a dotted-quad IPv4 address) is within the range. sub contains { my $self = shift; my $addr = shift; - $addr = NetAddr::IP->new($addr, 0) - unless ref($addr) and UNIVERSAL::isa($addr, 'NetAddr::IP'); + $addr = NetAddr::IP->new($addr, 0); return 0 unless $addr; my $start = NetAddr::IP->new($self->start, 0); - return ($addr >= $start and $addr - $start < $self->length) ? 1 : 0; + return ($addr >= $start and $addr < ( $start + $self->length) ) + ? 1 : 0; } =item as_string @@ -183,7 +186,7 @@ Returns a readable string showing the address range. sub as_string { my $self = shift; my $start = NetAddr::IP->new($self->start, 0); - my $end = $start + $self->length; + my $end = $start + $self->length - 1; if ( $self->length == 1 ) { # then just the address @@ -242,10 +245,13 @@ sub any_contains { =head1 DEVELOPER NOTE -L objects have netmasks. When using them to represent -range endpoints, be sure to set the netmask to I so that math on -the address doesn't stop at the subnet boundary. (The default is /32, -which doesn't work very well. Address ranges ignore subnet boundaries.) +L objects have netmasks. They also have overloaded operators +for addition and subtraction, but those have range limitations when comparing +addresses. (An IPv4 address is effectively a uint32; the difference +between two IPv4 addresses is the same range, but signed.) In later versions +of the library the C method can be used as a workaround, but +otherwise it's not safe to subtract two addresses that might differ in the +first bit of the first octet. =head1 BUGS