Example #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]
Example #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]
Example #3
0
 def testParsePrefix(self):
     from Exscript.util.ipv4 import parse_prefix
     self.assertEqual(('1.2.3.4', 24), parse_prefix('1.2.3.4'))
     self.assertEqual(('1.2.3.4', 32), parse_prefix('1.2.3.4', 32))
     self.assertEqual(('1.2.3.4', 15), parse_prefix('1.2.3.4/15'))
     self.assertEqual(('1.2.3.4', 15), parse_prefix('1.2.3.4/15', 32))
Example #4
0
 def testParsePrefix(self):
     from Exscript.util.ipv4 import parse_prefix
     self.assertEqual(('1.2.3.4', 24), parse_prefix('1.2.3.4'))
     self.assertEqual(('1.2.3.4', 32), parse_prefix('1.2.3.4',    32))
     self.assertEqual(('1.2.3.4', 15), parse_prefix('1.2.3.4/15'))
     self.assertEqual(('1.2.3.4', 15), parse_prefix('1.2.3.4/15', 32))