Example #1
0
/**
 * Get first available IP address
 */
function getFirstAvailableIPAddress($subnetId)
{
    global $db;
    # get variables from config file
    $database = new database($db['host'], $db['user'], $db['pass'], $db['name']);
    /* get all ip addresses in subnet */
    $query = 'SELECT `ip_addr` from `ipaddresses` where `subnetId` = "' . $subnetId . '" order by `ip_addr` ASC;';
    /* execute */
    try {
        $ipAddresses = $database->getArray($query);
    } catch (Exception $e) {
        $error = $e->getMessage();
        print "<div class='alert alert-error'>" . _('Error') . ":{$error}</div>";
        return false;
    }
    /* get subnet */
    $query = 'SELECT `subnet`,`mask` from `subnets` where `id` = "' . $subnetId . '";';
    $subnet2 = $database->getArray($query);
    $subnet = $subnet2[0]['subnet'];
    $mask = $subnet2[0]['mask'];
    /* create array of IP addresses */
    $ipaddressArray[] = $subnet;
    foreach ($ipAddresses as $ipaddress) {
        $ipaddressArray[] = $ipaddress['ip_addr'];
    }
    //get array size
    $size = sizeof($ipaddressArray);
    $curr = 0;
    //get type
    $type = IdentifyAddress($subnet);
    //if subnet is /32
    if ($mask == "32" && $type == "IPv4") {
        if ($size == 1) {
            $firstAvailable = $ipaddressArray[0];
        } else {
            $firstAvailable = false;
        }
    } else {
        if ($mask == "31" && $type == "IPv4") {
            if ($size == 1) {
                $firstAvailable = $ipaddressArray[0];
            } else {
                if ($size == 2) {
                    $delta = $ipaddressArray[1] - $ipaddressArray[0];
                    if ($delta == 1) {
                        $firstAvailable = $ipaddressArray[0];
                    } else {
                        $firstAvailable = gmp_strval(gmp_add($ipaddressArray[0], 1));
                    }
                } else {
                    $firstAvailable = false;
                }
            }
        } else {
            if ($mask == "128" && $type == "IPv6") {
                if ($size == 1) {
                    $firstAvailable = $ipaddressArray[0];
                } else {
                    $firstAvailable = false;
                }
            } else {
                if ($mask == "127" && $type == "IPv6") {
                    if ($size == 1) {
                        $firstAvailable = $ipaddressArray[0];
                    } else {
                        if ($size == 2) {
                            $delta = $ipaddressArray[1] - $ipaddressArray[0];
                            if ($delta == 1) {
                                $firstAvailable = $ipaddressArray[0];
                            } else {
                                $firstAvailable = gmp_strval(gmp_add($ipaddressArray[0], 1));
                            }
                        } else {
                            $firstAvailable = false;
                        }
                    }
                } else {
                    if ($size == 1) {
                        $firstAvailable = gmp_strval(gmp_add($ipaddressArray[0], 1));
                    } else {
                        //get first change -> delta > 1
                        for ($m = 1; $m <= $size - 1; $m++) {
                            $delta = gmp_strval(gmp_sub($ipaddressArray[$m], $ipaddressArray[$m - 1]));
                            //compare with previous
                            if ($delta != 1) {
                                $firstAvailable = gmp_strval(gmp_add($ipaddressArray[$m - 1], 1));
                                $m = $size;
                            } else {
                                $firstAvailable = gmp_strval(gmp_add($ipaddressArray[$m], 1));
                            }
                        }
                        //if bcast ignore!
                        if ($type == "IPv4") {
                            require_once 'PEAR/Net/IPv4.php';
                            $Net_IPv4 = new Net_IPv4();
                            $net = $Net_IPv4->parseAddress(transform2long($subnet) . "/" . $mask);
                            if ($net->broadcast == transform2long($firstAvailable)) {
                                $firstAvailable = false;
                            }
                        } else {
                            if ($type == "IPv6") {
                                require_once 'PEAR/Net/IPv6.php';
                                $Net_IPv6 = new Net_IPv6();
                                $net = $Net_IPv6->parseAddress(transform2long($subnet) . "/" . $mask);
                                if ($net->broadcast == transform2long($firstAvailable)) {
                                    $firstAvailable = false;
                                }
                            } else {
                                $firstAvailable = gmp_strval(gmp_add($ipaddressArray[$size - 1], 1));
                            }
                        }
                    }
                }
            }
        }
    }
    /* return first available IP address */
    return $firstAvailable;
}