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.
7 // Read system DNS config from /etc/resolv.conf
17 // See resolv.conf(5) on a Linux machine.
18 func dnsReadConfig(filename string) *dnsConfig {
21 timeout: 5 * time.Second,
24 file, err := open(filename)
26 conf.servers = defaultNS
27 conf.search = dnsDefaultSearch()
32 if fi, err := file.file.Stat(); err == nil {
33 conf.mtime = fi.ModTime()
35 conf.servers = defaultNS
36 conf.search = dnsDefaultSearch()
40 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
41 if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
50 case "nameserver": // add one name server
51 if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
52 // One more check: make sure server name is
53 // just an IP address. Otherwise we need DNS
55 if _, err := netip.ParseAddr(f[1]); err == nil {
56 conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
60 case "domain": // set search path to just this domain
62 conf.search = []string{ensureRooted(f[1])}
65 case "search": // set search path to given servers
66 conf.search = make([]string, 0, len(f)-1)
67 for i := 1; i < len(f); i++ {
68 name := ensureRooted(f[i])
72 conf.search = append(conf.search, name)
75 case "options": // magic options
76 for _, s := range f[1:] {
78 case hasPrefix(s, "ndots:"):
79 n, _, _ := dtoi(s[6:])
86 case hasPrefix(s, "timeout:"):
87 n, _, _ := dtoi(s[8:])
91 conf.timeout = time.Duration(n) * time.Second
92 case hasPrefix(s, "attempts:"):
93 n, _, _ := dtoi(s[9:])
100 case s == "single-request" || s == "single-request-reopen":
102 // http://man7.org/linux/man-pages/man5/resolv.conf.5.html
103 // "By default, glibc performs IPv4 and IPv6 lookups in parallel [...]
104 // This option disables the behavior and makes glibc
105 // perform the IPv6 and IPv4 requests sequentially."
106 conf.singleRequest = true
107 case s == "use-vc" || s == "usevc" || s == "tcp":
108 // Linux (use-vc), FreeBSD (usevc) and OpenBSD (tcp) option:
109 // http://man7.org/linux/man-pages/man5/resolv.conf.5.html
110 // "Sets RES_USEVC in _res.options.
111 // This option forces the use of TCP for DNS resolutions."
112 // https://www.freebsd.org/cgi/man.cgi?query=resolv.conf&sektion=5&manpath=freebsd-release-ports
113 // https://man.openbsd.org/resolv.conf.5
115 case s == "trust-ad":
118 // We use EDNS by default.
119 // Ignore this option.
120 case s == "no-reload":
123 conf.unknownOpt = true
129 // https://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/resolv.conf.5
130 // "the legal space-separated values are: bind, file, yp"
134 conf.unknownOpt = true
137 if len(conf.servers) == 0 {
138 conf.servers = defaultNS
140 if len(conf.search) == 0 {
141 conf.search = dnsDefaultSearch()
146 func dnsDefaultSearch() []string {
147 hn, err := getHostname()
152 if i := bytealg.IndexByteString(hn, '.'); i >= 0 && i < len(hn)-1 {
153 return []string{ensureRooted(hn[i+1:])}
158 func hasPrefix(s, prefix string) bool {
159 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
162 func ensureRooted(s string) string {
163 if len(s) > 0 && s[len(s)-1] == '.' {