From: Mateusz Poliwczak Date: Thu, 11 May 2023 17:27:02 +0000 (+0000) Subject: net: don't treat unknown sources as dns when there is a dns source X-Git-Tag: go1.21rc1~568 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e2500be54947186d82c7f4bd93468fdff8a23837;p=gostls13.git 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 Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Reviewed-by: Cherry Mui Run-TryBot: Mateusz Poliwczak TryBot-Result: Gopher Robot --- 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