parser.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2012 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 gopacket
  7. import (
  8. "fmt"
  9. )
  10. // DecodingLayer is an interface for packet layers that can decode themselves.
  11. //
  12. // The important part of DecodingLayer is that they decode themselves in-place.
  13. // Calling DecodeFromBytes on a DecodingLayer totally resets the entire layer to
  14. // the new state defined by the data passed in. A returned error leaves the
  15. // DecodingLayer in an unknown intermediate state, thus its fields should not be
  16. // trusted.
  17. //
  18. // Because the DecodingLayer is resetting its own fields, a call to
  19. // DecodeFromBytes should normally not require any memory allocation.
  20. type DecodingLayer interface {
  21. // DecodeFromBytes resets the internal state of this layer to the state
  22. // defined by the passed-in bytes. Slices in the DecodingLayer may
  23. // reference the passed-in data, so care should be taken to copy it
  24. // first should later modification of data be required before the
  25. // DecodingLayer is discarded.
  26. DecodeFromBytes(data []byte, df DecodeFeedback) error
  27. // CanDecode returns the set of LayerTypes this DecodingLayer can
  28. // decode. For Layers that are also DecodingLayers, this will most
  29. // often be that Layer's LayerType().
  30. CanDecode() LayerClass
  31. // NextLayerType returns the LayerType which should be used to decode
  32. // the LayerPayload.
  33. NextLayerType() LayerType
  34. // LayerPayload is the set of bytes remaining to decode after a call to
  35. // DecodeFromBytes.
  36. LayerPayload() []byte
  37. }
  38. // DecodingLayerParser parses a given set of layer types. See DecodeLayers for
  39. // more information on how DecodingLayerParser should be used.
  40. type DecodingLayerParser struct {
  41. // DecodingLayerParserOptions is the set of options available to the
  42. // user to define the parser's behavior.
  43. DecodingLayerParserOptions
  44. first LayerType
  45. decoders map[LayerType]DecodingLayer
  46. df DecodeFeedback
  47. // Truncated is set when a decode layer detects that the packet has been
  48. // truncated.
  49. Truncated bool
  50. }
  51. // AddDecodingLayer adds a decoding layer to the parser. This adds support for
  52. // the decoding layer's CanDecode layers to the parser... should they be
  53. // encountered, they'll be parsed.
  54. func (l *DecodingLayerParser) AddDecodingLayer(d DecodingLayer) {
  55. for _, typ := range d.CanDecode().LayerTypes() {
  56. l.decoders[typ] = d
  57. }
  58. }
  59. // SetTruncated is used by DecodingLayers to set the Truncated boolean in the
  60. // DecodingLayerParser. Users should simply read Truncated after calling
  61. // DecodeLayers.
  62. func (l *DecodingLayerParser) SetTruncated() {
  63. l.Truncated = true
  64. }
  65. // NewDecodingLayerParser creates a new DecodingLayerParser and adds in all
  66. // of the given DecodingLayers with AddDecodingLayer.
  67. //
  68. // Each call to DecodeLayers will attempt to decode the given bytes first by
  69. // treating them as a 'first'-type layer, then by using NextLayerType on
  70. // subsequently decoded layers to find the next relevant decoder. Should a
  71. // deoder not be available for the layer type returned by NextLayerType,
  72. // decoding will stop.
  73. func NewDecodingLayerParser(first LayerType, decoders ...DecodingLayer) *DecodingLayerParser {
  74. dlp := &DecodingLayerParser{
  75. decoders: make(map[LayerType]DecodingLayer),
  76. first: first,
  77. }
  78. dlp.df = dlp // Cast this once to the interface
  79. for _, d := range decoders {
  80. dlp.AddDecodingLayer(d)
  81. }
  82. return dlp
  83. }
  84. // DecodeLayers decodes as many layers as possible from the given data. It
  85. // initially treats the data as layer type 'typ', then uses NextLayerType on
  86. // each subsequent decoded layer until it gets to a layer type it doesn't know
  87. // how to parse.
  88. //
  89. // For each layer successfully decoded, DecodeLayers appends the layer type to
  90. // the decoded slice. DecodeLayers truncates the 'decoded' slice initially, so
  91. // there's no need to empty it yourself.
  92. //
  93. // This decoding method is about an order of magnitude faster than packet
  94. // decoding, because it only decodes known layers that have already been
  95. // allocated. This means it doesn't need to allocate each layer it returns...
  96. // instead it overwrites the layers that already exist.
  97. //
  98. // Example usage:
  99. // func main() {
  100. // var eth layers.Ethernet
  101. // var ip4 layers.IPv4
  102. // var ip6 layers.IPv6
  103. // var tcp layers.TCP
  104. // var udp layers.UDP
  105. // var payload gopacket.Payload
  106. // parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &eth, &ip4, &ip6, &tcp, &udp, &payload)
  107. // var source gopacket.PacketDataSource = getMyDataSource()
  108. // decodedLayers := make([]gopacket.LayerType, 0, 10)
  109. // for {
  110. // data, _, err := source.ReadPacketData()
  111. // if err != nil {
  112. // fmt.Println("Error reading packet data: ", err)
  113. // continue
  114. // }
  115. // fmt.Println("Decoding packet")
  116. // err = parser.DecodeLayers(data, &decodedLayers)
  117. // for _, typ := range decodedLayers {
  118. // fmt.Println(" Successfully decoded layer type", typ)
  119. // switch typ {
  120. // case layers.LayerTypeEthernet:
  121. // fmt.Println(" Eth ", eth.SrcMAC, eth.DstMAC)
  122. // case layers.LayerTypeIPv4:
  123. // fmt.Println(" IP4 ", ip4.SrcIP, ip4.DstIP)
  124. // case layers.LayerTypeIPv6:
  125. // fmt.Println(" IP6 ", ip6.SrcIP, ip6.DstIP)
  126. // case layers.LayerTypeTCP:
  127. // fmt.Println(" TCP ", tcp.SrcPort, tcp.DstPort)
  128. // case layers.LayerTypeUDP:
  129. // fmt.Println(" UDP ", udp.SrcPort, udp.DstPort)
  130. // }
  131. // }
  132. // if decodedLayers.Truncated {
  133. // fmt.Println(" Packet has been truncated")
  134. // }
  135. // if err != nil {
  136. // fmt.Println(" Error encountered:", err)
  137. // }
  138. // }
  139. // }
  140. //
  141. // If DecodeLayers is unable to decode the next layer type, it will return the
  142. // error UnsupportedLayerType.
  143. func (l *DecodingLayerParser) DecodeLayers(data []byte, decoded *[]LayerType) (err error) {
  144. l.Truncated = false
  145. if !l.IgnorePanic {
  146. defer panicToError(&err)
  147. }
  148. typ := l.first
  149. *decoded = (*decoded)[:0] // Truncated decoded layers.
  150. for len(data) > 0 {
  151. decoder, ok := l.decoders[typ]
  152. if !ok {
  153. if l.IgnoreUnsupported {
  154. return nil
  155. }
  156. return UnsupportedLayerType(typ)
  157. } else if err = decoder.DecodeFromBytes(data, l.df); err != nil {
  158. return err
  159. }
  160. *decoded = append(*decoded, typ)
  161. typ = decoder.NextLayerType()
  162. data = decoder.LayerPayload()
  163. }
  164. return nil
  165. }
  166. // UnsupportedLayerType is returned by DecodingLayerParser if DecodeLayers
  167. // encounters a layer type that the DecodingLayerParser has no decoder for.
  168. type UnsupportedLayerType LayerType
  169. // Error implements the error interface, returning a string to say that the
  170. // given layer type is unsupported.
  171. func (e UnsupportedLayerType) Error() string {
  172. return fmt.Sprintf("No decoder for layer type %v", LayerType(e))
  173. }
  174. func panicToError(e *error) {
  175. if r := recover(); r != nil {
  176. *e = fmt.Errorf("panic: %v", r)
  177. }
  178. }
  179. // DecodingLayerParserOptions provides options to affect the behavior of a given
  180. // DecodingLayerParser.
  181. type DecodingLayerParserOptions struct {
  182. // IgnorePanic determines whether a DecodingLayerParser should stop
  183. // panics on its own (by returning them as an error from DecodeLayers)
  184. // or should allow them to raise up the stack. Handling errors does add
  185. // latency to the process of decoding layers, but is much safer for
  186. // callers. IgnorePanic defaults to false, thus if the caller does
  187. // nothing decode panics will be returned as errors.
  188. IgnorePanic bool
  189. // IgnoreUnsupported will stop parsing and return a nil error when it
  190. // encounters a layer it doesn't have a parser for, instead of returning an
  191. // UnsupportedLayerType error. If this is true, it's up to the caller to make
  192. // sure that all expected layers have been parsed (by checking the decoded
  193. // slice).
  194. IgnoreUnsupported bool
  195. }