Commit 5e1306d1 authored by Jason Toffaletti's avatar Jason Toffaletti

fallback when evdns isn't available

--HG--
extra : transplant_source : k%A2%0F%08%E0%13f%93%02%89%DC%E2%CF%0Dc%EE%81%A6%83%8A
parent 23799440
...@@ -639,69 +639,74 @@ def wrap_ssl000(sock, keyfile=None, certfile=None): ...@@ -639,69 +639,74 @@ def wrap_ssl000(sock, keyfile=None, certfile=None):
return ssl_sock return ssl_sock
# TODO: if core.HAS_EVDNS:
# might need to map evdns errors to socket errors # TODO:
# for example, DNS_ERR_NOTEXIST(3) is: # might need to map evdns errors to socket errors
# socket.gaierror: [Errno -2] Name or service not known # for example, DNS_ERR_NOTEXIST(3) is:
# socket.gaierror: [Errno -2] Name or service not known
def _dns_helper(result, type, ttl, addrs, args):
(waiter,) = args def _dns_helper(result, type, ttl, addrs, args):
waiter.switch((result, type, ttl, addrs)) (waiter,) = args
waiter.switch((result, type, ttl, addrs))
_ip_re = re.compile('[\d\.]+')
_ip_re = re.compile('[\d\.]+')
def gethostbyname(hostname):
# TODO: this is supposed to iterate through all the addresses def gethostbyname(hostname):
# could use a global dict(hostname, iter) # TODO: this is supposed to iterate through all the addresses
# - fix these nasty hacks for localhost, ips, etc. # could use a global dict(hostname, iter)
if hostname == 'localhost': # hack # - fix these nasty hacks for localhost, ips, etc.
return '127.0.0.1' if hostname == 'localhost': # hack
if _ip_re.match(hostname): # hack return '127.0.0.1'
return hostname if _ip_re.match(hostname): # hack
waiter = Waiter() return hostname
core.dns_resolve_ipv4(hostname, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter) waiter = Waiter()
result, type, ttl, addrs = waiter.wait() core.dns_resolve_ipv4(hostname, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter)
if result != core.DNS_ERR_NONE: result, type, ttl, addrs = waiter.wait()
# hack to make testSockName pass if result != core.DNS_ERR_NONE:
# should use /etc/hosts # hack to make testSockName pass
if hostname == __socket__.gethostname(): # should use /etc/hosts
return '0.0.0.0' if hostname == __socket__.gethostname():
raise gaierror(result) return '0.0.0.0'
return random.choice(addrs) raise gaierror(result)
return random.choice(addrs)
def getaddrinfo(host, port, family=__socket__.AF_INET, socktype=__socket__.SOCK_STREAM, proto=0, flags=0):
waiter = Waiter() def getaddrinfo(host, port, family=__socket__.AF_INET, socktype=__socket__.SOCK_STREAM, proto=0, flags=0):
if family == __socket__.AF_INET: waiter = Waiter()
core.dns_resolve_ipv4(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter) if family == __socket__.AF_INET:
elif family == __socket__.AF_INET6: core.dns_resolve_ipv4(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter)
core.dns_resolve_ipv6(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter) elif family == __socket__.AF_INET6:
else: core.dns_resolve_ipv6(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter)
raise NotImplementedError else:
result, type, ttl, addrs = waiter.wait() raise NotImplementedError
if result != core.DNS_ERR_NONE: result, type, ttl, addrs = waiter.wait()
raise gaierror(result) if result != core.DNS_ERR_NONE:
r = [] raise gaierror(result)
for addr in addrs: r = []
r.append((family, socktype, proto, '', (addr, port))) for addr in addrs:
return r r.append((family, socktype, proto, '', (addr, port)))
return r
def getnameinfo(sockaddr, flags):
# http://svn.python.org/view/python/trunk/Modules/socketmodule.c?view=markup def getnameinfo(sockaddr, flags):
# see socket_getnameinfo # http://svn.python.org/view/python/trunk/Modules/socketmodule.c?view=markup
try: # see socket_getnameinfo
host, port = sockaddr[:2] try:
port = int(port) host, port = sockaddr[:2]
except: port = int(port)
# make testRefCountGetNameInfo pass except ValueError:
del sockaddr # make testRefCountGetNameInfo pass
raise SystemError del sockaddr
waiter = Waiter() raise SystemError
core.dns_resolve_reverse(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter) waiter = Waiter()
result, type, ttl, addrs = waiter.wait() core.dns_resolve_reverse(host, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter)
if result != core.DNS_ERR_NONE: result, type, ttl, addrs = waiter.wait()
raise gaierror(result) if result != core.DNS_ERR_NONE:
return (addrs, port) raise gaierror(result)
return (addrs, port)
else:
# fallback to blocking versions
gethostbyname = __socket__.gethostbyname
getaddrinfo = __socket__.getaddrinfo
getnameinfo = __socket__.getnameinfo
def wrap_ssl(sock, keyfile=None, certfile=None): def wrap_ssl(sock, keyfile=None, certfile=None):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment