validate an IPv6 integer netmask
340995
def __ipv6_netmask(value):
    ''''''
    valid, errmsg = False, 'IPv6 netmask (0->128)'
    valid, value, _ = __int(value)
    valid = (valid and 0 <= value <= 128)
    return (valid, value, errmsg)
403088
def ip_bracket(addr):
    '''
    
    '''
    addr = ipaddress.ip_address(addr)
    return ('[{}]' if addr.version == 6 else '{}').format(addr)
262069
def _validate_cidr(self, rule):
        
        try:
            network = ipaddress.IPv4Network(rule['app'])
        except (ipaddress.NetmaskValueError, ValueError) as error:
            raise SpinnakerSecurityGroupCreationFailed(error)

        self.log.debug('Validating CIDR: %s', network.exploded)

        return True
725394
def is_ipv6(address):
    """"""
    try:
        address = netaddr.IPAddress(address)
    except netaddr.AddrFormatError:
        # probably a hostname - so not an address at all!
        return False

    return address.version == 6
752070
def ip_in_ipmask(ip, ipmask):
    '''
    
    '''
    mask_ip, mask = str_to_ipmask(ipmask)
    return ip_in_ip_mask(ip, mask_ip, mask)
1024072
def check_ip(addr):
    '''
    
    '''

    try:
        addr = addr.rsplit('/', 1)
    except AttributeError:
        # Non-string passed
        return False

    if salt.utils.network.is_ipv4(addr[0]):
        try:
            if 1 <= int(addr[1]) <= 32:
                return True
        except ValueError:
            # Non-int subnet notation
            return False
        except IndexError:
            # No subnet notation used (i.e. just an IPv4 address)
            return True

    if salt.utils.network.is_ipv6(addr[0]):
        try:
            if 8 <= int(addr[1]) <= 128:
                return True
        except ValueError:
            # Non-int subnet notation
            return False
        except IndexError:
            # No subnet notation used (i.e. just an IPv4 address)
            return True

    return False
72787
def in6_cidr2mask(m):
    
    if m > 128 or m < 0:
        raise Kamene_Exception("value provided to in6_cidr2mask outside [0, 128] domain (%d)" % m)

    t = []
    for i in range(0, 4):
        t.append(max(0, 2**32  - 2**(32-min(32, m))))
        m -= 32

    return b"".join([ struct.pack('!I', i) for i in t ])
400414
private int computeNetworkPrefixFromMask(IPAddress mask) {
		
		int result = 0;
		int tmp = mask.getIPAddress();
		
		while ((tmp & 0x00000001) == 0x00000001) {
			result++;
			tmp = tmp >>> 1;
		}
		
		if (tmp != 0) {
			return -1;
		}
		
		return result;
	}
845007
def netmask(mask):
    '''
    
    '''
    if not isinstance(mask, string_types):
        return False

    octets = mask.split('.')
    if not len(octets) == 4:
        return False

    return ipv4_addr(mask) and octets == sorted(octets, reverse=True)
578486
public void expectIpv6(String name) {
        expectIpv6(name, messages.get(Validation.IPV6_KEY.name(), name));
    }
359377
def ipv6_prefix_to_mask(prefix):
    
    if prefix > 128 or prefix < 0:
        raise ValueError("invalid cidr prefix for ipv6")
    else:
        mask = ((1 << 128) - 1) ^ ((1 << (128 - prefix)) - 1)
        f = 15  # 0xf or 0b1111
        hex_mask_str = ''
        for i in range(0, 32):
            hex_mask_str = format((mask & f), 'x') + hex_mask_str
            mask = mask >> 4
            if i != 31 and i & 3 == 3:
                hex_mask_str = ':' + hex_mask_str
        return hex_mask_str
1235999
function isIPv4(val) {
  // It will accept anything with three dots (.) and numeric digits
  const ipv4Regex = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  if (!ipv4Regex.test(val)) {
    return false;
  }
  // This part of code do checking for any invalid IP address like 256.256.256.256
  var parts = val.split('.').sort(function (a, b) {
    return a - b;
  });
  return parts[3] <= 255;
}
469868
def is_reserved(self):
        
        reserved_nets = [IPv6Network('::/8'), IPv6Network('100::/8'),
                         IPv6Network('200::/7'), IPv6Network('400::/6'),
                         IPv6Network('800::/5'), IPv6Network('1000::/4'),
                         IPv6Network('4000::/3'), IPv6Network('6000::/3'),
                         IPv6Network('8000::/3'), IPv6Network('A000::/3'),
                         IPv6Network('C000::/3'), IPv6Network('E000::/4'),
                         IPv6Network('F000::/5'), IPv6Network('F800::/6'),
                         IPv6Network('FE00::/9')]

        return any(self in x for x in reserved_nets)
1207814
public static boolean isValidNetmask(String netmask)
	{
		try
		{
			return isValidNetmask(InetAddress.getByName(netmask));
		}
		catch (UnknownHostException e)
		{
			return false;
		}
	}
695910
def cidr_to_netmask(cidr_range)
      cidr = IP::CIDR.new(cidr_range)
      return cidr.ip.ip_address, cidr.long_netmask.ip_address
    end
786351
def cidr_netmask(cidr):
    '''
    
    '''
    ips = netaddr.IPNetwork(cidr)
    return six.text_type(ips.netmask)
437277
public static function check_ipv6($ip)
	{
		$ip = self::uncompress($ip);
		list($ipv6, $ipv4) = self::split_v6_v4($ip);
		$ipv6 = explode(':', $ipv6);
		$ipv4 = explode('.', $ipv4);
		if (count($ipv6) === 8 && count($ipv4) === 1 || count($ipv6) === 6 && count($ipv4) === 4)
		{
			foreach ($ipv6 as $ipv6_part)
			{
				// The section can't be empty
				if ($ipv6_part === '')
					return false;

				// Nor can it be over four characters
				if (strlen($ipv6_part) > 4)
					return false;

				// Remove leading zeros (this is safe because of the above)
				$ipv6_part = ltrim($ipv6_part, '0');
				if ($ipv6_part === '')
					$ipv6_part = '0';

				// Check the value is valid
				$value = hexdec($ipv6_part);
				if (dechex($value) !== strtolower($ipv6_part) || $value < 0 || $value > 0xFFFF)
					return false;
			}
			if (count($ipv4) === 4)
			{
				foreach ($ipv4 as $ipv4_part)
				{
					$value = (int) $ipv4_part;
					if ((string) $value !== $ipv4_part || $value < 0 || $value > 0xFF)
						return false;
				}
			}
			return true;
		}
		else
		{
			return false;
		}
	}