Пример #1
0
def in_network(scope, prefixes, destination, default_pfxlen = [24]):
    """
    Returns True if the given destination is in the network range that is
    defined by the given prefix (e.g. 10.0.0.1/22). If the given prefix
    does not have a prefix length specified, the given default prefix length
    is applied. If no such prefix length is given, the default length is
    /24.

    If a list of prefixes is passed, this function returns True only if
    the given destination is in ANY of the given prefixes.

    @type  prefixes: string
    @param prefixes: A prefix, or a list of IP prefixes.
    @type  destination: string
    @param destination: An IP address.
    @type  default_pfxlen: int
    @param default_pfxlen: The default prefix length.
    @rtype:  True
    @return: Whether the given destination is in the given network.
    """
    needle = ipv4.ip2int(destination[0])
    for prefix in prefixes:
        network, pfxlen = ipv4.parse_prefix(prefix, default_pfxlen[0])
        mask            = ipv4.pfxlen2mask_int(pfxlen)
        if needle & mask == ipv4.ip2int(network) & mask:
            return [True]
    return [False]
Пример #2
0
def in_network(scope, prefixes, destination, default_pfxlen=[24]):
    """
    Returns True if the given destination is in the network range that is
    defined by the given prefix (e.g. 10.0.0.1/22). If the given prefix
    does not have a prefix length specified, the given default prefix length
    is applied. If no such prefix length is given, the default length is
    /24.

    If a list of prefixes is passed, this function returns True only if
    the given destination is in ANY of the given prefixes.

    :type  prefixes: string
    :param prefixes: A prefix, or a list of IP prefixes.
    :type  destination: string
    :param destination: An IP address.
    :type  default_pfxlen: int
    :param default_pfxlen: The default prefix length.
    :rtype:  True
    :return: Whether the given destination is in the given network.
    """
    needle = ipv4.ip2int(destination[0])
    for prefix in prefixes:
        network, pfxlen = ipv4.parse_prefix(prefix, default_pfxlen[0])
        mask = ipv4.pfxlen2mask_int(pfxlen)
        if needle & mask == ipv4.ip2int(network) & mask:
            return [True]
    return [False]
Пример #3
0
 def testIp2Int(self):
     from Exscript.util.ipv4 import ip2int
     self.assertEqual(ip2int('0.0.0.0'), 0x00000000)
     self.assertEqual(ip2int('255.255.255.255'), 0xFFFFFFFF)
     self.assertEqual(ip2int('255.255.255.0'), 0xFFFFFF00)
     self.assertEqual(ip2int('0.255.255.0'), 0x00FFFF00)
     self.assertEqual(ip2int('0.128.255.0'), 0x0080FF00)
Пример #4
0
 def testIp2Int(self):
     from Exscript.util.ipv4 import ip2int
     self.assertEqual(ip2int('0.0.0.0'),         0x00000000l)
     self.assertEqual(ip2int('255.255.255.255'), 0xFFFFFFFFl)
     self.assertEqual(ip2int('255.255.255.0'),   0xFFFFFF00l)
     self.assertEqual(ip2int('0.255.255.0'),     0x00FFFF00l)
     self.assertEqual(ip2int('0.128.255.0'),     0x0080FF00l)
Пример #5
0
def mask(scope, ips, mask):
    """
    Applies the given IP mask (e.g. 255.255.255.0) to the given IP address
    (or list of IP addresses) and returns it.

    :type  ips: string
    :param ips: A prefix, or a list of IP prefixes.
    :type  mask: string
    :param mask: An IP mask.
    :rtype:  string
    :return: The network(s) that result(s) from applying the mask.
    """
    mask = ipv4.ip2int(mask[0])
    return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
Пример #6
0
def mask(scope, ips, mask):
    """
    Applies the given IP mask (e.g. 255.255.255.0) to the given IP address
    (or list of IP addresses) and returns it.

    @type  ips: string
    @param ips: A prefix, or a list of IP prefixes.
    @type  mask: string
    @param mask: An IP mask.
    @rtype:  string
    @return: The network(s) that result(s) from applying the mask.
    """
    mask = ipv4.ip2int(mask[0])
    return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
Пример #7
0
 def testInt2Ip(self):
     from Exscript.util.ipv4 import int2ip, ip2int
     for ip in ('0.0.0.0',
                '255.255.255.255',
                '255.255.255.0',
                '0.255.255.0',
                '0.128.255.0'):
         self.assertEqual(int2ip(ip2int(ip)), ip)
Пример #8
0
def pfxmask(scope, ips, pfxlen):
    """
    Applies the given prefix length to the given ips, resulting in a
    (list of) IP network addresses.

    :type  ips: string
    :param ips: An IP address, or a list of IP addresses.
    :type  pfxlen: int
    :param pfxlen: An IP prefix length.
    :rtype:  string
    :return: The mask(s) that result(s) from converting the prefix length.
    """
    mask = ipv4.pfxlen2mask_int(pfxlen[0])
    return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
Пример #9
0
def pfxmask(scope, ips, pfxlen):
    """
    Applies the given prefix length to the given ips, resulting in a
    (list of) IP network addresses.

    @type  ips: string
    @param ips: An IP address, or a list of IP addresses.
    @type  pfxlen: int
    @param pfxlen: An IP prefix length.
    @rtype:  string
    @return: The mask(s) that result(s) from converting the prefix length.
    """
    mask = ipv4.pfxlen2mask_int(pfxlen[0])
    return [ipv4.int2ip(ipv4.ip2int(ip) & mask) for ip in ips]
Пример #10
0
 def testInt2Ip(self):
     from Exscript.util.ipv4 import int2ip, ip2int
     for ip in ('0.0.0.0', '255.255.255.255', '255.255.255.0',
                '0.255.255.0', '0.128.255.0'):
         self.assertEqual(int2ip(ip2int(ip)), ip)