...
Source file
src/net/dnsconfig_unix.go
Documentation: net
1
2
3
4
5
6
7
8
9 package net
10
11 import (
12 "internal/bytealg"
13 "net/netip"
14 "time"
15 )
16
17
18 func dnsReadConfig(filename string) *dnsConfig {
19 conf := &dnsConfig{
20 ndots: 1,
21 timeout: 5 * time.Second,
22 attempts: 2,
23 }
24 file, err := open(filename)
25 if err != nil {
26 conf.servers = defaultNS
27 conf.search = dnsDefaultSearch()
28 conf.err = err
29 return conf
30 }
31 defer file.close()
32 if fi, err := file.file.Stat(); err == nil {
33 conf.mtime = fi.ModTime()
34 } else {
35 conf.servers = defaultNS
36 conf.search = dnsDefaultSearch()
37 conf.err = err
38 return conf
39 }
40 for line, ok := file.readLine(); ok; line, ok = file.readLine() {
41 if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
42
43 continue
44 }
45 f := getFields(line)
46 if len(f) < 1 {
47 continue
48 }
49 switch f[0] {
50 case "nameserver":
51 if len(f) > 1 && len(conf.servers) < 3 {
52
53
54
55 if _, err := netip.ParseAddr(f[1]); err == nil {
56 conf.servers = append(conf.servers, JoinHostPort(f[1], "53"))
57 }
58 }
59
60 case "domain":
61 if len(f) > 1 {
62 conf.search = []string{ensureRooted(f[1])}
63 }
64
65 case "search":
66 conf.search = make([]string, 0, len(f)-1)
67 for i := 1; i < len(f); i++ {
68 name := ensureRooted(f[i])
69 if name == "." {
70 continue
71 }
72 conf.search = append(conf.search, name)
73 }
74
75 case "options":
76 for _, s := range f[1:] {
77 switch {
78 case hasPrefix(s, "ndots:"):
79 n, _, _ := dtoi(s[6:])
80 if n < 0 {
81 n = 0
82 } else if n > 15 {
83 n = 15
84 }
85 conf.ndots = n
86 case hasPrefix(s, "timeout:"):
87 n, _, _ := dtoi(s[8:])
88 if n < 1 {
89 n = 1
90 }
91 conf.timeout = time.Duration(n) * time.Second
92 case hasPrefix(s, "attempts:"):
93 n, _, _ := dtoi(s[9:])
94 if n < 1 {
95 n = 1
96 }
97 conf.attempts = n
98 case s == "rotate":
99 conf.rotate = true
100 case s == "single-request" || s == "single-request-reopen":
101
102
103
104
105
106 conf.singleRequest = true
107 case s == "use-vc" || s == "usevc" || s == "tcp":
108
109
110
111
112
113
114 conf.useTCP = true
115 case s == "trust-ad":
116 conf.trustAD = true
117 case s == "edns0":
118
119
120 case s == "no-reload":
121 conf.noReload = true
122 default:
123 conf.unknownOpt = true
124 }
125 }
126
127 case "lookup":
128
129
130
131 conf.lookup = f[1:]
132
133 default:
134 conf.unknownOpt = true
135 }
136 }
137 if len(conf.servers) == 0 {
138 conf.servers = defaultNS
139 }
140 if len(conf.search) == 0 {
141 conf.search = dnsDefaultSearch()
142 }
143 return conf
144 }
145
146 func dnsDefaultSearch() []string {
147 hn, err := getHostname()
148 if err != nil {
149
150 return nil
151 }
152 if i := bytealg.IndexByteString(hn, '.'); i >= 0 && i < len(hn)-1 {
153 return []string{ensureRooted(hn[i+1:])}
154 }
155 return nil
156 }
157
158 func hasPrefix(s, prefix string) bool {
159 return len(s) >= len(prefix) && s[:len(prefix)] == prefix
160 }
161
162 func ensureRooted(s string) string {
163 if len(s) > 0 && s[len(s)-1] == '.' {
164 return s
165 }
166 return s + "."
167 }
168
View as plain text