Skip to content

Commit

Permalink
Handle stale stored messages with conflicting message IDs.
Browse files Browse the repository at this point in the history
Signed-off-by: Vidar Madsen <[email protected]>
  • Loading branch information
vidarino committed Feb 18, 2021
1 parent 36b4236 commit 6bea9f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,32 @@ int db__message_reconnect_reset(struct mosquitto *context)
}


int db__message_remove_incoming(struct mosquitto* context, uint16_t mid)
{
struct mosquitto_client_msg* tail, * tmp;
bool deleted = false;

if (!context) return MOSQ_ERR_INVAL;

DL_FOREACH_SAFE(context->msgs_in.inflight, tail, tmp) {
if (tail->mid == mid) {
if (tail->store->qos != 2) {
return MOSQ_ERR_PROTOCOL;
}
db__message_remove(&context->msgs_in, tail);
deleted = true;
}
}

if (deleted) {
return MOSQ_ERR_SUCCESS;
}
else {
return MOSQ_ERR_NOT_FOUND;
}
}


int db__message_release_incoming(struct mosquitto *context, uint16_t mid)
{
struct mosquitto_client_msg *tail, *tmp;
Expand Down
7 changes: 7 additions & 0 deletions src/handle_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@ int handle__publish(struct mosquitto *context)
if(msg->qos > 0){
db__message_store_find(context, msg->source_mid, &stored);
}

if (stored && (stored->qos != msg->qos || stored->payloadlen != msg->payloadlen || strcmp(stored->topic, msg->topic) || memcmp(stored->payload, msg->payload, msg->payloadlen))){
log__printf(NULL, MOSQ_LOG_WARNING, "Reused message ID from %s detected. Clearing from storage.", context->id);
db__message_remove_incoming(context, stored->mid);
stored = NULL;
}

if(!stored){
if(msg->qos == 0
|| db__ready_for_flight(&context->msgs_in, msg->qos)
Expand Down

0 comments on commit 6bea9f7

Please sign in to comment.