2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2011-12-13 00:40:51 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
const cacheMaxAge = 5 * time.Minute
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func parseLiteralIP(addr string) string {
|
|
|
|
var ip IP
|
|
|
|
var zone string
|
|
|
|
ip = parseIPv4(addr)
|
|
|
|
if ip == nil {
|
|
|
|
ip, zone = parseIPv6(addr, true)
|
|
|
|
}
|
|
|
|
if ip == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if zone == "" {
|
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
return ip.String() + "%" + zone
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Simple cache.
|
|
|
|
var hosts struct {
|
|
|
|
sync.Mutex
|
2011-01-21 19:19:03 +01:00
|
|
|
byName map[string][]string
|
|
|
|
byAddr map[string][]string
|
2011-12-13 20:16:27 +01:00
|
|
|
expire time.Time
|
2011-01-21 19:19:03 +01:00
|
|
|
path string
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func readHosts() {
|
2011-12-13 20:16:27 +01:00
|
|
|
now := time.Now()
|
2015-10-31 01:59:47 +01:00
|
|
|
hp := testHookHostsPath
|
2011-12-13 20:16:27 +01:00
|
|
|
if len(hosts.byName) == 0 || now.After(hosts.expire) || hosts.path != hp {
|
2010-12-03 05:34:57 +01:00
|
|
|
hs := make(map[string][]string)
|
2011-01-21 19:19:03 +01:00
|
|
|
is := make(map[string][]string)
|
2010-12-03 05:34:57 +01:00
|
|
|
var file *file
|
|
|
|
if file, _ = open(hp); file == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
|
|
|
|
if i := byteIndex(line, '#'); i >= 0 {
|
|
|
|
// Discard comments.
|
|
|
|
line = line[0:i]
|
|
|
|
}
|
|
|
|
f := getFields(line)
|
2015-10-31 01:59:47 +01:00
|
|
|
if len(f) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := parseLiteralIP(f[0])
|
|
|
|
if addr == "" {
|
2010-12-03 05:34:57 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := 1; i < len(f); i++ {
|
|
|
|
h := f[i]
|
2015-10-31 01:59:47 +01:00
|
|
|
hs[h] = append(hs[h], addr)
|
|
|
|
is[addr] = append(is[addr], h)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the data cache.
|
2015-01-15 01:27:56 +01:00
|
|
|
hosts.expire = now.Add(cacheMaxAge)
|
2010-12-03 05:34:57 +01:00
|
|
|
hosts.path = hp
|
2011-01-21 19:19:03 +01:00
|
|
|
hosts.byName = hs
|
|
|
|
hosts.byAddr = is
|
2010-12-03 05:34:57 +01:00
|
|
|
file.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
// lookupStaticHost looks up the addresses for the given host from /etc/hosts.
|
2010-12-03 05:34:57 +01:00
|
|
|
func lookupStaticHost(host string) []string {
|
|
|
|
hosts.Lock()
|
|
|
|
defer hosts.Unlock()
|
|
|
|
readHosts()
|
2011-01-21 19:19:03 +01:00
|
|
|
if len(hosts.byName) != 0 {
|
|
|
|
if ips, ok := hosts.byName[host]; ok {
|
2010-12-03 05:34:57 +01:00
|
|
|
return ips
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
// lookupStaticAddr looks up the hosts for the given address from /etc/hosts.
|
2011-01-21 19:19:03 +01:00
|
|
|
func lookupStaticAddr(addr string) []string {
|
|
|
|
hosts.Lock()
|
|
|
|
defer hosts.Unlock()
|
|
|
|
readHosts()
|
2015-10-31 01:59:47 +01:00
|
|
|
addr = parseLiteralIP(addr)
|
|
|
|
if addr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2011-01-21 19:19:03 +01:00
|
|
|
if len(hosts.byAddr) != 0 {
|
|
|
|
if hosts, ok := hosts.byAddr[addr]; ok {
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|