From 7a13877184e7fcb53acf85e871ebedaf06bde443 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Fri, 9 Aug 2019 20:55:24 +0200 Subject: [PATCH] fix mqtt non-terminated strings --- src/network/XD0MQTT.cpp | 14 ++++++++++---- src/network/XD0MQTT.h | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/network/XD0MQTT.cpp b/src/network/XD0MQTT.cpp index a323d77..7ba6a03 100644 --- a/src/network/XD0MQTT.cpp +++ b/src/network/XD0MQTT.cpp @@ -60,13 +60,15 @@ esp_err_t XD0MQTT::mqtt_event_handler_cb(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "TOPIC=%.*s\r\n", event->topic_len, event->topic); ESP_LOGI(TAG, "DATA=%.*s\r\n", event->data_len, event->data); for (const auto &subscription : subscriptions_) { + char topic[128]; + strncpy(topic, event->topic, event->topic_len); topic[event->topic_len] = '\0'; // literal match - if (strcmp(event->topic, subscription.topic) == 0) { - subscription.cb(event->topic, event->data); + if (event->topic_len == strlen(subscription.topic) && strncmp(event->topic, subscription.topic, event->topic_len) == 0) { + subscription.cb(topic, event->data, event->data_len); // '#' wildcard } else if (subscription.topic[strlen(subscription.topic)-1] == '#'){ - if (strncmp(event->topic, subscription.topic, strlen(subscription.topic)-1) == 0) { - subscription.cb(event->topic, event->data); + if (event->topic_len >= strlen(subscription.topic) && strncmp(event->topic, subscription.topic, strlen(subscription.topic)-1) == 0) { + subscription.cb(topic, event->data, event->data_len); } } // ToDo: '+' wildcard @@ -128,6 +130,10 @@ void XD0MQTT::end(void) { esp_mqtt_client_destroy(client); } +bool XD0MQTT::isConnected(void) { + return connected; +} + bool XD0MQTT::publish(const char* topic, const char* data, int len, int qos, int retain) { int msg_id = esp_mqtt_client_publish(client, topic, data, len, qos, retain); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); diff --git a/src/network/XD0MQTT.h b/src/network/XD0MQTT.h index a4699fd..43837da 100644 --- a/src/network/XD0MQTT.h +++ b/src/network/XD0MQTT.h @@ -17,7 +17,7 @@ #include "mqtt_client.h" -typedef std::function cb_t; +typedef std::function cb_t; struct subscription_t { const char* topic; @@ -37,9 +37,10 @@ class XD0MQTT { bool subscribe(const char* topic, const cb_t &cb, int qos=1); bool unsubscribe(const char* topic); esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event); - bool connected = false; + bool isConnected(void); std::vector subscriptions_; private: + bool connected = false; // openssl s_client -showcerts -connect home.xd0.de:8883 /dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem const char* rootCACertificate = \ "-----BEGIN CERTIFICATE-----\n" \