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