-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
718 lines (648 loc) · 19.1 KB
/
properties.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package mqttpackets
import (
"bytes"
"fmt"
"io"
)
// PropPayloadFormat, etc are the list of property codes for the
// MQTT packet properties
const (
PropPayloadFormat byte = 1
PropMessageExpiry byte = 2
PropContentType byte = 3
PropResponseTopic byte = 8
PropCorrelationData byte = 9
PropSubscriptionIdentifier byte = 11
PropSessionExpiryInterval byte = 17
PropAssignedClientID byte = 18
PropServerKeepAlive byte = 19
PropAuthMethod byte = 21
PropAuthData byte = 22
PropRequestProblemInfo byte = 23
PropWillDelayInterval byte = 24
PropRequestResponseInfo byte = 25
PropResponseInfo byte = 26
PropServerReference byte = 28
PropReasonString byte = 31
PropReceiveMaximum byte = 33
PropTopicAliasMaximum byte = 34
PropTopicAlias byte = 35
PropMaximumQOS byte = 36
PropRetainAvailable byte = 37
PropUser byte = 38
PropMaximumPacketSize byte = 39
PropWildcardSubAvailable byte = 40
PropSubIDAvailable byte = 41
PropSharedSubAvailable byte = 42
)
// User is a struct for the User properties, originally it was a map
// then it was pointed out that user properties are allowed to appear
// more than once
type User struct {
Key, Value string
}
// Properties is a struct representing the all the described properties
// allowed by the MQTT protocol, determining the validity of a property
// relvative to the packettype it was received in is provided by the
// ValidateID function
type Properties struct {
// PayloadFormat indicates the format of the payload of the message
// 0 is unspecified bytes
// 1 is UTF8 encoded character data
PayloadFormat *byte
// MessageExpiry is the lifetime of the message in seconds
MessageExpiry *uint32
// ContentType is a UTF8 string describing the content of the message
// for example it could be a MIME type
ContentType string
// ResponseTopic is a UTF8 string indicating the topic name to which any
// response to this message should be sent
ResponseTopic string
// CorrelationData is binary data used to associate future response
// messages with the original request message
CorrelationData []byte
// SubscriptionIdentifier is an identifier of the subscription to which
// the Publish matched
SubscriptionIdentifier *int
// SessionExpiryInterval is the time in seconds after a client disconnects
// that the server should retain the session information (subscriptions etc)
SessionExpiryInterval *uint32
// AssignedClientID is the server assigned client identifier in the case
// that a client connected without specifying a clientID the server
// generates one and returns it in the Connack
AssignedClientID string
// ServerKeepAlive allows the server to specify in the Connack packet
// the time in seconds to be used as the keep alive value
ServerKeepAlive *uint16
// AuthMethod is a UTF8 string containing the name of the authentication
// method to be used for extended authentication
AuthMethod string
// AuthData is binary data containing authentication data
AuthData []byte
// RequestProblemInfo is used by the Client to indicate to the server to
// include the Reason String and/or User Properties in case of failures
RequestProblemInfo *byte
// WillDelayInterval is the number of seconds the server waits after the
// point at which it would otherwise send the will message before sending
// it. The client reconnecting before that time expires causes the server
// to cancel sending the will
WillDelayInterval *uint32
// RequestResponseInfo is used by the Client to request the Server provide
// Response Information in the Connack
RequestResponseInfo *byte
// ResponseInfo is a UTF8 encoded string that can be used as the basis for
// createing a Response Topic. The way in which the Client creates a
// Response Topic from the Response Information is not defined. A common
// use of this is to pass a globally unique portion of the topic tree which
// is reserved for this Client for at least the lifetime of its Session. This
// often cannot just be a random name as both the requesting Client and the
// responding Client need to be authorized to use it. It is normal to use this
// as the root of a topic tree for a particular Client. For the Server to
// return this information, it normally needs to be correctly configured.
// Using this mechanism allows this configuration to be done once in the
// Server rather than in each Client
ResponseInfo string
// ServerReference is a UTF8 string indicating another server the client
// can use
ServerReference string
// ReasonString is a UTF8 string representing the reason associated with
// this response, intended to be human readable for diagnostic purposes
ReasonString string
// ReceiveMaximum is the maximum number of QOS1 & 2 messages allowed to be
// 'inflight' (not having received a PUBACK/PUBCOMP response for)
ReceiveMaximum *uint16
// TopicAliasMaximum is the highest value permitted as a Topic Alias
TopicAliasMaximum *uint16
// TopicAlias is used in place of the topic string to reduce the size of
// packets for repeated messages on a topic
TopicAlias *uint16
// MaximumQOS is the highest QOS level permitted for a Publish
MaximumQOS *byte
// RetainAvailable indicates whether the server supports messages with the
// retain flag set
RetainAvailable *byte
// User is a slice of user provided properties (key and value)
User []User
// MaximumPacketSize allows the client or server to specify the maximum packet
// size in bytes that they support
MaximumPacketSize *uint32
// WildcardSubAvailable indicates whether wildcard subscriptions are permitted
WildcardSubAvailable *byte
// SubIDAvailable indicates whether subscription identifiers are supported
SubIDAvailable *byte
// SharedSubAvailable indicates whether shared subscriptions are supported
SharedSubAvailable *byte
}
// Pack takes all the defined properties for an Properties and produces
// a slice of bytes representing the wire format for the information
func (i *Properties) Pack(p byte) []byte {
var b bytes.Buffer
if i == nil {
return nil
}
if p == PUBLISH {
if i.PayloadFormat != nil {
b.WriteByte(PropPayloadFormat)
b.WriteByte(*i.PayloadFormat)
}
if i.MessageExpiry != nil {
b.WriteByte(PropMessageExpiry)
writeUint32(*i.MessageExpiry, &b)
}
if i.ContentType != "" {
b.WriteByte(PropContentType)
writeString(i.ContentType, &b)
}
if i.ResponseTopic != "" {
b.WriteByte(PropResponseTopic)
writeString(i.ResponseTopic, &b)
}
if i.CorrelationData != nil && len(i.CorrelationData) > 0 {
b.WriteByte(PropCorrelationData)
writeBinary(i.CorrelationData, &b)
}
if i.TopicAlias != nil {
b.WriteByte(PropTopicAlias)
writeUint16(*i.TopicAlias, &b)
}
}
if p == PUBLISH || p == SUBSCRIBE {
if i.SubscriptionIdentifier != nil {
b.WriteByte(PropSubscriptionIdentifier)
encodeVBIdirect(*i.SubscriptionIdentifier, &b)
}
}
if p == CONNECT || p == CONNACK {
if i.ReceiveMaximum != nil {
b.WriteByte(PropReceiveMaximum)
writeUint16(*i.ReceiveMaximum, &b)
}
if i.TopicAliasMaximum != nil {
b.WriteByte(PropTopicAliasMaximum)
writeUint16(*i.TopicAliasMaximum, &b)
}
if i.MaximumQOS != nil {
b.WriteByte(PropMaximumQOS)
b.WriteByte(*i.MaximumQOS)
}
if i.MaximumPacketSize != nil {
b.WriteByte(PropMaximumPacketSize)
writeUint32(*i.MaximumPacketSize, &b)
}
}
if p == CONNACK {
if i.AssignedClientID != "" {
b.WriteByte(PropAssignedClientID)
writeString(i.AssignedClientID, &b)
}
if i.ServerKeepAlive != nil {
b.WriteByte(PropServerKeepAlive)
writeUint16(*i.ServerKeepAlive, &b)
}
if i.WildcardSubAvailable != nil {
b.WriteByte(PropWildcardSubAvailable)
b.WriteByte(*i.WildcardSubAvailable)
}
if i.SubIDAvailable != nil {
b.WriteByte(PropSubIDAvailable)
b.WriteByte(*i.SubIDAvailable)
}
if i.SharedSubAvailable != nil {
b.WriteByte(PropSharedSubAvailable)
b.WriteByte(*i.SharedSubAvailable)
}
if i.RetainAvailable != nil {
b.WriteByte(PropRetainAvailable)
b.WriteByte(*i.RetainAvailable)
}
if i.ResponseInfo != "" {
b.WriteByte(PropResponseInfo)
writeString(i.ResponseInfo, &b)
}
}
if p == CONNECT {
if i.RequestProblemInfo != nil {
b.WriteByte(PropRequestProblemInfo)
b.WriteByte(*i.RequestProblemInfo)
}
if i.WillDelayInterval != nil {
b.WriteByte(PropWillDelayInterval)
writeUint32(*i.WillDelayInterval, &b)
}
if i.RequestResponseInfo != nil {
b.WriteByte(PropRequestResponseInfo)
b.WriteByte(*i.RequestResponseInfo)
}
}
if p == CONNECT || p == CONNACK || p == DISCONNECT {
if i.SessionExpiryInterval != nil {
b.WriteByte(PropSessionExpiryInterval)
writeUint32(*i.SessionExpiryInterval, &b)
}
}
if p == CONNECT || p == CONNACK || p == AUTH {
if i.AuthMethod != "" {
b.WriteByte(PropAuthMethod)
writeString(i.AuthMethod, &b)
}
if i.AuthData != nil && len(i.AuthData) > 0 {
b.WriteByte(PropAuthData)
writeBinary(i.AuthData, &b)
}
}
if p == CONNACK || p == DISCONNECT {
if i.ServerReference != "" {
b.WriteByte(PropServerReference)
writeString(i.ServerReference, &b)
}
}
if p != CONNECT {
if i.ReasonString != "" {
b.WriteByte(PropReasonString)
writeString(i.ReasonString, &b)
}
}
for _, v := range i.User {
b.WriteByte(PropUser)
writeString(v.Key, &b)
writeString(v.Value, &b)
}
return b.Bytes()
}
// PackBuf will create a bytes.Buffer of the packed properties, it
// will only pack the properties appropriate to the packet type p
// even though other properties may exist, it will silently ignore
// them
func (i *Properties) PackBuf(p byte) *bytes.Buffer {
var b bytes.Buffer
if i == nil {
return nil
}
if p == PUBLISH {
if i.PayloadFormat != nil {
b.WriteByte(PropPayloadFormat)
b.WriteByte(*i.PayloadFormat)
}
if i.MessageExpiry != nil {
b.WriteByte(PropMessageExpiry)
writeUint32(*i.MessageExpiry, &b)
}
if i.ContentType != "" {
b.WriteByte(PropContentType)
writeString(i.ContentType, &b)
}
if i.ResponseTopic != "" {
b.WriteByte(PropResponseTopic)
writeString(i.ResponseTopic, &b)
}
if i.CorrelationData != nil && len(i.CorrelationData) > 0 {
b.WriteByte(PropCorrelationData)
writeBinary(i.CorrelationData, &b)
}
if i.TopicAlias != nil {
b.WriteByte(PropTopicAlias)
writeUint16(*i.TopicAlias, &b)
}
}
if p == PUBLISH || p == SUBSCRIBE {
if i.SubscriptionIdentifier != nil {
b.WriteByte(PropSubscriptionIdentifier)
encodeVBIdirect(*i.SubscriptionIdentifier, &b)
}
}
if p == CONNECT || p == CONNACK {
if i.ReceiveMaximum != nil {
b.WriteByte(PropReceiveMaximum)
writeUint16(*i.ReceiveMaximum, &b)
}
if i.TopicAliasMaximum != nil {
b.WriteByte(PropTopicAliasMaximum)
writeUint16(*i.TopicAliasMaximum, &b)
}
if i.MaximumQOS != nil {
b.WriteByte(PropMaximumQOS)
b.WriteByte(*i.MaximumQOS)
}
if i.MaximumPacketSize != nil {
b.WriteByte(PropMaximumPacketSize)
writeUint32(*i.MaximumPacketSize, &b)
}
}
if p == CONNACK {
if i.AssignedClientID != "" {
b.WriteByte(PropAssignedClientID)
writeString(i.AssignedClientID, &b)
}
if i.ServerKeepAlive != nil {
b.WriteByte(PropServerKeepAlive)
writeUint16(*i.ServerKeepAlive, &b)
}
if i.WildcardSubAvailable != nil {
b.WriteByte(PropWildcardSubAvailable)
b.WriteByte(*i.WildcardSubAvailable)
}
if i.SubIDAvailable != nil {
b.WriteByte(PropSubIDAvailable)
b.WriteByte(*i.SubIDAvailable)
}
if i.SharedSubAvailable != nil {
b.WriteByte(PropSharedSubAvailable)
b.WriteByte(*i.SharedSubAvailable)
}
if i.RetainAvailable != nil {
b.WriteByte(PropRetainAvailable)
b.WriteByte(*i.RetainAvailable)
}
if i.ResponseInfo != "" {
b.WriteByte(PropResponseInfo)
writeString(i.ResponseInfo, &b)
}
}
if p == CONNECT {
if i.RequestProblemInfo != nil {
b.WriteByte(PropRequestProblemInfo)
b.WriteByte(*i.RequestProblemInfo)
}
if i.WillDelayInterval != nil {
b.WriteByte(PropWillDelayInterval)
writeUint32(*i.WillDelayInterval, &b)
}
if i.RequestResponseInfo != nil {
b.WriteByte(PropRequestResponseInfo)
b.WriteByte(*i.RequestResponseInfo)
}
}
if p == CONNECT || p == CONNACK || p == DISCONNECT {
if i.SessionExpiryInterval != nil {
b.WriteByte(PropSessionExpiryInterval)
writeUint32(*i.SessionExpiryInterval, &b)
}
}
if p == CONNECT || p == CONNACK || p == AUTH {
if i.AuthMethod != "" {
b.WriteByte(PropAuthMethod)
writeString(i.AuthMethod, &b)
}
if i.AuthData != nil && len(i.AuthData) > 0 {
b.WriteByte(PropAuthData)
writeBinary(i.AuthData, &b)
}
}
if p == CONNACK || p == DISCONNECT {
if i.ServerReference != "" {
b.WriteByte(PropServerReference)
writeString(i.ServerReference, &b)
}
}
if p != CONNECT {
if i.ReasonString != "" {
b.WriteByte(PropReasonString)
writeString(i.ReasonString, &b)
}
}
for _, v := range i.User {
b.WriteByte(PropUser)
writeString(v.Key, &b)
writeString(v.Value, &b)
}
return &b
}
// Unpack takes a buffer of bytes and reads out the defined properties
// filling in the appropriate entries in the struct, it returns the number
// of bytes used to store the Prop data and any error in decoding them
func (i *Properties) Unpack(r *bytes.Buffer, p byte) error {
vbi, err := getVBI(r)
if err != nil {
return err
}
size, err := decodeVBI(vbi)
if err != nil {
return err
}
if size == 0 {
return nil
}
buf := bytes.NewBuffer(r.Next(size))
for {
PropType, err := buf.ReadByte()
if err != nil && err != io.EOF {
return err
}
if err == io.EOF {
break
}
if !ValidateID(p, PropType) {
return fmt.Errorf("invalid Prop type %d for packet %d", PropType, p)
}
switch PropType {
case PropPayloadFormat:
pf, err := buf.ReadByte()
if err != nil {
return err
}
i.PayloadFormat = &pf
case PropMessageExpiry:
pe, err := readUint32(buf)
if err != nil {
return err
}
i.MessageExpiry = &pe
case PropContentType:
ct, err := readString(buf)
if err != nil {
return err
}
i.ContentType = ct
case PropResponseTopic:
tr, err := readString(buf)
if err != nil {
return err
}
i.ResponseTopic = tr
case PropCorrelationData:
cd, err := readBinary(buf)
if err != nil {
return err
}
i.CorrelationData = cd
case PropSubscriptionIdentifier:
si, err := decodeVBI(buf)
if err != nil {
return err
}
i.SubscriptionIdentifier = &si
case PropSessionExpiryInterval:
se, err := readUint32(buf)
if err != nil {
return err
}
i.SessionExpiryInterval = &se
case PropAssignedClientID:
ac, err := readString(buf)
if err != nil {
return err
}
i.AssignedClientID = ac
case PropServerKeepAlive:
sk, err := readUint16(buf)
if err != nil {
return err
}
i.ServerKeepAlive = &sk
case PropAuthMethod:
am, err := readString(buf)
if err != nil {
return err
}
i.AuthMethod = am
case PropAuthData:
ad, err := readBinary(buf)
if err != nil {
return err
}
i.AuthData = ad
case PropRequestProblemInfo:
rp, err := buf.ReadByte()
if err != nil {
return err
}
i.RequestProblemInfo = &rp
case PropWillDelayInterval:
wd, err := readUint32(buf)
if err != nil {
return err
}
i.WillDelayInterval = &wd
case PropRequestResponseInfo:
rp, err := buf.ReadByte()
if err != nil {
return err
}
i.RequestResponseInfo = &rp
case PropResponseInfo:
ri, err := readString(buf)
if err != nil {
return err
}
i.ResponseInfo = ri
case PropServerReference:
sr, err := readString(buf)
if err != nil {
return err
}
i.ServerReference = sr
case PropReasonString:
rs, err := readString(buf)
if err != nil {
return err
}
i.ReasonString = rs
case PropReceiveMaximum:
rm, err := readUint16(buf)
if err != nil {
return err
}
i.ReceiveMaximum = &rm
case PropTopicAliasMaximum:
ta, err := readUint16(buf)
if err != nil {
return err
}
i.TopicAliasMaximum = &ta
case PropTopicAlias:
ta, err := readUint16(buf)
if err != nil {
return err
}
i.TopicAlias = &ta
case PropMaximumQOS:
mq, err := buf.ReadByte()
if err != nil {
return err
}
i.MaximumQOS = &mq
case PropRetainAvailable:
ra, err := buf.ReadByte()
if err != nil {
return err
}
i.RetainAvailable = &ra
case PropUser:
k, err := readString(buf)
if err != nil {
return err
}
v, err := readString(buf)
if err != nil {
return err
}
i.User = append(i.User, User{k, v})
case PropMaximumPacketSize:
mp, err := readUint32(buf)
if err != nil {
return err
}
i.MaximumPacketSize = &mp
case PropWildcardSubAvailable:
ws, err := buf.ReadByte()
if err != nil {
return err
}
i.WildcardSubAvailable = &ws
case PropSubIDAvailable:
si, err := buf.ReadByte()
if err != nil {
return err
}
i.SubIDAvailable = &si
case PropSharedSubAvailable:
ss, err := buf.ReadByte()
if err != nil {
return err
}
i.SharedSubAvailable = &ss
default:
return fmt.Errorf("unknown Prop type %d", PropType)
}
}
return nil
}
const will = byte(200)
// ValidProperties is a map of the various properties and the
// PacketTypes that property is valid for.
var ValidProperties = map[byte]map[byte]struct{}{
PropPayloadFormat: {PUBLISH: {}, will: {}},
PropMessageExpiry: {PUBLISH: {}, will: {}},
PropContentType: {PUBLISH: {}, will: {}},
PropResponseTopic: {PUBLISH: {}, will: {}},
PropCorrelationData: {PUBLISH: {}, will: {}},
PropTopicAlias: {PUBLISH: {}},
PropSubscriptionIdentifier: {PUBLISH: {}, SUBSCRIBE: {}},
PropSessionExpiryInterval: {CONNECT: {}, CONNACK: {}, DISCONNECT: {}},
PropAssignedClientID: {CONNACK: {}},
PropServerKeepAlive: {CONNACK: {}},
PropWildcardSubAvailable: {CONNACK: {}},
PropSubIDAvailable: {CONNACK: {}},
PropSharedSubAvailable: {CONNACK: {}},
PropRetainAvailable: {CONNACK: {}},
PropResponseInfo: {CONNACK: {}},
PropAuthMethod: {CONNECT: {}, CONNACK: {}, AUTH: {}},
PropAuthData: {CONNECT: {}, CONNACK: {}, AUTH: {}},
PropRequestProblemInfo: {CONNECT: {}},
PropWillDelayInterval: {will: {}},
PropRequestResponseInfo: {CONNECT: {}},
PropServerReference: {CONNACK: {}, DISCONNECT: {}},
PropReasonString: {CONNACK: {}, PUBACK: {}, PUBREC: {}, PUBREL: {}, PUBCOMP: {}, SUBACK: {}, UNSUBACK: {}, DISCONNECT: {}, AUTH: {}},
PropReceiveMaximum: {CONNECT: {}, CONNACK: {}},
PropTopicAliasMaximum: {CONNECT: {}, CONNACK: {}},
PropMaximumQOS: {CONNECT: {}, CONNACK: {}},
PropMaximumPacketSize: {CONNECT: {}, CONNACK: {}},
PropUser: {CONNECT: {}, CONNACK: {}, PUBLISH: {}, PUBACK: {}, PUBREC: {}, PUBREL: {}, PUBCOMP: {}, SUBSCRIBE: {}, UNSUBSCRIBE: {}, SUBACK: {}, UNSUBACK: {}, DISCONNECT: {}, AUTH: {}, will: {}},
}
// ValidateID takes a PacketType and a property name and returns
// a boolean indicating if that property is valid for that
// PacketType
func ValidateID(p byte, i byte) bool {
_, ok := ValidProperties[i][p]
return ok
}