From bd59fca1a2a1def2d1078f3f9c5f3b1c3367c9a0 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Thu, 15 Dec 2016 11:44:22 +0900 Subject: [PATCH 1/4] BugFix of Issue #44 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp index 3998e46..3eeba5e 100644 --- a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp @@ -55,14 +55,11 @@ void MQTTSNConnectionHandler::handleSearchgw(MQTTSNPacket* packet) { if (packet->getType() == MQTTSN_SEARCHGW) { - //if (_gateway->getClientList()->getClientCount() < MAX_CLIENTS) - //{ - MQTTSNPacket* gwinfo = new MQTTSNPacket(); - gwinfo->setGWINFO(_gateway->getGWParams()->gatewayId); - Event* ev1 = new Event(); - ev1->setBrodcastEvent(gwinfo); - _gateway->getClientSendQue()->post(ev1); - //} + MQTTSNPacket* gwinfo = new MQTTSNPacket(); + gwinfo->setGWINFO(_gateway->getGWParams()->gatewayId); + Event* ev1 = new Event(); + ev1->setBrodcastEvent(gwinfo); + _gateway->getClientSendQue()->post(ev1); } } From e05d205843ead37d1ec44b2406d4615ab0053c15 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 17 Jan 2017 15:36:02 +0900 Subject: [PATCH 2/4] BugFix: Wild card of Topic + and # handling. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 12 ++++++++---- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClient.cpp | 10 +++++----- MQTTSNGateway/src/MQTTSNGWClient.h | 10 +++++----- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index cbca3e7..9497414 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -49,6 +49,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet) /* create MQTTSN_topicid */ MQTTSN_topicid topicId; + uint16_t id = 0; if (pub.topiclen == 2) { @@ -61,11 +62,14 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet) topicId.type = MQTTSN_TOPIC_TYPE_NORMAL; topicId.data.long_.len = pub.topiclen; topicId.data.long_.name = pub.topic; - unsigned short id = client->getTopics()->getTopicId(&topicId); - topicId.data.id = id; + id = client->getTopics()->getTopicId(&topicId); } - if (topicId.data.id == 0) + if (id > 0) + { + topicId.data.id = id; + } + else { /* This message might be subscribed with wild card. */ Topic* topic = client->getTopics()->match(&topicId); @@ -85,7 +89,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet) /* add the Topic and get a TopicId */ topic = client->getTopics()->add(&topicId); - uint16_t id = topic->getTopicId(); + id = topic->getTopicId(); if (id > 0) { diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index 7701af8..5d093dc 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -136,7 +136,7 @@ void BrokerRecvTask::run(void) client->getNetwork()->close(); delete packet; - /* delete client when the client is not authorized on & session is clean */ + /* delete client when the client is not authorized & session is clean */ _gateway->getClientList()->erase(client); if ( client ) diff --git a/MQTTSNGateway/src/MQTTSNGWClient.cpp b/MQTTSNGateway/src/MQTTSNGWClient.cpp index 965e90e..93bee22 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClient.cpp @@ -830,7 +830,7 @@ Topics::~Topics() } } -uint16_t Topics::getTopicId(MQTTSN_topicid* topicid) +uint16_t Topics::getTopicId(const MQTTSN_topicid* topicid) { if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL) { @@ -863,7 +863,7 @@ Topic* Topics::getTopic(uint16_t id) return 0; } -Topic* Topics::getTopic(MQTTSN_topicid* topicid) +Topic* Topics::getTopic(const MQTTSN_topicid* topicid) { Topic* p = _first; while (p) @@ -877,7 +877,7 @@ Topic* Topics::getTopic(MQTTSN_topicid* topicid) return 0; } -Topic* Topics::add(MQTTSN_topicid* topicid) +Topic* Topics::add(const MQTTSN_topicid* topicid) { Topic* topic; uint16_t id = 0; @@ -901,7 +901,7 @@ Topic* Topics::add(MQTTSN_topicid* topicid) } -Topic* Topics::add(string* topicName) +Topic* Topics::add(const string* topicName) { Topic* topic = 0; @@ -942,7 +942,7 @@ uint16_t Topics::getNextTopicId() return ++_nextTopicId == 0xffff ? _nextTopicId += 2 : _nextTopicId; } -Topic* Topics::match(MQTTSN_topicid* topicid) +Topic* Topics::match(const MQTTSN_topicid* topicid) { if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL) { diff --git a/MQTTSNGateway/src/MQTTSNGWClient.h b/MQTTSNGateway/src/MQTTSNGWClient.h index c4012c7..76c5c11 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.h +++ b/MQTTSNGateway/src/MQTTSNGWClient.h @@ -127,13 +127,13 @@ class Topics public: Topics(); ~Topics(); - Topic* add(MQTTSN_topicid* topicid); - Topic* add(string* topic); - uint16_t getTopicId(MQTTSN_topicid* topic); + Topic* add(const MQTTSN_topicid* topicid); + Topic* add(const string* topic); + uint16_t getTopicId(const MQTTSN_topicid* topic); uint16_t getNextTopicId(); Topic* getTopic(uint16_t topicId); - Topic* getTopic(MQTTSN_topicid* topicid); - Topic* match(MQTTSN_topicid* topicid); + Topic* getTopic(const MQTTSN_topicid* topicid); + Topic* match(const MQTTSN_topicid* topicid); private: uint16_t _nextTopicId; From 7ac0d89fb11b345d107b49f80060b7448c1e436c Mon Sep 17 00:00:00 2001 From: tomoaki Date: Wed, 18 Jan 2017 09:54:07 +0900 Subject: [PATCH 3/4] BugFix of Issue #45 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 121 +++++++++++---------- 1 file changed, 61 insertions(+), 60 deletions(-) diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index 9497414..67d3df7 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -63,75 +63,76 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet) topicId.data.long_.len = pub.topiclen; topicId.data.long_.name = pub.topic; id = client->getTopics()->getTopicId(&topicId); - } - if (id > 0) - { - topicId.data.id = id; - } - else - { - /* This message might be subscribed with wild card. */ - Topic* topic = client->getTopics()->match(&topicId); - if (topic == 0) + if ( id > 0 ) { - WRITELOG(" Invalid Topic. PUBLISH message is canceled.\n"); - if (pub.header.bits.qos == 1) - { - replyACK(client, &pub, PUBACK); - } - else if ( pub.header.bits.qos == 2 ) - { - replyACK(client, &pub, PUBREC); - } - return; - } - - /* add the Topic and get a TopicId */ - topic = client->getTopics()->add(&topicId); - id = topic->getTopicId(); - - if (id > 0) - { - /* create REGACK */ - MQTTSNPacket* regPacket = new MQTTSNPacket(); - - MQTTSNString topicName; - topicName.lenstring.len = topicId.data.long_.len; - topicName.lenstring.data = topicId.data.long_.name; - - uint16_t regackMsgId = client->getNextSnMsgId(); - regPacket->setREGISTER(id, regackMsgId, &topicName); - - if (client->isSleep()) - { - client->setClientSleepPacket(regPacket); - WRITELOG(FORMAT_BL_NL, currentDateTime(), regPacket->getName(), - RIGHTARROW, client->getClientId(), "is sleeping. REGISTER was saved."); - } - else if (client->isActive()) - { - /* send REGISTER */ - Event* evrg = new Event(); - evrg->setClientSendEvent(client, regPacket); - _gateway->getClientSendQue()->post(evrg); - } - - /* send PUBLISH */ topicId.data.id = id; - snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, - (uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, - pub.payloadlen); - client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId); } else { - WRITELOG("\x1b[0m\x1b[31mMQTTGWPublishHandler Can't create a Topic.\n"); - return; + /* This message might be subscribed with wild card. */ + Topic* topic = client->getTopics()->match(&topicId); + if (topic == 0) + { + WRITELOG(" Invalid Topic. PUBLISH message is canceled.\n"); + if (pub.header.bits.qos == 1) + { + replyACK(client, &pub, PUBACK); + } + else if ( pub.header.bits.qos == 2 ) + { + replyACK(client, &pub, PUBREC); + } + return; + } + + /* add the Topic and get a TopicId */ + topic = client->getTopics()->add(&topicId); + id = topic->getTopicId(); + + if (id > 0) + { + /* create REGACK */ + MQTTSNPacket* regPacket = new MQTTSNPacket(); + + MQTTSNString topicName; + topicName.lenstring.len = topicId.data.long_.len; + topicName.lenstring.data = topicId.data.long_.name; + + uint16_t regackMsgId = client->getNextSnMsgId(); + regPacket->setREGISTER(id, regackMsgId, &topicName); + + if (client->isSleep()) + { + client->setClientSleepPacket(regPacket); + WRITELOG(FORMAT_BL_NL, currentDateTime(), regPacket->getName(), + RIGHTARROW, client->getClientId(), "is sleeping. REGISTER was saved."); + } + else if (client->isActive()) + { + /* send REGISTER */ + Event* evrg = new Event(); + evrg->setClientSendEvent(client, regPacket); + _gateway->getClientSendQue()->post(evrg); + } + + /* send PUBLISH */ + topicId.data.id = id; + snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, + (uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, + pub.payloadlen); + client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId); + } + else + { + WRITELOG("\x1b[0m\x1b[31mMQTTGWPublishHandler Can't create a Topic.\n"); + delete snPacket; + return; + } } } - /* TopicId was aquired. */ + /* TopicId was acquired. */ if (client->isSleep()) { /* client is sleeping. save PUBLISH */ From 8d93f0a96ec007c71f36a838a88dca145a515e00 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 24 Feb 2017 10:19:32 +0900 Subject: [PATCH 4/4] Update for older OpenSSL version. Signed-off-by: tomoaki --- MQTTSNGateway/src/linux/Network.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/MQTTSNGateway/src/linux/Network.cpp b/MQTTSNGateway/src/linux/Network.cpp index ba3069c..7c10ffa 100644 --- a/MQTTSNGateway/src/linux/Network.cpp +++ b/MQTTSNGateway/src/linux/Network.cpp @@ -290,7 +290,15 @@ bool Network::connect(const char* host, const char* port, const char* caPath, co { SSL_load_error_strings(); SSL_library_init(); + +#if ( OPENSSL_VERSION_NUMBER >= 0x10100000L ) _ctx = SSL_CTX_new(TLS_client_method()); +#elif ( OPENSSL_VERSION_NUMBER >= 0x10001000L ) + _ctx = SSL_CTX_new(TLSv1_client_method()); +#else + _ctx = SSL_CTX_new(SSLv23_client_method()); +#endif + if (_ctx == 0) { ERR_error_string_n(ERR_get_error(), errmsg, sizeof(errmsg));