diff options
Diffstat (limited to 'src/client/net/serverdiscovery.cpp')
-rw-r--r-- | src/client/net/serverdiscovery.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
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 |