fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / addr_range.pm
index 18ae1e2..1a8484f 100644 (file)
@@ -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<NetAddr::IP> objects have netmasks.  When using them to represent 
-range endpoints, be sure to set the netmask to I<zero> 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<NetAddr::IP> 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<bigint> 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