base.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 layers
  7. import (
  8. "github.com/google/gopacket"
  9. )
  10. // BaseLayer is a convenience struct which implements the LayerData and
  11. // LayerPayload functions of the Layer interface.
  12. type BaseLayer struct {
  13. // Contents is the set of bytes that make up this layer. IE: for an
  14. // Ethernet packet, this would be the set of bytes making up the
  15. // Ethernet frame.
  16. Contents []byte
  17. // Payload is the set of bytes contained by (but not part of) this
  18. // Layer. Again, to take Ethernet as an example, this would be the
  19. // set of bytes encapsulated by the Ethernet protocol.
  20. Payload []byte
  21. }
  22. // LayerContents returns the bytes of the packet layer.
  23. func (b *BaseLayer) LayerContents() []byte { return b.Contents }
  24. // LayerPayload returns the bytes contained within the packet layer.
  25. func (b *BaseLayer) LayerPayload() []byte { return b.Payload }
  26. type layerDecodingLayer interface {
  27. gopacket.Layer
  28. DecodeFromBytes([]byte, gopacket.DecodeFeedback) error
  29. NextLayerType() gopacket.LayerType
  30. }
  31. func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error {
  32. err := d.DecodeFromBytes(data, p)
  33. if err != nil {
  34. return err
  35. }
  36. p.AddLayer(d)
  37. next := d.NextLayerType()
  38. if next == gopacket.LayerTypeZero {
  39. return nil
  40. }
  41. return p.NextDecoder(next)
  42. }
  43. // hacky way to zero out memory... there must be a better way?
  44. var lotsOfZeros [1024]byte