mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-09 08:01:31 +00:00
disco: move disco pcap helper to disco package
Updates tailscale/corp#13464 Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:

committed by
Dave Anderson

parent
cde37f5307
commit
9a76deb4b0
40
disco/pcap.go
Normal file
40
disco/pcap.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package disco
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net/netip"
|
||||
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/types/key"
|
||||
)
|
||||
|
||||
// ToPCAPFrame marshals the bytes for a pcap record that describe a disco frame.
|
||||
//
|
||||
// Warning: Alloc garbage. Acceptable while capturing.
|
||||
func ToPCAPFrame(src netip.AddrPort, derpNodeSrc key.NodePublic, payload []byte) []byte {
|
||||
var (
|
||||
b bytes.Buffer
|
||||
flag uint8
|
||||
)
|
||||
b.Grow(128) // Most disco frames will probably be smaller than this.
|
||||
|
||||
if src.Addr() == tailcfg.DerpMagicIPAddr {
|
||||
flag |= 0x01
|
||||
}
|
||||
b.WriteByte(flag) // 1b: flag
|
||||
|
||||
derpSrc := derpNodeSrc.Raw32()
|
||||
b.Write(derpSrc[:]) // 32b: derp public key
|
||||
binary.Write(&b, binary.LittleEndian, uint16(src.Port())) // 2b: port
|
||||
addr, _ := src.Addr().MarshalBinary()
|
||||
binary.Write(&b, binary.LittleEndian, uint16(len(addr))) // 2b: len(addr)
|
||||
b.Write(addr) // Xb: addr
|
||||
binary.Write(&b, binary.LittleEndian, uint16(len(payload))) // 2b: len(payload)
|
||||
b.Write(payload) // Xb: payload
|
||||
|
||||
return b.Bytes()
|
||||
}
|
Reference in New Issue
Block a user