Esempio n. 1
0
function findPoolID($ip)
{
    global $pro_mysql_ip_pool_table;
    $ip_calc = new Net_IPv4();
    $ip_calc2 = new Net_IPv4();
    $q = "SELECT * FROM {$pro_mysql_ip_pool_table} WHERE 1;";
    $r = mysql_query($q) or die("Cannot query {$q} line " . __LINE__ . " file " . __FILE__ . " sql said: " . mysql_error());
    $n = mysql_num_rows($r);
    for ($i = 0; $i < $n; $i++) {
        $a = mysql_fetch_array($r);
        $ip_calc->ip = $a["ip_addr"];
        $ip_calc->netmask = $a["netmask"];
        $ret = $ip_calc->calculate();
        if (!is_object($ret)) {
            $ip_calc2->ip = $ip;
            $ip_calc2->netmask = $a["netmask"];
            $ip_calc2->calculate();
            $ret2 = $ip_calc2->calculate();
            if (!is_object($ret2)) {
                if ($ip_calc->network == $ip_calc2->network) {
                    return $a["id"];
                }
            }
        } else {
            echo "Error for IP pool " . $a["id"] . " please check IP and netmask.";
        }
    }
    return 0;
}
 /**
  * Parse a formatted IP address
  *
  * Given a network qualified IP address, attempt to parse out the parts
  * and calculate qualities of the address.
  *
  * The following formats are possible:
  *
  * [dot quad ip]/[ bitmask ]
  * [dot quad ip]/[ dot quad netmask ]
  * [dot quad ip]/[ hex string netmask ]
  *
  * The first would be [IP Address]/[BitMask]:
  * 192.168.0.0/16
  *
  * The second would be [IP Address] [Subnet Mask in quad dot notation]:
  * 192.168.0.0/255.255.0.0
  *
  * The third would be [IP Address] [Subnet Mask as Hex string]
  * 192.168.0.0/ffff0000
  *
  * Usage:
  *
  * $cidr = '192.168.0.50/16';
  * $net = Net_IPv4::parseAddress($cidr);
  * echo $net->network; // 192.168.0.0
  * echo $net->ip; // 192.168.0.50
  * echo $net->broadcast; // 192.168.255.255
  * echo $net->bitmask; // 16
  * echo $net->long; // 3232235520 (long/double version of 192.168.0.50)
  * echo $net->netmask; // 255.255.0.0
  *
  * @param  string $ip IP address netmask combination
  * @return object     true if syntax is valid, otherwise false
  */
 function parseAddress($address)
 {
     $myself = new Net_IPv4();
     if (strchr($address, "/")) {
         $parts = explode("/", $address);
         if (!$myself->validateIP($parts[0])) {
             return PEAR::raiseError("invalid IP address");
         }
         $myself->ip = $parts[0];
         // Check the style of netmask that was entered
         /*
          *  a hexadecimal string was entered
          */
         if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\$", $parts[1], $regs)) {
             // hexadecimal string
             $myself->netmask = hexdec($regs[1]) . "." . hexdec($regs[2]) . "." . hexdec($regs[3]) . "." . hexdec($regs[4]);
             /*
              *  a standard dot quad netmask was entered.
              */
         } else {
             if (strchr($parts[1], ".")) {
                 if (!$myself->validateNetmask($parts[1])) {
                     return PEAR::raiseError("invalid netmask value");
                 }
                 $myself->netmask = $parts[1];
                 /*
                  *  a CIDR bitmask type was entered
                  */
             } else {
                 if ($parts[1] >= 0 && $parts[1] <= 32) {
                     // bitmask was entered
                     $myself->bitmask = $parts[1];
                     /*
                      *  Some unknown format of netmask was entered
                      */
                 } else {
                     return PEAR::raiseError("invalid netmask value");
                 }
             }
         }
         $myself->calculate();
         return $myself;
     } else {
         if ($myself->validateIP($address)) {
             $myself->ip = $address;
             return $myself;
         } else {
             return PEAR::raiseError("invalid IP address");
         }
     }
 }
Esempio n. 3
0
function iprange($ip)
{
    $array = array();
    list($ip, $netmask) = explode('/', $ip, 2);
    // create IPv4 object
    $ip_calc = new Net_IPv4();
    // set variables
    $ip_calc->ip = $ip;
    $ip_calc->bitmask = $netmask;
    $error = $ip_calc->calculate();
    if (is_object($error)) {
        echo "An error occured: " . $error->getMessage();
    }
    $curr = ip2long($ip_calc->network) + 1;
    while ($curr < ip2long($ip_calc->broadcast)) {
        array_push($array, long2ip($curr));
        $curr += 1;
    }
    return $array;
}