예제 #1
0
파일: mask.py 프로젝트: bobveznat/aplib
    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
예제 #2
0
파일: mask.py 프로젝트: bobveznat/aplib
    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
예제 #3
0
파일: ip.py 프로젝트: bobveznat/aplib
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
예제 #4
0
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
예제 #5
0
파일: ip.py 프로젝트: bobveznat/aplib
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
예제 #6
0
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
예제 #7
0
파일: ip.py 프로젝트: bobveznat/aplib
 def parse_ip(address):
     ip = _net.parse_ipv4(address)
     if ip is None:
         raise IPValidationError(address)
     return ip
예제 #8
0
 def parse_ip(address):
     ip = _net.parse_ipv4(address)
     if ip is None:
         raise IPValidationError(address)
     return ip