-
Notifications
You must be signed in to change notification settings - Fork 1
/
ie.go
199 lines (183 loc) · 5.51 KB
/
ie.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
package ipfix
import (
"encoding/binary"
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"
)
// basicListID is the IE ID of basicLists as defined by RFC6313
const basicListID = 291
const ianaPen = 0
const reversePen = 29305
// subType represents additional information needed by RFC6313 datatypes
type subType interface{}
// InformationElement represents the description of an information element according to RFC7011
type InformationElement struct {
// Name of the information element
Name string
// Pen is the enterprise number (0 for iana reserved)
Pen uint32
// ID is the information element ID
ID uint16
// Type is the associated data type
Type Type
// Length is the length of the field value
Length uint16
subType subType
}
// NewInformationElement returns an information element for the given specification. If length is 0,
// the default length for this data type is chosen.
func NewInformationElement(name string, pen uint32, id uint16, t Type, length uint16) InformationElement {
if t != IllegalType && length == 0 {
length = DefaultSize[t]
}
return InformationElement{name, pen, id, t, length, nil}
}
// NewBasicList returns an InformationElement holding the basic list according to RFC6313. If number is 0,
// a variable length list is returned.
func NewBasicList(name string, subelement InformationElement, number uint16) InformationElement {
// RFC6313: semantic + template of element + number of elements * size of element
length := 1 + uint16(subelement.templateSize()) + number*subelement.Length
if number == 0 || subelement.Length == VariableLength || number == VariableLength {
length = VariableLength
}
return InformationElement{name, ianaPen, basicListID, BasicListType, length, subelement}
}
// Reverse returns the reverse information element according to RFC5103
func (ie InformationElement) Reverse() InformationElement {
if ie.Pen == reversePen {
// this is a reverse Element
name := strings.TrimPrefix(ie.Name, "reverse")
first, len := utf8.DecodeRuneInString(name)
name = string(unicode.ToLower(first)) + string(name[len:])
return InformationElement{
Name: name,
Pen: ianaPen,
ID: ie.ID,
Type: ie.Type,
Length: ie.Length,
subType: ie.subType,
}
}
if ie.Pen == ianaPen {
// this is a non-reverse element
name := ie.Name
first, len := utf8.DecodeRuneInString(name)
name = "reverse" + string(unicode.ToUpper(first)) + string(name[len:])
return InformationElement{
Name: name,
Pen: reversePen,
ID: ie.ID,
Type: ie.Type,
Length: ie.Length,
subType: ie.subType,
}
}
panic("This element has no reverse")
}
func (ie InformationElement) String() string {
if ie.Type == BasicListType {
return fmt.Sprintf("basicList{allOf}\n+%s", ie.subType)
}
if ie.Pen == 0 && ie.Name != "" && ie.ID != 0 {
return ie.Name
}
// Output information element spec according to RFC7013 Section 10.1
if (ie.Length == 0 && ie.Type != IllegalType) || ie.Length == DefaultSize[ie.Type] {
return fmt.Sprintf("%s(%d/%d)<%s>", ie.Name, ie.Pen, ie.ID, ie.Type)
}
return fmt.Sprintf("%s(%d/%d)<%s>[%d]", ie.Name, ie.Pen, ie.ID, ie.Type, ie.Length)
}
func (ie InformationElement) templateSize() int {
if ie.Pen == 0 {
return 4
}
return 8
}
func (ie InformationElement) serializeTo(buffer scratchBuffer) (int, error) {
ident := ie.ID
if ie.Pen == 0 {
b, err := buffer.append(4)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint16(b[2:], uint16(ie.Length))
binary.BigEndian.PutUint16(b[0:], uint16(ident))
return 4, nil
}
ident |= 0x8000
b, err := buffer.append(8)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint32(b[4:], uint32(ie.Pen))
binary.BigEndian.PutUint16(b[2:], uint16(ie.Length))
binary.BigEndian.PutUint16(b[0:], uint16(ident))
return 8, nil
}
// ListElement returns the InformationElement of a list item and true if this InformationElement is a list.
// Otherwise an empty InformationElement and false is returned.
func (ie InformationElement) ListElement() (InformationElement, bool) {
if ie.Type != BasicListType {
return InformationElement{}, false
}
return ie.subType.(InformationElement), true
}
func (ie InformationElement) serializeDataTo(buffer scratchBuffer, value interface{}) error {
switch ie.Type {
case BasicListType:
subie, _ := ie.ListElement()
// Header according to RFC6313
written := 1
var lengthbuffer []byte
if ie.Length == VariableLength {
// RFC6313 recommends 3 byte encoding of length field
b, err := buffer.append(3)
if err != nil {
return err
}
_ = b[2]
b[0] = 0xff
lengthbuffer = b[1:3]
}
// first semantic
b, err := buffer.append(1)
if err != nil {
return err
}
b[0] = byte(UndefinedSemantic)
// followed by template header
headersize, err := subie.serializeTo(buffer)
if err != nil {
return err
}
written += headersize
// followed by all the values
if value != nil {
values := reflect.ValueOf(value)
for values.Kind() == reflect.Ptr {
values = values.Elem()
}
l := values.Len()
for i := 0; i < l; i++ {
subiesize, err := subie.Type.serializeDataTo(buffer, values.Index(i).Interface(), int(subie.Length))
if err != nil {
return err
}
written += subiesize
}
}
if ie.Length == VariableLength {
binary.BigEndian.PutUint16(lengthbuffer, uint16(written))
} else {
if written != int(ie.Length) {
return BasicListMismatchError{written / int(subie.Length), int(ie.Length) / int(subie.Length)}
}
}
default:
ie.Type.serializeDataTo(buffer, value, int(ie.Length))
}
return nil
}