...
 
Commits (2)
......@@ -197,7 +197,8 @@ void LanLinkProvider::udpBroadcastReceived()
continue;
}
if (receivedPacket->get<QString>(QStringLiteral("deviceId")) == KdeConnectConfig::instance()->deviceId()) {
const QString deviceId = receivedPacket->get<QString>(QStringLiteral("deviceId"));
if (deviceId == KdeConnectConfig::instance()->deviceId()) {
//qCDebug(KDECONNECT_CORE) << "Ignoring my own broadcast";
delete receivedPacket;
continue;
......@@ -213,7 +214,15 @@ void LanLinkProvider::udpBroadcastReceived()
m_receivedIdentityPackets[socket].sender = sender;
connect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::tcpSocketConnected);
connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &LanLinkProvider::connectError);
socket->connectToHost(sender, tcpPort);
bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId);
configureSslSocket(socket, deviceId, isDeviceTrusted);
if (isDeviceTrusted) {
connect(socket, QOverload<const QList<QSslError> &>::of(&QSslSocket::sslErrors), this, &LanLinkProvider::sslErrors);
}
socket->connectToHostEncrypted(sender.toString(), tcpPort);
}
}
......@@ -248,49 +257,6 @@ void LanLinkProvider::tcpSocketConnected()
// If socket disconnects due to any reason after connection, link on ssl failure
connect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater);
NetworkPacket* receivedPacket = m_receivedIdentityPackets[socket].np;
const QString& deviceId = receivedPacket->get<QString>(QStringLiteral("deviceId"));
//qCDebug(KDECONNECT_CORE) << "tcpSocketConnected" << socket->isWritable();
// If network is on ssl, do not believe when they are connected, believe when handshake is completed
NetworkPacket np2(QLatin1String(""));
NetworkPacket::createIdentityPacket(&np2);
socket->write(np2.serialize());
bool success = socket->waitForBytesWritten();
if (success) {
qCDebug(KDECONNECT_CORE) << "TCP connection done (i'm the existing device)";
// if ssl supported
if (receivedPacket->get<int>(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) {
bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId);
configureSslSocket(socket, deviceId, isDeviceTrusted);
qCDebug(KDECONNECT_CORE) << "Starting server ssl (I'm the client TCP socket)";
connect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted);
if (isDeviceTrusted) {
connect(socket, QOverload<const QList<QSslError> &>::of(&QSslSocket::sslErrors), this, &LanLinkProvider::sslErrors);
}
socket->startServerEncryption();
return; // Return statement prevents from deleting received packet, needed in slot "encrypted"
} else {
qWarning() << receivedPacket->get<QString>(QStringLiteral("deviceName")) << "uses an old protocol version, this won't work";
//addLink(deviceId, socket, receivedPacket, LanDeviceLink::Remotely);
}
} else {
//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)";
m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT);
}
delete m_receivedIdentityPackets.take(socket).np;
//We don't delete the socket because now it's owned by the LanDeviceLink
}
......@@ -460,8 +426,6 @@ void LanLinkProvider::configureSslSocket(QSslSocket* socket, const QString& devi
void LanLinkProvider::configureSocket(QSslSocket* socket) {
socket->setProxy(QNetworkProxy::NoProxy);
socket->setSocketOption(QAbstractSocket::KeepAliveOption, QVariant(1));
#ifdef TCP_KEEPIDLE
......