diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 247f8d818..a3b84e6b5 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -79,16 +79,16 @@ void LanLinkProvider::onStart() { const QHostAddress bindAddress = mTestMode? QHostAddress::LocalHost : QHostAddress::Any; - bool success = mUdpSocket.bind(bindAddress, PORT, QUdpSocket::ShareAddress); + bool success = mUdpSocket.bind(bindAddress, UDP_PORT, QUdpSocket::ShareAddress); Q_ASSERT(success); qCDebug(KDECONNECT_CORE) << "onStart"; - mTcpPort = MIN_PORT; + mTcpPort = MIN_TCP_PORT; while (!mServer->listen(bindAddress, mTcpPort)) { mTcpPort++; - if (mTcpPort > MAX_PORT) { //No ports available? - qCritical(KDECONNECT_CORE) << "Error opening a port in range" << MIN_PORT << "-" << MAX_PORT; + if (mTcpPort > MAX_TCP_PORT) { //No ports available? + qCritical(KDECONNECT_CORE) << "Error opening a port in range" << MIN_TCP_PORT << "-" << MAX_TCP_PORT; mTcpPort = 0; return; } @@ -143,15 +143,15 @@ void LanLinkProvider::broadcastToNetwork() QHostAddress sourceAddress = ifaceAddress.ip(); if (sourceAddress.protocol() == QAbstractSocket::IPv4Protocol && sourceAddress != QHostAddress::LocalHost) { qCDebug(KDECONNECT_CORE()) << "Broadcasting as" << sourceAddress; - sendSocket.bind(sourceAddress, PORT); - sendSocket.writeDatagram(np.serialize(), destAddress, PORT); + sendSocket.bind(sourceAddress, UDP_PORT); + sendSocket.writeDatagram(np.serialize(), destAddress, UDP_PORT); sendSocket.close(); } } } } #else - mUdpSocket.writeDatagram(np.serialize(), destAddress, PORT); + mUdpSocket.writeDatagram(np.serialize(), destAddress, UDP_PORT); #endif } @@ -189,7 +189,7 @@ void LanLinkProvider::newUdpConnection() //udpBroadcastReceived continue; } - int tcpPort = receivedPackage->get(QStringLiteral("tcpPort"), PORT); + int tcpPort = receivedPackage->get(QStringLiteral("tcpPort")); //qCDebug(KDECONNECT_CORE) << "Received Udp identity package from" << sender << " asking for a tcp connection on port " << tcpPort; @@ -213,7 +213,7 @@ void LanLinkProvider::connectError() NetworkPackage np(QLatin1String("")); NetworkPackage::createIdentityPackage(&np); np.set(QStringLiteral("tcpPort"), mTcpPort); - mUdpSocket.writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, PORT); + mUdpSocket.writeDatagram(np.serialize(), receivedIdentityPackages[socket].sender, UDP_PORT); //The socket we created didn't work, and we didn't manage //to create a LanDeviceLink from it, deleting everything. @@ -276,7 +276,7 @@ void LanLinkProvider::connected() //I think this will never happen, but if it happens the deviceLink //(or the socket that is now inside it) might not be valid. Delete them. qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)"; - mUdpSocket.writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, PORT); + mUdpSocket.writeDatagram(np2.serialize(), receivedIdentityPackages[socket].sender, UDP_PORT); } delete receivedIdentityPackages.take(socket).np; diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index b8b83d083..30f798a2e 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -54,8 +54,9 @@ public: static void configureSslSocket(QSslSocket* socket, const QString& deviceId, bool isDeviceTrusted); static void configureSocket(QSslSocket* socket); - const static quint16 MIN_PORT = 1716; - const static quint16 MAX_PORT = 1764; + const static quint16 UDP_PORT = 1716; + const static quint16 MIN_TCP_PORT = 1716; + const static quint16 MAX_TCP_PORT = 1764; public Q_SLOTS: void onNetworkChange() override; diff --git a/tests/lanlinkprovidertest.cpp b/tests/lanlinkprovidertest.cpp index 5eba6e929..14f7114d0 100644 --- a/tests/lanlinkprovidertest.cpp +++ b/tests/lanlinkprovidertest.cpp @@ -58,7 +58,7 @@ private Q_SLOTS: private: - const int PORT = 8520; + const int TEST_PORT = 8520; // Add some private fields here LanLinkProvider mLanLinkProvider; Server* mServer; @@ -91,7 +91,7 @@ void LanLinkProviderTest::initTestCase() mLanLinkProvider.onStart(); - mIdentityPackage = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(PORT) + QStringLiteral("}}"); + mIdentityPackage = QStringLiteral("{\"id\":1439365924847,\"type\":\"kdeconnect.identity\",\"body\":{\"deviceId\":\"testdevice\",\"deviceName\":\"Test Device\",\"protocolVersion\":6,\"deviceType\":\"phone\",\"tcpPort\":") + QString::number(TEST_PORT) + QStringLiteral("}}"); } void LanLinkProviderTest::pairedDeviceTcpPackageReceived() @@ -100,7 +100,7 @@ void LanLinkProviderTest::pairedDeviceTcpPackageReceived() addTrustedDevice(); QUdpSocket* mUdpServer = new QUdpSocket; - bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::PORT, QUdpSocket::ShareAddress); + bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::UDP_PORT, QUdpSocket::ShareAddress); QVERIFY(b); QSignalSpy spy(mUdpServer, SIGNAL(readyRead())); @@ -157,11 +157,11 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived() mServer = new Server(this); mUdpSocket = new QUdpSocket(this); - mServer->listen(QHostAddress::LocalHost, PORT); + mServer->listen(QHostAddress::LocalHost, TEST_PORT); QSignalSpy spy(mServer, SIGNAL(newConnection())); - qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::PORT); // write an identity package to udp socket here, we do not broadcast it here + qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::UDP_PORT); // write an identity package to udp socket here, we do not broadcast it here QCOMPARE(bytesWritten, mIdentityPackage.size()); // We should have an incoming connection now, wait for incoming connection @@ -208,7 +208,7 @@ void LanLinkProviderTest::pairedDeviceUdpPackageReceived() void LanLinkProviderTest::unpairedDeviceTcpPackageReceived() { QUdpSocket* mUdpServer = new QUdpSocket; - bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::PORT, QUdpSocket::ShareAddress); + bool b = mUdpServer->bind(QHostAddress::LocalHost, LanLinkProvider::UDP_PORT, QUdpSocket::ShareAddress); QVERIFY(b); QSignalSpy spy(mUdpServer, SIGNAL(readyRead())); @@ -260,10 +260,10 @@ void LanLinkProviderTest::unpairedDeviceUdpPackageReceived() mServer = new Server(this); mUdpSocket = new QUdpSocket(this); - mServer->listen(QHostAddress::LocalHost, PORT); + mServer->listen(QHostAddress::LocalHost, TEST_PORT); QSignalSpy spy(mServer, &Server::newConnection); - qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::PORT); // write an identity package to udp socket here, we do not broadcast it here + qint64 bytesWritten = mUdpSocket->writeDatagram(mIdentityPackage.toLatin1(), QHostAddress::LocalHost, LanLinkProvider::UDP_PORT); // write an identity package to udp socket here, we do not broadcast it here QCOMPARE(bytesWritten, mIdentityPackage.size()); QVERIFY(!spy.isEmpty() || spy.wait());