public static function IpUtils::checkIp6

Validates an IPv6 address.

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

Parameters

string $requestIp:

string $ip:

Return value

boolean Whether the IP is valid

Throws

\RuntimeException When IPV6 support is not enabled

See also

https://github.com/dsp/v6tools

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/IpUtils.php, line 88

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;
}