]> Cypherpunks repositories - gostls13.git/commitdiff
net: make /etc/hosts lookups case-insensitive
authorBurcu Dogan <jbd@google.com>
Thu, 1 Oct 2015 19:01:50 +0000 (15:01 -0400)
committerBurcu Dogan <jbd@google.com>
Thu, 1 Oct 2015 20:52:54 +0000 (20:52 +0000)
The native Go host resolver was behaving differently than libc
and the entries in the /etc/hosts were handled in a case sensitive
way. In order to be compatible with libc's resolver, /etc/hosts
lookups must be case-insensitive.

Fixes #12806.

Change-Id: I3c14001abffadf7458fd1a027c91e6438a87f285
Reviewed-on: https://go-review.googlesource.com/15321
Run-TryBot: Burcu Dogan <jbd@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/hosts.go
src/net/hosts_test.go
src/net/testdata/hosts

index 27958c7cc50b655fe2015006f70c149ddc834ac0..aba2cea787454a6f66b596da054a094666e19621 100644 (file)
@@ -60,9 +60,11 @@ func readHosts() {
                                continue
                        }
                        for i := 1; i < len(f); i++ {
-                               h := f[i]
-                               hs[h] = append(hs[h], addr)
-                               is[addr] = append(is[addr], h)
+                               h := []byte(f[i])
+                               lowerASCIIBytes(h)
+                               lh := string(h)
+                               hs[lh] = append(hs[lh], addr)
+                               is[addr] = append(is[addr], lh)
                        }
                }
                // Update the data cache.
@@ -80,7 +82,11 @@ func lookupStaticHost(host string) []string {
        defer hosts.Unlock()
        readHosts()
        if len(hosts.byName) != 0 {
-               if ips, ok := hosts.byName[host]; ok {
+               // TODO(jbd,bradfitz): avoid this alloc if host is already all lowercase?
+               // or linear scan the byName map if it's small enough?
+               lowerHost := []byte(host)
+               lowerASCIIBytes(lowerHost)
+               if ips, ok := hosts.byName[string(lowerHost)]; ok {
                        return ips
                }
        }
index aca64c38b05c25812856ed05545a2d46bfd18d98..99569cd01629a480011600c4126652852c87d667 100644 (file)
@@ -74,6 +74,7 @@ var lookupStaticAddrTests = []struct {
                        {"255.255.255.255", []string{"broadcasthost"}},
                        {"127.0.0.2", []string{"odin"}},
                        {"127.0.0.3", []string{"odin"}},
+                       {"127.0.0.4", []string{"bor"}},
                        {"::2", []string{"odin"}},
                        {"127.1.1.1", []string{"thor"}},
                        {"127.1.1.2", []string{"ullr", "ullrhost"}},
index b601763898b7e27ded621cc207effa4a403cecc0..4b8abb431cbcd04117eb231fe82b501cc6c9bf10 100644 (file)
@@ -1,6 +1,8 @@
 255.255.255.255        broadcasthost
 127.0.0.2      odin
 127.0.0.3      odin  # inline comment 
+# case insensitivity
+127.0.0.4      Bor
 ::2             odin
 127.1.1.1      thor
 # aliases