forked from calmh/ipfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
298 lines (256 loc) · 7.15 KB
/
parser.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package ipfix
import (
"bytes"
"encoding/binary"
"errors"
"io"
"log"
"os"
"runtime"
)
var debug = os.Getenv("IPFIXDEBUG") != ""
// The version field in IPFIX messages should always have the value 10. If it
// does not, you get this error. It's probably a sign of a bug in the parser or
// the exporter and that we have lost synchronization with the data stream.
// Reestablishing the session is the only way forward at this point.
var ErrVersion = errors.New("incorrect version field in message header - out of sync?")
var ErrRead = errors.New("short read - malformed packet?")
// A Message is the top level construct representing an IPFIX message. A well
// formed message contains one or more sets of data or template information.
type Message struct {
Header MessageHeader
DataRecords []DataRecord
TemplateRecords []TemplateRecord
}
// The MessageHeader provides metadata for the entire Message. The sequence
// number and domain ID can be used to gain knowledge of messages lost on an
// unreliable transport such as UDP.
type MessageHeader struct {
Version uint16 // Always 0x0a
Length uint16
ExportTime uint32 // Epoch seconds
SequenceNumber uint32
DomainId uint32
}
type setHeader struct {
SetId uint16
Length uint16
}
type templateHeader struct {
TemplateId uint16
FieldCount uint16
}
// The DataRecord represents a single exported flow. The Fields each describe
// different aspects of the flow (source and destination address, counters,
// service, etc.).
type DataRecord struct {
TemplateId uint16
Fields [][]byte
}
// The TemplateRecord describes a data template, as used by DataRecords.
type TemplateRecord struct {
TemplateId uint16
FieldSpecifiers []TemplateFieldSpecifier
}
// The TemplateFieldSpecifier describes the ID and size of the corresponding
// Fields in a DataRecord.
type TemplateFieldSpecifier struct {
EnterpriseId uint32
FieldId uint16
Length uint16
}
// The Session is the context for IPFIX messages.
type Session struct {
templates [][]TemplateFieldSpecifier
reader io.Reader
minRecord []uint16
}
// NewSession initializes a new Session based on the provided io.Reader.
func NewSession(reader io.Reader) *Session {
s := Session{}
s.templates = make([][]TemplateFieldSpecifier, 65536)
s.reader = reader
s.minRecord = make([]uint16, 65536)
return &s
}
var msgHeaderLength = binary.Size(MessageHeader{})
var setHeaderLength = binary.Size(setHeader{})
var templateHeaderLength = binary.Size(templateHeader{})
// ReadMessage extracts and returns one message from the IPFIX stream. As long
// as err is nil, further messages can be read from the stream. Errors are not
// recoverable -- once an error has been returned, ReadMessage should not be
// called again on the same session.
func (s *Session) ReadMessage() (msg *Message, err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
msg = nil
err = r.(error)
}
}()
msg = &Message{}
msg.DataRecords = make([]DataRecord, 0)
msg.TemplateRecords = make([]TemplateRecord, 0)
msgHdr := MessageHeader{}
err = binary.Read(s.reader, binary.BigEndian, &msgHdr)
errorIf(err)
if debug {
log.Printf("read pktheader: %#v", msgHdr)
}
if msgHdr.Version != 10 {
errorIf(ErrVersion)
}
msg.Header = msgHdr
msgLen := int(msgHdr.Length) - msgHeaderLength
msgSlice := make([]byte, msgLen)
_, err = io.ReadFull(s.reader, msgSlice)
errorIf(err)
r := bytes.NewBuffer(msgSlice)
for r.Len() > 0 {
trecs, drecs := s.readSet(r)
msg.TemplateRecords = append(msg.TemplateRecords, trecs...)
msg.DataRecords = append(msg.DataRecords, drecs...)
}
return
}
func (s *Session) readSet(r *bytes.Buffer) (trecs []TemplateRecord, drecs []DataRecord) {
trecs = make([]TemplateRecord, 0)
drecs = make([]DataRecord, 0)
setHdr := setHeader{}
err := binary.Read(r, binary.BigEndian, &setHdr)
if debug {
log.Printf("read setheader: %#v", setHdr)
}
setEnd := r.Len() - int(setHdr.Length) + setHeaderLength
errorIf(err)
for r.Len() > setEnd {
if r.Len()-setEnd < int(s.minRecord[setHdr.SetId]) {
// Padding
return
} else if setHdr.SetId == 2 {
if debug {
log.Println("got template set")
}
// Template Set
ts := s.readTemplateRecord(r)
trecs = append(trecs, *ts)
if debug {
log.Println("template for set", ts.TemplateId)
for _, t := range ts.FieldSpecifiers {
log.Printf(" %v", t)
}
}
// Update the template cache
tid := ts.TemplateId
if len(ts.FieldSpecifiers) == 0 {
// Set was withdrawn
s.templates[tid] = nil
} else {
s.templates[tid] = ts.FieldSpecifiers
}
// Update the minimum record length cache
var minLength uint16
for i := range ts.FieldSpecifiers {
if ts.FieldSpecifiers[i].Length == 65535 {
minLength += 1
} else {
minLength += ts.FieldSpecifiers[i].Length
}
}
s.minRecord[tid] = minLength
} else if setHdr.SetId == 3 {
if debug {
log.Println("got options template set, unhandled")
}
// Options Template Set, not handled
r.Read(make([]byte, int(setHdr.Length)-setHeaderLength))
} else {
if debug {
log.Println("got data set for template", setHdr.SetId)
}
if tpl := s.templates[setHdr.SetId]; tpl != nil {
// Data set
ds := s.readDataRecord(r, tpl)
ds.TemplateId = setHdr.SetId
drecs = append(drecs, *ds)
} else {
if debug {
log.Println("set", setHdr.SetId, "is unknown")
}
// Data set with unknown template
// We can't trust set length, because we might be out of sync.
// Consume rest of message.
r.Read(make([]byte, r.Len()))
return
}
}
}
return
}
func (s *Session) readDataRecord(r *bytes.Buffer, tpl []TemplateFieldSpecifier) *DataRecord {
ds := DataRecord{}
ds.Fields = make([][]byte, len(tpl))
for i := range tpl {
var bs []byte
if tpl[i].Length == 65535 {
bs = s.readVariableLength(r)
} else {
bs = s.readFixedLength(r, int(tpl[i].Length))
}
ds.Fields[i] = bs
}
return &ds
}
func (s *Session) readTemplateRecord(r *bytes.Buffer) *TemplateRecord {
ts := TemplateRecord{}
th := templateHeader{}
err := binary.Read(r, binary.BigEndian, &th)
errorIf(err)
ts.TemplateId = th.TemplateId
ts.FieldSpecifiers = make([]TemplateFieldSpecifier, th.FieldCount)
for i := 0; i < int(th.FieldCount); i++ {
f := TemplateFieldSpecifier{}
err = binary.Read(r, binary.BigEndian, &f.FieldId)
errorIf(err)
err = binary.Read(r, binary.BigEndian, &f.Length)
errorIf(err)
if f.FieldId >= 0x8000 {
f.FieldId -= 0x8000
err = binary.Read(r, binary.BigEndian, &f.EnterpriseId)
errorIf(err)
}
ts.FieldSpecifiers[i] = f
}
return &ts
}
func (s *Session) readFixedLength(r *bytes.Buffer, n int) []byte {
bs := make([]byte, n)
n1, err := r.Read(bs)
errorIf(err)
if n1 != n {
panic(ErrRead)
}
return bs
}
func (s *Session) readVariableLength(r *bytes.Buffer) []byte {
var l int
var l0 uint8
err := binary.Read(r, binary.BigEndian, &l0)
errorIf(err)
if l0 < 255 {
l = int(l0)
} else {
var l1 uint16
err := binary.Read(r, binary.BigEndian, &l1)
errorIf(err)
l = int(l1)
}
return s.readFixedLength(r, l)
}
func errorIf(err error) {
if err != nil {
panic(err)
}
}