Source file
src/net/dnsclient.go
1
2
3
4
5 package net
6
7 import (
8 "internal/bytealg"
9 "internal/itoa"
10 "sort"
11
12 "golang.org/x/net/dns/dnsmessage"
13 )
14
15
16 func fastrand() uint32
17
18 func randInt() int {
19 x, y := fastrand(), fastrand()
20 u := uint(x)<<31 ^ uint(int32(y))
21 i := int(u >> 1)
22 return i
23 }
24
25 func randIntn(n int) int {
26 return randInt() % n
27 }
28
29
30
31
32 func reverseaddr(addr string) (arpa string, err error) {
33 ip := ParseIP(addr)
34 if ip == nil {
35 return "", &DNSError{Err: "unrecognized address", Name: addr}
36 }
37 if ip.To4() != nil {
38 return itoa.Uitoa(uint(ip[15])) + "." + itoa.Uitoa(uint(ip[14])) + "." + itoa.Uitoa(uint(ip[13])) + "." + itoa.Uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
39 }
40
41 buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
42
43 for i := len(ip) - 1; i >= 0; i-- {
44 v := ip[i]
45 buf = append(buf, hexDigit[v&0xF],
46 '.',
47 hexDigit[v>>4],
48 '.')
49 }
50
51 buf = append(buf, "ip6.arpa."...)
52 return string(buf), nil
53 }
54
55 func equalASCIIName(x, y dnsmessage.Name) bool {
56 if x.Length != y.Length {
57 return false
58 }
59 for i := 0; i < int(x.Length); i++ {
60 a := x.Data[i]
61 b := y.Data[i]
62 if 'A' <= a && a <= 'Z' {
63 a += 0x20
64 }
65 if 'A' <= b && b <= 'Z' {
66 b += 0x20
67 }
68 if a != b {
69 return false
70 }
71 }
72 return true
73 }
74
75
76
77
78 func isDomainName(s string) bool {
79
80 if s == "." {
81 return true
82 }
83
84
85
86
87
88
89
90
91
92 l := len(s)
93 if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
94 return false
95 }
96
97 last := byte('.')
98 nonNumeric := false
99 partlen := 0
100 for i := 0; i < len(s); i++ {
101 c := s[i]
102 switch {
103 default:
104 return false
105 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
106 nonNumeric = true
107 partlen++
108 case '0' <= c && c <= '9':
109
110 partlen++
111 case c == '-':
112
113 if last == '.' {
114 return false
115 }
116 partlen++
117 nonNumeric = true
118 case c == '.':
119
120 if last == '.' || last == '-' {
121 return false
122 }
123 if partlen > 63 || partlen == 0 {
124 return false
125 }
126 partlen = 0
127 }
128 last = c
129 }
130 if last == '-' || partlen > 63 {
131 return false
132 }
133
134 return nonNumeric
135 }
136
137
138
139
140
141
142
143
144
145 func absDomainName(s string) string {
146 if bytealg.IndexByteString(s, '.') != -1 && s[len(s)-1] != '.' {
147 s += "."
148 }
149 return s
150 }
151
152
153 type SRV struct {
154 Target string
155 Port uint16
156 Priority uint16
157 Weight uint16
158 }
159
160
161 type byPriorityWeight []*SRV
162
163 func (s byPriorityWeight) Len() int { return len(s) }
164 func (s byPriorityWeight) Less(i, j int) bool {
165 return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
166 }
167 func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
168
169
170
171 func (addrs byPriorityWeight) shuffleByWeight() {
172 sum := 0
173 for _, addr := range addrs {
174 sum += int(addr.Weight)
175 }
176 for sum > 0 && len(addrs) > 1 {
177 s := 0
178 n := randIntn(sum)
179 for i := range addrs {
180 s += int(addrs[i].Weight)
181 if s > n {
182 if i > 0 {
183 addrs[0], addrs[i] = addrs[i], addrs[0]
184 }
185 break
186 }
187 }
188 sum -= int(addrs[0].Weight)
189 addrs = addrs[1:]
190 }
191 }
192
193
194 func (addrs byPriorityWeight) sort() {
195 sort.Sort(addrs)
196 i := 0
197 for j := 1; j < len(addrs); j++ {
198 if addrs[i].Priority != addrs[j].Priority {
199 addrs[i:j].shuffleByWeight()
200 i = j
201 }
202 }
203 addrs[i:].shuffleByWeight()
204 }
205
206
207 type MX struct {
208 Host string
209 Pref uint16
210 }
211
212
213 type byPref []*MX
214
215 func (s byPref) Len() int { return len(s) }
216 func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
217 func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
218
219
220 func (s byPref) sort() {
221 for i := range s {
222 j := randIntn(i + 1)
223 s[i], s[j] = s[j], s[i]
224 }
225 sort.Sort(s)
226 }
227
228
229 type NS struct {
230 Host string
231 }
232
View as plain text