MQTT parsing fix

PUBLISHED_FROM=41f43cb0e707259740de3346308f746c2a3778fd
This commit is contained in:
Sergey Lyubka 2016-12-13 10:26:27 +00:00 committed by Cesanta Bot
parent 54ad1e4e47
commit a447ae5eab
2 changed files with 21 additions and 18 deletions

View File

@ -20,8 +20,8 @@
static const char *s_address = "localhost:1883";
static const char *s_user_name = NULL;
static const char *s_password = NULL;
struct mg_mqtt_topic_expression topic_expressions[] = {{"/stuff", 0}};
static const char *s_topic = "/stuff";
static struct mg_mqtt_topic_expression s_topic_expr = {NULL, 0};
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p;
@ -48,10 +48,9 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
exit(1);
}
printf("Subscribing to '/stuff'\n");
mg_mqtt_subscribe(nc, topic_expressions,
sizeof(topic_expressions) / sizeof(*topic_expressions),
42);
s_topic_expr.topic = s_topic;
printf("Subscribing to '%s'\n", s_topic);
mg_mqtt_subscribe(nc, &s_topic_expr, 1, 42);
break;
case MG_EV_MQTT_PUBACK:
printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
@ -89,14 +88,13 @@ int main(int argc, char **argv) {
/* Parse command line arguments */
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
s_address = argv[i + 1];
i++;
s_address = argv[++i];
} else if (strcmp(argv[i], "-u") == 0) {
s_user_name = argv[i + 1];
i++;
s_user_name = argv[++i];
} else if (strcmp(argv[i], "-t") == 0) {
s_topic = argv[++i];
} else if (strcmp(argv[i], "-p") == 0) {
s_password = argv[i + 1];
i++;
s_password = argv[++i];
}
}

View File

@ -9541,10 +9541,15 @@ void mg_basic_auth_header(const char *user, const char *pass,
/* Amalgamated: #include "mongoose/src/internal.h" */
/* Amalgamated: #include "mongoose/src/mqtt.h" */
static uint16_t getu16(const char *p) {
const uint8_t *up = (const uint8_t *) p;
return (up[0] << 8) + up[1];
}
static const char *scanto(const char *p, struct mg_str *s) {
s->len = ntohs(*(uint16_t *) p);
s->len = getu16(p);
s->p = p + 2;
return p + 2 + s->len;
return s->p + s->len;
}
MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
@ -9575,7 +9580,7 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
p = scanto(p, &mm->protocol_name);
mm->protocol_version = *(uint8_t *) p++;
mm->connect_flags = *(uint8_t *) p++;
mm->keep_alive_timer = ntohs(*(uint16_t *) p);
mm->keep_alive_timer = getu16(p);
p += 2;
if (p < end) p = scanto(p, &mm->client_id);
if (p < end && (mm->connect_flags & MG_MQTT_HAS_WILL))
@ -9606,11 +9611,11 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
case MG_MQTT_CMD_PUBREL:
case MG_MQTT_CMD_PUBCOMP:
case MG_MQTT_CMD_SUBACK:
mm->message_id = ntohs(*(uint16_t *) p);
mm->message_id = getu16(p);
break;
case MG_MQTT_CMD_PUBLISH: {
if (MG_MQTT_GET_QOS(header) > 0) {
mm->message_id = ntohs(*(uint16_t *) io->buf);
mm->message_id = getu16(p);
p += 2;
}
p = scanto(p, &mm->topic);
@ -9620,7 +9625,7 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
break;
}
case MG_MQTT_CMD_SUBSCRIBE:
mm->message_id = ntohs(*(uint16_t *) p);
mm->message_id = getu16(p);
p += 2;
/*
* topic expressions are left in the payload and can be parsed with