-
Notifications
You must be signed in to change notification settings - Fork 2
/
tuple.go
59 lines (48 loc) · 1.35 KB
/
tuple.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
package main
import (
"fmt"
"github.com/google/gopacket"
)
type tupleKey [2]gopacket.Flow
type tcp4Tuple struct {
netSrc, netDst, tcpSrc, tcpDst gopacket.Endpoint
}
func newTcp4Tuple(netFlow, tcpFlow gopacket.Flow) tcp4Tuple {
netSrc, netDst := netFlow.Endpoints()
tcpSrc, tcpDst := tcpFlow.Endpoints()
return tcp4Tuple{netSrc, netDst, tcpSrc, tcpDst}
}
func (tt tcp4Tuple) String() string {
return fmt.Sprintf("%v:%v->%v:%v", tt.netSrc, tt.tcpSrc, tt.netDst, tt.tcpDst)
}
func (tt tcp4Tuple) Src() string {
return fmt.Sprintf("%v:%v", tt.netSrc, tt.tcpSrc)
}
func (tt tcp4Tuple) Dst() string {
return fmt.Sprintf("%v:%v", tt.netDst, tt.tcpDst)
}
func (tt tcp4Tuple) srcKey() tupleKey {
return tupleKey{
gopacket.NewFlow(tt.netSrc.EndpointType(), tt.netSrc.Raw(), tt.netDst.Raw()),
gopacket.NewFlow(tt.tcpSrc.EndpointType(), tt.tcpSrc.Raw(), tt.tcpDst.Raw()),
}
}
func (tt tcp4Tuple) revKey() tupleKey {
return tupleKey{
gopacket.NewFlow(tt.netSrc.EndpointType(), tt.netDst.Raw(), tt.netSrc.Raw()),
gopacket.NewFlow(tt.tcpSrc.EndpointType(), tt.tcpDst.Raw(), tt.tcpSrc.Raw()),
}
}
// use lower addr/port for Key
func (tt tcp4Tuple) Key() tupleKey {
if tt.netSrc.LessThan(tt.netDst) {
return tt.srcKey()
}
if tt.netDst.LessThan(tt.netSrc) {
return tt.revKey()
}
if tt.tcpDst.LessThan(tt.tcpSrc) {
return tt.revKey()
}
return tt.srcKey()
}