/
connect.go
404 lines (334 loc) · 8.52 KB
/
connect.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
package packet
import (
"bytes"
"fmt"
)
// The supported MQTT versions.
const (
Version31 byte = 3
Version311 byte = 4
)
var versionNames = map[byte][]byte{
Version31: []byte("MQIsdp"),
Version311: []byte("MQTT"),
}
// A Connect packet is sent by a client to the server after a network
// connection has been established.
type Connect struct {
// The client's client id.
ClientID string
// The keep alive value.
KeepAlive uint16
// The authentication username.
Username string
// The authentication password.
Password string
// The clean session flag.
CleanSession bool
// The will message.
Will *Message
// The MQTT version 3 or 4 (defaults to 4 when 0).
Version byte
}
// NewConnect creates a new Connect packet.
func NewConnect() *Connect {
return &Connect{
CleanSession: true,
Version: 4,
}
}
// Type returns the packets type.
func (c *Connect) Type() Type {
return CONNECT
}
// String returns a string representation of the packet.
func (c *Connect) String() string {
will := "nil"
if c.Will != nil {
will = c.Will.String()
}
return fmt.Sprintf("<Connect ClientID=%q KeepAlive=%d Username=%q "+
"Password=%q CleanSession=%t Will=%s Version=%d>",
c.ClientID,
c.KeepAlive,
c.Username,
c.Password,
c.CleanSession,
will,
c.Version,
)
}
// Len returns the byte length of the encoded packet.
func (c *Connect) Len() int {
ml := c.len()
return headerLen(ml) + ml
}
// Decode reads from the byte slice argument. It returns the total number of
// bytes decoded, and whether there have been any errors during the process.
func (c *Connect) Decode(src []byte) (int, error) {
// decode header
total, _, _, err := decodeHeader(src, CONNECT)
if err != nil {
return total, err
}
// read protocol string
protoName, n, err := readLPBytes(src[total:], false, CONNECT)
total += n
if err != nil {
return total, err
}
// read version
versionByte, n, err := readUint8(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
// check protocol version
if versionByte != Version311 && versionByte != Version31 {
return total, makeError(CONNECT, "invalid protocol version (%d)", versionByte)
}
// set version
c.Version = versionByte
// check protocol version string
if !bytes.Equal(protoName, versionNames[c.Version]) {
return total, makeError(CONNECT, "invalid protocol version string (%s)", protoName)
}
// get connect flags
connectFlags, n, err := readUint8(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
// read flags
usernameFlag := ((connectFlags >> 7) & 0x1) == 1
passwordFlag := ((connectFlags >> 6) & 0x1) == 1
willFlag := ((connectFlags >> 2) & 0x1) == 1
willRetain := ((connectFlags >> 5) & 0x1) == 1
willQOS := QOS((connectFlags >> 3) & 0x3)
c.CleanSession = ((connectFlags >> 1) & 0x1) == 1
// check reserved bit
if connectFlags&0x1 != 0 {
return total, makeError(CONNECT, "reserved bit 0 is not 0")
}
// check will qos
if !willQOS.Successful() {
return total, makeError(CONNECT, "invalid QOS level (%d) for will message", willQOS)
}
// check will flags
if !willFlag && (willRetain || willQOS != 0) {
return total, makeError(CONNECT, "if the will flag is set to 0 the will qos and will retain fields must be set to zero")
}
// create will if present
if willFlag {
c.Will = &Message{QOS: willQOS, Retain: willRetain}
}
// check auth flags
if !usernameFlag && passwordFlag {
return total, makeError(CONNECT, "password flag is set but username flag is not set")
}
// read keep alive
ka, n, err := readUint(src[total:], 2, CONNECT)
total += n
if err != nil {
return total, err
}
// set keep alive
c.KeepAlive = uint16(ka)
// read client id
c.ClientID, n, err = readLPString(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
// if the client supplies a zero-byte clientID, the client must also set CleanSession to 1
if len(c.ClientID) == 0 && !c.CleanSession {
return total, makeError(CONNECT, "clean session must be 1 if client id is zero length")
}
// check will
if c.Will != nil {
// read will topic
c.Will.Topic, n, err = readLPString(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
// read will payload
c.Will.Payload, n, err = readLPBytes(src[total:], true, CONNECT)
total += n
if err != nil {
return total, err
}
}
// read username
if usernameFlag {
c.Username, n, err = readLPString(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
}
// read password
if passwordFlag {
c.Password, n, err = readLPString(src[total:], CONNECT)
total += n
if err != nil {
return total, err
}
}
return total, nil
}
// Encode writes the packet bytes into the byte slice from the argument. It
// returns the number of bytes encoded and whether there's any errors along
// the way. If there is an error, the byte slice should be considered invalid.
func (c *Connect) Encode(dst []byte) (int, error) {
// encode header
total, err := encodeHeader(dst, 0, c.len(), c.Len(), CONNECT)
if err != nil {
return total, err
}
// set default version byte
if c.Version == 0 {
c.Version = Version311
}
// check version byte
if c.Version != Version311 && c.Version != Version31 {
return total, makeError(CONNECT, "unsupported protocol version %d", c.Version)
}
// write version string
n, err := writeLPBytes(dst[total:], versionNames[c.Version], CONNECT)
total += n
if err != nil {
return total, err
}
// write version value
n, err = writeUint8(dst[total:], c.Version, CONNECT)
total += n
if err != nil {
return total, err
}
// prepare connect flags
var connectFlags byte
// set username flag
if len(c.Username) > 0 {
connectFlags |= 128 // 10000000
}
// set password flag
if len(c.Password) > 0 {
connectFlags |= 64 // 01000000
}
// check will
if c.Will != nil {
// set will flag
connectFlags |= 0x4 // 00000100
// check will topic length
if len(c.Will.Topic) == 0 {
return total, makeError(CONNECT, "will topic is empty")
}
// check will qos
if !c.Will.QOS.Successful() {
return total, makeError(CONNECT, "invalid will qos level %d", c.Will.QOS)
}
// set will qos flag
connectFlags = (connectFlags & 231) | (byte(c.Will.QOS) << 3) // 231 = 11100111
// set will retain flag
if c.Will.Retain {
connectFlags |= 32 // 00100000
}
}
// check client id and clean session
if len(c.ClientID) == 0 && !c.CleanSession {
return total, makeError(CONNECT, "clean session must be 1 if client id is zero length")
}
// set clean session flag
if c.CleanSession {
connectFlags |= 0x2 // 00000010
}
// write connect flags
n, err = writeUint8(dst[total:], connectFlags, CONNECT)
total += n
if err != nil {
return total, err
}
// write keep alive
n, err = writeUint(dst[total:], uint64(c.KeepAlive), 2, CONNECT)
total += n
if err != nil {
return total, err
}
// write client id
n, err = writeLPString(dst[total:], c.ClientID, CONNECT)
total += n
if err != nil {
return total, err
}
// check will
if c.Will != nil {
// write will topic
n, err = writeLPString(dst[total:], c.Will.Topic, CONNECT)
total += n
if err != nil {
return total, err
}
// write will payload
n, err = writeLPBytes(dst[total:], c.Will.Payload, CONNECT)
total += n
if err != nil {
return total, err
}
}
// check username and password
if len(c.Username) == 0 && len(c.Password) > 0 {
return total, makeError(CONNECT, "password set without username")
}
// write username
if len(c.Username) > 0 {
n, err = writeLPString(dst[total:], c.Username, CONNECT)
total += n
if err != nil {
return total, err
}
}
// write password
if len(c.Password) > 0 {
n, err = writeLPString(dst[total:], c.Password, CONNECT)
total += n
if err != nil {
return total, err
}
}
return total, nil
}
func (c *Connect) len() int {
// prepare total
total := 0
// add version
if c.Version == Version31 {
// 2 bytes protocol name length
// 6 bytes protocol name
// 1 byte protocol version
total += 2 + 6 + 1
} else {
// 2 bytes protocol name length
// 4 bytes protocol name
// 1 byte protocol version
total += 2 + 4 + 1
}
// add 1 byte connect flags
// add 2 bytes keep alive timer
total += 1 + 2
// add the clientID length
total += 2 + len(c.ClientID)
// add the will topic and will message length
if c.Will != nil {
total += 2 + len(c.Will.Topic) + 2 + len(c.Will.Payload)
}
// add the username length
if len(c.Username) > 0 {
total += 2 + len(c.Username)
}
// add the password length
if len(c.Password) > 0 {
total += 2 + len(c.Password)
}
return total
}