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]
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]
def testPfxlen2MaskInt(self): from Exscript.util.ipv4 import pfxlen2mask_int, int2ip self.assertEqual(int2ip(pfxlen2mask_int(32)), '255.255.255.255') self.assertEqual(int2ip(pfxlen2mask_int(31)), '255.255.255.254') self.assertEqual(int2ip(pfxlen2mask_int(30)), '255.255.255.252') self.assertEqual(int2ip(pfxlen2mask_int(2)), '192.0.0.0') self.assertEqual(int2ip(pfxlen2mask_int(1)), '128.0.0.0') self.assertEqual(int2ip(pfxlen2mask_int(0)), '0.0.0.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]
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]