Changeset 533


Ignore:
Timestamp:
10/31/12 15:47:53 (11 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...".

Location:
trunk/cgi-bin
Files:
2 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
  • trunk/cgi-bin/main.cgi

    r532 r533  
    405405  } else { # end show pool options
    406406
    407 ##fixme: uninitialized
    408     if ($webvar{fbassign} eq 'y') {
     407    if ($webvar{fbassign} && $webvar{fbassign} eq 'y') {
    409408      $cidr = new NetAddr::IP $webvar{block};
    410409      $webvar{maskbits} = $cidr->masklen;
     
    415414        return;
    416415      }
    417       my $sql;
    418       my $city;
    419       my $failmsg;
    420       my $extracond = '';
    421       if ($webvar{allocfrom} eq '-') {
    422 ##fixme: uninitialized
    423         $extracond = ($webvar{allowpriv} eq 'on' ? '' :
    424                 " and not (cidr <<= '192.168.0.0/16'".
    425                         " or cidr <<= '10.0.0.0/8'".
    426                         " or cidr <<= '172.16.0.0/12')");
    427       }
    428       my $sortorder;
     416
     417##fixme ick, ew, bleh.  gotta handle the failure message generation better.  push it into findAllocateFrom()?
     418      my $failmsg = "No suitable free block found.<br>\n";
    429419      if ($webvar{alloctype} eq 'rm') {
    430         if ($webvar{allocfrom} ne '-') {
    431           $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
    432                 " and cidr <<= '$webvar{allocfrom}'";
    433           $sortorder = "maskbits desc";
    434         } else {
    435           $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'";
    436           $sortorder = "maskbits desc";
    437         }
    438         $failmsg = "No suitable free block found.<br>\nWe do not have a free".
    439           " routeable block of that size.<br>\nYou will have to either route".
    440           " a set of smaller netblocks or a single smaller netblock.";
     420        $failmsg .= "We do not have a free routeable block of that size.<br>\n".
     421                "You will have to either route a set of smaller netblocks or a single smaller netblock.";
    441422      } else {
    442 ##fixme
    443 # This section needs serious Pondering.
    444         # Pools of most types get assigned to the POP they're "routed from"
    445         # This includes WAN blocks and other netblock "containers"
    446         # This does NOT include cable pools.
    447423        if ($webvar{alloctype} =~ /^.[pc]$/) {
    448           $city = $webvar{city};
    449           $failmsg = "No suitable free block found.<br>\nYou will have to route another".
    450             " superblock from one of the<br>\nmaster blocks or chose a smaller".
    451             " block size for the pool.";
     424          $failmsg .= "You will have to route another superblock from one of the<br>\n".
     425                "master blocks or chose a smaller block size for the pool.";
    452426        } else {
    453427          if (!$webvar{pop}) {
     
    455429            return;
    456430          }
    457           $city = $webvar{pop};
    458           $failmsg = "No suitable free block found.<br>\nYou will have to route another".
    459             " superblock to $webvar{pop}<br>\nfrom one of the master blocks or".
    460             " chose a smaller blocksize.";
    461         }
    462         if (defined $webvar{allocfrom} && $webvar{allocfrom} ne '-') {
    463           $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
    464                 " and cidr <<= '$webvar{allocfrom}' and routed='".
    465                 (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
    466           $sortorder = "maskbits desc,cidr";
    467         } else {
    468           $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
    469                 " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."'";
    470           $sortorder = "maskbits desc,cidr";
     431          $failmsg .= "You will have to route another superblock to $webvar{pop}<br>\n".
     432                "from one of the master blocks or chose a smaller blocksize.";
    471433        }
    472434      }
    473       $sql = $sql.$extracond." order by ".$sortorder;
    474       $sth = $ip_dbh->prepare($sql);
    475       $sth->execute;
    476       my @data = $sth->fetchrow_array();
    477       if ($data[0] eq "") {
     435
     436      $cidr = findAllocateFrom($ip_dbh, $webvar{maskbits}, $webvar{alloctype}, $webvar{city}, $webvar{pop},
     437        (master => $webvar{allocfrom}, allowpriv => $webvar{allowpriv}) );
     438      if (!$cidr) {
    478439        $page->param(err => $failmsg);
    479440        return;
    480441      }
    481       $cidr = new NetAddr::IP $data[0];
     442      $cidr = new NetAddr::IP $cidr;
    482443    } # check for freeblocks assignment or IPDB-controlled assignment
    483444
Note: See TracChangeset for help on using the changeset viewer.