Change IPv4 string to IPv6

Sander Steffann is mentioned in my previous question :

Addresses like 0000: 0000: 0000: 0000: 0000: 0000: 192.168.0.1 are written as 0000: 0000: 0000: 0000: 0000: 0000: c0a8: 0001, which is the exact same address in hexadecimal notation.

How can I detect in PHP if the address was written like this: ::0000:192.168.0.1or 0000::0000:192.168.0.1or 0000:0000:0000:0000:0000:0000:192.168.0.1etc.? Is it enough to check if the string is based on IP. AND ':'?

And how do I change this to a full line 0000:0000:0000:0000:0000:0000:c0a8:0001?

Is it correct to change this to IPv4, something like:

<?php 
$strIP = '0000:0000:0000:0000:0000:0000:192.168.0.1';

$strResult = substr($strIP, strrpos($strIP, ':'));

echo $strResult; //192.168.0.1 ?
?>

... or are the correct representations of IP strings more complex than this snippet can do?

+5
3

: , ? inet_pton() , , , .

, ::192.168.0.1 0000:0000:0000:0000:0000:0000:c0a8:0001, . , .

0000:0000:0000:0000:0000:0000:192.168.0.1 inet_pton(), inet_ntop(), IPv6, ::192.168.0.1. ::, : , , IPv4-; -)

:

function expand_ip_address($addr_str) {
  /* First convert to binary, which also does syntax checking */
  $addr_bin = @inet_pton($addr_str);
  if ($addr_bin === FALSE) {
    return FALSE;
  }

  $addr_hex = bin2hex($addr_bin);

  /* See if this is an IPv4-Compatible IPv6 address (deprecated) or an
     IPv4-Mapped IPv6 Address (used when IPv4 connections are mapped to
     an IPv6 sockets and convert it to a normal IPv4 address */
  if (strlen($addr_bin) == 16
  &&  substr($addr_hex, 0, 20) == str_repeat('0', 20)) {
    /* First 80 bits are zero: now see if bits 81-96 are either all 0 or all 1 */
    if (substr($addr_hex, 20, 4) == '0000')
    ||  substr($addr_hex, 20, 4) == 'ffff')) {
      /* Remove leading bits so only the IPv4 bits remain */
      $addr_bin = substr($addr_hex, 12);
    }
  }

  /* Then differentiate between IPv4 and IPv6 */
  if (strlen($addr_bin) == 4) {
    /* IPv4: print each byte as 3 digits and add dots between them */
    $ipv4_bytes = str_split($addr_bin);
    $ipv4_ints = array_map('ord', $ipv4_bytes);
    return vsprintf('%03d.%03d.%03d.%03d', $ipv4_ints);
  } else {
    /* IPv6: print as hex and add colons between each group of 4 hex digits */
    return implode(':', str_split($addr_hex, 4));
  }
}
+2

, inet_pton, , , .

$foo = inet_pton("::1");
for ($i = 0 ; $i < 8 ; $i++)
    $arr[$i] = sprintf("%02x%02x", ord($foo[$i * 2]), ord($foo[$i * 2 + 1]));
$addr = implode(":", $arr);
+3

I can’t believe that I wrote it all in one go, and it worked the first time.

$strIP = '0000:0000:0000:0000:0000:0000:192.168.0.1';
$arrIP = explode(':', $strIP);

if( preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $arrIP[count($arrIP)-1]) ) {
  $ip4parts = explode('.', $arrIP[count($arrIP)-1]);
  $ip6trans = sprintf("%02x%02x:%02x%02x", $ip4parts[0], $ip4parts[1], $ip4parts[2], $ip4parts[3]);
  $arrIP[count($arrIP)-1] = $ip6trans;

  $strIP = implode(':', $arrIP);
}
echo $strIP; //output: 0000:0000:0000:0000:0000:0000:c0a8:0001

Basically:

  • Expand the line to :
  • Check if the last quad is formatted as an IP4 address
  • Expand the last quad to .
  • Retype IP4 Octets into Two Hexagon Squares
  • Replace quad IP4 with new ones
  • Import array to :.
+2
source

All Articles