-
Notifications
You must be signed in to change notification settings - Fork 5
/
link.go
63 lines (53 loc) · 1.86 KB
/
link.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
package builtin
import "github.com/CN-TU/go-flows/packet"
func sourceMacAddressKey(packet packet.Buffer, scratch, scratchNoSort []byte) (int, int) {
link := packet.LinkLayer()
if link == nil {
return 0, 0
}
return copy(scratch, link.LinkFlow().Src().Raw()), 0
}
func destinationMacAddressKey(packet packet.Buffer, scratch, scratchNoSort []byte) (int, int) {
link := packet.LinkLayer()
if link == nil {
return 0, 0
}
return copy(scratch, link.LinkFlow().Dst().Raw()), 0
}
func init() {
packet.RegisterKeyPair(
packet.RegisterStringKey("sourceMacAddress",
"source address of link layer",
packet.KeyTypeSource, packet.KeyLayerLink, func(string) packet.KeyFunc { return sourceMacAddressKey }),
packet.RegisterStringKey("destinationMacAddress",
"destination address of link layer",
packet.KeyTypeDestination, packet.KeyLayerLink, func(string) packet.KeyFunc { return destinationMacAddressKey }),
)
}
////////////////////////////////////////////////////////////////////////////////
func ethernetTypeKey(packet packet.Buffer, scratch, scratchNoSort []byte) (int, int) {
t := packet.EtherType()
scratch[0] = byte(t >> 8)
scratch[1] = byte(t & 0x00FF)
return 2, 0
}
func init() {
packet.RegisterStringKey("ethernetType",
"protocol identifier of link layer",
packet.KeyTypeUnidirectional, packet.KeyLayerLink, func(string) packet.KeyFunc { return ethernetTypeKey })
}
////////////////////////////////////////////////////////////////////////////////
func vlanKey(packet packet.Buffer, scratch, scratchNoSort []byte) (int, int) {
t := packet.Dot1QLayers()
for i := range t {
vlan := t[i].VLANIdentifier
scratch[i*2] = byte(vlan >> 8)
scratch[i*2+1] = byte(vlan & 0x00FF)
}
return len(t) * 2, 0
}
func init() {
packet.RegisterStringKey("vlanID",
"vlan ID",
packet.KeyTypeUnidirectional, packet.KeyLayerLink, func(string) packet.KeyFunc { return vlanKey })
}