disco: add padding to pong message

Updates #311
This commit is contained in:
Claire Wang 2024-01-22 18:26:08 -05:00
parent 370ec6b46b
commit 528bb54aca
2 changed files with 17 additions and 3 deletions

View File

@ -227,6 +227,9 @@ func parseCallMeMaybe(ver uint8, p []byte) (m *CallMeMaybe, err error) {
type Pong struct { type Pong struct {
TxID [12]byte TxID [12]byte
Src netip.AddrPort // 18 bytes (16+2) on the wire; v4-mapped ipv6 for IPv4 Src netip.AddrPort // 18 bytes (16+2) on the wire; v4-mapped ipv6 for IPv4
// Padding is the number of 0 bytes at the end of the
// message. (It's used to probe path MTU.)
Padding int
} }
// pongLen is the length of a marshalled pong message, without the message // pongLen is the length of a marshalled pong message, without the message
@ -234,7 +237,7 @@ type Pong struct {
const pongLen = 12 + 16 + 2 const pongLen = 12 + 16 + 2
func (m *Pong) AppendMarshal(b []byte) []byte { func (m *Pong) AppendMarshal(b []byte) []byte {
ret, d := appendMsgHeader(b, TypePong, v0, pongLen) ret, d := appendMsgHeader(b, TypePong, v0, pongLen+m.Padding)
d = d[copy(d, m.TxID[:]):] d = d[copy(d, m.TxID[:]):]
ip16 := m.Src.Addr().As16() ip16 := m.Src.Addr().As16()
d = d[copy(d, ip16[:]):] d = d[copy(d, ip16[:]):]
@ -247,8 +250,10 @@ func parsePong(ver uint8, p []byte) (m *Pong, err error) {
return nil, errShort return nil, errShort
} }
m = new(Pong) m = new(Pong)
m.Padding = len(p)
copy(m.TxID[:], p) copy(m.TxID[:], p)
p = p[12:] p = p[copy(m.TxID[:], p):]
m.Padding -= pongLen
srcIP, _ := netip.AddrFromSlice(net.IP(p[:16])) srcIP, _ := netip.AddrFromSlice(net.IP(p[:16]))
p = p[16:] p = p[16:]
@ -263,7 +268,7 @@ func MessageSummary(m Message) string {
case *Ping: case *Ping:
return fmt.Sprintf("ping tx=%x padding=%v", m.TxID[:6], m.Padding) return fmt.Sprintf("ping tx=%x padding=%v", m.TxID[:6], m.Padding)
case *Pong: case *Pong:
return fmt.Sprintf("pong tx=%x", m.TxID[:6]) return fmt.Sprintf("pong tx=%x padding=%v", m.TxID[:6], m.Padding)
case *CallMeMaybe: case *CallMeMaybe:
return "call-me-maybe" return "call-me-maybe"
default: default:

View File

@ -68,6 +68,15 @@ func TestMarshalAndParse(t *testing.T) {
}, },
want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c fe d0 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1a 0a", want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c fe d0 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1a 0a",
}, },
{
name: "pong_with_padding",
m: &Pong{
TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
Src: mustIPPort("2.3.4.5:1234"),
Padding: 3,
},
want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 00 00 00 00 00 00 00 00 00 ff ff 02 03 04 05 04 d2 00 00 00",
},
{ {
name: "call_me_maybe", name: "call_me_maybe",
m: &CallMeMaybe{}, m: &CallMeMaybe{},