Browse Source

fix mqtt non-terminated strings

main
Hendrik Langer 5 years ago
parent
commit
7a13877184
  1. 14
      src/network/XD0MQTT.cpp
  2. 5
      src/network/XD0MQTT.h

14
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);

5
src/network/XD0MQTT.h

@ -17,7 +17,7 @@
#include "mqtt_client.h"
typedef std::function<void(char*, char*)> cb_t;
typedef std::function<void(char*, char*, int)> 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<subscription_t> subscriptions_;
private:
bool connected = false;
// openssl s_client -showcerts -connect home.xd0.de:8883 </dev/null 2>/dev/null|openssl x509 -outform PEM >mqtt_xd0.de.pem
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \

Loading…
Cancel
Save