decode.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "errors"
  9. )
  10. // DecodeFeedback is used by DecodingLayer layers to provide decoding metadata.
  11. type DecodeFeedback interface {
  12. // SetTruncated should be called if during decoding you notice that a packet
  13. // is shorter than internal layer variables (HeaderLength, or the like) say it
  14. // should be. It sets packet.Metadata().Truncated.
  15. SetTruncated()
  16. }
  17. type nilDecodeFeedback struct{}
  18. func (nilDecodeFeedback) SetTruncated() {}
  19. // NilDecodeFeedback implements DecodeFeedback by doing nothing.
  20. var NilDecodeFeedback DecodeFeedback = nilDecodeFeedback{}
  21. // PacketBuilder is used by layer decoders to store the layers they've decoded,
  22. // and to defer future decoding via NextDecoder.
  23. // Typically, the pattern for use is:
  24. // func (m *myDecoder) Decode(data []byte, p PacketBuilder) error {
  25. // if myLayer, err := myDecodingLogic(data); err != nil {
  26. // return err
  27. // } else {
  28. // p.AddLayer(myLayer)
  29. // }
  30. // // maybe do this, if myLayer is a LinkLayer
  31. // p.SetLinkLayer(myLayer)
  32. // return p.NextDecoder(nextDecoder)
  33. // }
  34. type PacketBuilder interface {
  35. DecodeFeedback
  36. // AddLayer should be called by a decoder immediately upon successful
  37. // decoding of a layer.
  38. AddLayer(l Layer)
  39. // The following functions set the various specific layers in the final
  40. // packet. Note that if many layers call SetX, the first call is kept and all
  41. // other calls are ignored.
  42. SetLinkLayer(LinkLayer)
  43. SetNetworkLayer(NetworkLayer)
  44. SetTransportLayer(TransportLayer)
  45. SetApplicationLayer(ApplicationLayer)
  46. SetErrorLayer(ErrorLayer)
  47. // NextDecoder should be called by a decoder when they're done decoding a
  48. // packet layer but not done with decoding the entire packet. The next
  49. // decoder will be called to decode the last AddLayer's LayerPayload.
  50. // Because of this, NextDecoder must only be called once all other
  51. // PacketBuilder calls have been made. Set*Layer and AddLayer calls after
  52. // NextDecoder calls will behave incorrectly.
  53. NextDecoder(next Decoder) error
  54. // DumpPacketData is used solely for decoding. If you come across an error
  55. // you need to diagnose while processing a packet, call this and your packet's
  56. // data will be dumped to stderr so you can create a test. This should never
  57. // be called from a production decoder.
  58. DumpPacketData()
  59. // DecodeOptions returns the decode options
  60. DecodeOptions() *DecodeOptions
  61. }
  62. // Decoder is an interface for logic to decode a packet layer. Users may
  63. // implement a Decoder to handle their own strange packet types, or may use one
  64. // of the many decoders available in the 'layers' subpackage to decode things
  65. // for them.
  66. type Decoder interface {
  67. // Decode decodes the bytes of a packet, sending decoded values and other
  68. // information to PacketBuilder, and returning an error if unsuccessful. See
  69. // the PacketBuilder documentation for more details.
  70. Decode([]byte, PacketBuilder) error
  71. }
  72. // DecodeFunc wraps a function to make it a Decoder.
  73. type DecodeFunc func([]byte, PacketBuilder) error
  74. // Decode implements Decoder by calling itself.
  75. func (d DecodeFunc) Decode(data []byte, p PacketBuilder) error {
  76. // function, call thyself.
  77. return d(data, p)
  78. }
  79. // DecodePayload is a Decoder that returns a Payload layer containing all
  80. // remaining bytes.
  81. var DecodePayload Decoder = DecodeFunc(decodePayload)
  82. // DecodeUnknown is a Decoder that returns an Unknown layer containing all
  83. // remaining bytes, useful if you run up against a layer that you're unable to
  84. // decode yet. This layer is considered an ErrorLayer.
  85. var DecodeUnknown Decoder = DecodeFunc(decodeUnknown)
  86. // DecodeFragment is a Decoder that returns a Fragment layer containing all
  87. // remaining bytes.
  88. var DecodeFragment Decoder = DecodeFunc(decodeFragment)
  89. // LayerTypeZero is an invalid layer type, but can be used to determine whether
  90. // layer type has actually been set correctly.
  91. var LayerTypeZero = RegisterLayerType(0, LayerTypeMetadata{Name: "Unknown", Decoder: DecodeUnknown})
  92. // LayerTypeDecodeFailure is the layer type for the default error layer.
  93. var LayerTypeDecodeFailure = RegisterLayerType(1, LayerTypeMetadata{Name: "DecodeFailure", Decoder: DecodeUnknown})
  94. // LayerTypePayload is the layer type for a payload that we don't try to decode
  95. // but treat as a success, IE: an application-level payload.
  96. var LayerTypePayload = RegisterLayerType(2, LayerTypeMetadata{Name: "Payload", Decoder: DecodePayload})
  97. // LayerTypeFragment is the layer type for a fragment of a layer transported
  98. // by an underlying layer that supports fragmentation.
  99. var LayerTypeFragment = RegisterLayerType(3, LayerTypeMetadata{Name: "Fragment", Decoder: DecodeFragment})
  100. // DecodeFailure is a packet layer created if decoding of the packet data failed
  101. // for some reason. It implements ErrorLayer. LayerContents will be the entire
  102. // set of bytes that failed to parse, and Error will return the reason parsing
  103. // failed.
  104. type DecodeFailure struct {
  105. data []byte
  106. err error
  107. stack []byte
  108. }
  109. // Error returns the error encountered during decoding.
  110. func (d *DecodeFailure) Error() error { return d.err }
  111. // LayerContents implements Layer.
  112. func (d *DecodeFailure) LayerContents() []byte { return d.data }
  113. // LayerPayload implements Layer.
  114. func (d *DecodeFailure) LayerPayload() []byte { return nil }
  115. // String implements fmt.Stringer.
  116. func (d *DecodeFailure) String() string {
  117. return "Packet decoding error: " + d.Error().Error()
  118. }
  119. // Dump implements Dumper.
  120. func (d *DecodeFailure) Dump() (s string) {
  121. if d.stack != nil {
  122. s = string(d.stack)
  123. }
  124. return
  125. }
  126. // LayerType returns LayerTypeDecodeFailure
  127. func (d *DecodeFailure) LayerType() LayerType { return LayerTypeDecodeFailure }
  128. // decodeUnknown "decodes" unsupported data types by returning an error.
  129. // This decoder will thus always return a DecodeFailure layer.
  130. func decodeUnknown(data []byte, p PacketBuilder) error {
  131. return errors.New("Layer type not currently supported")
  132. }