Changeset 695 for trunk


Ignore:
Timestamp:
02/13/15 12:26:44 (10 years ago)
Author:
Kris Deugau
Message:

/trunk

Mostly fix up edge case of extending an IP pool allocation. See #24.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r694 r695  
    12321232    # have to insert all pool IPs into poolips table as "unallocated".
    12331233    $sth = $dbh->prepare("INSERT INTO poolips (ip,custid,city,type,parent_id) VALUES (?,?,?,?,?)");
     1234
     1235    # in case of pool extension by some means, we need to see what IPs were already inserted
     1236    my $tmp1 = $dbh->selectall_arrayref("SELECT ip FROM poolips WHERE parent_id = ?", undef, $parent);
     1237    my %foundips;
     1238    foreach (@{$tmp1}) {
     1239      $foundips{$_->[0]} = 1;
     1240    }
     1241
     1242    # enumerate the hosts in the IP range - everything except the first (net) and last (bcast) IP
    12341243    my @poolip_list = $pool->hostenum;
     1244
     1245    # always check/add IPs from gw+1 through bcast-1:
     1246    # (but the set won't be in oooorderrrrr!  <pout>)
     1247    for (my $i=1; $i<=$#poolip_list; $i++) {
     1248      $sth->execute($poolip_list[$i]->addr, $pcustid, $city, $type, $parent) unless $foundips{$poolip_list[$i]};
     1249    }
     1250
     1251    # now do the special case - DSL/PPP blocks can use the "net", "gw", and "bcast" IPs.
     1252    # we exclude .0 and .255 anyway, since while they'll mostly work, they *will* behave badly here and there.
    12351253    if ($class eq 'all') { # (DSL-ish block - *all* IPs available
    12361254      if ($pool->addr !~ /\.0$/) {      # .0 causes weirdness.
    1237         $sth->execute($pool->addr, $pcustid, $city, $type, $parent);
    1238       }
    1239       for (my $i=0; $i<=$#poolip_list; $i++) {
    1240         $sth->execute($poolip_list[$i]->addr, $pcustid, $city, $type, $parent);
    1241       }
     1255        $sth->execute($pool->addr, $pcustid, $city, $type, $parent) unless $foundips{$pool->addr};
     1256      }
     1257      $sth->execute($poolip_list[0]->addr, $pcustid, $city, $type, $parent) unless $poolip_list[0];
    12421258      $pool--;
    12431259      if ($pool->addr !~ /\.255$/) {    # .255 can cause weirdness.
    1244         $sth->execute($pool->addr, $pcustid, $city, $type, $parent);
    1245       }
    1246     } else { # (real netblock)
    1247       for (my $i=1; $i<=$#poolip_list; $i++) {
    1248         $sth->execute($poolip_list[$i]->addr, $pcustid, $city, $type, $parent);
     1260        $sth->execute($pool->addr, $pcustid, $city, $type, $parent) unless $foundips{$pool->addr};
    12491261      }
    12501262    }
     
    13151327  return ('FAIL', 'No fields to update') if !@fieldlist;
    13161328
    1317   push @vallist, $args{block};
    13181329  my $sql = "UPDATE $updtable SET ";
    13191330  $sql .= join " = ?, ", @fieldlist;
    1320   $sql .= " = ? WHERE $keyfield = ?";
    13211331
    13221332  eval {
     1333    # check for block merge first...
     1334    if ($args{fbmerge}) {
     1335      my $cidr = NetAddr::IP->new($binfo->{block});
     1336      my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
     1337      # safety net?  make sure mergeable block passed in is really one or both of
     1338      # a) reserved for expansion of the block and
     1339      # b) confirmed CIDR-combinable
     1340      # "safety? SELECT foo FROM freeblocks WHERE cidr << ? AND masklen(cidr) = ?, $newblock, ".$cidr->masklen."\n";
     1341      $dbh->do("DELETE FROM freeblocks WHERE id=?", undef, $args{fbmerge});
     1342      # ... so we can append the change in the stored CIDR field to extend the allocation.
     1343      $sql .= " = ?, cidr";
     1344      push @vallist, $newblock;
     1345      # if we have an IP pool, call initPool to fill in any missing entries in the pool
     1346      if ($binfo->{type} =~ /^.p$/) {
     1347        my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'all', $args{block});
     1348        die $rmsg if $code eq 'FAIL';
     1349      } elsif ($binfo->{type} =~ /^.d$/) {
     1350        my ($code,$rmsg) = initPool($dbh, "$newblock", $binfo->{type}, $binfo->{city}, 'normal', $args{block});
     1351        die $rmsg if $code eq 'FAIL';
     1352      }
     1353    }
     1354
     1355    # append another SQL fragment
     1356    push @vallist, $args{block};
     1357    $sql .= " = ? WHERE $keyfield = ?";
     1358
    13231359    # do the update
    13241360    $dbh->do($sql, undef, @vallist);
  • trunk/cgi-bin/main.cgi

    r692 r695  
    904904  $page->param(vlan     => $blockinfo->{vlan});
    905905
     906  # Reserved-for-expansion
     907  $page->param(reserve  => $blockinfo->{reserve});
     908  $page->param(reserve_id => $blockinfo->{reserve_id});
     909  my $newblock = NetAddr::IP->new($cidr->addr, $cidr->masklen - 1)->network;
     910  $page->param(newblock => $newblock);
     911
    906912  # not happy with the upside-down logic, but...
    907913  $page->param(swipable => $blockinfo->{type} !~ /.i/);
     
    961967  }
    962968
     969  # Merge with reserved freeblock
     970  $updargs{fbmerge} = $webvar{expandme} if $webvar{expandme};
     971
    963972  my ($code,$msg) = updateBlock($ip_dbh, %updargs, iprev => \%iprev);
    964973
  • trunk/templates/edit.tmpl

    r691 r695  
    2121<td class="regular"><TMPL_VAR NAME=block></td>
    2222</tr>
     23
     24<TMPL_IF reserve>
     25<tr>
     26<td class="heading">Expand allocation:</td>
     27<td>
     28<input type="checkbox" name="expandme" value="<TMPL_VAR NAME=reserve_id>">Add <TMPL_VAR NAME=reserve> to make <TMPL_VAR NAME=newblock>
     29<input type="hidden" name="newblock" value="<TMPL_VAR NAME=newblock>">
     30</td>
     31</tr>
     32</TMPL_IF>
    2333
    2434<tr>
Note: See TracChangeset for help on using the changeset viewer.