protected function RequestMatcher::checkIp6

Validates an IPv6 address.

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

Parameters

string $requestIp:

string $ip:

Return value

boolean True valid, false if not.

See also

https://github.com/dsp/v6tools

1 call to RequestMatcher::checkIp6()
RequestMatcher::checkIp in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RequestMatcher.php
Validates an IP address.

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RequestMatcher.php, line 199

Class

RequestMatcher
RequestMatcher compares a pre-defined set of checks against a Request instance.

Namespace

Symfony\Component\HttpFoundation

Code

protected function checkIp6($requestIp, $ip) {
  if (!defined('AF_INET6')) {
    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;
}