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 netbsd openbsd
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 // We assume it's in resolv.conf anyway.
24 func dnsReadConfig() (*dnsConfig
, error
) {
25 file
, err
:= open("/etc/resolv.conf")
27 return nil, &DNSConfigError
{err
}
29 conf
:= new(dnsConfig
)
30 conf
.servers
= make([]string, 3)[0:0] // small, but the standard limit
31 conf
.search
= make([]string, 0)
36 for line
, ok
:= file
.readLine(); ok
; line
, ok
= file
.readLine() {
42 case "nameserver": // add one name server
45 if len(f
) > 1 && n
< cap(a
) {
46 // One more check: make sure server name is
47 // just an IP address. Otherwise we need DNS
50 switch len(ParseIP(name
)) {
52 name
= "[" + name
+ "]"
61 case "domain": // set search path to just this domain
63 conf
.search
= make([]string, 1)
66 conf
.search
= make([]string, 0)
69 case "search": // set search path to given servers
70 conf
.search
= make([]string, len(f
)-1)
71 for i
:= 0; i
< len(conf
.search
); i
++ {
72 conf
.search
[i
] = f
[i
+1]
75 case "options": // magic options
76 for i
:= 1; i
< len(f
); i
++ {
79 case len(s
) >= 6 && s
[0:6] == "ndots:":
85 case len(s
) >= 8 && s
[0:8] == "timeout:":
91 case len(s
) >= 8 && s
[0:9] == "attempts:":