Monday, June 22, 2015

MQTT dissecting CONNECT message

Below is the trace from wireshark for an MQTT connect message.


The main components of an MQTT message are

1. Fixed Length header
2. Variable Length header
3. Payload

In the figure above, highlighted the fixed length header and updated image is given below.

The first two bytes constitutes the fixed header. This is highlighted in red in the above image.

Byte 1 => has value 10 which has the bit format of 0001 0000

Bit 0 (right most bit) indicates whether the message should be retained
Bit 1 & 2 indicate the QoS level, values being 0, 1, 2
Bit 3 indicates the DUP flag, server sets this if the message is a DUPlicate
Bits 4, 5, 6, 7 altogether mentions the message type. In this case, it is 1, indicating the message as CONNECT

byte 2 of the Fixed length header denotes the length of the remaining data. In this case, it is Hex 49.

Next is variable length header, this is highlighted in green in the image above.
in the variable length header area, first comes the protocol name the first and second byte are respectively 0, 6 and then the word MQIsdp. The the protocol version number, which is 3 in the image above. Byte 10 in this section represents the Connect flags, each bit in this Byte has meaning below.

bit 7 => user name flag (if present or not)
bit 6 => password flag (if present or not)
bit 5, Will RETAIN => 0 for CONNECT message
bit 4, 3 Will QoS => 01 for will QOS in connect message
bit 2 will flag
bit 1 clean session => 1 indicates that it is a clean session
bit 0 is not used as of now

byte 11 and 12 denotes the keep alive timer value in this case 00 C2 value in decimal is 60 seconds.

Next is Payload of the CONNECT message
Below are the main components in Payload for Connect

For variable length header, each of the filed is having a prefix that represents the length of that field.

The first item in the variable length area is client Id, below image in green under line shows the variable header total content, and the red highlight box shows the client Id, first two digits being the length, 13 Hex, which is 19 chars.





1. Client Identifier : This is a UTF encoded string, the length can be from 1 to 23, This uniquely identifies device to the server. If the length exceeds, the CONNACK response with 2 indicating the identifier rejected.

Other items in the variable header are 2. Will topic, 3. Will message, 4. User name , 5. Password

references:


No comments:

Post a Comment