summaryrefslogtreecommitdiff
path: root/inc/lib/IP
diff options
context:
space:
mode:
authorJano Slota <jano@alokal.eu>2014-06-30 15:58:26 +0200
committerczaks <marcin@6irc.net>2014-07-06 01:25:37 +0200
commitdca7570b32c6d17024684f99371b0659d785b02e (patch)
tree24ab3def10b50227e5d2ab17ecfe192a874489ac /inc/lib/IP
parent6a3dbe5c983e55adf098abe261d7ee100c16c2e6 (diff)
Update jQuery UI to 1.11.0, GeoIPv6 and IP library
Diffstat (limited to 'inc/lib/IP')
-rwxr-xr-xinc/lib/IP/Lifo/IP/CIDR.php30
-rwxr-xr-xinc/lib/IP/Lifo/IP/IP.php44
2 files changed, 53 insertions, 21 deletions
diff --git a/inc/lib/IP/Lifo/IP/CIDR.php b/inc/lib/IP/Lifo/IP/CIDR.php
index e8fe32ce..5ed92f93 100755
--- a/inc/lib/IP/Lifo/IP/CIDR.php
+++ b/inc/lib/IP/Lifo/IP/CIDR.php
@@ -254,7 +254,9 @@ class CIDR
* and the starting IP is not on a valid network boundrary (eg: Displaying
* an IP from an interface).
*
- * @return string IP in CIDR notation "ipaddr/prefix"
+ * <b>Note: The CIDR block returned is NOT always bit aligned.</b>
+ *
+ * @return string IP in CIDR notation "start_ip/prefix"
*/
public function getCidr()
{
@@ -262,6 +264,22 @@ class CIDR
}
/**
+ * Get the TRUE cidr notation for the subnet block.
+ *
+ * This is useful for when you want a string representation of the IP/prefix
+ * and the starting IP is not on a valid network boundrary (eg: Displaying
+ * an IP from an interface).
+ *
+ * <b>Note: The CIDR block returned is ALWAYS bit aligned.</b>
+ *
+ * @return string IP in CIDR notation "network/prefix"
+ */
+ public function getTrueCidr()
+ {
+ return $this->getNetwork() . '/' . $this->prefix;
+ }
+
+ /**
* Get the [low,high] range of the CIDR block
*
* Prefix sensitive.
@@ -382,8 +400,9 @@ class CIDR
{
// use fixed length HEX strings so we can easily do STRING comparisons
// instead of using slower bccomp() math.
- list($lo,$hi) = array_map(function($v){ return sprintf("%032s", IP::inet_ptoh($v)); }, CIDR::cidr_to_range($ip));
- list($min,$max) = array_map(function($v){ return sprintf("%032s", IP::inet_ptoh($v)); }, CIDR::cidr_to_range($cidr));
+ $map = function($v){ return sprintf("%032s", IP::inet_ptoh($v)); };
+ list($lo,$hi) = array_map($map, CIDR::cidr_to_range($ip));
+ list($min,$max) = array_map($map, CIDR::cidr_to_range($cidr));
/** visualization of logic used below
lo-hi = $ip to check
@@ -447,7 +466,8 @@ class CIDR
}
// force bit length to 32 or 128 depending on type of IP
- $bitlen = (false === filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ? 128 : 32;
+ $version = IP::isIPv4($ip) ? 4 : 6;
+ $bitlen = $version == 4 ? 32 : 128;
if ($bits === null) {
// if no prefix is given use the length of the binary string which
@@ -469,7 +489,7 @@ class CIDR
// calculate "broadcast" (not technically a broadcast in IPv6)
$ip2 = BC::bcor($ip1, BC::bcnot($netmask));
- return array(IP::inet_dtop($ip1), IP::inet_dtop($ip2));
+ return array(IP::inet_dtop($ip1, $version), IP::inet_dtop($ip2, $version));
}
/**
diff --git a/inc/lib/IP/Lifo/IP/IP.php b/inc/lib/IP/Lifo/IP/IP.php
index 4d22aa76..1af587ff 100755
--- a/inc/lib/IP/Lifo/IP/IP.php
+++ b/inc/lib/IP/Lifo/IP/IP.php
@@ -26,6 +26,10 @@ abstract class IP
{
// shortcut for IPv4 addresses
if (strpos($ip, ':') === false && strpos($ip, '.') !== false) {
+ // remove any cidr block notation
+ if (($o = strpos($ip, '/')) !== false) {
+ $ip = substr($ip, 0, $o);
+ }
return sprintf('%u', ip2long($ip));
}
@@ -55,8 +59,11 @@ abstract class IP
/**
* Convert a decimal string into a human readable IP address.
+ *
+ * @param decimal $decimal Decimal number to convert into presentational IP string.
+ * @param integer $version Force IP version to 4 or 6. Leave null for automatic.
*/
- public static function inet_dtop($decimal, $expand = false)
+ public static function inet_dtop($decimal, $version = null)
{
$parts = array();
$parts[1] = bcdiv($decimal, '79228162514264337593543950336', 0); // >> 96
@@ -74,25 +81,27 @@ abstract class IP
$part = (int) $part;
}
- // if the first 96bits is all zeros then we can safely assume we
- // actually have an IPv4 address. Even though it's technically possible
- // you're not really ever going to see an IPv6 address in the range:
- // ::0 - ::ffff
- // It's feasible to see an IPv6 address of "::", in which case the
- // caller is going to have to account for that on their own.
- if (($parts[1] | $parts[2] | $parts[3]) == 0) {
+ if (!$version) {
+ // if the first 96bits is all zeros then we can safely assume we
+ // actually have an IPv4 address. Even though it's technically possible
+ // you're not really ever going to see an IPv6 address in the range:
+ // ::0 - ::ffff
+ // It's feasible to see an IPv6 address of "::", in which case the
+ // caller is going to have to account for that on their own (or
+ // pass $version to this function).
+ if (($parts[1] | $parts[2] | $parts[3]) == 0) {
+ $version = 4;
+ }
+ }
+
+ if ($version == 4) {
$ip = long2ip($parts[4]);
} else {
$packed = pack('N4', $parts[1], $parts[2], $parts[3], $parts[4]);
$ip = inet_ntop($packed);
}
- // Turn IPv6 to IPv4 if it's IPv4
- if (preg_match('/^::\d+\./', $ip)) {
- return substr($ip, 2);
- }
-
- return $expand ? self::inet_expand($ip) : $ip;
+ return $ip;
}
/**
@@ -107,8 +116,11 @@ abstract class IP
/**
* Convert a human readable (presentational) IP address into a BINARY string.
*/
- public static function inet_ptob($ip, $bits = 128)
+ public static function inet_ptob($ip, $bits = null)
{
+ if ($bits === null) {
+ $bits = self::isIPv4($ip) ? 32 : 128;
+ }
return BC::bcdecbin(self::inet_ptod($ip), $bits);
}
@@ -157,7 +169,7 @@ abstract class IP
* One use-case for this is IP 6to4 tunnels used in networking.
*
* @example
- * to_ipv4("10.10.10.10") == a0a:a0a
+ * to_ipv6("10.10.10.10") == a0a:a0a
*
* @param string $ip IPv4 address.
* @param boolean $mapped If true a Full IPv6 address is returned within the