vppn/node/header.go
2024-12-19 20:53:52 +01:00

69 lines
1.4 KiB
Go

package node
import "unsafe"
// ----------------------------------------------------------------------------
const (
routingStreamID = 2
routingHeaderSize = 24
routingCipherOverhead = 16
dataStreamID = 1
dataHeaderSize = 12
dataCipherOverhead = 16
)
// TODO: Rename
type xHeader struct {
Counter uint64 // Init with fasttime.Now() << 30 to ensure monotonic.
SourceIP byte
DestIP byte
}
func (h *xHeader) Parse(b []byte) {
h.Counter = *(*uint64)(unsafe.Pointer(&b[1]))
h.SourceIP = b[9]
h.DestIP = b[10]
}
func (h *xHeader) Marshal(streamID byte, buf []byte) {
buf[0] = streamID
*(*uint64)(unsafe.Pointer(&buf[1])) = h.Counter
buf[9] = h.SourceIP
buf[10] = h.DestIP
buf[11] = 0
}
// ----------------------------------------------------------------------------
// TODO: Remove this code.
const (
headerSize = 24
streamData = 1
streamRouting = 2
)
type header struct {
Counter uint64 // Init with fasttime.Now() << 30 to ensure monotonic.
SourceIP byte
DestIP byte
Forward byte
Stream byte // See stream* constants.
}
func (hdr *header) Parse(nb []byte) {
hdr.Counter = *(*uint64)(unsafe.Pointer(&nb[0]))
hdr.SourceIP = nb[8]
hdr.DestIP = nb[9]
hdr.Forward = nb[10]
hdr.Stream = nb[11]
}
func (hdr header) Marshal(buf []byte) {
*(*uint64)(unsafe.Pointer(&buf[0])) = hdr.Counter
buf[8] = hdr.SourceIP
buf[9] = hdr.DestIP
buf[10] = hdr.Forward
buf[11] = hdr.Stream
}