Skip to content

Commit

Permalink
types/key: export constants for key size, not a method.
Browse files Browse the repository at this point in the history
Signed-off-by: David Anderson <danderson@tailscale.com>
  • Loading branch information
danderson committed Oct 30, 2021
1 parent 6422789 commit 84c3a09
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions derp/derp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions derp/derp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions disco/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
Expand All @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions types/key/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 4 additions & 6 deletions types/key/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions wgengine/magicsock/magicsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down

0 comments on commit 84c3a09

Please sign in to comment.