-
Notifications
You must be signed in to change notification settings - Fork 7
/
hep.go
200 lines (184 loc) · 5.98 KB
/
hep.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"strconv"
"time"
"log"
)
// HEP chuncks
const (
Version = 1 // Chunk 0x0001 IP protocol family (0x02=IPv4, 0x0a=IPv6)
Protocol = 2 // Chunk 0x0002 IP protocol ID (0x06=TCP, 0x11=UDP)
IP4SrcIP = 3 // Chunk 0x0003 IPv4 source address
IP4DstIP = 4 // Chunk 0x0004 IPv4 destination address
IP6SrcIP = 5 // Chunk 0x0005 IPv6 source address
IP6DstIP = 6 // Chunk 0x0006 IPv6 destination address
SrcPort = 7 // Chunk 0x0007 Protocol source port
DstPort = 8 // Chunk 0x0008 Protocol destination port
Tsec = 9 // Chunk 0x0009 Unix timestamp, seconds
Tmsec = 10 // Chunk 0x000a Unix timestamp, microseconds
ProtoType = 11 // Chunk 0x000b Protocol type (DNS, LOG, RTCP, SIP)
NodeID = 12 // Chunk 0x000c Capture client ID
NodePW = 14 // Chunk 0x000e Authentication key (plain text / TLS connection)
Payload = 15 // Chunk 0x000f Captured packet payload
CID = 17 // Chunk 0x0011 Correlation ID
Vlan = 18 // Chunk 0x0012 VLAN
NodeName = 19 // Chunk 0x0013 NodeName
)
// HEP represents HEP packet
type HEP struct {
Version uint32 `protobuf:"varint,1,req,name=Version" json:"Version"`
Protocol uint32 `protobuf:"varint,2,req,name=Protocol" json:"Protocol"`
SrcIP string
DstIP string
SrcPort uint32 `protobuf:"varint,5,req,name=SrcPort" json:"SrcPort"`
DstPort uint32 `protobuf:"varint,6,req,name=DstPort" json:"DstPort"`
Tsec uint32 `protobuf:"varint,7,req,name=Tsec" json:"Tsec"`
Tmsec uint32 `protobuf:"varint,8,req,name=Tmsec" json:"Tmsec"`
ProtoType uint32 `protobuf:"varint,9,req,name=ProtoType" json:"ProtoType"`
NodeID uint32 `protobuf:"varint,10,req,name=NodeID" json:"NodeID"`
NodePW string `protobuf:"bytes,11,req,name=NodePW" json:"NodePW"`
Payload string `protobuf:"bytes,12,req,name=Payload" json:"Payload"`
CID string `protobuf:"bytes,13,req,name=CID" json:"CID"`
Vlan uint32 `protobuf:"varint,14,req,name=Vlan" json:"Vlan"`
ProtoString string
Timestamp time.Time
NodeName string
TargetName string
SID string
}
// DecodeHEP returns a parsed HEP message
func DecodeHEP(packet []byte) (*HEP, error) {
hep := &HEP{}
err := hep.parse(packet)
if err != nil {
return nil, err
}
// log.Printf("HEP decoded ", hep)
return hep, nil
}
func (h *HEP) parse(packet []byte) error {
var err error
if bytes.HasPrefix(packet, []byte{0x48, 0x45, 0x50, 0x33}) {
err = h.parseHEP(packet)
if err != nil {
log.Println("Warning>", err)
return err
}
} else {
//err = h.Unmarshal(packet)
//if err != nil {
// log.Println("malformed packet with length %d which is neither hep nor protobuf encapsulated", len(packet))
return err
// }
}
h.Timestamp = time.Unix(int64(h.Tsec), int64(h.Tmsec*1000))
if h.Tsec == 0 && h.Tmsec == 0 {
log.Println("Debug> got null timestamp from nodeID %d", h.NodeID)
h.Timestamp = time.Now()
}
if h.NodeName == "" {
h.NodeName = strconv.FormatUint(uint64(h.NodeID), 10)
}
//log.Println("Debug> %+v\n\n", h)
return nil
}
//--------------------------
func (h *HEP) parseHEP(packet []byte) error {
length := binary.BigEndian.Uint16(packet[4:6])
// if int(length) != len(packet) {
// return fmt.Errorf("HEP packet length is %d but should be %d", len(packet), length)
// }
currentByte := uint16(6)
for currentByte < length {
hepChunk := packet[currentByte:]
if len(hepChunk) < 6 {
return fmt.Errorf("HEP chunk must be >= 6 byte long but is %d", len(hepChunk))
}
//chunkVendorId := binary.BigEndian.Uint16(hepChunk[:2])
chunkType := binary.BigEndian.Uint16(hepChunk[2:4])
chunkLength := binary.BigEndian.Uint16(hepChunk[4:6])
if len(hepChunk) < int(chunkLength) || int(chunkLength) < 6 {
return fmt.Errorf("HEP chunk with %d byte < chunkLength %d or chunkLength < 6", len(hepChunk), chunkLength)
}
chunkBody := hepChunk[6:chunkLength]
switch chunkType {
case Version, Protocol, ProtoType:
if len(chunkBody) != 1 {
return fmt.Errorf("HEP chunkType %d should be 1 byte long but is %d", chunkType, len(chunkBody))
}
case SrcPort, DstPort, Vlan:
if len(chunkBody) != 2 {
return fmt.Errorf("HEP chunkType %d should be 2 byte long but is %d", chunkType, len(chunkBody))
}
case IP4SrcIP, IP4DstIP, Tsec, Tmsec, NodeID:
if len(chunkBody) != 4 {
return fmt.Errorf("HEP chunkType %d should be 4 byte long but is %d", chunkType, len(chunkBody))
}
case IP6SrcIP, IP6DstIP:
if len(chunkBody) != 16 {
return fmt.Errorf("HEP chunkType %d should be 16 byte long but is %d", chunkType, len(chunkBody))
}
}
switch chunkType {
case Version:
h.Version = uint32(chunkBody[0])
case Protocol:
h.Protocol = uint32(chunkBody[0])
case IP4SrcIP:
h.SrcIP = net.IP(chunkBody).To4().String()
case IP4DstIP:
h.DstIP = net.IP(chunkBody).To4().String()
case IP6SrcIP:
h.SrcIP = net.IP(chunkBody).To16().String()
case IP6DstIP:
h.DstIP = net.IP(chunkBody).To16().String()
case SrcPort:
h.SrcPort = uint32(binary.BigEndian.Uint16(chunkBody))
case DstPort:
h.DstPort = uint32(binary.BigEndian.Uint16(chunkBody))
case Tsec:
h.Tsec = binary.BigEndian.Uint32(chunkBody)
case Tmsec:
h.Tmsec = binary.BigEndian.Uint32(chunkBody)
case ProtoType:
h.ProtoType = uint32(chunkBody[0])
switch h.ProtoType {
case 1:
h.ProtoString = "sip"
case 5:
h.ProtoString = "rtcp"
case 34:
h.ProtoString = "rtpagent"
case 35:
h.ProtoString = "rtcpxr"
case 38:
h.ProtoString = "horaclifix"
case 53:
h.ProtoString = "dns"
case 100:
h.ProtoString = "log"
default:
h.ProtoString = strconv.Itoa(int(h.ProtoType))
}
case NodeID:
h.NodeID = binary.BigEndian.Uint32(chunkBody)
case NodePW:
h.NodePW = string(chunkBody)
case Payload:
h.Payload = string(chunkBody)
case CID:
h.CID = string(chunkBody)
case Vlan:
h.Vlan = uint32(binary.BigEndian.Uint16(chunkBody))
case NodeName:
h.NodeName = string(chunkBody)
default:
}
currentByte += chunkLength
}
return nil
}