1
0
mirror of https://github.com/golang/go synced 2024-11-26 20:01:19 -07:00

net: don't treat unknown sources as dns when there is a dns source

Change-Id: I3a6c3a804604b1e74a1ea6b66ab2c932a0ac973a
GitHub-Last-Rev: ea5403549a
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>
This commit is contained in:
Mateusz Poliwczak 2023-05-11 17:27:02 +00:00 committed by Gopher Robot
parent 7db6d8a29d
commit e2500be549
2 changed files with 44 additions and 10 deletions

View File

@ -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,

View File

@ -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