X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Faddr_block.pm;h=5fd64bf7a861b6df848f92a9f844f93bb047b576;hp=6a62777be222209f42ca1e5ecfd4d5cf2ae1d2cf;hb=14f234a943f1e4b5bbc6fe90254b8f5ca677f028;hpb=87f255507af9f14dfbccd37eefd71a148f9af344 diff --git a/FS/FS/addr_block.pm b/FS/FS/addr_block.pm index 6a62777be..5fd64bf7a 100755 --- a/FS/FS/addr_block.pm +++ b/FS/FS/addr_block.pm @@ -1,17 +1,14 @@ package FS::addr_block; +use base qw(FS::Record); use strict; -use vars qw( @ISA ); -use FS::Record qw( qsearchs qsearch dbh ); -use FS::router; -use FS::svc_broadband; -use FS::Conf; -use FS::IP_Mixin; -use NetAddr::IP; use Carp qw( carp ); use List::Util qw( first ); - -@ISA = qw( FS::Record ); +use NetAddr::IP; +use FS::Conf; +use FS::Record qw( qsearch dbh ); #qsearchs +use FS::IP_Mixin; +use FS::addr_range; =head1 NAME @@ -183,25 +180,11 @@ sub check { Returns the FS::router object corresponding to this object. If the block is unassigned, returns undef. -=cut - -sub router { - my $self = shift; - return qsearchs('router', { routernum => $self->routernum }); -} - =item svc_broadband Returns a list of FS::svc_broadband objects associated with this object. -=cut - -sub svc_broadband { - my $self = shift; - return qsearch('svc_broadband', { blocknum => $self->blocknum }); -} - =item NetAddr Returns a NetAddr::IP object for this block's address and netmask. @@ -224,6 +207,27 @@ sub cidr { $self->NetAddr->cidr; } +=item free_addrs + +Returns an aref sorted list of free addresses in the block. + +=cut + +sub free_addrs { + my $self = shift; + + my %used_addr_map = + map {$_ => 1} + FS::IP_Mixin->used_addresses($self), + FS::Conf->new()->config('exclude_ip_addr'); + + [ + grep { !exists $used_addr_map{$_} } + map { $_->addr } + $self->NetAddr->hostenum + ]; +} + =item next_free_addr Returns a NetAddr::IP object corresponding to the first unassigned address @@ -256,7 +260,17 @@ sub next_free_addr { # just do a linear search of the block my $freeaddr = $selfaddr->network + 1; while ( $freeaddr < $selfaddr->broadcast ) { - return $freeaddr unless $used{ $freeaddr->addr }; + # also make sure it's not blocked from assignment by an address range + if ( !$used{$freeaddr->addr } ) { + my ($range) = grep { !$_->allow_use } + FS::addr_range->any_contains($freeaddr->addr); + if ( !$range ) { + # then we've found a free address + return $freeaddr; + } + # otherwise, skip to the end of the range + $freeaddr = NetAddr::IP->new($range->end, $self->ip_netmask); + } $freeaddr++; } return; @@ -382,12 +396,6 @@ To be implemented. Returns the agent (see L) for this address block, if one exists. -=cut - -sub agent { - qsearchs('agent', { 'agentnum' => shift->agentnum } ); -} - =item label Returns text including the router name, gateway ip, and netmask for this @@ -401,6 +409,24 @@ sub label { ($router ? $router->routername : '(unallocated)'). ':'. $self->NetAddr; } +=item router + +Returns the router assigned to this block. + +=cut + +# necessary, because this can't be foreign keyed + +sub router { + my $self = shift; + my $routernum = $self->routernum; + if ( $routernum ) { + return FS::router->by_key($routernum); + } else { + return; + } +} + =back =head1 BUGS @@ -411,4 +437,3 @@ now because that's the smallest block that makes any sense at all. =cut 1; -