diff options
author | Corey Thomasson <cthom.lists@gmail.com> | 2011-04-14 10:30:56 +1000 |
---|---|---|
committer | Andrew Gerrand <adg@golang.org> | 2011-04-14 10:30:56 +1000 |
commit | 785fbd94fda295469aac854f8c7eadb78832b457 (patch) | |
tree | 1756e62fd2e4e645075d7daa78d2f2d1cab68b7e | |
parent | eb5e4b85194f2c01c8eac0c8f47c509f0e198d14 (diff) | |
download | go-785fbd94fda295469aac854f8c7eadb78832b457.tar.gz go-785fbd94fda295469aac854f8c7eadb78832b457.zip |
net: sort records returned by LookupMX
R=rog, adg, rsc
CC=golang-dev
https://golang.org/cl/4388048
-rw-r--r-- | src/pkg/net/dnsclient.go | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/pkg/net/dnsclient.go b/src/pkg/net/dnsclient.go index 32cea6125e..c3e727bcef 100644 --- a/src/pkg/net/dnsclient.go +++ b/src/pkg/net/dnsclient.go @@ -21,6 +21,7 @@ import ( "rand" "sync" "time" + "sort" ) // DNSError represents a DNS lookup error. @@ -410,18 +411,32 @@ type MX struct { Pref uint16 } -// LookupMX returns the DNS MX records associated with name. -func LookupMX(name string) (entries []*MX, err os.Error) { - var records []dnsRR - _, records, err = lookup(name, dnsTypeMX) +// byPref implements sort.Interface to sort MX records by preference +type byPref []*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] } + +// LookupMX returns the DNS MX records for the given domain name sorted by preference. +func LookupMX(name string) (mx []*MX, err os.Error) { + _, rr, err := lookup(name, dnsTypeMX) if err != nil { return } - entries = make([]*MX, len(records)) - for i := range records { - r := records[i].(*dnsRR_MX) - entries[i] = &MX{r.Mx, r.Pref} + mx = make([]*MX, len(rr)) + for i := range rr { + r := rr[i].(*dnsRR_MX) + mx[i] = &MX{r.Mx, r.Pref} + } + // Shuffle the records to match RFC 5321 when sorted + for i := range mx { + j := rand.Intn(i + 1) + mx[i], mx[j] = mx[j], mx[i] } + sort.Sort(byPref(mx)) return } |