10 // ClientConfig wraps the contents of the /etc/resolv.conf file.
11 type ClientConfig struct {
12 Servers []string // servers to use
13 Search []string // suffixes to append to local name
14 Port string // what port to use
15 Ndots int // number of dots in name to trigger absolute lookup
16 Timeout int // seconds before giving up on packet
17 Attempts int // lost packets before giving up on server, not used in the package dns
20 // ClientConfigFromFile parses a resolv.conf(5) like file and returns
22 func ClientConfigFromFile(resolvconf string) (*ClientConfig, error) {
23 file, err := os.Open(resolvconf)
28 c := new(ClientConfig)
29 scanner := bufio.NewScanner(file)
30 c.Servers = make([]string, 0)
31 c.Search = make([]string, 0)
38 if err := scanner.Err(); err != nil {
41 line := scanner.Text()
42 f := strings.Fields(line)
47 case "nameserver": // add one name server
49 // One more check: make sure server name is
50 // just an IP address. Otherwise we need DNS
53 c.Servers = append(c.Servers, name)
56 case "domain": // set search path to just this domain
58 c.Search = make([]string, 1)
61 c.Search = make([]string, 0)
64 case "search": // set search path to given servers
65 c.Search = make([]string, len(f)-1)
66 for i := 0; i < len(c.Search); i++ {
70 case "options": // magic options
71 for i := 1; i < len(f); i++ {
74 case len(s) >= 6 && s[:6] == "ndots:":
75 n, _ := strconv.Atoi(s[6:])
80 case len(s) >= 8 && s[:8] == "timeout:":
81 n, _ := strconv.Atoi(s[8:])
86 case len(s) >= 8 && s[:9] == "attempts:":
87 n, _ := strconv.Atoi(s[9:])