1
0

header.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package header
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. )
  8. //Version 1 header:
  9. // 0 31
  10. // |-----------------------------------------------------------------------|
  11. // | Version (uint4) | Type (uint4) | Subtype (uint8) | Reserved (uint16) | 32
  12. // |-----------------------------------------------------------------------|
  13. // | Remote index (uint32) | 64
  14. // |-----------------------------------------------------------------------|
  15. // | Message counter | 96
  16. // | (uint64) | 128
  17. // |-----------------------------------------------------------------------|
  18. // | payload... |
  19. type m = map[string]any
  20. const (
  21. Version uint8 = 1
  22. Len = 16
  23. // MaxOverhead is the most a payload grows on its way to the underlay.
  24. // A relayed packet has two headers and two AEAD tags, one for the inner tunnel and another for the relay tunnel.
  25. // Every supported cipher (today) has a 16 byte tag.
  26. MaxOverhead = 2 * (Len + 16)
  27. )
  28. type MessageType uint8
  29. type MessageSubType uint8
  30. const (
  31. Handshake MessageType = 0
  32. Message MessageType = 1
  33. RecvError MessageType = 2
  34. LightHouse MessageType = 3
  35. Test MessageType = 4
  36. CloseTunnel MessageType = 5
  37. Control MessageType = 6
  38. )
  39. var typeMap = map[MessageType]string{
  40. Handshake: "handshake",
  41. Message: "message",
  42. RecvError: "recvError",
  43. LightHouse: "lightHouse",
  44. Test: "test",
  45. CloseTunnel: "closeTunnel",
  46. Control: "control",
  47. }
  48. const (
  49. MessageNone MessageSubType = 0
  50. MessageRelay MessageSubType = 1
  51. )
  52. const (
  53. TestRequest MessageSubType = 0
  54. TestReply MessageSubType = 1
  55. )
  56. const (
  57. HandshakeIXPSK0 MessageSubType = 0
  58. HandshakeXXPSK0 MessageSubType = 1
  59. )
  60. var ErrHeaderTooShort = errors.New("header is too short")
  61. var subTypeTestMap = map[MessageSubType]string{
  62. TestRequest: "testRequest",
  63. TestReply: "testReply",
  64. }
  65. var subTypeNoneMap = map[MessageSubType]string{0: "none"}
  66. var subTypeMap = map[MessageType]*map[MessageSubType]string{
  67. Message: {
  68. MessageNone: "none",
  69. MessageRelay: "relay",
  70. },
  71. RecvError: &subTypeNoneMap,
  72. LightHouse: &subTypeNoneMap,
  73. Test: &subTypeTestMap,
  74. CloseTunnel: &subTypeNoneMap,
  75. Handshake: {
  76. HandshakeIXPSK0: "ix_psk0",
  77. },
  78. Control: &subTypeNoneMap,
  79. }
  80. type H struct {
  81. Version uint8
  82. Type MessageType
  83. Subtype MessageSubType
  84. Reserved uint16
  85. RemoteIndex uint32
  86. MessageCounter uint64
  87. }
  88. // Encode uses the provided byte array to encode the provided header values into.
  89. // Byte array must be capped higher than HeaderLen or this will panic
  90. func Encode(b []byte, v uint8, t MessageType, st MessageSubType, ri uint32, c uint64) []byte {
  91. b = b[:Len]
  92. b[0] = v<<4 | byte(t&0x0f)
  93. b[1] = byte(st)
  94. binary.BigEndian.PutUint16(b[2:4], 0)
  95. binary.BigEndian.PutUint32(b[4:8], ri)
  96. binary.BigEndian.PutUint64(b[8:16], c)
  97. return b
  98. }
  99. // String creates a readable string representation of a header
  100. func (h *H) String() string {
  101. if h == nil {
  102. return "<nil>"
  103. }
  104. return fmt.Sprintf("ver=%d type=%s subtype=%s reserved=%#x remoteindex=%v messagecounter=%v",
  105. h.Version, h.TypeName(), h.SubTypeName(), h.Reserved, h.RemoteIndex, h.MessageCounter)
  106. }
  107. // MarshalJSON creates a json string representation of a header
  108. func (h *H) MarshalJSON() ([]byte, error) {
  109. return json.Marshal(m{
  110. "version": h.Version,
  111. "type": h.TypeName(),
  112. "subType": h.SubTypeName(),
  113. "reserved": h.Reserved,
  114. "remoteIndex": h.RemoteIndex,
  115. "messageCounter": h.MessageCounter,
  116. })
  117. }
  118. // Encode turns header into bytes
  119. func (h *H) Encode(b []byte) ([]byte, error) {
  120. if h == nil {
  121. return nil, errors.New("nil header")
  122. }
  123. return Encode(b, h.Version, h.Type, h.Subtype, h.RemoteIndex, h.MessageCounter), nil
  124. }
  125. // Parse is a helper function to parses given bytes into new Header struct
  126. func (h *H) Parse(b []byte) error {
  127. if len(b) < Len {
  128. return ErrHeaderTooShort
  129. }
  130. // get upper 4 bytes
  131. h.Version = uint8((b[0] >> 4) & 0x0f)
  132. // get lower 4 bytes
  133. h.Type = MessageType(b[0] & 0x0f)
  134. h.Subtype = MessageSubType(b[1])
  135. h.Reserved = binary.BigEndian.Uint16(b[2:4])
  136. h.RemoteIndex = binary.BigEndian.Uint32(b[4:8])
  137. h.MessageCounter = binary.BigEndian.Uint64(b[8:16])
  138. return nil
  139. }
  140. // TypeName will transform the headers message type into a human string
  141. func (h *H) TypeName() string {
  142. return TypeName(h.Type)
  143. }
  144. // TypeName will transform a nebula message type into a human string
  145. func TypeName(t MessageType) string {
  146. if n, ok := typeMap[t]; ok {
  147. return n
  148. }
  149. return "unknown"
  150. }
  151. // SubTypeName will transform the headers message sub type into a human string
  152. func (h *H) SubTypeName() string {
  153. return SubTypeName(h.Type, h.Subtype)
  154. }
  155. func (h *H) IsValidSubType() bool {
  156. return IsValidSubType(h.Type, h.Subtype)
  157. }
  158. // SubTypeName will transform a nebula message sub type into a human string
  159. func SubTypeName(t MessageType, s MessageSubType) string {
  160. if n, ok := subTypeMap[t]; ok {
  161. if x, ok := (*n)[s]; ok {
  162. return x
  163. }
  164. }
  165. return "unknown"
  166. }
  167. func IsValidSubType(t MessageType, s MessageSubType) bool {
  168. switch t {
  169. case Message:
  170. return s == MessageNone || s == MessageRelay
  171. case Handshake:
  172. return s == HandshakeIXPSK0
  173. case Test:
  174. return s == TestReply || s == TestRequest
  175. case Control, CloseTunnel, RecvError, LightHouse:
  176. return s == 0
  177. default:
  178. return false
  179. }
  180. }
  181. // NewHeader turns bytes into a header
  182. func NewHeader(b []byte) (*H, error) {
  183. h := new(H)
  184. if err := h.Parse(b); err != nil {
  185. return nil, err
  186. }
  187. return h, nil
  188. }