Skip to content

Commit

Permalink
Add retain_available support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Nov 22, 2018
1 parent 36e8659 commit 9560c5b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions man/mosquitto.conf.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,16 @@
<para>Reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>retain_available</option> [ true | false ]</term>
<listitem>
<para>If set to false, then retained messages are not
supported. Clients that send a message with the retain
bit will be disconnected if this option is set to
false. Defaults to true.</para>
<para>Reloaded on reload signal.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>retained_persistence</option> [ true | false ]</term>
<listitem>
Expand Down
6 changes: 6 additions & 0 deletions mosquitto.conf
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
#per_listener_settings false


# Set to false to disable retained message support. If a client publishes a
# message with the retain bit set, it will be disconnected if this is set to
# false.
#retain_available true


# =================================================================
# Default listener
# =================================================================
Expand Down
3 changes: 3 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ static void config__init_reload(struct mosquitto_db *db, struct mosquitto__confi
config->persistence_file = NULL;
config->persistent_client_expiration = 0;
config->queue_qos0_messages = false;
config->retain_available = true;
config->set_tcp_nodelay = false;
config->sys_interval = 10;
config->upgrade_outgoing_qos = false;
Expand Down Expand Up @@ -1729,6 +1730,8 @@ int config__read_file_core(struct mosquitto__config *config, bool reload, struct
#else
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available.");
#endif
}else if(!strcmp(token, "retain_available")){
if(conf__parse_bool(&token, token, &config->retain_available, saveptr)) return MOSQ_ERR_INVAL;
}else if(!strcmp(token, "retry_interval")){
log__printf(NULL, MOSQ_LOG_WARNING, "Warning: The retry_interval option is no longer available.");
}else if(!strcmp(token, "round_robin")){
Expand Down
4 changes: 4 additions & 0 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ int db__messages_easy_queue(struct mosquitto_db *db, struct mosquitto *context,
topic_heap = mosquitto__strdup(topic);
if(!topic_heap) return MOSQ_ERR_INVAL;

if(db->config->retain_available == false){
retain = 0;
}

if(UHPA_ALLOC(payload_uhpa, payloadlen) == 0){
mosquitto__free(topic_heap);
return MOSQ_ERR_NOMEM;
Expand Down
8 changes: 8 additions & 0 deletions src/handle_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context)
password_flag = connect_flags & 0x40;
username_flag = connect_flags & 0x80;

if(will && will_retain && db->config->retain_available == false){
if(protocol_version == mosq_p_mqtt5){
send__connack(db, context, 0, MQTT_RC_RETAIN_NOT_SUPPORTED);
}
rc = 1;
goto handle_connect_error;
}

if(packet__read_uint16(&context->in_packet, &(context->keepalive))){
rc = 1;
goto handle_connect_error;
Expand Down
7 changes: 7 additions & 0 deletions src/handle_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context)
}
retain = (header & 0x01);

if(retain && db->config->retain_available == false){
if(context->protocol == mosq_p_mqtt5){
send__disconnect(context, MQTT_RC_RETAIN_NOT_SUPPORTED, NULL);
}
return 1;
}

if(packet__read_string(&context->in_packet, &topic, &slen)) return 1;
if(!slen){
/* Invalid publish topic, disconnect client. */
Expand Down
1 change: 1 addition & 0 deletions src/mosquitto_broker_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ struct mosquitto__config {
char *pid_file;
bool queue_qos0_messages;
bool per_listener_settings;
bool retain_available;
bool set_tcp_nodelay;
int sys_interval;
bool upgrade_outgoing_qos;
Expand Down
7 changes: 7 additions & 0 deletions src/send_connack.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ int send__connack(struct mosquitto_db *db, struct mosquitto *context, int ack, i
packet->command = CMD_CONNACK;
packet->remaining_length = 2;
if(context->protocol == mosq_p_mqtt5){
if(reason_code < 128 && db->config->retain_available == false){
rc = mosquitto_property_add_byte(&properties, MQTT_PROP_RETAIN_AVAILABLE, 0);
if(rc){
mosquitto__free(packet);
return rc;
}
}
proplen = property__get_length_all(properties);
varbytes = packet__varint_bytes(proplen);
packet->remaining_length += proplen + varbytes;
Expand Down

0 comments on commit 9560c5b

Please sign in to comment.