コード例 #1
0
ファイル: functions-tools.php プロジェクト: retexica/phpipam
/**
 * ipCalc calculations
 */
function calculateIpCalcResult($cidr)
{
    /* first verify address type */
    $type = IdentifyAddress($cidr);
    /* IPv4 */
    if ($type == "IPv4") {
        $net = Net_IPv4::parseAddress($cidr);
        //set ip address type
        $out['Type'] = 'IPv4';
        //calculate network details
        $out['IP address'] = $net->ip;
        // 192.168.0.50
        $out['Network'] = $net->network;
        // 192.168.0.0
        $out['Broadcast'] = $net->broadcast;
        // 192.168.255.255
        $out['Subnet bitmask'] = $net->bitmask;
        // 16
        $out['Subnet netmask'] = $net->netmask;
        // 255.255.0.0
        $out['Subnet wildcard'] = long2ip(~ip2long($net->netmask));
        //0.0.255.255
        //calculate min/max IP address
        $out['Min host IP'] = long2ip(ip2long($out['Network']) + 1);
        $out['Max host IP'] = long2ip(ip2long($out['Broadcast']) - 1);
        $out['Number of hosts'] = ip2long($out['Broadcast']) - ip2long($out['Min host IP']);
        //subnet class
        $out['Subnet Class'] = checkIpv4AddressType($out['Network'], $out['Broadcast']);
        //if IP == subnet clear the Host fields
        if ($out['IP address'] == $out['Network']) {
            $out['IP address'] = "/";
        }
    } else {
        //set ip address type
        $out['Type'] = 'IPv6';
        //calculate network details
        /*         $out['Host address']              = Net_IPv6::removeNetmaskSpec ( $cidr );  */
        $out['Host address'] = $cidr;
        $out['Host address'] = Net_IPv6::compress($out['Host address'], 1);
        $out['Host address (uncompressed)'] = Net_IPv6::uncompress($out['Host address']);
        $mask = Net_IPv6::getNetmaskSpec($cidr);
        $subnet = Net_IPv6::getNetmask($cidr);
        $out['Subnet prefix'] = Net_IPv6::compress($subnet) . '/' . $mask;
        $out['Prefix length'] = Net_IPv6::getNetmaskSpec($cidr);
        // get reverse DNS entries
        $out['Host Reverse DNS'] = calculateReverseDNS6($out['Host address (uncompressed)']);
        $out['Subnet Reverse DNS'] = calculateReverseDNS6($subnet, $mask);
        //if IP == subnet clear the Host fields and Host Reverse DNS
        if ($out['Host address'] == $out['Subnet prefix']) {
            $out['Host address'] = '/';
            $out['Host address (uncompressed)'] = '/';
            unset($out['Host Reverse DNS']);
        }
        //min / max hosts
        $maxIp = gmp_strval(gmp_add(gmp_sub(gmp_pow(2, 128 - $mask), 1), ip2long6($subnet)));
        $out['Min host IP'] = long2ip6(gmp_strval(gmp_add(ip2long6($subnet), 1)));
        $out['Max host IP'] = long2ip6($maxIp);
        $out['Number of hosts'] = MaxHosts($mask, 1);
        //address type
        $out['Address type'] = Net_IPv6::getAddressType($cidr);
        $out['Address type'] = checkIpv6AddressType($out['Address type']);
    }
    /* return results */
    return $out;
}
コード例 #2
0
function is_ipv6_valid($ipv6_address, $ipv6_prefixlen = NULL)
{
    if (strpos($ipv6_address, '/') !== FALSE) {
        list($ipv6_address, $ipv6_prefixlen) = explode('/', $ipv6_address);
    }
    // False if prefix less or equal 0 and more 128
    if (is_numeric($ipv6_prefixlen) && ($ipv6_prefixlen < '0' || $ipv6_prefixlen > '128')) {
        return FALSE;
    }
    // False if invalid IPv6 syntax
    if (!Net_IPv6::checkIPv6($ipv6_address)) {
        return FALSE;
    }
    $ipv6_type = Net_IPv6::getAddressType($ipv6_address);
    // False if link-local
    if ($ipv6_type == NET_IPV6_LOCAL_LINK || $ipv6_type == NET_IPV6_UNSPECIFIED) {
        return FALSE;
    }
    return TRUE;
}
コード例 #3
0
ファイル: functions.inc.php プロジェクト: REAP720801/librenms
function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen, $ipv6_origin)
{
    global $device, $config;
    $ipv6_network = Net_IPv6::getNetmask("{$ipv6_address}/{$ipv6_prefixlen}") . '/' . $ipv6_prefixlen;
    $ipv6_compressed = Net_IPv6::compress($ipv6_address);
    if (Net_IPv6::getAddressType($ipv6_address) == NET_IPV6_LOCAL_LINK) {
        # ignore link-locals (coming from IPV6-MIB)
        return;
    }
    if (dbFetchCell("SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?", array($device['device_id'], $ifIndex)) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') {
        $port_id = dbFetchCell("SELECT port_id FROM `ports` WHERE device_id = ? AND ifIndex = ?", array($device['device_id'], $ifIndex));
        if (dbFetchCell("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ?", array($ipv6_network)) < '1') {
            dbInsert(array('ipv6_network' => $ipv6_network), 'ipv6_networks');
            echo "N";
        }
        // Below looks like a duplicate of the above FIXME
        if (dbFetchCell("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ?", array($ipv6_network)) < '1') {
            dbInsert(array('ipv6_network' => $ipv6_network), 'ipv6_networks');
            echo "N";
        }
        $ipv6_network_id = dbFetchCell("SELECT `ipv6_network_id` FROM `ipv6_networks` WHERE `ipv6_network` = ?", array($ipv6_network));
        if (dbFetchCell("SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ?", array($ipv6_address, $ipv6_prefixlen, $port_id)) == '0') {
            dbInsert(array('ipv6_address' => $ipv6_address, 'ipv6_compressed' => $ipv6_compressed, 'ipv6_prefixlen' => $ipv6_prefixlen, 'ipv6_origin' => $ipv6_origin, 'ipv6_network_id' => $ipv6_network_id, 'port_id' => $port_id), 'ipv6_addresses');
            echo "+";
        } else {
            echo ".";
        }
        $full_address = "{$ipv6_address}/{$ipv6_prefixlen}";
        $valid_address = $full_address . "-" . $port_id;
        $valid['ipv6'][$valid_address] = 1;
    }
}
コード例 #4
0
ファイル: functions.inc.php プロジェクト: arrmo/librenms
function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen, $ipv6_origin, $context_name = '')
{
    global $device;
    $ipv6_network = Net_IPv6::getNetmask("{$ipv6_address}/{$ipv6_prefixlen}") . '/' . $ipv6_prefixlen;
    $ipv6_compressed = Net_IPv6::compress($ipv6_address);
    if (Net_IPv6::getAddressType($ipv6_address) == NET_IPV6_LOCAL_LINK) {
        // ignore link-locals (coming from IPV6-MIB)
        return;
    }
    if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') {
        $port_id = dbFetchCell('SELECT port_id FROM `ports` WHERE device_id = ? AND ifIndex = ?', array($device['device_id'], $ifIndex));
        if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = ?', array($ipv6_network)) < '1') {
            dbInsert(array('ipv6_network' => $ipv6_network, 'context_name' => $context_name), 'ipv6_networks');
            echo 'N';
        } else {
            //Update Context
            dbUpdate(array('context_name' => $device['context_name']), 'ipv6_network', '`ipv6_network` = ?', array($ipv6_network));
            echo 'n';
        }
        $ipv6_network_id = dbFetchCell('SELECT `ipv6_network_id` FROM `ipv6_networks` WHERE `ipv6_network` = ? AND `context_name` = ?', array($ipv6_network, $context_name));
        if (dbFetchCell('SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ?', array($ipv6_address, $ipv6_prefixlen, $port_id)) == '0') {
            dbInsert(array('ipv6_address' => $ipv6_address, 'ipv6_compressed' => $ipv6_compressed, 'ipv6_prefixlen' => $ipv6_prefixlen, 'ipv6_origin' => $ipv6_origin, 'ipv6_network_id' => $ipv6_network_id, 'port_id' => $port_id, 'context_name' => $context_name), 'ipv6_addresses');
            echo '+';
        } else {
            //Update Context
            dbUpdate(array('context_name' => $device['context_name']), 'ipv6_address', '`ipv6_address` = ? AND `ipv6_prefixlen` = ? AND `port_id` = ?', array($ipv6_address, $ipv6_prefixlen, $port_id));
            echo '.';
        }
        $full_address = "{$ipv6_address}/{$ipv6_prefixlen}";
        $valid_address = $full_address . '-' . $port_id;
        $valid['ipv6'][$valid_address] = 1;
    }
    //end if
}
コード例 #5
0
function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen, $ipv6_origin)
{
    global $device, $config;
    $ipv6_network = Net_IPv6::getNetmask("{$ipv6_address}/{$ipv6_prefixlen}") . '/' . $ipv6_prefixlen;
    $ipv6_compressed = Net_IPv6::compress($ipv6_address);
    if (Net_IPv6::getAddressType($ipv6_address) == NET_IPV6_LOCAL_LINK) {
        # ignore link-locals (coming from IPV6-MIB)
        return;
    }
    # FIXME dbFacile
    if (mysql_result(mysql_query("SELECT count(*) FROM `ports`\n        WHERE device_id = '" . $device['device_id'] . "' AND `ifIndex` = '{$ifIndex}'"), 0) != '0' && $ipv6_prefixlen > '0' && $ipv6_prefixlen < '129' && $ipv6_compressed != '::1') {
        $i_query = "SELECT port_id FROM `ports` WHERE device_id = '" . $device['device_id'] . "' AND `ifIndex` = '{$ifIndex}'";
        $port_id = mysql_result(mysql_query($i_query), 0);
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '{$ipv6_network}'"), 0) < '1') {
            mysql_query("INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES ('{$ipv6_network}')");
            echo "N";
        }
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_networks` WHERE `ipv6_network` = '{$ipv6_network}'"), 0) < '1') {
            mysql_query("INSERT INTO `ipv6_networks` (`ipv6_network`) VALUES ('{$ipv6_network}')");
            echo "N";
        }
        $ipv6_network_id = @mysql_result(mysql_query("SELECT `ipv6_network_id` from `ipv6_networks` WHERE `ipv6_network` = '{$ipv6_network}'"), 0);
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ipv6_addresses` WHERE `ipv6_address` = '{$ipv6_address}' AND `ipv6_prefixlen` = '{$ipv6_prefixlen}' AND `port_id` = '{$port_id}'"), 0) == '0') {
            mysql_query("INSERT INTO `ipv6_addresses` (`ipv6_address`, `ipv6_compressed`, `ipv6_prefixlen`, `ipv6_origin`, `ipv6_network_id`, `port_id`)\n                                   VALUES ('{$ipv6_address}', '{$ipv6_compressed}', '{$ipv6_prefixlen}', '{$ipv6_origin}', '{$ipv6_network_id}', '{$port_id}')");
            echo "+";
        } else {
            echo ".";
        }
        $full_address = "{$ipv6_address}/{$ipv6_prefixlen}";
        $valid_address = $full_address . "-" . $port_id;
        $valid['ipv6'][$valid_address] = 1;
    }
}