Source file
src/net/dnsclient.go
Documentation: net
1
2
3
4
5 package net
6
7 import (
8 "sort"
9
10 "golang.org/x/net/dns/dnsmessage"
11 )
12
13
14 func fastrand() uint32
15
16 func randInt() int {
17 x, y := fastrand(), fastrand()
18 u := uint(x)<<31 ^ uint(int32(y))
19 i := int(u >> 1)
20 return i
21 }
22
23 func randIntn(n int) int {
24 return randInt() % n
25 }
26
27
28
29
30 func reverseaddr(addr string) (arpa string, err error) {
31 ip := ParseIP(addr)
32 if ip == nil {
33 return "", &DNSError{Err: "unrecognized address", Name: addr}
34 }
35 if ip.To4() != nil {
36 return uitoa(uint(ip[15])) + "." + uitoa(uint(ip[14])) + "." + uitoa(uint(ip[13])) + "." + uitoa(uint(ip[12])) + ".in-addr.arpa.", nil
37 }
38
39 buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
40
41 for i := len(ip) - 1; i >= 0; i-- {
42 v := ip[i]
43 buf = append(buf, hexDigit[v&0xF],
44 '.',
45 hexDigit[v>>4],
46 '.')
47 }
48
49 buf = append(buf, "ip6.arpa."...)
50 return string(buf), nil
51 }
52
53 func equalASCIIName(x, y dnsmessage.Name) bool {
54 if x.Length != y.Length {
55 return false
56 }
57 for i := 0; i < int(x.Length); i++ {
58 a := x.Data[i]
59 b := y.Data[i]
60 if 'A' <= a && a <= 'Z' {
61 a += 0x20
62 }
63 if 'A' <= b && b <= 'Z' {
64 b += 0x20
65 }
66 if a != b {
67 return false
68 }
69 }
70 return true
71 }
72
73
74
75
76 func isDomainName(s string) bool {
77
78
79
80
81
82
83
84
85 l := len(s)
86 if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
87 return false
88 }
89
90 last := byte('.')
91 nonNumeric := false
92 partlen := 0
93 for i := 0; i < len(s); i++ {
94 c := s[i]
95 switch {
96 default:
97 return false
98 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
99 nonNumeric = true
100 partlen++
101 case '0' <= c && c <= '9':
102
103 partlen++
104 case c == '-':
105
106 if last == '.' {
107 return false
108 }
109 partlen++
110 nonNumeric = true
111 case c == '.':
112
113 if last == '.' || last == '-' {
114 return false
115 }
116 if partlen > 63 || partlen == 0 {
117 return false
118 }
119 partlen = 0
120 }
121 last = c
122 }
123 if last == '-' || partlen > 63 {
124 return false
125 }
126
127 return nonNumeric
128 }
129
130
131
132
133
134
135
136
137
138 func absDomainName(b []byte) string {
139 hasDots := false
140 for _, x := range b {
141 if x == '.' {
142 hasDots = true
143 break
144 }
145 }
146 if hasDots && b[len(b)-1] != '.' {
147 b = append(b, '.')
148 }
149 return string(b)
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