blob: cd59b467861b5e1527be01e47b6e736510c6031d [file] [log] [blame]
Takahiro Suzukid7bf8202020-12-17 20:21:59 +09001// 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
7package layers
8
9import (
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.
15type 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.
27func (b *BaseLayer) LayerContents() []byte { return b.Contents }
28
29// LayerPayload returns the bytes contained within the packet layer.
30func (b *BaseLayer) LayerPayload() []byte { return b.Payload }
31
32type layerDecodingLayer interface {
33 gopacket.Layer
34 DecodeFromBytes([]byte, gopacket.DecodeFeedback) error
35 NextLayerType() gopacket.LayerType
36}
37
38func 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?
52var lotsOfZeros [1024]byte