1 package FS::addr_block;
5 use FS::Record qw( qsearchs qsearch dbh );
12 @ISA = qw( FS::Record );
16 FS::addr_block - Object methods for addr_block records
22 $record = new FS::addr_block \%hash;
23 $record = new FS::addr_block { 'column' => 'value' };
25 $error = $record->insert;
27 $error = $new_record->replace($old_record);
29 $error = $record->delete;
31 $error = $record->check;
35 An FS::addr_block record describes an address block assigned for broadband
36 access. FS::addr_block inherits from FS::Record. The following fields are
41 =item blocknum - primary key, used in FS::svc_broadband to associate
42 services to the block.
44 =item routernum - the router (see FS::router) to which this
47 =item ip_gateway - the gateway address used by customers within this block.
49 =item ip_netmask - the netmask of the block, expressed as an integer.
51 =item manual_flag - prohibit automatic ip assignment from this block when true.
53 =item agentnum - optional agent number (see L<FS::agent>)
63 Create a new record. To add the record to the database, see "insert".
67 sub table { 'addr_block'; }
71 Adds this record to the database. If there is an error, returns the error,
72 otherwise returns false.
76 Deletes this record from the database. If there is an error, returns the
77 error, otherwise returns false.
83 return 'Block must be deallocated and have no services before deletion'
84 if $self->router || $self->svc_broadband;
86 local $SIG{HUP} = 'IGNORE';
87 local $SIG{INT} = 'IGNORE';
88 local $SIG{QUIT} = 'IGNORE';
89 local $SIG{TERM} = 'IGNORE';
90 local $SIG{TSTP} = 'IGNORE';
91 local $SIG{PIPE} = 'IGNORE';
93 my $oldAutoCommit = $FS::UID::AutoCommit;
94 local $FS::UID::AutoCommit = 0;
97 my $error = $self->SUPER::delete;
99 $dbh->rollback if $oldAutoCommit;
103 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
108 =item replace OLD_RECORD
110 Replaces OLD_RECORD with this one in the database. If there is an error,
111 returns the error, otherwise returns false.
113 At present it's not possible to reallocate a block to a different router
114 except by deallocating it first, which requires that none of its addresses
115 be assigned. This is probably as it should be.
118 my ( $new, $old ) = ( shift, shift );
120 unless($new->routernum == $old->routernum) {
121 my @svc = $self->svc_broadband;
123 return 'Block has assigned addresses: '.
124 join ', ', map {$_->ip_addr} @svc;
127 return 'Block is already allocated'
128 if($new->routernum && $old->routernum);
137 Checks all fields to make sure this is a valid record. If there is an error,
138 returns the error, otherwise returns false. Called by the insert and replace
147 $self->ut_number('routernum')
148 || $self->ut_ip('ip_gateway')
149 || $self->ut_number('ip_netmask')
150 || $self->ut_enum('manual_flag', [ '', 'Y' ])
151 || $self->ut_agentnum_acl('agentnum', 'Broadband global configuration')
153 return $error if $error;
156 # A routernum of 0 indicates an unassigned block and is allowed
157 return "Unknown routernum"
158 if ($self->routernum and not $self->router);
160 my $self_addr = $self->NetAddr;
161 return "Cannot parse address: ". $self->ip_gateway . '/' . $self->ip_netmask
164 if (not $self->blocknum) {
166 my $block_addr = $_->NetAddr;
167 if($block_addr->contains($self_addr)
168 or $self_addr->contains($block_addr)) { $_; };
169 } qsearch( 'addr_block', {});
171 return "Block intersects existing block ".$_->ip_gateway."/".$_->ip_netmask;
181 Returns the FS::router object corresponding to this object. If the
182 block is unassigned, returns undef.
188 return qsearchs('router', { routernum => $self->routernum });
193 Returns a list of FS::svc_broadband objects associated
200 return qsearch('svc_broadband', { blocknum => $self->blocknum });
205 Returns a NetAddr::IP object for this block's address and netmask.
211 new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
216 Returns a CIDR string for this block's address and netmask, i.e. 10.4.20.0/24
222 $self->NetAddr->cidr;
227 Returns a NetAddr::IP object corresponding to the first unassigned address
228 in the block (other than the network, broadcast, or gateway address). If
229 there are no free addresses, returns false. There are never free addresses
230 when manual_flag is true.
237 return '' if $self->manual_flag;
239 my $conf = new FS::Conf;
240 my @excludeaddr = $conf->config('exclude_ip_addr');
243 ( (map { $_->NetAddr->addr }
245 qsearch('svc_broadband', { blocknum => $self->blocknum }))
249 my @free = $self->NetAddr->hostenum;
250 while (my $ip = shift @free) {
251 if (not grep {$_ eq $ip->addr;} @used) { return $ip; };
258 =item allocate -- deprecated
260 Allocates this address block to a router. Takes an FS::router object
263 At present it's not possible to reallocate a block to a different router
264 except by deallocating it first, which requires that none of its addresses
265 be assigned. This is probably as it should be.
270 my ($self, $router) = @_;
271 carp "deallocate deprecated -- use replace";
273 return 'Block must be allocated to a router'
274 unless(ref $router eq 'FS::router');
276 my $new = new FS::addr_block {$self->hash};
277 $new->routernum($router->routernum);
278 return $new->replace($self);
282 =item deallocate -- deprecated
284 Deallocates the block (i.e. sets the routernum to 0). If any addresses in the
285 block are assigned to services, it fails.
290 carp "deallocate deprecated -- use replace";
293 my $new = new FS::addr_block {$self->hash};
295 return $new->replace($self);
300 Splits this address block into two equal blocks, occupying the same space as
301 the original block. The first of the two will also have the same blocknum.
302 The gateway address of each block will be set to the first usable address, i.e.
303 (network address)+1. Since this method is designed for use on unallocated
304 blocks, this is probably the correct behavior.
306 (At present, splitting allocated blocks is disallowed. Anyone who wants to
307 implement this is reminded that each split costs three addresses, and any
308 customers who were using these addresses will have to be moved; depending on
309 how full the block was before being split, they might have to be moved to a
310 different block. Anyone who I<still> wants to implement it is asked to tie it
311 to a configuration switch so that site admins can disallow it.)
317 # We should consider using Attribute::Handlers/Aspect/Hook::LexWrap/
318 # something to atomicize functions, so that we can say
320 # sub split_block : atomic {
322 # instead of repeating all this AutoCommit verbage in every
323 # sub that does more than one database operation.
325 my $oldAutoCommit = $FS::UID::AutoCommit;
326 local $FS::UID::AutoCommit = 0;
333 return 'Block is already allocated';
336 #TODO: Smallest allowed block should be a config option.
337 if ($self->NetAddr->masklen() ge 30) {
338 return 'Cannot split blocks with a mask length >= 30';
342 $ip[0] = $self->NetAddr;
343 @ip = map {$_->first()} $ip[0]->split($self->ip_netmask + 1);
346 $new[$_] = new FS::addr_block {$self->hash};
347 $new[$_]->ip_gateway($ip[$_]->addr);
348 $new[$_]->ip_netmask($ip[$_]->masklen);
351 $new[1]->blocknum('');
353 $error = $new[0]->replace($self);
359 $error = $new[1]->insert;
365 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
375 Returns the agent (see L<FS::agent>) for this address block, if one exists.
380 qsearchs('agent', { 'agentnum' => shift->agentnum } );
385 Returns text including the router name, gateway ip, and netmask for this
392 my $router = $self->router;
393 ($router ? $router->routername : '(unallocated)'). ':'. $self->NetAddr;
400 Minimum block size should be a config option. It's hardcoded at /30 right
401 now because that's the smallest block that makes any sense at all.