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 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
}
34 for line
, ok
:= file
.readLine(); ok
; line
, ok
= file
.readLine() {
40 case "nameserver": // add one name server
41 if len(f
) > 1 && len(conf
.servers
) < 3 { // small, but the standard limit
42 // One more check: make sure server name is
43 // just an IP address. Otherwise we need DNS
45 if parseIPv4(f
[1]) != nil {
46 conf
.servers
= append(conf
.servers
, f
[1])
47 } else if ip
, _
:= parseIPv6(f
[1], true); ip
!= nil {
48 conf
.servers
= append(conf
.servers
, f
[1])
52 case "domain": // set search path to just this domain
54 conf
.search
= []string{f
[1]}
57 case "search": // set search path to given servers
58 conf
.search
= make([]string, len(f
)-1)
59 for i
:= 0; i
< len(conf
.search
); i
++ {
60 conf
.search
[i
] = f
[i
+1]
63 case "options": // magic options
64 for i
:= 1; i
< len(f
); i
++ {
67 case hasPrefix(s
, "ndots:"):
73 case hasPrefix(s
, "timeout:"):
79 case hasPrefix(s
, "attempts:"):
94 func hasPrefix(s
, prefix
string) bool {
95 return len(s
) >= len(prefix
) && s
[:len(prefix
)] == prefix