標籤:mqtt 訊息格式 mosquitto wireshark
我們知道MQTT是一個物聯網協議的一個規範,MQTT的協議最新的兩個版本是:3.1.1和3.1.0
(1) 3.1.0的規範如下
http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html
(2) 3.1.1的規範如下
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718029
規範3.1.0和3.1.1還是有不少的區別。比如可變頭部中,在3.1.0的規範中,其關鍵字是,“MQISdP”
表1. MQTT V3.1規範可變訊息頭的前8個位元組
|
Description |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Protocol Name |
byte 1 |
Length MSB (0) |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
byte 2 |
Length LSB (6) |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
byte 3 |
‘M‘ |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
byte 4 |
‘Q‘ |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
1 |
byte 5 |
‘I‘ |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
1 |
byte 6 |
‘s‘ |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
byte 7 |
‘d‘ |
0 |
1 |
1 |
0 |
0 |
1 |
0 |
0 |
byte 8 |
‘p‘ |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
表2. MQTT V3.1.1可變訊息頭的前6個位元組
|
描述 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
協議名稱 |
byte 1 |
Length MSB (0) |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
byte 2 |
Length LSB (4) |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
byte 3 |
‘M’ |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
byte 4 |
‘Q’ |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
1 |
byte 5 |
‘T’ |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
0 |
byte 6 |
‘T’ |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
0 |
Protocol Level |
|
描述 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
byte 7 |
Level (4) |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
規範主要描述了訊息的通用格式,訊息的命令,訊息流程動等等的規範和規則。在前面的四個章節,我們對MQTT有了一個感性的大概認識,在接下來的系列文章,我將帶大家把MQTT的下面14種命令訊息通過抓包軟體(WireShark)並結合具體的規範和大家全部過一遍。 注意,因為我用的Eclipse Paho API的java庫,支援MQTT 3.1.1 的版本,且mosquitto也支援3.1.1的版本,所以本系列所有的抓包都會是基於MQTT 3.1.1的規範,而不是MQTT 3.1的規範,請大家注意。下面的表格描述了MQTT的14中不同的訊息類型。
表3. MQTT的14種訊息類型(Command message)
名字 |
值 |
資訊流方向 |
描述 |
Reserved |
0 |
Forbidden |
Reserved |
CONNECT |
1 |
Client to Server |
Client request to connect to Server |
CONNACK |
2 |
Server to Client |
Connect acknowledgment |
PUBLISH |
3 |
Client to Server or Server to Client |
Publish message |
PUBACK |
4 |
Client to Server or Server to Client |
Publish acknowledgment |
PUBREC |
5 |
Client to Server or Server to Client |
Publish received (assured delivery part 1) |
PUBREL |
6 |
Client to Server or Server to Client |
Publish release (assured delivery part 2) |
PUBCOMP |
7 |
Client to Server or Server to Client |
Publish complete (assured delivery part 3) |
SUBSCRIBE |
8 |
Client to Server |
Client subscribe request |
SUBACK |
9 |
Server to Client |
Subscribe acknowledgment |
UNSUBSCRIBE |
10 |
Client to Server |
Unsubscribe request |
UNSUBACK |
11 |
Server to Client |
Unsubscribe acknowledgment |
PINGREQ |
12 |
Client to Server |
PING request |
PINGRESP |
13 |
Server to Client |
PING response |
DISCONNECT |
14 |
Client to Server |
Client is disconnecting |
Reserved |
15 |
Forbidden |
Reserved |
在接下來的章節我們將會逐一個給大家用WireShark抓包,並給大家一個一個分析其協議,敬請期待和關注,謝謝。
[5] MQTT,mosquitto,Eclipse Paho---MQTT訊息格式分析概述