-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.go
494 lines (397 loc) · 10.1 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
package mqtt
import (
"errors"
"io"
"log"
)
// errors
var (
InclompleteHeader = errors.New("incomplete header")
MaxMessageLength = errors.New("message length exceeds server maximum")
MessageLengthInvalid = errors.New("message length exceeds maximum")
IncompleteMessage = errors.New("incomplete message")
UnknownMessageType = errors.New("unknown mqtt message type")
ReservedMessageType = errors.New("reserved message type")
ConnectMsgLacksProtocol = errors.New("connect message has no protocol field")
ConnectProtocolUnexp = errors.New("connect message protocol is not 'MQIsdp'")
TooLongClientID = errors.New("connect client id is too long")
UnknownMessageID = errors.New("unknown message id")
)
const maxMessageLength = 1024 * 1024 * 6
// CONNACK return codes
const (
ACCEPTED = 0
UNACCEPTABLE_PROTOV = 1
IDENTIFIER_REJ = 2
SERVER_UNAVAIL = 3
BAD_USER_OR_PASS = 4
NOT_AUTHORIZED = 5
)
// message types
const (
CONNECT = 1
CONNACK = 2
PUBLISH = 3
PUBACK = 4
PUBREC = 5
PUBREL = 6
PUBCOMP = 7
SUBSCRIBE = 8
SUBACK = 9
UNSUBSCRIBE = 10
UNSUBACK = 11
PINGREQ = 12
PINGRESP = 13
DISCONNECT = 14
)
// string representation of message types
var messageType = [...]string{"reserved", "CONNECT", "CONNACK", "PUBLISH",
"PUBACK", "PUBREC", "PUBREL", "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE",
"UNSUBACK", "PINGREQ", "PINGRESP", "DISCONNECT"}
///////////////////////////////////////////////////////////////////////////////
type Message struct {
Topic string
Buf []byte
QoS byte
retain bool
}
///////////////////////////////////////////////////////////////////////////////
func readString(buf []byte) (int, string) {
length, b := readBytes(buf)
return length, string(b)
}
func readBytes(buf []byte) (int, []byte) {
if len(buf) < 2 {
return 0, nil
}
length := (int(buf[0])<<8 + int(buf[1])) + 2
if len(buf) < length {
return 0, nil
}
return length, buf[2:length]
}
///////////////////////////////////////////////////////////////////////////////
type FixedHeader struct {
mtype byte
dup bool
qos byte
retain bool
length int
}
func (fh *FixedHeader) Read(reader io.Reader) error {
var headBuf [1]byte
n, err := reader.Read(headBuf[:])
if err != nil {
return err // read error
}
if n == 0 {
return io.EOF // connection closed
}
fh.mtype = byte(headBuf[0] >> 4)
fh.dup = bool(headBuf[0]&0x8 != 0)
fh.qos = byte((headBuf[0] & 0x6) >> 1)
fh.retain = bool(headBuf[0]&0x1 != 0)
if fh.mtype == 0 || fh.mtype == 15 {
return ReservedMessageType // reserved type
}
var multiplier int = 1
var length int
for {
n, err = reader.Read(headBuf[:])
if err != nil {
return err // read error
}
if n == 0 {
return InclompleteHeader // connection closed in header
}
length += int(headBuf[0]&127) * multiplier
if length > maxMessageLength {
return MaxMessageLength // server maximum message size exceeded
}
if headBuf[0]&128 == 0 {
break
}
if multiplier > 0x4000 {
return MessageLengthInvalid // mqtt maximum message size exceeded
}
multiplier *= 128
}
fh.length = length
return nil
}
///////////////////////////////////////////////////////////////////////////////
// read from a reader (input stream) a new mqtt message
func (ctx *Context) Read(reader io.Reader) {
var fh FixedHeader
if err := fh.Read(reader); err != nil {
ctx.Fail(err)
return
}
buf := make([]byte, fh.length)
_, err := io.ReadFull(reader, buf)
if err != nil {
ctx.Fail(IncompleteMessage)
return
}
// log.Printf("Message: %s (length:%d qos:%d dup:%t retain:%t)",
// messageType[fh.mtype],
// fh.length,
// fh.qos,
// fh.dup,
// fh.retain)
switch fh.mtype {
case CONNECT:
ctx.ReadConnectMessage(reader, &fh, buf)
case SUBSCRIBE:
ctx.ReadSubscribeMessage(reader, &fh, buf)
case PUBLISH:
ctx.ReadPublishMessage(reader, &fh, buf)
case PUBREL:
ctx.ReadPubrelMessage(reader, &fh, buf)
case PUBREC:
ctx.ReadPubrecMessage(reader, &fh, buf)
case PUBCOMP:
ctx.ReadPubcompMessage(reader, &fh, buf)
case PINGREQ:
ctx.PingResp()
case DISCONNECT:
ctx.Close()
}
}
///////////////////////////////////////////////////////////////////////////////
// parse CONNECT messages
func (ctx *Context) ReadConnectMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
l, protocol := readString(buf)
if l == 0 {
ctx.Fail(ConnectMsgLacksProtocol)
return
}
if protocol != "MQIsdp" {
ctx.Failf("unsupported protocol '%.12s'", protocol)
return
}
buf = buf[l:]
//
if len(buf) < 1 {
ctx.Fail(IncompleteMessage)
return
}
version := buf[0]
if version != 0x03 {
ctx.ConnAck(UNACCEPTABLE_PROTOV)
return
}
buf = buf[1:]
//
if len(buf) < 1 {
ctx.Fail(IncompleteMessage)
return
}
connFlags := buf[0]
// log.Printf("Connection Flags: %d", connFlags)
// cleanSession := connFlags&0x02 != 0
// if cleanSession {
// log.Println("Clean Session: true")
// }
willFlag := connFlags&0x04 != 0
willQoS := connFlags & 0x18 >> 3
willRetain := connFlags&0x20 != 0
passwordFlag := connFlags&0x40 != 0
usernameFlag := connFlags&0x80 != 0
buf = buf[1:]
//
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
// keepAliveTimer := int(buf[0])<<8 + int(buf[1])
// log.Printf("KeepAlive Timer: %d", keepAliveTimer)
// TODO set SetDeadline() to conn
buf = buf[2:]
//
l, ctx.ClientID = readString(buf)
if l == 0 {
ctx.Fail(IncompleteMessage)
return
}
if l > 128 {
// should be max 23, but some client implementations ignore this
// so we increase the size to 128
ctx.ConnAck(IDENTIFIER_REJ)
return
}
buf = buf[l:]
//
if willFlag {
var will Message
will.retain = willRetain
will.QoS = willQoS
l, will.Topic = readString(buf)
if l == 0 {
ctx.Fail(IncompleteMessage)
return
}
buf = buf[l:]
l, will.Buf = readBytes(buf)
if l == 0 {
ctx.Fail(IncompleteMessage)
return
}
log.Printf("Will: topic:%q qos:%d %q\n", will.Topic, will.QoS, will.Buf)
ctx.Will = &will
buf = buf[l:]
}
//
var username, password string
if usernameFlag {
l, username = readString(buf)
if l == 0 {
ctx.Fail(IncompleteMessage)
return
}
buf = buf[l:]
if passwordFlag {
l, password = readString(buf)
if l != 0 {
buf = buf[l:]
}
}
}
if ctx.server.handler != nil && ctx.server.handler.Connect(ctx, username, password) == nil {
ctx.ConnAck(ACCEPTED)
} else {
if !usernameFlag {
ctx.ConnAck(NOT_AUTHORIZED)
} else {
ctx.ConnAck(BAD_USER_OR_PASS)
}
}
}
///////////////////////////////////////////////////////////////////////////////
// parse a SUBSCRIBE message and send SUBACK
func (ctx *Context) ReadSubscribeMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
mid := int(buf[0])<<8 + int(buf[1])
buf = buf[2:]
var s int
for i, l := 0, len(buf); i != l; s++ {
i += (int(buf[i]) << 8) + int(buf[i+1]) + 2 + 1
if i > l {
ctx.Fail(IncompleteMessage)
return
}
}
l := 2 + s
head, body := Head(0x90, l, l) // SUBACK
body[0] = byte(mid >> 8) // mid MSB
body[1] = byte(mid & 0xff) // mid LSB
s = 2
for len(buf) != 0 {
l, topic := readString(buf)
qos := buf[l] & 0x03
buf = buf[l+1:]
// grantedQos
body[s] = ctx.Subscribe(topic, qos)
s++
}
ctx.Write(head)
}
///////////////////////////////////////////////////////////////////////////////
// parse a PUBLISH message and tell the server about it
func (ctx *Context) ReadPublishMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
l, topic := readString(buf)
if l == 0 {
ctx.Fail(IncompleteMessage)
return
}
buf = buf[l:]
if fh.qos == 0 { // QoS 0
ctx.server.Publish(ctx, &Message{topic, buf, 0, fh.retain})
} else { // QoS 1 or 2
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
mid := int(buf[0])<<8 + int(buf[1])
buf = buf[2:]
msg := &Message{topic, buf, fh.qos, fh.retain}
if fh.qos == 1 {
ctx.server.Publish(ctx, msg)
// send PUBACK message
buf := make([]byte, 4)
buf[0] = 0x40 // PUBACK
buf[1] = 0x02 // remaining length: 2
buf[2] = byte(mid >> 8)
buf[3] = byte(mid & 0xff)
ctx.Write(buf)
} else {
ctx.messages[mid] = msg // store
// send PUBREC message
buf := make([]byte, 4)
buf[0] = 0x50 // PUBREC
buf[1] = 0x02 // remaining length: 2
buf[2] = byte(mid >> 8)
buf[3] = byte(mid & 0xff)
ctx.Write(buf)
}
}
}
///////////////////////////////////////////////////////////////////////////////
// parse a PUBREL message (a response to a PUBREC at QoS 2)
// the message has alredy been stored at the previous PUBREC message
func (ctx *Context) ReadPubrelMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
mid := int(buf[0])<<8 + int(buf[1])
msg, ok := ctx.messages[mid]
if !ok {
ctx.Fail(UnknownMessageID)
return
}
ctx.server.Publish(ctx, msg)
delete(ctx.messages, mid)
// send PUBREC message
buf = make([]byte, 4)
buf[0] = 0x70 // PUBCOMP
buf[1] = 0x02 // remaining length: 2
buf[2] = byte(mid >> 8)
buf[3] = byte(mid & 0xff)
ctx.Write(buf)
}
///////////////////////////////////////////////////////////////////////////////
// parse a PUBREC message
// (a response to a publish from this server to a client on qos 2)
func (ctx *Context) ReadPubrecMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
mid := int(buf[0])<<8 + int(buf[1])
// send PUBREL message
buf = make([]byte, 4)
buf[0] = 0x62 // PUBREL at qos 1
buf[1] = 0x02 // remaining length: 2
buf[2] = byte(mid >> 8)
buf[3] = byte(mid & 0xff)
ctx.Write(buf)
}
///////////////////////////////////////////////////////////////////////////////
// parse a PUBCOMP message
// (a response to a PUBREL from a client to this server)
func (ctx *Context) ReadPubcompMessage(reader io.Reader, fh *FixedHeader, buf []byte) {
if len(buf) < 2 {
ctx.Fail(IncompleteMessage)
return
}
// mid := int(buf[0])<<8 + int(buf[1])
}
//////////////////////////////////////////////////////////////////////////////