From: Bob Halley Date: Sun, 8 Apr 2012 11:10:28 +0000 (+0100) Subject: try TCP if UDP response is truncated X-Git-Tag: v1.10.0~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7523aa1e5db9ad7cb3365e8498844b22ebd87bf2;p=thirdparty%2Fdnspython.git try TCP if UDP response is truncated --- diff --git a/ChangeLog b/ChangeLog index 5ab98387..9b5d4e7d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +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 diff --git a/dns/resolver.py b/dns/resolver.py index 90f95e8e..2f56c7de 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 @@ -778,6 +779,12 @@ 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 @@ -800,6 +807,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: