]> Cypherpunks repositories - gostls13.git/commitdiff
net: don't treat unknown sources as dns when there is a dns source
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Thu, 11 May 2023 17:27:02 +0000 (17:27 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 11 May 2023 23:49:19 +0000 (23:49 +0000)
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>

src/net/conf.go
src/net/conf_test.go

index cec996f7f0cd2f58239b9fa220ad2d7025956ab5..1db166c9e3c2dfbba1bde6b4c0490abe514e66c4 100644 (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,
index d2cdac80837fa85534ebd85c872e1103b17e2fab..0f324b245aef12269d3160a97879317ac9803c5f 100644 (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