1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Read static host/IP entries from /etc/hosts.
14 const cacheMaxAge
= int64(300) // 5 minutes.
16 // hostsPath points to the file with static IP/address entries.
17 var hostsPath
= "/etc/hosts"
22 data
map[string][]string
28 now
, _
, _
:= os
.Time()
30 if len(hosts
.data
) == 0 || hosts
.time
+cacheMaxAge
<= now || hosts
.path
!= hp
{
31 hs
:= make(map[string][]string)
33 if file
, _
= open(hp
); file
== nil {
36 for line
, ok
:= file
.readLine(); ok
; line
, ok
= file
.readLine() {
37 if i
:= byteIndex(line
, '#'); i
>= 0 {
42 if len(f
) < 2 ||
ParseIP(f
[0]) == nil {
45 for i
:= 1; i
< len(f
); i
++ {
47 hs
[h
] = append(hs
[h
], f
[0])
50 // Update the data cache.
51 hosts
.time
, _
, _
= os
.Time()
58 // lookupStaticHosts looks up the addresses for the given host from /etc/hosts.
59 func lookupStaticHost(host
string) []string {
63 if len(hosts
.data
) != 0 {
64 if ips
, ok
:= hosts
.data
[host
]; ok
{