update to latest github.com/mjl-/adns, synced to go1.24.1

This commit is contained in:
Mechiel Lukkien
2025-03-21 18:42:02 +01:00
parent 70aedddc90
commit 773d8cc959
14 changed files with 188 additions and 159 deletions

View File

@ -5,9 +5,10 @@
package adns
import (
mathrand "math/rand"
"cmp"
mathrand "math/rand/v2"
"net"
"sort"
"slices"
"golang.org/x/net/dns/dnsmessage"
@ -16,7 +17,7 @@ import (
)
func randInt() int {
return mathrand.Int()
return int(uint(mathrand.Uint64()) >> 1) // clear sign bit
}
func randIntn(n int) int {
@ -72,6 +73,16 @@ func equalASCIIName(x, y dnsmessage.Name) bool {
// isDomainName checks if a string is a presentation-format domain name
// (currently restricted to hostname-compatible "preferred name" LDH labels and
// SRV-like "underscore labels"; see golang.org/issue/12421).
//
// isDomainName should be an internal detail,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:
// - github.com/sagernet/sing
//
// Do not remove or change the type signature.
// See go.dev/issue/67401.
//
// disabled: go:linkname isDomainName
func isDomainName(s string) bool {
// The root domain name is valid. See golang.org/issue/45715.
if s == "." {
@ -149,12 +160,6 @@ func absDomainName(s string) string {
// byPriorityWeight sorts SRV records by ascending priority and weight.
type byPriorityWeight []*net.SRV
func (s byPriorityWeight) Len() int { return len(s) }
func (s byPriorityWeight) Less(i, j int) bool {
return s[i].Priority < s[j].Priority || (s[i].Priority == s[j].Priority && s[i].Weight < s[j].Weight)
}
func (s byPriorityWeight) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// shuffleByWeight shuffles SRV records by weight using the algorithm
// described in RFC 2782.
func (addrs byPriorityWeight) shuffleByWeight() {
@ -181,7 +186,12 @@ func (addrs byPriorityWeight) shuffleByWeight() {
// sort reorders SRV records as specified in RFC 2782.
func (addrs byPriorityWeight) sort() {
sort.Sort(addrs)
slices.SortFunc(addrs, func(a, b *net.SRV) int {
if r := cmp.Compare(a.Priority, b.Priority); r != 0 {
return r
}
return cmp.Compare(a.Weight, b.Weight)
})
i := 0
for j := 1; j < len(addrs); j++ {
if addrs[i].Priority != addrs[j].Priority {
@ -192,18 +202,16 @@ func (addrs byPriorityWeight) sort() {
addrs[i:].shuffleByWeight()
}
// byPref implements sort.Interface to sort MX records by preference
// byPref sorts MX records by preference
type byPref []*net.MX
func (s byPref) Len() int { return len(s) }
func (s byPref) Less(i, j int) bool { return s[i].Pref < s[j].Pref }
func (s byPref) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// sort reorders MX records as specified in RFC 5321.
func (s byPref) sort() {
for i := range s {
j := randIntn(i + 1)
s[i], s[j] = s[j], s[i]
}
sort.Sort(s)
slices.SortFunc(s, func(a, b *net.MX) int {
return cmp.Compare(a.Pref, b.Pref)
})
}