]> Cypherpunks repositories - gostls13.git/commitdiff
net: centralize resolver selection logic
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Sat, 26 Aug 2023 16:43:17 +0000 (16:43 +0000)
committerGopher Robot <gobot@golang.org>
Sat, 26 Aug 2023 22:50:29 +0000 (22:50 +0000)
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 <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/conf.go
src/net/dnsclient_unix.go
src/net/lookup_plan9.go
src/net/lookup_unix.go
src/net/lookup_windows.go

index 77cc6355928b01dbbd8783df011710f5c10dd8d6..ff3ec20c8a83a650a9e775dcce2a80d9e9eb38d0 100644 (file)
@@ -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.
index 6f2bdbed2d5a72570aa4db8b8492a156e2c7fcfb..6dc4dbe269164ab195a584f47b52c978aaedc896 100644 (file)
@@ -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
 }
index 5404b996e449395c89dcec49226af27dd3351260..c49b5a50891b1c15cd26168e06e9d41fa25d5c96 100644 (file)
@@ -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)
index 56ae11e961b6e050704548ce3e830b2aa131dcff..8b852beef32e94417084dbf8f694d6ce9c85f3a8 100644 (file)
@@ -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)
index 33d5ac5fb45d6a39c3f917b51dc6740bf8274add..c370c790be78547d6e05b0533cc8d0326cf24e92 100644 (file)
@@ -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)
        }