Added MakeSurrogateIP to help with sorting

pull/13/head
Chris Bednarski 9 years ago
parent 84a954d8bd
commit 1377ff4274

@ -31,9 +31,18 @@ func (h Hostlist) Len() int {
return len(h)
}
// MakeSurrogateIP takes an IP like 127.0.0.1 and munges it to 0.0.0.1 so we can
// sort it more easily.
func MakeSurrogateIP(IP net.IP) net.IP {
if string(IP[0:3]) == "127" {
return net.IP("0" + string(IP[3:]))
}
return IP
}
// Less determines the sort order of two Hostnames, part of sort.Interface
func (h Hostlist) Less(A, B int) bool {
// Sort 127.0.0.1, 127.0.1.1 and "localhost" at the top
// Sort "localhost" at the top
if h[A].Domain == "localhost" {
return true
}
@ -52,20 +61,25 @@ func (h Hostlist) Less(A, B int) bool {
}
// Compare the the IP addresses (byte array)
if !h[A].IP.Equal(h[B].IP) {
for charIndex := range h[A].IP {
// We want to push 127. to the top so we're going to mark it zero.
surrogateA := MakeSurrogateIP(h[A].IP)
surrogateB := MakeSurrogateIP(h[B].IP)
if !surrogateA.Equal(surrogateB) {
for charIndex := range surrogateA {
// A and B's IPs differ at this index, and A is less. A wins!
if h[A].IP[charIndex] < h[B].IP[charIndex] {
if surrogateA[charIndex] < surrogateB[charIndex] {
return true
}
// A and B's IPs differ at this index, and B is less. A loses!
if h[A].IP[charIndex] > h[B].IP[charIndex] {
if surrogateA[charIndex] > surrogateB[charIndex] {
return false
}
}
// If we got here then the IPs are the same and we want to continue on
// to the domain sorting section.
}
// Prep for domain sorting
// Prep for sorting by domain name
aLength := len(h[A].Domain)
bLength := len(h[B].Domain)
max := aLength
@ -97,8 +111,9 @@ func (h Hostlist) Less(A, B int) bool {
}
}
// Seems like everything was the same, so it can't be Less. Also since we
// can't Add something twice we should never end up here. Just in case...
// If we got here then A and B are the same -- by definition A is not Less
// than B so we return false. Technically we shouldn't get here since Add
// should not allow duplicates, but we'll guard anyway.
return false
}
@ -164,7 +179,6 @@ func (h *Hostlist) Add(host *Hostname) error {
}
}
*h = append(*h, host)
h.Sort()
return nil
}

@ -7,6 +7,21 @@ import (
"testing"
)
func TestMakeSurrogateIP(t *testing.T) {
const orig = "127.0.0.1"
const expected1 = "0.0.0.1"
IP1 := string(hostess.MakeSurrogateIP(net.IP(orig)))
if IP1 != expected1 {
t.Errorf("Expected %s to convert to %s; got %s", orig, expected1, IP1)
}
const expected2 = "10.20.30.40"
IP2 := string(hostess.MakeSurrogateIP(net.IP(expected2)))
if IP2 != expected2 {
t.Errorf("Expected %s to remain unchanged; got %s", expected2, IP2)
}
}
func TestContainsDomainIp(t *testing.T) {
hosts := hostess.NewHostlist()
hosts.Add(hostess.NewHostname(domain, ip, false))

Loading…
Cancel
Save