Search code examples
phpip-addressipv6

Shortening an IPv6 address ending in 0's via PHP


I'm using an IPv6 class found on GitHub to do some IP manipulation but I noticed that there is an issue with shortening certain address, typically ending in 0.

When I enter the address 2001::6dcd:8c74:0:0:0:0, it results in 2001::6dcd:8c74::::.

$address = '2001::6dcd:8c74:0:0:0:0';
// Check to see if address is already compacted
if (strpos($address, '::') === FALSE) {
    $parts = explode(':', $address);
    $new_parts = array();
    $ignore = FALSE;
    $done = FALSE;

    for ($i = 0; $i < count($parts); $i++) {
        if (intval(hexdec($parts[$i])) === 0 && $ignore == FALSE && $done == FALSE) {
            $ignore = TRUE;
            $new_parts[] = '';
            if ($i == 0) {
                $new_parts = '';
            }
        } else if (intval(hexdec($parts[$i])) === 0 && $ignore == TRUE && $done == FALSE) {
            continue;
        } else if (intval(hexdec($parts[$i])) !== 0 && $ignore == TRUE) {
            $done = TRUE;
            $ignore = FALSE;
            $new_parts[] = $parts[$i];
        } else {
            $new_parts[] = $parts[$i];
        }
    }

    // Glue everything back together
    $address = implode(':', $new_parts);

}

// Remove the leading 0's
$new_address = preg_replace("/:0{1,3}/", ":", $address);

// $this->compact = $new_address;

// return $this->compact;
echo $new_address; // Outputs: 2001::6dcd:8c74::::

Solution

  • Without that problem line at the bottom you get 2001::6dcd:8c74:0:0:0:0. Now, before the leading 0's are all replaced, the function checks to see if the address ends in :0 before removing all leading 0's.

    if (substr($address, -2) != ':0') {
        $new_address = preg_replace("/:0{1,3}/", ":", $address);
    } else {
        $new_address = $address;
    }
    

    Another check is added to catch other possible valid IPv6 addresses from being malformed.

    if (isset($new_parts)) {
        if (count($new_parts) < 8 && array_pop($new_parts) == '') {
            $new_address .= ':0';
        }
    }
    

    The new full function looks like this:

    // Check to see if address is already compacted
    if (strpos($address, '::') === FALSE) {
        $parts = explode(':', $address);
        $new_parts = array();
        $ignore = FALSE;
        $done = FALSE;
    
        for ($i = 0; $i < count($parts); $i++) {
            if (intval(hexdec($parts[$i])) === 0 && $ignore == FALSE && $done == FALSE) {
                $ignore = TRUE;
                $new_parts[] = '';
                if ($i == 0) {
                    $new_parts = '';
                }
            } else if (intval(hexdec($parts[$i])) === 0 && $ignore == TRUE && $done == FALSE) {
                continue;
            } else if (intval(hexdec($parts[$i])) !== 0 && $ignore == TRUE) {
                $done = TRUE;
                $ignore = FALSE;
                $new_parts[] = $parts[$i];
            } else {
                $new_parts[] = $parts[$i];
            }
        }
    
        // Glue everything back together
        $address = implode(':', $new_parts);
    
    }
    // Check to see if this ends in a shortened :0 before replacing all
    // leading 0's
    if (substr($address, -2) != ':0') {
        // Remove the leading 0's
        $new_address = preg_replace("/:0{1,3}/", ":", $address);
    } else {
        $new_address = $address;
    }
    // Since new_parts isn't always set, check to see if it's set before
    // trying to fix possibly broken shortened addresses ending in 0.
    // (Ex: Trying to shorten 2001:19f0::0 will result in unset array)
    if (isset($new_parts)) {
        // Some addresses (Ex: starting addresses for a range) can end in
        // all 0's resulting in the last value in the new parts array to be
        // an empty string.  Catch that case here and add the remaining :0
        // for a complete shortened address.
        if (count($new_parts) < 8 && array_pop($new_parts) == '') {
            $new_address .= ':0';
        }
    }
    
    // $this->compact = $new_address;
    
    // return $this->compact;
    echo $new_address; // Outputs: 2001::6dcd:8c74:0:0:0:0
    

    It's not the cleanest solution and could possibly have holes in its logic depending on what the address is. If I find any other issues with it I will update this question/answer.