2021-11-03 19:54:04 -06:00
|
|
|
package header
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Version 1 header:
|
|
|
|
// 0 31
|
|
|
|
// |-----------------------------------------------------------------------|
|
|
|
|
// | Version (uint4) | Type (uint4) | Subtype (uint8) | Reserved (uint16) | 32
|
|
|
|
// |-----------------------------------------------------------------------|
|
|
|
|
// | Remote index (uint32) | 64
|
|
|
|
// |-----------------------------------------------------------------------|
|
|
|
|
// | Message counter | 96
|
|
|
|
// | (uint64) | 128
|
|
|
|
// |-----------------------------------------------------------------------|
|
|
|
|
// | payload... |
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
type m map[string]interface{}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
const (
|
2021-11-03 19:54:04 -06:00
|
|
|
Version uint8 = 1
|
|
|
|
Len = 16
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
type MessageType uint8
|
|
|
|
type MessageSubType uint8
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
const (
|
2021-11-03 19:54:04 -06:00
|
|
|
Handshake MessageType = 0
|
|
|
|
Message MessageType = 1
|
|
|
|
RecvError MessageType = 2
|
|
|
|
LightHouse MessageType = 3
|
|
|
|
Test MessageType = 4
|
|
|
|
CloseTunnel MessageType = 5
|
2022-06-21 12:35:23 -06:00
|
|
|
Control MessageType = 6
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var typeMap = map[MessageType]string{
|
|
|
|
Handshake: "handshake",
|
|
|
|
Message: "message",
|
|
|
|
RecvError: "recvError",
|
|
|
|
LightHouse: "lightHouse",
|
|
|
|
Test: "test",
|
|
|
|
CloseTunnel: "closeTunnel",
|
2022-06-27 11:33:29 -06:00
|
|
|
Control: "control",
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2022-06-21 12:35:23 -06:00
|
|
|
const (
|
|
|
|
MessageNone MessageSubType = 0
|
|
|
|
MessageRelay MessageSubType = 1
|
|
|
|
)
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
const (
|
2021-11-03 19:54:04 -06:00
|
|
|
TestRequest MessageSubType = 0
|
|
|
|
TestReply MessageSubType = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
HandshakeIXPSK0 MessageSubType = 0
|
|
|
|
HandshakeXXPSK0 MessageSubType = 1
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var ErrHeaderTooShort = errors.New("header is too short")
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var subTypeTestMap = map[MessageSubType]string{
|
|
|
|
TestRequest: "testRequest",
|
|
|
|
TestReply: "testReply",
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var subTypeNoneMap = map[MessageSubType]string{0: "none"}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
var subTypeMap = map[MessageType]*map[MessageSubType]string{
|
2022-06-27 11:33:29 -06:00
|
|
|
Message: {
|
|
|
|
MessageNone: "none",
|
|
|
|
MessageRelay: "relay",
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
RecvError: &subTypeNoneMap,
|
|
|
|
LightHouse: &subTypeNoneMap,
|
|
|
|
Test: &subTypeTestMap,
|
|
|
|
CloseTunnel: &subTypeNoneMap,
|
|
|
|
Handshake: {
|
|
|
|
HandshakeIXPSK0: "ix_psk0",
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
2022-06-21 12:35:23 -06:00
|
|
|
Control: &subTypeNoneMap,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
type H struct {
|
2019-11-19 10:00:20 -07:00
|
|
|
Version uint8
|
2021-11-03 19:54:04 -06:00
|
|
|
Type MessageType
|
|
|
|
Subtype MessageSubType
|
2019-11-19 10:00:20 -07:00
|
|
|
Reserved uint16
|
|
|
|
RemoteIndex uint32
|
|
|
|
MessageCounter uint64
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
// Encode uses the provided byte array to encode the provided header values into.
|
2019-11-19 10:00:20 -07:00
|
|
|
// Byte array must be capped higher than HeaderLen or this will panic
|
2021-11-03 19:54:04 -06:00
|
|
|
func Encode(b []byte, v uint8, t MessageType, st MessageSubType, ri uint32, c uint64) []byte {
|
|
|
|
b = b[:Len]
|
|
|
|
b[0] = v<<4 | byte(t&0x0f)
|
2019-11-19 10:00:20 -07:00
|
|
|
b[1] = byte(st)
|
|
|
|
binary.BigEndian.PutUint16(b[2:4], 0)
|
|
|
|
binary.BigEndian.PutUint32(b[4:8], ri)
|
|
|
|
binary.BigEndian.PutUint64(b[8:16], c)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// String creates a readable string representation of a header
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) String() string {
|
2019-11-19 10:00:20 -07:00
|
|
|
if h == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("ver=%d type=%s subtype=%s reserved=%#x remoteindex=%v messagecounter=%v",
|
|
|
|
h.Version, h.TypeName(), h.SubTypeName(), h.Reserved, h.RemoteIndex, h.MessageCounter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON creates a json string representation of a header
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) MarshalJSON() ([]byte, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
return json.Marshal(m{
|
|
|
|
"version": h.Version,
|
|
|
|
"type": h.TypeName(),
|
|
|
|
"subType": h.SubTypeName(),
|
|
|
|
"reserved": h.Reserved,
|
|
|
|
"remoteIndex": h.RemoteIndex,
|
|
|
|
"messageCounter": h.MessageCounter,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode turns header into bytes
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) Encode(b []byte) ([]byte, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if h == nil {
|
|
|
|
return nil, errors.New("nil header")
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
return Encode(b, h.Version, h.Type, h.Subtype, h.RemoteIndex, h.MessageCounter), nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse is a helper function to parses given bytes into new Header struct
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) Parse(b []byte) error {
|
|
|
|
if len(b) < Len {
|
|
|
|
return ErrHeaderTooShort
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
// get upper 4 bytes
|
|
|
|
h.Version = uint8((b[0] >> 4) & 0x0f)
|
|
|
|
// get lower 4 bytes
|
2021-11-03 19:54:04 -06:00
|
|
|
h.Type = MessageType(b[0] & 0x0f)
|
|
|
|
h.Subtype = MessageSubType(b[1])
|
2019-11-19 10:00:20 -07:00
|
|
|
h.Reserved = binary.BigEndian.Uint16(b[2:4])
|
|
|
|
h.RemoteIndex = binary.BigEndian.Uint32(b[4:8])
|
|
|
|
h.MessageCounter = binary.BigEndian.Uint64(b[8:16])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TypeName will transform the headers message type into a human string
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) TypeName() string {
|
2019-11-19 10:00:20 -07:00
|
|
|
return TypeName(h.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TypeName will transform a nebula message type into a human string
|
2021-11-03 19:54:04 -06:00
|
|
|
func TypeName(t MessageType) string {
|
2019-11-19 10:00:20 -07:00
|
|
|
if n, ok := typeMap[t]; ok {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubTypeName will transform the headers message sub type into a human string
|
2021-11-03 19:54:04 -06:00
|
|
|
func (h *H) SubTypeName() string {
|
2019-11-19 10:00:20 -07:00
|
|
|
return SubTypeName(h.Type, h.Subtype)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubTypeName will transform a nebula message sub type into a human string
|
2021-11-03 19:54:04 -06:00
|
|
|
func SubTypeName(t MessageType, s MessageSubType) string {
|
2019-11-19 10:00:20 -07:00
|
|
|
if n, ok := subTypeMap[t]; ok {
|
|
|
|
if x, ok := (*n)[s]; ok {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHeader turns bytes into a header
|
2021-11-03 19:54:04 -06:00
|
|
|
func NewHeader(b []byte) (*H, error) {
|
|
|
|
h := new(H)
|
2019-11-19 10:00:20 -07:00
|
|
|
if err := h.Parse(b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return h, nil
|
|
|
|
}
|