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