Changeset 78


Ignore:
Timestamp:
11/23/04 17:44:12 (20 years ago)
Author:
Kris Deugau
Message:

/trunk

Updated allocBlock() to properly handle and return errors
"Temporarily" copy-pasted initPool() code into allocBlock()

-> Depending on real functionality, this may become permanent. :(

File:
1 edited

Legend:

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

    r77 r78  
    2222@ISA            = qw(Exporter);
    2323@EXPORT_OK      = qw(&initIPDBGlocals &connectDB &finish &checkDBSanity &allocateBlock
    24                         &initPool &mailNotify);
     24                        &mailNotify);
    2525
    2626@EXPORT         = (); # Export nothing by default.
    27 %EXPORT_TAGS    = ( ALL => [qw( &initIPDBGlocals &connectDB &checkDBSanity
    28                                 &allocateBlock &initPool &mailNotify)]
     27%EXPORT_TAGS    = ( ALL => [qw( &initIPDBGlocals &connectDB &finish &checkDBSanity
     28                                &allocateBlock &mailNotify)]
    2929                  );
    3030
     
    136136  my $alloc_from = new NetAddr::IP $_[2];
    137137  my $sth;
     138  my $msg;      # To contain the error message, if any.
    138139
    139140  # Enable transactions and error handling
     
    164165    };
    165166    if ($@) {
    166       # failure
     167      $msg = $@;
     168      eval { $dbh->rollback; };
     169      return ('FAIL',$msg);
    167170    } else {
    168       # success
     171      return ('OK',"OK");
    169172    }
    170173
    171174  } else { # end IP-from-pool allocation
    172     # Set $cidr here as it may not be a valid IP address elsewhere.
    173 #    my $cidr = new NetAddr::IP $webvar{fullcidr};
    174175
    175176    if ($cidr == $alloc_from) {
     
    189190        } else {
    190191          # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
    191 ##err How to handle pools?
    192 ##workhere
    193 #print "IPDB.pm: $cidr\n";
    194192          $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'");
    195193          $sth->execute;
     
    199197                $cidr->masklen.",'$circid')");
    200198          $sth->execute;
    201 ## if $type =~ /^[cdsmw]p$/ initpool($dbh,$cidr,$type)
     199
     200          # And initialize the pool, if necessary
     201          if ($type =~ /^.p$/) {
     202            my ($pooltype) = ($type =~ /^(.)p$/);
     203            # have to insert all pool IPs into poolips table as "unallocated".
     204            $sth = $dbh->prepare("insert into poolips values ('$cidr',".
     205                " ?, '6750400', '$city', '$pooltype', 'y', '', '', '')");
     206            my @poolip_list = $cidr->hostenum;
     207            if ($type eq 'dp') { # (DSLish block - *all* IPs available
     208              $sth->execute($cidr->addr);
     209              for (my $i=0; $i<=$#poolip_list; $i++) {
     210                $sth->execute($poolip_list[$i]->addr);
     211              }
     212              $cidr--;
     213              $sth->execute($cidr->addr);
     214            } else { # (real netblock)
     215              for (my $i=1; $i<=$#poolip_list; $i++) {
     216                $sth->execute($poolip_list[$i]->addr);
     217              }
     218            }
     219          } # end type = pool
    202220        } # routing vs non-routing netblock
    203221        $dbh->commit;
    204       };  # end of eval
    205 ##err
     222      }; # end of eval
    206223      if ($@) {
     224        $msg = $@;
    207225        eval { $dbh->rollback; };
    208         return (2,$@);
     226        return ('FAIL',$@);
    209227      } else {
    210 ## do we return here or ?
    211         return (1,"OK");
    212         # Success
    213       } # error handler
     228        return ('OK',"OK");
     229      }
    214230
    215231    } else { # cidr != alloc_from
     
    268284                ",'$circid')");
    269285          $sth->execute;
    270 ##err do we initPool here, or force the caller to do it?
    271 # Flexibility says we let the caller do it, to allow more options.
     286
     287          # And initialize the pool, if necessary
     288          if ($type =~ /^.p$/) {
     289            my ($pooltype) = ($type =~ /^(.)p$/);
     290            # have to insert all pool IPs into poolips table as "unallocated".
     291            $sth = $dbh->prepare("insert into poolips values ('$cidr',".
     292                " ?, '6750400', '$city', '$pooltype', 'y', '', '', '')");
     293            my @poolip_list = $cidr->hostenum;
     294            if ($type eq 'dp') { # (DSLish block - *all* IPs available
     295              $sth->execute($cidr->addr);
     296              for (my $i=0; $i<=$#poolip_list; $i++) {
     297                $sth->execute($poolip_list[$i]->addr);
     298              }
     299              $cidr--;
     300              $sth->execute($cidr->addr);
     301            } else { # (real netblock)
     302              for (my $i=1; $i<=$#poolip_list; $i++) {
     303                $sth->execute($poolip_list[$i]->addr);
     304              }
     305            }
     306          } # end type = pool
    272307        } # done with netblock alloctype != rr
    273308        $dbh->commit;
    274309      }; # end eval
    275310      if ($@) {
     311        $msg = $@;
    276312        eval { $dbh->rollback; };
    277         return (2,$@);
     313        return ('FAIL',$msg);
    278314      } else {
    279 ##err
    280         return (1,"OK");
     315        return ('OK',"OK");
    281316      }
    282317
     
    293328# indicating whether the pool should allow allocation of literally every
    294329# IP, or if it should reserve network/gateway/broadcast IPs
     330# Note that this is NOT done in a transaction, that's why it's a private
     331# function and should ONLY EVER get called from allocateBlock()
    295332sub initPool {
    296333  my ($dbh,undef,$type,$city,$class) = @_;
    297334  my $pool = new NetAddr::IP $_[1];
    298335
    299   my $pooltype = ($type =~ /^(.)p$/);
     336  my ($pooltype) = ($type =~ /^(.)p$/);
    300337  my $sth;
    301338
    302   # Enable transactions and error handling
    303   local $dbh->{AutoCommit} = 0; # These need to be local so we don't
    304   local $dbh->{RaiseError} = 1; # step on our toes by accident.
    305 
    306   # Begin SQL transaction block
    307   eval {
    308     # have to insert all pool IPs into poolips table as "unallocated".
    309     $sth = $dbh->prepare("insert into poolips values ('$pool',".
     339  # have to insert all pool IPs into poolips table as "unallocated".
     340  $sth = $dbh->prepare("insert into poolips values ('$pool',".
    310341        " ?, '6750400', '$city', '$pooltype', 'y', '', '', '')");
    311     my @poolip_list = $pool->hostenum;
    312     if ($class) { # (real netblock)
    313       for (my $i=1; $i<=$#poolip_list; $i++) {
    314         $sth->execute($poolip_list[$i]->addr);
    315       }
    316     } else { # (DSL-ish block - *all* IPs available
    317       $sth->execute($pool->addr);
    318       for (my $i=0; $i<=$#poolip_list; $i++) {
    319         $sth->execute($poolip_list[$i]->addr);
    320       }
    321       $pool--;
    322       $sth->execute($pool->addr);
     342  my @poolip_list = $pool->hostenum;
     343  if ($class eq 'all') { # (DSL-ish block - *all* IPs available
     344    $sth->execute($pool->addr);
     345    for (my $i=0; $i<=$#poolip_list; $i++) {
     346      $sth->execute($poolip_list[$i]->addr);
    323347    }
    324     $dbh->commit;
    325   }; # end eval
    326   if ($@) {
    327     eval { $dbh->rollback; };
    328 print "$@\n";
    329     return (2,"$@");
    330   } else {
    331 ##err
    332     return (1,"OK");
     348    $pool--;
     349    $sth->execute($pool->addr);
     350  } else { # (real netblock)
     351    for (my $i=1; $i<=$#poolip_list; $i++) {
     352      $sth->execute($poolip_list[$i]->addr);
     353    }
    333354  }
    334 
    335355} # end initPool()
    336356
Note: See TracChangeset for help on using the changeset viewer.