1 package FS::addr_block;
5 use FS::Record qw( qsearchs qsearch dbh );
10 @ISA = qw( FS::Record );
14 FS::addr_block - Object methods for addr_block records
20 $record = new FS::addr_block \%hash;
21 $record = new FS::addr_block { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::addr_block record describes an address block assigned for broadband
34 access. FS::addr_block inherits from FS::Record. The following fields are
39 =item blocknum - primary key, used in FS::svc_broadband to associate
40 services to the block.
42 =item routernum - the router (see FS::router) to which this
45 =item ip_gateway - the gateway address used by customers within this block.
47 =item ip_netmask - the netmask of the block, expressed as an integer.
57 Create a new record. To add the record to the database, see "insert".
61 sub table { 'addr_block'; }
65 Adds this record to the database. If there is an error, returns the error,
66 otherwise returns false.
70 Deletes this record from the database. If there is an error, returns the
71 error, otherwise returns false.
75 return 'Block must be deallocated before deletion'
81 =item replace OLD_RECORD
83 Replaces OLD_RECORD with this one in the database. If there is an error,
84 returns the error, otherwise returns false.
88 Checks all fields to make sure this is a valid record. If there is an error,
89 returns the error, otherwise returns false. Called by the insert and replace
98 $self->ut_number('routernum')
99 || $self->ut_ip('ip_gateway')
100 || $self->ut_number('ip_netmask')
102 return $error if $error;
105 # A routernum of 0 indicates an unassigned block and is allowed
106 return "Unknown routernum"
107 if ($self->routernum and not $self->router);
109 my $self_addr = $self->NetAddr;
110 return "Cannot parse address: ". $self->ip_gateway . '/' . $self->ip_netmask
113 if (not $self->blocknum) {
115 my $block_addr = $_->NetAddr;
116 if($block_addr->contains($self_addr)
117 or $self_addr->contains($block_addr)) { $_; };
118 } qsearch( 'addr_block', {});
120 return "Block intersects existing block ".$_->ip_gateway."/".$_->ip_netmask;
130 Returns the FS::router object corresponding to this object. If the
131 block is unassigned, returns undef.
137 return qsearchs('router', { routernum => $self->routernum });
142 Returns a list of FS::svc_broadband objects associated
149 return qsearch('svc_broadband', { blocknum => $self->blocknum });
154 Returns a NetAddr::IP object for this block's address and netmask.
161 return new NetAddr::IP ($self->ip_gateway, $self->ip_netmask);
166 Returns a NetAddr::IP object corresponding to the first unassigned address
167 in the block (other than the network, broadcast, or gateway address). If
168 there are no free addresses, returns false.
175 my @used = map { $_->NetAddr->addr }
177 qsearch('svc_broadband', { blocknum => $self->blocknum }) );
179 my @free = $self->NetAddr->hostenum;
180 while (my $ip = shift @free) {
181 if (not grep {$_ eq $ip->addr;} @used) { return $ip; };
190 Allocates this address block to a router. Takes an FS::router object
193 At present it's not possible to reallocate a block to a different router
194 except by deallocating it first, which requires that none of its addresses
195 be assigned. This is probably as it should be.
200 my ($self, $router) = @_;
202 return 'Block is already allocated'
205 return 'Block must be allocated to a router'
206 unless(ref $router eq 'FS::router');
208 my @svc = $self->svc_broadband;
210 return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
213 my $new = new FS::addr_block {$self->hash};
214 $new->routernum($router->routernum);
215 return $new->replace($self);
221 Deallocates the block (i.e. sets the routernum to 0). If any addresses in the
222 block are assigned to services, it fails.
229 my @svc = $self->svc_broadband;
231 return 'Block has assigned addresses: '. join ', ', map {$_->ip_addr} @svc;
234 my $new = new FS::addr_block {$self->hash};
236 return $new->replace($self);
241 Splits this address block into two equal blocks, occupying the same space as
242 the original block. The first of the two will also have the same blocknum.
243 The gateway address of each block will be set to the first usable address, i.e.
244 (network address)+1. Since this method is designed for use on unallocated
245 blocks, this is probably the correct behavior.
247 (At present, splitting allocated blocks is disallowed. Anyone who wants to
248 implement this is reminded that each split costs three addresses, and any
249 customers who were using these addresses will have to be moved; depending on
250 how full the block was before being split, they might have to be moved to a
251 different block. Anyone who I<still> wants to implement it is asked to tie it
252 to a configuration switch so that site admins can disallow it.)
258 # We should consider using Attribute::Handlers/Aspect/Hook::LexWrap/
259 # something to atomicize functions, so that we can say
261 # sub split_block : atomic {
263 # instead of repeating all this AutoCommit verbage in every
264 # sub that does more than one database operation.
266 my $oldAutoCommit = $FS::UID::AutoCommit;
267 local $FS::UID::AutoCommit = 0;
274 return 'Block is already allocated';
277 #TODO: Smallest allowed block should be a config option.
278 if ($self->NetAddr->masklen() ge 30) {
279 return 'Cannot split blocks with a mask length >= 30';
283 $ip[0] = $self->NetAddr;
284 @ip = map {$_->first()} $ip[0]->split($self->ip_netmask + 1);
287 $new[$_] = new FS::addr_block {$self->hash};
288 $new[$_]->ip_gateway($ip[$_]->addr);
289 $new[$_]->ip_netmask($ip[$_]->masklen);
292 $new[1]->blocknum('');
294 $error = $new[0]->replace($self);
300 $error = $new[1]->insert;
306 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
318 Minimum block size should be a config option. It's hardcoded at /30 right
319 now because that's the smallest block that makes any sense at all.