Cover V12, i12
dec2003.tar

Listing 1 ipassign.php

<?
/*START IP ALLOCATION SECTION*/
foreach ($Ranges as $Key => $Value) {
    if ($IPMode == $Key){
        $DefinedRange = TRUE;
        break;
    }
}

if ($DefinedRange == TRUE){
    //The option is in the ranges array
    $ThisRange = $Ranges[$IPMode];
    if ($IPNet!="" or $IPSubNet!="" or $IPNode!="") {
        echo "<BR><BR><font color=\"#FF0000\"><b>Error: Can't select $ThisRange->Label mode \
          of ip allocation and specify manual IP. Record not added</b></font><BR><BR>";
        exit;
    }
    if ($ThisRange->SubSize == 1){
    // Only one subnet, get the next free IP for this range
    $SybaseQuery="SELECT min(IP) FROM $gSybaseDB..FreeIP WHERE IP not in 
    (SELECT IPNode from $gSybaseDB..$gSybaseTable where IPSubNet=".$ThisRange->Subnets[0]." \
            and DateOut=null) and IP >= $ThisRange->NodeLower and IP <= $ThisRange->NodeUpper";
    $SybaseResult=sybase_query($SybaseQuery,$SybaseLink);
    $MyRow=sybase_fetch_row($SybaseResult);
    list($a,$IP)=each($MyRow);
    if ($IP=="") {
            echo "<BR><BR><font color=\"#FF0000\"><b>Severe error: no more free ips in the \
              specified range. Abort</b></font><BR><BR>";
            exit;
        }
    $IPSN = $ThisRange->Subnets[0];
    }
    else { //Range spans multiple subnets
        //look for ips in the first subnet
        $SybaseQuery="SELECT min(IP)FROM $gSybaseDB..FreeIP WHERE IP not in 
        (SELECT IPNode from $gSybaseDB..$gSybaseTable where IPSubNet=".$ThisRange->Subnets[0]." \
          and DateOut=null) and IP >= $ThisRange->NodeLower";
        $SybaseResult=sybase_query($SybaseQuery,$SybaseLink);
        $MyRow=sybase_fetch_row($SybaseResult);
        list($a,$IP)=each($MyRow);
        if ($IP=="") {
            //No ips in the first subnet, cycle through the rest
            for ($i=1; $i < $ThisRange->SubSize - 1; $i++){
                $SybaseQuery="SELECT min(IP)FROM $gSybaseDB..FreeIP WHERE IP not in 
                (SELECT IPNode from $gSybaseDB..$gSybaseTable where \
                  IPSubNet=".$ThisRange->Subnets[$i]." and DateOut=null)";
                $SybaseResult=sybase_query($SybaseQuery,$SybaseLink);
                $MyRow=sybase_fetch_row($SybaseResult);
                list($a,$IP)=each($MyRow);
                if ($IP!="") { 
                    //Valid IP has been retrieved
                    $IPSN = $ThisRange->Subnets[$i];
                    break;
                }         
            }
            if ($IP=="") {
                //IP still not allocated, try last subnet
                $SybaseQuery="SELECT min(IP)FROM $gSybaseDB..FreeIP WHERE IP not in 
                (SELECT IPNode from $gSybaseDB..$gSybaseTable where \
                  IPSubNet=".$ThisRange->Subnets[$ThisRange->SubSize-1]." and DateOut=null) and
                IP <= $ThisRange->NodeUpper";
                $SybaseResult=sybase_query($SybaseQuery,$SybaseLink);
                $MyRow=sybase_fetch_row($SybaseResult);
                list($a,$IP)=each($MyRow);
                if ($IP=="") {
                //No IP able to be allocated. error.
                echo "<BR><BR><font color=\"#FF0000\"><b>Severe error: no more free ips in \
                  the specified range. Abort</b></font><BR><BR>";
                exit;
                }
                $IPSN = $ThisRange->Subnets[$ThisRange->SubSize-1];
            }    
        }
        else {
            $IPSN = $ThisRange->Subnets[0];
        }       
    }
    $IPNet = $gDefaultIPNet;
}
elseif ($IPMode=="Manual") {
    // Manual IP allocation
    echo "<BR><BR>IPMode Manual<BR><BR>";
    if ($IPNet=="" or $IPSubNet=="" or $IPNode=="") {
        // Allow blank IP, may be an non-ip connected device
    } 
    else {
        //  ---  Does this IP address already exist? Duplicates allowed, only warn
        $SybaseQuery="SELECT IPNode FROM $gSybaseDB..$gSybaseTable WHERE IPNet=\"$IPNet\" and \
          IPSubNet=$IPSubNet and IPNode=$IPNode and DateOut=null";
        $SybaseResult = sybase_query($SybaseQuery, $SybaseLink);
        $MyRow=sybase_fetch_row($SybaseResult);
        list($a,$count)=each($MyRow);
        if ($count!="") {
            echo "<BR><BR>Warning: The IP address $IPNet.$IPSubNet.$IPNode is already in use. \
              The record <b>HAS</b> been assigned<BR><BR>";
        }
    }
    $IPSN=$IPSubNet;
    $IP=$IPNode;
}
else {
        //The allocation is neither manual or in the range array i.e. error.
        echo "<BR><BR>Unknown IP allocation mode $IPMode. Record not added<BR><BR>";
        exit;
}
echo "<BR><BR>Allocating $IPNet.$IPSN.$IP<BR><BR>";

/*END IP ALLOCATION SECTION*/
?>