From c72dd39f92cf1d1fc63368916b33e5ccfd2e868e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 2 Apr 2020 10:26:36 +0100 Subject: [PATCH] Allow MQTT v5.0 outgoing bridges to fall back to MQTT v3.1.1 This applies if connecting to a v3.x only broker. --- ChangeLog.txt | 2 ++ src/handle_connack.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index 103eb35f..bdc53bfc 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -8,6 +8,8 @@ Broker: bridging to e.g. Amazon or Google. - Add support for MQTT v5 bridges to handle the "retain-available" property being false. +- Allow MQTT v5.0 outgoing bridges to fall back to MQTT v3.1.1 if connecting + to a v3.x only broker. Client library: - Client no longer generates random client ids for v3.1.1 clients, these are diff --git a/src/handle_connack.c b/src/handle_connack.c index ef59b13c..8b6dc83c 100644 --- a/src/handle_connack.c +++ b/src/handle_connack.c @@ -41,6 +41,18 @@ int handle__connack(struct mosquitto_db *db, struct mosquitto *context) if(packet__read_byte(&context->in_packet, &reason_code)) return 1; if(context->protocol == mosq_p_mqtt5){ + if(context->in_packet.remaining_length == 2 && reason_code == CONNACK_REFUSED_PROTOCOL_VERSION){ + /* We have connected to a MQTT v3.x broker that doesn't support MQTT v5.0 + * It has correctly replied with a CONNACK code of a bad protocol version. + */ + log__printf(NULL, MOSQ_LOG_NOTICE, + "Warning: Remote bridge %s does not support MQTT v5.0, reconnecting using MQTT v3.1.1.", + context->bridge->name); + + context->protocol = mosq_p_mqtt311; + context->bridge->protocol_version = mosq_p_mqtt311; + return MOSQ_ERR_PROTOCOL; + } rc = property__read_all(CMD_CONNACK, &context->in_packet, &properties); if(rc) return rc; mosquitto_property_free_all(&properties);