UDP port can be a constant

This commit is contained in:
Albert Vaca Cintora 2023-07-03 15:16:54 +02:00
parent 8247e6eca6
commit 49fa11dfc1
2 changed files with 8 additions and 15 deletions

View file

@ -41,12 +41,10 @@ static const int MAX_REMEMBERED_IDENTITY_PACKETS = 42;
static const long MILLIS_DELAY_BETWEEN_CONNECTIONS_TO_SAME_DEVICE = 500;
LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint16 udpListenPort)
LanLinkProvider::LanLinkProvider(bool testMode)
: m_server(new Server(this))
, m_udpSocket(this)
, m_tcpPort(0)
, m_udpBroadcastPort(udpBroadcastPort)
, m_udpListenPort(udpListenPort)
, m_testMode(testMode)
, m_combineNetworkChangeTimer(this)
#ifdef KDECONNECT_MDNS
@ -98,12 +96,12 @@ void LanLinkProvider::onStart()
{
const QHostAddress bindAddress = m_testMode ? QHostAddress::LocalHost : QHostAddress::Any;
bool success = m_udpSocket.bind(bindAddress, m_udpListenPort, QUdpSocket::ShareAddress);
bool success = m_udpSocket.bind(bindAddress, UDP_PORT, QUdpSocket::ShareAddress);
if (!success) {
QAbstractSocket::SocketError sockErr = m_udpSocket.error();
// Refer to https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum to decode socket error number
QString errorMessage = QString::fromLatin1(QMetaEnum::fromType<QAbstractSocket::SocketError>().valueToKey(sockErr));
qCritical(KDECONNECT_CORE) << "Failed to bind UDP socket on port" << m_udpListenPort << "with error" << errorMessage;
qCritical(KDECONNECT_CORE) << "Failed to bind UDP socket on port" << UDP_PORT << "with error" << errorMessage;
}
Q_ASSERT(success);
@ -229,7 +227,7 @@ void LanLinkProvider::sendUdpIdentityPacket(QUdpSocket &socket, const QList<QHos
const QByteArray payload = identityPacket.serialize();
for (auto &address : addresses) {
qint64 bytes = socket.writeDatagram(payload, address, m_udpBroadcastPort);
qint64 bytes = socket.writeDatagram(payload, address, UDP_PORT);
if (bytes == -1 && socket.error() == QAbstractSocket::DatagramTooLargeError) {
// On macOS and FreeBSD, UDP broadcasts larger than MTU get dropped. See:
// https://opensource.apple.com/source/xnu/xnu-3789.1.32/bsd/netinet/ip_output.c.auto.html#:~:text=/*%20don%27t%20allow%20broadcast%20messages%20to%20be%20fragmented%20*/
@ -239,7 +237,7 @@ void LanLinkProvider::sendUdpIdentityPacket(QUdpSocket &socket, const QList<QHos
identityPacket.set(QStringLiteral("outgoingCapabilities"), QStringList());
identityPacket.set(QStringLiteral("incomingCapabilities"), QStringList());
const QByteArray smallPayload = identityPacket.serialize();
socket.writeDatagram(smallPayload, address, m_udpBroadcastPort);
socket.writeDatagram(smallPayload, address, UDP_PORT);
}
}
}
@ -330,7 +328,7 @@ void LanLinkProvider::connectError(QAbstractSocket::SocketError socketError)
qCDebug(KDECONNECT_CORE) << "Fallback (1), try reverse connection (send udp packet)" << socket->errorString();
NetworkPacket np = KdeConnectConfig::instance().deviceInfo().toIdentityPacket();
np.set(QStringLiteral("tcpPort"), m_tcpPort);
m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackets[socket].sender, m_udpBroadcastPort);
m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT);
// The socket we created didn't work, and we didn't manage
// to create a LanDeviceLink from it, deleting everything.
@ -380,7 +378,7 @@ void LanLinkProvider::tcpSocketConnected()
// The socket doesn't seem to work, so we can't create the connection.
qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)";
m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, m_udpBroadcastPort);
m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT);
// Disconnect should trigger deleteLater, which should remove the socket from m_receivedIdentityPackets
socket->disconnectFromHost();

View file

@ -34,10 +34,8 @@ class KDECONNECTCORE_EXPORT LanLinkProvider : public LinkProvider
public:
/**
* @param testMode Some special overrides needed while testing
* @param udpBroadcastPort Port which should be used for *sending* identity packets
* @param udpListenPort Port which should be used for *receiving* identity packets
*/
LanLinkProvider(bool testMode = false, quint16 udpBroadcastPort = UDP_PORT, quint16 udpListenPort = UDP_PORT);
LanLinkProvider(bool testMode = false);
~LanLinkProvider() override;
QString name() override
@ -83,9 +81,6 @@ private:
QUdpSocket m_udpSocket;
quint16 m_tcpPort;
quint16 m_udpBroadcastPort;
quint16 m_udpListenPort;
QMap<QString, LanDeviceLink *> m_links;
struct PendingConnect {