forked from google/tflow2
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemplates.go
178 lines (147 loc) · 5.57 KB
/
templates.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
// Copyright 2017 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nf9
import "unsafe"
const (
// numPreAllocFlowDataRecs is number of elements to pre allocate in DataRecs slice
numPreAllocFlowDataRecs = 20
)
var (
sizeOfTemplateRecordHeader = unsafe.Sizeof(TemplateRecordHeader{})
sizeOfOptionsTemplateRecordHeader = unsafe.Sizeof(OptionsTemplateRecordHeader{})
sizeOfOptionScope = unsafe.Sizeof(OptionScope{})
)
// TemplateRecordHeader represents the header of a template record
type TemplateRecordHeader struct {
// Number of fields in this Template Record. Because a Template FlowSet
// usually contains multiple Template Records, this field allows the
// Collector to determine the end of the current Template Record and
// the start of the next.
FieldCount uint16
// Each of the newly generated Template Records is given a unique
// Template ID. This uniqueness is local to the Observation Domain that
// generated the Template ID. Template IDs of Data FlowSets are numbered
// from 256 to 65535.
TemplateID uint16
}
// OptionsTemplateRecordHeader represents the header of an option template record
type OptionsTemplateRecordHeader struct {
// The length (in bytes) of any options field definitions
// contained in this Options Template Record.
OptionLength uint16
// Number of fields in this Template Record. Because a Template FlowSet
// usually contains multiple Template Records, this field allows the
// Collector to determine the end of the current Template Record and
// the start of the next.
OptionScopeLength uint16
// Each of the newly generated Template Records is given a unique
// Template ID. This uniqueness is local to the Observation Domain that
// generated the Template ID. Template IDs of Data FlowSets are numbered
// from 256 to 65535.
TemplateID uint16
}
// TemplateRecords is a single template that describes structure of a Flow Record
// (actual Netflow data).
type TemplateRecords struct {
Header *TemplateRecordHeader
// List of scopes
OptionScopes []*OptionScope
// List of fields in this Template Record.
Records []*TemplateRecord
Packet *Packet
Values [][]byte
}
// OptionScope represents an option scope in an options template flowset
type OptionScope struct {
// The length (in bytes) of the Scope field, as it would appear in
//an Options Data Record.
ScopeFieldLength uint16
//A numeric value that represents the type of field that would
//appear in the Options Template Record. Refer to the Field Type
//Definitions section.
ScopeFieldType uint16
}
//TemplateRecord represents a Template Record as described in RFC3954
type TemplateRecord struct {
// The length (in bytes) of the field.
Length uint16
// A numeric value that represents the type of field.
Type uint16
}
// FlowDataRecord is actual NetFlow data. This structure does not contain any
// information about the actual data meaning. It must be combined with
// corresponding TemplateRecord to be decoded to a single NetFlow data row.
type FlowDataRecord struct {
// List of Flow Data Record values stored in raw format as []byte
Values [][]byte
}
// sizeOfTemplateRecord is the raw size of a TemplateRecord
var sizeOfTemplateRecord = unsafe.Sizeof(TemplateRecord{})
// DecodeFlowSet uses current TemplateRecord to decode data in Data FlowSet to
// a list of Flow Data Records.
/*func (dtpl *TemplateRecords) DecodeFlowSet(set FlowSet) (list []FlowDataRecord) {
if set.Header.FlowSetID != dtpl.Header.TemplateID {
return nil
}
var record FlowDataRecord
// Pre-allocate some room for flows
list = make([]FlowDataRecord, 0, numPreAllocFlowDataRecs)
// Assume total record length must be >= 4, otherwise it is impossible
// to distinguish between padding and new record. Padding MUST be
// supported.
n := len(set.Flows)
count := 0
for n >= 4 {
record.Values, count = parseFieldValues(set.Flows[0:n], dtpl.Records)
if record.Values == nil {
return
}
list = append(list, record)
n = n - count
}
return
}*/
// DecodeFlowSet uses current TemplateRecord to decode data in Data FlowSet to
// a list of Flow Data Records.
func DecodeFlowSet(templateRecords []*TemplateRecord, set FlowSet) (list []FlowDataRecord) {
var record FlowDataRecord
// Pre-allocate some room for flows
list = make([]FlowDataRecord, 0, numPreAllocFlowDataRecs)
// Assume total record length must be >= 4, otherwise it is impossible
// to distinguish between padding and new record. Padding MUST be
// supported.
n := len(set.Flows)
count := 0
for n >= 4 {
record.Values, count = parseFieldValues(set.Flows[0:n], templateRecords)
if record.Values == nil {
return
}
list = append(list, record)
n = n - count
}
return
}
// parseFieldValues reads actual fields values from a Data Record utilizing a template
func parseFieldValues(flows []byte, fields []*TemplateRecord) ([][]byte, int) {
count := 0
n := len(flows)
values := make([][]byte, len(fields))
for i, f := range fields {
if n < int(f.Length) {
return nil, 0
}
values[i] = flows[n-int(f.Length) : n]
count += int(f.Length)
n -= int(f.Length)
}
return values, count
}