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
This commit is contained in:
2018-01-15 14:10:18 +00:00
parent 22b21450d2
commit 6872baa9a9

View File

@@ -162,6 +162,8 @@ class ipaddresses
* Based on http://www.weberdev.com/get_example.php3?ExampleID=3921 * Based on http://www.weberdev.com/get_example.php3?ExampleID=3921
* @param string $ip The IP address to uncompress * @param string $ip The IP address to uncompress
* Example : $ip="::" => return "0:0:0:0:0:0:0:0" * 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) public function uncompressIPv6 ($ip)
{ {
@@ -195,6 +197,19 @@ class ipaddresses
if (substr_count ($ip, ":") !== 7) if (substr_count ($ip, ":") !== 7)
throw new \Exception ("uncompressIPv6: Invalid IP provided", 500); 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; return $ip;
} }
@@ -232,6 +247,8 @@ class ipaddresses
* @param string $ip The IP to complete * @param string $ip The IP to complete
* @return string The address in nibbles * @return string The address in nibbles
* Example : $ip = "::", return "0000:0000:0000:0000:0000:0000:0000:0000" * 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) public function completeAddressWithZero ($ip)
{ {
@@ -457,8 +474,9 @@ class ipaddresses
*/ */
private function networkFirstLastIP ($ip, $cidr, $map) private function networkFirstLastIP ($ip, $cidr, $map)
{ {
echo "$ip, $cidr, $map\n";
if ($this->validIPAddress ($ip) === false) if ($this->validIPAddress ($ip) === false)
throw new \Excpetion (dgettext("domframework", "Invalid IP address"), throw new \Exception (dgettext("domframework", "Invalid IP address"),
500); 500);
$ipv4 = $this->validIPv4Address ($ip); $ipv4 = $this->validIPv4Address ($ip);
if ($ipv4 === true) if ($ipv4 === true)
@@ -488,6 +506,7 @@ class ipaddresses
{ {
$ip = $this->completeAddressWithZero ($ip); $ip = $this->completeAddressWithZero ($ip);
$ip = str_replace (":", "", $ip); $ip = str_replace (":", "", $ip);
echo strlen ($ip)."\n";
$ipArr = str_split ($ip, 2); $ipArr = str_split ($ip, 2);
$ipBin = ""; $ipBin = "";
foreach ($ipArr as $ip) foreach ($ipArr as $ip)
@@ -498,6 +517,7 @@ class ipaddresses
for ($i = $cidr ; $i < 128 ; $i++) for ($i = $cidr ; $i < 128 ; $i++)
$cidrBin .= "0"; $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 // Get the binary value of IP if the mask is 1 or put $map if the mask is 0
$ipBaseBin = ""; $ipBaseBin = "";
for ($i = 0 ; $i < strlen ($ipBin) ; $i++) for ($i = 0 ; $i < strlen ($ipBin) ; $i++)