X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FIP_Mixin.pm;h=b68b0b6241a6cbfe527058b30a32da0a6537dfa3;hb=e2ee874843b19f6c5221f5ce0af90979dba34e57;hp=fdeb51da76721ef40fd9b594c4d5e473c44427cb;hpb=87f255507af9f14dfbccd37eefd71a148f9af344;p=freeside.git diff --git a/FS/FS/IP_Mixin.pm b/FS/FS/IP_Mixin.pm index fdeb51da7..b68b0b624 100644 --- a/FS/FS/IP_Mixin.pm +++ b/FS/FS/IP_Mixin.pm @@ -4,6 +4,7 @@ use strict; use NetAddr::IP; use FS::addr_block; use FS::router; +use FS::addr_range; use FS::Record qw(qsearch); use FS::Conf; # careful about importing anything here--it will end up in a LOT of @@ -129,6 +130,10 @@ sub assign_ip_addr { my $self = shift; my %opt = @_; + #otherwise we'll get the same assignment for concurrent identical calls + # this will serialize them + $_->lock_table foreach @subclasses; + my @blocks; my $na = $self->NetAddr; @@ -152,14 +157,14 @@ sub assign_ip_addr { # don't exit early on assigning a free address--check the rest of # the blocks to see if the current address is in one of them. if (!$new_addr) { - $new_addr = $block->next_free_addr->addr; + $new_addr = $block->next_free_addr; $new_block = $block; } } return 'No IP address available on this router' unless $new_addr; - $self->ip_addr($new_addr); + $self->ip_addr($new_addr->addr); $self->addr_block($new_block); ''; } @@ -200,12 +205,21 @@ sub check_ip_addr { return '' if $addr eq ''; my $na = $self->NetAddr or return "Can't parse address '$addr'"; + # if there's a chosen address block, check that the address is in it if ( my $block = $self->addr_block ) { if ( !$block->NetAddr->contains($na) ) { return "Address $addr not in block ".$block->cidr; } } - # this returns '' if the address is in use by $self. + # if the address is in any designated ranges, check that they don't + # disallow use + foreach my $range (FS::addr_range->any_contains($addr)) { + if ( !$range->allow_use ) { + return "Address $addr is in ".$range->desc." range ".$range->as_string; + } + } + # check that nobody else is sitting on the address + # (this returns '' if the address is in use by $self) if ( my $dup = $self->is_used($self->ip_addr) ) { return "Address $addr in use by $dup"; } @@ -250,18 +264,22 @@ sub router { FS::router->by_key($self->routernum); } -=item used_addresses [ BLOCK ] +=item used_addresses [ FS::addr_block ] -Returns a list of all addresses (in BLOCK, or in all blocks) -that are in use. If called as an instance method, excludes -that instance from the search. +Returns a list of all addresses in use within the given L. + +If called as an instance method, excludes that instance from the search. =cut sub used_addresses { - my $self = shift; - my $block = shift; - return ( map { $_->_used_addresses($block, $self) } @subclasses ); + my ($self, $block) = @_; + + ( + $block->ip_gateway ? $block->ip_gateway : (), + $block->NetAddr->broadcast->addr, + map { $_->_used_addresses($block, $self ) } @subclasses + ); } sub _used_addresses {