forked from calmh/ipfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.go
174 lines (153 loc) · 4.71 KB
/
interpreter.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
package ipfix
import (
"bytes"
"encoding/binary"
"net"
)
// Interpreter provides translation between the raw bytes of a DataRecord
// and the actual values as specified by the corresponding template.
type Interpreter struct {
dictionary fieldDictionary
session *Session
}
// IPFIX type of an Information Element ("Field").
type FieldType string
// The available field types as defined by RFC 5102.
const (
Uint8 FieldType = "unsigned8"
Uint16 FieldType = "unsigned16"
Uint32 FieldType = "unsigned32"
Uint64 FieldType = "unsigned64"
Int8 FieldType = "signed8"
Int16 FieldType = "signed16"
Int32 FieldType = "signed32"
Int64 FieldType = "signed64"
Float32 FieldType = "float32"
Float64 FieldType = "float64"
Boolean FieldType = "boolean"
MacAddress FieldType = "macAddress"
OctetArray FieldType = "octetArray"
String FieldType = "string"
DateTimeSeconds FieldType = "dateTimeSeconds"
DateTimeMilliseconds FieldType = "dateTimeMilliseconds"
DateTimeMicroseconds FieldType = "dateTimeMicroseconds"
DateTimeNanoseconds FieldType = "dateTimeNanoseconds"
Ipv4Address FieldType = "ipv4Address"
Ipv6Address FieldType = "ipv6Address"
)
// DictionaryEntry provied a mapping between an (Enterprise, Field) pair and a Name and Type.
type DictionaryEntry struct {
Name string
FieldId uint16
EnterpriseId uint32
Type FieldType
}
type dictionaryKey struct {
EnterpriseId uint32
FieldId uint16
}
type fieldDictionary map[dictionaryKey]DictionaryEntry
// An InterpretedField is a field with the field name filled in and the value
// converted to the appropriate type. If this is not possible (because the
// name and type of the field is unknown at the time of interpretation), Name
// will be the empty string, Value will be a nil interface and RawValue will
// contain the original bytes.
type InterpretedField struct {
Name string
EnterpriseId uint32
FieldId uint16
Value interface{}
RawValue []byte
}
// NewInterpreter craets a new Interpreter based on the specified Session.
func NewInterpreter(s *Session) *Interpreter {
return &Interpreter{builtinDictionary, s}
}
// Interpret a raw DataRecord into a list of InterpretedFields.
func (i *Interpreter) Interpret(ds *DataRecord) []InterpretedField {
tpl := i.session.templates[ds.TemplateId]
if tpl == nil {
return nil
}
fieldList := make([]InterpretedField, len(tpl))
for j, field := range tpl {
intf := InterpretedField{FieldId: field.FieldId, EnterpriseId: field.EnterpriseId}
entry, ok := i.dictionary[dictionaryKey{field.EnterpriseId, field.FieldId}]
if !ok {
intf.RawValue = ds.Fields[j]
} else {
intf.Name = entry.Name
intf.Value = interpretBytes(ds.Fields[j], entry.Type)
}
fieldList[j] = intf
}
return fieldList
}
// Interpret a raw DataRecord into a map of InterpretedFields.
func (i *Interpreter) InterpretMap(ds *DataRecord) map[string]InterpretedField {
tpl := i.session.templates[ds.TemplateId]
if tpl == nil {
return nil
}
fieldMap := make(map[string]InterpretedField, len(tpl))
for j, field := range tpl {
intf := InterpretedField{FieldId: field.FieldId, EnterpriseId: field.EnterpriseId}
entry, ok := i.dictionary[dictionaryKey{field.EnterpriseId, field.FieldId}]
if !ok {
intf.RawValue = ds.Fields[j]
} else {
intf.Name = entry.Name
intf.Value = interpretBytes(ds.Fields[j], entry.Type)
}
fieldMap[intf.Name] = intf
}
return fieldMap
}
// Add a DictionaryEntry (containing a vendor field) to the dictionary used by Interpret.
func (i *Interpreter) AddDictionaryEntry(e DictionaryEntry) {
i.dictionary[dictionaryKey{e.EnterpriseId, e.FieldId}] = e
}
func interpretBytes(bs []byte, t FieldType) interface{} {
switch t {
case Uint8, Uint16, Uint32, Uint64:
var s uint64
for _, b := range bs {
s = s << 8
s += uint64(b)
}
return s
case Int8:
return int8(bs[0])
case Int16:
var s int16
binary.Read(bytes.NewBuffer(bs), binary.BigEndian, &s)
return s
case Int32, DateTimeSeconds:
var s int32
binary.Read(bytes.NewBuffer(bs), binary.BigEndian, &s)
return s
case Int64:
var s int64
binary.Read(bytes.NewBuffer(bs), binary.BigEndian, &s)
return s
case Float32:
var s float32
binary.Read(bytes.NewBuffer(bs), binary.BigEndian, &s)
return s
case Float64:
var s float64
binary.Read(bytes.NewBuffer(bs), binary.BigEndian, &s)
return s
case Boolean:
return bs[0] != 0
case MacAddress, OctetArray:
return bs
case String:
return string(bs)
case Ipv4Address:
return net.IP(bs)
case Ipv6Address:
return net.IP(bs)
}
return bs
}