public function execute($params = array()) { $ip = filter_input(INPUT_POST, 'ip'); $netmask = filter_input(INPUT_POST, 'netmask'); $validateIP = new Net_IPv4(); $net = $validateIP->getNetLength($netmask); $subnet = $validateIP->getSubnet($ip, $netmask); $ipValid = $subnet . '/' . $net; $geo_model = new Geo(); $geos = $geo_model->getGeoList($ipValid); require 'app/Views/Geo_View.php'; }
function _get_ranges(&$config) { // get all ip ranges $ranges = array(); foreach ($config as $carrier => $carrier_ips) { foreach ($carrier_ips as $cidr) { $cidr = @Net_IPv4::parseAddress($cidr); $ranges[] = array('broadcast' => $this->ip2double($cidr->broadcast), 'carrier' => $carrier, 'network' => $this->ip2double($cidr->network)); } } // sort usort($ranges, array($this, 'compareByNetwork')); // unite sequent ip range $l = count($ranges); $cur =& $ranges[0]; for ($i = 1; $i < $l; $i++) { $next =& $ranges[$i]; $condition = $cur['broadcast'] + 1 === $next['network'] && $cur['carrier'] === $next['carrier']; if ($condition) { $cur['broadcast'] = $next['broadcast']; unset($ranges[$i]); } else { $cur =& $ranges[$i]; } } $ranges = array_values($ranges); // compile data $retval = "<?php\n" . "// auto-generated by %s\n" . "// date: %s\n" . "return %s;\n"; $retval = sprintf($retval, __CLASS__, date('Y/m/d H:i:s'), var_export($ranges, true)); return $retval; }
function get_config_by_network($ip, &$config_list) { if (!is_array($config_list)) { return array(); } foreach ($config_list as $network => $config) { if (Net_IPv4::ipInNetwork($ip, $network)) { return $config; } } return array(); }
public function isMobileIPAddress() { $ipList = (array) (include sfContext::getInstance()->getConfigCache()->checkConfig('config/mobile_ip_address.yml')); require_once 'Net/IPv4.php'; $result = false; foreach ($ipList as $mobileIp) { if (Net_IPv4::ipInNetwork($_SERVER['REMOTE_ADDR'], $mobileIp)) { $result = true; break; } } return $result; }
function testWillcom() { $this->mobile_ip->initialize($this->controller); $ranges = $this->ranges['willcom']; foreach ($ranges as $cidr) { $cidr = @Net_IPv4::parseAddress($cidr); $network = explode('.', $cidr->network); $broadcast = explode('.', $cidr->broadcast); $ip = "{$network[0]}.{$network[1]}.{$network[2]}"; for ($i = $network[3]; $i <= $broadcast[3]; $i++) { $this->assertEqual($this->mobile_ip->carrier("{$ip}.{$i}"), 'willcom'); } } }
public function isMobileIPAddress() { require_once 'Net/IPv4.php'; $ipList = (array) (include sfContext::getInstance()->getConfigCache()->checkConfig('config/mobile_ip_address.yml')); $carrier = strtolower($this->getMobile()->getCarrierLongName()); $list = array(); if (isset($ipList[$carrier])) { $list = $ipList[$carrier]; } $result = false; foreach ($list as $mobileIp) { if (Net_IPv4::ipInNetwork($_SERVER['REMOTE_ADDR'], $mobileIp)) { $result = true; break; } } return $result; }
function MAX_remotehostPrivateAddress($ip) { setupIncludePath(); require_once 'Net/IPv4.php'; $aPrivateNetworks = array('10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '127.0.0.0/24'); foreach ($aPrivateNetworks as $privateNetwork) { if (Net_IPv4::ipInNetwork($ip, $privateNetwork)) { return true; } } return false; }
function is_client_authorized($clientip) { global $config; if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) { d_echo("Unauthorized graphs allowed\n"); return true; } if (isset($config['allow_unauth_graphs_cidr'])) { foreach ($config['allow_unauth_graphs_cidr'] as $range) { if (Net_IPv4::ipInNetwork($clientip, $range)) { d_echo("Unauthorized graphs allowed from {$range}\n"); return true; } } } return false; }
/** * Reformat incomplete IPv4 address to decimal for search! */ function reformatIPv4forSearch($ip) { //remove % sign if present $ip = str_replace("%", "", $ip); //remove last . $size = count($ip); $lastChar = substr($ip, -1); if ($lastChar == ".") { $ip = substr($ip, 0, -1); } /* check if subnet provided, then we have all we need */ if (strpos($ip, "/") > 0) { require_once 'PEAR/Net/IPv4.php'; $net = Net_IPv4::parseAddress($ip); $result['low'] = transform2decimal($net->network); $result['high'] = transform2decimal($net->broadcast); } else { /* if subnet is not provided maye wildcard is, so explode it to array */ $ip = explode(".", $ip); //4 is ok if (sizeof($ip) == 4) { $temp = implode(".", $ip); $result['low'] = $result['high'] = transform2decimal($temp); } else { if (sizeof($ip) == 3) { $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { if (sizeof($ip) == 2) { $ip[2] = 0; $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[2] = 255; $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { if (sizeof($ip) == 1) { $ip[1] = 0; $ip[2] = 0; $ip[3] = 0; $result['low'] = transform2decimal(implode(".", $ip)); $ip[1] = 255; $ip[2] = 255; $ip[3] = 255; $result['high'] = transform2decimal(implode(".", $ip)); } else { $result['low'] = implode(".", $ip); $result['high'] = implode(".", $ip); } } } } } //return result! return $result; }
function MAX_remotehostPrivateAddress($ip) { setupIncludePath(); require_once 'Net/IPv4.php'; // Define the private address networks, see // http://rfc.net/rfc1918.html $aPrivateNetworks = array('10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '127.0.0.0/24'); foreach ($aPrivateNetworks as $privateNetwork) { if (Net_IPv4::ipInNetwork($ip, $privateNetwork)) { return true; } } return false; }
/** * Determines whether or not the supplied IP is within the supplied network. * * This function determines whether an IP address is within a network. * The IP address ($ip) must be supplied in dot-quad format, and the * network ($network) may be either a string containing a CIDR * formatted network definition, or a Net_IPv4 object. * * @param string $ip A dot quad representation of an IP address * @param string $network A string representing the network in CIDR format or a Net_IPv4 object. * @return bool true if the IP address exists within the network */ function ipInNetwork($ip, $network) { if (!is_object($network) || strcasecmp(get_class($network), 'net_ipv4') != 0) { $network = Net_IPv4::parseAddress($network); } if (strcasecmp(get_class($network), 'pear_error') === 0) { return false; } $net = Net_IPv4::ip2double($network->network); $bcast = Net_IPv4::ip2double($network->broadcast); $ip = Net_IPv4::ip2double($ip); unset($network); if ($ip >= $net && $ip <= $bcast) { return true; } return false; }
</dl>'; } echo "</a></li>"; } /// SEARCH IP ADDRESSES list($addr, $mask) = explode('/', $queryString); $address_type = "ipv4"; if (is_numeric(stripos($queryString, ':abcdef'))) { $address_type = 'ipv6'; } switch ($address_type) { case 'ipv6': $ip_valid = Net_IPv6::checkIPv6($addr); break; case 'ipv4': $ip_valid = Net_IPv4::validateIP($addr); break; } # if ($ip_valid) # { # // If address valid -> seek occurrence in network # if (!$mask) { $mask = ($address_type === 'ipv4') ? '32' : '128'; }# # } else { // If address not valid -> seek LIKE $where .= ' AND A.`ipv4_address` LIKE ?'; $param[] = '%' . $addr . '%'; # } // FIXME no v6 yet. $query = 'SELECT * '; $query .= 'FROM `ipv4_addresses` AS A '; $query .= 'LEFT JOIN `ports` ON `A`.`port_id` = `ports`.`port_id` ';
$old_table[$entry['ifIndex']][$entry['ipv4_address']] = $entry; } // Process founded IPv4 addresses $valid[$ip_version] = array(); $check_networks = array(); if (count($ip_data)) { foreach ($ip_data as $ifIndex => $addresses) { if (!isset($cache['port_index'][$device_id][$ifIndex])) { continue; } // continue if ifIndex not found $port_id = $cache['port_index'][$device_id][$ifIndex]; foreach ($addresses as $ipv4_address => $entry) { $update_array = array(); $ipv4_mask = $entry['ipAdEntNetMask']; $addr = Net_IPv4::parseAddress($ipv4_address . '/' . $ipv4_mask); $ipv4_prefixlen = $addr->bitmask; $ipv4_network = $addr->network . '/' . $ipv4_prefixlen; $full_address = $ipv4_address . '/' . $ipv4_prefixlen; // First check networks $ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($ipv4_network)); if (empty($ipv4_network_id)) { $ipv4_network_id = dbInsert(array('ipv4_network' => $ipv4_network), 'ipv4_networks'); echo 'N'; } // Check IPs in DB if (isset($old_table[$ifIndex][$ipv4_address])) { foreach (array('ipv4_prefixlen', 'ipv4_network_id', 'port_id') as $param) { if ($old_table[$ifIndex][$ipv4_address][$param] != ${$param}) { $update_array[$param] = ${$param}; }
/** * Get first available IP address */ function getFirstAvailableIPAddress($subnetId) { global $database; /* 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-danger'>" . _('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); // IPv4 if ($type == "IPv4") { //if subnet is /32 if ($mask == "32") { if ($size == 1) { $firstAvailable = $ipaddressArray[0]; } else { $firstAvailable = false; } } elseif ($mask == "31") { if ($size == 1) { $firstAvailable = $ipaddressArray[0]; } elseif ($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; } } elseif ($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! 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 subnet is /128 if ($mask == "128" && $type == "IPv6") { if ($size == 1) { $firstAvailable = $ipaddressArray[0]; } else { $firstAvailable = false; } } elseif ($mask == "127" && $type == "IPv6") { if ($size == 1) { $firstAvailable = $ipaddressArray[0]; } elseif ($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; } } elseif ($size == 1) { $firstAvailable = gmp_strval($ipaddressArray[0]); } elseif ($subnet == $ipaddressArray[0]) { $firstAvailable = gmp_strval($subnet); } 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! $firstAvailable = gmp_strval(gmp_add($ipaddressArray[$size - 1], 1)); } } /* return first available IP address */ return $firstAvailable; }
/** * ユーザーエージェントチェック * * @access private */ function _checkAgent() { if (!$this->isMobile()) { return; } static $userAgent; if ($userAgent === NULL) { $userAgent = Net_UserAgent_Mobile::factory(); } $container =& DIContainerFactory::getContainer(); $getdata =& $container->getComponent("GetData"); $config =& $getdata->getParameter("config"); //$configView =& $container->getComponent("configView"); //$config = $configView->getConfigByCatid(_SYS_CONF_MODID, _GENERAL_CONF_CATID); $session =& $container->getComponent("Session"); $tel_id = $userAgent->getSerialNumber() . $userAgent->getUID(); $tel_id = !empty($tel_id) ? $userAgent->getUserAgent() . " " . $tel_id : ""; if (empty($tel_id)) { $tel_id = $session->getParameter("_mobile_tel_id"); } $this->_mobile_info = array("currier" => $userAgent->getCarrierLongName(), "user_agent" => $userAgent->getUserAgent(), "tel_id" => $tel_id, "utn" => $userAgent->isDoCoMo() ? " utn" : "", "model" => $userAgent->getModel(), "autologin" => $config[0]['autologin_use']['conf_value'] == _AUTOLOGIN_OK ? _AUTOLOGIN_OK : _OFF); $session->setParameter("_mobile_tel_id", $tel_id); if ($userAgent->isDoCoMo()) { if (empty($_SERVER["HTTP_REFERER"])) { $_SERVER["HTTP_REFERER"] = BASE_URL; } } /* * IPチェック(調査日 2010.09.01) */ $mobile_ip["DoCoMo"] = array("210.153.84.0/24", "210.136.161.0/24", "210.153.86.0/24", "124.146.174.0/24", "124.146.175.0/24", "202.229.176.0/24", "202.229.177.0/24", "202.229.178.0/24", "202.229.179.0/24", "111.89.188.0/24", "111.89.189.0/24", "111.89.190.0/24", "111.89.191.0/24", "210.153.87.0/24", "203.138.180.0/24", "203.138.181.0/24", "203.138.203.0/24"); $mobile_ip["EZWeb"] = array("210.230.128.224/28", "121.111.227.160/27", "61.117.1.0/28", "219.108.158.0/27", "219.125.146.0/28", "61.117.2.32/29", "61.117.2.40/29", "219.108.158.40/29", "219.125.148.0/25", "222.5.63.0/25", "222.5.63.128/25", "222.5.62.128/25", "59.135.38.128/25", "219.108.157.0/25", "219.125.145.0/25", "121.111.231.0/25", "121.111.227.0/25", "118.152.214.192/26", "118.159.131.0/25", "118.159.133.0/25", "118.159.132.160/27", "111.86.142.0/26", "111.86.141.64/26", "111.86.141.128/26", "111.86.141.192/26", "118.159.133.192/26", "111.86.143.192/27", "111.86.143.224/27", "111.86.147.0/27", "111.86.142.128/26", "111.86.142.192/26", "111.86.143.0/26", "61.117.0.128/25", "61.117.1.128/25", "218.222.1.0/25", "218.222.1.128/28", "218.222.1.160/28", "61.202.3.64/28", "219.125.148.160/27", "121.111.231.160/27", "219.125.148.192/27", "222.7.56.0/27", "222.7.56.32/27", "222.7.56.96/27", "222.7.56.128/27", "222.7.56.192/27", "222.7.56.224/27", "222.7.57.64/27", "222.7.57.96/27", "222.7.57.128/27", "222.7.57.160/27", "222.7.57.192/27", "222.7.57.224/27", "219.125.151.128/27", "219.125.151.160/27", "219.125.151.192/27", "222.7.57.32/27", "222.15.68.192/26", "59.135.39.128/27", "118.152.214.160/27", "118.152.214.128/27", "222.1.136.96/27", "222.1.136.64/27", "59.128.128.0/20", "210.169.40.0/24", "211.5.2.128/25", "222.7.57.0/24", "210.196.3.192/26", "211.5.7.0/24", "59.135.38.128/25", "210.196.5.192/26", "218.222.1.0/24", "61.117.0.0/24", "210.230.128.0/24", "61.117.1.0/24", "219.125.148.0/24", "210.230.141.192/26", "219.108.158.0/26", "61.117.2.0/26", "210.234.105.32/29", "210.251.1.192/26", "219.125.151.128/25", "210.234.108.64/26", "61.202.3.0/24", "210.251.2.0/27", "222.5.63.0/24", "211.5.1.0/24", "222.7.56.0/24"); $mobile_ip["Softbank"] = array("123.108.237.0/27", "202.253.96.224/27", "210.146.7.192/26", "210.175.1.128/25", "123.108.237.224/27", "202.253.96.0/27", "123.108.236.0/24", "202.179.203.0/24", "202.179.203.0/24", "210.146.60.128/25", "210.169.171.0/24", "210.169.176.0/24", "210.175.1.128/25", "210.169.130.112/28", "210.228.189.0/24", "123.108.236.0/24", "202.179.204.0/24", "210.146.60.192/26", "210.151.9.128/26", "210.228.189.0/24", "211.8.159.128/25", "123.108.237.240/28", "202.253.96.0/28", "123.108.237.240/28", "202.253.96.0/28", "219.73.128.0/17", "117.46.128.0/17"); $mobile_ip["WILLCOM"] = array("61.198.128.0/24", "61.198.129.0/24", "61.198.130.0/24", "61.198.131.0/24", "61.198.132.0/24", "61.198.133.0/24", "61.198.134.0/24", "61.198.135.0/24", "61.198.136.0/24", "61.198.137.0/24", "61.198.138.100/32", "61.198.138.101/32", "61.198.138.102/32", "61.198.138.103/32", "61.198.139.0/29", "61.198.139.128/27", "61.198.139.160/28", "61.198.140.0/24", "61.198.141.0/24", "61.198.142.0/24", "61.198.143.0/24", "61.198.160.0/24", "61.198.161.0/24", "61.198.162.0/24", "61.198.163.0/24", "61.198.164.0/24", "61.198.165.0/24", "61.198.166.0/24", "61.198.168.0/24", "61.198.169.0/24", "61.198.170.0/24", "61.198.171.0/24", "61.198.172.0/24", "61.198.173.0/24", "61.198.174.0/24", "61.198.175.0/24", "61.198.248.0/24", "61.198.249.0/24", "61.198.250.0/24", "61.198.251.0/24", "61.198.252.0/24", "61.198.253.0/24", "61.198.254.0/24", "61.198.255.0/24", "61.204.0.0/24", "61.204.2.0/24", "61.204.3.0/25", "61.204.3.128/25", "61.204.4.0/24", "61.204.5.0/24", "61.204.6.0/25", "61.204.6.128/25", "61.204.7.0/25", "61.204.92.0/24", "61.204.93.0/24", "61.204.94.0/24", "61.204.95.0/24", "125.28.0.0/24", "125.28.1.0/24", "125.28.15.0/24", "125.28.16.0/24", "125.28.17.0/24", "125.28.2.0/24", "125.28.3.0/24", "125.28.4.0/24", "125.28.5.0/24", "125.28.8.0/24", "210.168.246.0/24", "210.168.247.0/24", "210.169.92.0/24", "210.169.93.0/24", "210.169.94.0/24", "210.169.95.0/24", "210.169.96.0/24", "210.169.97.0/24", "210.169.98.0/24", "210.169.99.0/24", "211.126.192.128/25", "211.18.232.0/24", "211.18.233.0/24", "211.18.234.0/24", "211.18.235.0/24", "211.18.236.0/24", "211.18.237.0/24", "219.108.10.0/24", "219.108.11.0/24", "219.108.12.0/24", "219.108.13.0/24", "219.108.14.0/24", "219.108.15.0/24", "219.108.7.0/24", "219.108.8.0/24", "219.108.9.0/24", "221.119.0.0/24", "221.119.1.0/24", "221.119.2.0/24", "221.119.3.0/24", "221.119.4.0/24", "221.119.6.0/24", "221.119.7.0/24", "221.119.8.0/24", "221.119.9.0/24", "114.20.49.0/24", "114.20.50.0/24", "114.20.51.0/24", "114.20.52.0/24", "114.20.53.0/24", "114.20.54.0/24", "114.20.55.0/24", "114.20.56.0/24", "114.20.57.0/24", "114.20.58.0/24", "114.20.59.0/24", "114.20.60.0/24", "114.20.61.0/24", "114.20.62.0/24", "114.20.63.0/24", "114.20.64.0/24", "114.20.65.0/24", "114.20.66.0/24", "114.20.67.0/24", "114.21.255.0/27", "219.108.2.0/24", "219.108.3.0/24", "125.28.6.0/24", "125.28.7.0/24", "125.28.11.0/24", "125.28.12.0/24", "125.28.13.0/24", "125.28.14.0/24", "211.18.238.0/24", "211.18.239.0/24", "219.108.4.0/24", "219.108.5.0/24", "219.108.6.0/24", "221.119.5.0/24", "124.211.23.0/26"); $this->_mobile_info["ip_address"] = $_SERVER['REMOTE_ADDR']; $this->_mobile_info["ip_addr_zone"] = ""; $this->_mobile_info["proper_route"] = _OFF; foreach ($mobile_ip as $key => $ip_list) { foreach ($ip_list as $zone) { $valid = Net_IPv4::ipInNetwork($this->_mobile_info["ip_address"], $zone); if ($valid) { $this->_mobile_info["ip_addr_zone"] = $zone; $this->_mobile_info["proper_route"] = _ON; break; } } if ($valid) { break; } } if (empty($this->_mobile_info['proper_route'])) { $this->_mobile_info['autologin'] = _OFF; } }
} if (isset($opts['n'])) { $force_network = true; } if (isset($opts['b'])) { $force_broadcast = true; } if (isset($opts['r'])) { $net = Net_IPv4::parseAddress($opts['r']); if (ip2long($net->network) !== false) { perform_snmp_scan($net, $force_network, $force_broadcast); echo 'Scanned ' . $stats['count'] . ' IPs, Already known ' . $stats['known'] . ' Devices, Added ' . $stats['added'] . ' Devices, Failed to add ' . $stats['failed'] . ' Devices.' . PHP_EOL; echo 'Runtime: ' . (microtime(true) - $ts) . ' secs' . PHP_EOL; } else { echo 'Could not interpret supplied CIDR noted IP-Range: ' . $opts['r'] . PHP_EOL; exit(2); } } elseif (isset($config['nets']) && !empty($config['nets'])) { if (!is_array($config['nets'])) { $config['nets'] = array($config['nets']); } foreach ($config['nets'] as $subnet) { $net = Net_IPv4::parseAddress($subnet); perform_snmp_scan($net, $force_network, $force_broadcast); } echo 'Scanned ' . $stats['count'] . ' IPs, Already know ' . $stats['known'] . ' Devices, Added ' . $stats['added'] . ' Devices, Failed to add ' . $stats['failed'] . ' Devices.' . PHP_EOL; echo 'Runtime: ' . (microtime(true) - $ts) . ' secs' . PHP_EOL; } else { echo 'Please either add a range argument with \'-r <CIDR_RANGE>\' or define $config[\'nets\'] in your config.php' . PHP_EOL; exit(2); }
include $config['install_dir'] . "/includes/rewrites.inc.php"; include $config['install_dir'] . "/includes/rrdtool.inc.php"; include $config['install_dir'] . "/includes/entities.inc.php"; include $config['html_dir'] . "/includes/functions.inc.php"; if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) { $auth = TRUE; // hardcode auth for all with config function print_debug('认证旁路 $config[\'allow_unauth_graphs\'].'); } elseif (isset($config['allow_unauth_graphs_cidr']) && count($config['allow_unauth_graphs_cidr'])) { foreach ($config['allow_unauth_graphs_cidr'] as $range) { list($net, $mask) = explode('/', trim($range)); if (Net_IPv4::validateIP($net)) { // IPv4 $mask = $mask != NULL ? $mask : '32'; $range = $net . '/' . $mask; if ($mask >= 0 && $mask <= 32 && Net_IPv4::ipInNetwork($_SERVER['REMOTE_ADDR'], $range)) { $auth = TRUE; // hardcode authenticated for matched subnet print_debug("认证的CIDR匹配IPv4 {$range}."); break; } } elseif (Net_IPv6::checkIPv6($net)) { // IPv6 $mask = $mask != NULL ? $mask : '128'; $range = $net . '/' . $mask; if ($mask >= 0 && $mask <= 128 && Net_IPv6::isInNetmask($_SERVER['REMOTE_ADDR'], $range)) { $auth = TRUE; // hardcode authenticated for matched subnet print_debug("认证的CIDR匹配IPv6 {$range}"); break; }
ini_set('error_reporting', E_ALL); } require '../includes/defaults.inc.php'; require '../config.php'; require_once '../includes/definitions.inc.php'; require 'includes/functions.inc.php'; require '../includes/functions.php'; require 'includes/authenticate.inc.php'; if (!$_SESSION['authenticated']) { echo 'unauthenticated'; exit; } $output = ''; if ($_GET['query'] && $_GET['cmd']) { $host = $_GET['query']; if (Net_IPv6::checkIPv6($host) || Net_IPv4::validateip($host) || preg_match('/^[a-zA-Z0-9.-]*$/', $host)) { switch ($_GET['cmd']) { case 'whois': $cmd = $config['whois'] . " {$host} | grep -v \\%"; break; case 'ping': $cmd = $config['ping'] . " -c 5 {$host}"; break; case 'tracert': $cmd = $config['mtr'] . " -r -c 5 {$host}"; break; case 'nmap': if ($_SESSION['userlevel'] != '10') { echo 'insufficient privileges'; } else { $cmd = $config['nmap'] . " {$host}";
function drawPool($id) { $pools = fullIPUsage(); $mypool = $pools[$id]; $out = ""; $out .= "<h2>" . $mypool["location"] . ": " . $mypool["ip_addr"] . "/" . $mypool["netmask"] . "</h2>"; $out .= _("IP pool size:") . " " . $mypool["pool_size"] . "<br>"; $out .= _("Number of SSL IP(s):") . " " . $mypool["nbr_ssl"] . "<br>"; $out .= _("Number of dom0 IP(s):") . " " . $mypool["nbr_dom0"] . "<br>"; $out .= _("Number of VPS IP(s):") . " " . $mypool["nbr_vps"] . "<br>"; $out .= _("Number of dedicated IP(s):") . " " . $mypool["nbr_dedicated"] . "<br>"; $out .= _("Total number of used IPs:") . " " . $mypool["nbr_total_used"] . "<br>"; $out .= _("Number of IPs remaining in the pool:") . " " . $mypool["ip_remaining"] . "<br>"; $ip_calc = new Net_IPv4(); $ip_long = $ip_calc->ip2double($mypool["ip_addr"]); $out .= "<table class=\"dtcDatagrid_table_props\" border=\"1\"><tr><td class=\"dtcDatagrid_table_titles\">" . _("IP address") . "</td><td class=\"dtcDatagrid_table_titles\">" . _("Type") . "</td><td class=\"dtcDatagrid_table_titles\">" . _("Name") . "</td><td class=\"dtcDatagrid_table_titles\">" . _("Provision") . "</td></tr>"; for ($i = 0; $i < $mypool["pool_size"]; $i++) { $ip = long2ip($ip_long); if ($i % 2) { $class = "dtcDatagrid_table_flds_alt"; } else { $class = "dtcDatagrid_table_flds"; } $out .= "<tr><td class=\"{$class}\" style=\"text-align:right;\">" . $ip . "</td>"; if (isset($mypool["all_ips"][$ip])) { $all_ips = $mypool["all_ips"]; $out .= "<td class=\"{$class}\" style=\"text-align:center;\">" . $mypool["all_ips"][$ip]["type"] . "</td>"; if (isset($all_ips[$ip]["available"])) { if ($all_ips[$ip]["available"] == "yes") { $av = "<b>" . _("FREE") . "</b>"; } else { $av = _("no"); } } else { $av = "-"; } switch ($all_ips[$ip]["type"]) { case "vps": $out .= "<td class=\"{$class}\" style=\"text-align:center;\">" . $all_ips[$ip]["vps_xen_name"] . ":" . $all_ips[$ip]["vps_server_hostname"] . "</td><td class=\"{$class}\" style=\"text-align:center;\">" . $av . "</td>"; break; case "ssl": $out .= "<td class=\"{$class}\" style=\"text-align:center;\">-</td><td class=\"{$class}\" style=\"text-align:center;\">" . $av . "</td>"; break; case "dedicated": $out .= "<td class=\"{$class}\">" . $all_ips[$ip]["dedicated_server_hostname"] . "</td><td class=\"{$class}\" style=\"text-align:center;\">-</td>"; break; case "dom0": $out .= "<td class=\"{$class}\">" . $all_ips[$ip]["hostname"] . "</td><td class=\"{$class}\" style=\"text-align:center;\">-</td>"; break; default: $out .= "<td class=\"{$class}\" style=\"text-align:center;\">-</td><td class=\"{$class}\" style=\"text-align:center;\">-</td>"; break; } $out .= "</tr>"; } else { $out .= "<td class=\"{$class}\" colspan=\"3\" style=\"text-align:center;\"><b>" . _("FREE") . "</b></td></tr>"; } $ip_long++; } $out .= "</table>"; return $out; }
function is_ipv4_valid($ipv4_address, $ipv4_prefixlen = NULL) { if (strpos($ipv4_address, '/') !== FALSE) { list($ipv4_address, $ipv4_prefixlen) = explode('/', $ipv4_address); } // False if prefix less or equal 0 and more 32 if (is_numeric($ipv4_prefixlen) && ($ipv4_prefixlen < '1' || $ipv4_prefixlen > '32')) { return FALSE; } // False if invalid IPv4 syntax if (!Net_IPv4::validateIP($ipv4_address)) { return FALSE; } // False if 0.0.0.0 if ($ipv4_address == '0.0.0.0') { return FALSE; } return TRUE; }
/** * Determines whether or not the supplied IP is within the supplied network. * * This function determines whether an IP address is within a network. * The IP address ($ip) must be supplied in dot-quad format, and the * network ($network) may be either a string containing a CIDR * formatted network definition, or a Net_IPv4 object. * * @param string $ip A quad-dot representation of an IP address * @param string $network A string representing the network in CIDR format or a Net_IPv4 object. * @return boolean true if the IP address exists within the network */ function ipInNetwork($ip, $network) { if (!is_object($network) || get_class($network) != 'net_ipv4') { $network = Net_IPv4::parseAddress($network); } if (!is_object($network) || get_class($network) != 'net_ipv4') { return $network; } $net = Net_IPv4::ip2double($network->network); $bcast = Net_IPv4::ip2double($network->broadcast); $ip = Net_IPv4::ip2double($ip); unset($network); if ($ip >= $net && $ip <= $bcast) { return TRUE; } return FALSE; return (double) sprintf("%u", ip2long($ip)); }
echo "<script type='text/javascript'>window.listNetworkInterface=" . json_encode($listnetworkinterfaces) . ";</script>"; /**************************************************/ #speedandduplex $interfacerec->sql = "SELECT id_device_ifspeedduplex,device_ifspeedduplex FROM _device_ifspeedduplex"; $interfacerec->getRec(); $listifspeedduplex = $interfacerec->dbData; /**************************************************/ #label $interfacerec->sql = "\nSELECT \na.id_device_interfacelabel,\na.device_interfacelabel \nFROM _device_interfacelabel a\nWHERE NOT EXISTS \n(SELECT b.id_device_interfacelabel \nFROM _cfg_device_interface b \nWHERE a.id_device_interfacelabel=b.id_device_interfacelabel \nAND b.id_cfg_device=" . $_POST["id_cfg_device"] . " \nAND b.cfg_device_interface_enabled=1) AND id_device=" . $_POST["id_device"]; $interfacerec->getRec(); $listiflabel = $interfacerec->dbData; /**************************************************/ #netmask $listnetmask = array(); for ($i = 15; $i < 33; $i++) { $net = Net_IPv4::parseAddress("0.0.0.0/" . $i); $net->bitmask = $i; $netmask = $net->netmask; $listNetmask[] = array($i, $netmask . " / " . $i); } /**************************************************/ # Filterable Objects ## Network $gdbo = new ArcDb(); $gdbo->dbConStr = $globalDBCON; $gdbo->dbType = $globalDBTP; $gdbo->dbSchema = $globalDB; $gdbo->sql = "SELECT\nid_cfg_device_ip4netaddress,\nCONCAT(CAST(inet_ntoa(cfg_device_ip4netaddress) as CHAR),'/',CAST(cfg_device_ip4netaddress_nmbits as CHAR)) as Network\nFROM _cfg_device_ip4netaddress"; $gdbo->dbFilter = " WHERE id_cfg_device_netgroup="; $gdbo->type = "list"; $gdbo->id = "id_cfg_device_ip4netaddress";
function match_network($ip, $nets, $first = FALSE) { $return = FALSE; $ip_version = get_ip_version($ip); if ($ip_version) { if (!is_array($nets)) { $nets = array($nets); } foreach ($nets as $net) { $ip_in_net = FALSE; $revert = preg_match("/^\\!/", $net) ? TRUE : FALSE; // NOT match network $net = preg_replace("/^\\!/", "", $net); if ($ip_version == 4) { if (strpos($net, '.') === FALSE) { continue; } // NOT IPv4 net, skip if (strpos($net, '/') === FALSE) { $net .= '/32'; } // NET without mask as single IP $ip_in_net = Net_IPv4::ipInNetwork($ip, $net); } else { if (strpos($net, ':') === FALSE) { continue; } if (strpos($net, '/') === FALSE) { $net .= '/128'; } // NET without mask as single IP $ip_in_net = Net_IPv6::isInNetmask($ip, $net); } if ($revert && $ip_in_net) { return FALSE; } // Return FALSE if IP founded in network where should NOT match if ($first && $ip_in_net) { return TRUE; } // Return TRUE if IP founded in first match $return = $return || $ip_in_net; } } return $return; }
ini_set('error_reporting', E_ALL); } require '../includes/defaults.inc.php'; require '../config.php'; require_once '../includes/definitions.inc.php'; require 'includes/functions.inc.php'; require '../includes/functions.php'; require 'includes/authenticate.inc.php'; if (!$_SESSION['authenticated']) { echo 'unauthenticated'; exit; } $output = ''; if ($_GET['query'] && $_GET['cmd']) { $host = $_GET['query']; if (Net_IPv6::checkIPv6($host) || Net_IPv4::validateip($host) || filter_var('http://' . $host, FILTER_VALIDATE_URL)) { switch ($_GET['cmd']) { case 'whois': $cmd = $config['whois'] . " {$host} | grep -v \\%"; break; case 'ping': $cmd = $config['ping'] . " -c 5 {$host}"; break; case 'tracert': $cmd = $config['mtr'] . " -r -c 5 {$host}"; break; case 'nmap': if ($_SESSION['userlevel'] != '10') { echo 'insufficient privileges'; } else { $cmd = $config['nmap'] . " {$host}";
* * @package observium * @subpackage discovery * @author Adam Armstrong <*****@*****.**> * @copyright (C) 2006 - 2013 Adam Armstrong * */ include "includes/defaults.inc.php"; include "config.php"; include "includes/definitions.inc.php"; include "includes/functions.php"; $handle = fopen("ips.txt", "w"); foreach (dbFetchRows("SELECT * FROM `ipv4_networks`") as $data) { $cidr = $data['ipv4_network']; list($network, $bits) = explode("/", $cidr); if ($bits != '32' && $bits != '32' && $bits > '22') { $addr = Net_IPv4::parseAddress($cidr); $broadcast = $addr->broadcast; $ip = ip2long($network) + '1'; $end = ip2long($broadcast); while ($ip < $end) { $ipdotted = long2ip($ip); if (dbFetchCell("SELECT COUNT(ipv4_address_id) FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ipdotted)) == '0' && match_network($config['nets'], $ipdotted)) { fputs($handle, $ipdotted . "\n"); } $ip++; } } } fclose($handle); shell_exec("fping -t 100 -f ips.txt > ips-scanned.txt");
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; }
<?php echo 'IPv4 Addresses : '; $oids = trim(snmp_walk($device, 'ipAdEntIfIndex', '-Osq', 'IP-MIB')); $oids = str_replace('ipAdEntIfIndex.', '', $oids); foreach (explode("\n", $oids) as $data) { $data = trim($data); list($oid, $ifIndex) = explode(' ', $data); $mask = trim(snmp_get($device, "ipAdEntNetMask.{$oid}", '-Oqv', 'IP-MIB')); $addr = Net_IPv4::parseAddress("{$oid}/{$mask}"); $network = $addr->network . '/' . $addr->bitmask; $cidr = $addr->bitmask; if (dbFetchCell('SELECT COUNT(*) FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)) != '0' && $oid != '0.0.0.0' && $oid != 'ipAdEntIfIndex') { $port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex)); if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)) < '1') { dbInsert(array('ipv4_network' => $network), 'ipv4_networks'); // echo("Create Subnet $network\n"); echo 'S'; } $ipv4_network_id = dbFetchCell('SELECT `ipv4_network_id` FROM `ipv4_networks` WHERE `ipv4_network` = ?', array($network)); if (dbFetchCell('SELECT COUNT(*) FROM `ipv4_addresses` WHERE `ipv4_address` = ? AND `ipv4_prefixlen` = ? AND `port_id` = ?', array($oid, $cidr, $port_id)) == '0') { dbInsert(array('ipv4_address' => $oid, 'ipv4_prefixlen' => $cidr, 'ipv4_network_id' => $ipv4_network_id, 'port_id' => $port_id), 'ipv4_addresses'); // echo("Added $oid/$cidr to $port_id ( $hostname $ifIndex )\n $i_query\n"); echo '+'; } else { echo '.'; } $full_address = "{$oid}/{$cidr}|{$ifIndex}"; $valid_v4[$full_address] = 1; } else { echo '!';
/** * convert an IP address into a hex value * * @param string $IP * @return string */ protected function convertIpToHex($host) { if (!isset($host) || empty($host) || !is_string($host)) { static::raiseError(__METHOD__ . '(), $host parameter is invalid!'); return false; } global $ms; $ipv4 = new Net_IPv4(); $parsed = $ipv4->parseAddress($host); // if CIDR contains no netmask or was unparsable, we assume /32 if (empty($parsed->netmask)) { $parsed->netmask = "255.255.255.255"; } if (!$ipv4->validateIP($parsed->ip)) { $ms->throwError(_("Incorrect IP address! Can not convert it to hex!")); } if (!$ipv4->validateNetmask($parsed->netmask)) { $ms->throwError(_("Incorrect Netmask! Can not convert it to hex!")); } if (($hex_host = $ipv4->atoh($parsed->ip)) == false) { $ms->throwError(_("Failed to convert " . $parsed->ip . " to hex!")); } if (($hex_subnet = $ipv4->atoh($parsed->netmask)) == false) { $ms->throwError(_("Failed to convert " . $parsed->netmask . " to hex!")); } return array('ip' => $hex_host, 'netmask' => $hex_subnet); }
function netmask2cidr($netmask) { $addr = Net_IPv4::parseAddress("1.2.3.4/{$netmask}"); return $addr->bitmask; }
ini_set('error_reporting', E_ALL); } include "../includes/defaults.inc.php"; include "../config.php"; include_once "../includes/definitions.inc.php"; include "includes/functions.inc.php"; include "../includes/functions.inc.php"; include "includes/authenticate.inc.php"; if (!$_SESSION['authenticated']) { echo "unauthenticated"; exit; } if ($_GET['query'] && $_GET['cmd']) { $host = $_GET['query']; $ip = ''; if (Net_IPv4::validateIP($host)) { $ip = $host; $ip_version = 4; } elseif (Net_IPv6::checkIPv6($host)) { $ip = $host; $ip_version = 6; } else { $ip = gethostbyname($host); if ($ip && $ip != $host) { $ip_version = 4; } else { $ip = gethostbyname6($host, FALSE); if ($ip) { $ip_version = 6; } }