-
Notifications
You must be signed in to change notification settings - Fork 3
/
event.go
104 lines (79 loc) · 1.85 KB
/
event.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"fmt"
"unsafe"
"golang.org/x/sys/unix"
)
type directionType uint16
func (d directionType) String() string {
switch d {
case 1:
return DirIngress
case 2:
return DirEgress
default:
return "UNKNOWN"
}
}
type event struct {
VlanTCI uint16
VlanEncapProto uint16
Ifindex uint32
Mark uint32
Direction directionType
TotalLen uint16
Data [108]byte
}
const _ = int(unsafe.Sizeof(event{}))
func (ev *event) output(devs map[int]string) {
var b bytes.Buffer
b.Grow(int(ev.TotalLen))
fmt.Fprintf(&b, "ifindex: %d(%s) ", ev.Ifindex, devs[int(ev.Ifindex)])
fmt.Fprintf(&b, "dir=%s ", ev.Direction)
fmt.Fprintf(&b, "mark=0x%x(%d)\n", ev.Mark, ev.Mark)
if ev.VlanEncapProto != 0 {
fmt.Fprintf(&b, "\tVLAN: ID=%d\n", ev.VlanTCI&0x0FFF)
}
ev.outputHeaders(&b, ev.Data[:ev.TotalLen])
fmt.Print(b.String())
}
func (ev *event) outputHeaders(b *bytes.Buffer, buf []byte) {
eth, buf := newEthhdr(buf)
fmt.Fprintf(b, "\tETH: %s\n", eth)
isVlan, isIP := eth.isVlanProto(), eth.isIPProto()
for isVlan {
var vh vlanhdr
vh, buf = newVlanhdr(buf)
fmt.Fprintf(b, "\tVLAN: %s\n", vh)
buf = buf[len(vh):]
isVlan, isIP = vh.isVlanProto(), vh.isIPProto()
}
if !isIP {
fmt.Fprint(b, "\tNext protocol should be IPv4\n")
return
}
iph, buf := newIPhdr(buf)
if !iph.isV4() {
fmt.Fprint(b, "\tOnly support IPv4\n")
return
}
fmt.Fprintf(b, "\tIPv4: %s\n", iph)
switch iph.protocol() {
case unix.IPPROTO_ICMP:
icmph, _ := newIcmphdr(buf)
fmt.Fprintf(b, "\tICMPv4: %s\n", icmph)
case unix.IPPROTO_TCP:
tcph, _ := newTCPhdr(buf)
fmt.Fprintf(b, "\tTCP: %s\n", tcph)
case unix.IPPROTO_UDP:
udph, buf := newUDPhdr(buf)
fmt.Fprintf(b, "\tUDP: %s\n", udph)
if !udph.isVxLAN() {
return
}
vxh, buf := newVxlanhdr(buf)
fmt.Fprintf(b, "\tVxLAN: %s\n", vxh)
ev.outputHeaders(b, buf)
}
}