From 4d4b3599918aab8ee6315c7f2f35f70db89e4d02 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Thu, 11 Jan 2024 20:20:42 +0300 Subject: [PATCH] home: imp code --- internal/home/client.go | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/home/client.go b/internal/home/client.go index bcf5ac2e717..8a044e9b583 100644 --- a/internal/home/client.go +++ b/internal/home/client.go @@ -1,6 +1,7 @@ package home import ( + "bytes" "encoding" "fmt" "net" @@ -113,12 +114,11 @@ func (c *persistentClient) setIDs(ids []string) (err error) { } slices.SortFunc(c.IPs, netip.Addr.Compare) - slices.SortFunc(c.Subnets, func(a, b netip.Prefix) int { - return strings.Compare(a.String(), b.String()) - }) + // TODO(s.chzhen): Use netip.PrefixCompare in Go 1.23. + slices.SortFunc(c.Subnets, subnetCompare) slices.SortFunc(c.MACs, func(a, b net.HardwareAddr) int { - return strings.Compare(a.String(), b.String()) + return bytes.Compare(a, b) }) slices.Sort(c.ClientIDs) @@ -126,6 +126,27 @@ func (c *persistentClient) setIDs(ids []string) (err error) { return nil } +// subnetCompare is a comparison function for the two subnets. It returns -1 if +// x sorts before y, 1 if x sorts after y, and 0 if their relative sorting +// position is the same. +func subnetCompare(x, y netip.Prefix) (cmp int) { + if x == y { + return 0 + } + + xAddr, xBits := x.Addr(), x.Bits() + yAddr, yBits := y.Addr(), y.Bits() + if xBits == yBits { + return xAddr.Compare(yAddr) + } + + if xBits > yBits { + return -1 + } else { + return 1 + } +} + // setID parses id into typed field if there is no error. func (c *persistentClient) setID(id string) (err error) { if id == "" {