1 package FS::addr_block;
5 use FS::Record qw( qsearchs qsearch dbh );
11 @ISA = qw( FS::Record );
15 FS::addr_block - Object methods for addr_block records
21 $record = new FS::addr_block \%hash;
22 $record = new FS::addr_block { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::addr_block record describes an address block assigned for broadband
35 access. FS::addr_block inherits from FS::Record. The following fields are
40 =item blocknum - primary key, used in FS::svc_broadband to associate
41 services to the block.
43 =item routernum - the router (see FS::router) to which this
46 =item ip_gateway - the gateway address used by customers within this block.
48 =item ip_netmask - the netmask of the block, expressed as an integer.
58 Create a new record. To add the record to the database, see "insert".
62 sub table { 'addr_block'; }
66 Adds this record to the database. If there is an error, returns the error,
67 otherwise returns false.
71 Deletes this record from the database. If there is an error, returns the
72 error, otherwise returns false.
76 return 'Block must be deallocated before deletion'
82 =item replace OLD_RECORD
84 Replaces OLD_RECORD with this one in the database. If there is an error,
85 returns the error, otherwise returns false.
89 Checks all fields to make sure this is a valid record. If there is an error,
90 returns the error, otherwise returns false. Called by the insert and replace
99 $self->ut_number('routernum')
100 || $self->ut_ip('ip_gateway')
101 || $self->ut_number('ip_netmask')
103 return $error if $error;
106 # A routernum of 0 indicates an unassigned block and is allowed
107 return "Unknown routernum"
108 if ($self->routernum and not $self->router);
110 my $self_addr = $self->NetAddr;
111 return "Cannot parse address: ". $self->ip_gateway . '/' . $self->ip_netmask
114 if (not $self->blocknum) {
116 my $block_addr = $_->NetAddr;
117 if($block_addr->contains($self_addr)
118 or $self_addr->contains($block_addr)) { $_; };
119 } qsearch( 'addr_block', {});
121 return "Block intersects existing block ".$_->ip_gateway."/".$_->ip_netmask;
131 Returns the FS::router object corresponding to this object. If the
132 block is unassigned, returns undef.
138 return qsearchs('router', { routernum => $self->routernum });
143 Returns a list of FS::svc_broadband objects associated
150 return qsearch('svc_broadband', { blocknum => $self->blocknum });
155 Returns a NetAddr::IP object for this block's address and netmask.
162 return new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
167 Returns a NetAddr::IP object corresponding to the first unassigned address
168 in the block (other than the network, broadcast, or gateway address). If
169 there are no free addresses, returns false.
176 my $conf = new FS::Conf;
177 my @excludeaddr = $conf->config('exclude_ip_addr');
180 ( (map { $_->NetAddr->addr }
182 qsearch('svc_broadband', { blocknum => $self->blocknum }))
186 my @free = $self->NetAddr->hostenum;
187 while (my $ip = shift @free) {
188 if (not grep {$_ eq $ip->addr;} @used) { return $ip; };
197 Allocates this address block to a router. Takes an FS::router object
200 At present it's not possible to reallocate a block to a different router
201 except by deallocating it first, which requires that none of its addresses
202 be assigned. This is probably as it should be.
207 my ($self, $router) = @_;
209 return 'Block is already allocated'
212 return 'Block must be allocated to a router'
213 unless(ref $router eq 'FS::router');
215 my @svc = $self->svc_broadband;
217 return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
220 my $new = new FS::addr_block {$self->hash};
221 $new->routernum($router->routernum);
222 return $new->replace($self);
228 Deallocates the block (i.e. sets the routernum to 0). If any addresses in the
229 block are assigned to services, it fails.
236 my @svc = $self->svc_broadband;
238 return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
241 my $new = new FS::addr_block {$self->hash};
243 return $new->replace($self);
248 Splits this address block into two equal blocks, occupying the same space as
249 the original block. The first of the two will also have the same blocknum.
250 The gateway address of each block will be set to the first usable address, i.e.
251 (network address)+1. Since this method is designed for use on unallocated
252 blocks, this is probably the correct behavior.
254 (At present, splitting allocated blocks is disallowed. Anyone who wants to
255 implement this is reminded that each split costs three addresses, and any
256 customers who were using these addresses will have to be moved; depending on
257 how full the block was before being split, they might have to be moved to a
258 different block. Anyone who I<still> wants to implement it is asked to tie it
259 to a configuration switch so that site admins can disallow it.)
265 # We should consider using Attribute::Handlers/Aspect/Hook::LexWrap/
266 # something to atomicize functions, so that we can say
268 # sub split_block : atomic {
270 # instead of repeating all this AutoCommit verbage in every
271 # sub that does more than one database operation.
273 my $oldAutoCommit = $FS::UID::AutoCommit;
274 local $FS::UID::AutoCommit = 0;
281 return 'Block is already allocated';
284 #TODO: Smallest allowed block should be a config option.
285 if ($self->NetAddr->masklen() ge 30) {
286 return 'Cannot split blocks with a mask length >= 30';
290 $ip[0] = $self->NetAddr;
291 @ip = map {$_->first()} $ip[0]->split($self->ip_netmask + 1);
294 $new[$_] = new FS::addr_block {$self->hash};
295 $new[$_]->ip_gateway($ip[$_]->addr);
296 $new[$_]->ip_netmask($ip[$_]->masklen);
299 $new[1]->blocknum('');
301 $error = $new[0]->replace($self);
307 $error = $new[1]->insert;
313 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
325 Minimum block size should be a config option. It's hardcoded at /30 right
326 now because that's the smallest block that makes any sense at all.