From 071aed2aaa0ed819582c5bff44b70d43c61f504a Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 26 Aug 2023 16:43:17 +0000 Subject: [PATCH] net: centralize resolver selection logic This change removes the per GOOS hostLookupOrder wrappers. passes the correct hostname to hostLookupOrder (windows, plan9), so that the netdns+2 GODEBUG doesn't show empty hostnames. Uses the mustUseGoResolver instead of hostLookupOrder, hostLookupOrder should only be used for hostname resolution, not for lookups that do only DNS. Change-Id: I18bbff06957910ae25c2bc78dfa9a46da76529fd GitHub-Last-Rev: a27545dc25fffb3a51da9d943ffa9bd1a09182ee GitHub-Pull-Request: golang/go#61525 Reviewed-on: https://go-review.googlesource.com/c/go/+/512215 Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills Run-TryBot: Mateusz Poliwczak TryBot-Result: Gopher Robot --- src/net/conf.go | 19 ++++++++++++++++++- src/net/dnsclient_unix.go | 3 +-- src/net/lookup_plan9.go | 38 +++++++++----------------------------- src/net/lookup_unix.go | 3 --- src/net/lookup_windows.go | 28 ++++++++++------------------ 5 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/net/conf.go b/src/net/conf.go index 77cc6355928..ff3ec20c8a8 100644 --- a/src/net/conf.go +++ b/src/net/conf.go @@ -185,7 +185,24 @@ func goosPrefersCgo() bool { // required to use the go resolver. The provided Resolver is optional. // This will report true if the cgo resolver is not available. func (c *conf) mustUseGoResolver(r *Resolver) bool { - return c.netGo || r.preferGo() || !cgoAvailable + if !cgoAvailable { + return true + } + + if runtime.GOOS == "plan9" { + // TODO(bradfitz): for now we only permit use of the PreferGo + // implementation when there's a non-nil Resolver with a + // non-nil Dialer. This is a sign that they the code is trying + // to use their DNS-speaking net.Conn (such as an in-memory + // DNS cache) and they don't want to actually hit the network. + // Once we add support for looking the default DNS servers + // from plan9, though, then we can relax this. + if r == nil || r.Dial == nil { + return false + } + } + + return c.netGo || r.preferGo() } // addrLookupOrder determines which strategy to use to resolve addresses. diff --git a/src/net/dnsclient_unix.go b/src/net/dnsclient_unix.go index 6f2bdbed2d5..6dc4dbe2691 100644 --- a/src/net/dnsclient_unix.go +++ b/src/net/dnsclient_unix.go @@ -606,8 +606,7 @@ func goLookupIPFiles(name string) (addrs []IPAddr, canonical string) { // goLookupIP is the native Go implementation of LookupIP. // The libc versions are in cgo_*.go. -func (r *Resolver) goLookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) { - order, conf := systemConf().hostLookupOrder(r, host) +func (r *Resolver) goLookupIP(ctx context.Context, network, host string, order hostLookupOrder, conf *dnsConfig) (addrs []IPAddr, err error) { addrs, _, err = r.goLookupIPCNAMEOrder(ctx, network, host, order, conf) return } diff --git a/src/net/lookup_plan9.go b/src/net/lookup_plan9.go index 5404b996e44..c49b5a50891 100644 --- a/src/net/lookup_plan9.go +++ b/src/net/lookup_plan9.go @@ -184,31 +184,11 @@ loop: return } -// preferGoOverPlan9 reports whether the resolver should use the -// "PreferGo" implementation rather than asking plan9 services -// for the answers. -func (r *Resolver) preferGoOverPlan9() bool { - _, _, res := r.preferGoOverPlan9WithOrderAndConf() - return res -} - -func (r *Resolver) preferGoOverPlan9WithOrderAndConf() (hostLookupOrder, *dnsConfig, bool) { - order, conf := systemConf().hostLookupOrder(r, "") // name is unused - - // TODO(bradfitz): for now we only permit use of the PreferGo - // implementation when there's a non-nil Resolver with a - // non-nil Dialer. This is a sign that they the code is trying - // to use their DNS-speaking net.Conn (such as an in-memory - // DNS cache) and they don't want to actually hit the network. - // Once we add support for looking the default DNS servers - // from plan9, though, then we can relax this. - return order, conf, order != hostLookupCgo && r != nil && r.Dial != nil -} - func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) { - if r.preferGoOverPlan9() { - return r.goLookupIP(ctx, network, host) + if order, conf := systemConf().hostLookupOrder(r, host); order != hostLookupCgo { + return r.goLookupIP(ctx, network, host, order, conf) } + lits, err := r.lookupHost(ctx, host) if err != nil { return @@ -253,7 +233,7 @@ func (*Resolver) lookupPort(ctx context.Context, network, service string) (port } func (r *Resolver) lookupCNAME(ctx context.Context, name string) (cname string, err error) { - if order, conf, preferGo := r.preferGoOverPlan9WithOrderAndConf(); preferGo { + if order, conf := systemConf().hostLookupOrder(r, name); order != hostLookupCgo { return r.goLookupCNAME(ctx, name, order, conf) } @@ -274,7 +254,7 @@ func (r *Resolver) lookupCNAME(ctx context.Context, name string) (cname string, } func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*SRV, err error) { - if r.preferGoOverPlan9() { + if systemConf().mustUseGoResolver(r) { return r.goLookupSRV(ctx, service, proto, name) } var target string @@ -306,7 +286,7 @@ func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) ( } func (r *Resolver) lookupMX(ctx context.Context, name string) (mx []*MX, err error) { - if r.preferGoOverPlan9() { + if systemConf().mustUseGoResolver(r) { return r.goLookupMX(ctx, name) } lines, err := queryDNS(ctx, name, "mx") @@ -327,7 +307,7 @@ func (r *Resolver) lookupMX(ctx context.Context, name string) (mx []*MX, err err } func (r *Resolver) lookupNS(ctx context.Context, name string) (ns []*NS, err error) { - if r.preferGoOverPlan9() { + if systemConf().mustUseGoResolver(r) { return r.goLookupNS(ctx, name) } lines, err := queryDNS(ctx, name, "ns") @@ -345,7 +325,7 @@ func (r *Resolver) lookupNS(ctx context.Context, name string) (ns []*NS, err err } func (r *Resolver) lookupTXT(ctx context.Context, name string) (txt []string, err error) { - if r.preferGoOverPlan9() { + if systemConf().mustUseGoResolver(r) { return r.goLookupTXT(ctx, name) } lines, err := queryDNS(ctx, name, "txt") @@ -361,7 +341,7 @@ func (r *Resolver) lookupTXT(ctx context.Context, name string) (txt []string, er } func (r *Resolver) lookupAddr(ctx context.Context, addr string) (name []string, err error) { - if order, conf, preferGo := r.preferGoOverPlan9WithOrderAndConf(); preferGo { + if order, conf := systemConf().addrLookupOrder(r, addr); order != hostLookupCgo { return r.goLookupPTR(ctx, addr, order, conf) } arpa, err := reverseaddr(addr) diff --git a/src/net/lookup_unix.go b/src/net/lookup_unix.go index 56ae11e961b..8b852beef32 100644 --- a/src/net/lookup_unix.go +++ b/src/net/lookup_unix.go @@ -62,9 +62,6 @@ func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, } func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) { - if r.preferGo() { - return r.goLookupIP(ctx, network, host) - } order, conf := systemConf().hostLookupOrder(r, host) if order == hostLookupCgo { return cgoLookupIP(ctx, network, host) diff --git a/src/net/lookup_windows.go b/src/net/lookup_windows.go index 33d5ac5fb45..c370c790be7 100644 --- a/src/net/lookup_windows.go +++ b/src/net/lookup_windows.go @@ -91,19 +91,11 @@ func (r *Resolver) lookupHost(ctx context.Context, name string) ([]string, error return addrs, nil } -// preferGoOverWindows reports whether the resolver should use the -// pure Go implementation rather than making win32 calls to ask the -// kernel for its answer. -func (r *Resolver) preferGoOverWindows() bool { - conf := systemConf() - order, _ := conf.hostLookupOrder(r, "") // name is unused - return order != hostLookupCgo -} - func (r *Resolver) lookupIP(ctx context.Context, network, name string) ([]IPAddr, error) { - if r.preferGoOverWindows() { - return r.goLookupIP(ctx, network, name) + if order, conf := systemConf().hostLookupOrder(r, name); order != hostLookupCgo { + return r.goLookupIP(ctx, network, name, order, conf) } + // TODO(bradfitz,brainman): use ctx more. See TODO below. var family int32 = syscall.AF_UNSPEC @@ -200,7 +192,7 @@ func (r *Resolver) lookupIP(ctx context.Context, network, name string) ([]IPAddr } func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int, error) { - if r.preferGoOverWindows() { + if systemConf().mustUseGoResolver(r) { return lookupPortMap(network, service) } @@ -249,7 +241,7 @@ func (r *Resolver) lookupPort(ctx context.Context, network, service string) (int } func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) { - if order, conf := systemConf().hostLookupOrder(r, ""); order != hostLookupCgo { + if order, conf := systemConf().hostLookupOrder(r, name); order != hostLookupCgo { return r.goLookupCNAME(ctx, name, order, conf) } @@ -274,7 +266,7 @@ func (r *Resolver) lookupCNAME(ctx context.Context, name string) (string, error) } func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) { - if r.preferGoOverWindows() { + if systemConf().mustUseGoResolver(r) { return r.goLookupSRV(ctx, service, proto, name) } // TODO(bradfitz): finish ctx plumbing. Nothing currently depends on this. @@ -303,7 +295,7 @@ func (r *Resolver) lookupSRV(ctx context.Context, service, proto, name string) ( } func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) { - if r.preferGoOverWindows() { + if systemConf().mustUseGoResolver(r) { return r.goLookupMX(ctx, name) } // TODO(bradfitz): finish ctx plumbing. Nothing currently depends on this. @@ -326,7 +318,7 @@ func (r *Resolver) lookupMX(ctx context.Context, name string) ([]*MX, error) { } func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) { - if r.preferGoOverWindows() { + if systemConf().mustUseGoResolver(r) { return r.goLookupNS(ctx, name) } // TODO(bradfitz): finish ctx plumbing. Nothing currently depends on this. @@ -348,7 +340,7 @@ func (r *Resolver) lookupNS(ctx context.Context, name string) ([]*NS, error) { } func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) { - if r.preferGoOverWindows() { + if systemConf().mustUseGoResolver(r) { return r.goLookupTXT(ctx, name) } // TODO(bradfitz): finish ctx plumbing. Nothing currently depends on this. @@ -374,7 +366,7 @@ func (r *Resolver) lookupTXT(ctx context.Context, name string) ([]string, error) } func (r *Resolver) lookupAddr(ctx context.Context, addr string) ([]string, error) { - if order, conf := systemConf().hostLookupOrder(r, ""); order != hostLookupCgo { + if order, conf := systemConf().addrLookupOrder(r, addr); order != hostLookupCgo { return r.goLookupPTR(ctx, addr, order, conf) }