From 6872baa9a92b6efb6bd1f20fd8ea376e7a6149f9 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Mon, 15 Jan 2018 14:10:18 +0000 Subject: [PATCH] ipaddresses : Manage the IPv4 blocks in IPv6 : ::ffff:192.168.1.2 git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4033 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- ipaddresses.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ipaddresses.php b/ipaddresses.php index ee7082b..4936b2b 100644 --- a/ipaddresses.php +++ b/ipaddresses.php @@ -162,6 +162,8 @@ class ipaddresses * Based on http://www.weberdev.com/get_example.php3?ExampleID=3921 * @param string $ip The IP address to uncompress * Example : $ip="::" => return "0:0:0:0:0:0:0:0" + * Example : $ip = "::ffff:127.0.0.1", + * return "0:0:0:0:0:0:ffff:7f00:1" */ public function uncompressIPv6 ($ip) { @@ -195,6 +197,19 @@ class ipaddresses if (substr_count ($ip, ":") !== 7) throw new \Exception ("uncompressIPv6: Invalid IP provided", 500); } + if (substr ($ip, 0, 17) === "0:0:0:0:0:0:ffff:") + { + // Manage the IPv4 blocks in IPv6 : ::ffff:192.168.1.2 + // If the IP is already in valid IPv6 (without dots), skip this part + $ipv4Block = substr ($ip, 17); + if (strpos ($ipv4Block, ".") !== false) + { + @list ($ip1, $ip2, $ip3, $ip4) = @explode (".", $ipv4Block); + // remove the first 0: as there is 2 nibbles for the IPv4 address + $ip = substr ($ip, 2, 15); + $ip .= sprintf ("%x:%x", $ip1 * 256 + $ip2, $ip3 * 256 + $ip4); + } + } return $ip; } @@ -232,6 +247,8 @@ class ipaddresses * @param string $ip The IP to complete * @return string The address in nibbles * Example : $ip = "::", return "0000:0000:0000:0000:0000:0000:0000:0000" + * Example : $ip = "::ffff:127.0.0.1", + * return "0000:0000:0000:0000:0000:0000:ffff:7f00:0001" */ public function completeAddressWithZero ($ip) { @@ -457,8 +474,9 @@ class ipaddresses */ private function networkFirstLastIP ($ip, $cidr, $map) { +echo "$ip, $cidr, $map\n"; if ($this->validIPAddress ($ip) === false) - throw new \Excpetion (dgettext("domframework", "Invalid IP address"), + throw new \Exception (dgettext("domframework", "Invalid IP address"), 500); $ipv4 = $this->validIPv4Address ($ip); if ($ipv4 === true) @@ -488,6 +506,7 @@ class ipaddresses { $ip = $this->completeAddressWithZero ($ip); $ip = str_replace (":", "", $ip); +echo strlen ($ip)."\n"; $ipArr = str_split ($ip, 2); $ipBin = ""; foreach ($ipArr as $ip) @@ -498,6 +517,7 @@ class ipaddresses for ($i = $cidr ; $i < 128 ; $i++) $cidrBin .= "0"; } +echo strlen ($ipBin)."\n"; // Get the binary value of IP if the mask is 1 or put $map if the mask is 0 $ipBaseBin = ""; for ($i = 0 ; $i < strlen ($ipBin) ; $i++)