/** * Convert an IPv4 address into an IPv6 address. * * One use-case for this is IP 6to4 tunnels used in networking. * * @example * 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 * official ipv4to6 mapped space "0:0:0:0:0:ffff:x:x" */ public static function to_ipv6($ip, $mapped = false) { if (!self::isIPv4($ip)) { throw new \InvalidArgumentException("Invalid IPv4 address \"{$ip}\""); } $num = IP::inet_ptod($ip); $o1 = dechex($num >> 16); $o2 = dechex($num & 0xffff); return $mapped ? "0:0:0:0:0:ffff:{$o1}:{$o2}" : "{$o1}:{$o2}"; }
/** * Return the network mask for the prefix given. * * Normally this is only useful for IPv4 addresses but you can generate a * mask for IPv6 addresses as well, only because its mathematically * possible. * * @param integer $prefix CIDR prefix bits (0-128) * @param integer $version IP version. If null the version will be detected * based on the prefix length given. */ public static function prefix_to_mask($prefix, $version = null) { if ($version === null) { $version = $prefix > 32 ? 6 : 4; } if ($prefix < 0 or $prefix > 128) { throw new \InvalidArgumentException("Invalid prefix length \"{$prefix}\""); } if ($version != 4 and $version != 6) { throw new \InvalidArgumentException("Invalid version \"{$version}\". Must be 4 or 6"); } if ($version == 4) { return long2ip($prefix == 0 ? 0 : 0xffffffff >> 32 - $prefix << 32 - $prefix); } else { return IP::inet_dtop($prefix == 0 ? 0 : BC::bcleft(BC::bcright(BC::MAX_UINT_128, 128 - $prefix), 128 - $prefix)); } }