-
Notifications
You must be signed in to change notification settings - Fork 7
/
field.go
91 lines (79 loc) · 2.62 KB
/
field.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
package nf9packet
import "fmt"
// Field describes type and length of a single value in a Flow Data Record.
// Field does not contain the record value itself it is just a description of
// what record value will look like.
type Field struct {
// A numeric value that represents the type of field.
Type uint16
// The length (in bytes) of the field.
Length uint16
}
// String is a convenience method for printing field name as a default field
// value string representation.
func (f Field) String() string {
return f.Name()
}
// Name returns a short field type identifier based on RFC 3954 and Cisco
// documentation. For unkown field types string "UNKNOWN_TYPE" will be returned.
func (f *Field) Name() string {
if e, ok := fieldDb[f.Type]; ok {
return e.Name
}
return fmt.Sprintf("UNKNOWN_TYPE_%d", f.Type)
}
// DefaultLength returns length of field type as specified in RFC 3954 and Cisco
// documentation. For variable length fields and unknown fields -1 is returned.
func (f *Field) DefaultLength() int {
if e, ok := fieldDb[f.Type]; ok {
return e.Length
}
return -1
}
// Description returns field type description based on RFC 3954 and Cisco
// documentation. For unkown field types string "Unknown type" will be returned.
func (f *Field) Description() string {
if e, ok := fieldDb[f.Type]; ok {
return e.Description
}
return fmt.Sprintf("Unknown type (%d)", f.Type)
}
// DataToString converts field value to string representation based on field
// type. If used with unknow field type string "n/a" will be returned.
func (f *Field) DataToString(data []byte) string {
if e, ok := fieldDb[f.Type]; ok {
return e.String(data)
}
return "n/a"
}
// DataToUint64 converts field value to uint64. This function will not generate
// errors if used with incompatible field types. Field value length can be up to
// 8 bytes, for longer values only first 8 bytes are used.
func (f *Field) DataToUint64(data []byte) uint64 {
return fieldToUInteger(data)
}
// ScopeName is the same as Name() but should be used only for Scope Fields
func (f *Field) ScopeName() string {
switch f.Type {
case 1:
return "System"
case 2:
return "Interface"
case 3:
return "Line Card"
case 4:
return "Cache"
case 5:
return "Template"
default:
return "Unknown"
}
}
// ScopeDefaultLength is the same as DefaultLength() but should be used only for Scope Fields
func (f *Field) ScopeDefaultLength() int {
return -1
}
// ScopeDescription is the same as Description but should be used only for Scope Fields
func (f *Field) ScopeDescription() string {
return "The relevant portion of the Exporter/NetFlow process to which the Options Template Record refers."
}