Files
librenms-librenms/LibreNMS/Util/IPv6.php
Tony Murray 1d777693e1 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)
2017-08-08 14:14:58 -05:00

151 lines
4.4 KiB
PHP

<?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));
}
}