eclipse-mosquitto / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
9.1k stars 2.4k forks source link

on TLS Error Disconnect Callback sets mqtt_reason Code to 7 #1984

Open tiolan opened 3 years ago

tiolan commented 3 years ago

I have the following setup:

I set the disconnect callback (it is in C++)

    mosquitto_disconnect_v5_callback_set(
        pClient, [](struct mosquitto* pClient, void* pThis, int mqttRc, const mosquitto_property* pProps) {
            static_cast<MosquittoClient*>(pThis)->onDisconnectCb(pClient, mqttRc, pProps);
        });

When trying to connect to the broker mosquitto_connect_async returns ERROR_TLS (expected) and onDisconnectCb is invoked (expected, wrong certificate).

void
MosquittoClient::onDisconnectCb(struct mosquitto* pClient, int mqttRc, const mosquitto_property* pProps)
{
   ...
}

the field mqttRc is in this case set to 7.

I do not find any definition for an mqtt5_reason code 7. https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901031 https://github.com/eclipse/mosquitto/blob/a33c28cfe19261b8c3856df88b1d4827055abc71/include/mqtt_protocol.h#L127

Did I misunderstand the API and this field is not always the mqtt5_reason code?

ralight commented 3 years ago

The documentation says this about the on_disconnect rc value:

 *  rc -   integer value indicating the reason for the disconnect. A value of 0
 *         means the client has called <mosquitto_disconnect>. Any other value
 *         indicates that the disconnect is unexpected.

In other words, you shouldn't rely on taking any meaning from the error condition, other than that it is an error. Now in practice there is meaning to the value. Prior to MQTT v5, it was a mosquitto error code. Since 1.6, MQTT v5 connections also have it set to an MQTT v5 reason code in some situations.

I think for 2.1 it can change so that the values are specified, and it is possible to distinguish between a Mosquitto error code and an MQTT reason code.

Mosquitto error code 7 is MOSQ_ERR_CONN_LOST, connection lost.