33 lines
792 B
Go
33 lines
792 B
Go
package node
|
|
|
|
import "unsafe"
|
|
|
|
const headerSize = 24
|
|
|
|
type header struct {
|
|
Counter uint64 // Init with fasttime.Now() << 30 to ensure monotonic.
|
|
SourceIP byte
|
|
ViaIP byte
|
|
DestIP byte
|
|
PacketType byte // The packet type. See PACKET_* constants.
|
|
DataSize uint16 // Data size following associated data.
|
|
}
|
|
|
|
func (hdr *header) Parse(nb []byte) {
|
|
hdr.Counter = *(*uint64)(unsafe.Pointer(&nb[0]))
|
|
hdr.SourceIP = nb[8]
|
|
hdr.ViaIP = nb[9]
|
|
hdr.DestIP = nb[10]
|
|
hdr.PacketType = nb[11]
|
|
hdr.DataSize = *(*uint16)(unsafe.Pointer(&nb[12]))
|
|
}
|
|
|
|
func (hdr header) Marshal(buf []byte) {
|
|
*(*uint64)(unsafe.Pointer(&buf[0])) = hdr.Counter
|
|
buf[8] = hdr.SourceIP
|
|
buf[9] = hdr.ViaIP
|
|
buf[10] = hdr.DestIP
|
|
buf[11] = hdr.PacketType
|
|
*(*uint16)(unsafe.Pointer(&buf[12])) = hdr.DataSize
|
|
}
|