feature: New IP parsing classes. Removes usage of Pear Net_IPv4 and Net_IPv6. (#7106)

* Refactor remove use of pear Net_IPv4 and Net_IPv6

Caution, I added tests to test the functionality of the new functions, but I'm not 100% certain the replaced usages match 100%.  Please test.

Left usage in generate-iplist.php because I'm not sure the use of that script, so I didn't want to implement additional functionality to support it.

Add support for ipv6 fo ipInNetwork() function

Add phpdocs to functions
Remove accidental inclusion

Use binary operations instead of string for networKFromIp()
Tidy up binary operations in ipInNetwork()

tidy parsing cidr network

Change to a class based implementation.

Update phpdocs.
Fully catch exceptions for hex parsing.

Fix mistake.

support older phpunit versions

Fix php shifting to negative with php 5

* Fix graph authorization. Uncaught exception and checking an ipv4 against an ipv6 network == fail.

* Also remove colons from hex strings.

* Fix typo in bgp polling
Two passes of removing invalid characters from hex strings. (That way we don't mangle valid IPs)
This commit is contained in:
Tony Murray 2017-08-08 14:14:58 -05:00 committed by GitHub
parent d8140467ae
commit 1d777693e1
30 changed files with 771 additions and 191 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* InvalidIpException.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Exceptions;
class InvalidIpException extends \Exception
{
}

197
LibreNMS/Util/IP.php Normal file
View File

@ -0,0 +1,197 @@
<?php
/**
* IP.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use LibreNMS\Exceptions\InvalidIpException;
abstract class IP
{
public $ip;
public $cidr;
public $host_bits;
/**
* Convert a hex-string to an IP address. For example: "c0 a8 01 fe" -> 192.168.1.254
* @param string $hex
* @param bool $ignore_errors Do not throw exceptions, instead return null on error.
* @return IP|null
* @throws InvalidIpException
*/
public static function fromHexString($hex, $ignore_errors = false)
{
$hex = str_replace(array(' ', '"'), '', $hex);
try {
return self::parse($hex);
} catch (InvalidIpException $e) {
// ignore
}
$hex = str_replace(array(':', '.'), '', $hex);
try {
if (strlen($hex) == 8) {
return new IPv4(long2ip(hexdec($hex)));
}
if (strlen($hex) == 32) {
return new IPv6(implode(':', str_split($hex, 4)));
}
} catch (InvalidIpException $e) {
if (!$ignore_errors) {
throw $e;
}
}
if (!$ignore_errors) {
throw new InvalidIpException("Could not parse into IP: $hex");
}
return null;
}
/**
* Parse an IP or IP Network into an IP object. Works with IPv6 and IPv4 addresses.
* @param string $ip
* @param bool $ignore_errors Do not throw exceptions, instead return null on error.
* @return IP|null
* @throws InvalidIpException
*/
public static function parse($ip, $ignore_errors = false)
{
try {
return new IPv4($ip);
} catch (InvalidIpException $e) {
// ignore ipv4 failure and try ipv6
}
try {
return new IPv6($ip);
} catch (InvalidIpException $e) {
if (!$ignore_errors) {
throw new InvalidIpException("$ip is not a valid IP address");
}
}
return null;
}
/**
* Check if the supplied IP is valid.
* @param string $ip
* @param bool $exclude_reserved Exclude reserved IP ranges.
* @return bool
*/
public static function isValid($ip, $exclude_reserved = false)
{
return IPv4::isValid($ip, $exclude_reserved) || IPv6::isValid($ip, $exclude_reserved);
}
/**
* Get the network of this IP in cidr format.
* @param int $cidr If not given will use the cidr stored with this IP
* @return string
*/
public function getNetwork($cidr = null)
{
if (is_null($cidr)) {
$cidr = $this->cidr;
}
return $this->getNetworkAddress($cidr) . "/$cidr";
}
/**
* Get the network address of this IP
* @param int $cidr If not given will use the cidr stored with this IP
* @return string
*/
abstract public function getNetworkAddress($cidr = null);
/**
* Check if this IP address is contained inside the network
* @param string $network should be in cidr format.
* @return mixed
*/
abstract public function inNetwork($network);
/**
* Check if this IP is in the reserved range.
* @return bool
*/
public function isReserved()
{
return filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) === false;
}
/**
* Remove extra 0s from this IPv6 address to make it easier to read.
* IPv4 addresses, just return the address.
* @return string|false
*/
public function compressed()
{
return (string)$this->ip;
}
/**
* Expand this IPv6 address to it's full IPv6 representation. For example: ::1 -> 0000:0000:0000:0000:0000:0000:0000:0001
* IPv4 addresses, just return the address.
* @return string
*/
public function uncompressed()
{
return (string)$this->ip;
}
/**
* Get the family of this IP.
* @return string ipv4 or ipv6
*/
public function getFamily()
{
return $this->host_bits == 32 ? 'ipv4' : 'ipv6';
}
public function __toString()
{
if ($this->cidr == $this->host_bits) {
return (string)$this->ip;
}
return $this->ip . "/{$this->cidr}";
}
/**
* Extract an address from a cidr, assume a host is given if it does not contain /
* @param string $ip
* @return array [$ip, $cidr]
*/
protected function extractCidr($ip)
{
return array_pad(explode('/', $ip, 2), 2, $this->host_bits);
}
}

122
LibreNMS/Util/IPv4.php Normal file
View File

