Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in Hostname Resolution with multiple Nameservers #1086

Closed
ecedreamer opened this issue May 23, 2024 · 2 comments
Closed

Issue in Hostname Resolution with multiple Nameservers #1086

ecedreamer opened this issue May 23, 2024 · 2 comments

Comments

@ecedreamer
Copy link

Whenever I try to resolve a hostname by providing multiple nameservers, the resolution is successful in one order of nameservers, but not successful in another order of nameservers.

To Reproduce
In the script below

import dns.resolver

def resolve_hostname(hostname, dns_servers):
    try:
        # Resolve A record (IPv4 address)i
        resolver = dns.resolver.Resolver()
        resolver.nameservers = dns_servers
        result = resolver.resolve(hostname, 'A')
        for ipval in result:
            print(f'IP Address: {ipval.to_text()}')

    except dns.resolver.NoAnswer:
        print(f'No answer found for {hostname}')
    except dns.resolver.NXDOMAIN:
        print(f'Hostname {hostname} does not exist')
    except dns.exception.Timeout:
        print(f'Request to resolve {hostname} timed out')
    except dns.exception.DNSException as e:
        print(f'An error occurred: {e}')

if __name__ == "__main__":
    hostname_to_resolve = 'my.host.name'
    print("Forward Order")
    resolve_hostname(hostname_to_resolve, ["10.3.3.2", "10.3.4.5"])
    print("=============================================")
    print("Reverse Order")
    resolve_hostname(hostname_to_resolve, ["10.3.4.5", "10.0.3.2"])

If "my.host.name" was present in "10.3.3.2" nameserver only, then the code below gives the resolved IP

resolve_hostname(hostname_to_resolve, ["10.3.3.2", "10.3.4.5"])

but the code below where nameservers are passed in reverse order can not give the result

resolve_hostname(hostname_to_resolve, ["10.3.4.5", "10.0.3.2"])

On digging down further,

1133 (answer, done) = resolution.query_result(response, None) 

The line above in the resolver.py is giving done=True even if the answer is empty. This is not allowing us to check the hostname in other nameservers.

Context

  • dnspython version 2.2.1, ..., 2.6.1
  • Python version 3.7.4, ..., 3.6.1
  • OS: all
@rthalley
Copy link
Owner

The list of nameservers in the stub resolver configuration is for redundancy; it is not a search list. Resolution stops as soon as any nameserver answers the question positively or negatively. So, for example if you are querying for "my.host.name" type AAAA and the first nameserver has rcode NXDOMAIN, or has a NOERROR rcode but there is no AAAA, then those negative responses are still answers and resolution stops. Resolution would only proceed past the first nameserver if the first nameserver didn't reply, replied with something that didn't parse, or had an rcode other than NOERROR, NXDOMAIN, or YXDOMAIN.

@ecedreamer
Copy link
Author

Thanks @rthalley for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants