1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/net/dns/dnsmessage"
15 // provided by runtime
19 return int(fastrandu() >> 1) // clear sign bit
22 func randIntn(n int) int {
26 // reverseaddr returns the in-addr.arpa. or ip6.arpa. hostname of the IP
27 // address addr suitable for rDNS (PTR) record lookup or an error if it fails
28 // to parse the IP address.
29 func reverseaddr(addr string) (arpa string, err error) {
32 return "", &DNSError{Err: "unrecognized address", Name: addr}
35 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 buf := make([]byte, 0, len(ip)*4+len("ip6.arpa."))
39 // Add it, in reverse, to the buffer
40 for i := len(ip) - 1; i >= 0; i-- {
42 buf = append(buf, hexDigit[v&0xF],
47 // Append "ip6.arpa." and return (buf already has the final .)
48 buf = append(buf, "ip6.arpa."...)
49 return string(buf), nil
52 func equalASCIIName(x, y dnsmessage.Name) bool {
53 if x.Length != y.Length {
56 for i := 0; i < int(x.Length); i++ {
59 if 'A' <= a && a <= 'Z' {
62 if 'A' <= b && b <= 'Z' {
72 // isDomainName checks if a string is a presentation-format domain name
73 // (currently restricted to hostname-compatible "preferred name" LDH labels and
74 // SRV-like "underscore labels"; see golang.org/issue/12421).
75 func isDomainName(s string) bool {
76 // The root domain name is valid. See golang.org/issue/45715.
81 // See RFC 1035, RFC 3696.
82 // Presentation format has dots before every label except the first, and the
83 // terminal empty label is optional here because we assume fully-qualified
84 // (absolute) input. We must therefore reserve space for the first and last
85 // labels' length octets in wire format, where they are necessary and the
86 // maximum total length is 255.
87 // So our _effective_ maximum is 253, but 254 is not rejected if the last
88 // character is a dot.
90 if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
95 nonNumeric := false // true once we've seen a letter or hyphen
97 for i := 0; i < len(s); i++ {
102 case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
105 case '0' <= c && c <= '9':
109 // Byte before dash cannot be dot.
116 // Byte before dot cannot be dot, dash.
117 if last == '.' || last == '-' {
120 if partlen > 63 || partlen == 0 {
127 if last == '-' || partlen > 63 {
134 // absDomainName returns an absolute domain name which ends with a
135 // trailing dot to match pure Go reverse resolver and all other lookup
137 // See golang.org/issue/12189.
138 // But we don't want to add dots for local names from /etc/hosts.
139 // It's hard to tell so we settle on the heuristic that names without dots
140 // (like "localhost" or "myhost") do not get trailing dots, but any other
142 func absDomainName(s string) string {
143 if bytealg.IndexByteString(s, '.') != -1 && s[len(s)-1] != '.' {
149 // An SRV represents a single DNS SRV record.
157 // byPriorityWeight sorts SRV records by ascending priority and weight.
158 type byPriorityWeight []*SRV
160 func (s byPriorityWeight) Len() int { return len(s) }
161 func (s byPriorityWeight) Less(i, j int) bool {
162 return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
164 func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
166 // shuffleByWeight shuffles SRV records by weight using the algorithm
167 // described in RFC 2782.
168 func (addrs byPriorityWeight) shuffleByWeight() {
170 for _, addr := range addrs {
171 sum += int(addr.Weight)
173 for sum > 0 && len(addrs) > 1 {
176 for i := range addrs {
177 s += int(addrs[i].Weight)
180 addrs[0], addrs[i] = addrs[i], addrs[0]
185 sum -= int(addrs[0].Weight)
190 // sort reorders SRV records as specified in RFC 2782.
191 func (addrs byPriorityWeight) sort() {
194 for j := 1; j < len(addrs); j++ {
195 if addrs[i].Priority != addrs[j].Priority {
196 addrs[i:j].shuffleByWeight()
200 addrs[i:].shuffleByWeight()
203 // An MX represents a single DNS MX record.
209 // byPref implements sort.Interface to sort MX records by preference
212 func (s byPref) Len() int { return len(s) }
213 func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
214 func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
216 // sort reorders MX records as specified in RFC 5321.
217 func (s byPref) sort() {
220 s[i], s[j] = s[j], s[i]
225 // An NS represents a single DNS NS record.