def _check_ldap_service(self, sp): for server_address in sp.server_names.splitlines(): try: getaddrinfo(server_address.strip(), None) except gaierror: self.stdout.write( "Address does not resolve | LDAP entity: %s | address: %s" % (sp.entity_id, server_address))
def test_hostname_unicode(self): import _socket domain = u"испытание.pythontest.net" _socket.gethostbyname(domain) _socket.gethostbyname_ex(domain) _socket.getaddrinfo(domain, 0, _socket.AF_UNSPEC, _socket.SOCK_STREAM) s = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM) s.connect((domain, 80)) s.close() raises(TypeError, s.connect, (domain + '\x00', 80))
def _check_oidc_service(self, sp): redirecturis = [] for redirecuri in RedirectUri.objects.filter( sp=sp, end_at=None).values_list('uri', flat=True): redirecturis.append(urlparse(redirecuri).netloc.split(":")[0]) for redirecuri in set(redirecturis): try: getaddrinfo(redirecuri, None) except gaierror: self.stdout.write( "Address does not resolve | OIDC entity: %s | address: %s" % (sp.entity_id, redirecuri))
def _check_saml_service(self, sp): endpoints = [] for endpoint in Endpoint.objects.filter( sp=sp, end_at=None).values_list('location', flat=True): endpoints.append(urlparse(endpoint).netloc.split(":")[0]) for endpoint in set(endpoints): try: getaddrinfo(endpoint, None) except gaierror: self.stdout.write( "Address does not resolve | SAML entity: %s | address: %s" % (sp.entity_id, endpoint))
def _resolve_special(hostname, family): if hostname == '': result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE) if len(result) != 1: raise error('wildcard resolved to multiple address') return result[0][4][0] return hostname
def getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): if ((host in (u'localhost', b'localhost') or (is_ipv6_addr(host) and host.startswith('fe80'))) or not isinstance(host, str) or (flags & AI_NUMERICHOST)): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) host is localhost or a link-local ipv6; dnspython returns the wrong # scope-id for those. # 3) AI_NUMERICHOST flag is set return _socket.getaddrinfo(host, port, family, socktype, proto, flags) if family == AF_UNSPEC: # This tends to raise in the case that a v6 address did not exist # but a v4 does. So we break it into two parts. # Note that if there is no ipv6 in the hosts file, but there *is* # an ipv4, and there *is* an ipv6 in the nameservers, we will return # both (from the first call). The system resolver on OS X only returns # the results from the hosts file. doubleclick.com is one example. # See also https://github.com/gevent/gevent/issues/1012 try: return _getaddrinfo(host, port, family, socktype, proto, flags) except socket.gaierror: try: return _getaddrinfo(host, port, AF_INET6, socktype, proto, flags) except socket.gaierror: return _getaddrinfo(host, port, AF_INET, socktype, proto, flags) else: return _getaddrinfo(host, port, family, socktype, proto, flags)
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): ## wrapper """Resolve host and port into list of address info entries. Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as 'http', a numeric port number or None. By passing None as the value of host and port, you can pass NULL to the underlying C API. The family, type and proto arguments can be optionally specified in order to narrow the list of addresses returned. Passing zero as a value for each of these arguments selects the full range of results. """ # We override this function since we want to translate the numeric family # and socket type values to enum constants. addrlist = [] for res in _socket.getaddrinfo(host, port, family, type, proto, flags): ## underlying C API af, socktype, proto, canonname, sa = res addrlist.append((_intenum_converter(af, AddressFamily), _intenum_converter(socktype, SocketKind), proto, canonname, sa)) ## canonname, the canonical name CNAME, DNS for domain name ## sa, source address (host, port) return addrlist
def getaddrinfo(host, port, *args, **kwargs): """*Some* approximation of :func:`socket.getaddrinfo` implemented using :mod:`gevent.dns`. If *host* is not a string, does not has any dots or is a numeric IP address, then the standard :func:`socket.getaddrinfo` is called. Otherwise, calls either :func:`resolve_ipv4` or :func:`resolve_ipv6` and formats the result the way :func:`socket.getaddrinfo` does it. Differs in the following ways: * raises :class:`DNSError` (a subclass of :class:`gaierror`) with libevent-dns error codes instead of standard socket error codes * IPv6 support is untested. * AF_UNSPEC only tries IPv4 * only supports TCP, UDP, IP protocols * port must be numeric, does not support string service names. see socket.getservbyname * *flags* argument is ignored Additionally, supports *evdns_flags* keyword arguments (default ``0``) that is passed to :mod:`dns` functions. """ family, socktype, proto, _flags = args + (None, ) * (4 - len(args)) if not isinstance(host, str) or '.' not in host or _ip4_re.match(host): return _socket.getaddrinfo(host, port, *args) evdns_flags = kwargs.pop('evdns_flags', 0) if kwargs: raise TypeError('Unsupported keyword arguments: %s' % (kwargs.keys(), )) if family in (None, AF_INET, AF_UNSPEC): family = AF_INET # TODO: AF_UNSPEC means try both AF_INET and AF_INET6 _ttl, addrs = resolve_ipv4(host, evdns_flags) elif family == AF_INET6: _ttl, addrs = resolve_ipv6(host, evdns_flags) else: raise NotImplementedError( 'family is not among AF_UNSPEC/AF_INET/AF_INET6: %r' % (family, )) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktype is not None: socktype_proto = [(x, y) for (x, y) in socktype_proto if socktype == x] if proto is not None: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] result = [] for addr in addrs: for socktype, proto in socktype_proto: result.append( (family, socktype, proto, '', (inet_ntop(family, addr), port))) return result
def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0, evdns_flags=0): """*Some* approximation of :func:`socket.getaddrinfo` implemented using :mod:`gevent.dns`. If *host* is not a string, does not has any dots or is a numeric IP address, then the standard :func:`socket.getaddrinfo` is called. Otherwise, calls :func:`resolve_ipv4` (for ``AF_INET``) or :func:`resolve_ipv6` (for ``AF_INET6``) or both (for ``AF_UNSPEC``) and formats the result the way :func:`socket.getaddrinfo` does it. Differs in the following ways: * raises :class:`DNSError` (a subclass of :class:`gaierror`) with libevent-dns error codes instead of standard socket error codes * *flags* argument is ignored * for IPv6, flow info and scope id are always 0 Additionally, supports *evdns_flags* keyword arguments (default ``0``) that is passed to :mod:`dns` functions. """ if isinstance(host, unicode): host = host.encode('idna') if not isinstance(host, str) or \ '.' not in host or \ _ip4_re.match(host) is not None or \ family not in (AF_UNSPEC, AF_INET, AF_INET6): return _socket.getaddrinfo(host, port, family, socktype, proto, flags) if isinstance(port, basestring): try: if socktype == 0: try: port = getservbyname(port, 'tcp') socktype = SOCK_STREAM except socket.error: port = getservbyname(port, 'udp') socktype = SOCK_DGRAM elif socktype == SOCK_STREAM: port = getservbyname(port, 'tcp') elif socktype == SOCK_DGRAM: port = getservbyname(port, 'udp') else: raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype') except error, ex: if 'not found' in str(ex): raise gaierror(EAI_SERVICE, 'Servname not supported for ai_socktype') else: raise gaierror(str(ex))
def resolve(hostname, port=False): """ Resolves hostname to IP address. IPv6 addresses get mapped to IPv6 if a port isn't supplied returns str(IP_address) """ if port: return _socket.getaddrinfo(hostname, port)[0][4][0] else: return _socket.gethostbyname(hostname)
def _resolve_special(hostname, family): if not isinstance(hostname, hostname_types): raise TypeError("argument 1 must be str, bytes or bytearray, not %s" % (type(hostname),)) if hostname == '': result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE) if len(result) != 1: raise error('wildcard resolved to multiple address') return result[0][4][0] return hostname
def _resolve_special(hostname, family): if not isinstance(hostname, hostname_types): raise TypeError("argument 1 must be str, bytes or bytearray, not %s" % (type(hostname), )) if hostname == '': result = getaddrinfo(None, 0, family, SOCK_DGRAM, 0, AI_PASSIVE) if len(result) != 1: raise error('wildcard resolved to multiple address') return result[0][4][0] return hostname
def getaddrinfo(host, port, *args, **kwargs): """*Some* approximation of :func:`socket.getaddrinfo` implemented using :mod:`gevent.dns`. If *host* is not a string, does not has any dots or is a numeric IP address, then the standard :func:`socket.getaddrinfo` is called. Otherwise, calls either :func:`resolve_ipv4` or :func:`resolve_ipv6` and formats the result the way :func:`socket.getaddrinfo` does it. Differs in the following ways: * raises :class:`DNSError` (a subclass of :class:`gaierror`) with libevent-dns error codes instead of standard socket error codes * IPv6 support is untested. * AF_UNSPEC only tries IPv4 * only supports TCP, UDP, IP protocols * port must be numeric, does not support string service names. see socket.getservbyname * *flags* argument is ignored Additionally, supports *evdns_flags* keyword arguments (default ``0``) that is passed to :mod:`dns` functions. """ family, socktype, proto, _flags = args + (None, ) * (4 - len(args)) if isinstance(host, unicode): host = host.encode('idna') if not isinstance(host, str) or '.' not in host or _ip4_re.match(host): return _socket.getaddrinfo(host, port, *args) evdns_flags = kwargs.pop('evdns_flags', 0) if kwargs: raise TypeError('Unsupported keyword arguments: %s' % (kwargs.keys(), )) if family in (None, AF_INET, AF_UNSPEC): family = AF_INET # TODO: AF_UNSPEC means try both AF_INET and AF_INET6 _ttl, addrs = resolve_ipv4(host, evdns_flags) elif family == AF_INET6: _ttl, addrs = resolve_ipv6(host, evdns_flags) else: raise NotImplementedError('family is not among AF_UNSPEC/AF_INET/AF_INET6: %r' % (family, )) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktype: socktype_proto = [(x, y) for (x, y) in socktype_proto if socktype == x] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] result = [] for addr in addrs: for socktype, proto in socktype_proto: result.append((family, socktype, proto, '', (inet_ntop(family, addr), port))) return result
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0): """Resolve host and port into list of address info entries. Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as 'http', a numeric port number or None. By passing None as the value of host and port, you can pass NULL to the underlying C API. The family, type and proto arguments can be optionally specified in order to narrow the list of addresses returned. Passing zero as a value for each of these arguments selects the full range of results. """ # We override this function since we want to translate the numeric family # and socket type values to enum constants. addrlist = [] for res in _socket.getaddrinfo(host, port, family, type, proto, flags): af, socktype, proto, canonname, sa = res addrlist.append((_intenum_converter(af, AddressFamily), _intenum_converter(socktype, SocketKind), proto, canonname, sa)) return addrlist
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): # pylint:disable=too-many-locals,too-many-branches if isinstance(host, text_type): host = host.encode('idna') elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: # Because there is no plug into ares.gethostbyname_file ares lib function, # we must call gethostbyname with IPv4 first and then IPv6. If the # two are called simultaneously as before, and if a DNS server is down, and # even if the IPv4 address is in hosts file, it will wait for IPv6 to hit # missing/invalid DNS server which will cause a huge wait. # This change has the benefit of returning immediately if IPv4 address is in # hosts file but drawback of waiting for IPv4 to miss hosts file lookup AND # missing DNS hit before looking at IPv6 if IPv4 address is not defined in hosts # file. This all can be better solved by looking in the hosts file for IPv4 and # and v6 first before attempting to look into DNS servers. values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET) if not len(values.get(throw_on_error=False)): values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET6) elif family == AF_INET: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET) elif family == AF_INET6: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET6) else: raise gaierror(5, 'ai_family not supported: %r' % (family, )) values = ares_values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append((AF_INET, socktype, proto, '', sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == '::1': dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, '', sockaddr)) # As of 2016, some platforms return IPV6 first and some do IPV4 first, # and some might even allow configuration of which is which. For backwards # compatibility with earlier releases (but not necessarily resolver_thread!) # we return 4 first. See https://github.com/gevent/gevent/issues/815 for more. result += result4 + result6 if not result: raise gaierror(-5, 'No address associated with hostname') return result
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): # pylint:disable=too-many-locals,too-many-branches if isinstance(host, text_type): host = host.encode('idna') elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: ares_values = Values(self.hub, 2) ares.gethostbyname(ares_values, host, AF_INET) ares.gethostbyname(ares_values, host, AF_INET6) elif family == AF_INET: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET) elif family == AF_INET6: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET6) else: raise gaierror(5, 'ai_family not supported: %r' % (family, )) values = ares_values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append( (AF_INET, socktype, proto, '', sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == '::1': dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, '', sockaddr)) result += result4 + result6 if not result: raise gaierror(-5, 'No address associated with hostname') return result
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): # pylint:disable=too-many-locals,too-many-branches if isinstance(host, text_type): host = host.encode("idna") elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: ares_values = Values(self.hub, 2) ares.gethostbyname(ares_values, host, AF_INET) ares.gethostbyname(ares_values, host, AF_INET6) elif family == AF_INET: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET) elif family == AF_INET6: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET6) else: raise gaierror(5, "ai_family not supported: %r" % (family,)) values = ares_values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append((AF_INET, socktype, proto, "", sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == "::1": dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, "", sockaddr)) # As of 2016, some platforms return IPV6 first and some do IPV4 first, # and some might even allow configuration of which is which. For backwards # compatibility with earlier releases (but not necessarily resolver_thread!) # we return 4 first. See https://github.com/gevent/gevent/issues/815 for more. result += result4 + result6 if not result: raise gaierror(-5, "No address associated with hostname") return result
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): # pylint:disable=too-many-locals,too-many-branches if isinstance(host, text_type): host = host.encode('idna') elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: ares_values = Values(self.hub, 2) ares.gethostbyname(ares_values, host, AF_INET) ares.gethostbyname(ares_values, host, AF_INET6) elif family == AF_INET: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET) elif family == AF_INET6: ares_values = Values(self.hub, 1) ares.gethostbyname(ares_values, host, AF_INET6) else: raise gaierror(5, 'ai_family not supported: %r' % (family, )) values = ares_values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype4, proto4 in socktype_proto: result4.append( (AF_INET, socktype4, proto4, '', sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == '::1': dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype6, proto6 in socktype_proto: dest.append( (AF_INET6, socktype6, proto6, '', sockaddr)) # As of 2016, some platforms return IPV6 first and some do IPV4 first, # and some might even allow configuration of which is which. For backwards # compatibility with earlier releases (but not necessarily resolver_thread!) # we return 4 first. See https://github.com/gevent/gevent/issues/815 for more. result += result4 + result6 if not result: raise gaierror(-5, 'No address associated with hostname') return result
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): if isinstance(host, unicode): host = host.encode('idna') elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set if self.pool is not None: return self.pool.apply_e(BaseException, getaddrinfo, (host, port, family, socktype, proto, flags)) else: return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* if self.pool is not None and '.' not in host: # localhost, <broadcast> return self.pool.apply_e(BaseException, getaddrinfo, (host, port, family, socktype, proto, flags)) origport = port port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: values = Values(self.hub, 2) ares.gethostbyname(values, host, AF_INET) ares.gethostbyname(values, host, AF_INET6) elif family == AF_INET: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET) elif family == AF_INET6: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET6) else: if self.pool is not None: return self.pool.apply_e(BaseException, getaddrinfo, (host, origport, family, socktype, proto, flags)) else: return getaddrinfo(host, origport, family, socktype, proto, flags) values = values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append((AF_INET, socktype, proto, '', sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == '::1': dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, '', sockaddr)) result += result4 + result6 if not result: raise gaierror(-5, 'No address associated with hostname') return result
def test_getaddrinfo(self): '''Tests _socket.getaddrinfo''' joe = { ("127.0.0.1", 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 1): "[(2, 0, 0, '', ('127.0.0.1', 1))]", ("127.0.0.1", 0, 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 0): "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 1): "[(2, 0, 0, '', ('127.0.0.1', 0))]", } tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, -100000, 0) tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 100000, 0) tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0) #just try them as-is for params, value in joe.items(): addrinfo = _socket.getaddrinfo(*params) self.assertEqual(repr(addrinfo), value) #change the address family for addr_fam in ["AF_INET", "AF_UNSPEC"]: addrinfo = _socket.getaddrinfo("127.0.0.1", 0, eval("_socket." + addr_fam), 0, 0, 0) self.assertEqual(repr(addrinfo), "[(2, 0, 0, '', ('127.0.0.1', 0))]") #change the _socket type for socktype in ["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]: socktype = eval("_socket." + socktype) addrinfo = _socket.getaddrinfo("127.0.0.1", 0, 0, socktype, 0, 0) self.assertEqual( repr(addrinfo), "[(2, " + str(socktype) + ", 0, '', ('127.0.0.1', 0))]") #change the protocol for proto in IPPROTO_DICT.keys( ): #["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]: try: proto = eval("_socket." + proto) except: print(proto) continue addrinfo = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, proto, 0) self.assertEqual( repr(addrinfo), "[(2, 0, " + str(proto) + ", '', ('127.0.0.1', 0))]") #negative cases #TODO - this actually passes on a Windows 7 machine... #self.assertRaises(_socket.gaierror, _socket.getaddrinfo, "should never work.dfkdfjkkjdfkkdfjkdjf", 0) self.assertRaises(_socket.gaierror, _socket.getaddrinfo, "1", 0) if is_cli: self.assertRaises(_socket.gaierror, _socket.getaddrinfo, ".", 0) else: self.assertRaises(UnicodeError, _socket.getaddrinfo, ".", 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 3.14, 0, 0, 0, 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 0, -1, 0, 0, 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 0, 0, -1, 0, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 1000000, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, -1000000, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): if isinstance(host, text_type): host = host.encode("idna") elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: values = Values(self.hub, 2) ares.gethostbyname(values, host, AF_INET) ares.gethostbyname(values, host, AF_INET6) elif family == AF_INET: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET) elif family == AF_INET6: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET6) else: raise gaierror(5, "ai_family not supported: %r" % (family,)) values = values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append((AF_INET, socktype, proto, "", sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == "::1": dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, "", sockaddr)) result += result4 + result6 if not result: raise gaierror(-5, "No address associated with hostname") return result
def _getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0): if isinstance(host, unicode): host = host.encode('idna') elif not isinstance(host, str) or (flags & AI_NUMERICHOST): # this handles cases which do not require network access # 1) host is None # 2) host is of an invalid type # 3) AI_NUMERICHOST flag is set if self.pool is not None: return self.pool.apply_e( BaseException, getaddrinfo, (host, port, family, socktype, proto, flags)) else: return getaddrinfo(host, port, family, socktype, proto, flags) # we also call _socket.getaddrinfo below if family is not one of AF_* if self.pool is not None and '.' not in host: # localhost, <broadcast> return self.pool.apply_e( BaseException, getaddrinfo, (host, port, family, socktype, proto, flags)) origport = port port, socktypes = self._lookup_port(port, socktype) socktype_proto = [(SOCK_STREAM, 6), (SOCK_DGRAM, 17), (SOCK_RAW, 0)] if socktypes: socktype_proto = [(x, y) for (x, y) in socktype_proto if x in socktypes] if proto: socktype_proto = [(x, y) for (x, y) in socktype_proto if proto == y] ares = self.ares if family == AF_UNSPEC: values = Values(self.hub, 2) ares.gethostbyname(values, host, AF_INET) ares.gethostbyname(values, host, AF_INET6) elif family == AF_INET: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET) elif family == AF_INET6: values = Values(self.hub, 1) ares.gethostbyname(values, host, AF_INET6) else: if self.pool is not None: return self.pool.apply_e( BaseException, getaddrinfo, (host, origport, family, socktype, proto, flags)) else: return getaddrinfo(host, origport, family, socktype, proto, flags) values = values.get() if len(values) == 2 and values[0] == values[1]: values.pop() result = [] result4 = [] result6 = [] for addrs in values: if addrs.family == AF_INET: for addr in addrs[-1]: sockaddr = (addr, port) for socktype, proto in socktype_proto: result4.append( (AF_INET, socktype, proto, '', sockaddr)) elif addrs.family == AF_INET6: for addr in addrs[-1]: if addr == '::1': dest = result else: dest = result6 sockaddr = (addr, port, 0, 0) for socktype, proto in socktype_proto: dest.append((AF_INET6, socktype, proto, '', sockaddr)) result += result4 + result6 if not result: raise gaierror(-5, 'No address associated with hostname') return result
def test_getaddrinfo(self): '''Tests _socket.getaddrinfo''' joe = { ("127.0.0.1", 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 1) : "[(2, 0, 0, '', ('127.0.0.1', 1))]", ("127.0.0.1", 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", ("127.0.0.1", 0, 0, 0, 0, 1) : "[(2, 0, 0, '', ('127.0.0.1', 0))]", } tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, -100000, 0) tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 100000, 0) tmp = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0) #just try them as-is for params,value in joe.iteritems(): addrinfo = _socket.getaddrinfo(*params) self.assertEqual(repr(addrinfo), value) #change the address family for addr_fam in ["AF_INET", "AF_UNSPEC"]: addrinfo = _socket.getaddrinfo("127.0.0.1", 0, eval("_socket." + addr_fam), 0, 0, 0) self.assertEqual(repr(addrinfo), "[(2, 0, 0, '', ('127.0.0.1', 0))]") #change the _socket type for socktype in ["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]: socktype = eval("_socket." + socktype) addrinfo = _socket.getaddrinfo("127.0.0.1", 0, 0, socktype, 0, 0) self.assertEqual(repr(addrinfo), "[(2, " + str(socktype) + ", 0, '', ('127.0.0.1', 0))]") #change the protocol for proto in IPPROTO_DICT.keys():#["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]: try: proto = eval("_socket." + proto) except: print proto continue addrinfo = _socket.getaddrinfo("127.0.0.1", 0, 0, 0, proto, 0) self.assertEqual(repr(addrinfo), "[(2, 0, " + str(proto) + ", '', ('127.0.0.1', 0))]") #negative cases #TODO - this actually passes on a Windows 7 machine... #self.assertRaises(_socket.gaierror, _socket.getaddrinfo, "should never work.dfkdfjkkjdfkkdfjkdjf", 0) self.assertRaises(_socket.gaierror, _socket.getaddrinfo, "1", 0) self.assertRaises(_socket.gaierror, _socket.getaddrinfo, ".", 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 3.14, 0, 0, 0, 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 0, -1, 0, 0, 0) self.assertRaises(_socket.error, _socket.getaddrinfo, "127.0.0.1", 0, 0, -1, 0, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 1000000, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, -1000000, 0) _socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)