$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;
}
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
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
=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