Skip to content

Commit af88fb6

Browse files
committed
net: permit use of Resolver.PreferGo, netgo on Windows and Plan 9
This reverts commit CL 401754 (440c931) which reverted CL 400654, thus reapplying CL 400654, re-adding the func init() { netGo = true } to cgo_stub.go CL 400654 had originally removed (mistakenly during development?) that had broken the darwin nocgo builder. Fixes #33097 Change-Id: I90f59746d2ceb6b5d2bd832c9fc90068f8ff7417 Reviewed-on: https://go-review.googlesource.com/c/go/+/409234 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent a21cf91 commit af88fb6

15 files changed

+835
-290
lines changed

src/net/addrselect.go

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build unix
6-
75
// Minimal RFC 6724 address selection.
86

97
package net

src/net/conf.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build unix
5+
//go:build !js
66

77
package net
88

@@ -21,7 +21,7 @@ type conf struct {
2121
forceCgoLookupHost bool
2222

2323
netGo bool // go DNS resolution forced
24-
netCgo bool // cgo DNS resolution forced
24+
netCgo bool // non-go DNS resolution forced (cgo, or win32)
2525

2626
// machine has an /etc/mdns.allow file
2727
hasMDNSAllow bool
@@ -49,9 +49,23 @@ func initConfVal() {
4949
confVal.dnsDebugLevel = debugLevel
5050
confVal.netGo = netGo || dnsMode == "go"
5151
confVal.netCgo = netCgo || dnsMode == "cgo"
52+
if !confVal.netGo && !confVal.netCgo && (runtime.GOOS == "windows" || runtime.GOOS == "plan9") {
53+
// Neither of these platforms actually use cgo.
54+
//
55+
// The meaning of "cgo" mode in the net package is
56+
// really "the native OS way", which for libc meant
57+
// cgo on the original platforms that motivated
58+
// PreferGo support before Windows and Plan9 got support,
59+
// at which time the GODEBUG=netdns=go and GODEBUG=netdns=cgo
60+
// names were already kinda locked in.
61+
confVal.netCgo = true
62+
}
5263

5364
if confVal.dnsDebugLevel > 0 {
5465
defer func() {
66+
if confVal.dnsDebugLevel > 1 {
67+
println("go package net: confVal.netCgo =", confVal.netCgo, " netGo =", confVal.netGo)
68+
}
5569
switch {
5670
case confVal.netGo:
5771
if netGo {
@@ -75,6 +89,10 @@ func initConfVal() {
7589
return
7690
}
7791

92+
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
93+
return
94+
}
95+
7896
// If any environment-specified resolver options are specified,
7997
// force cgo. Note that LOCALDOMAIN can change behavior merely
8098
// by being specified with the empty string.
@@ -129,7 +147,19 @@ func (c *conf) hostLookupOrder(r *Resolver, hostname string) (ret hostLookupOrde
129147
}
130148
fallbackOrder := hostLookupCgo
131149
if c.netGo || r.preferGo() {
132-
fallbackOrder = hostLookupFilesDNS
150+
switch c.goos {
151+
case "windows":
152+
// TODO(bradfitz): implement files-based
153+
// lookup on Windows too? I guess /etc/hosts
154+
// kinda exists on Windows. But for now, only
155+
// do DNS.
156+
fallbackOrder = hostLookupDNS
157+
default:
158+
fallbackOrder = hostLookupFilesDNS
159+
}
160+
}
161+
if c.goos == "windows" || c.goos == "plan9" {
162+
return fallbackOrder
133163
}
134164
if c.forceCgoLookupHost || c.resolv.unknownOpt || c.goos == "android" {
135165
return fallbackOrder

src/net/dnsclient_unix.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build unix
5+
//go:build !js
66

77
// DNS client: see RFC 1035.
88
// Has to be linked into package net for Dial.
@@ -20,6 +20,7 @@ import (
2020
"internal/itoa"
2121
"io"
2222
"os"
23+
"runtime"
2324
"sync"
2425
"time"
2526

@@ -381,12 +382,21 @@ func (conf *resolverConfig) tryUpdate(name string) {
381382
}
382383
conf.lastChecked = now
383384

384-
var mtime time.Time
385-
if fi, err := os.Stat(name); err == nil {
386-
mtime = fi.ModTime()
387-
}
388-
if mtime.Equal(conf.dnsConfig.mtime) {
389-
return
385+
switch runtime.GOOS {
386+
case "windows":
387+
// There's no file on disk, so don't bother checking
388+
// and failing.
389+
//
390+
// The Windows implementation of dnsReadConfig (called
391+
// below) ignores the name.
392+
default:
393+
var mtime time.Time
394+
if fi, err := os.Stat(name); err == nil {
395+
mtime = fi.ModTime()
396+
}
397+
if mtime.Equal(conf.dnsConfig.mtime) {
398+
return
399+
}
390400
}
391401

392402
dnsConf := dnsReadConfig(name)

src/net/dnsconfig.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package net
6+
7+
import (
8+
"os"
9+
"sync/atomic"
10+
"time"
11+
)
12+
13+
var (
14+
defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
15+
getHostname = os.Hostname // variable for testing
16+
)
17+
18+
type dnsConfig struct {
19+
servers []string // server addresses (in host:port form) to use
20+
search []string // rooted suffixes to append to local name
21+
ndots int // number of dots in name to trigger absolute lookup
22+
timeout time.Duration // wait before giving up on a query, including retries
23+
attempts int // lost packets before giving up on server
24+
rotate bool // round robin among servers
25+
unknownOpt bool // anything unknown was encountered
26+
lookup []string // OpenBSD top-level database "lookup" order
27+
err error // any error that occurs during open of resolv.conf
28+
mtime time.Time // time of resolv.conf modification
29+
soffset uint32 // used by serverOffset
30+
singleRequest bool // use sequential A and AAAA queries instead of parallel queries
31+
useTCP bool // force usage of TCP for DNS resolutions
32+
}
33+
34+
// serverOffset returns an offset that can be used to determine
35+
// indices of servers in c.servers when making queries.
36+
// When the rotate option is enabled, this offset increases.
37+
// Otherwise it is always 0.
38+
func (c *dnsConfig) serverOffset() uint32 {
39+
if c.rotate {
40+
return atomic.AddUint32(&c.soffset, 1) - 1 // return 0 to start
41+
}
42+
return 0
43+
}

src/net/dnsconfig_unix.go

+1-35
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,17 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build unix
5+
//go:build !js && !windows
66

77
// Read system DNS config from /etc/resolv.conf
88

99
package net
1010

1111
import (
1212
"internal/bytealg"
13-
"os"
14-
"sync/atomic"
1513
"time"
1614
)
1715

18-
var (
19-
defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
20-
getHostname = os.Hostname // variable for testing
21-
)
22-
23-
type dnsConfig struct {
24-
servers []string // server addresses (in host:port form) to use
25-
search []string // rooted suffixes to append to local name
26-
ndots int // number of dots in name to trigger absolute lookup
27-
timeout time.Duration // wait before giving up on a query, including retries
28-
attempts int // lost packets before giving up on server
29-
rotate bool // round robin among servers
30-
unknownOpt bool // anything unknown was encountered
31-
lookup []string // OpenBSD top-level database "lookup" order
32-
err error // any error that occurs during open of resolv.conf
33-
mtime time.Time // time of resolv.conf modification
34-
soffset uint32 // used by serverOffset
35-
singleRequest bool // use sequential A and AAAA queries instead of parallel queries
36-
useTCP bool // force usage of TCP for DNS resolutions
37-
}
38-
3916
// See resolv.conf(5) on a Linux machine.
4017
func dnsReadConfig(filename string) *dnsConfig {
4118
conf := &dnsConfig{
@@ -156,17 +133,6 @@ func dnsReadConfig(filename string) *dnsConfig {
156133
return conf
157134
}
158135

159-
// serverOffset returns an offset that can be used to determine
160-
// indices of servers in c.servers when making queries.
161-
// When the rotate option is enabled, this offset increases.
162-
// Otherwise it is always 0.
163-
func (c *dnsConfig) serverOffset() uint32 {
164-
if c.rotate {
165-
return atomic.AddUint32(&c.soffset, 1) - 1 // return 0 to start
166-
}
167-
return 0
168-
}
169-
170136
func dnsDefaultSearch() []string {
171137
hn, err := getHostname()
172138
if err != nil {

src/net/dnsconfig_windows.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package net
6+
7+
import (
8+
"syscall"
9+
"time"
10+
)
11+
12+
func dnsReadConfig(ignoredFilename string) (conf *dnsConfig) {
13+
conf = &dnsConfig{
14+
ndots: 1,
15+
timeout: 5 * time.Second,
16+
attempts: 2,
17+
}
18+
defer func() {
19+
if len(conf.servers) == 0 {
20+
conf.servers = defaultNS
21+
}
22+
}()
23+
aas, err := adapterAddresses()
24+
if err != nil {
25+
return
26+
}
27+
// TODO(bradfitz): this just collects all the DNS servers on all
28+
// the interfaces in some random order. It should order it by
29+
// default route, or only use the default route(s) instead.
30+
// In practice, however, it mostly works.
31+
for _, aa := range aas {
32+
for dns := aa.FirstDnsServerAddress; dns != nil; dns = dns.Next {
33+
sa, err := dns.Address.Sockaddr.Sockaddr()
34+
if err != nil {
35+
continue
36+
}
37+
var ip IP
38+
switch sa := sa.(type) {
39+
case *syscall.SockaddrInet4:
40+
ip = IPv4(sa.Addr[0], sa.Addr[1], sa.Addr[2], sa.Addr[3])
41+
case *syscall.SockaddrInet6:
42+
ip = make(IP, IPv6len)
43+
copy(ip, sa.Addr[:])
44+
if ip[0] == 0xfe && ip[1] == 0xc0 {
45+
// Ignore these fec0/10 ones. Windows seems to
46+
// populate them as defaults on its misc rando
47+
// interfaces.
48+
continue
49+
}
50+
default:
51+
// Unexpected type.
52+
continue
53+
}
54+
conf.servers = append(conf.servers, JoinHostPort(ip.String(), "53"))
55+
}
56+
}
57+
return conf
58+
}

0 commit comments

Comments
 (0)