// canUseCgo reports whether calling cgo functions is allowed
// for non-hostname lookups.
func (c *conf) canUseCgo() bool {
- return c.hostLookupOrder("") == hostLookupCgo
+ return c.hostLookupOrder(nil, "") == hostLookupCgo
}
// hostLookupOrder determines which strategy to use to resolve hostname.
-func (c *conf) hostLookupOrder(hostname string) (ret hostLookupOrder) {
+// The provided Resolver is optional. nil means to not consider its options.
+func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrder) {
if c.dnsDebugLevel > 1 {
defer func() {
print("go package net: hostLookupOrder(", hostname, ") = ", ret.String(), "\n")
}()
}
fallbackOrder := hostLookupCgo
- if c.netGo {
+ if c.netGo || (r != nil && r.PreferGo) {
fallbackOrder = hostLookupFilesDNS
}
if c.forceCgoLookupHost || c.resolv.unknownOpt || c.goos == "android" {
tests := []struct {
name string
c *conf
+ resolver *Resolver
hostTests []nssHostTest
}{
{
{"x.com", "myhostname", hostLookupCgo},
},
},
+ // Issue 24393: make sure "Resolver.PreferGo = true" acts like netgo.
+ {
+ name: "resolver-prefergo",
+ resolver: &Resolver{PreferGo: true},
+ c: &conf{
+ goos: "darwin",
+ forceCgoLookupHost: true, // always true for darwin
+ resolv: defaultResolvConf,
+ nss: nssStr(""),
+ netCgo: true,
+ },
+ hostTests: []nssHostTest{
+ {"localhost", "myhostname", hostLookupFilesDNS},
+ },
+ },
}
origGetHostname := getHostname
for _, ht := range tt.hostTests {
getHostname = func() (string, error) { return ht.localhost, nil }
- gotOrder := tt.c.hostLookupOrder(ht.host)
+ gotOrder := tt.c.hostLookupOrder(tt.resolver, ht.host)
if gotOrder != ht.want {
t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want)
}
// goLookupIP is the native Go implementation of LookupIP.
// The libc versions are in cgo_*.go.
func (r *Resolver) goLookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
- order := systemConf().hostLookupOrder(host)
+ order := systemConf().hostLookupOrder(r, host)
addrs, _, err = r.goLookupIPCNAMEOrder(ctx, host, order)
return
}
// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME.
func (r *Resolver) goLookupCNAME(ctx context.Context, host string) (string, error) {
- order := systemConf().hostLookupOrder(host)
+ order := systemConf().hostLookupOrder(r, host)
_, cname, err := r.goLookupIPCNAMEOrder(ctx, host, order)
return cname.String(), err
}
}
func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
- order := systemConf().hostLookupOrder(host)
+ order := systemConf().hostLookupOrder(r, host)
if !r.PreferGo && order == hostLookupCgo {
if addrs, err, ok := cgoLookupHost(ctx, host); ok {
return addrs, err
if r.PreferGo {
return r.goLookupIP(ctx, host)
}
- order := systemConf().hostLookupOrder(host)
+ order := systemConf().hostLookupOrder(r, host)
if order == hostLookupCgo {
if addrs, err, ok := cgoLookupIP(ctx, host); ok {
return addrs, err