Changeset 533 for trunk/cgi-bin/IPDB.pm


Ignore:
Timestamp:
10/31/12 15:47:53 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Clean up and move SQL for finding a free block to allocate from
into IPDB.pm. See #34.
Also fix a minor "Use of uninitialized value...".

File:
1 edited

Legend:

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

    r532 r533  
    2828        &addMaster
    2929        &listSummary &listMaster &listRBlock &listFree &listPool
    30         &getTypeList &getPoolSelect
     30        &getTypeList &getPoolSelect &findAllocateFrom
    3131        &getParent &getRoutedCity
    3232        &allocateBlock &updateBlock &deleteBlock &getBlockData
     
    4242                &addMaster
    4343                &listSummary &listMaster &listRBlock &listFree &listPool
    44                 &getTypeList &getPoolSelect
     44                &getTypeList &getPoolSelect &findAllocateFrom
    4545                &getParent &getRoutedCity
    4646                &allocateBlock &updateBlock &deleteBlock &getBlockData
     
    501501## IPDB::getPoolSelect()
    502502# Get a list of pools matching the passed city and type that have 1 or more free IPs
     503# Returns an arrayref to a list of hashrefs
    503504sub getPoolSelect {
    504505  my $dbh = shift;
     
    518519  return $plist;
    519520} # end getPoolSelect()
     521
     522
     523## IPDB::findAllocateFrom()
     524# Find free block to add a new allocation from.  (CIDR block version of pool select above, more or less)
     525# Takes
     526#  - mask length
     527#  - allocation type
     528#  - POP city "parent"
     529#  - optional master-block restriction
     530#  - optional flag to allow automatic pick-from-private-network-ranges
     531# Returns a string with the first CIDR block matching the criteria, if any
     532sub findAllocateFrom {
     533  my $dbh = shift;
     534  my $maskbits = shift;
     535  my $type = shift;
     536  my $city = shift;
     537  my $pop = shift;
     538  my %optargs = @_;
     539
     540  my $failmsg = "No suitable free block found\n";
     541
     542## Set up the SQL to find out what freeblock we can (probably) use for an allocation.
     543## Very large systems will require development of a reserve system (possibly an extension
     544## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?)
     545## Also populate a value list for the DBI call.
     546
     547  my @vallist = ($maskbits, ($type eq 'rm' ? 'n' : ($type =~ /^(.)r$/ ? "$1" : 'y')) );
     548  my $sql = "SELECT cidr FROM freeblocks WHERE maskbits <= ? AND routed = ?";
     549
     550  # for PPP(oE) and container types, the POP city is the one attached to the pool.
     551  # individual allocations get listed with the customer city site.
     552  ##fixme:  chain cities to align roughly with a full layer-2 node graph
     553  $city = $pop if $type !~ /^.[pc]$/;
     554  if ($type ne 'rm') {
     555    $sql .= " AND city = ?";
     556    push @vallist, $city;
     557  }
     558  # if a specific master was requested, allow the requestor to self->shoot(foot)
     559  if ($optargs{master} && $optargs{master} ne '-') {
     560    $sql .= " AND cidr <<= ?" if $optargs{master} ne '-';
     561    push @vallist, $optargs{master};
     562  } else {
     563    # if a specific master was NOT requested, filter out the RFC 1918 private networks
     564    if (!$optargs{allowpriv}) {
     565      $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')";
     566    }
     567  }
     568  # Sorting and limiting, since we don't (currently) care to provide a selection of
     569  # blocks to carve up.  This preserves something resembling optimal usage of the IP
     570  # space by forcing contiguous allocations and free blocks as much as possible.
     571  $sql .= " ORDER BY maskbits DESC,cidr LIMIT 1";
     572
     573  my ($fbfound) = $dbh->selectrow_array($sql, undef, @vallist);
     574  return $fbfound;
     575} # end findAllocateFrom()
    520576
    521577
Note: See TracChangeset for help on using the changeset viewer.