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 freebsd linux 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 var dnsconfigError error
22 type DNSConfigError struct {
26 func (e *DNSConfigError) Error() string {
27 return "error reading DNS config: " + e.Err.Error()
30 func (e *DNSConfigError) Timeout() bool { return false }
31 func (e *DNSConfigError) Temporary() bool { return false }
33 // See resolv.conf(5) on a Linux machine.
34 // TODO(rsc): Supposed to call uname() and chop the beginning
35 // of the host name to get the default search domain.
36 // We assume it's in resolv.conf anyway.
37 func dnsReadConfig() (*dnsConfig, error) {
38 file, err := open("/etc/resolv.conf")
40 return nil, &DNSConfigError{err}
42 conf := new(dnsConfig)
43 conf.servers = make([]string, 3)[0:0] // small, but the standard limit
44 conf.search = make([]string, 0)
49 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
55 case "nameserver": // add one name server
58 if len(f) > 1 && n < cap(a) {
59 // One more check: make sure server name is
60 // just an IP address. Otherwise we need DNS
63 switch len(ParseIP(name)) {
65 name = "[" + name + "]"
74 case "domain": // set search path to just this domain
76 conf.search = make([]string, 1)
79 conf.search = make([]string, 0)
82 case "search": // set search path to given servers
83 conf.search = make([]string, len(f)-1)
84 for i := 0; i < len(conf.search); i++ {
85 conf.search[i] = f[i+1]
88 case "options": // magic options
89 for i := 1; i < len(f); i++ {
92 case len(s) >= 6 && s[0:6] == "ndots:":
98 case len(s) >= 8 && s[0:8] == "timeout:":
104 case len(s) >= 8 && s[0:9] == "attempts:":
105 n, _, _ := dtoi(s, 9)