qmqtt_ssl_socket.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * qmqtt_ssl_socket.cpp - qmqtt SSL socket
  3. *
  4. * Copyright (c) 2013 Ery Lee <ery.lee at gmail dot com>
  5. * Copyright (c) 2016 Matthias Dieter Wallnöfer
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of mqttc nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "qmqtt_ssl_socket_p.h"
  34. #ifndef QT_NO_SSL
  35. #include <QSslSocket>
  36. #include <QSslError>
  37. QMQTT::SslSocket::SslSocket(const QSslConfiguration& config, QObject* parent)
  38. : SocketInterface(parent)
  39. , _socket(new QSslSocket(this))
  40. {
  41. _socket->setSslConfiguration(config);
  42. connect(_socket.data(), &QSslSocket::encrypted, this, &SocketInterface::connected);
  43. connect(_socket.data(), &QSslSocket::disconnected, this, &SocketInterface::disconnected);
  44. connect(_socket.data(),
  45. #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
  46. static_cast<void (QSslSocket::*)(QAbstractSocket::SocketError)>(&QSslSocket::errorOccurred),
  47. #else
  48. static_cast<void (QSslSocket::*)(QAbstractSocket::SocketError)>(&QSslSocket::error),
  49. #endif
  50. this,
  51. static_cast<void (SocketInterface::*)(QAbstractSocket::SocketError)>(&SocketInterface::error));
  52. connect(_socket.data(),
  53. static_cast<void (QSslSocket::*)(const QList<QSslError>&)>(&QSslSocket::sslErrors),
  54. this,
  55. &SslSocket::sslErrors);
  56. }
  57. QMQTT::SslSocket::~SslSocket()
  58. {
  59. }
  60. QIODevice *QMQTT::SslSocket::ioDevice()
  61. {
  62. return _socket.data();
  63. }
  64. void QMQTT::SslSocket::connectToHost(const QHostAddress& address, quint16 port)
  65. {
  66. _socket->connectToHostEncrypted(address.toString(), port);
  67. }
  68. void QMQTT::SslSocket::connectToHost(const QString& hostName, quint16 port)
  69. {
  70. _socket->connectToHostEncrypted(hostName, port);
  71. }
  72. void QMQTT::SslSocket::disconnectFromHost()
  73. {
  74. _socket->disconnectFromHost();
  75. }
  76. QAbstractSocket::SocketState QMQTT::SslSocket::state() const
  77. {
  78. return _socket->state();
  79. }
  80. QAbstractSocket::SocketError QMQTT::SslSocket::error() const
  81. {
  82. return _socket->error();
  83. }
  84. void QMQTT::SslSocket::ignoreSslErrors(const QList<QSslError>& errors)
  85. {
  86. _socket->ignoreSslErrors(errors);
  87. }
  88. void QMQTT::SslSocket::ignoreSslErrors()
  89. {
  90. _socket->ignoreSslErrors();
  91. }
  92. QSslConfiguration QMQTT::SslSocket::sslConfiguration() const
  93. {
  94. return _socket->sslConfiguration();
  95. }
  96. void QMQTT::SslSocket::setSslConfiguration(const QSslConfiguration& config)
  97. {
  98. _socket->setSslConfiguration(config);
  99. }
  100. #endif // QT_NO_SSL