@ -0,0 +1,122 @@
<?php
/**
* IPv4.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use LibreNMS\Exceptions\InvalidIpException;
class IPv4 extends IP
{
/**
* IPv4 constructor.
* @param $ipv4
* @throws InvalidIpException
*/
public function __construct($ipv4)
{
$this->host_bits = 32;
list($this->ip, $this->cidr) = $this->extractCidr($ipv4);
if (!self::isValid($this->ip)) {
throw new InvalidIpException("$ipv4 is not a valid ipv4 address");
}
}
/**
* Check if the supplied IP is valid.
* @param string $ipv4
* @param bool $exclude_reserved Exclude reserved IP ranges.
* @return bool
*/
public static function isValid($ipv4, $exclude_reserved = false)
{
$filter = FILTER_FLAG_IPV4;
if ($exclude_reserved) {
$filter |= FILTER_FLAG_NO_RES_RANGE;
}
return filter_var($ipv4, FILTER_VALIDATE_IP, $filter) !== false;
}
/**
* Convert an IPv4 network mask to a bit mask. For example: 255.255.255.0 -> 24
* @param string $netmask
* @return int
*/
public static function netmask2cidr($netmask)
{
$long = ip2long($netmask);
$base = ip2long('255.255.255.255');
return (int)(32 - log(($long ^ $base) + 1, 2));
}
/**
* Returns the netmask of this IP address. For example: 255.255.255.0
* @return string
*/
public function getNetmask()
{
return long2ip($this->cidr2long($this->cidr));
}
/**
* Convert an IPv4 bit mask to a long. Generally used with long2ip() or bitwise operations.
* @return int
*/
private function cidr2long($cidr)
{
return -1 << (32 - (int)$cidr);
}
/**
* Check if this IP address is contained inside the network.
* @param string $network should be in cidr format.
* @return mixed
*/
public function inNetwork($network)
{
list($net, $cidr) = $this->extractCidr($network);
if (!self::isValid($net)) {
return false;
}
$mask = $this->cidr2long($cidr);
return ((ip2long($this->ip) & $mask) == (ip2long($net) & $mask));
}
/**
* Get the network address of this IP
* @param int $cidr if not given will use the cidr stored with this IP
* @return string
*/
public function getNetworkAddress($cidr = null)
{
if (is_null($cidr)) {
$cidr = $this->cidr;
}
return long2ip(ip2long($this->ip) & $this->cidr2long($cidr));
}
}

150
LibreNMS/Util/IPv6.php Normal file
View File

@ -0,0 +1,150 @@
<?php
/**
* IPv6.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use LibreNMS\Exceptions\InvalidIpException;
class IPv6 extends IP
{
/**
* IPv6 constructor.
* @param $ipv6
* @throws InvalidIpException
*/
public function __construct($ipv6)
{
$this->host_bits = 128;
list($this->ip, $this->cidr) = $this->extractCidr($ipv6);
if (!self::isValid($this->ip)) {
throw new InvalidIpException("$ipv6 is not a valid ipv4 address");
}
$this->ip = $this->compressed(); // store in compressed format
}
/**
* Check if the supplied IP is valid.
* @param string $ipv6
* @param bool $exclude_reserved Exclude reserved IP ranges.
* @return bool
*/
public static function isValid($ipv6, $exclude_reserved = false)
{
$filter = FILTER_FLAG_IPV6;
if ($exclude_reserved) {
$filter |= FILTER_FLAG_NO_RES_RANGE;
}
return filter_var($ipv6, FILTER_VALIDATE_IP, $filter) !== false;
}
/**
* Remove extra 0s from this IPv6 address to make it easier to read.
* @return string|false
*/
public function compressed()
{
return inet6_ntop(inet_pton($this->ip));
}
/**
* Get the network address of this IP
* @param int $cidr If not given will use the cidr stored with this IP
* @return string
*/
public function getNetworkAddress($cidr = null)
{
if (is_null($cidr)) {
$cidr = $this->cidr;
}
$net_bytes = unpack('n*', inet_pton($this->ip));
foreach ($net_bytes as $index => $byte) {
$shift = min($cidr - 16 * ($index - 1), 16);
if ($shift > 0) {
$mask = ~(0xffff >> $shift) & 0xffff;
$net_bytes[$index] = $byte & $mask;
} else {
$net_bytes[$index] = 0;
}
}
array_unshift($net_bytes, 'n*'); // add pack format
return inet6_ntop(call_user_func_array('pack', $net_bytes));
}
/**
* Check if this IP address is contained inside the network
* @param string $network should be in cidr format.
* @return mixed
*/
public function inNetwork($network)
{
list($net, $cidr) = $this->extractCidr($network);
if (!self::isValid($net)) {
return false;
}
$net_bytes = unpack('n*', inet_pton($net));
$ip_bytes = unpack('n*', inet_pton($this->ip));
if ($net_bytes === false || $ip_bytes === false) {
return false;
}
// unpack indexes start at 1 and go to 8 for an ipv6
for ($index = 1; $index <= 8; $index++) {
$shift = $cidr - 16 * ($index - 1);
if ($shift > 0) {
$mask = ~(0xffff >> $shift) & 0xffff;
if (($net_bytes[$index] & $mask) != ($ip_bytes[$index] & $mask)) {
return false;
}
} else {
break; // we've passed the network bits, who cares about the rest.
}
}
return true;
}
/**
* Expand this IPv6 address to it's full IPv6 representation. For example: ::1 -> 0000:0000:0000:0000:0000:0000:0000:0001
* @return string
*/
public function uncompressed()
{
// remove ::
$replacement = ':' . str_repeat('0000:', 8 - substr_count($this->ip, ':'));
$ip = str_replace('::', $replacement, $this->ip);
// zero pad
$parts = explode(':', $ip, 8);
return implode(':', array_map(function ($section) {
return zeropad($section, 4);
}, $parts));
}
}

View File

