starcluster.iptools

Package starcluster :: Module iptools |
[hide private]
[frames] | no frames]

# Module iptools

source code

    Utitlities for dealing with ip addresses.

    Functions:
      - validate_ip: Validate a dotted-quad ip address.
      - ip2long: Convert a dotted-quad ip address to a network byte order 32-bit
        integer.
      - long2ip: Convert a network byte order 32-bit integer to a dotted quad ip
        address.
      - ip2hex: Convert a dotted-quad ip address to a hex encoded network byte
        order 32-bit integer.
      - hex2ip: Convert a hex encoded network byte order 32-bit integer to a
        dotted-quad ip address.
      - validate_cidr: Validate a CIDR notation ip address.
      - cidr2block: Convert a CIDR notation ip address into a tuple containing
        network block start and end addresses.

    Objects:
      - IpRange: Range of ip addresses providing ``in`` and iteration.
      - IpRangeList: List of IpRange objects providing ``in`` and iteration.

    The IpRangeList object can be used in a django settings file to allow CIDR
    notation and/or (start, end) ranges to be used in the INTERNAL_IPS list.

    Example:
      INTERNAL_IPS = IpRangeList(
          '127.0.0.1',
          '192.168/16',
          ('10.0.0.1', '10.0.0.19'),
          )

Version: 0.5.0-dev

Classes | [hide private]
| IpRange Range of ip addresses.
| IpRangeList List of IpRange objects.

Functions | [hide private]
validate_ip(s) Validate a dotted-quad ip address. | source code
validate_cidr(s) Validate a CIDR notation ip address. | source code
ip2long(ip) Convert a dotted-quad ip address to a network byte order 32-bit integer. | source code
long2ip(l) Convert a network byte order 32-bit integer to a dotted quad ip address. | source code
ip2hex(addr) Convert a dotted-quad ip address to a hex encoded number. | source code
hex2ip(hex_str) Convert a hex encoded integer to a dotted-quad ip address. | source code
cidr2block(cidr) Convert a CIDR notation ip address into a tuple containing the network block start and end addresses. | source code
iptools_test() | source code

Variables | [hide private]
| _DOTTED_QUAD_RE = `re.compile(r'^`(`\d`{1,3}`\.`)``{,3}`\d`{1,3}`$')`
| _CIDR_RE = `re.compile(r'^`(`\d`{1,3}`\.`)``{,3}`\d`{1,3}`/\d`{1,2}`$')`
| _MAX_IP = `4294967295`
| _MIN_IP = `0`
| __package__ = ``'``starcluster``'``

Function Details | [hide private]

### validate_ip(s) | source code
    Validate a dotted-quad ip address.

    The string is considered a valid dotted-quad address if it consists of
    one to four octets (0-255) seperated by periods (.).

    >>> validate_ip('127.0.0.1')
    True

    >>> validate_ip('127.0')
    True

    >>> validate_ip('127.0.0.256')
    False

    >>> validate_ip(None)
    Traceback (most recent call last):
        ...
    TypeError: expected string or buffer

    Args:
        s: String to validate as a dotted-quad ip address
    Returns:
        True if str is a valid dotted-quad ip address, False otherwise

### validate_cidr(s) | source code
    Validate a CIDR notation ip address.

    The string is considered a valid CIDR address if it consists of one to
    four octets (0-255) seperated by periods (.) followed by a forward slash
    (/) and a bit mask length (1-32).

    >>> validate_cidr('127.0.0.1/32')
    True

    >>> validate_cidr('127.0/8')
    True

    >>> validate_cidr('127.0.0.256/32')
    False

    >>> validate_cidr('127.0.0.0')
    False

    >>> validate_cidr(None)
    Traceback (most recent call last):
        ...
    TypeError: expected string or buffer

    Args:
        str: String to validate as a CIDR ip address
    Returns:
        True if str is a valid CIDR address, False otherwise

### ip2long(ip) | source code
    Convert a dotted-quad ip address to a network byte order 32-bit integer.

    >>> ip2long('127.0.0.1')
    2130706433

    >>> ip2long('127.1')
    2130706433

    >>> ip2long('127')
    2130706432

    >>> ip2long('127.0.0.256') is None
    True

    Args:
        ip: Dotted-quad ip address (eg. '127.0.0.1')

    Returns:
        Network byte order 32-bit integer or None if ip is invalid

### long2ip(l) | source code
    Convert a network byte order 32-bit integer to a dotted quad ip address.

    >>> long2ip(2130706433)
    '127.0.0.1'

    >>> long2ip(_MIN_IP)
    '0.0.0.0'

    >>> long2ip(_MAX_IP)
    '255.255.255.255'

    >>> long2ip(None) #doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
        ...
    TypeError: unsupported operand type(s) for >>: 'NoneType' and 'int'

    >>> long2ip(-1) #doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
        ...
    TypeError: expected int between 0 and 4294967295 inclusive

    >>> long2ip(374297346592387463875L) #doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
        ...
    TypeError: expected int between 0 and 4294967295 inclusive

    >>> long2ip(_MAX_IP + 1) #doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
        ...
    TypeError: expected int between 0 and 4294967295 inclusive

    Args:
        l: Network byte order 32-bit integer
    Returns:
        Dotted-quad ip address (eg. '127.0.0.1')

### ip2hex(addr) | source code
Convert a dotted-quad ip address to a hex encoded number.
    >>> ip2hex('0.0.0.1')
    '00000001'
    >>> ip2hex('127.0.0.1')
    '7f000001'
    >>> ip2hex('127.255.255.255')
    '7fffffff'
    >>> ip2hex('128.0.0.1')
    '80000001'
    >>> ip2hex('128.1')
    '80000001'
    >>> ip2hex('255.255.255.255')
    'ffffffff'

### hex2ip(hex_str) | source code
Convert a hex encoded integer to a dotted-quad ip address.
    >>> hex2ip('00000001')
    '0.0.0.1'
    >>> hex2ip('7f000001')
    '127.0.0.1'
    >>> hex2ip('7fffffff')
    '127.255.255.255'
    >>> hex2ip('80000001')
    '128.0.0.1'
    >>> hex2ip('ffffffff')
    '255.255.255.255'

### cidr2block(cidr) | source code
    Convert a CIDR notation ip address into a tuple containing the network
    block start and end addresses.

    >>> cidr2block('127.0.0.1/32')
    ('127.0.0.1', '127.0.0.1')

    >>> cidr2block('127/8')
    ('127.0.0.0', '127.255.255.255')

    >>> cidr2block('127.0.1/16')
    ('127.0.0.0', '127.0.255.255')

    >>> cidr2block('127.1/24')
    ('127.1.0.0', '127.1.0.255')

    >>> cidr2block('127.0.0.3/29')
    ('127.0.0.0', '127.0.0.7')

    >>> cidr2block('127/0')
    ('0.0.0.0', '255.255.255.255')

    Args:
        cidr: CIDR notation ip address (eg. '127.0.0.1/8')
    Returns:
        Tuple of block (start, end) or None if invalid