Skip to content

Commit

Permalink
windows DNS resolution: follow-up 9b0773c
Browse files Browse the repository at this point in the history
related: #5176, #4421

prev was failing on systems where
IPv6 is not available but DNS can resolve AAAA records
(my artificial test environment with IPv6 disabled was also filtering AAAA DNS)
  • Loading branch information
SomberNight committed Mar 26, 2019
1 parent 1cfac92 commit c4fb58c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions electrum/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,31 +526,36 @@ def needs_dns_resolving(host):
return False
return True
def resolve_with_dnspython(host):
addrs = []
# try IPv6
try:
answers = dns.resolver.query(host, dns.rdatatype.AAAA)
return str(answers[0])
addrs += [str(answer) for answer in answers]
except dns.exception.DNSException as e:
pass
except BaseException as e:
print_error(f'dnspython failed to resolve dns (AAAA) with error: {e}')
# try IPv4
try:
answers = dns.resolver.query(host, dns.rdatatype.A)
return str(answers[0])
addrs += [str(answer) for answer in answers]
except dns.exception.DNSException as e:
# dns failed for some reason, e.g. dns.resolver.NXDOMAIN
# this is normal. Simply report back failure:
raise socket.gaierror(11001, 'getaddrinfo failed') from e

This comment has been minimized.

Copy link
@cculianu

cculianu Mar 26, 2019

Collaborator

Question: Would it make sense to make this conditional on addrs being empty here? Not sure if that can actually happen in reality (AAAA working, but A raising?) -- but if you did get an answer for AAAA but an exception for A: the question is -- is it better to return what you got or to propagate the exception outward?

Honest question.

This comment has been minimized.

Copy link
@SomberNight

SomberNight Mar 26, 2019

Author Member

You're right, it should suppress the exception then. See d07fce0

except BaseException as e:
# Possibly internal error in dnspython :( see #4483
print_error(f'dnspython failed to resolve dns (A) with error: {e}')
if addrs:
return addrs
# Fall back to original socket.getaddrinfo to resolve dns.
return host
addr = host
return [host]
addrs = [host]
if needs_dns_resolving(host):
addr = resolve_with_dnspython(host)
return socket._getaddrinfo(addr, *args, **kwargs)
addrs = resolve_with_dnspython(host)
list_of_list_of_socketinfos = [socket._getaddrinfo(addr, *args, **kwargs) for addr in addrs]
list_of_socketinfos = [item for lst in list_of_list_of_socketinfos for item in lst]
return list_of_socketinfos

@log_exceptions
async def set_parameters(self, net_params: NetworkParameters):
Expand Down

1 comment on commit c4fb58c

@cculianu
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. 👍

Please sign in to comment.