def _genRegionIps( self, connections, bracket_ipv6=False, include_name=False ): """Generate IP addresses for all region controllers this rack controller is connected to.""" def _getNameFromEventloop(eventloop): if ":" in eventloop: return eventloop.split(":", 1)[0] else: return eventloop # Filter the connects by region. conn_per_region = defaultdict(set) for eventloop, connection in connections.items(): conn_per_region[eventloop.split(":")[0]].add(connection) for eventloop, connections in conn_per_region.items(): # Sort the connections so the same IP is always picked per # region controller. This ensures that the HTTP configuration # is not reloaded unless its actually required to reload. conn = list(sorted(connections, key=lambda conn: conn.address[0]))[ 0 ] addr = IPAddress(conn.address[0]) if addr.is_ipv4_mapped(): addr = str(addr.ipv4()) elif addr.version == 6 and bracket_ipv6: addr = "[%s]" % addr else: addr = str(addr) if include_name: yield (_getNameFromEventloop(eventloop), addr) else: yield addr
def get_version(request): func = request.GET.get('func', '') remote_addr = IPAddress(request.remote_addr) data = { 'address': remote_addr.format(), 'version': remote_addr.version, 'ipv4_mapped': remote_addr.is_ipv4_mapped(), } return Response( body='%s(%s);' % (func, json.dumps(data)), content_type='text/javascript')
def _normalize_ip_address(addr): """ When we used mapped ipv4 (starting with ::FFFF/96) we need to normalize it to ipv4 in order to compare it with value used in commonName in the certificate. """ ip = IPAddress(addr) if ip.is_ipv4_mapped(): addr = str(ip.ipv4()) return addr
def normaliseAddress(address): """Normalise an IP address.""" try: address = IPAddress(address) except AddrFormatError: return address # Hostname? else: if address.is_ipv4_mapped(): return address.ipv4() else: return address
def normalize_mapped_address(ipaddr): """ Converts a IPv4-mapped IPv6 address into a IPv4 address. Handles both the ::ffff:192.0.2.128 format as well as the deprecated ::192.0.2.128 format. :param ipaddr: IP address [str] :return: normalized IP address [str] """ ipaddr = IPAddress(ipaddr) if ipaddr.is_ipv4_compat() or ipaddr.is_ipv4_mapped(): ipaddr = ipaddr.ipv4() return str(ipaddr)
def get_best_subnet_for_ip(self, ip): """Find the most-specific managed Subnet the specified IP address belongs to.""" ip = IPAddress(ip) if ip.is_ipv4_mapped(): ip = ip.ipv4() subnets = self.raw(self.find_best_subnet_for_ip_query, params=[str(ip)]) for subnet in subnets: return subnet # This is stable because the query is ordered. else: return None
def normalise_address(address): """Normalise an IP address into a form suitable for the `ntp` daemon. It seems to prefer non-mapped IPv4 addresses, for example. Hostnames are passed through. """ try: address = IPAddress(address) except AddrFormatError: return address # Hostname. else: if address.is_ipv4_mapped(): return address.ipv4() else: return address
def is_loopback_address(hostname): """Determine if the given hostname appears to be a loopback address. :param hostname: either a hostname or an IP address. No resolution is done, but 'localhost' is considered to be loopback. :type hostname: str :return: True if the address is a loopback address. """ try: ip = IPAddress(hostname) except AddrFormatError: return hostname.lower() in {"localhost", "localhost."} return ip.is_loopback() or (ip.is_ipv4_mapped() and ip.ipv4().is_loopback())
def _genRegionIps(self): """Generate IP addresses for all region controllers this rack controller is connected to.""" # Filter the connects by region. conn_per_region = defaultdict(set) for eventloop, connection in self._rpc_service.connections.items(): conn_per_region[eventloop.split(':')[0]].add(connection) for _, connections in conn_per_region.items(): # Sort the connections so the same IP is always picked per # region controller. This ensures that the HTTP configuration # is not reloaded unless its actually required to reload. conn = list(sorted( connections, key=lambda conn: conn.address[0]))[0] addr = IPAddress(conn.address[0]) if addr.is_ipv4_mapped(): yield str(addr.ipv4()) else: yield str(addr)
def test_ip_v4_to_ipv6_mapped(): ip = IPAddress('192.0.2.15').ipv6() assert ip == IPAddress('::ffff:192.0.2.15') assert ip.is_ipv4_mapped() assert not ip.is_ipv4_compat()