[5] MQTT,mosquitto,Eclipse Paho---MQTT消息格式分析概述
我们知道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抓包,并给大家一个一个分析其协议,敬请期待和关注,谢谢。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。