1 // Copyright 2012 Google, Inc. All rights reserved.
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
13 // Layer represents a single decoded packet layer (using either the
14 // OSI or TCP/IP definition of a layer). When decoding, a packet's data is
15 // broken up into a number of layers. The caller may call LayerType() to
16 // figure out which type of layer they've received from the packet. Optionally,
17 // they may then use a type assertion to get the actual layer type for deep
18 // inspection of the data.
19 type Layer interface {
20 // LayerType is the gopacket type for this layer.
22 // LayerContents returns the set of bytes that make up this layer.
23 LayerContents() []byte
24 // LayerPayload returns the set of bytes contained within this layer, not
25 // including the layer itself.
29 // Payload is a Layer containing the payload of a packet. The definition of
30 // what constitutes the payload of a packet depends on previous layers; for
31 // TCP and UDP, we stop decoding above layer 4 and return the remaining
32 // bytes as a Payload. Payload is an ApplicationLayer.
35 // LayerType returns LayerTypePayload
36 func (p Payload) LayerType() LayerType { return LayerTypePayload }
38 // LayerContents returns the bytes making up this layer.
39 func (p Payload) LayerContents() []byte { return []byte(p) }
41 // LayerPayload returns the payload within this layer.
42 func (p Payload) LayerPayload() []byte { return nil }
44 // Payload returns this layer as bytes.
45 func (p Payload) Payload() []byte { return []byte(p) }
47 // String implements fmt.Stringer.
48 func (p Payload) String() string { return fmt.Sprintf("%d byte(s)", len(p)) }
50 // GoString implements fmt.GoStringer.
51 func (p Payload) GoString() string { return LongBytesGoString([]byte(p)) }
53 // CanDecode implements DecodingLayer.
54 func (p Payload) CanDecode() LayerClass { return LayerTypePayload }
56 // NextLayerType implements DecodingLayer.
57 func (p Payload) NextLayerType() LayerType { return LayerTypeZero }
59 // DecodeFromBytes implements DecodingLayer.
60 func (p *Payload) DecodeFromBytes(data []byte, df DecodeFeedback) error {
65 // SerializeTo writes the serialized form of this layer into the
66 // SerializationBuffer, implementing gopacket.SerializableLayer.
67 // See the docs for gopacket.SerializableLayer for more info.
68 func (p Payload) SerializeTo(b SerializeBuffer, opts SerializeOptions) error {
69 bytes, err := b.PrependBytes(len(p))
77 // decodePayload decodes data by returning it all in a Payload layer.
78 func decodePayload(data []byte, p PacketBuilder) error {
80 if err := payload.DecodeFromBytes(data, p); err != nil {
84 p.SetApplicationLayer(payload)
88 // Fragment is a Layer containing a fragment of a larger frame, used by layers
89 // like IPv4 and IPv6 that allow for fragmentation of their payloads.
92 // LayerType returns LayerTypeFragment
93 func (p *Fragment) LayerType() LayerType { return LayerTypeFragment }
95 // LayerContents implements Layer.
96 func (p *Fragment) LayerContents() []byte { return []byte(*p) }
98 // LayerPayload implements Layer.
99 func (p *Fragment) LayerPayload() []byte { return nil }
101 // Payload returns this layer as a byte slice.
102 func (p *Fragment) Payload() []byte { return []byte(*p) }
104 // String implements fmt.Stringer.
105 func (p *Fragment) String() string { return fmt.Sprintf("%d byte(s)", len(*p)) }
107 // CanDecode implements DecodingLayer.
108 func (p *Fragment) CanDecode() LayerClass { return LayerTypeFragment }
110 // NextLayerType implements DecodingLayer.
111 func (p *Fragment) NextLayerType() LayerType { return LayerTypeZero }
113 // DecodeFromBytes implements DecodingLayer.
114 func (p *Fragment) DecodeFromBytes(data []byte, df DecodeFeedback) error {
119 // SerializeTo writes the serialized form of this layer into the
120 // SerializationBuffer, implementing gopacket.SerializableLayer.
121 // See the docs for gopacket.SerializableLayer for more info.
122 func (p *Fragment) SerializeTo(b SerializeBuffer, opts SerializeOptions) error {
123 bytes, err := b.PrependBytes(len(*p))
131 // decodeFragment decodes data by returning it all in a Fragment layer.
132 func decodeFragment(data []byte, p PacketBuilder) error {
133 payload := &Fragment{}
134 if err := payload.DecodeFromBytes(data, p); err != nil {
138 p.SetApplicationLayer(payload)
142 // These layers correspond to Internet Protocol Suite (TCP/IP) layers, and their
143 // corresponding OSI layers, as best as possible.
145 // LinkLayer is the packet layer corresponding to TCP/IP layer 1 (OSI layer 2)
146 type LinkLayer interface {
151 // NetworkLayer is the packet layer corresponding to TCP/IP layer 2 (OSI
153 type NetworkLayer interface {
158 // TransportLayer is the packet layer corresponding to the TCP/IP layer 3 (OSI
160 type TransportLayer interface {
165 // ApplicationLayer is the packet layer corresponding to the TCP/IP layer 4 (OSI
166 // layer 7), also known as the packet payload.
167 type ApplicationLayer interface {
172 // ErrorLayer is a packet layer created when decoding of the packet has failed.
173 // Its payload is all the bytes that we were unable to decode, and the returned
174 // error details why the decoding failed.
175 type ErrorLayer interface {