-
Notifications
You must be signed in to change notification settings - Fork 7
/
mux.go
40 lines (30 loc) · 780 Bytes
/
mux.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
package main
import (
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type mux map[gopacket.LayerType]func(source net.HardwareAddr, layer gopacket.Layer) bool
func newMux() mux {
return make(mux)
}
func (m mux) add(layerType gopacket.LayerType, fun func(source net.HardwareAddr, layer gopacket.Layer) bool) {
m[layerType] = fun
}
func (m mux) process(packet gopacket.Packet) bool {
var source net.HardwareAddr
if ethernetLayer := packet.Layer(layers.LayerTypeEthernet); ethernetLayer != nil {
ethernet := ethernetLayer.(*layers.Ethernet)
source = ethernet.SrcMAC
}
recognized := false
for t, f := range m {
if l := packet.Layer(t); l != nil {
r := f(source, l)
if r {
recognized = true
}
}
}
return recognized
}