Skip to content

Commit 0b20aec

Browse files
committed
net: make Resolver.PreferGo work more as documented
Fixes #24393 Change-Id: I8bcee34cdf30472663d866ed6056301d8445215c Reviewed-on: https://go-review.googlesource.com/100875 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 86a3389 commit 0b20aec

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/net/conf.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,19 @@ func initConfVal() {
114114
// canUseCgo reports whether calling cgo functions is allowed
115115
// for non-hostname lookups.
116116
func (c *conf) canUseCgo() bool {
117-
return c.hostLookupOrder("") == hostLookupCgo
117+
return c.hostLookupOrder(nil, "") == hostLookupCgo
118118
}
119119

120120
// hostLookupOrder determines which strategy to use to resolve hostname.
121-
func (c *conf) hostLookupOrder(hostname string) (ret hostLookupOrder) {
121+
// The provided Resolver is optional. nil means to not consider its options.
122+
func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrder) {
122123
if c.dnsDebugLevel > 1 {
123124
defer func() {
124125
print("go package net: hostLookupOrder(", hostname, ") = ", ret.String(), "\n")
125126
}()
126127
}
127128
fallbackOrder := hostLookupCgo
128-
if c.netGo {
129+
if c.netGo || (r != nil && r.PreferGo) {
129130
fallbackOrder = hostLookupFilesDNS
130131
}
131132
if c.forceCgoLookupHost || c.resolv.unknownOpt || c.goos == "android" {

src/net/conf_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestConfHostLookupOrder(t *testing.T) {
3333
tests := []struct {
3434
name string
3535
c *conf
36+
resolver *Resolver
3637
hostTests []nssHostTest
3738
}{
3839
{
@@ -322,6 +323,21 @@ func TestConfHostLookupOrder(t *testing.T) {
322323
{"x.com", "myhostname", hostLookupCgo},
323324
},
324325
},
326+
// Issue 24393: make sure "Resolver.PreferGo = true" acts like netgo.
327+
{
328+
name: "resolver-prefergo",
329+
resolver: &Resolver{PreferGo: true},
330+
c: &conf{
331+
goos: "darwin",
332+
forceCgoLookupHost: true, // always true for darwin
333+
resolv: defaultResolvConf,
334+
nss: nssStr(""),
335+
netCgo: true,
336+
},
337+
hostTests: []nssHostTest{
338+
{"localhost", "myhostname", hostLookupFilesDNS},
339+
},
340+
},
325341
}
326342

327343
origGetHostname := getHostname
@@ -331,7 +347,7 @@ func TestConfHostLookupOrder(t *testing.T) {
331347
for _, ht := range tt.hostTests {
332348
getHostname = func() (string, error) { return ht.localhost, nil }
333349

334-
gotOrder := tt.c.hostLookupOrder(ht.host)
350+
gotOrder := tt.c.hostLookupOrder(tt.resolver, ht.host)
335351
if gotOrder != ht.want {
336352
t.Errorf("%s: hostLookupOrder(%q) = %v; want %v", tt.name, ht.host, gotOrder, ht.want)
337353
}

src/net/dnsclient_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func goLookupIPFiles(name string) (addrs []IPAddr) {
524524
// goLookupIP is the native Go implementation of LookupIP.
525525
// The libc versions are in cgo_*.go.
526526
func (r *Resolver) goLookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
527-
order := systemConf().hostLookupOrder(host)
527+
order := systemConf().hostLookupOrder(r, host)
528528
addrs, _, err = r.goLookupIPCNAMEOrder(ctx, host, order)
529529
return
530530
}
@@ -676,7 +676,7 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order
676676

677677
// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME.
678678
func (r *Resolver) goLookupCNAME(ctx context.Context, host string) (string, error) {
679-
order := systemConf().hostLookupOrder(host)
679+
order := systemConf().hostLookupOrder(r, host)
680680
_, cname, err := r.goLookupIPCNAMEOrder(ctx, host, order)
681681
return cname.String(), err
682682
}

src/net/lookup_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *Resolver) dial(ctx context.Context, network, server string) (Conn, erro
7474
}
7575

7676
func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string, err error) {
77-
order := systemConf().hostLookupOrder(host)
77+
order := systemConf().hostLookupOrder(r, host)
7878
if !r.PreferGo && order == hostLookupCgo {
7979
if addrs, err, ok := cgoLookupHost(ctx, host); ok {
8080
return addrs, err
@@ -89,7 +89,7 @@ func (r *Resolver) lookupIP(ctx context.Context, host string) (addrs []IPAddr, e
8989
if r.PreferGo {
9090
return r.goLookupIP(ctx, host)
9191
}
92-
order := systemConf().hostLookupOrder(host)
92+
order := systemConf().hostLookupOrder(r, host)
9393
if order == hostLookupCgo {
9494
if addrs, err, ok := cgoLookupIP(ctx, host); ok {
9595
return addrs, err

0 commit comments

Comments
 (0)