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
77package 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
0 commit comments