Changeset 428 for trunk


Ignore:
Timestamp:
07/08/10 18:07:09 (14 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint - this change still needs testing
Trim reference to ViaNet IP ranges in IPDB.pm (see #26) by

generalizing netblock-in-DSL-pool deallocation (part of #6)

Note the diff is larger than the real change due to whitespace shuffling

File:
1 edited

Legend:

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

    r426 r428  
    721721      } # end alloctype general case
    722722
    723 ##TEMP
    724 ## Temporary wrapper to "properly" deallocate sIP PPPoE/DSL "netblocks" in 209.91.185.0/24
    725 ## Note that we should really general-case this.
    726 my $staticpool = new NetAddr::IP "209.91.185.0/24";
    727 ##TEMP
    728 if ($cidr->within($staticpool)) {
    729 ##TEMP
    730   # We've already deleted the block, now we have to stuff its IPs into the pool.
    731   my $sth2 = $dbh->prepare("insert into poolips values ('209.91.185.0/24',?,'$defcustid','Sudbury','di','y','','','')");
    732   $sth2->execute($cidr->addr);
    733   foreach my $ip ($cidr->hostenum) {
    734     $sth2->execute("$ip");
    735   }
    736   $cidr--;
    737   $sth2->execute($cidr->addr);
    738 
    739 ##TEMP
    740 } else {
    741 ##TEMP
    742 
    743       # Now we look for larger-or-equal-sized free blocks in the same master (routed)
    744       # (super)block. If there aren't any, we can't combine blocks anyway.  If there
    745       # are, we check to see if we can combine blocks.
    746       # Execute the statement prepared in the if-else above.
    747 
    748       $sth->execute;
     723      ## Deallocate legacy blocks stashed in the middle of a DSL static IP pool
     724      ## This may be expandable to an even more general case of contained netblock, or other pool types.
     725
     726      # Find out if the block we're deallocating is within a DSL pool
     727      my $sth2 = $dbh->prepare("SELECT cidr,city,type FROM allocations WHERE type LIKE '_p' AND cidr >>= ?");
     728      $sth2->execute("$cidr");
     729      my ($pool,$poolcity,$pooltype) = $sth2->fetchrow_array;
     730
     731      if ($pool || $sth2->rows) {
     732        # We've already deleted the block, now we have to stuff its IPs into the pool.
     733        $pooltype =~ s/p$/i/;   # change type to static IP
     734        $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) values ".
     735                "('$pool',?,'$poolcity','$pooltype','$defcustid')");
     736        # don't insert .0
     737##fixme:  need to not insert .1 on "real netblock" pools (DHCPish)
     738        $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
     739        foreach my $ip ($cidr->hostenum) {
     740          $sth2->execute("$ip");
     741        }
     742        $cidr--;
     743        # don't insert .255
     744        $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
     745      } else {  # done returning IPs from a block to a static DSL pool
     746
     747        # Now we look for larger-or-equal-sized free blocks in the same master (routed)
     748        # (super)block. If there aren't any, we can't combine blocks anyway.  If there
     749        # are, we check to see if we can combine blocks.
     750        # Execute the statement prepared in the if-else above.
     751
     752        $sth->execute;
    749753
    750754# NetAddr::IP->compact() attempts to produce the smallest inclusive block
     
    756760#       $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
    757761
    758       my (@together, @combinelist);
    759       my $i=0;
    760       while (my @data = $sth->fetchrow_array) {
    761         my $testIP = new NetAddr::IP $data[0];
    762         @together = $testIP->compact($cidr);
    763         my $num = @together;
    764         if ($num == 1) {
    765           $cidr = $together[0];
    766           $combinelist[$i++] = $testIP;
     762        my (@together, @combinelist);
     763        my $i=0;
     764        while (my @data = $sth->fetchrow_array) {
     765          my $testIP = new NetAddr::IP $data[0];
     766          @together = $testIP->compact($cidr);
     767          my $num = @together;
     768          if ($num == 1) {
     769            $cidr = $together[0];
     770            $combinelist[$i++] = $testIP;
     771          }
    767772        }
    768       }
    769 
    770       # Clear old freeblocks entries - if any.  They should all be within
    771       # the $cidr determined above.
    772       $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
    773       $sth->execute;
    774 
    775       # insert "new" freeblocks entry
    776       if ($type eq 'rm') {
    777         $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
     773
     774        # Clear old freeblocks entries - if any.  They should all be within
     775        # the $cidr determined above.
     776        $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'");
     777        $sth->execute;
     778
     779        # insert "new" freeblocks entry
     780        if ($type eq 'rm') {
     781          $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)".
    778782                " values ('$cidr',".$cidr->masklen.",'<NULL>')");
    779       } else {
    780         # Magic hackery to insert "correct" data for deallocation of
    781         # non-contained blocks allocated from within a container.
    782         $type = 'pr' if $con_type && $con_type eq 'pc';
    783 
    784         $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
     783        } else {
     784          # Magic hackery to insert "correct" data for deallocation of
     785          # non-contained blocks allocated from within a container.
     786          $type = 'pr' if $con_type && $con_type eq 'pc';
     787
     788          $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
    785789                " values ('$cidr',".$cidr->masklen.
    786790                ",(select city from routed where cidr >>= '$cidr'),'".
    787791                (($type =~ /^(.)r$/) ? "$1" : 'y')."')");
    788       }
    789       $sth->execute;
    790 
    791 ##TEMP
    792 }
    793 ##TEMP
    794 
     792        }
     793        $sth->execute;
     794
     795      } # done returning IPs to the appropriate place
     796
     797die "blah, missed the case\n";
    795798      # If we got here, we've succeeded.  Whew!
    796799      $dbh->commit;
    797800    }; # end eval
    798801    if ($@) {
     802      $msg = $@;
    799803      eval { $dbh->rollback; };
    800804      return ('FAIL', $msg);
Note: See TracChangeset for help on using the changeset viewer.