@ -14,6 +14,8 @@
$init_modules = array();
require realpath(__DIR__ . '/..') . '/includes/init.php';
include_once 'Net/IPv4.php';
$handle = fopen('ips.txt', 'w');
foreach (dbFetchRows('SELECT * FROM `ipv4_networks`') as $data) {
@ -22,7 +24,7 @@ foreach (dbFetchRows('SELECT * FROM `ipv4_networks`') as $data) {
if ($bits != '32' && $bits != '32' && $bits > '22') {
$addr = Net_IPv4::parseAddress($cidr);
$broadcast = $addr->broadcast;
$ip = ip2long($network) + '1';
$ip = ip2long($network) + 1;
$end = ip2long($broadcast);
while ($ip < $end) {
$ipdotted = long2ip($ip);

View File

@ -44,10 +44,7 @@ yum install epel-release
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php70w php70w-cli php70w-gd php70w-mysql php70w-snmp php70w-pear php70w-curl php70w-common httpd net-snmp mariadb ImageMagick jwhois nmap mtr rrdtool MySQL-python net-snmp-utils cronie php70w-mcrypt fping git
pear install Net_IPv4-1.3.4
pear install Net_IPv6-1.2.2b2
yum install php70w php70w-cli php70w-gd php70w-mysql php70w-snmp php70w-curl php70w-common httpd net-snmp mariadb ImageMagick jwhois nmap mtr rrdtool MySQL-python net-snmp-utils cronie php70w-mcrypt fping git
```
In `/etc/php.ini` ensure date.timezone is set to your preferred time zone. See http://php.net/manual/en/timezones.php for a list of supported timezones. Valid examples are: "America/New_York", "Australia/Brisbane", "Etc/UTC".

View File

@ -44,10 +44,7 @@ yum install epel-release
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php70w php70w-cli php70w-gd php70w-mysql php70w-snmp php70w-pear php70w-curl php70w-common php70w-fpm nginx net-snmp mariadb ImageMagick jwhois nmap mtr rrdtool MySQL-python net-snmp-utils cronie php70w-mcrypt fping git
pear install Net_IPv4-1.3.4
pear install Net_IPv6-1.2.2b2
yum install php70w php70w-cli php70w-gd php70w-mysql php70w-snmp php70w-curl php70w-common php70w-fpm nginx net-snmp mariadb ImageMagick jwhois nmap mtr rrdtool MySQL-python net-snmp-utils cronie php70w-mcrypt fping git
```
In `/etc/php.ini` ensure date.timezone is set to your preferred time zone. See http://php.net/manual/en/timezones.php for a list of supported timezones. Valid examples are: "America/New_York", "Australia/Brisbane", "Etc/UTC".

View File

@ -36,7 +36,7 @@ lower_case_table_names=0
#### Install / Configure Apache
`apt-get install libapache2-mod-php7.0 php7.0-cli php7.0-mysql php7.0-gd php7.0-snmp php-pear php7.0-curl snmp graphviz php7.0-mcrypt php7.0-json apache2 fping imagemagick whois mtr-tiny nmap python-mysqldb snmpd php-net-ipv4 php-net-ipv6 rrdtool git`
`apt-get install libapache2-mod-php7.0 php7.0-cli php7.0-mysql php7.0-gd php7.0-snmp php7.0-curl snmp graphviz php7.0-mcrypt php7.0-json apache2 fping imagemagick whois mtr-tiny nmap python-mysqldb snmpd rrdtool git`
In `/etc/php/7.0/apache2/php.ini` and `/etc/php/7.0/cli/php.ini`, ensure date.timezone is set to your preferred time zone. See http://php.net/manual/en/timezones.php for a list of supported timezones. Valid examples are: "America/New_York", "Australia/Brisbane", "Etc/UTC".

View File

@ -36,7 +36,7 @@ lower_case_table_names=0
#### Install / Configure Nginx
`apt-get install php7.0-cli php7.0-mysql php7.0-gd php7.0-snmp php-pear php7.0-curl php7.0-fpm snmp graphviz php7.0-mcrypt php7.0-json nginx-full fping imagemagick whois mtr-tiny nmap python-mysqldb snmpd php-net-ipv4 php-net-ipv6 rrdtool git`
`apt-get install php7.0-cli php7.0-mysql php7.0-gd php7.0-snmp php7.0-curl php7.0-fpm snmp graphviz php7.0-mcrypt php7.0-json nginx-full fping imagemagick whois mtr-tiny nmap python-mysqldb snmpd rrdtool git`
In `/etc/php/7.0/fpm/php.ini` and `/etc/php/7.0/cli/php.ini`, ensure date.timezone is set to your preferred time zone. See http://php.net/manual/en/timezones.php for a list of supported timezones. Valid examples are: "America/New_York", "Australia/Brisbane", "Etc/UTC".

View File

@ -1482,11 +1482,10 @@ function list_arp()
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
$arp = dbFetchRows("SELECT `ipv4_mac`.* FROM `ipv4_mac` LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id` WHERE `ports`.`device_id` = ?", array($device_id));
} elseif (str_contains($ip, '/')) {
$ipv4 = new Net_IPv4();
$net = $ipv4->parseAddress($ip);
list($net, $cidr) = explode('/', $ip, 2);
$arp = dbFetchRows(
'SELECT * FROM `ipv4_mac` WHERE (inet_aton(`ipv4_address`) & ?) = ?',
array(ip2long($net->netmask), ip2long($net->network))
array(cidr2long($cidr), ip2long($net))
);
} else {
$arp = dbFetchRows("SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=?", array($ip));
@ -1534,7 +1533,7 @@ function list_services()
$where[] = '`service_type` LIKE ?';
$params[] = $_GET['type'];
}
//GET by Host
if (isset($router['hostname'])) {
$hostname = $router['hostname'];

View File

@ -1,6 +1,8 @@
<?php
// This file prints a table row for each interface
use LibreNMS\Util\IP;
$port['device_id'] = $device['device_id'];
$port['hostname'] = $device['hostname'];
@ -40,7 +42,7 @@ if ($port_details) {
foreach (dbFetchRows('SELECT * FROM `ipv6_addresses` WHERE `port_id` = ?', array($port['port_id'])) as $ip6) {
;
echo "$break <a class=interface-desc href=\"javascript:popUp('netcmd.php?cmd=whois&amp;query=".$ip6['ipv6_address']."')\">".Net_IPv6::compress($ip6['ipv6_address']).'/'.$ip6['ipv6_prefixlen'].'</a>';
echo "$break <a class=interface-desc href=\"javascript:popUp('netcmd.php?cmd=whois&amp;query=".$ip6['ipv6_address']."')\">".IP::parse($ip6['ipv6_address'], true).'/'.$ip6['ipv6_prefixlen'].'</a>';
$break = ',';
}
}

View File

@ -5,6 +5,8 @@ $(function () {
</script>
<?php
use LibreNMS\Util\IP;
// This file prints a table row for each interface
$port['device_id'] = $device['device_id'];
$port['hostname'] = $device['hostname'];
@ -62,7 +64,7 @@ if ($port_details) {
}
foreach (dbFetchRows('SELECT * FROM `ipv6_addresses` WHERE `port_id` = ?', array($port['port_id'])) as $ip6) {
echo "$break <a class=interface-desc href=\"javascript:popUp('netcmd.php?cmd=whois&amp;query=".$ip6['ipv6_address']."')\">".Net_IPv6::compress($ip6['ipv6_address']).'/'.$ip6['ipv6_prefixlen'].'</a>';
echo "$break <a class=interface-desc href=\"javascript:popUp('netcmd.php?cmd=whois&amp;query=".$ip6['ipv6_address']."')\">".IP::parse($ip6['ipv6_address'], true).'/'.$ip6['ipv6_prefixlen'].'</a>';
$break = '<br />';
}
}

View File

@ -1,5 +1,7 @@
<?php
use LibreNMS\Util\IP;
$param = array();
if (is_admin() === false && is_read() === false) {
@ -81,14 +83,12 @@ foreach (dbFetchRows($sql, $param) as $interface) {
$speed = humanspeed($interface['ifSpeed']);
$type = humanmedia($interface['ifType']);
if ($_POST['search_type'] == 'ipv6') {
list($prefix, $length) = explode('/', $interface['ipv6_network']);
$address = Net_IPv6::compress($interface['ipv6_address']).'/'.$length;
if ($_POST['search_type'] == 'mac') {
$address = (string)IP::parse($interface['ipv6_network'], true);
} elseif ($_POST['search_type'] == 'mac') {
$address = formatMac($interface['ifPhysAddress']);
} else {
list($prefix, $length) = explode('/', $interface['ipv4_network']);
$address = $interface['ipv4_address'].'/'.$length;
$address = (string)IP::parse($interface['ipv6_network'], true);
}
if ($interface['in_errors'] > 0 || $interface['out_errors'] > 0) {

View File

@ -1,5 +1,7 @@
<?php
use LibreNMS\Util\IP;
$link_array = array(
'page' => 'device',
'device' => $device['device_id'],
@ -191,9 +193,8 @@ foreach (dbFetchRows("SELECT * FROM `bgpPeers` WHERE `device_id` = ? $extra_sql
unset($sep);
if (filter_var($peer['bgpLocalAddr'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
$peer['bgpPeerIdentifier'] = Net_IPv6::compress($peer['bgpPeerIdentifier']);
}
// make ipv6 look pretty
$peer['bgpPeerIdentifier'] = (string)IP::parse($peer['bgpPeerIdentifier'], true);
// display overlib graphs
$graph_array = array();

View File

@ -1,5 +1,8 @@
<?php
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\IPv6;
if ($_SESSION['userlevel'] < '5') {
include 'includes/error-no-perm.inc.php';
} else {
@ -240,14 +243,15 @@ if ($_SESSION['userlevel'] < '5') {
}
}
if (filter_var($peer['bgpLocalAddr'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
$peer_ip = Net_IPv6::compress($peer['bgpLocalAddr']);
} else {
try {
$peer_ip = new IPv6($peer['bgpLocalAddr']);
} catch (InvalidIpException $e) {
$peer_ip = $peer['bgpLocalAddr'];
}
if (filter_var($peer['bgpPeerIdentifier'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
$peer_ident = Net_IPv6::compress($peer['bgpPeerIdentifier']);
} else {
try {
$peer_ident = new IPv6($peer['bgpPeerIdentifier']);
} catch (InvalidIpException $e) {
$peer_ident = $peer['bgpPeerIdentifier'];
}

View File

@ -16,6 +16,10 @@
* the source code distribution for details.
*/
use LibreNMS\Config;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\IP;
function generate_priority_icon($priority)
{
$map = array(
@ -338,7 +342,7 @@ function device_by_id_cache($device_id, $refresh = '0')
$device = $cache['devices']['id'][$device_id];
} else {
$device = dbFetchRow("SELECT * FROM `devices` WHERE `device_id` = ?", array($device_id));
//order vrf_lite_cisco with context, this will help to get the vrf_name and instance_name all the time
$vrfs_lite_cisco = dbFetchRows("SELECT * FROM `vrf_lite_cisco` WHERE `device_id` = ?", array($device_id));
if (!empty($vrfs_lite_cisco)) {
@ -475,11 +479,7 @@ function safedescr($descr)
function zeropad($num, $length = 2)
{
while (strlen($num) < $length) {
$num = '0'.$num;
}
return $num;
return str_pad($num, $length, '0', STR_PAD_LEFT);
}
function set_dev_attrib($device, $attrib_type, $attrib_value)
@ -706,19 +706,19 @@ function is_mib_graph($type, $subtype)
*/
function is_client_authorized($clientip)
{
global $config;
if (isset($config['allow_unauth_graphs']) && $config['allow_unauth_graphs']) {
if (Config::get('allow_unauth_graphs', false)) {
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)) {
foreach (Config::get('allow_unauth_graphs_cidr', array()) as $range) {
try {
if (IP::parse($clientip)->inNetwork($range)) {
d_echo("Unauthorized graphs allowed from $range\n");
return true;
}
} catch (InvalidIpException $e) {
d_echo("Client IP ($clientip) is invalid.\n");
}
}
@ -1727,29 +1727,6 @@ function get_vm_parent_id($device)
return dbFetchCell("SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?", array($device['hostname'],$device['hostname'].'.'.$config['mydomain']));
}
/**
* @param $string
* @param string $ver
* @return bool
*/
function is_ip($string, $ver = 'ipv4ipv6')
{
if ($ver === 'ipv4ipv6') {
if (filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == true || filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == true) {
return true;
}
} elseif ($ver === 'ipv4') {
if (filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) == true) {
return true;
}
} elseif ($ver === 'ipv6') {
if (filter_var($string, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) == true) {
return true;
}
}
return false;
}
/**
* Fetch a user preference from the database
* Do not use strict comparison as results could be strings

View File

@ -1,4 +1,7 @@
<?php
use LibreNMS\Util\IP;
if ($config['enable_bgp']) {
if (key_exists('vrf_lite_cisco', $device) && (count($device['vrf_lite_cisco'])!=0)) {
$vrfs_lite_cisco = $device['vrf_lite_cisco'];
@ -81,23 +84,9 @@ if ($config['enable_bgp']) {
$j_bgp = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PeerEntry', $jbgp, 'BGP4-V2-MIB-JUNIPER', 'junos');
d_echo($j_bgp);
foreach ($j_bgp as $index => $entry) {
switch ($entry['jnxBgpM2PeerRemoteAddrType']) {
case 'ipv4':
$ip = long2ip(hexdec($entry['jnxBgpM2PeerRemoteAddr']));
d_echo("peerindex for ipv4 $ip is ".$entry['jnxBgpM2PeerIndex']."\n");
$j_peerIndexes[$ip] = $entry['jnxBgpM2PeerIndex'];
break;
case 'ipv6':
$ip6 = trim(str_replace(' ', '', $entry['jnxBgpM2PeerRemoteAddr']), '"');
$ip6 = substr($ip6, 0, 4).':'.substr($ip6, 4, 4).':'.substr($ip6, 8, 4).':'.substr($ip6, 12, 4).':'.substr($ip6, 16, 4).':'.substr($ip6, 20, 4).':'.substr($ip6, 24, 4).':'.substr($ip6, 28, 4);
$ip6 = Net_IPv6::compress($ip6);
d_echo("peerindex for ipv6 $ip6 is ".$entry['jnxBgpM2PeerIndex']."\n");
$j_peerIndexes[$ip6] = $entry['jnxBgpM2PeerIndex'];
break;
default:
echo "HALP? Don't know RemoteAddrType ".$entry['jnxBgpM2PeerRemoteAddrType']."!\n";
break;
}
$ip = IP::fromHexString($entry['jnxBgpM2PeerRemoteAddr'], true);
d_echo("peerindex for '.$ip->getFamily().' $ip is ".$entry['jnxBgpM2PeerIndex']."\n");
$j_peerIndexes[$ip] = $entry['jnxBgpM2PeerIndex'];
}
}

View File

@ -1,5 +1,7 @@
<?php
use LibreNMS\Util\IP;
if ($config['enable_sla'] && $device['os_group'] == 'cisco') {
$slas = snmp_walk($device, 'ciscoRttMonMIB.ciscoRttMonObjects.rttMonCtrl', '-Osq', '+CISCO-RTTMON-MIB');
@ -58,7 +60,7 @@ if ($config['enable_sla'] && $device['os_group'] == 'cisco') {
break;
case 'echo':
$data['tag'] = hex_to_ip($sla_config['rttMonEchoAdminTargetAddress']);
$data['tag'] = IP::fromHexString($sla_config['rttMonEchoAdminTargetAddress'], true);
break;
case 'jitter':

View File

@ -2,6 +2,8 @@
global $link_exists;
use LibreNMS\Util\IP;
$community = $device['community'];
if ($device['os'] == 'ironware' && $config['autodiscovery']['xdp'] === true) {
@ -48,7 +50,7 @@ if ($config['autodiscovery']['xdp'] === true) {
d_echo($cdp_if_array);
foreach ($cdp_if_array as $entry_key => $cdp) {
if (is_valid_hostname($cdp['cdpCacheDeviceId']) || ($config['discovery_by_ip'] == true)) {
$cdp_ip = hex_to_ip($cdp['cdpCacheAddress']);
$cdp_ip = IP::fromHexString($cdp['cdpCacheAddress'], true);
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ? OR `hostname` = ?', array($cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId'], $cdp_ip));
if (!$remote_device_id &&
@ -150,7 +152,7 @@ if ($device['os'] == 'pbn' && $config['autodiscovery']['xdp'] === true) {
d_echo($ptopo_array);
foreach ($ptopo_array as $ptopo) {
if (strcmp(trim($ptopo['ptopoConnRemoteChassis']), trim($lldp['lldpRemChassisId'])) == 0) {
$discover_hostname = hex_to_ip($ptopo['ptopoConnAgentNetAddr']);
$discover_hostname = IP::fromHexString($ptopo['ptopoConnAgentNetAddr'], true);
break;
}
}

View File

@ -13,6 +13,8 @@
*/
use LibreNMS\Exceptions\HostExistsException;
use LibreNMS\Util\IP;
use LibreNMS\Util\IPv6;
function discover_new_device($hostname, $device = '', $method = '', $interface = '')
{
@ -734,14 +736,15 @@ function discover_process_ipv6(&$valid, $ifIndex, $ipv6_address, $ipv6_prefixlen
{
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) {
if (!IPv6::isValid($ipv6_address, true)) {
// ignore link-locals (coming from IPV6-MIB)
return;
}
$ipv6 = new IPv6($ipv6_address);
$ipv6_network = $ipv6->getNetwork($ipv6_prefixlen);
$ipv6_compressed = $ipv6->compressed();
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));
@ -1181,7 +1184,7 @@ function build_bgp_peers($device, $data, $peer2)
$octets = count(explode(".", $peer_ip));
if ($octets > 11) {
// ipv6
$peer_ip = Net_IPv6::compress(snmp2ipv6(implode('.', array_slice(explode('.', $peer_ip), (count(explode('.', $peer_ip)) - 16)))));
$peer_ip = (string)IP::parse(snmp2ipv6(implode('.', array_slice(explode('.', $peer_ip), (count(explode('.', $peer_ip)) - 16)))), true);
} else {
// ipv4
$peer_ip = implode('.', array_slice(explode('.', $peer_ip), (count(explode('.', $peer_ip)) - 4)));

View File

@ -1,5 +1,7 @@
<?php
use LibreNMS\Util\IPv4;
if (key_exists('vrf_lite_cisco', $device) && (count($device['vrf_lite_cisco'])!= 0)) {
$vrfs_lite_cisco = $device['vrf_lite_cisco'];
} else {
@ -14,9 +16,9 @@ foreach ($vrfs_lite_cisco as $vrf) {
$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;
$cidr = IPv4::netmask2cidr($mask);
$network = "$oid/$cidr";
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));

View File

@ -12,6 +12,8 @@
*/
// Define some error messages
use LibreNMS\Util\IP;
$error_poolaction = array();
$error_poolaction[0] = "Unused";
$error_poolaction[1] = "Reboot";
@ -72,7 +74,7 @@ if (!is_null($ltmVirtualServEntry) || !is_null($ltmVsStatusEntry) || !is_null($l
$result['hash'] = hash('crc32', $result['UID']);
// Now that we have our UID we can pull all the other data we need.
$result['IP'] = hex_to_ip($ltmVirtualServEntry['1.3.6.1.4.1.3375.2.2.10.1.2.1.3.'.$index]);
$result['IP'] = IP::fromHexString($ltmVirtualServEntry['1.3.6.1.4.1.3375.2.2.10.1.2.1.3.'.$index], true);
$result['port'] = $ltmVirtualServEntry['1.3.6.1.4.1.3375.2.2.10.1.2.1.6.'.$index];
$result['pool'] = $ltmVirtualServEntry['1.3.6.1.4.1.3375.2.2.10.1.2.1.19.'.$index];
@ -168,7 +170,7 @@ if (!is_null($ltmVirtualServEntry) || !is_null($ltmVsStatusEntry) || !is_null($l
$result['hash'] = hash('crc32', $result['UID']);
// Now that we have our UID we can pull all the other data we need.
$result['IP'] = hex_to_ip($ltmPoolMemberEntry['1.3.6.1.4.1.3375.2.2.5.3.2.1.3.'.$index]);
$result['IP'] = IP::fromHexString($ltmPoolMemberEntry['1.3.6.1.4.1.3375.2.2.5.3.2.1.3.'.$index], true);
$result['port'] = $ltmPoolMemberEntry['1.3.6.1.4.1.3375.2.2.5.3.2.1.4.'.$index];
$result['ratio'] = $ltmPoolMemberEntry['1.3.6.1.4.1.3375.2.2.5.3.2.1.6.'.$index];
$result['weight'] = $ltmPoolMemberEntry['1.3.6.1.4.1.3375.2.2.5.3.2.1.7.'.$index];

View File

@ -11,6 +11,8 @@
* the source code distribution for details.
*/
use LibreNMS\Util\IP;
$module = 'ntp';
$component = new LibreNMS\Component();
@ -46,7 +48,7 @@ if (is_null($cntpPeersVarEntry)) {
$result['peer'] = $cntpPeersVarEntry['1.3.6.1.4.1.9.9.168.1.2.1.1'][3][$index];
$result['port'] = $cntpPeersVarEntry['1.3.6.1.4.1.9.9.168.1.2.1.1'][4][$index];
$result['stratum'] = $cntpPeersVarEntry['1.3.6.1.4.1.9.9.168.1.2.1.1'][9][$index];
$result['peerref'] = hex_to_ip($cntpPeersVarEntry['1.3.6.1.4.1.9.9.168.1.2.1.1'][15][$index]);
$result['peerref'] = IP::fromHexString($cntpPeersVarEntry['1.3.6.1.4.1.9.9.168.1.2.1.1'][15][$index], true);
$result['label'] = $result['peer'].":".$result['port'];
// Set the status, 16 = Bad

View File

@ -15,8 +15,10 @@ use LibreNMS\Exceptions\HostExistsException;
use LibreNMS\Exceptions\HostIpExistsException;
use LibreNMS\Exceptions\HostUnreachableException;
use LibreNMS\Exceptions\HostUnreachablePingException;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Exceptions\InvalidPortAssocModeException;
use LibreNMS\Exceptions\SnmpVersionUnsupportedException;
use LibreNMS\Util\IP;
function set_debug($debug)
{
@ -505,17 +507,6 @@ function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp
return $device;
}
function netmask2cidr($netmask)
{
$addr = Net_IPv4::parseAddress("1.2.3.4/$netmask");
return $addr->bitmask;
}
function cidr2netmask($netmask)
{
return (long2ip(ip2long("255.255.255.255") << (32-$netmask)));
}
function formatUptime($diff, $format = "long")
{
$yearsDiff = floor($diff/31536000);
@ -767,6 +758,7 @@ function match_network($nets, $ip, $first = false)
return $return;
}
// FIXME port to LibreNMS\Util\IPv6 class
function snmp2ipv6($ipv6_snmp)
{
$ipv6 = explode('.', $ipv6_snmp);
@ -788,16 +780,17 @@ function snmp2ipv6($ipv6_snmp)
return implode(':', $ipv6_2);
}
// FIXME port to LibreNMS\Util\IPv6 class
function ipv62snmp($ipv6)
{
$ipv6_split = array();
$ipv6_ex = explode(':', Net_IPv6::uncompress($ipv6));
for ($i = 0; $i < 8; $i++) {
$ipv6_ex[$i] = zeropad($ipv6_ex[$i], 4);
try {
$ipv6_ip = str_replace(':', '', IP::parse($ipv6)->compressed());
} catch (InvalidIpException $e) {
return '';
}
$ipv6_ip = implode('', $ipv6_ex);
for ($i = 0; $i < 32;
$i+=2) {
$ipv6_split = array();
for ($i = 0; $i < 32; $i+=2) {
$ipv6_split[] = hexdec(substr($ipv6_ip, $i, 2));
}
@ -1345,32 +1338,6 @@ function first_oid_match($device, $list)
}
/**
* Convert a hex ip to a human readable string
*
* @param string $hex
* @return string
*/
function hex_to_ip($hex)
{
$hex = str_replace(array(' ', '"'), '', $hex);
if (filter_var($hex, FILTER_VALIDATE_IP)) {
return $hex;
}
if (strlen($hex) == 8) {
return long2ip(hexdec($hex));
}
if (strlen($hex) == 32) {
$ipv6 = implode(':', str_split($hex, 4));
return preg_replace('/:0*([0-9a-fA-F])/', ':$1', $ipv6);
}
return ''; // invalid input
}
function fix_integer_value($value)
{
if ($value < 0) {

View File

@ -33,11 +33,6 @@ $install_dir = realpath(__DIR__ . '/..');
$config['install_dir'] = $install_dir;
chdir($install_dir);
if (!getenv('TRAVIS')) {
include_once 'Net/IPv4.php';
include_once 'Net/IPv6.php';
}
# composer autoload
require $install_dir . '/vendor/autoload.php';
if (version_compare(PHP_VERSION, '5.4', '>=')) {

View File

@ -1,6 +1,7 @@
<?php
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\IP;
if ($config['enable_bgp']) {
$peers = dbFetchRows('SELECT * FROM bgpPeers WHERE device_id = ?', array($device['device_id']));
@ -74,7 +75,7 @@ if ($config['enable_bgp']) {
$v = preg_replace('/(\S+\s+\S+)\s/', '$1:', $v);
$v = strtolower($v);
} else {
$v = hex_to_ip($v);
$v = IP::fromHexString($v, true);
}
}
@ -99,7 +100,7 @@ if ($config['enable_bgp']) {
$octets = count(explode(".", $peer_ip_snmp));
if ($octets > 11) {
// ipv6
$tmp_peer_ip = Net_IPv6::compress(snmp2ipv6(implode('.', array_slice(explode('.', $peer_ip_snmp), (count(explode('.', $peer_ip_snmp)) - 16)))));
$tmp_peer_ip = (string)IP::parse(snmp2ipv6(implode('.', array_slice(explode('.', $peer_ip_snmp), (count(explode('.', $peer_ip_snmp)) - 16)))), true);
} else {
// ipv4
$tmp_peer_ip = implode('.', array_slice(explode('.', $peer_ip_snmp), (count(explode('.', $peer_ip_snmp)) - 4)));
@ -130,13 +131,9 @@ if ($config['enable_bgp']) {
$bgpPeerOutTotalMessages = $peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerOutTotalMessages'];
$bgpPeerFsmEstablishedTime = $peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerFsmEstablishedTime'];
$bgpPeerInUpdateElapsedTime = $peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerInUpdatesElapsedTime'];
if ($peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerRemoteAddrType'] == 'ipv4') {
$bgpLocalAddr = long2ip(hexdec($peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerLocalAddr']));
} elseif ($peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerRemoteAddrType'] == 'ipv6') {
$ip6 = trim(str_replace(' ', '', $peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerLocalAddr']), '"');
$ip6 = substr($ip6, 0, 4).':'.substr($ip6, 4, 4).':'.substr($ip6, 8, 4).':'.substr($ip6, 12, 4).':'.substr($ip6, 16, 4).':'.substr($ip6, 20, 4).':'.substr($ip6, 24, 4).':'.substr($ip6, 28, 4);
$bgpLocalAddr = Net_IPv6::compress($ip6);
}
$bgpLocalAddr = IP::fromHexString($peer_data_tmp[$junos[$peer_ip]['hash']]['jnxBgpM2PeerLocalAddr'], true);
d_echo("State = $bgpPeerState - AdminStatus: $bgpPeerAdminStatus\n");
if ($bgpLocalAddr == '00000000000000000000000000000000') {
@ -286,7 +283,7 @@ if ($config['enable_bgp']) {
$j_prefixes = snmpwalk_cache_multi_oid($device, 'jnxBgpM2PrefixOutPrefixes', $j_prefixes, 'BGP4-V2-MIB-JUNIPER', 'junos', '-OQnU');
d_echo($j_prefixes);
}
$cbgpPeerAcceptedPrefixes = array_shift($j_prefixes['1.3.6.1.4.1.2636.5.1.1.2.6.2.1.8.'.$junos[$peer_ip]['index'].".$afis[$afi].".$safis[$safi]]);
$cbgpPeerDeniedPrefixes = array_shift($j_prefixes['1.3.6.1.4.1.2636.5.1.1.2.6.2.1.9.'.$junos[$peer_ip]['index'].".$afis[$afi].".$safis[$safi]]);
$cbgpPeerAdvertisedPrefixes = array_shift($j_prefixes['1.3.6.1.4.1.2636.5.1.1.2.6.2.1.10.'.$junos[$peer_ip]['index'].".$afis[$afi].".$safis[$safi]]);

View File

@ -25,6 +25,10 @@
namespace LibreNMS\Tests;
use LibreNMS\Util\IP;
use LibreNMS\Util\IPv4;
use LibreNMS\Util\IPv6;
class CommonFunctionsTest extends \PHPUnit_Framework_TestCase
{
public function testStrContains()
@ -95,16 +99,6 @@ class CommonFunctionsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(2, set_null(2, 0, 2));
}
public function testIsIp()
{
$this->assertTrue(is_ip('192.168.0.1'));
$this->assertTrue(is_ip('192.168.0.1', 'ipv4'));
$this->assertTrue(is_ip('2001:4860:4860::8888', 'ipv6'));
$this->assertFalse(is_ip('2001:4860:4860::8888', 'ipv4'));
$this->assertFalse(is_ip('192.168.0.1', 'ipv6'));
$this->assertFalse(is_ip('not_an_ip'));
}
public function testDisplay()
{
$this->assertEquals('&lt;html&gt;string&lt;/html&gt;', display('<html>string</html>'));

View File

@ -50,22 +50,4 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase
$this->assertFalse(isHexString('a5 fj 53'));
$this->assertFalse(isHexString('a5fe53'));
}
public function testHexToIp()
{
$this->assertSame("192.168.1.254", hex_to_ip("c0 a8 01 fe"));
$this->assertSame("192.168.1.254", hex_to_ip("c0a801fe"));
$this->assertSame("192.168.1.254", hex_to_ip("c0 a8 01 fe "));
$this->assertNotSame("192.168.1.1.254", hex_to_ip("c0 a8 01 01 fe"));
$this->assertSame("192.168.1.254", hex_to_ip("\"c0 a8 01 fe\""));
$this->assertSame("192.168.1.254", hex_to_ip("192.168.1.254"));
$this->assertSame('2001:db8:0:0:0:0:2:1', hex_to_ip('2001:db8:0:0:0:0:2:1'));
$this->assertSame('2001:db8:0:0:0:0:2:1', hex_to_ip('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01'));
$this->assertSame('2001:db8:0:0:0:0:2:1', hex_to_ip('20010db8000000000000000000020001'));
$this->assertNotSame(
'2001:db8:0:0:0:0:2:0:1',
hex_to_ip('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 00 00 01')
);
}
}

170
tests/IpTest.php Normal file
View File

@ -0,0 +1,170 @@
<?php
/**
* IpTest.php
*
* Tests Util\IP classes
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
use LibreNMS\Util\IP;
use LibreNMS\Util\IPv4;
use LibreNMS\Util\IPv6;
class IpTest extends \PHPUnit\Framework\TestCase
{
public function testIsValid()
{
$this->assertTrue(IP::isValid('192.168.0.1'));
$this->assertTrue(IP::isValid('192.168.0.1'));
$this->assertTrue(IP::isValid('2001:4860:4860::8888'));
$this->assertTrue(IPv4::isValid('192.168.0.1'));
$this->assertTrue(IPv6::isValid('2001:4860:4860::8888'));
$this->assertFalse(IPv4::isValid('2001:4860:4860::8888'));
$this->assertFalse(IPv6::isValid('192.168.0.1'));
$this->assertFalse(IP::isValid('not_an_ip'));
$this->assertTrue(IPv4::isValid('8.8.8.8', true));
$this->assertTrue(IP::isValid('8.8.8.8', true));
$this->assertTrue(IPv4::isValid('192.168.0.1', true));
$this->assertTrue(IPv6::isValid('FF81::', true));
$this->assertTrue(IPv6::isValid('2001:db8:85a3::8a2e:370:7334', true));
$this->assertFalse(IPv4::isValid('127.0.0.1', true));
$this->assertFalse(IPv6::isValid('::1', true));
$this->assertFalse(IP::isValid('169.254.1.1', true));
$this->assertFalse(IP::isValid('fe80::1', true));
$this->assertFalse(IPv4::isValid('fe80::1', true));
$this->assertFalse(IP::isValid('Falafel', true));
}
public function testIpParse()
{
$this->assertEquals('192.168.0.1', IP::parse('192.168.0.1'));
$this->assertEquals('127.0.0.1', IP::parse('127.0.0.1'));
$this->assertEquals('2001:db8:85a3::8a2e:370:7334', IP::parse('2001:db8:85a3::8a2e:370:7334'));
$this->assertEquals('::1', IP::parse('::1'));
$this->assertEquals('192.168.0.1', new IPv4('192.168.0.1'));
$this->assertEquals('127.0.0.1', new IPv4('127.0.0.1'));
$this->assertEquals('2001:db8:85a3::8a2e:370:7334', new IPv6('2001:db8:85a3::8a2e:370:7334'));
$this->assertEquals('::1', new IPv6('::1'));
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
new IPv6('192.168.0.1');
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
new IPv6('127.0.0.1');
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
new IPv4('2001:db8:85a3::8a2e:370:7334');
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
new IPv4('::1');
}
public function testHexToIp()
{
$this->assertEquals("192.168.1.254", IP::fromHexString("c0 a8 01 fe"));
$this->assertEquals("192.168.1.254", IP::fromHexString("c0a801fe"));
$this->assertEquals("192.168.1.254", IP::fromHexString("c0 a8 01 fe "));
$this->assertEquals("192.168.1.254", IP::fromHexString("\"c0 a8 01 fe\""));
$this->assertEquals("192.168.1.254", IP::fromHexString("192.168.1.254"));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('2001:db8::2:1'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20:01:0d:b8:00:00:00:00:00:00:00:00:00:02:00:01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('"20.01.0d.b8.00.00.00.00.00.00.00.00.00.02.00.01"'));
$this->assertEquals('2001:db8::2:1', IP::fromHexString('20010db8000000000000000000020001'));
$this->assertEquals('::', IP::fromHexString('00000000000000000000000000000000'));
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
IP::fromHexString("c0 a8 01 01 fe");
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
IP::fromHexString('20 01 0d b8 00 00 00 00 00 00 00 00 00 02 00 00 00 01');
}
public function testNetmask2Cidr()
{
$this->assertSame(32, IPv4::netmask2cidr('255.255.255.255'));
$this->assertSame(30, IPv4::netmask2cidr('255.255.255.252'));
$this->assertSame(26, IPv4::netmask2cidr('255.255.255.192'));
$this->assertSame(16, IPv4::netmask2cidr('255.255.0.0'));
}
public function testIpInNetwork()
{
$this->assertTrue(IP::parse('192.168.1.0')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.32')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.254')->inNetwork('192.168.1.0/24'));
$this->assertTrue(IP::parse('192.168.1.255')->inNetwork('192.168.1.0/24'));
$this->assertFalse(IP::parse('192.168.1.1')->inNetwork('192.168.1.0'));
$this->assertFalse(IP::parse('10.4.3.2')->inNetwork('192.168.1.0/16'));
$this->assertTrue(IP::parse('::1')->inNetwork('::/64'));
$this->assertTrue(IP::parse('2001:db7:85a3::8a2e:370:7334')->inNetwork('::/0'));
$this->assertFalse(IP::parse('2001:db7:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::/64'));
$this->assertTrue(IP::parse('2001:db8:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::/64'));
$this->assertTrue(IP::parse('2001:db8:85a3::8a2e:370:7334')->inNetwork('2001:db8:85a3::8a2e:370:7334/128'));
$this->assertFalse(IP::parse('2001:db8:85a3::8a2e:370:7335')->inNetwork('2001:db8:85a3::8a2e:370:7334/128'));
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('42')->inNetwork('192.168.1.0/4');
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('192.168.1.256')->inNetwork('192.168.1.0/24');
$this->setExpectedException('LibreNMS\Exceptions\InvalidIpException');
IP::parse('192.168.1.0')->inNetwork('192.168.1.0');
}
public function testIpv6Compress()
{
$this->assertEquals('::1', IP::parse('0:0:0:0:0:0:0:1'));
$this->assertSame('::1', IP::parse('0:0:0:0:0:0:0:1')->compressed());
$this->assertSame('::', IP::parse('0:0:0:0:0:0:0:0')->compressed());
$this->assertSame('::', IP::parse('0000:0000:0000:0000:0000:0000:0000:0000')->compressed());
$this->assertSame('2001:db8:85a3::8a2e:370:7334', IP::parse('2001:0db8:85a3:0000:0000:8a2e:0370:7334')->compressed());
}
public function testIpv6Uncompress()
{
$this->assertSame('0000:0000:0000:0000:0000:0000:0000:0001', IP::parse('::1')->uncompressed());
$this->assertSame('0000:0000:0000:0000:0000:0000:0000:0000', IP::parse('::')->uncompressed());
$this->assertSame('2001:0db8:85a3:0000:0000:8a2e:0370:7334', IP::parse('2001:db8:85a3::8a2e:370:7334')->uncompressed());
$this->assertSame('2001:0db8:85a3:0001:0001:8a2e:0370:7334', IP::parse('2001:db8:85a3:1:1:8a2e:370:7334')->uncompressed());
}
public function testNetworkFromIp()
{
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.34')->getNetwork(24));
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.0/24')->getNetwork());
$this->assertSame('192.168.1.0/24', IP::parse('192.168.1.255/24')->getNetwork());
$this->assertSame('192.168.1.0', IP::parse('192.168.1.34')->getNetworkAddress(24));
$this->assertSame('192.168.16.0/20', IP::parse('192.168.23.45')->getNetwork(20));
$this->assertSame('2001:db8:85a3::/64', IP::parse('2001:db8:85a3:0:341a:8a2e:0370:7334')->getNetwork(64));
$this->assertSame('2001:db8:85a3:3400::/54', IP::parse('2001:db8:85a3:369a::370:7334/54')->getNetwork());
$this->assertSame('2001:db8:85a3:3600::/55', IP::parse('2001:db8:85a3:369a::370:7334/55')->getNetwork());
$this->assertSame('2001:db8:85a3:341a::370:7334/128', IP::parse('2001:db8:85a3:341a::370:7334')->getNetwork());
$this->assertSame('2001:db8:85a3:341a::370:7334', IP::parse('2001:db8:85a3:341a::370:7334/128')->getNetworkAddress());
}
}

View File

@ -136,13 +136,6 @@ if (!($username === 'root' || (isset($config['user']) && $username === $config['
print_fail('You need to run this script as root' . (isset($config['user']) ? ' or '.$config['user'] : ''));
}
if (class_exists('Net_IPv4') === false) {
print_fail("It doesn't look like Net_IPv4 is installed");
}
if (class_exists('Net_IPv6') === false) {
print_fail("It doesn't look like Net_IPv6 is installed");
}
// Let's test the user configured if we have it
if (isset($config['user'])) {
$tmp_user = $config['user'];