base.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Layer represents a single decoded packet layer (using either the
  11. // OSI or TCP/IP definition of a layer). When decoding, a packet's data is
  12. // broken up into a number of layers. The caller may call LayerType() to
  13. // figure out which type of layer they've received from the packet. Optionally,
  14. // they may then use a type assertion to get the actual layer type for deep
  15. // inspection of the data.
  16. type Layer interface {
  17. // LayerType is the gopacket type for this layer.
  18. LayerType() LayerType
  19. // LayerContents returns the set of bytes that make up this layer.
  20. LayerContents() []byte
  21. // LayerPayload returns the set of bytes contained within this layer, not
  22. // including the layer itself.
  23. LayerPayload() []byte
  24. }
  25. // Payload is a Layer containing the payload of a packet. The definition of
  26. // what constitutes the payload of a packet depends on previous layers; for
  27. // TCP and UDP, we stop decoding above layer 4 and return the remaining
  28. // bytes as a Payload. Payload is an ApplicationLayer.
  29. type Payload []byte
  30. // LayerType returns LayerTypePayload
  31. func (p Payload) LayerType() LayerType { return LayerTypePayload }
  32. // LayerContents returns the bytes making up this layer.
  33. func (p Payload) LayerContents() []byte { return []byte(p) }
  34. // LayerPayload returns the payload within this layer.
  35. func (p Payload) LayerPayload() []byte { return nil }
  36. // Payload returns this layer as bytes.
  37. func (p Payload) Payload() []byte { return []byte(p) }
  38. // String implements fmt.Stringer.
  39. func (p Payload) String() string { return fmt.Sprintf("%d byte(s)", len(p)) }
  40. // GoString implements fmt.GoStringer.
  41. func (p Payload) GoString() string { return LongBytesGoString([]byte(p)) }
  42. // CanDecode implements DecodingLayer.
  43. func (p Payload) CanDecode() LayerClass { return LayerTypePayload }
  44. // NextLayerType implements DecodingLayer.
  45. func (p Payload) NextLayerType() LayerType { return LayerTypeZero }
  46. // DecodeFromBytes implements DecodingLayer.
  47. func (p *Payload) DecodeFromBytes(data []byte, df DecodeFeedback) error {
  48. *p = Payload(data)
  49. return nil
  50. }
  51. // SerializeTo writes the serialized form of this layer into the
  52. // SerializationBuffer, implementing gopacket.SerializableLayer.
  53. // See the docs for gopacket.SerializableLayer for more info.
  54. func (p Payload) SerializeTo(b SerializeBuffer, opts SerializeOptions) error {
  55. bytes, err := b.PrependBytes(len(p))
  56. if err != nil {
  57. return err
  58. }
  59. copy(bytes, p)
  60. return nil
  61. }
  62. // decodePayload decodes data by returning it all in a Payload layer.
  63. func decodePayload(data []byte, p PacketBuilder) error {
  64. payload := &Payload{}
  65. if err := payload.DecodeFromBytes(data, p); err != nil {
  66. return err
  67. }
  68. p.AddLayer(payload)
  69. p.SetApplicationLayer(payload)
  70. return nil
  71. }
  72. // Fragment is a Layer containing a fragment of a larger frame, used by layers
  73. // like IPv4 and IPv6 that allow for fragmentation of their payloads.
  74. type Fragment []byte
  75. // LayerType returns LayerTypeFragment
  76. func (p *Fragment) LayerType() LayerType { return LayerTypeFragment }
  77. // LayerContents implements Layer.
  78. func (p *Fragment) LayerContents() []byte { return []byte(*p) }
  79. // LayerPayload implements Layer.
  80. func (p *Fragment) LayerPayload() []byte { return nil }
  81. // Payload returns this layer as a byte slice.
  82. func (p *Fragment) Payload() []byte { return []byte(*p) }
  83. // String implements fmt.Stringer.
  84. func (p *Fragment) String() string { return fmt.Sprintf("%d byte(s)", len(*p)) }
  85. // CanDecode implements DecodingLayer.
  86. func (p *Fragment) CanDecode() LayerClass { return LayerTypeFragment }
  87. // NextLayerType implements DecodingLayer.
  88. func (p *Fragment) NextLayerType() LayerType { return LayerTypeZero }
  89. // DecodeFromBytes implements DecodingLayer.
  90. func (p *Fragment) DecodeFromBytes(data []byte, df DecodeFeedback) error {
  91. *p = Fragment(data)
  92. return nil
  93. }
  94. // SerializeTo writes the serialized form of this layer into the
  95. // SerializationBuffer, implementing gopacket.SerializableLayer.
  96. // See the docs for gopacket.SerializableLayer for more info.
  97. func (p *Fragment) SerializeTo(b SerializeBuffer, opts SerializeOptions) error {
  98. bytes, err := b.PrependBytes(len(*p))
  99. if err != nil {
  100. return err
  101. }
  102. copy(bytes, *p)
  103. return nil
  104. }
  105. // decodeFragment decodes data by returning it all in a Fragment layer.
  106. func decodeFragment(data []byte, p PacketBuilder) error {
  107. payload := &Fragment{}
  108. if err := payload.DecodeFromBytes(data, p); err != nil {
  109. return err
  110. }
  111. p.AddLayer(payload)
  112. p.SetApplicationLayer(payload)
  113. return nil
  114. }
  115. // These layers correspond to Internet Protocol Suite (TCP/IP) layers, and their
  116. // corresponding OSI layers, as best as possible.
  117. // LinkLayer is the packet layer corresponding to TCP/IP layer 1 (OSI layer 2)
  118. type LinkLayer interface {
  119. Layer
  120. LinkFlow() Flow
  121. }
  122. // NetworkLayer is the packet layer corresponding to TCP/IP layer 2 (OSI
  123. // layer 3)
  124. type NetworkLayer interface {
  125. Layer
  126. NetworkFlow() Flow
  127. }
  128. // TransportLayer is the packet layer corresponding to the TCP/IP layer 3 (OSI
  129. // layer 4)
  130. type TransportLayer interface {
  131. Layer
  132. TransportFlow() Flow
  133. }
  134. // ApplicationLayer is the packet layer corresponding to the TCP/IP layer 4 (OSI
  135. // layer 7), also known as the packet payload.
  136. type ApplicationLayer interface {
  137. Layer
  138. Payload() []byte
  139. }
  140. // ErrorLayer is a packet layer created when decoding of the packet has failed.
  141. // Its payload is all the bytes that we were unable to decode, and the returned
  142. // error details why the decoding failed.
  143. type ErrorLayer interface {
  144. Layer
  145. Error() error
  146. }