101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package peer
|
|
|
|
import (
|
|
"net/netip"
|
|
)
|
|
|
|
const (
|
|
PacketTypeSyn = iota + 1
|
|
PacketTypeSynAck
|
|
PacketTypeAck
|
|
PacketTypeProbe
|
|
PacketTypeAddrDiscovery
|
|
)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type PacketSyn struct {
|
|
TraceID uint64 // TraceID to match response w/ request.
|
|
//SentAt int64 // Unixmilli.
|
|
//SharedKeyType byte // Currently only 1 is supported for AES.
|
|
SharedKey [32]byte // Our shared key.
|
|
Direct bool
|
|
PossibleAddrs [8]netip.AddrPort // Possible public addresses of the sender.
|
|
}
|
|
|
|
func (p PacketSyn) Marshal(buf []byte) []byte {
|
|
return newBinWriter(buf).
|
|
Byte(PacketTypeSyn).
|
|
Uint64(p.TraceID).
|
|
//Int64(p.SentAt).
|
|
//Byte(p.SharedKeyType).
|
|
SharedKey(p.SharedKey).
|
|
Bool(p.Direct).
|
|
AddrPort8(p.PossibleAddrs).
|
|
Build()
|
|
}
|
|
|
|
func ParsePacketSyn(buf []byte) (p PacketSyn, err error) {
|
|
err = newBinReader(buf[1:]).
|
|
Uint64(&p.TraceID).
|
|
//Int64(&p.SentAt).
|
|
//Byte(&p.SharedKeyType).
|
|
SharedKey(&p.SharedKey).
|
|
Bool(&p.Direct).
|
|
AddrPort8(&p.PossibleAddrs).
|
|
Error()
|
|
return
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type PacketAck struct {
|
|
TraceID uint64
|
|
ToAddr netip.AddrPort
|
|
PossibleAddrs [8]netip.AddrPort // Possible public addresses of the sender.
|
|
}
|
|
|
|
func (p PacketAck) Marshal(buf []byte) []byte {
|
|
return newBinWriter(buf).
|
|
Byte(PacketTypeAck).
|
|
Uint64(p.TraceID).
|
|
AddrPort(p.ToAddr).
|
|
AddrPort8(p.PossibleAddrs).
|
|
Build()
|
|
}
|
|
|
|
func ParsePacketAck(buf []byte) (p PacketAck, err error) {
|
|
err = newBinReader(buf[1:]).
|
|
Uint64(&p.TraceID).
|
|
AddrPort(&p.ToAddr).
|
|
AddrPort8(&p.PossibleAddrs).
|
|
Error()
|
|
return
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A probeReqPacket is sent from a client to a server to determine if direct
|
|
// UDP communication can be used.
|
|
type PacketProbe struct {
|
|
TraceID uint64
|
|
}
|
|
|
|
func (p PacketProbe) Marshal(buf []byte) []byte {
|
|
return newBinWriter(buf).
|
|
Byte(PacketTypeProbe).
|
|
Uint64(p.TraceID).
|
|
Build()
|
|
}
|
|
|
|
func ParsePacketProbe(buf []byte) (p PacketProbe, err error) {
|
|
err = newBinReader(buf[1:]).
|
|
Uint64(&p.TraceID).
|
|
Error()
|
|
return
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
type PacketLocalDiscovery struct{}
|