-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot3.go
66 lines (50 loc) · 1.56 KB
/
dot3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package protocols
import (
"encoding/binary"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"net"
)
var LayerTypeDot3 = gopacket.RegisterLayerType(2001, gopacket.LayerTypeMetadata{Name: "Ethernet8023", Decoder: gopacket.DecodeFunc(decodeDot3)})
type Dot3 struct {
layers.BaseLayer
DstMAC, SrcMAC net.HardwareAddr
Length uint16
}
func (d *Dot3) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
// Calculate the length for the fixed header part: DstMAC (6 bytes) + SrcMAC (6 bytes) + Length (2 bytes)
length := 6 + 6 + 2
// Prepend bytes for the header at the beginning of the packet
buf, err := b.PrependBytes(length)
if err != nil {
return err
}
// Copy DstMAC and SrcMAC to the buffer
copy(buf[0:6], d.DstMAC)
copy(buf[6:12], d.SrcMAC)
// Handle options
if opts.FixLengths {
d.Length = uint16(len(b.Bytes())) - uint16(length)
}
binary.BigEndian.PutUint16(buf[12:14], d.Length)
return nil
}
func (d *Dot3) LayerType() gopacket.LayerType { return LayerTypeDot3 }
func (d *Dot3) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
d.DstMAC = net.HardwareAddr(data[0:6])
d.SrcMAC = net.HardwareAddr(data[6:12])
d.Length = binary.BigEndian.Uint16(data[12:14])
return nil
}
func (d *Dot3) NextLayerType() gopacket.LayerType {
return layers.LayerTypeLLC
}
func (d *Dot3) CanDecode() gopacket.LayerClass {
return LayerTypeDot3
}
func decodeDot3(data []byte, p gopacket.PacketBuilder) error {
d := &Dot3{}
d.DecodeFromBytes(data, p)
p.AddLayer(d)
return p.NextDecoder(d.NextLayerType())
}