2010-01-15 14:43:14 -07: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.
|
|
|
|
|
|
|
|
// Read static host/IP entries from /etc/hosts.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2011-11-11 12:40:41 -07:00
|
|
|
"time"
|
2010-01-15 14:43:14 -07:00
|
|
|
)
|
|
|
|
|
2011-11-30 10:01:46 -07:00
|
|
|
const cacheMaxAge = 5 * time.Minute
|
2010-01-15 14:43:14 -07:00
|
|
|
|
|
|
|
// hostsPath points to the file with static IP/address entries.
|
|
|
|
var hostsPath = "/etc/hosts"
|
|
|
|
|
|
|
|
// Simple cache.
|
|
|
|
var hosts struct {
|
|
|
|
sync.Mutex
|
2011-01-19 13:11:03 -07:00
|
|
|
byName map[string][]string
|
|
|
|
byAddr map[string][]string
|
2011-11-30 10:01:46 -07:00
|
|
|
expire time.Time
|
2011-01-19 13:11:03 -07:00
|
|
|
path string
|
2010-01-15 14:43:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func readHosts() {
|
2011-11-30 10:01:46 -07:00
|
|
|
now := time.Now()
|
2010-01-15 14:43:14 -07:00
|
|
|
hp := hostsPath
|
2011-11-30 10:01:46 -07:00
|
|
|
if len(hosts.byName) == 0 || now.After(hosts.expire) || hosts.path != hp {
|
2010-01-15 14:43:14 -07:00
|
|
|
hs := make(map[string][]string)
|
2011-01-19 13:11:03 -07:00
|
|
|
is := make(map[string][]string)
|
2010-01-15 14:43:14 -07:00
|
|
|
var file *file
|
2010-01-26 17:18:29 -07:00
|
|
|
if file, _ = open(hp); file == nil {
|
|
|
|
return
|
|
|
|
}
|
2010-01-15 14:43:14 -07:00
|
|
|
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)
|
|
|
|
if len(f) < 2 || ParseIP(f[0]) == nil {
|
|
|
|
continue
|
|
|
|
}
|
2010-01-25 15:57:04 -07:00
|
|
|
for i := 1; i < len(f); i++ {
|
|
|
|
h := f[i]
|
2010-10-27 20:47:23 -06:00
|
|
|
hs[h] = append(hs[h], f[0])
|
2011-01-19 13:11:03 -07:00
|
|
|
is[f[0]] = append(is[f[0]], h)
|
2010-01-25 15:57:04 -07:00
|
|
|
}
|
2010-01-15 14:43:14 -07:00
|
|
|
}
|
|
|
|
// Update the data cache.
|
2014-06-11 21:33:44 -06:00
|
|
|
hosts.expire = now.Add(cacheMaxAge)
|
2010-01-15 14:43:14 -07:00
|
|
|
hosts.path = hp
|
2011-01-19 13:11:03 -07:00
|
|
|
hosts.byName = hs
|
|
|
|
hosts.byAddr = is
|
2010-01-15 14:43:14 -07:00
|
|
|
file.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-04 19:35:16 -06:00
|
|
|
// lookupStaticHost looks up the addresses for the given host from /etc/hosts.
|
2010-01-15 14:43:14 -07:00
|
|
|
func lookupStaticHost(host string) []string {
|
|
|
|
hosts.Lock()
|
|
|
|
defer hosts.Unlock()
|
|
|
|
readHosts()
|
2011-01-19 13:11:03 -07:00
|
|
|
if len(hosts.byName) != 0 {
|
|
|
|
if ips, ok := hosts.byName[host]; ok {
|
2010-01-15 14:43:14 -07:00
|
|
|
return ips
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2011-01-19 13:11:03 -07:00
|
|
|
|
2011-04-04 19:35:16 -06:00
|
|
|
// lookupStaticAddr looks up the hosts for the given address from /etc/hosts.
|
2011-01-19 13:11:03 -07:00
|
|
|
func lookupStaticAddr(addr string) []string {
|
|
|
|
hosts.Lock()
|
|
|
|
defer hosts.Unlock()
|
|
|
|
readHosts()
|
|
|
|
if len(hosts.byAddr) != 0 {
|
|
|
|
if hosts, ok := hosts.byAddr[addr]; ok {
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|