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 // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
7 // Read system DNS config from /etc/resolv.conf
11 type dnsConfig
struct {
12 servers
[]string // servers to use
13 search
[]string // suffixes to append to local name
14 ndots
int // number of dots in name to trigger absolute lookup
15 timeout
int // seconds before giving up on packet
16 attempts
int // lost packets before giving up on server
17 rotate
bool // round robin among servers
20 // See resolv.conf(5) on a Linux machine.
21 // TODO(rsc): Supposed to call uname() and chop the beginning
22 // of the host name to get the default search domain.
23 func dnsReadConfig(filename
string) (*dnsConfig
, error
) {
24 file
, err
:= open(filename
)
26 return nil, &DNSConfigError
{err
}
28 conf
:= new(dnsConfig
)
29 conf
.servers
= make([]string, 0, 3) // small, but the standard limit
30 conf
.search
= make([]string, 0)
35 for line
, ok
:= file
.readLine(); ok
; line
, ok
= file
.readLine() {
41 case "nameserver": // add one name server
44 if len(f
) > 1 && n
< cap(a
) {
45 // One more check: make sure server name is
46 // just an IP address. Otherwise we need DNS
49 switch len(ParseIP(name
)) {
51 name
= "[" + name
+ "]"
60 case "domain": // set search path to just this domain
62 conf
.search
= make([]string, 1)
65 conf
.search
= make([]string, 0)
68 case "search": // set search path to given servers
69 conf
.search
= make([]string, len(f
)-1)
70 for i
:= 0; i
< len(conf
.search
); i
++ {
71 conf
.search
[i
] = f
[i
+1]
74 case "options": // magic options
75 for i
:= 1; i
< len(f
); i
++ {
78 case hasPrefix(s
, "ndots:"):
84 case hasPrefix(s
, "timeout:"):
90 case hasPrefix(s
, "attempts:"):
107 func hasPrefix(s
, prefix
string) bool {
108 return len(s
) >= len(prefix
) && s
[:len(prefix
)] == prefix