From 9b6b1a68f49a2807175af07df58a8c277f2f4204 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 17 Sep 2021 14:02:29 +0100 Subject: [PATCH] Add MQTT version check --- examples/mqtt-server/Makefile | 2 +- examples/mqtt-server/main.c | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/mqtt-server/Makefile b/examples/mqtt-server/Makefile index 3d179d14..7870cca0 100644 --- a/examples/mqtt-server/Makefile +++ b/examples/mqtt-server/Makefile @@ -1,7 +1,7 @@ PROG ?= example all: $(PROG) - $(DEBUGGER) ./$(PROG) $(ARGS) + $(RUN) ./$(PROG) $(ARGS) $(PROG): main.c $(CC) ../../mongoose.c -I../.. -W -Wall -DMG_ENABLE_LINES=1 $(CFLAGS) -o $(PROG) main.c diff --git a/examples/mqtt-server/main.c b/examples/mqtt-server/main.c index d8305442..82124963 100644 --- a/examples/mqtt-server/main.c +++ b/examples/mqtt-server/main.c @@ -27,15 +27,21 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { LOG(LL_DEBUG, ("cmd %d qos %d", mm->cmd, mm->qos)); switch (mm->cmd) { case MQTT_CMD_CONNECT: { - // Client connects. Return success, do not check user/password - uint8_t response[] = {0, 0}; - mg_mqtt_send_header(c, MQTT_CMD_CONNACK, 0, sizeof(response)); - mg_send(c, response, sizeof(response)); + // Client connects + if (mm->dgram.len < 9) { + mg_error(c, "Malformed MQTT frame"); + } else if (mm->dgram.ptr[8] != 4) { + mg_error(c, "Unsupported MQTT version %d", mm->dgram.ptr[8]); + } else { + uint8_t response[] = {0, 0}; + mg_mqtt_send_header(c, MQTT_CMD_CONNACK, 0, sizeof(response)); + mg_send(c, response, sizeof(response)); + } break; } case MQTT_CMD_SUBSCRIBE: { // Client subscribes - int pos = 4; // Initial topic offset, where ID ends + size_t pos = 4; // Initial topic offset, where ID ends uint8_t qos; struct mg_str topic; while ((pos = mg_mqtt_next_sub(mm, &topic, &qos, pos)) > 0) { @@ -60,6 +66,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { break; } } + } else if (ev == MG_EV_ACCEPT) { + // c->is_hexdumping = 1; } else if (ev == MG_EV_CLOSE) { // Client disconnects. Remove from the subscription list for (struct sub *next, *sub = s_subs; sub != NULL; sub = next) {