diff --git a/derp/derp_client.go b/derp/derp_client.go index 6982f38af..50377da85 100644 --- a/derp/derp_client.go +++ b/derp/derp_client.go @@ -210,12 +210,12 @@ func (c *Client) send(dstKey key.NodePublic, pkt []byte) (ret error) { c.wmu.Lock() defer c.wmu.Unlock() if c.rate != nil { - pktLen := frameHeaderLen + dstKey.RawLen() + len(pkt) + pktLen := frameHeaderLen + key.NodePublicRawLen + len(pkt) if !c.rate.AllowN(time.Now(), pktLen) { return nil // drop } } - if err := writeFrameHeader(c.bw, frameSendPacket, uint32(dstKey.RawLen()+len(pkt))); err != nil { + if err := writeFrameHeader(c.bw, frameSendPacket, uint32(key.NodePublicRawLen+len(pkt))); err != nil { return err } if _, err := c.bw.Write(dstKey.AppendTo(nil)); err != nil { diff --git a/derp/derp_server.go b/derp/derp_server.go index 451e49939..e759c5ea4 100644 --- a/derp/derp_server.go +++ b/derp/derp_server.go @@ -1017,7 +1017,7 @@ func (s *Server) verifyClient(clientKey key.NodePublic, info *clientInfo) error } func (s *Server) sendServerKey(lw *lazyBufioWriter) error { - buf := make([]byte, 0, len(magic)+s.publicKey.RawLen()) + buf := make([]byte, 0, len(magic)+key.NodePublicRawLen) buf = append(buf, magic...) buf = s.publicKey.AppendTo(buf) err := writeFrame(lw.bw(), frameServerKey, buf) @@ -1469,7 +1469,7 @@ func (c *sclient) sendPacket(srcKey key.NodePublic, contents []byte) (err error) withKey := !srcKey.IsZero() pktLen := len(contents) if withKey { - pktLen += srcKey.RawLen() + pktLen += key.NodePublicRawLen } if err = writeFrameHeader(c.bw.bw(), frameRecvPacket, uint32(pktLen)); err != nil { return err diff --git a/disco/disco.go b/disco/disco.go index 552684e73..c46c83e18 100644 --- a/disco/disco.go +++ b/disco/disco.go @@ -123,7 +123,7 @@ func (m *Ping) AppendMarshal(b []byte) []byte { dataLen := 12 hasKey := !m.NodeKey.IsZero() if hasKey { - dataLen += m.NodeKey.RawLen() + dataLen += key.NodePublicRawLen } ret, d := appendMsgHeader(b, TypePing, v0, dataLen) n := copy(d, m.TxID[:]) @@ -141,8 +141,8 @@ func parsePing(ver uint8, p []byte) (m *Ping, err error) { p = p[copy(m.TxID[:], p):] // Deliberately lax on longer-than-expected messages, for future // compatibility. - if len(p) >= m.NodeKey.RawLen() { - m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:m.NodeKey.RawLen()])) + if len(p) >= key.NodePublicRawLen { + m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:key.NodePublicRawLen])) } return m, nil } diff --git a/types/key/disco.go b/types/key/disco.go index cf3fb1fca..881ec4de6 100644 --- a/types/key/disco.go +++ b/types/key/disco.go @@ -21,6 +21,10 @@ const ( // This prefix is used in the control protocol, so cannot be // changed. discoPublicHexPrefix = "discokey:" + + // DiscoPublicRawLen is the length in bytes of a DiscoPublic, when + // serialized with AppendTo, Raw32 or WriteRawWithoutAllocating. + DiscoPublicRawLen = 32 ) // DiscoPrivate is a disco key, used for peer-to-peer path discovery. @@ -115,12 +119,6 @@ func (k DiscoPublic) AppendTo(buf []byte) []byte { return append(buf, k.k[:]...) } -// RawLen returns the length of k when to the format handled by -// ReadRawWithoutAllocating and WriteRawWithoutAllocating. -func (k DiscoPublic) RawLen() int { - return 32 -} - // String returns the output of MarshalText as a string. func (k DiscoPublic) String() string { bs, err := k.MarshalText() diff --git a/types/key/node.go b/types/key/node.go index bf7088c3d..bddfdbec6 100644 --- a/types/key/node.go +++ b/types/key/node.go @@ -33,6 +33,10 @@ const ( // This prefix is used in the control protocol, so cannot be // changed. nodePublicHexPrefix = "nodekey:" + + // NodePublicRawLen is the length in bytes of a NodePublic, when + // serialized with AppendTo, Raw32 or WriteRawWithoutAllocating. + NodePublicRawLen = 32 ) // NodePrivate is a node key, used for WireGuard tunnels and @@ -190,12 +194,6 @@ func (k NodePublic) AppendTo(buf []byte) []byte { return append(buf, k.k[:]...) } -// RawLen returns the length of k when to the format handled by -// ReadRawWithoutAllocating and WriteRawWithoutAllocating. -func (k NodePublic) RawLen() int { - return 32 -} - // ReadRawWithoutAllocating initializes k with bytes read from br. // The reading is done ~4x slower than io.ReadFull, but in exchange is // allocation-free. diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 986603cf4..74e864a93 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1798,7 +1798,7 @@ func (c *Conn) sendDiscoMessage(dst netaddr.IPPort, dstKey tailcfg.NodeKey, dstD // it was received from at the DERP layer. derpNodeSrc is zero when received // over UDP. func (c *Conn) handleDiscoMessage(msg []byte, src netaddr.IPPort, derpNodeSrc tailcfg.NodeKey) (isDiscoMsg bool) { - headerLen := len(disco.Magic) + key.DiscoPublic{}.RawLen() + const headerLen = len(disco.Magic) + key.DiscoPublicRawLen if len(msg) < headerLen || string(msg[:len(disco.Magic)]) != disco.Magic { return false } @@ -1809,7 +1809,7 @@ func (c *Conn) handleDiscoMessage(msg []byte, src netaddr.IPPort, derpNodeSrc ta // Use naked returns for all following paths. isDiscoMsg = true - sender := key.DiscoPublicFromRaw32(mem.B(msg[len(disco.Magic) : len(disco.Magic)+key.DiscoPublic{}.RawLen()])) + sender := key.DiscoPublicFromRaw32(mem.B(msg[len(disco.Magic):headerLen])) c.mu.Lock() defer c.mu.Unlock()