From a0dacba877b1c4dc9e7d87cd12b38868e439d219 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 29 Apr 2021 15:43:12 -0700 Subject: [PATCH] wgengine/magicsock: simplify legacy endpoint DstToString Legacy endpoints (addrSet) currently reconstruct their dst string when requested. Instead, store the dst string we were given to begin with. In addition to being simpler and cheaper, this makes less code aware of how to interpret endpoint strings. Signed-off-by: Josh Bleecher Snyder --- wgengine/magicsock/legacy.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/wgengine/magicsock/legacy.go b/wgengine/magicsock/legacy.go index db59580e3..c07166235 100644 --- a/wgengine/magicsock/legacy.go +++ b/wgengine/magicsock/legacy.go @@ -43,6 +43,7 @@ func (c *Conn) createLegacyEndpointLocked(pk key.Public, addrs string) (conn.End Logf: c.logf, publicKey: pk, curAddr: -1, + rawdst: addrs, } if addrs != "" { @@ -384,6 +385,9 @@ type addrSet struct { // set to a better one. This is only to suppress some // redundant logs. loggedLogPriMask uint32 + + // rawdst is the destination string from/for wireguard-go. + rawdst string } // derpID returns this addrSet's home DERP node, or 0 if none is found. @@ -426,17 +430,7 @@ func (a *addrSet) DstToBytes() []byte { return packIPPort(a.dst()) } func (a *addrSet) DstToString() string { - var addrs []string - for _, addr := range a.ipPorts { - addrs = append(addrs, addr.String()) - } - - a.mu.Lock() - defer a.mu.Unlock() - if a.roamAddr != nil { - addrs = append(addrs, a.roamAddr.String()) - } - return strings.Join(addrs, ",") + return a.rawdst } func (a *addrSet) DstIP() net.IP { return a.dst().IP.IPAddr().IP // TODO: add netaddr accessor to cut an alloc here?