Commit a447ae5e authored by Sergey Lyubka's avatar Sergey Lyubka Committed by Cesanta Bot

MQTT parsing fix

PUBLISHED_FROM=41f43cb0e707259740de3346308f746c2a3778fd
parent 54ad1e4e
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
static const char *s_address = "localhost:1883"; static const char *s_address = "localhost:1883";
static const char *s_user_name = NULL; static const char *s_user_name = NULL;
static const char *s_password = NULL; static const char *s_password = NULL;
static const char *s_topic = "/stuff";
struct mg_mqtt_topic_expression topic_expressions[] = {{"/stuff", 0}}; static struct mg_mqtt_topic_expression s_topic_expr = {NULL, 0};
static void ev_handler(struct mg_connection *nc, int ev, void *p) { static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) 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) { ...@@ -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); printf("Got mqtt connection error: %d\n", msg->connack_ret_code);
exit(1); exit(1);
} }
printf("Subscribing to '/stuff'\n"); s_topic_expr.topic = s_topic;
mg_mqtt_subscribe(nc, topic_expressions, printf("Subscribing to '%s'\n", s_topic);
sizeof(topic_expressions) / sizeof(*topic_expressions), mg_mqtt_subscribe(nc, &s_topic_expr, 1, 42);
42);
break; break;
case MG_EV_MQTT_PUBACK: case MG_EV_MQTT_PUBACK:
printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id); printf("Message publishing acknowledged (msg_id: %d)\n", msg->message_id);
...@@ -89,14 +88,13 @@ int main(int argc, char **argv) { ...@@ -89,14 +88,13 @@ int main(int argc, char **argv) {
/* Parse command line arguments */ /* Parse command line arguments */
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) { if (strcmp(argv[i], "-h") == 0) {
s_address = argv[i + 1]; s_address = argv[++i];
i++;
} else if (strcmp(argv[i], "-u") == 0) { } else if (strcmp(argv[i], "-u") == 0) {
s_user_name = argv[i + 1]; s_user_name = argv[++i];
i++; } else if (strcmp(argv[i], "-t") == 0) {
s_topic = argv[++i];
} else if (strcmp(argv[i], "-p") == 0) { } else if (strcmp(argv[i], "-p") == 0) {
s_password = argv[i + 1]; s_password = argv[++i];
i++;
} }
} }
......
...@@ -9541,10 +9541,15 @@ void mg_basic_auth_header(const char *user, const char *pass, ...@@ -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/internal.h" */
/* Amalgamated: #include "mongoose/src/mqtt.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) { 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; 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) { 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) { ...@@ -9575,7 +9580,7 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
p = scanto(p, &mm->protocol_name); p = scanto(p, &mm->protocol_name);
mm->protocol_version = *(uint8_t *) p++; mm->protocol_version = *(uint8_t *) p++;
mm->connect_flags = *(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; p += 2;
if (p < end) p = scanto(p, &mm->client_id); if (p < end) p = scanto(p, &mm->client_id);
if (p < end && (mm->connect_flags & MG_MQTT_HAS_WILL)) 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) { ...@@ -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_PUBREL:
case MG_MQTT_CMD_PUBCOMP: case MG_MQTT_CMD_PUBCOMP:
case MG_MQTT_CMD_SUBACK: case MG_MQTT_CMD_SUBACK:
mm->message_id = ntohs(*(uint16_t *) p); mm->message_id = getu16(p);
break; break;
case MG_MQTT_CMD_PUBLISH: { case MG_MQTT_CMD_PUBLISH: {
if (MG_MQTT_GET_QOS(header) > 0) { if (MG_MQTT_GET_QOS(header) > 0) {
mm->message_id = ntohs(*(uint16_t *) io->buf); mm->message_id = getu16(p);
p += 2; p += 2;
} }
p = scanto(p, &mm->topic); p = scanto(p, &mm->topic);
...@@ -9620,7 +9625,7 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) { ...@@ -9620,7 +9625,7 @@ MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
break; break;
} }
case MG_MQTT_CMD_SUBSCRIBE: case MG_MQTT_CMD_SUBSCRIBE:
mm->message_id = ntohs(*(uint16_t *) p); mm->message_id = getu16(p);
p += 2; p += 2;
/* /*
* topic expressions are left in the payload and can be parsed with * topic expressions are left in the payload and can be parsed with
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment