diff options
Diffstat (limited to 'src/client/net')
-rw-r--r-- | src/client/net/serverconnection.cpp | 51 | ||||
-rw-r--r-- | src/client/net/serverconnection.h | 16 | ||||
-rw-r--r-- | src/client/net/serverdiscovery.cpp | 33 | ||||
-rw-r--r-- | src/client/net/serverdiscovery.h | 6 |
4 files changed, 54 insertions, 52 deletions
diff --git a/src/client/net/serverconnection.cpp b/src/client/net/serverconnection.cpp index ca19c76..690ba23 100644 --- a/src/client/net/serverconnection.cpp +++ b/src/client/net/serverconnection.cpp @@ -1,21 +1,20 @@ #include "serverconnection.h" -#include <QtCore> -#include <QPixmap> -#include <QGuiApplication> -#include <QHostInfo> -#include <unistd.h> -#include <cstdlib> -#include <sys/types.h> -#include <pwd.h> -//#define verbose #include "../vnc/vncserver.h" - #include "../../shared/util.h" #include "../../shared/settings.h" #include "../util/platform/blankscreen.h" #include "../clientapp/clientapp.h" +#include <QPixmap> +#include <QGuiApplication> +#include <QHostInfo> +#include <QScreen> +// For getting logged-in username +#include <sys/types.h> +#include <pwd.h> +#include <unistd.h> + #define CHALLENGE_LEN 20 ServerConnection::ServerConnection(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect) : @@ -23,16 +22,14 @@ ServerConnection::ServerConnection(const QString& host, const quint16 port, cons { _socket = new QSslSocket(); _blank = new BlankScreen(); - connect(_socket, SIGNAL(encrypted()), this, SLOT(sock_connected())); - connect(_socket, SIGNAL(readyRead()), this, SLOT(sock_dataArrival())); - connect(_socket, SIGNAL(disconnected()), this, SLOT(sock_closed())); - connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sock_error(QAbstractSocket::SocketError))); - connect(_socket, - SIGNAL(sslErrors(const QList<QSslError> &)), - this, - SLOT(sslErrors(const QList<QSslError> &)) + connect(_socket, &QSslSocket::encrypted, this, &ServerConnection::sock_connected); + connect(_socket, &QSslSocket::readyRead, this, &ServerConnection::sock_dataArrival); + connect(_socket, &QSslSocket::disconnected, this, &ServerConnection::sock_closed); + connect(_socket, QOverload<QAbstractSocket::SocketError>::of(&QSslSocket::errorOccurred), + this, &ServerConnection::sock_error); + connect(_socket, QOverload<const QList<QSslError> &>::of(&QSslSocket::sslErrors), + this, &ServerConnection::sslErrors ); - connect(_socket, &QSslSocket::peerVerifyError, [=](const QSslError &error) { qDebug() << "PVE:" << error.errorString(); }); qDebug("Connecting to %s on port %d", host.toUtf8().data(), int(port)); _socket->ignoreSslErrors(); _socket->connectToHostEncrypted(host, port); @@ -40,7 +37,7 @@ ServerConnection::ServerConnection(const QString& host, const quint16 port, cons _lastData = QDateTime::currentMSecsSinceEpoch() + PING_TIMEOUT_MS; _timerConnectionCheck = startTimer(5000); // Connect the vnc start/stop signal to this class, so we can tell the server about successful vnc server startup - connect(VncServer::instance(), SIGNAL(started(int, QString&, QString&)), this, SLOT(onVncServerStartStop(int, QString&, QString&))); + connect(VncServer::instance(), &VncServer::started, this, &ServerConnection::onVncServerStartStop); } ServerConnection::~ServerConnection() @@ -106,13 +103,18 @@ void ServerConnection::handleMsg() _lastData = QDateTime::currentMSecsSinceEpoch() + PING_TIMEOUT_MS; const QString &id = _fromServer.getFieldString(_ID); + if (id == _ERROR) { + qWarning() << "Server sent error message:" << _fromServer.getFieldString(_ERROR); + return; + } + if (_authed == 0) { if (id == _CHALLENGE) { // Initial challenge request by server emit stateChange(ConnectWindow::AwaitingChallengeResponse); _myChallenge.resize(CHALLENGE_LEN); for (int i = 0; i < CHALLENGE_LEN; ++i) { - _myChallenge[i] = char(qrand() & 0xff); + _myChallenge[i] = char(slxrand() & 0xff); } QByteArray serverChallenge(_fromServer.getFieldBytes(_CHALLENGE)); _toServer.reset(); @@ -320,7 +322,7 @@ void ServerConnection::timerEvent(QTimerEvent *event) * server was succesfully started, or was terminated (either planned or * crashed). */ -void ServerConnection::onVncServerStartStop(int port, QString& ropass, QString& rwpass) +void ServerConnection::onVncServerStartStop(int port, const QString &ropass, const QString &rwpass) { _toServer.reset(); _toServer.setField(_ID, _VNCSERVER); @@ -357,9 +359,8 @@ void ServerConnection::onVncViewerStartStop(const bool started, const int client void ServerConnection::sslErrors(const QList<QSslError> & errors) { _socket->ignoreSslErrors(); - for (QList<QSslError>::const_iterator it = errors.begin(); it != errors.end(); it++) { - const QSslError &err = *it; - qDebug("Connect SSL: %s", qPrintable(err.errorString())); + for (const auto &err : errors) { + qDebug("Connect SSL: %s", qPrintable(err.errorString())); if (err.error() == QSslError::HostNameMismatch) continue; // We don't pay attention to hostnames for validation if (err.error() == QSslError::SelfSignedCertificate) diff --git a/src/client/net/serverconnection.h b/src/client/net/serverconnection.h index f5f6264..f434f0b 100644 --- a/src/client/net/serverconnection.h +++ b/src/client/net/serverconnection.h @@ -33,18 +33,15 @@ private: void checkLocalConnection(); public: - ServerConnection(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect); + ServerConnection(const QString& host, quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect); void disconnectFromServer(); - ~ServerConnection(); + ~ServerConnection() override; inline bool isConnected() const { return _socket != nullptr && _socket->state() == QAbstractSocket::ConnectedState; } - const inline QString getPeerAdress() const - { - return _socket->peerAddress().toString(); - } + QString getPeerAdress() const { return _socket->peerAddress().toString(); } bool isLocalConnection() { if (_isLocalConnection == -1) { @@ -57,7 +54,7 @@ public: void sendAttention(bool on); protected: - void timerEvent(QTimerEvent *event); + void timerEvent(QTimerEvent *event) override; private slots: void sslErrors(const QList<QSslError> & errors); // triggered for errors that occur during SSL negotiation @@ -66,9 +63,10 @@ private slots: void sock_error(QAbstractSocket::SocketError errcode); // triggered if an error occurs on the socket void sock_connected(); // triggered if the connection is established and ready to use - void onVncServerStartStop(int port, QString& ropass, QString& rwpass); // triggered if the local vnc server was started + void onVncServerStartStop(int port, const QString &ropass, const QString &rwpass); // triggered if the local vnc server was started - void onVncViewerStartStop(const bool started, const int clientId); +public slots: + void onVncViewerStartStop(bool started, int clientId); signals: void openVnc(const QString& host, int port, const QString& passwd, bool ro, bool fullscreen, const QString& caption, const int clientId, const QByteArray& rawThumb); diff --git a/src/client/net/serverdiscovery.cpp b/src/client/net/serverdiscovery.cpp index b2d7605..5076a8b 100644 --- a/src/client/net/serverdiscovery.cpp +++ b/src/client/net/serverdiscovery.cpp @@ -4,8 +4,7 @@ #include "../../shared/network.h" #include "../../shared/util.h" #include "serverdiscovery.h" -#include <assert.h> - +#include "../util/util.h" /** * Ctor @@ -21,33 +20,31 @@ ServerDiscovery::ServerDiscovery(QObject *parent) /* Try to get a UDP port for server discovery */ int tries = 10; while (tries-- != 0) { - quint16 port = quint16(16384 + qrand() % 32768); + quint16 port = quint16(16384 + slxrand() % 32768); if (_discoverySocket.bind(QHostAddress::AnyIPv4, port)) break; if (tries == 0) qFatal("Could not bind to any UDP port for server discovery."); } // Handle incoming messages - connect(&_discoverySocket, SIGNAL(readyRead()), this, SLOT(onUdpReadyRead())); + connect(&_discoverySocket, &QUdpSocket::readyRead, this, &ServerDiscovery::onUdpReadyRead); /* Setup the discovery timer */ _discoveryTimer.setInterval(_minDiscoveryInterval); _discoveryTimer.setSingleShot(true); // - connect(&_discoveryTimer, SIGNAL(timeout()), this, SLOT(doDiscovery())); + connect(&_discoveryTimer, &QTimer::timeout, this, &ServerDiscovery::doDiscovery); } /** * Dtor */ -ServerDiscovery::~ServerDiscovery() -{ -} +ServerDiscovery::~ServerDiscovery() = default; /** * @brief start */ -void ServerDiscovery::start(const QByteArray& sessionName, QString mgrIP) +void ServerDiscovery::start(const QByteArray& sessionName, const QString& mgrIP) { if (!mgrIP.isEmpty()) { _mgrIP.setAddress(mgrIP); @@ -98,8 +95,8 @@ void ServerDiscovery::doDiscovery() if (_salt2.size() < SALT_LEN) _salt2.resize(SALT_LEN); for (int i = 0; i < SALT_LEN; ++i) { - salt1[i] = char(qrand() & 0xff); - _salt2[i] = char(qrand() & 0xff); + salt1[i] = char(slxrand() & 0xff); + _salt2[i] = char(slxrand() & 0xff); } _packet.reset(); _packet.setField(_HASH, genSha1(&_nameBytes, &salt1, &iplist)); @@ -109,7 +106,7 @@ void ServerDiscovery::doDiscovery() // Check if specifig manager IP is given. If not broadcast in whole network. if (_mgrIP != QHostAddress::Null) { - qDebug() << "Broadcasting to " << _mgrIP.toString(); + qDebug() << "Sending discovery to " << _mgrIP.toString(); if (!_packet.writeMessage(&_discoverySocket, _mgrIP, SERVICE_DISCOVERY_PORT)) qDebug("Failed"); } else { @@ -141,7 +138,7 @@ void ServerDiscovery::onUdpReadyRead() { char data[UDPBUFSIZ]; QHostAddress addr; - quint16 port; + quint16 peerPort; while (_discoverySocket.hasPendingDatagrams()) { // Discard any packets if discovery is stopped if (!this->isActive()) { @@ -149,13 +146,15 @@ void ServerDiscovery::onUdpReadyRead() continue; } - const qint64 size = _discoverySocket.readDatagram(data, UDPBUFSIZ, &addr, &port); + const qint64 size = _discoverySocket.readDatagram(data, UDPBUFSIZ, &addr, &peerPort); if (size <= 0) //|| clientApp->connection() != nullptr) // TODO CHECK continue; _packet.reset(); - if (_packet.readMessage(data, quint32(size)) != NM_READ_OK) + if (_packet.readMessage(data, quint32(size)) != NM_READ_OK) { + qDebug() << "Corrupt discovery reply from" << addr.toString(); continue; + } // Valid packet, process it: const QByteArray hash(_packet.getFieldBytes(_HASH)); @@ -165,6 +164,7 @@ void ServerDiscovery::onUdpReadyRead() // Check if the source IP of the packet matches any of the addresses given in the IP list if (!Network::isAddressInList(QString::fromUtf8(iplist), addr.toString())) { + qDebug() << "Received bogus discovery reply from" << addr.toString() << "... Not in" << iplist; ++_ipErrorCount; emit error(ErrorType::InvalidIpList, _hashErrorCount); continue; @@ -173,6 +173,7 @@ void ServerDiscovery::onUdpReadyRead() // If so, check if the submitted hash seems valid if (genSha1(&_nameBytes, &_salt2, &iplist, &port, &cert) != hash && _mgrIP != addr) { // did not match local session name, or other data was spoofed + qDebug() << "Received bogus session name in discovery reply from" << addr.toString(); ++_hashErrorCount; emit error(ErrorType::InvalidHash, _ipErrorCount); continue; @@ -187,6 +188,8 @@ void ServerDiscovery::onUdpReadyRead() const ushort iport = QString::fromUtf8(port).toUShort(&ok); if (ok) { emit serverDetected(addr.toString(), quint16(iport), _nameBytes, cert, (_mgrIP == addr)); + } else { + qDebug() << "... but server advertises unparsable port" << port; } // Stop the discovery diff --git a/src/client/net/serverdiscovery.h b/src/client/net/serverdiscovery.h index d7d6010..21f9bf9 100644 --- a/src/client/net/serverdiscovery.h +++ b/src/client/net/serverdiscovery.h @@ -17,10 +17,10 @@ public: InvalidHash }; - explicit ServerDiscovery(QObject *parent = 0); - ~ServerDiscovery(); + explicit ServerDiscovery(QObject *parent = nullptr); + ~ServerDiscovery() override; - void start(const QByteArray& sessionName, QString mgrIP); + void start(const QByteArray& sessionName, const QString& mgrIP); void stop(); inline bool isActive() { return _discoveryTimer.isActive(); } |