disco: use key.NodePublic instead of tailcfg.NodeKey.

Updates #3206

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

@ -25,8 +25,9 @@ import (
"fmt" "fmt"
"net" "net"
"go4.org/mem"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/types/key"
) )
// Magic is the 6 byte header of all discovery messages. // Magic is the 6 byte header of all discovery messages.
@ -115,19 +116,19 @@ type Ping struct {
// It shouldn't be trusted by itself, but can be combined with // It shouldn't be trusted by itself, but can be combined with
// netmap data to reduce the discokey:nodekey relation from 1:N to // netmap data to reduce the discokey:nodekey relation from 1:N to
// 1:1. // 1:1.
NodeKey tailcfg.NodeKey NodeKey key.NodePublic
} }
func (m *Ping) AppendMarshal(b []byte) []byte { func (m *Ping) AppendMarshal(b []byte) []byte {
dataLen := 12 dataLen := 12
hasKey := !m.NodeKey.IsZero() hasKey := !m.NodeKey.IsZero()
if hasKey { if hasKey {
dataLen += len(m.NodeKey) dataLen += m.NodeKey.RawLen()
} }
ret, d := appendMsgHeader(b, TypePing, v0, dataLen) ret, d := appendMsgHeader(b, TypePing, v0, dataLen)
n := copy(d, m.TxID[:]) n := copy(d, m.TxID[:])
if hasKey { if hasKey {
copy(d[n:], m.NodeKey[:]) m.NodeKey.AppendTo(d[:n])
} }
return ret return ret
} }
@ -138,8 +139,10 @@ func parsePing(ver uint8, p []byte) (m *Ping, err error) {
} }
m = new(Ping) m = new(Ping)
p = p[copy(m.TxID[:], p):] p = p[copy(m.TxID[:], p):]
if len(p) >= len(m.NodeKey) { // Deliberately lax on longer-than-expected messages, for future
copy(m.NodeKey[:], p) // compatibility.
if len(p) >= m.NodeKey.RawLen() {
m.NodeKey = key.NodePublicFromRaw32(mem.B(p[:m.NodeKey.RawLen()]))
} }
return m, nil return m, nil
} }

@ -10,8 +10,9 @@ import (
"strings" "strings"
"testing" "testing"
"go4.org/mem"
"inet.af/netaddr" "inet.af/netaddr"
"tailscale.com/tailcfg" "tailscale.com/types/key"
) )
func TestMarshalAndParse(t *testing.T) { func TestMarshalAndParse(t *testing.T) {
@ -30,13 +31,8 @@ func TestMarshalAndParse(t *testing.T) {
{ {
name: "ping_with_nodekey_src", name: "ping_with_nodekey_src",
m: &Ping{ m: &Ping{
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
NodeKey: tailcfg.NodeKey{ NodeKey: key.NodePublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
1: 1,
2: 2,
30: 30,
31: 31,
},
}, },
want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f", want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f",
}, },

@ -1936,8 +1936,8 @@ func (c *Conn) unambiguousNodeKeyOfPingLocked(dm *disco.Ping, dk key.DiscoPublic
// Pings after 1.16.0 contains its node source. See if it maps back. // Pings after 1.16.0 contains its node source. See if it maps back.
if !dm.NodeKey.IsZero() { if !dm.NodeKey.IsZero() {
if ep, ok := c.peerMap.endpointForNodeKey(dm.NodeKey); ok && ep.discoKey == dk { if ep, ok := c.peerMap.endpointForNodeKey(dm.NodeKey.AsNodeKey()); ok && ep.discoKey == dk {
return dm.NodeKey, true return dm.NodeKey.AsNodeKey(), true
} }
} }
@ -3507,7 +3507,7 @@ func (de *endpoint) sendDiscoPing(ep netaddr.IPPort, txid stun.TxID, logLevel di
selfPubKey, _ := de.c.publicKeyAtomic.Load().(tailcfg.NodeKey) selfPubKey, _ := de.c.publicKeyAtomic.Load().(tailcfg.NodeKey)
sent, _ := de.sendDiscoMessage(ep, &disco.Ping{ sent, _ := de.sendDiscoMessage(ep, &disco.Ping{
TxID: [12]byte(txid), TxID: [12]byte(txid),
NodeKey: selfPubKey, NodeKey: selfPubKey.AsNodePublic(),
}, logLevel) }, logLevel)
if !sent { if !sent {
de.forgetPing(txid) de.forgetPing(txid)

Loading…
Cancel
Save