-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.go
446 lines (408 loc) · 8.64 KB
/
packets.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
package mqttpackets
import (
"bytes"
"fmt"
"io"
"net"
)
// PacketType is a type alias to byte representing the different
// MQTT control packet types
// type PacketType byte
// The following consts are the packet type number for each of the
// different control packets in MQTT
const (
_ byte = iota
CONNECT
CONNACK
PUBLISH
PUBACK
PUBREC
PUBREL
PUBCOMP
SUBSCRIBE
SUBACK
UNSUBSCRIBE
UNSUBACK
PINGREQ
PINGRESP
DISCONNECT
AUTH
)
type (
// Packet is the interface defining the unique parts of a controlpacket
Packet interface {
Unpack(*bytes.Buffer) error
Buffers() net.Buffers
WriteTo(io.Writer) (int64, error)
}
// FixedHeader is the definition of a control packet fixed header
FixedHeader struct {
remainingLength int
Type byte
Flags byte
}
// ControlPacket is the definition of a control packet
ControlPacket struct {
Content Packet
FixedHeader
}
Version byte
)
const (
MQTTv31 Version = 3
MQTTv311 Version = 4
MQTTv5 Version = 5
)
// WriteTo operates on a FixedHeader and takes the option values and produces
// the wire format byte that represents these.
func (f *FixedHeader) WriteTo(w io.Writer) (int64, error) {
if _, err := w.Write([]byte{byte(f.Type)<<4 | f.Flags}); err != nil {
return 0, err
}
if _, err := w.Write(encodeVBI(f.remainingLength)); err != nil {
return 0, err
}
return 0, nil
}
// PacketID is a helper function that returns the value of the PacketID
// field from any kind of mqtt packet in the Content element
func (c *ControlPacket) PacketID() uint16 {
switch r := c.Content.(type) {
case *Publish:
return r.PacketID
case *Puback:
return r.PacketID
case *Pubrec:
return r.PacketID
case *Pubrel:
return r.PacketID
case *Pubcomp:
return r.PacketID
case *Subscribe:
return r.PacketID
case *Suback:
return r.PacketID
case *Unsubscribe:
return r.PacketID
case *Unsuback:
return r.PacketID
default:
return 0
}
}
func (c *ControlPacket) PacketType() string {
return [...]string{
"",
"CONNECT",
"CONNACK",
"PUBLISH",
"PUBACK",
"PUBREC",
"PUBREL",
"PUBCOMP",
"SUBSCRIBE",
"SUBACK",
"UNSUBSCRIBE",
"UNSUBACK",
"PINGREQ",
"PINGRESP",
"DISCONNECT",
"AUTH",
}[c.FixedHeader.Type]
}
// NewControlPacket takes a packetType and returns a pointer to a
// ControlPacket where the VariableHeader field is a pointer to an
// instance of a VariableHeader definition for that packetType
// Packet will be created as v3 if Version is not set correctly.
func NewControlPacket(t byte, v Version) *ControlPacket {
cp := &ControlPacket{FixedHeader: FixedHeader{Type: t}}
switch t {
case CONNECT:
content := &Connect{
ProtocolName: "MQTT",
ProtocolVersion: v,
}
if v == MQTTv31 {
content.ProtocolName = "MQIsdp"
}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case CONNACK:
content := &Connack{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PUBLISH:
content := &Publish{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PUBACK:
content := &Puback{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PUBREC:
content := &Pubrec{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PUBREL:
cp.Flags = 2
content := &Pubrel{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PUBCOMP:
content := &Pubcomp{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case SUBSCRIBE:
cp.Flags = 2
content := &Subscribe{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case SUBACK:
content := &Suback{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case UNSUBSCRIBE:
cp.Flags = 2
content := &Unsubscribe{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case UNSUBACK:
content := &Unsuback{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case PINGREQ:
cp.Content = &Pingreq{}
case PINGRESP:
cp.Content = &Pingresp{}
case DISCONNECT:
content := &Disconnect{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
case AUTH:
cp.Flags = 1
content := &Auth{}
if v == MQTTv5 {
content.Properties = &Properties{}
}
cp.Content = content
default:
return nil
}
return cp
}
// ReadPacket reads a control packet from a io.Reader and returns a completed
// struct with the appropriate data.
// Version can be set to 0 when reading a Connect packet.
// Packet will be parsed as v3 if Version is not set correctly when reading other types of packets.
func ReadPacket(r io.Reader, v Version) (*ControlPacket, error) {
t := [1]byte{}
_, err := io.ReadFull(r, t[:])
if err != nil {
return nil, err
}
pt := t[0] >> 4
cp := NewControlPacket(pt, v)
if cp == nil {
return nil, fmt.Errorf("invalid packet type requested, %d", pt)
}
cp.Flags = t[0] & 0xF
if cp.Type == PUBLISH {
cp.Content.(*Publish).QoS = (cp.Flags & 0x6) >> 1
}
vbi, err := getVBI(r)
if err != nil {
return nil, err
}
cp.remainingLength, err = decodeVBI(vbi)
if err != nil {
return nil, err
}
var content bytes.Buffer
content.Grow(cp.remainingLength)
n, err := io.CopyN(&content, r, int64(cp.remainingLength))
if err != nil {
return nil, err
}
if n != int64(cp.remainingLength) {
return nil, fmt.Errorf("failed to read packet, expected %d bytes, read %d", cp.remainingLength, n)
}
err = cp.Content.Unpack(&content)
if err != nil {
return nil, err
}
return cp, nil
}
// WriteTo writes a packet to an io.Writer, handling packing all the parts of
// a control packet.
func (c *ControlPacket) WriteTo(w io.Writer) (int64, error) {
c.remainingLength = 0
buffers := c.Content.Buffers()
for _, b := range buffers {
c.remainingLength += len(b)
}
var header bytes.Buffer
if _, err := c.FixedHeader.WriteTo(&header); err != nil {
return 0, err
}
buffers = append(net.Buffers{header.Bytes()}, buffers...)
return buffers.WriteTo(w)
}
func encodeVBI(length int) []byte {
var x int
b := [4]byte{}
for {
digit := byte(length % 128)
length /= 128
if length > 0 {
digit |= 0x80
}
b[x] = digit
x++
if length == 0 {
return b[:x]
}
}
}
func encodeVBIdirect(length int, buf *bytes.Buffer) {
var x int
b := [4]byte{}
for {
digit := byte(length % 128)
length /= 128
if length > 0 {
digit |= 0x80
}
b[x] = digit
x++
if length == 0 {
buf.Write(b[:x])
return
}
}
}
func getVBI(r io.Reader) (*bytes.Buffer, error) {
var ret bytes.Buffer
digit := [1]byte{}
for {
_, err := io.ReadFull(r, digit[:])
if err != nil {
return nil, err
}
ret.WriteByte(digit[0])
if digit[0] <= 0x7f {
return &ret, nil
}
}
}
func decodeVBI(r *bytes.Buffer) (int, error) {
var vbi uint32
var multiplier uint32
for {
digit, err := r.ReadByte()
if err != nil && err != io.EOF {
return 0, err
}
vbi |= uint32(digit&127) << multiplier
if (digit & 128) == 0 {
break
}
multiplier += 7
}
return int(vbi), nil
}
func writeUint16(u uint16, b *bytes.Buffer) error {
if err := b.WriteByte(byte(u >> 8)); err != nil {
return err
}
return b.WriteByte(byte(u))
}
func writeUint32(u uint32, b *bytes.Buffer) error {
if err := b.WriteByte(byte(u >> 24)); err != nil {
return err
}
if err := b.WriteByte(byte(u >> 16)); err != nil {
return err
}
if err := b.WriteByte(byte(u >> 8)); err != nil {
return err
}
return b.WriteByte(byte(u))
}
func writeString(s string, b *bytes.Buffer) {
writeUint16(uint16(len(s)), b)
b.WriteString(s)
}
func writeBinary(d []byte, b *bytes.Buffer) {
writeUint16(uint16(len(d)), b)
b.Write(d)
}
func readUint16(b *bytes.Buffer) (uint16, error) {
b1, err := b.ReadByte()
if err != nil {
return 0, err
}
b2, err := b.ReadByte()
if err != nil {
return 0, err
}
return (uint16(b1) << 8) | uint16(b2), nil
}
func readUint32(b *bytes.Buffer) (uint32, error) {
b1, err := b.ReadByte()
if err != nil {
return 0, err
}
b2, err := b.ReadByte()
if err != nil {
return 0, err
}
b3, err := b.ReadByte()
if err != nil {
return 0, err
}
b4, err := b.ReadByte()
if err != nil {
return 0, err
}
return (uint32(b1) << 24) | (uint32(b2) << 16) | (uint32(b3) << 8) | uint32(b4), nil
}
func readBinary(b *bytes.Buffer) ([]byte, error) {
size, err := readUint16(b)
if err != nil {
return nil, err
}
var s bytes.Buffer
s.Grow(int(size))
if _, err := io.CopyN(&s, b, int64(size)); err != nil {
return nil, err
}
return s.Bytes(), nil
}
func readString(b *bytes.Buffer) (string, error) {
s, err := readBinary(b)
return string(s), err
}