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