Skip to content

Commit

Permalink
Disonnect clients with invalid MQTT packets
Browse files Browse the repository at this point in the history
(cherry picked from commit 049fd29)
  • Loading branch information
CurlyMoo committed Jan 15, 2020
1 parent 5bb7578 commit 43d5de3
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions libs/pilight/core/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,13 @@ int mqtt_decode(struct mqtt_pkt_t **pkt, unsigned char *buf, unsigned int len, u
if(len < msglength+((*pos)-1)) {
/*
* Packet too short for full message
* so we are waiting for additional
* content. Not an error and also not
* a successful decode, so a return 1.
*/
mqtt_free((*pkt));
FREE((*pkt));

return 1;
}

Expand Down Expand Up @@ -2018,6 +2022,12 @@ static void client_read_cb(uv_poll_t *req, ssize_t *nread, char *buf) {
if(*nread > 0) {
buf[*nread] = '\0';

// int i = 0;
// for(i=0;i<*nread;i++) {
// printf("0x02x ", (unsigned char)buf[i]);
// }
// printf("\n");

while(*nread > 0 && (ret = mqtt_decode(&pkt, (unsigned char *)buf, *nread, &pos)) == 0) {
memmove(&buf[0], &buf[pos], *nread-pos);
*nread -= pos;
Expand Down Expand Up @@ -2146,8 +2156,18 @@ static void client_read_cb(uv_poll_t *req, ssize_t *nread, char *buf) {
pthread_mutex_unlock(&mqtt_lock);
#endif

uv_custom_write(req);
uv_custom_read(req);

if(ret == -1) {
/*
* Dirty fix:
* disconnect clients when an invalid package
* has been received to prevent endless loops
*/
uv_custom_write(req);
} else {
uv_custom_write(req);
uv_custom_read(req);
}
}

static void server_read_cb(uv_poll_t *req, ssize_t *nread, char *buf) {
Expand Down

0 comments on commit 43d5de3

Please sign in to comment.