-
Notifications
You must be signed in to change notification settings - Fork 7
/
decode.go
182 lines (142 loc) · 4.38 KB
/
decode.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
package nf9packet
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
func errorMissingData(bytes int) error {
return fmt.Errorf("Incomplete packet, missing at least %d bytes.", bytes)
}
func errorIncompatibleVersion(version uint16) error {
return fmt.Errorf("Incompatible protocol version v%d, only v9 is supported", version)
}
func errorExtraBytes(bytes int) error {
return fmt.Errorf("Extra %d bytes at the end of the packet.", bytes)
}
func parseFieldList(buf *bytes.Buffer, count int) (list []Field) {
list = make([]Field, count)
for i := 0; i < count; i++ {
binary.Read(buf, binary.BigEndian, &list[i])
}
return
}
func parseOptionsTemplateFlowSet(data []byte, header *FlowSetHeader) (interface{}, error) {
var set OptionsTemplateFlowSet
var t OptionsTemplateRecord
set.Id = header.Id
set.Length = header.Length
buf := bytes.NewBuffer(data)
headerLen := binary.Size(t.TemplateId) + binary.Size(t.ScopeLength) + binary.Size(t.OptionLength)
for buf.Len() >= 4 { // Padding aligns to 4 byte boundary
if buf.Len() < headerLen {
return nil, errorMissingData(headerLen - buf.Len())
}
binary.Read(buf, binary.BigEndian, &t.TemplateId)
binary.Read(buf, binary.BigEndian, &t.ScopeLength)
binary.Read(buf, binary.BigEndian, &t.OptionLength)
if buf.Len() < int(t.ScopeLength)+int(t.OptionLength) {
return nil, errorMissingData(int(t.ScopeLength) + int(t.OptionLength) - buf.Len())
}
scopeCount := int(t.ScopeLength) / binary.Size(Field{})
optionCount := int(t.OptionLength) / binary.Size(Field{})
t.Scopes = parseFieldList(buf, scopeCount)
t.Options = parseFieldList(buf, optionCount)
set.Records = append(set.Records, t)
}
return set, nil
}
func parseTemplateFlowSet(data []byte, header *FlowSetHeader) (interface{}, error) {
var set TemplateFlowSet
var t TemplateRecord
set.Id = header.Id
set.Length = header.Length
buf := bytes.NewBuffer(data)
headerLen := binary.Size(t.TemplateId) + binary.Size(t.FieldCount)
for buf.Len() >= 4 { // Padding aligns to 4 byte boundary
if buf.Len() < headerLen {
return nil, errorMissingData(headerLen - buf.Len())
}
binary.Read(buf, binary.BigEndian, &t.TemplateId)
binary.Read(buf, binary.BigEndian, &t.FieldCount)
fieldsLen := int(t.FieldCount) * binary.Size(Field{})
if fieldsLen > buf.Len() {
return nil, errorMissingData(fieldsLen - buf.Len())
}
t.Fields = parseFieldList(buf, int(t.FieldCount))
set.Records = append(set.Records, t)
}
return set, nil
}
func parseDataFlowSet(data []byte, header *FlowSetHeader) (interface{}, error) {
var set DataFlowSet
set.Id = header.Id
set.Length = header.Length
set.Data = data
return set, nil
}
func parseFlowSet(buf *bytes.Buffer) (interface{}, error) {
var setHeader FlowSetHeader
if buf.Len() < binary.Size(setHeader) {
return nil, errorMissingData(binary.Size(setHeader) - buf.Len())
}
binary.Read(buf, binary.BigEndian, &setHeader)
setDataLen := int(setHeader.Length) - binary.Size(setHeader)
if setDataLen > buf.Len() {
return nil, errorMissingData(setDataLen - buf.Len())
}
switch {
case setHeader.Id == 0:
return parseTemplateFlowSet(buf.Next(setDataLen), &setHeader)
case setHeader.Id == 1:
return parseOptionsTemplateFlowSet(buf.Next(setDataLen), &setHeader)
default:
return parseDataFlowSet(buf.Next(setDataLen), &setHeader)
}
}
// Decode is the main function of this package. It converts raw packet bytes to
// Packet struct.
func Decode(data []byte) (*Packet, error) {
var p Packet
var err error
// Create local copy of the "data" in case "data" slice is reused
// by the caller.
localData := make([]byte, len(data))
copy(localData, data)
buf := bytes.NewBuffer(localData)
if err := chainReads(buf, binary.BigEndian,
&p.Version,
&p.Count,
&p.SysUpTime,
&p.UnixSecs,
&p.SequenceNumber,
&p.SourceId,
); err != nil {
return nil, err
}
if p.Version != 9 {
return nil, errorIncompatibleVersion(p.Version)
}
p.FlowSets = make([]interface{}, 0, p.Count)
for i := 0; buf.Len() > 0 && i < int(p.Count); i++ {
p.FlowSets = p.FlowSets[0 : i+1]
p.FlowSets[i], err = parseFlowSet(buf)
if err != nil {
return nil, err
}
}
if buf.Len() > 0 {
return nil, errorExtraBytes(buf.Len())
}
return &p, nil
}
func chainReads(r io.Reader, order binary.ByteOrder, args ...interface{}) error {
var err error
for _, arg := range args {
err = binary.Read(r, order, arg)
if err != nil {
return err
}
}
return nil
}