-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
55 lines (48 loc) · 1.21 KB
/
hash.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
package balance
import (
"encoding/binary"
"errors"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
"github.com/satta/gommunityid"
)
var cid gommunityid.CommunityID
func init() {
var err error
cid, err = gommunityid.GetCommunityIDByVersion(1, 0)
if err != nil {
panic("unable to initialize community ID")
}
}
func hash(pkt gopacket.Packet) (string, error) {
nl := pkt.NetworkLayer()
if nl == nil {
return "", errors.New("no transport layer")
}
tl := pkt.TransportLayer()
if tl == nil {
return "", errors.New("no transport layer")
}
var transportID uint8 = 0
switch tl.LayerType() {
case layers.LayerTypeICMPv4:
transportID = gommunityid.ProtoICMP
case layers.LayerTypeTCP:
transportID = gommunityid.ProtoTCP
case layers.LayerTypeUDP:
transportID = gommunityid.ProtoUDP
case layers.LayerTypeICMPv6:
transportID = gommunityid.ProtoICMP6
case layers.LayerTypeSCTP:
transportID = gommunityid.ProtoSCTP
}
return cid.CalcHex(
gommunityid.MakeFlowTuple(
nl.NetworkFlow().Src().Raw(),
nl.NetworkFlow().Dst().Raw(),
binary.BigEndian.Uint16(tl.TransportFlow().Src().Raw()),
binary.BigEndian.Uint16(tl.TransportFlow().Dst().Raw()),
transportID,
),
), nil
}