forked from mozilla-services/FindMyDevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
551 lines (506 loc) · 11.8 KB
/
message.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
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Mike Trinkala (trink@mozilla.com)
#
# ***** END LICENSE BLOCK *****/
// Extensions to make Message more useable in our current code outside the scope
// of protocol buffers. See message.pb.go for the actually message definition.
/*
Internal message representation.
*/
package message
import (
"bytes"
"fmt"
"reflect"
)
const (
HEADER_DELIMITER_SIZE = 2 // record separator + len
HEADER_FRAMING_SIZE = HEADER_DELIMITER_SIZE + 1 // unit separator
MAX_HEADER_SIZE = 255
MAX_MESSAGE_SIZE = 64 * 1024
MAX_RECORD_SIZE = HEADER_FRAMING_SIZE + MAX_HEADER_SIZE + MAX_MESSAGE_SIZE
RECORD_SEPARATOR = uint8(0x1e)
UNIT_SEPARATOR = uint8(0x1f)
UUID_SIZE = 16
)
type MessageSigningConfig struct {
Name string `toml:"name"`
Hash string `toml:"hmac_hash"`
Key string `toml:"hmac_key"`
Version uint32 `toml:"version"`
}
func (h *Header) SetMessageLength(v uint32) {
if h != nil {
if h.MessageLength == nil {
h.MessageLength = new(uint32)
}
*h.MessageLength = v
}
}
func (h *Header) SetHmacHashFunction(v Header_HmacHashFunction) {
if h != nil {
if h.HmacHashFunction == nil {
h.HmacHashFunction = new(Header_HmacHashFunction)
}
*h.HmacHashFunction = v
}
}
func (h *Header) SetHmacSigner(v string) {
if h != nil {
h.HmacSigner = &v
}
}
func (h *Header) SetHmacKeyVersion(v uint32) {
if h != nil {
if h.HmacKeyVersion == nil {
h.HmacKeyVersion = new(uint32)
}
*h.HmacKeyVersion = v
}
}
func (h *Header) SetHmac(v []byte) {
if h != nil {
if cap(h.Hmac) < len(v) {
h.Hmac = make([]byte, len(v))
}
copy(h.Hmac, v)
}
}
func (m *Message) SetUuid(v []byte) {
if m != nil {
if cap(m.Uuid) != UUID_SIZE {
m.Uuid = make([]byte, UUID_SIZE)
}
copy(m.Uuid, v)
}
}
func (m *Message) SetTimestamp(v int64) {
if m != nil {
if m.Timestamp == nil {
m.Timestamp = new(int64)
}
*m.Timestamp = v
}
}
func (m *Message) SetType(v string) {
if m != nil {
m.Type = &v
}
}
func (m *Message) SetLogger(v string) {
if m != nil {
m.Logger = &v
}
}
func (m *Message) SetSeverity(v int32) {
if m != nil {
if m.Severity == nil {
m.Severity = new(int32)
}
*m.Severity = v
}
}
func (m *Message) SetPayload(v string) {
if m != nil {
m.Payload = &v
}
}
func (m *Message) SetEnvVersion(v string) {
if m != nil {
m.EnvVersion = &v
}
}
func (m *Message) SetPid(v int32) {
if m != nil {
if m.Pid == nil {
m.Pid = new(int32)
}
*m.Pid = v
}
}
func (m *Message) SetHostname(v string) {
if m != nil {
m.Hostname = &v
}
}
// Message assignment operator
func (src *Message) Copy(dst *Message) {
if src == nil || dst == nil || src == dst {
return
}
if cap(src.Uuid) > 0 {
dst.SetUuid(src.Uuid)
} else {
dst.Uuid = nil
}
if src.Timestamp != nil {
dst.SetTimestamp(*src.Timestamp)
} else {
dst.Timestamp = nil
}
if src.Type != nil {
dst.SetType(*src.Type)
} else {
dst.Type = nil
}
if src.Logger != nil {
dst.SetLogger(*src.Logger)
} else {
dst.Logger = nil
}
if src.Severity != nil {
dst.SetSeverity(*src.Severity)
} else {
dst.Severity = nil
}
if src.Payload != nil {
dst.SetPayload(*src.Payload)
} else {
dst.Payload = nil
}
if src.EnvVersion != nil {
dst.SetEnvVersion(*src.EnvVersion)
} else {
dst.EnvVersion = nil
}
if src.Pid != nil {
dst.SetPid(*src.Pid)
} else {
dst.Pid = nil
}
if src.Hostname != nil {
dst.SetHostname(*src.Hostname)
} else {
dst.Hostname = nil
}
dst.Fields = make([]*Field, len(src.Fields))
for i, v := range src.Fields {
dst.Fields[i] = CopyField(v)
}
// ignore XXX_unrecognized
}
// Message copy constructor
func CopyMessage(src *Message) *Message {
if src == nil {
return nil
}
dst := &Message{}
src.Copy(dst)
return dst
}
func getValueType(v reflect.Value) (t Field_ValueType, err error) {
switch v.Kind() {
case reflect.String:
t = Field_STRING
case reflect.Array, reflect.Slice:
if v.Type().Elem().Kind() == reflect.Uint8 {
t = Field_BYTES
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
t = Field_INTEGER
case reflect.Float32, reflect.Float64:
t = Field_DOUBLE
case reflect.Bool:
t = Field_BOOL
default:
err = fmt.Errorf("unsupported value kind: %v type: %v", v.Kind(), v.Type())
}
return
}
// Adds a Field (name/value) pair to the message
func (m *Message) AddField(f *Field) {
if m == nil {
return
}
l := len(m.Fields)
c := cap(m.Fields)
if l == c {
tmp := make([]*Field, l+1, c*2+1)
copy(tmp, m.Fields)
m.Fields = tmp
} else {
m.Fields = m.Fields[0 : l+1]
}
m.Fields[l] = f
}
// Field constructor
func NewField(name string, value interface{}, representation string) (f *Field, err error) {
v := reflect.ValueOf(value)
t, err := getValueType(v)
if err == nil {
f = NewFieldInit(name, t, representation)
f.AddValue(value)
}
return
}
// Field initializer sets up the key, value type, and format but does not actually add a value
func NewFieldInit(name string, valueType Field_ValueType, representation string) *Field {
f := &Field{}
f.Name = new(string)
*f.Name = name
f.ValueType = new(Field_ValueType)
*f.ValueType = valueType
f.Representation = new(string)
*f.Representation = representation
return f
}
// Creates an array of values in this field, of the same type, in the order they were added
func (f *Field) AddValue(value interface{}) error {
if f == nil {
return fmt.Errorf("Field is nil")
}
v := reflect.ValueOf(value)
t, err := getValueType(v)
if err != nil {
return err
}
if t != f.GetValueType() {
return fmt.Errorf("The field contains: %v; attempted to add %v",
Field_ValueType_name[int32(*f.ValueType)], Field_ValueType_name[int32(t)])
}
switch f.GetValueType() {
case Field_STRING:
l := len(f.ValueString)
c := cap(f.ValueString)
if l == c {
tmp := make([]string, l+1, c*2+1)
copy(tmp, f.ValueString)
f.ValueString = tmp
} else {
f.ValueString = f.ValueString[0 : l+1]
}
f.ValueString[l] = v.String()
case Field_BYTES:
l := len(f.ValueBytes)
c := cap(f.ValueBytes)
if l == c {
tmp := make([][]byte, l+1, c*2+1)
copy(tmp, f.ValueBytes)
f.ValueBytes = tmp
} else {
f.ValueBytes = f.ValueBytes[0 : l+1]
}
b := v.Bytes()
f.ValueBytes[l] = make([]byte, len(b))
copy(f.ValueBytes[l], b)
case Field_INTEGER:
l := len(f.ValueInteger)
c := cap(f.ValueInteger)
if l == c {
tmp := make([]int64, l+1, c*2+1)
copy(tmp, f.ValueInteger)
f.ValueInteger = tmp
} else {
f.ValueInteger = f.ValueInteger[0 : l+1]
}
f.ValueInteger[l] = v.Int()
case Field_DOUBLE:
l := len(f.ValueDouble)
c := cap(f.ValueDouble)
if l == c {
tmp := make([]float64, l+1, c*2+1)
copy(tmp, f.ValueDouble)
f.ValueDouble = tmp
} else {
f.ValueDouble = f.ValueDouble[0 : l+1]
}
f.ValueDouble[l] = v.Float()
case Field_BOOL:
l := len(f.ValueBool)
c := cap(f.ValueBool)
if l == c {
tmp := make([]bool, l+1, c*2+1)
copy(tmp, f.ValueBool)
f.ValueBool = tmp
} else {
f.ValueBool = f.ValueBool[0 : l+1]
}
f.ValueBool[l] = v.Bool()
}
return nil
}
// Helper function that returns the appropriate value object.
func (f *Field) GetValue() (value interface{}) {
switch f.GetValueType() {
case Field_STRING:
if len(f.ValueString) > 0 {
value = f.ValueString[0]
}
case Field_BYTES:
if len(f.ValueBytes) > 0 {
value = f.ValueBytes[0]
}
case Field_INTEGER:
if len(f.ValueInteger) > 0 {
value = f.ValueInteger[0]
}
case Field_DOUBLE:
if len(f.ValueDouble) > 0 {
value = f.ValueDouble[0]
}
case Field_BOOL:
if len(f.ValueBool) > 0 {
value = f.ValueBool[0]
}
}
return
}
// Field copy constructor
func CopyField(src *Field) *Field {
if src == nil {
return nil
}
dst := NewFieldInit(*src.Name, *src.ValueType, *src.Representation)
if src.ValueString != nil {
dst.ValueString = make([]string, len(src.ValueString))
copy(dst.ValueString, src.ValueString)
}
if src.ValueBytes != nil {
dst.ValueBytes = make([][]byte, len(src.ValueBytes))
copy(dst.ValueBytes, src.ValueBytes)
}
if src.ValueInteger != nil {
dst.ValueInteger = make([]int64, len(src.ValueInteger))
copy(dst.ValueInteger, src.ValueInteger)
}
if src.ValueDouble != nil {
dst.ValueDouble = make([]float64, len(src.ValueDouble))
copy(dst.ValueDouble, src.ValueDouble)
}
if src.ValueBool != nil {
dst.ValueBool = make([]bool, len(src.ValueBool))
copy(dst.ValueBool, src.ValueBool)
}
return dst
}
// FindFirstField finds and returns the first field with the specified name
// if not found nil is returned
func (m *Message) FindFirstField(name string) *Field {
if m == nil {
return nil
}
if m.Fields != nil {
for i := 0; i < len(m.Fields); i++ {
if m.Fields[i].Name != nil && *m.Fields[i].Name == name {
return m.Fields[i]
}
}
}
return nil
}
// GetFieldValue helper function to simplify extracting single value fields
func (m *Message) GetFieldValue(name string) (value interface{}, ok bool) {
if m == nil {
return
}
f := m.FindFirstField(name)
if f == nil {
return
}
return f.GetValue(), true
}
// FindAllFields finds and returns all the fields with the specified name
// if not found a nil slice is returned
func (m *Message) FindAllFields(name string) (all []*Field) {
if m == nil {
return
}
if m.Fields != nil {
for _, v := range m.Fields {
if v != nil && *v.Name == name {
l := len(all)
c := cap(all)
if l == c {
tmp := make([]*Field, l+1, c*2+1)
copy(tmp, all)
all = tmp
} else {
all = all[0 : l+1]
}
all[l] = v
}
}
}
return
}
// Test for message equality, for use in tests.
func (m *Message) Equals(other interface{}) bool {
vSelf := reflect.ValueOf(m).Elem()
vOther := reflect.ValueOf(other).Elem()
var sField, oField reflect.Value
for i := 0; i < vSelf.NumField(); i++ {
sField = vSelf.Field(i)
oField = vOther.Field(i)
switch i {
case 0: // uuid
if !bytes.Equal(sField.Bytes(), oField.Bytes()) {
return false
}
case 1, 2, 3, 4, 5, 6, 7, 8:
if sField.Kind() == reflect.Ptr {
if sField.IsNil() || oField.IsNil() {
if !(sField.IsNil() && oField.IsNil()) {
return false
}
} else {
s := reflect.Indirect(sField)
o := reflect.Indirect(oField)
if s.Interface() != o.Interface() {
return false
}
}
} else {
if sField.Interface() != oField.Interface() {
return false
}
}
case 9: // Fields
if !reflect.DeepEqual(sField.Interface(), oField.Interface()) {
return false
}
case 10: // XXX_unrecognized
// ignore
}
}
return true
}
func (m *Message) GetUuidString() string {
if m != nil {
if len(m.Uuid) == UUID_SIZE {
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", m.Uuid[:4],
m.Uuid[4:6], m.Uuid[6:8], m.Uuid[8:10], m.Uuid[10:])
}
}
return ""
}
// Convenience function for creating a new integer field on a message object.
func NewIntField(m *Message, name string, val int, representation string) (err error) {
if f, err := NewField(name, val, representation); err == nil {
m.AddField(f)
}
return
}
// Convenience function for creating a new int64 field on a message object.
func NewInt64Field(m *Message, name string, val int64, representation string) {
if f, err := NewField(name, val, representation); err == nil {
m.AddField(f)
}
return
}
// Convenience function for creating and setting a string field called "name"
// on a message object.
func NewStringField(m *Message, name string, val string) {
if f, err := NewField(name, val, ""); err == nil {
m.AddField(f)
}
return
}