mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* 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)
123 lines
3.3 KiB
PHP
123 lines
3.3 KiB
PHP
<?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));
|
|
}
|
|
}
|