def to_address(name): """Convert a reverse map domain name into textual address form. @param name: an IPv4 or IPv6 address in reverse-map form. @type name: name.Name object @rtype: str """ if name.is_subdomain(ipv4_reverse_domain): name = name.relativize(ipv4_reverse_domain) labels = list(name.labels) labels.reverse() text = '.'.join(labels) # run through inet_aton() to check syntax and make pretty. return ipv4.inet_ntoa(ipv4.inet_aton(text)) elif name.is_subdomain(ipv6_reverse_domain): name = name.relativize(ipv6_reverse_domain) labels = list(name.labels) labels.reverse() parts = [] i = 0 l = len(labels) while i < l: parts.append(''.join(labels[i:i+4])) i += 4 text = ':'.join(parts) # run through inet_aton() to check syntax and make pretty. return ipv6.inet_ntoa(ipv6.inet_aton(text)) else: raise exception.SyntaxError('unknown reverse-map address family')
def is_multicast(text): """Is the textual-form network address a multicast address? @param text: the textual address @raises ValueError: the address family cannot be determined from the input. @rtype: bool """ try: first = ord(ipv4.inet_aton(text)[0]) return (first >= 224 and first <= 239) except: try: first = ord(ipv6.inet_aton(text)[0]) return (first == 255) except: raise ValueError
def af_for_address(text): """Determine the address family of a textual-form network address. @param text: the textual address @type text: string @raises ValueError: the address family cannot be determined from the input. @rtype: int """ try: junk = ipv4.inet_aton(text) return AF_INET except: try: junk = ipv6.inet_aton(text) return AF_INET6 except: raise ValueError
def inet_pton(family, text): """Convert the textual form of a network address into its binary form. @param family: the address family @type family: int @param text: the textual address @type text: string @raises NotImplementedError: the address family specified is not implemented. @rtype: string """ if family == AF_INET: return ipv4.inet_aton(text) elif family == AF_INET6: return ipv6.inet_aton(text) else: raise NotImplementedError
def _gethostbyaddr(ip): try: addr = ipv6.inet_aton(ip) sockaddr = (ip, 80, 0, 0) family = socket.AF_INET6 except: sockaddr = (ip, 80) family = socket.AF_INET (name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD) aliases = [] addresses = [] tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP, socket.AI_CANONNAME) canonical = tuples[0][3] for item in tuples: addresses.append(item[4][0]) # XXX we just ignore aliases return (canonical, aliases, addresses)
def from_address(text): """Convert an IPv4 or IPv6 address in textual form into a Name object whose value is the reverse-map domain name of the address. @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1', '::1') @type text: str @rtype: name.Name object """ try: v6 = ipv6.inet_aton(text) if ipv6.is_mapped(v6): parts = ['%d' % ord(byte) for byte in v6[12:]] origin = ipv4_reverse_domain else: parts = list(v6.encode('hex_codec')) origin = ipv6_reverse_domain except: parts = ['%d' % ord(byte) for byte in ipv4.inet_aton(text)] origin = ipv4_reverse_domain parts.reverse() return name.from_text('.'.join(parts), origin=origin)
def _getaddrinfo(host=None, service=None, family=socket.AF_UNSPEC, socktype=0, proto=0, flags=0): if flags & (socket.AI_ADDRCONFIG|socket.AI_V4MAPPED) != 0: raise NotImplementedError if host is None and service is None: raise socket.gaierror(socket.EAI_NONAME) v6addrs = [] v4addrs = [] canonical_name = None try: # Is host None or a V6 address literal? if host is None: canonical_name = 'localhost' if flags & socket.AI_PASSIVE != 0: v6addrs.append('::') v4addrs.append('0.0.0.0') else: v6addrs.append('::1') v4addrs.append('127.0.0.1') else: parts = host.split('%') if len(parts) == 2: ahost = parts[0] else: ahost = host addr = ipv6.inet_aton(ahost) v6addrs.append(host) canonical_name = host except: try: # Is it a V4 address literal? addr = ipv4.inet_aton(host) v4addrs.append(host) canonical_name = host except: if flags & socket.AI_NUMERICHOST == 0: try: qname = None if family == socket.AF_INET6 or family == socket.AF_UNSPEC: v6 = _resolver.query(host, rdatatype.AAAA, raise_on_no_answer=False) # Note that setting host ensures we query the same name # for A as we did for AAAA. host = v6.qname canonical_name = v6.canonical_name.to_text(True) if v6.rrset is not None: for rdata in v6.rrset: v6addrs.append(rdata.address) if family == socket.AF_INET or family == socket.AF_UNSPEC: v4 = _resolver.query(host, rdatatype.A, raise_on_no_answer=False) host = v4.qname canonical_name = v4.canonical_name.to_text(True) if v4.rrset is not None: for rdata in v4.rrset: v4addrs.append(rdata.address) except resolver.NXDOMAIN: raise socket.gaierror(socket.EAI_NONAME) except: raise socket.gaierror(socket.EAI_SYSTEM) port = None try: # Is it a port literal? if service is None: port = 0 else: port = int(service) except: if flags & socket.AI_NUMERICSERV == 0: try: port = socket.getservbyname(service) except: pass if port is None: raise socket.gaierror(socket.EAI_NONAME) tuples = [] if socktype == 0: socktypes = [socket.SOCK_DGRAM, socket.SOCK_STREAM] else: socktypes = [socktype] if flags & socket.AI_CANONNAME != 0: cname = canonical_name else: cname = '' if family == socket.AF_INET6 or family == socket.AF_UNSPEC: for addr in v6addrs: for socktype in socktypes: for proto in _protocols_for_socktype[socktype]: tuples.append((socket.AF_INET6, socktype, proto, cname, (addr, port, 0, 0))) if family == socket.AF_INET or family == socket.AF_UNSPEC: for addr in v4addrs: for socktype in socktypes: for proto in _protocols_for_socktype[socktype]: tuples.append((socket.AF_INET, socktype, proto, cname, (addr, port))) if len(tuples) == 0: raise socket.gaierror(socket.EAI_NONAME) return tuples