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
14 "github.com/google/gopacket"
17 // Loopback contains the header for loopback encapsulation. This header is
18 // used by both BSD and OpenBSD style loopback decoding (pcap's DLT_NULL
19 // and DLT_LOOP, respectively).
20 type Loopback struct {
25 // LayerType returns LayerTypeLoopback.
26 func (l *Loopback) LayerType() gopacket.LayerType { return LayerTypeLoopback }
28 // DecodeFromBytes decodes the given bytes into this layer.
29 func (l *Loopback) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
31 return errors.New("Loopback packet too small")
34 // The protocol could be either big-endian or little-endian, we're
35 // not sure. But we're PRETTY sure that the value is less than
36 // 256, so we can check the first two bytes.
38 if data[0] == 0 && data[1] == 0 {
39 prot = binary.BigEndian.Uint32(data[:4])
41 prot = binary.LittleEndian.Uint32(data[:4])
44 return fmt.Errorf("Invalid loopback protocol %q", data[:4])
47 l.Family = ProtocolFamily(prot)
48 l.BaseLayer = BaseLayer{data[:4], data[4:]}
52 // CanDecode returns the set of layer types that this DecodingLayer can decode.
53 func (l *Loopback) CanDecode() gopacket.LayerClass {
54 return LayerTypeLoopback
57 // NextLayerType returns the layer type contained by this DecodingLayer.
58 func (l *Loopback) NextLayerType() gopacket.LayerType {
59 return l.Family.LayerType()
62 // SerializeTo writes the serialized form of this layer into the
63 // SerializationBuffer, implementing gopacket.SerializableLayer.
64 func (l *Loopback) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
65 bytes, err := b.PrependBytes(4)
69 binary.LittleEndian.PutUint32(bytes, uint32(l.Family))
73 func decodeLoopback(data []byte, p gopacket.PacketBuilder) error {
75 if err := l.DecodeFromBytes(data, gopacket.NilDecodeFeedback); err != nil {
79 return p.NextDecoder(l.Family)