Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / layers / linux_sll.go
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         "encoding/binary"
11         "errors"
12         "fmt"
13         "net"
14
15         "github.com/google/gopacket"
16 )
17
18 type LinuxSLLPacketType uint16
19
20 const (
21         LinuxSLLPacketTypeHost      LinuxSLLPacketType = 0 // To us
22         LinuxSLLPacketTypeBroadcast LinuxSLLPacketType = 1 // To all
23         LinuxSLLPacketTypeMulticast LinuxSLLPacketType = 2 // To group
24         LinuxSLLPacketTypeOtherhost LinuxSLLPacketType = 3 // To someone else
25         LinuxSLLPacketTypeOutgoing  LinuxSLLPacketType = 4 // Outgoing of any type
26         // These ones are invisible by user level
27         LinuxSLLPacketTypeLoopback  LinuxSLLPacketType = 5 // MC/BRD frame looped back
28         LinuxSLLPacketTypeFastroute LinuxSLLPacketType = 6 // Fastrouted frame
29 )
30
31 func (l LinuxSLLPacketType) String() string {
32         switch l {
33         case LinuxSLLPacketTypeHost:
34                 return "host"
35         case LinuxSLLPacketTypeBroadcast:
36                 return "broadcast"
37         case LinuxSLLPacketTypeMulticast:
38                 return "multicast"
39         case LinuxSLLPacketTypeOtherhost:
40                 return "otherhost"
41         case LinuxSLLPacketTypeOutgoing:
42                 return "outgoing"
43         case LinuxSLLPacketTypeLoopback:
44                 return "loopback"
45         case LinuxSLLPacketTypeFastroute:
46                 return "fastroute"
47         }
48         return fmt.Sprintf("Unknown(%d)", int(l))
49 }
50
51 type LinuxSLL struct {
52         BaseLayer
53         PacketType   LinuxSLLPacketType
54         AddrLen      uint16
55         Addr         net.HardwareAddr
56         EthernetType EthernetType
57 }
58
59 // LayerType returns LayerTypeLinuxSLL.
60 func (sll *LinuxSLL) LayerType() gopacket.LayerType { return LayerTypeLinuxSLL }
61
62 func (sll *LinuxSLL) CanDecode() gopacket.LayerClass {
63         return LayerTypeLinuxSLL
64 }
65
66 func (sll *LinuxSLL) LinkFlow() gopacket.Flow {
67         return gopacket.NewFlow(EndpointMAC, sll.Addr, nil)
68 }
69
70 func (sll *LinuxSLL) NextLayerType() gopacket.LayerType {
71         return sll.EthernetType.LayerType()
72 }
73
74 func (sll *LinuxSLL) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
75         if len(data) < 16 {
76                 return errors.New("Linux SLL packet too small")
77         }
78         sll.PacketType = LinuxSLLPacketType(binary.BigEndian.Uint16(data[0:2]))
79         sll.AddrLen = binary.BigEndian.Uint16(data[4:6])
80
81         sll.Addr = net.HardwareAddr(data[6 : sll.AddrLen+6])
82         sll.EthernetType = EthernetType(binary.BigEndian.Uint16(data[14:16]))
83         sll.BaseLayer = BaseLayer{data[:16], data[16:]}
84
85         return nil
86 }
87
88 func decodeLinuxSLL(data []byte, p gopacket.PacketBuilder) error {
89         sll := &LinuxSLL{}
90         if err := sll.DecodeFromBytes(data, p); err != nil {
91                 return err
92         }
93         p.AddLayer(sll)
94         p.SetLinkLayer(sll)
95         return p.NextDecoder(sll.EthernetType)
96 }