From: Bob Halley Date: Sun, 8 Apr 2012 11:10:44 +0000 (+0100) Subject: try TCP if UDP response is truncated X-Git-Tag: v1.10.0-py3~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc59c1ed92027ba82e6807e01f28b9b8cb8a120f;p=thirdparty%2Fdnspython.git try TCP if UDP response is truncated --- diff --git a/ChangeLog b/ChangeLog index 108cb403..9b5d4e7d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-04-08 Bob Halley + + * dns/resolver.py (Resolver.query): Switch to TCP when a UDP + response is truncated. Handle nameservers that serve on UDP + but not TCP. + 2012-04-07 Bob Halley * dns/zone.py (from_xfr): dns.zone.from_xfr() now takes a diff --git a/dns/resolver.py b/dns/resolver.py index c6214ca2..9f9b438a 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -23,6 +23,7 @@ import sys import time import dns.exception +import dns.flags import dns.ipv4 import dns.ipv6 import dns.message @@ -772,6 +773,13 @@ class Resolver(object): response = dns.query.udp(request, nameserver, timeout, self.port, source=source) + if response.flags & dns.flags.TC: + # Response truncated; retry with TCP. + timeout = self._compute_timeout(start) + response = dns.query.tcp(request, nameserver, + timeout, self.port, + source=source) + except (socket.error, dns.exception.Timeout): # # Communication failure or timeout. Go to the @@ -794,6 +802,16 @@ class Resolver(object): nameservers.remove(nameserver) response = None continue + except EOFError: + # + # We're using TCP and they hung up on us. + # Probably they don't support TCP (though + # they're supposed to!). Take it out of the + # mix and continue. + # + nameservers.remove(nameserver) + response = None + continue rcode = response.rcode() if rcode == dns.rcode.NOERROR or \ rcode == dns.rcode.NXDOMAIN: