- # This should catch errors in the ip_addr and ip_netmask. If it doesn't,
- # they'll almost certainly not map into a valid block anyway.
- my $self_addr = new NetAddr::IP ($self->ip_addr, $self->ip_netmask);
- return 'Cannot parse address: ' . $self->ip_addr . '/' . $self->ip_netmask unless $self_addr;
-
- my @block = grep {
- my $block_addr = new NetAddr::IP ($_->ip_gateway, $_->ip_netmask);
- if ($block_addr->contains($self_addr)) { $_ };
- } qsearch( 'ac_block', { acnum => $self->acnum });
-
- if(scalar @block == 0) {
- return 'Block not found for address '.$self->ip_addr.' in actype '.$self->actypenum;
- } elsif(scalar @block > 1) {
- return 'ERROR: Intersecting blocks found for address '.$self->ip_addr.' :'.
- join ', ', map {$_->ip_addr . '/' . $_->ip_netmask} @block;
+ if (not($self->ip_addr) or $self->ip_addr eq '0.0.0.0') {
+ my $next_addr = $self->addr_block->next_free_addr;
+ if ($next_addr) {
+ $self->ip_addr($next_addr->addr);
+ } else {
+ return "No free addresses in addr_block (blocknum: ".$self->blocknum.")";
+ }