Skip to content

Commit 85bfa33

Browse files
committed
net: fix case insensitivity lookup for local database such as /etc/hosts
The previous change for #12806 modified internal lookup tables and made LookupAddr return forcibly lowercased host names by accident. This change fixes the issue again without any behavioral change for LookupAddr and adds missing test cases for lookupStaticHost and lookupStaticAddr. Updates #12806. Fixes #13359. Change-Id: Ifff4741cd79eb8b320b1b0f8c5e02b3a167c9fa8 Reviewed-on: https://go-review.googlesource.com/17217 Run-TryBot: Mikio Hara <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 98abf29 commit 85bfa33

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

src/net/hosts.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ func parseLiteralIP(addr string) string {
2727
return ip.String() + "%" + zone
2828
}
2929

30-
// Simple cache.
30+
// hosts contains known host entries.
3131
var hosts struct {
3232
sync.Mutex
33+
34+
// Key for the list of literal IP addresses must be a host
35+
// name. It would be part of DNS labels, a FQDN or an absolute
36+
// FQDN.
37+
// For now the key is converted to lower case for convenience.
3338
byName map[string][]string
39+
40+
// Key for the list of host names must be a literal IP address
41+
// including IPv6 address with zone identifier.
42+
// We don't support old-classful IP address notation.
3443
byAddr map[string][]string
44+
3545
expire time.Time
3646
path string
3747
}
@@ -60,11 +70,12 @@ func readHosts() {
6070
continue
6171
}
6272
for i := 1; i < len(f); i++ {
73+
name := f[i]
6374
h := []byte(f[i])
6475
lowerASCIIBytes(h)
65-
lh := string(h)
66-
hs[lh] = append(hs[lh], addr)
67-
is[addr] = append(is[addr], lh)
76+
key := string(h)
77+
hs[key] = append(hs[key], addr)
78+
is[addr] = append(is[addr], name)
6879
}
6980
}
7081
// Update the data cache.

src/net/hosts_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package net
66

77
import (
88
"reflect"
9+
"strings"
910
"testing"
1011
)
1112

@@ -48,6 +49,13 @@ var lookupStaticHostTests = []struct {
4849
{"localhost.localdomain", []string{"fe80::3%lo0"}},
4950
},
5051
},
52+
{
53+
"testdata/case-hosts", // see golang.org/issue/12806
54+
[]staticHostEntry{
55+
{"PreserveMe", []string{"127.0.0.1", "::1"}},
56+
{"PreserveMe.local", []string{"127.0.0.1", "::1"}},
57+
},
58+
},
5159
}
5260

5361
func TestLookupStaticHost(t *testing.T) {
@@ -56,9 +64,12 @@ func TestLookupStaticHost(t *testing.T) {
5664
for _, tt := range lookupStaticHostTests {
5765
testHookHostsPath = tt.name
5866
for _, ent := range tt.ents {
59-
addrs := lookupStaticHost(ent.in)
60-
if !reflect.DeepEqual(addrs, ent.out) {
61-
t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, ent.in, addrs, ent.out)
67+
ins := []string{ent.in, strings.ToLower(ent.in), strings.ToUpper(ent.in)}
68+
for _, in := range ins {
69+
addrs := lookupStaticHost(in)
70+
if !reflect.DeepEqual(addrs, ent.out) {
71+
t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", tt.name, in, addrs, ent.out)
72+
}
6273
}
6374
}
6475
}
@@ -74,7 +85,6 @@ var lookupStaticAddrTests = []struct {
7485
{"255.255.255.255", []string{"broadcasthost"}},
7586
{"127.0.0.2", []string{"odin"}},
7687
{"127.0.0.3", []string{"odin"}},
77-
{"127.0.0.4", []string{"bor"}},
7888
{"::2", []string{"odin"}},
7989
{"127.1.1.1", []string{"thor"}},
8090
{"127.1.1.2", []string{"ullr", "ullrhost"}},
@@ -104,6 +114,13 @@ var lookupStaticAddrTests = []struct {
104114
{"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}},
105115
},
106116
},
117+
{
118+
"testdata/case-hosts", // see golang.org/issue/12806
119+
[]staticHostEntry{
120+
{"127.0.0.1", []string{"PreserveMe", "PreserveMe.local"}},
121+
{"::1", []string{"PreserveMe", "PreserveMe.local"}},
122+
},
123+
},
107124
}
108125

109126
func TestLookupStaticAddr(t *testing.T) {

src/net/testdata/case-hosts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
127.0.0.1 PreserveMe PreserveMe.local
2+
::1 PreserveMe PreserveMe.local

src/net/testdata/hosts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
255.255.255.255 broadcasthost
22
127.0.0.2 odin
33
127.0.0.3 odin # inline comment
4-
# case insensitivity
5-
127.0.0.4 Bor
64
::2 odin
75
127.1.1.1 thor
86
# aliases

0 commit comments

Comments
 (0)