Pure go dns resolver return directly if found entry in /etc/hosts #42829
Comments
Have you configured your system to not use /etc/hosts, i.e., edited /etc/nsswitch.conf to not have "files" before "dns" on a line starting with "hosts:" ? I believe the intended behavior is for the pure Go resolver (GODEBUG=netdns=go) to mimic the behavior of the system resolver (GODEBUG=netdns=cgo) as closely as reasonable. If nsswitch.conf is configured to use /etc/hosts (e.g., hosts: files dns), then I think what you describe actually is intended behavior: Go sees that you have configured your system to resolve 10.0.8.6 to a single name by having a single entry in /etc/hosts. If you add the second name for 10.0.8.6 to /etc/hosts, then the Go resolver will return both. If you remove the entry from /etc/hosts altogether, then the Go resolver will use DNS and again return both. I tested, and I can confirm on my end that if I do configure nsswitch to not use /etc/hosts, then the pure Go resolver still does use it for reverse lookups (LookupAddr()), and this sounds like a bug. Forward lookups like LookupHost() works as expected. The way I've understood things: The "pure Go resolver" is not a "pure DNS client". I hope the docs don't make it sound like that is the case. The Go resolver attempts to mimic and implement in Go the behavior of the C library or system resolver that other programs use. This means that it reads /etc/nsswitch.conf, /etc/resolv.conf and /etc/hosts and acts on these. The intention is that Go programs resolve names and addresses the same way as other programs like C and Python programs. The system administrator can configure name resolution for the whole system by editing e.g., nsswitch.conf or /etc/hosts, and all programs will use this. The admin doesn't need to separately configure Go programs and other programs. dig is not necessarily a good example, because it does not use a resolver but is explicitly a DNS query tool. If you use curl, ssh, ping, you will find that they too use nsswitch, /etc/hosts etc., as they should. If a DNS client for Go is what you are looking for, then https://github.com/miekg/dns is excellent and is based on the DNS code in the pure Go resolver. |
Thanks @antong . |
Closing this at the request of the issue creator. Please feel free to open the issue again if you feel this was closed in error. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
dns settings
dig -x 10.0.8.6 return 2 domains
The pure go dnsResolver only return one if there's one entry in /etc/hosts
I checked the golang code here:
go/src/net/dnsclient_unix.go
Line 741 in 750b372
that the pure go resolver hardcoded check /etc/hosts/ first, and if found, return.
What did you expect to see?
net.DefaultResolver.LookupAddr(ctx, 10.0.8.6) return two entries
What did you see instead?
net.DefaultResolver.LookupAddr(ctx, 10.0.8.6) return only one entry
The text was updated successfully, but these errors were encountered: