Source file
src/net/dnsclient.go
Documentation: net
1
2
3
4
5 package net
6
7 import (
8 "math/rand"
9 "sort"
10 )
11
12
13
14
15 func reverseaddr(addr string) (arpa string, err error) {
16 ip := ParseIP(addr)
17 if ip == nil {
18 return "", &DNSError{Err: "unrecognized address", Name: addr}
19 }
20 if ip.To4() != nil {
21 return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
22 }
23
24 buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
25
26 for i := len(ip) - 1; i >= 0; i-- {
27 v := ip[i]
28 buf = append(buf, hexDigit[v&0xF])
29 buf = append(buf, '.')
30 buf = append(buf, hexDigit[v>>4])
31 buf = append(buf, '.')
32 }
33
34 buf = append(buf, "ip6.arpa."...)
35 return string(buf), nil
36 }
37
38
39
40 func answer(name, server string, dns *dnsMsg, qtype uint16) (cname string, addrs []dnsRR, err error) {
41 addrs = make([]dnsRR, 0, len(dns.answer))
42
43 if dns.rcode == dnsRcodeNameError {
44 return "", nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, Server: server}
45 }
46 if dns.rcode != dnsRcodeSuccess {
47
48
49
50
51
52 err := &DNSError{Err: "server misbehaving", Name: name, Server: server}
53 if dns.rcode == dnsRcodeServerFailure {
54 err.IsTemporary = true
55 }
56 return "", nil, err
57 }
58
59
60
61
62
63
64 Cname:
65 for cnameloop := 0; cnameloop < 10; cnameloop++ {
66 addrs = addrs[0:0]
67 for _, rr := range dns.answer {
68 if _, justHeader := rr.(*dnsRR_Header); justHeader {
69
70
71
72
73 continue
74 }
75 h := rr.Header()
76 if h.Class == dnsClassINET && equalASCIILabel(h.Name, name) {
77 switch h.Rrtype {
78 case qtype:
79 addrs = append(addrs, rr)
80 case dnsTypeCNAME:
81
82 name = rr.(*dnsRR_CNAME).Cname
83 continue Cname
84 }
85 }
86 }
87 if len(addrs) == 0 {
88 return "", nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, Server: server}
89 }
90 return name, addrs, nil
91 }
92
93 return "", nil, &DNSError{Err: "too many redirects", Name: name, Server: server}
94 }
95
96 func equalASCIILabel(x, y string) bool {
97 if len(x) != len(y) {
98 return false
99 }
100 for i := 0; i < len(x); i++ {
101 a := x[i]
102 b := y[i]
103 if 'A' <= a && a <= 'Z' {
104 a += 0x20
105 }
106 if 'A' <= b && b <= 'Z' {
107 b += 0x20
108 }
109 if a != b {
110 return false
111 }
112 }
113 return true
114 }
115
116
117
118
119 func isDomainName(s string) bool {
120
121
122
123
124
125
126
127
128 l := len(s)
129 if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
130 return false
131 }
132
133 last := byte('.')
134 ok := false
135 partlen := 0
136 for i := 0; i < len(s); i++ {
137 c := s[i]
138 switch {
139 default:
140 return false
141 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
142 ok = true
143 partlen++
144 case '0' <= c && c <= '9':
145
146 partlen++
147 case c == '-':
148
149 if last == '.' {
150 return false
151 }
152 partlen++
153 case c == '.':
154
155 if last == '.' || last == '-' {
156 return false
157 }
158 if partlen > 63 || partlen == 0 {
159 return false
160 }
161 partlen = 0
162 }
163 last = c
164 }
165 if last == '-' || partlen > 63 {
166 return false
167 }
168
169 return ok
170 }
171
172
173
174
175
176
177
178
179
180 func absDomainName(b []byte) string {
181 hasDots := false
182 for _, x := range b {
183 if x == '.' {
184 hasDots = true
185 break
186 }
187 }
188 if hasDots && b[len(b)-1] != '.' {
189 b = append(b, '.')
190 }
191 return string(b)
192 }
193
194
195 type SRV struct {
196 Target string
197 Port uint16
198 Priority uint16
199 Weight uint16
200 }
201
202
203 type byPriorityWeight []*SRV
204
205 func (s byPriorityWeight) Len() int { return len(s) }
206 func (s byPriorityWeight) Less(i, j int) bool {
207 return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
208 }
209 func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
210
211
212
213 func (addrs byPriorityWeight) shuffleByWeight() {
214 sum := 0
215 for _, addr := range addrs {
216 sum += int(addr.Weight)
217 }
218 for sum > 0 && len(addrs) > 1 {
219 s := 0
220 n := rand.Intn(sum)
221 for i := range addrs {
222 s += int(addrs[i].Weight)
223 if s > n {
224 if i > 0 {
225 addrs[0], addrs[i] = addrs[i], addrs[0]
226 }
227 break
228 }
229 }
230 sum -= int(addrs[0].Weight)
231 addrs = addrs[1:]
232 }
233 }
234
235
236 func (addrs byPriorityWeight) sort() {
237 sort.Sort(addrs)
238 i := 0
239 for j := 1; j < len(addrs); j++ {
240 if addrs[i].Priority != addrs[j].Priority {
241 addrs[i:j].shuffleByWeight()
242 i = j
243 }
244 }
245 addrs[i:].shuffleByWeight()
246 }
247
248
249 type MX struct {
250 Host string
251 Pref uint16
252 }
253
254
255 type byPref []*MX
256
257 func (s byPref) Len() int { return len(s) }
258 func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
259 func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
260
261
262 func (s byPref) sort() {
263 for i := range s {
264 j := rand.Intn(i + 1)
265 s[i], s[j] = s[j], s[i]
266 }
267 sort.Sort(s)
268 }
269
270
271 type NS struct {
272 Host string
273 }
274
View as plain text