/** * Create a new instance for the given IPv6 ddress * * @param string $addr The IPv6 address */ public function __construct($addr) { if (!IPv6Address::validate($addr)) { throw new \InvalidArgumentException("Not a valid IPv6 address."); } $this->addr = $addr; }
/** * Checks if the give IPv6 Address is part of the subnet. * * @param string The IPv6 address to check */ public function isInSubnet($ipv6addr) { if (!IPv6Address::validate($ipv6addr)) { throw new \InvalidArgumentException("Not a valid IPv6 address."); } $bytes_addr = unpack("n*", inet_pton($this->addr)); $bytes_test = unpack("n*", inet_pton($ipv6addr)); for ($i = 1; $i <= ceil($this->preflen / 16); $i++) { $left = $this->preflen - 16 * ($i - 1); $left = $left <= 16 ? $left : 16; $mask = ~(0xffff >> $left) & 0xffff; if (($bytes_addr[$i] & $mask) != ($bytes_test[$i] & $mask)) { return false; } } return true; }