From f7a29a67ac18a27bfa74bc9caeb6bd886fc3173c Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Thu, 18 Jun 2009 14:10:28 +0100 Subject: [PATCH] When checking for "same address", use the binary form of the address. By doing that, we don't get confused if the textual forms for the addresses are equivalent but different (which can happen with IPv6 addresses). Thanks to Kim Davies for finding this bug. --- dns/query.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/dns/query.py b/dns/query.py index 93adc2eb..81a78b43 100644 --- a/dns/query.py +++ b/dns/query.py @@ -72,6 +72,14 @@ def _wait_for_readable(s, expiration): def _wait_for_writable(s, expiration): _wait_for([], [s], [s], expiration) +def _addresses_equal(af, a1, a2): + # Convert the first value of the tuple, which is a textual format + # address into binary form, so that we are not confused by different + # textual representations of the same address + n1 = dns.inet.inet_pton(af, a1[0]) + n2 = dns.inet.inet_pton(af, a2[0]) + return n1 == n2 and a1[1:] == a2[1:] + def udp(q, where, timeout=None, port=53, af=None, source=None, source_port=0, ignore_unexpected=False, one_rr_per_rrset=False): """Return the response obtained after sending a query via UDP. @@ -127,9 +135,9 @@ def udp(q, where, timeout=None, port=53, af=None, source=None, source_port=0, while 1: _wait_for_readable(s, expiration) (wire, from_address) = s.recvfrom(65535) - if from_address == destination or \ - (dns.inet.is_multicast(where) and \ - from_address[1] == destination[1]): + if _addresses_equal(from_address, destination) or \ + (dns.inet.is_multicast(where) and \ + from_address[1:] == destination[1:]): break if not ignore_unexpected: raise UnexpectedSource, \ -- 2.47.3