github.com/gopacket/gopacket@v1.1.0/base.go (about) 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 gopacket 8 9 import ( 10 "fmt" 11 ) 12 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. 21 LayerType() LayerType 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. 26 LayerPayload() []byte 27 } 28 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. 33 type Payload []byte 34 35 // LayerType returns LayerTypePayload 36 func (p Payload) LayerType() LayerType { return LayerTypePayload } 37 38 // LayerContents returns the bytes making up this layer. 39 func (p Payload) LayerContents() []byte { return []byte(p) } 40 41 // LayerPayload returns the payload within this layer. 42 func (p Payload) LayerPayload() []byte { return nil } 43 44 // Payload returns this layer as bytes. 45 func (p Payload) Payload() []byte { return []byte(p) } 46 47 // String implements fmt.Stringer. 48 func (p Payload) String() string { return fmt.Sprintf("%d byte(s)", len(p)) } 49 50 // GoString implements fmt.GoStringer. 51 func (p Payload) GoString() string { return LongBytesGoString([]byte(p)) } 52 53 // CanDecode implements DecodingLayer. 54 func (p Payload) CanDecode() LayerClass { return LayerTypePayload } 55 56 // NextLayerType implements DecodingLayer. 57 func (p Payload) NextLayerType() LayerType { return LayerTypeZero } 58 59 // DecodeFromBytes implements DecodingLayer. 60 func (p *Payload) DecodeFromBytes(data []byte, df DecodeFeedback) error { 61 *p = Payload(data) 62 return nil 63 } 64 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)) 70 if err != nil { 71 return err 72 } 73 copy(bytes, p) 74 return nil 75 } 76 77 // decodePayload decodes data by returning it all in a Payload layer. 78 func decodePayload(data []byte, p PacketBuilder) error { 79 payload := &Payload{} 80 if err := payload.DecodeFromBytes(data, p); err != nil { 81 return err 82 } 83 p.AddLayer(payload) 84 p.SetApplicationLayer(payload) 85 return nil 86 } 87 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. 90 type Fragment []byte 91 92 // LayerType returns LayerTypeFragment 93 func (p *Fragment) LayerType() LayerType { return LayerTypeFragment } 94 95 // LayerContents implements Layer. 96 func (p *Fragment) LayerContents() []byte { return []byte(*p) } 97 98 // LayerPayload implements Layer. 99 func (p *Fragment) LayerPayload() []byte { return nil } 100 101 // Payload returns this layer as a byte slice. 102 func (p *Fragment) Payload() []byte { return []byte(*p) } 103 104 // String implements fmt.Stringer. 105 func (p *Fragment) String() string { return fmt.Sprintf("%d byte(s)", len(*p)) } 106 107 // CanDecode implements DecodingLayer. 108 func (p *Fragment) CanDecode() LayerClass { return LayerTypeFragment } 109 110 // NextLayerType implements DecodingLayer. 111 func (p *Fragment) NextLayerType() LayerType { return LayerTypeZero } 112 113 // DecodeFromBytes implements DecodingLayer. 114 func (p *Fragment) DecodeFromBytes(data []byte, df DecodeFeedback) error { 115 *p = Fragment(data) 116 return nil 117 } 118 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)) 124 if err != nil { 125 return err 126 } 127 copy(bytes, *p) 128 return nil 129 } 130 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 { 135 return err 136 } 137 p.AddLayer(payload) 138 p.SetApplicationLayer(payload) 139 return nil 140 } 141 142 // These layers correspond to Internet Protocol Suite (TCP/IP) layers, and their 143 // corresponding OSI layers, as best as possible. 144 145 // LinkLayer is the packet layer corresponding to TCP/IP layer 1 (OSI layer 2) 146 type LinkLayer interface { 147 Layer 148 LinkFlow() Flow 149 } 150 151 // NetworkLayer is the packet layer corresponding to TCP/IP layer 2 (OSI 152 // layer 3) 153 type NetworkLayer interface { 154 Layer 155 NetworkFlow() Flow 156 } 157 158 // TransportLayer is the packet layer corresponding to the TCP/IP layer 3 (OSI 159 // layer 4) 160 type TransportLayer interface { 161 Layer 162 TransportFlow() Flow 163 } 164 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 { 168 Layer 169 Payload() []byte 170 } 171 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 { 176 Layer 177 Error() error 178 }