diff --git a/src/mqtt/mqtt.pri b/src/mqtt/mqtt.pri index 76de864..b38a1cc 100644 --- a/src/mqtt/mqtt.pri +++ b/src/mqtt/mqtt.pri @@ -16,7 +16,6 @@ PRIVATE_HEADERS += \ $$PWD/qmqtt_message_p.h \ $$PWD/qmqtt_network_p.h \ $$PWD/qmqtt_socket_p.h \ - $$PWD/qmqtt_ssl_network_p.h \ $$PWD/qmqtt_ssl_socket_p.h \ $$PWD/qmqtt_timer_p.h @@ -26,7 +25,6 @@ SOURCES += \ $$PWD/qmqtt_frame.cpp \ $$PWD/qmqtt_message.cpp \ $$PWD/qmqtt_network.cpp \ - $$PWD/qmqtt_ssl_network.cpp \ $$PWD/qmqtt_routesubscription.cpp \ $$PWD/qmqtt_routedmessage.cpp \ $$PWD/qmqtt_router.cpp \ diff --git a/src/mqtt/qmqtt_client_p.cpp b/src/mqtt/qmqtt_client_p.cpp index 4a386b7..988b713 100644 --- a/src/mqtt/qmqtt_client_p.cpp +++ b/src/mqtt/qmqtt_client_p.cpp @@ -84,7 +84,7 @@ void QMQTT::ClientPrivate::init(const QString& hostName, const quint16 port, { _hostName = hostName; _port = port; - init(new SslNetwork(config, ignoreSelfSigned)); + init(new Network(config, ignoreSelfSigned)); } #endif // QT_NO_SSL diff --git a/src/mqtt/qmqtt_client_p.h b/src/mqtt/qmqtt_client_p.h index 1fd92f2..8d773ac 100644 --- a/src/mqtt/qmqtt_client_p.h +++ b/src/mqtt/qmqtt_client_p.h @@ -35,7 +35,6 @@ #include "qmqtt_client.h" #include "qmqtt_client_p.h" #include "qmqtt_network_p.h" -#include "qmqtt_ssl_network_p.h" #include #ifndef QT_NO_SSL diff --git a/src/mqtt/qmqtt_network.cpp b/src/mqtt/qmqtt_network.cpp index b75ce5c..0aee1bc 100644 --- a/src/mqtt/qmqtt_network.cpp +++ b/src/mqtt/qmqtt_network.cpp @@ -32,10 +32,13 @@ #include #include "qmqtt_network_p.h" #include "qmqtt_socket_p.h" +#include "qmqtt_ssl_socket_p.h" #include "qmqtt_timer_p.h" +const QString DEFAULT_HOST_NAME = QStringLiteral("localhost"); const QHostAddress DEFAULT_HOST = QHostAddress::LocalHost; const quint16 DEFAULT_PORT = 1883; +const quint16 DEFAULT_SSL_PORT = 8883; const bool DEFAULT_AUTORECONNECT = false; const int DEFAULT_AUTORECONNECT_INTERVAL_MS = 5000; @@ -52,6 +55,21 @@ QMQTT::Network::Network(QObject* parent) initialize(); } +#ifndef QT_NO_SSL +QMQTT::Network::Network(const QSslConfiguration &config, bool ignoreSelfSigned, QObject *parent) + : NetworkInterface(parent) + , _port(DEFAULT_SSL_PORT) + , _hostName(DEFAULT_HOST_NAME) + , _autoReconnect(DEFAULT_AUTORECONNECT) + , _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS) + , _bytesRemaining(0) + , _socket(new QMQTT::SslSocket(config, ignoreSelfSigned)) + , _autoReconnectTimer(new QMQTT::Timer) +{ + initialize(); +} +#endif // QT_NO_SSL + QMQTT::Network::Network(SocketInterface* socketInterface, TimerInterface* timerInterface, QObject* parent) : NetworkInterface(parent) diff --git a/src/mqtt/qmqtt_network_p.h b/src/mqtt/qmqtt_network_p.h index 42e0c7d..c1535f6 100644 --- a/src/mqtt/qmqtt_network_p.h +++ b/src/mqtt/qmqtt_network_p.h @@ -41,6 +41,10 @@ #include #include +#ifndef QT_NO_SSL +QT_FORWARD_DECLARE_CLASS(QSslConfiguration) +#endif // QT_NO_SSL + namespace QMQTT { class SocketInterface; @@ -52,6 +56,9 @@ class Network : public NetworkInterface public: Network(QObject* parent = NULL); +#ifndef QT_NO_SSL + Network(const QSslConfiguration& config, bool ignoreSelfSigned, QObject* parent = NULL); +#endif // QT_NO_SSL Network(SocketInterface* socketInterface, TimerInterface* timerInterface, QObject* parent = NULL); ~Network(); diff --git a/src/mqtt/qmqtt_ssl_network.cpp b/src/mqtt/qmqtt_ssl_network.cpp deleted file mode 100644 index 29943f1..0000000 --- a/src/mqtt/qmqtt_ssl_network.cpp +++ /dev/null @@ -1,234 +0,0 @@ -/* - * qmqtt_ssl_network.cpp - qmqtt SSL network - * - * Copyright (c) 2013 Ery Lee - * Copyright (c) 2016 Matthias Dieter Wallnöfer - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of mqttc nor the names of its contributors may be used - * to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ -#include -#include "qmqtt_ssl_network_p.h" -#include "qmqtt_ssl_socket_p.h" -#include "qmqtt_timer_p.h" - -#ifndef QT_NO_SSL - -const QString DEFAULT_HOST_NAME = QStringLiteral("localhost"); -const quint16 DEFAULT_PORT = 8883; -const bool DEFAULT_AUTORECONNECT = false; -const int DEFAULT_AUTORECONNECT_INTERVAL_MS = 5000; - -QMQTT::SslNetwork::SslNetwork(const QSslConfiguration &config, bool ignoreSelfSigned, QObject* parent) - : NetworkInterface(parent) - , _port(DEFAULT_PORT) - , _hostName(DEFAULT_HOST_NAME) - , _autoReconnect(DEFAULT_AUTORECONNECT) - , _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS) - , _bytesRemaining(0) - , _socket(new QMQTT::SslSocket(config, ignoreSelfSigned)) - , _autoReconnectTimer(new QMQTT::Timer) -{ - initialize(); -} - -QMQTT::SslNetwork::SslNetwork(SocketInterface* socketInterface, TimerInterface* timerInterface, - QObject* parent) - : NetworkInterface(parent) - , _port(DEFAULT_PORT) - , _hostName(DEFAULT_HOST_NAME) - , _autoReconnect(DEFAULT_AUTORECONNECT) - , _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS) - , _bytesRemaining(0) - , _socket(socketInterface) - , _autoReconnectTimer(timerInterface) -{ - initialize(); -} - -void QMQTT::SslNetwork::initialize() -{ - _socket->setParent(this); - _autoReconnectTimer->setParent(this); - _autoReconnectTimer->setSingleShot(true); - _autoReconnectTimer->setInterval(_autoReconnectInterval); - - QObject::connect(_socket, &SocketInterface::connected, this, &SslNetwork::connected); - QObject::connect(_socket, &SocketInterface::disconnected, this, &SslNetwork::onDisconnected); - QObject::connect(_socket->ioDevice(), &QIODevice::readyRead, this, &SslNetwork::onSocketReadReady); - QObject::connect( - _autoReconnectTimer, &TimerInterface::timeout, - this, static_cast(&SslNetwork::connectToHost)); - QObject::connect(_socket, - static_cast(&SocketInterface::error), - this, &SslNetwork::onSocketError); -} - -QMQTT::SslNetwork::~SslNetwork() -{ -} - -bool QMQTT::SslNetwork::isConnectedToHost() const -{ - return _socket->state() == QAbstractSocket::ConnectedState; -} - -void QMQTT::SslNetwork::connectToHost(const QHostAddress& host, const quint16 port) -{ - Q_UNUSED(host); - Q_UNUSED(port); - - qCritical("qmqtt: SSL does not work with host addresses!"); - emit error(QAbstractSocket::ConnectionRefusedError); -} - -void QMQTT::SslNetwork::connectToHost(const QString& hostName, const quint16 port) -{ - _hostName = hostName; - _port = port; - connectToHost(); -} - -void QMQTT::SslNetwork::connectToHost() -{ - _bytesRemaining = 0; - _socket->connectToHost(_hostName, _port); -} - -void QMQTT::SslNetwork::onSocketError(QAbstractSocket::SocketError socketError) -{ - emit error(socketError); - if(_autoReconnect) - { - _autoReconnectTimer->start(); - } -} - -void QMQTT::SslNetwork::sendFrame(Frame& frame) -{ - if(_socket->state() == QAbstractSocket::ConnectedState) - { - QDataStream out(_socket->ioDevice()); - frame.write(out); - } -} - -void QMQTT::SslNetwork::disconnectFromHost() -{ - _socket->disconnectFromHost(); -} - -QAbstractSocket::SocketState QMQTT::SslNetwork::state() const -{ - return _socket->state(); -} - -bool QMQTT::SslNetwork::autoReconnect() const -{ - return _autoReconnect; -} - -void QMQTT::SslNetwork::setAutoReconnect(const bool autoReconnect) -{ - _autoReconnect = autoReconnect; -} - -int QMQTT::SslNetwork::autoReconnectInterval() const -{ - return _autoReconnectInterval; -} - -void QMQTT::SslNetwork::setAutoReconnectInterval(const int autoReconnectInterval) -{ - _autoReconnectInterval = autoReconnectInterval; -} - -void QMQTT::SslNetwork::onSocketReadReady() -{ - QIODevice *ioDevice = _socket->ioDevice(); - while(!ioDevice->atEnd()) - { - if(_bytesRemaining == 0) - { - if (!ioDevice->getChar(reinterpret_cast(&_header))) - { - // malformed packet - emit error(QAbstractSocket::OperationError); - ioDevice->close(); - return; - } - - _bytesRemaining = readRemainingLength(); - if (_bytesRemaining < 0) - { - // malformed remaining length - emit error(QAbstractSocket::OperationError); - ioDevice->close(); - return; - } - } - - QByteArray data = ioDevice->read(_bytesRemaining); - _buffer.append(data); - _bytesRemaining -= data.size(); - - if(_bytesRemaining == 0) - { - Frame frame(_header, _buffer); - _buffer.clear(); - emit received(frame); - } - } -} - -int QMQTT::SslNetwork::readRemainingLength() -{ - quint8 byte = 0; - int length = 0; - int multiplier = 1; - QIODevice *ioDevice = _socket->ioDevice(); - do { - if (!ioDevice->getChar(reinterpret_cast(&byte))) - return -1; - length += (byte & 127) * multiplier; - multiplier *= 128; - if (multiplier > 128*128*128) - return -1; - } while ((byte & 128) != 0); - - return length; -} - -void QMQTT::SslNetwork::onDisconnected() -{ - emit disconnected(); - if(_autoReconnect) - { - _autoReconnectTimer->start(); - } -} - -#endif // QT_NO_SSL diff --git a/src/mqtt/qmqtt_ssl_network_p.h b/src/mqtt/qmqtt_ssl_network_p.h deleted file mode 100644 index 02ddb50..0000000 --- a/src/mqtt/qmqtt_ssl_network_p.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * qmqtt_ssl_network_p.h - qmqtt SSL network private header - * - * Copyright (c) 2013 Ery Lee - * Copyright (c) 2016 Matthias Dieter Wallnöfer - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of mqttc nor the names of its contributors may be used - * to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ -#ifndef QMQTT_SSL_NETWORK_P_H -#define QMQTT_SSL_NETWORK_P_H - -#include "qmqtt_networkinterface.h" -#include "qmqtt_frame.h" -#include -#include -#include -#include -#include -#include - -#ifndef QT_NO_SSL - -QT_FORWARD_DECLARE_CLASS(QSslConfiguration) - -namespace QMQTT { - -class SocketInterface; -class TimerInterface; - -class SslNetwork : public NetworkInterface -{ - Q_OBJECT - -public: - SslNetwork(const QSslConfiguration& config, bool ignoreSelfSigned, QObject* parent = NULL); - SslNetwork(SocketInterface* socketInterface, TimerInterface* timerInterface, - QObject* parent = NULL); - ~SslNetwork(); - - void sendFrame(Frame& frame); - bool isConnectedToHost() const; - bool autoReconnect() const; - void setAutoReconnect(const bool autoReconnect); - QAbstractSocket::SocketState state() const; - int autoReconnectInterval() const; - void setAutoReconnectInterval(const int autoReconnectInterval); - -public slots: - void connectToHost(const QHostAddress& host, const quint16 port); - void connectToHost(const QString& hostName, const quint16 port); - void disconnectFromHost(); - -protected slots: - void onSocketError(QAbstractSocket::SocketError socketError); - -protected: - void initialize(); - int readRemainingLength(); - - quint16 _port; - QString _hostName; - QByteArray _buffer; - bool _autoReconnect; - int _autoReconnectInterval; - int _bytesRemaining; - quint8 _header; - SocketInterface* _socket; - TimerInterface* _autoReconnectTimer; - -protected slots: - void onSocketReadReady(); - void onDisconnected(); - void connectToHost(); - -private: - Q_DISABLE_COPY(SslNetwork) -}; - -} // namespace QMQTT - -#endif // QT_NO_SSL - -#endif // QMQTT_SSL_NETWORK_P_H diff --git a/src/mqtt/qmqtt_ssl_socket.cpp b/src/mqtt/qmqtt_ssl_socket.cpp index 744ea1f..9c547d1 100644 --- a/src/mqtt/qmqtt_ssl_socket.cpp +++ b/src/mqtt/qmqtt_ssl_socket.cpp @@ -103,7 +103,7 @@ void QMQTT::SslSocket::sslErrors(const QList &errors) { if (!_ignoreSelfSigned) return; - for (QSslError error: errors) + foreach (QSslError error, errors) { if (error.error() != QSslError::SelfSignedCertificate && error.error() != QSslError::SelfSignedCertificateInChain) diff --git a/tests/gtest/tests/tests.qbs b/tests/gtest/tests/tests.qbs index cde72c3..f1846bf 100644 --- a/tests/gtest/tests/tests.qbs +++ b/tests/gtest/tests/tests.qbs @@ -45,9 +45,4 @@ Product { Depends { name: "qmqtt" } - - Depends { - /* TODO: if needed, create mqtt-private */ - name: "mqtt-private" - } }