X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Faddr_block.pm;h=eb84dafc32ed03d0c201a1c46599b314d043e8fe;hp=5815f1abd5e9c7eeab9e79662defc7c0ea41bb07;hb=b79b0ebca0c15ad527de3d589cda36da63b0601e;hpb=a1871d3d13c1dafa93b956762c0d23728d261da7 diff --git a/FS/FS/addr_block.pm b/FS/FS/addr_block.pm index 5815f1abd..eb84dafc3 100755 --- a/FS/FS/addr_block.pm +++ b/FS/FS/addr_block.pm @@ -1,15 +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 NetAddr::IP; use Carp qw( carp ); - -@ISA = qw( FS::Record ); +use List::Util qw( first ); +use NetAddr::IP; +use FS::Conf; +use FS::Record qw( qsearch dbh ); #qsearchs +use FS::IP_Mixin; +use FS::addr_range; =head1 NAME @@ -48,6 +47,8 @@ block is assigned. =item ip_netmask - the netmask of the block, expressed as an integer. +=item manual_flag - prohibit automatic ip assignment from this block when true. + =item agentnum - optional agent number (see L) =back @@ -74,14 +75,35 @@ otherwise returns false. Deletes this record from the database. If there is an error, returns the error, otherwise returns false. +=cut + sub delete { my $self = shift; - return 'Block must be deallocated before deletion' - if $self->router; - - $self->SUPER::delete; + return 'Block must be deallocated and have no services before deletion' + if $self->router || $self->svc_broadband; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $error = $self->SUPER::delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + ''; } + =item replace OLD_RECORD Replaces OLD_RECORD with this one in the database. If there is an error, @@ -124,7 +146,8 @@ sub check { $self->ut_number('routernum') || $self->ut_ip('ip_gateway') || $self->ut_number('ip_netmask') - || $self->ut_agentnum_acl('agentnum', 'Engineering global configuration') + || $self->ut_enum('manual_flag', [ '', 'Y' ]) + || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration') ; return $error if $error; @@ -157,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. @@ -198,33 +207,69 @@ sub cidr { $self->NetAddr->cidr; } +=item free_addrs + +Returns a 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_in_block($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 in the block (other than the network, broadcast, or gateway address). If -there are no free addresses, returns false. +there are no free addresses, returns nothing. There are never free addresses +when manual_flag is true. + +There is no longer a method to return all free addresses in a block. =cut sub next_free_addr { my $self = shift; + my $selfaddr = $self->NetAddr; + + return () if $self->manual_flag; my $conf = new FS::Conf; my @excludeaddr = $conf->config('exclude_ip_addr'); - -my @used = -( (map { $_->NetAddr->addr } - ($self, - qsearch('svc_broadband', { blocknum => $self->blocknum })) - ), @excludeaddr -); - - my @free = $self->NetAddr->hostenum; - while (my $ip = shift @free) { - if (not grep {$_ eq $ip->addr;} @used) { return $ip; }; - } - ''; + my %used = map { $_ => 1 } + ( + @excludeaddr, + $selfaddr->addr, + $selfaddr->network->addr, + $selfaddr->broadcast->addr, + FS::IP_Mixin->used_addresses_in_block($self) + ); + + # just do a linear search of the block + my $freeaddr = $selfaddr->network + 1; + while ( $freeaddr < $selfaddr->broadcast ) { + # 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; } @@ -347,12 +392,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 @@ -366,6 +405,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 @@ -376,4 +433,3 @@ now because that's the smallest block that makes any sense at all. =cut 1; -