aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2023-05-11 17:27:02 +0000
committerGopher Robot <gobot@golang.org>2023-05-11 23:49:19 +0000
commite2500be54947186d82c7f4bd93468fdff8a23837 (patch)
treeb08c980d15fea8622afa3577e0213ba39c074cf7 /src
parent7db6d8a29d1f9bc4265ff4eb77547c7aa5f8c87e (diff)
downloadgo-e2500be54947186d82c7f4bd93468fdff8a23837.tar.xz
net: don't treat unknown sources as dns when there is a dns source
Change-Id: I3a6c3a804604b1e74a1ea6b66ab2c932a0ac973a GitHub-Last-Rev: ea5403549a51a29a2799674d74425b480253d2f1 GitHub-Pull-Request: golang/go#60025 Reviewed-on: https://go-review.googlesource.com/c/go/+/493236 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Mateusz Poliwczak <mpoliwczak34@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/net/conf.go34
-rw-r--r--src/net/conf_test.go20
2 files changed, 44 insertions, 10 deletions
diff --git a/src/net/conf.go b/src/net/conf.go
index cec996f7f0..1db166c9e3 100644
--- a/src/net/conf.go
+++ b/src/net/conf.go
@@ -360,9 +360,12 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
return fallbackOrder, dnsConf
}
- var filesSource, dnsSource, unknownSource bool
+ var hasDNSSource bool
+ var hasDNSSourceChecked bool
+
+ var filesSource, dnsSource bool
var first string
- for _, src := range srcs {
+ for i, src := range srcs {
if src.source == "files" || src.source == "dns" {
if canUseCgo && !src.standardCriteria() {
// non-standard; let libc deal with it.
@@ -371,6 +374,8 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
if src.source == "files" {
filesSource = true
} else {
+ hasDNSSource = true
+ hasDNSSourceChecked = true
dnsSource = true
}
if first == "" {
@@ -424,16 +429,25 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
}
}
- unknownSource = true
- if first == "" {
- first = src.source
+ if !hasDNSSourceChecked {
+ hasDNSSourceChecked = true
+ for _, v := range srcs[i+1:] {
+ if v.source == "dns" {
+ hasDNSSource = true
+ break
+ }
+ }
}
- }
- // If we saw a source we don't recognize, which can only
- // happen if we can't use the cgo resolver, treat it as DNS.
- if unknownSource {
- dnsSource = true
+ // If we saw a source we don't recognize, which can only
+ // happen if we can't use the cgo resolver, treat it as DNS,
+ // but only when there is no dns in all other sources.
+ if !hasDNSSource {
+ dnsSource = true
+ if first == "" {
+ first = "dns"
+ }
+ }
}
// Cases where Go can handle it without cgo and C thread overhead,
diff --git a/src/net/conf_test.go b/src/net/conf_test.go
index d2cdac8083..0f324b245a 100644
--- a/src/net/conf_test.go
+++ b/src/net/conf_test.go
@@ -364,6 +364,26 @@ func TestConfHostLookupOrder(t *testing.T) {
{"x.com", "myhostname", hostLookupDNSFiles},
},
},
+ {
+ name: "dns-among-unknown-sources",
+ resolver: &Resolver{PreferGo: true},
+ c: &conf{},
+ resolv: defaultResolvConf,
+ nss: nssStr(t, "hosts: mymachines files dns"),
+ hostTests: []nssHostTest{
+ {"x.com", "myhostname", hostLookupFilesDNS},
+ },
+ },
+ {
+ name: "dns-among-unknown-sources-2",
+ resolver: &Resolver{PreferGo: true},
+ c: &conf{},
+ resolv: defaultResolvConf,
+ nss: nssStr(t, "hosts: dns mymachines files"),
+ hostTests: []nssHostTest{
+ {"x.com", "myhostname", hostLookupDNSFiles},
+ },
+ },
}
origGetHostname := getHostname