lcm.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2018 Google, Inc. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. const (
  13. // LCMShortHeaderMagic is the LCM small message header magic number
  14. LCMShortHeaderMagic uint32 = 0x4c433032
  15. // LCMFragmentedHeaderMagic is the LCM fragmented message header magic number
  16. LCMFragmentedHeaderMagic uint32 = 0x4c433033
  17. )
  18. // LCM (Lightweight Communications and Marshalling) is a set of libraries and
  19. // tools for message passing and data marshalling, targeted at real-time systems
  20. // where high-bandwidth and low latency are critical. It provides a
  21. // publish/subscribe message passing model and automatic
  22. // marshalling/unmarshalling code generation with bindings for applications in a
  23. // variety of programming languages.
  24. //
  25. // References
  26. // https://lcm-proj.github.io/
  27. // https://github.com/lcm-proj/lcm
  28. type LCM struct {
  29. // Common (short & fragmented header) fields
  30. Magic uint32
  31. SequenceNumber uint32
  32. // Fragmented header only fields
  33. PayloadSize uint32
  34. FragmentOffset uint32
  35. FragmentNumber uint16
  36. TotalFragments uint16
  37. // Common field
  38. ChannelName string
  39. // Gopacket helper fields
  40. Fragmented bool
  41. fingerprint LCMFingerprint
  42. contents []byte
  43. payload []byte
  44. }
  45. // LCMFingerprint is the type of a LCM fingerprint.
  46. type LCMFingerprint uint64
  47. var (
  48. // lcmLayerTypes contains a map of all LCM fingerprints that we support and
  49. // their LayerType
  50. lcmLayerTypes = map[LCMFingerprint]gopacket.LayerType{}
  51. layerTypeIndex = 1001
  52. )
  53. // RegisterLCMLayerType allows users to register decoders for the underlying
  54. // LCM payload. This is done based on the fingerprint that every LCM message
  55. // contains and which identifies it uniquely. If num is not the zero value it
  56. // will be used when registering with RegisterLayerType towards gopacket,
  57. // otherwise an incremental value starting from 1001 will be used.
  58. func RegisterLCMLayerType(num int, name string, fingerprint LCMFingerprint,
  59. decoder gopacket.Decoder) gopacket.LayerType {
  60. metadata := gopacket.LayerTypeMetadata{Name: name, Decoder: decoder}
  61. if num == 0 {
  62. num = layerTypeIndex
  63. layerTypeIndex++
  64. }
  65. lcmLayerTypes[fingerprint] = gopacket.RegisterLayerType(num, metadata)
  66. return lcmLayerTypes[fingerprint]
  67. }
  68. // SupportedLCMFingerprints returns a slice of all LCM fingerprints that has
  69. // been registered so far.
  70. func SupportedLCMFingerprints() []LCMFingerprint {
  71. fingerprints := make([]LCMFingerprint, 0, len(lcmLayerTypes))
  72. for fp := range lcmLayerTypes {
  73. fingerprints = append(fingerprints, fp)
  74. }
  75. return fingerprints
  76. }
  77. // GetLCMLayerType returns the underlying LCM message's LayerType.
  78. // This LayerType has to be registered by using RegisterLCMLayerType.
  79. func GetLCMLayerType(fingerprint LCMFingerprint) gopacket.LayerType {
  80. layerType, ok := lcmLayerTypes[fingerprint]
  81. if !ok {
  82. return gopacket.LayerTypePayload
  83. }
  84. return layerType
  85. }
  86. func decodeLCM(data []byte, p gopacket.PacketBuilder) error {
  87. lcm := &LCM{}
  88. err := lcm.DecodeFromBytes(data, p)
  89. if err != nil {
  90. return err
  91. }
  92. p.AddLayer(lcm)
  93. p.SetApplicationLayer(lcm)
  94. return p.NextDecoder(lcm.NextLayerType())
  95. }
  96. // DecodeFromBytes decodes the given bytes into this layer.
  97. func (lcm *LCM) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  98. offset := 0
  99. lcm.Magic = binary.BigEndian.Uint32(data[offset:4])
  100. offset += 4
  101. if lcm.Magic != LCMShortHeaderMagic && lcm.Magic != LCMFragmentedHeaderMagic {
  102. return fmt.Errorf("Received LCM header magic %v does not match know "+
  103. "LCM magic numbers. Dropping packet.", lcm.Magic)
  104. }
  105. lcm.SequenceNumber = binary.BigEndian.Uint32(data[offset:8])
  106. offset += 4
  107. if lcm.Magic == LCMFragmentedHeaderMagic {
  108. lcm.Fragmented = true
  109. lcm.PayloadSize = binary.BigEndian.Uint32(data[offset : offset+4])
  110. offset += 4
  111. lcm.FragmentOffset = binary.BigEndian.Uint32(data[offset : offset+4])
  112. offset += 4
  113. lcm.FragmentNumber = binary.BigEndian.Uint16(data[offset : offset+2])
  114. offset += 2
  115. lcm.TotalFragments = binary.BigEndian.Uint16(data[offset : offset+2])
  116. offset += 2
  117. } else {
  118. lcm.Fragmented = false
  119. }
  120. if !lcm.Fragmented || (lcm.Fragmented && lcm.FragmentNumber == 0) {
  121. buffer := make([]byte, 0)
  122. for _, b := range data[offset:] {
  123. offset++
  124. if b == 0 {
  125. break
  126. }
  127. buffer = append(buffer, b)
  128. }
  129. lcm.ChannelName = string(buffer)
  130. }
  131. lcm.fingerprint = LCMFingerprint(
  132. binary.BigEndian.Uint64(data[offset : offset+8]))
  133. lcm.contents = data[:offset]
  134. lcm.payload = data[offset:]
  135. return nil
  136. }
  137. // CanDecode returns a set of layers that LCM objects can decode.
  138. // As LCM objects can only decode the LCM layer, we just return that layer.
  139. func (lcm LCM) CanDecode() gopacket.LayerClass {
  140. return LayerTypeLCM
  141. }
  142. // NextLayerType specifies the LCM payload layer type following this header.
  143. // As LCM packets are serialized structs with uniq fingerprints for each uniq
  144. // combination of data types, lookup of correct layer type is based on that
  145. // fingerprint.
  146. func (lcm LCM) NextLayerType() gopacket.LayerType {
  147. if !lcm.Fragmented || (lcm.Fragmented && lcm.FragmentNumber == 0) {
  148. return GetLCMLayerType(lcm.fingerprint)
  149. }
  150. return gopacket.LayerTypeFragment
  151. }
  152. // LayerType returns LayerTypeLCM
  153. func (lcm LCM) LayerType() gopacket.LayerType {
  154. return LayerTypeLCM
  155. }
  156. // LayerContents returns the contents of the LCM header.
  157. func (lcm LCM) LayerContents() []byte {
  158. return lcm.contents
  159. }
  160. // LayerPayload returns the payload following this LCM header.
  161. func (lcm LCM) LayerPayload() []byte {
  162. return lcm.payload
  163. }
  164. // Payload returns the payload following this LCM header.
  165. func (lcm LCM) Payload() []byte {
  166. return lcm.LayerPayload()
  167. }
  168. // Fingerprint returns the LCM fingerprint of the underlying message.
  169. func (lcm LCM) Fingerprint() LCMFingerprint {
  170. return lcm.fingerprint
  171. }