types/key: export constants for key size, not a method.

Signed-off-by: David Anderson <danderson@tailscale.com>
pull/3227/head
David Anderson 3 years ago
parent 6422789ea0
commit 84c3a09a8d

@ -210,12 +210,12 @@ func (c *Client) send(dstKey key.NodePublic, pkt []byte) (ret error) {
c.wmu.Lock() c.wmu.Lock()
defer c.wmu.Unlock() defer c.wmu.Unlock()
if c.rate != nil { if c.rate != nil {
pktLen := frameHeaderLen + dstKey.RawLen() + len(pkt) pktLen := frameHeaderLen + key.NodePublicRawLen + len(pkt)
if !c.rate.AllowN(time.Now(), pktLen) { if !c.rate.AllowN(time.Now(), pktLen) {
return nil // drop 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 return err
} }
if _, err := c.bw.Write(dstKey.AppendTo(nil)); err != nil { if _, err := c.bw.Write(dstKey.AppendTo(nil)); err != nil {

@ -1017,7 +1017,7 @@ func (s *Server) verifyClient(clientKey key.NodePublic, info *clientInfo) error
} }
func (s *Server) sendServerKey(lw *lazyBufioWriter) 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 = append(buf, magic...)
buf = s.publicKey.AppendTo(buf) buf = s.publicKey.AppendTo(buf)
err := writeFrame(lw.bw(), frameServerKey, 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() withKey := !srcKey.IsZero()
pktLen := len(contents) pktLen := len(contents)
if withKey { if withKey {
pktLen += srcKey.RawLen() pktLen += key.NodePublicRawLen
} }
if err = writeFrameHeader(c.bw.bw(), frameRecvPacket, uint32(pktLen)); err != nil { if err = writeFrameHeader(c.bw.bw(), frameRecvPacket, uint32(pktLen)); err != nil {
return err return err

@ -123,7 +123,7 @@ func (m *Ping) AppendMarshal(b []byte) []byte {
dataLen := 12 dataLen := 12
hasKey := !m.NodeKey.IsZero() hasKey := !m.NodeKey.IsZero()
if hasKey { if hasKey {
dataLen += m.NodeKey.RawLen() dataLen += key.NodePublicRawLen
} }
ret, d := appendMsgHeader(b, TypePing, v0, dataLen) ret, d := appendMsgHeader(b, TypePing, v0, dataLen)
n := copy(d, m.TxID[:]) 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):] p = p[copy(m.TxID[:], p):]
// Deliberately lax on longer-than-expected messages, for future // Deliberately lax on longer-than-expected messages, for future
// compatibility. // compatibility.
if len(p) >= m.NodeKey.RawLen() { if len(p) >= key.NodePublicRawLen {
m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:m.NodeKey.RawLen()])) m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:key.NodePublicRawLen]))
} }
return m, nil return m, nil
} }

@ -21,6 +21,10 @@ const (
// This prefix is used in the control protocol, so cannot be // This prefix is used in the control protocol, so cannot be
// changed. // changed.
discoPublicHexPrefix = "discokey:" 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. // 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[:]...) 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. // String returns the output of MarshalText as a string.
func (k DiscoPublic) String() string { func (k DiscoPublic) String() string {
bs, err := k.MarshalText() bs, err := k.MarshalText()

@ -33,6 +33,10 @@ const (
// This prefix is used in the control protocol, so cannot be // This prefix is used in the control protocol, so cannot be
// changed. // changed.
nodePublicHexPrefix = "nodekey:" 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 // 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[:]...) 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. // ReadRawWithoutAllocating initializes k with bytes read from br.
// The reading is done ~4x slower than io.ReadFull, but in exchange is // The reading is done ~4x slower than io.ReadFull, but in exchange is
// allocation-free. // allocation-free.

@ -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 // it was received from at the DERP layer. derpNodeSrc is zero when received
// over UDP. // over UDP.
func (c *Conn) handleDiscoMessage(msg []byte, src netaddr.IPPort, derpNodeSrc tailcfg.NodeKey) (isDiscoMsg bool) { 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 { if len(msg) < headerLen || string(msg[:len(disco.Magic)]) != disco.Magic {
return false return false
} }
@ -1809,7 +1809,7 @@ func (c *Conn) handleDiscoMessage(msg []byte, src netaddr.IPPort, derpNodeSrc ta
// Use naked returns for all following paths. // Use naked returns for all following paths.
isDiscoMsg = true 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() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()

Loading…
Cancel
Save