From d8f5aacd7f8ba1de2d8c2a4937b0d48650710a06 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 27 Oct 2020 11:35:06 +0000 Subject: [PATCH] Add the `bridge_max_packet_size` option. Closes #265. --- ChangeLog.txt | 1 + man/mosquitto.conf.5.xml | 14 ++++++++++++++ mosquitto.conf | 8 ++++++++ src/bridge.c | 2 ++ src/conf.c | 13 +++++++++++++ src/handle_connack.c | 10 ++++++++++ src/mosquitto_broker_internal.h | 1 + 7 files changed, 49 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index 9d57863c..0f6ba818 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -58,6 +58,7 @@ Broker: - The broker now sends the receive-maximum property for MQTT v5 CONNACKs. - mosquitto_password now forbids the : character. Closes #1833. - Fix `log_timestamp_format` not applying to `log_dest topic`. Closes #1862. +- Add the `bridge_max_packet_size` option. Closes #265. Client library: - Client no longer generates random client ids for v3.1.1 clients, these are diff --git a/man/mosquitto.conf.5.xml b/man/mosquitto.conf.5.xml index db934e56..9367bb7a 100644 --- a/man/mosquitto.conf.5.xml +++ b/man/mosquitto.conf.5.xml @@ -1521,6 +1521,20 @@ openssl dhparam -out dhparam.pem 2048 true. + + value + + + If you wish to restrict the size of messages sent to a + remote bridge, use this option. This sets the maximum + number of bytes for the total message, including headers + and payload. Note that MQTT v5 brokers may provide their + own maximum-packet-size property. In this case, the + smaller of the two limits will be used. Set to 0 for + "unlimited". + + + [ true | false ] diff --git a/mosquitto.conf b/mosquitto.conf index b5ab55d0..8979bcba 100644 --- a/mosquitto.conf +++ b/mosquitto.conf @@ -786,6 +786,14 @@ # all outgoing messages to that bridge, regardless of any other setting. #bridge_outgoing_retain true +# If you wish to restrict the size of messages sent to a remote bridge, use the +# bridge_max_packet_size option. This sets the maximum number of bytes for +# the total message, including headers and payload. +# Note that MQTT v5 brokers may provide their own maximum-packet-size property. +# In this case, the smaller of the two limits will be used. +# Set to 0 for "unlimited". +#bridge_max_packet_size 0 + # ----------------------------------------------------------------- # Certificate based SSL/TLS support diff --git a/src/bridge.c b/src/bridge.c index 36f8db6f..59d1a53c 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -162,6 +162,7 @@ int bridge__connect_step1(struct mosquitto_db *db, struct mosquitto *context) context->in_packet.payload = NULL; context->ping_t = 0; context->bridge->lazy_reconnect = false; + context->maximum_packet_size = context->bridge->maximum_packet_size; bridge__packet_cleanup(context); db__message_reconnect_reset(db, context); @@ -339,6 +340,7 @@ int bridge__connect(struct mosquitto_db *db, struct mosquitto *context) context->in_packet.payload = NULL; context->ping_t = 0; context->bridge->lazy_reconnect = false; + context->maximum_packet_size = context->bridge->maximum_packet_size; bridge__packet_cleanup(context); db__message_reconnect_reset(db, context); diff --git a/src/conf.c b/src/conf.c index a759ad2e..2df0e91a 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1035,6 +1035,19 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct if(conf__parse_bool(&token, "bridge_require_ocsp", &cur_bridge->tls_ocsp_required, saveptr)) return MOSQ_ERR_INVAL; #else log__printf(NULL, MOSQ_LOG_WARNING, "Warning: TLS support not available."); +#endif + }else if(!strcmp(token, "bridge_max_packet_size")){ +#if defined(WITH_BRIDGE) + if(reload) continue; // Bridges not valid for reloading. + if(!cur_bridge){ + log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration."); + return MOSQ_ERR_INVAL; + } + if(conf__parse_int(&token, "bridge_max_packet_size", &tmp_int, saveptr)) return MOSQ_ERR_INVAL; + if(tmp_int < 0) tmp_int = 0; + cur_bridge->maximum_packet_size = (uint32_t)tmp_int; +#else + log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); #endif }else if(!strcmp(token, "bridge_outgoing_retain")){ #if defined(WITH_BRIDGE) diff --git a/src/handle_connack.c b/src/handle_connack.c index ec487bba..0aa10b69 100644 --- a/src/handle_connack.c +++ b/src/handle_connack.c @@ -32,6 +32,7 @@ int handle__connack(struct mosquitto_db *db, struct mosquitto *context) uint8_t connect_acknowledge; uint8_t reason_code; mosquitto_property *properties = NULL; + uint32_t maximum_packet_size; if(!context){ return MOSQ_ERR_INVAL; @@ -55,6 +56,15 @@ int handle__connack(struct mosquitto_db *db, struct mosquitto *context) } rc = property__read_all(CMD_CONNACK, &context->in_packet, &properties); if(rc) return rc; + + if(mosquitto_property_read_int32(properties, MQTT_PROP_MAXIMUM_PACKET_SIZE, + &maximum_packet_size, false)){ + + if(context->maximum_packet_size == 0 || context->maximum_packet_size > maximum_packet_size){ + context->maximum_packet_size = maximum_packet_size; + } + } + mosquitto_property_free_all(&properties); } mosquitto_property_free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 5f3cd20a..6c3eee05 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -586,6 +586,7 @@ struct mosquitto__bridge{ int backoff_base; int backoff_cap; int threshold; + uint32_t maximum_packet_size; bool lazy_reconnect; bool attempt_unsubscribe; bool initial_notification_done;