-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
598 lines (581 loc) · 12.8 KB
/
types.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package ipfix
import (
"encoding/binary"
"math"
"net"
"time"
)
// Type is a datatype according to RFC7011
type Type int
// DateTimeSeconds represents time in units of seconds from 00:00 UTC, Januray 1, 1970 according to RFC5102.
type DateTimeSeconds uint64
// DateTimeMilliseconds represents time in units of milliseconds from 00:00 UTC, Januray 1, 1970 according to RFC5102.
type DateTimeMilliseconds uint64
// DateTimeMicroseconds represents time in units of microseconds from 00:00 UTC, Januray 1, 1970 according to RFC5102.
type DateTimeMicroseconds uint64
// DateTimeNanoseconds represents time in units of nanoseconds from 00:00 UTC, Januray 1, 1970 according to RFC5102.
type DateTimeNanoseconds uint64
const (
// OctetArrayType as defined by RFC7011
OctetArrayType Type = iota
// Unsigned8Type as defined by RFC7011
Unsigned8Type
// Unsigned16Type as defined by RFC7011
Unsigned16Type
// Unsigned32Type as defined by RFC7011
Unsigned32Type
// Unsigned64Type as defined by RFC7011
Unsigned64Type
// Signed8Type as defined by RFC7011
Signed8Type
// Signed16Type as defined by RFC7011
Signed16Type
// Signed32Type as defined by RFC7011
Signed32Type
// Signed64Type as defined by RFC7011
Signed64Type
// Float32Type as defined by RFC7011
Float32Type
// Float64Type as defined by RFC7011
Float64Type
// BooleanType as defined by RFC7011
BooleanType
// MacAddressType as defined by RFC7011
MacAddressType
// StringType as defined by RFC7011
StringType
// DateTimeSecondsType as defined by RFC7011
DateTimeSecondsType
// DateTimeMillisecondsType as defined by RFC7011
DateTimeMillisecondsType
// DateTimeMicrosecondsType as defined by RFC7011
DateTimeMicrosecondsType
// DateTimeNanosecondsType as defined by RFC7011
DateTimeNanosecondsType
// Ipv4AddressType as defined by RFC7011
Ipv4AddressType
// Ipv6AddressType as defined by RFC7011
Ipv6AddressType
// BasicListType as defined by RFC7011
BasicListType
// IllegalType is an undefined type
IllegalType = -1
)
// VariableLength is the variable length specifier as defined by RFC7011
const VariableLength uint16 = 65535
// DefaultSize can be used to look up the default size of an ipfix type
var DefaultSize = [...]uint16{
VariableLength,
1,
2,
4,
8,
1,
2,
4,
8,
4,
8,
1,
6,
VariableLength,
4,
8,
8,
8,
4,
16,
VariableLength,
}
// NameToType converts the given textual representation of a type to the ipfix type. Returns IllegalType if the type is not recognised.
func NameToType(x []byte) Type {
switch string(x) {
case "octetArray":
return OctetArrayType
case "unsigned8":
return Unsigned8Type
case "unsigned16":
return Unsigned16Type
case "unsigned32":
return Unsigned32Type
case "unsigned64":
return Unsigned64Type
case "signed8":
return Signed8Type
case "signed16":
return Signed16Type
case "signed32":
return Signed32Type
case "signed64":
return Signed64Type
case "float32":
return Float32Type
case "float64":
return Float64Type
case "boolean":
return BooleanType
case "macAddress":
return MacAddressType
case "string":
return StringType
case "dateTimeSeconds":
return DateTimeSecondsType
case "dateTimeMilliseconds":
return DateTimeMillisecondsType
case "dateTimeMicroseconds":
return DateTimeMicrosecondsType
case "dateTimeNanoseconds":
return DateTimeNanosecondsType
case "ipv4Address":
return Ipv4AddressType
case "ipv6Address":
return Ipv6AddressType
}
return IllegalType
}
func (t Type) String() string {
switch t {
case OctetArrayType:
return "octetArray"
case Unsigned8Type:
return "unsigned8"
case Unsigned16Type:
return "unsigned16"
case Unsigned32Type:
return "unsigned32"
case Unsigned64Type:
return "unsigned64"
case Signed8Type:
return "signed8"
case Signed16Type:
return "signed16"
case Signed32Type:
return "signed32"
case Signed64Type:
return "signed64"
case Float32Type:
return "float32"
case Float64Type:
return "float64"
case BooleanType:
return "boolean"
case MacAddressType:
return "macAddress"
case StringType:
return "string"
case DateTimeSecondsType:
return "dateTimeSeconds"
case DateTimeMillisecondsType:
return "dateTimeMilliseconds"
case DateTimeMicrosecondsType:
return "dateTimeMicroseconds"
case DateTimeNanosecondsType:
return "dateTimeNanoseconds"
case Ipv4AddressType:
return "ipv4Address"
case Ipv6AddressType:
return "ipv6Address"
case BasicListType:
return "basicList"
case IllegalType:
return "<bad>"
}
return "unknownType"
}
//Seconds between NTP and Unix epoch
const ntp2Unix uint32 = 0x83AA7E80
func (t Type) serializeDataTo(buffer scratchBuffer, value interface{}, length int) (int, error) {
switch t {
case OctetArrayType, Ipv4AddressType, Ipv6AddressType, MacAddressType, StringType:
return serializeOctetArrayTo(buffer, t, value, length)
case Unsigned8Type, Unsigned16Type, Unsigned32Type, Unsigned64Type, Signed8Type, Signed16Type, Signed32Type, Signed64Type, BooleanType:
return serializeIntegerTo(buffer, t, value, length)
case Float32Type, Float64Type:
return serializeFloatTo(buffer, t, value, length)
case DateTimeSecondsType, DateTimeMillisecondsType, DateTimeMicrosecondsType, DateTimeNanosecondsType:
return serializeDateTimeTo(buffer, t, value, length)
}
return 0, IllegalTypeError(t)
}
func serializeOctetArrayTo(buffer scratchBuffer, t Type, value interface{}, length int) (int, error) {
var val []byte
switch v := value.(type) {
case string:
val = []byte(v)
case []byte:
val = v
case net.IP:
val = []byte(v)
case net.HardwareAddr:
val = []byte(v)
case nil:
// val is already nil
default:
return 0, ConversionError{t, value}
}
if length == 0 {
length = int(DefaultSize[t])
}
if length == int(VariableLength) {
length = len(val)
written := length
var assign []byte
if length == 0 {
b, err := buffer.append(length + 1)
if err != nil {
return 0, err
}
b[0] = 0
return 1, nil
} else if length < 255 {
written++
b, err := buffer.append(length + 1)
if err != nil {
return 0, err
}
_ = b[1]
b[0] = uint8(length)
assign = b[1:]
} else {
written += 3
b, err := buffer.append(length + 3)
if err != nil {
return 0, err
}
_ = b[2]
b[0] = 0xff
binary.BigEndian.PutUint16(b[1:3], uint16(length))
assign = b[3:]
}
copy(assign, val)
return written, nil
}
if len(val) == length {
b, err := buffer.append(length)
if err != nil {
return 0, err
}
copy(b, val)
return length, nil
}
if t == Ipv4AddressType || t == Ipv6AddressType || t == MacAddressType {
if val == nil {
tmp, err := buffer.append(length)
if err != nil {
return 0, err
}
for i := range tmp {
tmp[i] = 0
}
return length, nil
}
return 0, ConversionError{t, value}
}
var clear []byte
assign, err := buffer.append(length)
if err != nil {
return 0, err
}
copy(assign, val)
clear = assign[len(val):]
for i := range clear {
clear[i] = 0
}
return length, nil
}
func serializeIntegerTo(buffer scratchBuffer, t Type, value interface{}, length int) (int, error) {
var val uint64
switch v := value.(type) {
case float64:
val = uint64(v)
case float32:
val = uint64(v)
case int64:
val = uint64(v)
case int32:
val = uint64(v)
case int16:
val = uint64(v)
case int8:
val = uint64(v)
case int:
val = uint64(v)
case uint64:
val = v
case uint32:
val = uint64(v)
case uint16:
val = uint64(v)
case uint8:
val = uint64(v)
case uint:
val = uint64(v)
case nil:
// val already 0
case bool:
if v {
val = 1
} else {
val = 2
}
default:
return 0, ConversionError{t, value}
}
if length == 0 {
length = int(DefaultSize[t])
}
switch length {
case 1:
b, err := buffer.append(1)
if err != nil {
return 0, err
}
b[0] = byte(val)
return 1, nil
case 2:
b, err := buffer.append(2)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint16(b, uint16(val))
return 2, nil
case 3:
b, err := buffer.append(3)
if err != nil {
return 0, err
}
_ = b[2]
b[0] = byte(val >> 16)
b[1] = byte(val >> 8)
b[2] = byte(val)
return 3, nil
case 4:
b, err := buffer.append(4)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint32(b, uint32(val))
return 4, nil
case 5:
b, err := buffer.append(5)
if err != nil {
return 0, err
}
_ = b[4]
b[0] = byte(val >> 32)
b[1] = byte(val >> 24)
b[2] = byte(val >> 16)
b[3] = byte(val >> 8)
b[4] = byte(val)
return 5, nil
case 6:
b, err := buffer.append(6)
if err != nil {
return 0, err
}
_ = b[5]
b[0] = byte(val >> 40)
b[1] = byte(val >> 32)
b[2] = byte(val >> 24)
b[3] = byte(val >> 16)
b[4] = byte(val >> 8)
b[5] = byte(val)
return 6, nil
case 7:
b, err := buffer.append(7)
if err != nil {
return 0, err
}
_ = b[6]
b[0] = byte(val >> 48)
b[1] = byte(val >> 40)
b[2] = byte(val >> 32)
b[3] = byte(val >> 24)
b[4] = byte(val >> 16)
b[5] = byte(val >> 8)
b[6] = byte(val)
return 7, nil
case 8:
b, err := buffer.append(8)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint64(b, val)
return 8, nil
default:
return 0, SizeError{t, length}
}
}
func serializeFloatTo(buffer scratchBuffer, t Type, value interface{}, length int) (int, error) {
if t == Float32Type {
var val float32
switch v := value.(type) {
case float64:
val = float32(v)
case float32:
val = v
case int64:
val = float32(v)
case int32:
val = float32(v)
case int16:
val = float32(v)
case int8:
val = float32(v)
case int:
val = float32(v)
case uint64:
val = float32(v)
case uint32:
val = float32(v)
case uint16:
val = float32(v)
case uint8:
val = float32(v)
case uint:
val = float32(v)
case nil:
// val already 0
case bool:
if v {
val = 1
} else {
val = 2
}
default:
return 0, ConversionError{t, value}
}
b, err := buffer.append(4)
if err != nil {
return 0, err
}
bits := math.Float32bits(val)
binary.BigEndian.PutUint32(b, bits)
return 4, nil
}
var val float64
switch v := value.(type) {
case float64:
val = v
case float32:
val = float64(v)
case int64:
val = float64(v)
case int32:
val = float64(v)
case int16:
val = float64(v)
case int8:
val = float64(v)
case int:
val = float64(v)
case uint64:
val = float64(v)
case uint32:
val = float64(v)
case uint16:
val = float64(v)
case uint8:
val = float64(v)
case uint:
val = float64(v)
case nil:
// val already 0
case bool:
if v {
val = 1
} else {
val = 2
}
default:
return 0, ConversionError{t, value}
}
switch length {
case 4:
b, err := buffer.append(4)
if err != nil {
return 0, err
}
bits := math.Float32bits(float32(val))
binary.BigEndian.PutUint32(b, bits)
case 8:
b, err := buffer.append(8)
if err != nil {
return 0, err
}
bits := math.Float64bits(val)
binary.BigEndian.PutUint64(b, bits)
default:
return 0, SizeError{t, length}
}
return length, nil
}
func serializeDateTimeTo(buffer scratchBuffer, t Type, value interface{}, length int) (int, error) {
var seconds, nanoseconds uint64
switch v := value.(type) {
case time.Time:
seconds = uint64(v.Unix())
nanoseconds = uint64(v.Nanosecond())
case DateTimeMilliseconds:
seconds = uint64(v) / 1e3
nanoseconds = (uint64(v) % 1e3) * 1e6
case DateTimeMicroseconds:
seconds = uint64(v) / 1e6
nanoseconds = (uint64(v) % 1e6) * 1e3
case DateTimeNanoseconds:
seconds = uint64(v) / 1e9
nanoseconds = uint64(v) % 1e9
case uint64:
seconds = v / 1e9
nanoseconds = v % 1e9
case int64:
seconds = uint64(v) / 1e9
nanoseconds = uint64(v) % 1e9
case float64:
seconds = uint64(v) / 1e9
nanoseconds = uint64(v) % 1e9
case nil:
// val already 0
default:
return 0, ConversionError{t, value}
}
switch t {
case DateTimeSecondsType:
b, err := buffer.append(4)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint32(b, uint32(seconds))
return 4, nil
case DateTimeMillisecondsType:
b, err := buffer.append(8)
if err != nil {
return 0, err
}
binary.BigEndian.PutUint64(b, uint64(seconds*1e3+nanoseconds/1e6))
return 8, nil
case DateTimeMicrosecondsType:
//NTP epoch as 32bit seconds + 32bit fraction (~244ps)
//-> get time in Unixpoch seconds, add ntp epoch to unix epoch offset
//-> shift nanoseconds 32 bit to the left divide by nano (1e9)
//according to RFC7011 the last 11 bits should be zero (0xFFFFF800) to get micro seconds (~.447 microsecond resolution)
b, err := buffer.append(8)
if err != nil {
return 0, err
}
_ = b[7]
binary.BigEndian.PutUint32(b[:4], uint32(seconds)+ntp2Unix)
binary.BigEndian.PutUint32(b[4:8], uint32((nanoseconds<<32)/1e9)&0xFFFFF800)
return 8, nil
case DateTimeNanosecondsType:
b, err := buffer.append(8)
if err != nil {
return 0, err
}
_ = b[7]
binary.BigEndian.PutUint32(b[:4], uint32(seconds)+ntp2Unix)
binary.BigEndian.PutUint32(b[4:8], uint32((nanoseconds<<32)/1e9)+1)
return 8, nil
}
return 0, ConversionError{t, value}
}