vppn/peer/router-types.go
2024-12-12 21:11:17 +01:00

75 lines
1.7 KiB
Go

package peer
import (
"net/netip"
"unsafe"
"vppn/m"
)
// ----------------------------------------------------------------------------
// A route is used by a peer to send or receive packets. A peer won't send
// packets on a route that isn't up, but will process incoming packets on
// that route.
type route struct {
PeerIP byte
Up bool
Mediator bool
SignPubKey []byte
EncSharedKey []byte // Shared key for encoding / decoding packets.
Addr netip.AddrPort // Address to send to.
ViaIP byte // If != 0, this is a forwarding address.
}
type peerUpdate struct {
PeerIP byte
*m.Peer // nil => delete.
}
// ----------------------------------------------------------------------------
// Wrapper for routing packets.
type wrapper[T any] struct {
T T
Nonce Nonce
SrcAddr netip.AddrPort
}
func newWrapper[T any](srcAddr netip.AddrPort, nonce Nonce) wrapper[T] {
return wrapper[T]{
SrcAddr: srcAddr,
Nonce: nonce,
}
}
// ----------------------------------------------------------------------------
type Ping struct {
SentAt int64 // unix milli
}
func (p *Ping) Parse(buf []byte) {
p.SentAt = *(*int64)(unsafe.Pointer(&buf[0]))
}
func (p Ping) Marshal(buf []byte) {
*(*int64)(unsafe.Pointer(&buf[0])) = p.SentAt
}
// ----------------------------------------------------------------------------
type Pong struct {
SentAt int64 // unix mili
RecvdAt int64 // unix mili
}
func (p *Pong) Parse(buf []byte) {
p.SentAt = *(*int64)(unsafe.Pointer(&buf[0]))
p.RecvdAt = *(*int64)(unsafe.Pointer(&buf[8]))
}
func (p *Pong) Marshal(buf []byte) {
*(*int64)(unsafe.Pointer(&buf[0])) = p.SentAt
*(*int64)(unsafe.Pointer(&buf[8])) = p.RecvdAt
}