forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: New IP parsing classes. Removes usage of Pear Net_IPv4 and N…
…et_IPv6. (librenms#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)
- Loading branch information
Showing
30 changed files
with
771 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
*/ | ||
|
||
namespace LibreNMS\Exceptions; | ||
|
||
class InvalidIpException extends \Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
*/ | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <[email protected]> | ||
*/ | ||
|
||
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)); | ||
} | ||
} |
Oops, something went wrong.