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