You are here

public static function IpUtils::checkIp6 in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/IpUtils.php \Symfony\Component\HttpFoundation\IpUtils::checkIp6()

Compares two IPv6 addresses. In case a subnet is given, it checks if it contains the request IP.

@author David Soria Parra <dsp at php dot net>

Parameters

string $requestIp IPv6 address to check:

string $ip IPv6 address or subnet in CIDR notation:

Return value

bool Whether the IP is valid

Throws

\RuntimeException When IPV6 support is not enabled

See also

https://github.com/dsp/v6tools

File

vendor/symfony/http-foundation/IpUtils.php, line 98

Class

IpUtils
Http utility functions.

Namespace

Symfony\Component\HttpFoundation

Code

public static function checkIp6($requestIp, $ip) {
  if (!(extension_loaded('sockets') && defined('AF_INET6') || @inet_pton('::1'))) {
    throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
  }
  if (false !== strpos($ip, '/')) {
    list($address, $netmask) = explode('/', $ip, 2);
    if ($netmask < 1 || $netmask > 128) {
      return false;
    }
  }
  else {
    $address = $ip;
    $netmask = 128;
  }
  $bytesAddr = unpack('n*', inet_pton($address));
  $bytesTest = unpack('n*', inet_pton($requestIp));
  for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
    $left = $netmask - 16 * ($i - 1);
    $left = $left <= 16 ? $left : 16;
    $mask = ~(0xffff >> $left) & 0xffff;
    if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
      return false;
    }
  }
  return true;
}