def parse_mask(cls, mask): if mask.startswith('0x'): if len(mask) != 10: raise MaskValidationError(mask) else: try: mask_int = int(mask, 16) format = MASK_FORMAT.HEX except ValueError: raise MaskValidationError(mask) elif mask.startswith('/'): format = MASK_FORMAT.PREFIX prefixlen, mask_int = cls._parse_prefixlen(mask[1:]) else: try: prefixlen, mask_int = cls._parse_prefixlen(mask) format = MASK_FORMAT.PREFIX except MaskValidationError: try: mask_int = _net.parse_ipv4(mask) format = MASK_FORMAT.DOTTED_QUAD except ValueError: raise MaskValidationError(mask) # We don't allow non-contiguous netmasks. try: prefixlen = Mask4._bit_mask_map[mask_int] except KeyError: raise MaskValidationError(mask) return prefixlen, mask_int, format
def is_ipv4(address): """Determine if an address is an IPv4 address. :Parameters: - `address`: The IP address string to check. :Return: Returns True if it is a v4 address, otherwise False. """ return _net.parse_ipv4(address) is not None
def ptoh(address): """Converts from presentation format or ip address string to packed address in host-byte order. :Parameters: - `address`: The IP address string to convert. :Return: Returns the packed ip address. :Exceptions: - `IPValidationError`: if address is not valid v4 or v6 address. """ result = _net.parse_ipv4(address) if result is None: return IPv6.parse_ip(address) return result
def parse_ip(address): ip = _net.parse_ipv4(address) if ip is None: raise IPValidationError(address) return ip