Commit d96a3a2d authored by Burcu Dogan's avatar Burcu Dogan

net: make /etc/hosts lookups case-insensitive

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent f1b25c4c
...@@ -60,9 +60,11 @@ func readHosts() { ...@@ -60,9 +60,11 @@ func readHosts() {
continue continue
} }
for i := 1; i < len(f); i++ { for i := 1; i < len(f); i++ {
h := f[i] h := []byte(f[i])
hs[h] = append(hs[h], addr) lowerASCIIBytes(h)
is[addr] = append(is[addr], h) lh := string(h)
hs[lh] = append(hs[lh], addr)
is[addr] = append(is[addr], lh)
} }
} }
// Update the data cache. // Update the data cache.
...@@ -80,7 +82,11 @@ func lookupStaticHost(host string) []string { ...@@ -80,7 +82,11 @@ func lookupStaticHost(host string) []string {
defer hosts.Unlock() defer hosts.Unlock()
readHosts() readHosts()
if len(hosts.byName) != 0 { 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 return ips
} }
} }
......
...@@ -74,6 +74,7 @@ var lookupStaticAddrTests = []struct { ...@@ -74,6 +74,7 @@ var lookupStaticAddrTests = []struct {
{"255.255.255.255", []string{"broadcasthost"}}, {"255.255.255.255", []string{"broadcasthost"}},
{"127.0.0.2", []string{"odin"}}, {"127.0.0.2", []string{"odin"}},
{"127.0.0.3", []string{"odin"}}, {"127.0.0.3", []string{"odin"}},
{"127.0.0.4", []string{"bor"}},
{"::2", []string{"odin"}}, {"::2", []string{"odin"}},
{"127.1.1.1", []string{"thor"}}, {"127.1.1.1", []string{"thor"}},
{"127.1.1.2", []string{"ullr", "ullrhost"}}, {"127.1.1.2", []string{"ullr", "ullrhost"}},
......
255.255.255.255 broadcasthost 255.255.255.255 broadcasthost
127.0.0.2 odin 127.0.0.2 odin
127.0.0.3 odin # inline comment 127.0.0.3 odin # inline comment
# case insensitivity
127.0.0.4 Bor
::2 odin ::2 odin
127.1.1.1 thor 127.1.1.1 thor
# aliases # aliases
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment