Changeset View
Changeset View
Standalone View
Standalone View
core/backends/lan/lanlinkprovider.cpp
Show First 20 Lines • Show All 126 Lines • ▼ Show 20 Line(s) | 122 | { | |||
---|---|---|---|---|---|
127 | } | 127 | } | ||
128 | 128 | | |||
129 | Q_ASSERT(m_tcpPort != 0); | 129 | Q_ASSERT(m_tcpPort != 0); | ||
130 | 130 | | |||
131 | qCDebug(KDECONNECT_CORE()) << "Broadcasting identity packet"; | 131 | qCDebug(KDECONNECT_CORE()) << "Broadcasting identity packet"; | ||
132 | 132 | | |||
133 | QHostAddress destAddress = m_testMode? QHostAddress::LocalHost : QHostAddress(QStringLiteral("255.255.255.255")); | 133 | QHostAddress destAddress = m_testMode? QHostAddress::LocalHost : QHostAddress(QStringLiteral("255.255.255.255")); | ||
134 | 134 | | |||
135 | NetworkPackage np(QLatin1String("")); | 135 | NetworkPacket np(QLatin1String("")); | ||
136 | NetworkPackage::createIdentityPackage(&np); | 136 | NetworkPacket::createIdentityPacket(&np); | ||
137 | np.set(QStringLiteral("tcpPort"), m_tcpPort); | 137 | np.set(QStringLiteral("tcpPort"), m_tcpPort); | ||
138 | 138 | | |||
139 | #ifdef Q_OS_WIN | 139 | #ifdef Q_OS_WIN | ||
140 | //On Windows we need to broadcast from every local IP address to reach all networks | 140 | //On Windows we need to broadcast from every local IP address to reach all networks | ||
141 | QUdpSocket sendSocket; | 141 | QUdpSocket sendSocket; | ||
142 | sendSocket.setProxy(QNetworkProxy::NoProxy); | 142 | sendSocket.setProxy(QNetworkProxy::NoProxy); | ||
143 | for (const QNetworkInterface& iface : QNetworkInterface::allInterfaces()) { | 143 | for (const QNetworkInterface& iface : QNetworkInterface::allInterfaces()) { | ||
144 | if ( (iface.flags() & QNetworkInterface::IsUp) | 144 | if ( (iface.flags() & QNetworkInterface::IsUp) | ||
Show All 26 Lines | 168 | while (m_udpSocket.hasPendingDatagrams()) { | |||
171 | datagram.resize(m_udpSocket.pendingDatagramSize()); | 171 | datagram.resize(m_udpSocket.pendingDatagramSize()); | ||
172 | QHostAddress sender; | 172 | QHostAddress sender; | ||
173 | 173 | | |||
174 | m_udpSocket.readDatagram(datagram.data(), datagram.size(), &sender); | 174 | m_udpSocket.readDatagram(datagram.data(), datagram.size(), &sender); | ||
175 | 175 | | |||
176 | if (sender.isLoopback() && !m_testMode) | 176 | if (sender.isLoopback() && !m_testMode) | ||
177 | continue; | 177 | continue; | ||
178 | 178 | | |||
179 | NetworkPackage* receivedPackage = new NetworkPackage(QLatin1String("")); | 179 | NetworkPacket* receivedPacket = new NetworkPacket(QLatin1String("")); | ||
180 | bool success = NetworkPackage::unserialize(datagram, receivedPackage); | 180 | bool success = NetworkPacket::unserialize(datagram, receivedPacket); | ||
181 | 181 | | |||
182 | //qCDebug(KDECONNECT_CORE) << "udp connection from " << receivedPackage->; | 182 | //qCDebug(KDECONNECT_CORE) << "udp connection from " << receivedPacket->; | ||
183 | 183 | | |||
184 | //qCDebug(KDECONNECT_CORE) << "Datagram " << datagram.data() ; | 184 | //qCDebug(KDECONNECT_CORE) << "Datagram " << datagram.data() ; | ||
185 | 185 | | |||
186 | if (!success || receivedPackage->type() != PACKAGE_TYPE_IDENTITY) { | 186 | if (!success || receivedPacket->type() != PACKET_TYPE_IDENTITY) { | ||
187 | delete receivedPackage; | 187 | delete receivedPacket; | ||
188 | continue; | 188 | continue; | ||
189 | } | 189 | } | ||
190 | 190 | | |||
191 | if (receivedPackage->get<QString>(QStringLiteral("deviceId")) == KdeConnectConfig::instance()->deviceId()) { | 191 | if (receivedPacket->get<QString>(QStringLiteral("deviceId")) == KdeConnectConfig::instance()->deviceId()) { | ||
192 | //qCDebug(KDECONNECT_CORE) << "Ignoring my own broadcast"; | 192 | //qCDebug(KDECONNECT_CORE) << "Ignoring my own broadcast"; | ||
193 | delete receivedPackage; | 193 | delete receivedPacket; | ||
194 | continue; | 194 | continue; | ||
195 | } | 195 | } | ||
196 | 196 | | |||
197 | int tcpPort = receivedPackage->get<int>(QStringLiteral("tcpPort")); | 197 | int tcpPort = receivedPacket->get<int>(QStringLiteral("tcpPort")); | ||
198 | 198 | | |||
199 | //qCDebug(KDECONNECT_CORE) << "Received Udp identity package from" << sender << " asking for a tcp connection on port " << tcpPort; | 199 | //qCDebug(KDECONNECT_CORE) << "Received Udp identity packet from" << sender << " asking for a tcp connection on port " << tcpPort; | ||
200 | 200 | | |||
201 | QSslSocket* socket = new QSslSocket(this); | 201 | QSslSocket* socket = new QSslSocket(this); | ||
202 | socket->setProxy(QNetworkProxy::NoProxy); | 202 | socket->setProxy(QNetworkProxy::NoProxy); | ||
203 | m_receivedIdentityPackages[socket].np = receivedPackage; | 203 | m_receivedIdentityPackets[socket].np = receivedPacket; | ||
204 | m_receivedIdentityPackages[socket].sender = sender; | 204 | m_receivedIdentityPackets[socket].sender = sender; | ||
205 | connect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | 205 | connect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | ||
206 | connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | 206 | connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | ||
207 | socket->connectToHost(sender, tcpPort); | 207 | socket->connectToHost(sender, tcpPort); | ||
208 | } | 208 | } | ||
209 | } | 209 | } | ||
210 | 210 | | |||
211 | void LanLinkProvider::connectError() | 211 | void LanLinkProvider::connectError() | ||
212 | { | 212 | { | ||
213 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | 213 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | ||
214 | if (!socket) return; | 214 | if (!socket) return; | ||
215 | disconnect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | 215 | disconnect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | ||
216 | disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | 216 | disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | ||
217 | 217 | | |||
218 | qCDebug(KDECONNECT_CORE) << "Fallback (1), try reverse connection (send udp packet)" << socket->errorString(); | 218 | qCDebug(KDECONNECT_CORE) << "Fallback (1), try reverse connection (send udp packet)" << socket->errorString(); | ||
219 | NetworkPackage np(QLatin1String("")); | 219 | NetworkPacket np(QLatin1String("")); | ||
220 | NetworkPackage::createIdentityPackage(&np); | 220 | NetworkPacket::createIdentityPacket(&np); | ||
221 | np.set(QStringLiteral("tcpPort"), m_tcpPort); | 221 | np.set(QStringLiteral("tcpPort"), m_tcpPort); | ||
222 | m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackages[socket].sender, UDP_PORT); | 222 | m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT); | ||
223 | 223 | | |||
224 | //The socket we created didn't work, and we didn't manage | 224 | //The socket we created didn't work, and we didn't manage | ||
225 | //to create a LanDeviceLink from it, deleting everything. | 225 | //to create a LanDeviceLink from it, deleting everything. | ||
226 | delete m_receivedIdentityPackages.take(socket).np; | 226 | delete m_receivedIdentityPackets.take(socket).np; | ||
227 | delete socket; | 227 | delete socket; | ||
228 | } | 228 | } | ||
229 | 229 | | |||
230 | //We received a UDP package and answered by connecting to them by TCP. This gets called on a succesful connection. | 230 | //We received a UDP packet and answered by connecting to them by TCP. This gets called on a succesful connection. | ||
231 | void LanLinkProvider::connected() | 231 | void LanLinkProvider::connected() | ||
232 | { | 232 | { | ||
233 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | 233 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | ||
234 | 234 | | |||
235 | if (!socket) return; | 235 | if (!socket) return; | ||
236 | disconnect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | 236 | disconnect(socket, &QAbstractSocket::connected, this, &LanLinkProvider::connected); | ||
237 | disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | 237 | disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectError())); | ||
238 | 238 | | |||
239 | configureSocket(socket); | 239 | configureSocket(socket); | ||
240 | 240 | | |||
241 | // If socket disconnects due to any reason after connection, link on ssl failure | 241 | // If socket disconnects due to any reason after connection, link on ssl failure | ||
242 | connect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater); | 242 | connect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater); | ||
243 | 243 | | |||
244 | NetworkPackage* receivedPackage = m_receivedIdentityPackages[socket].np; | 244 | NetworkPacket* receivedPacket = m_receivedIdentityPackets[socket].np; | ||
245 | const QString& deviceId = receivedPackage->get<QString>(QStringLiteral("deviceId")); | 245 | const QString& deviceId = receivedPacket->get<QString>(QStringLiteral("deviceId")); | ||
246 | //qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable(); | 246 | //qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable(); | ||
247 | 247 | | |||
248 | // If network is on ssl, do not believe when they are connected, believe when handshake is completed | 248 | // If network is on ssl, do not believe when they are connected, believe when handshake is completed | ||
249 | NetworkPackage np2(QLatin1String("")); | 249 | NetworkPacket np2(QLatin1String("")); | ||
250 | NetworkPackage::createIdentityPackage(&np2); | 250 | NetworkPacket::createIdentityPacket(&np2); | ||
251 | socket->write(np2.serialize()); | 251 | socket->write(np2.serialize()); | ||
252 | bool success = socket->waitForBytesWritten(); | 252 | bool success = socket->waitForBytesWritten(); | ||
253 | 253 | | |||
254 | if (success) { | 254 | if (success) { | ||
255 | 255 | | |||
256 | qCDebug(KDECONNECT_CORE) << "TCP connection done (i'm the existing device)"; | 256 | qCDebug(KDECONNECT_CORE) << "TCP connection done (i'm the existing device)"; | ||
257 | 257 | | |||
258 | // if ssl supported | 258 | // if ssl supported | ||
259 | if (receivedPackage->get<int>(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { | 259 | if (receivedPacket->get<int>(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { | ||
260 | 260 | | |||
261 | bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId); | 261 | bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId); | ||
262 | configureSslSocket(socket, deviceId, isDeviceTrusted); | 262 | configureSslSocket(socket, deviceId, isDeviceTrusted); | ||
263 | 263 | | |||
264 | qCDebug(KDECONNECT_CORE) << "Starting server ssl (I'm the client TCP socket)"; | 264 | qCDebug(KDECONNECT_CORE) << "Starting server ssl (I'm the client TCP socket)"; | ||
265 | 265 | | |||
266 | connect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | 266 | connect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | ||
267 | 267 | | |||
268 | if (isDeviceTrusted) { | 268 | if (isDeviceTrusted) { | ||
269 | connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | 269 | connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | ||
270 | } | 270 | } | ||
271 | 271 | | |||
272 | socket->startServerEncryption(); | 272 | socket->startServerEncryption(); | ||
273 | 273 | | |||
274 | return; // Return statement prevents from deleting received package, needed in slot "encrypted" | 274 | return; // Return statement prevents from deleting received packet, needed in slot "encrypted" | ||
275 | } else { | 275 | } else { | ||
276 | qWarning() << receivedPackage->get<QString>(QStringLiteral("deviceName")) << "uses an old protocol version, this won't work"; | 276 | qWarning() << receivedPacket->get<QString>(QStringLiteral("deviceName")) << "uses an old protocol version, this won't work"; | ||
277 | //addLink(deviceId, socket, receivedPackage, LanDeviceLink::Remotely); | 277 | //addLink(deviceId, socket, receivedPacket, LanDeviceLink::Remotely); | ||
278 | } | 278 | } | ||
279 | 279 | | |||
280 | } else { | 280 | } else { | ||
281 | //I think this will never happen, but if it happens the deviceLink | 281 | //I think this will never happen, but if it happens the deviceLink | ||
282 | //(or the socket that is now inside it) might not be valid. Delete them. | 282 | //(or the socket that is now inside it) might not be valid. Delete them. | ||
283 | qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)"; | 283 | qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)"; | ||
284 | m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackages[socket].sender, UDP_PORT); | 284 | m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT); | ||
285 | } | 285 | } | ||
286 | 286 | | |||
287 | delete m_receivedIdentityPackages.take(socket).np; | 287 | delete m_receivedIdentityPackets.take(socket).np; | ||
288 | //We don't delete the socket because now it's owned by the LanDeviceLink | 288 | //We don't delete the socket because now it's owned by the LanDeviceLink | ||
289 | } | 289 | } | ||
290 | 290 | | |||
291 | void LanLinkProvider::encrypted() | 291 | void LanLinkProvider::encrypted() | ||
292 | { | 292 | { | ||
293 | qCDebug(KDECONNECT_CORE) << "Socket succesfully stablished an SSL connection"; | 293 | qCDebug(KDECONNECT_CORE) << "Socket succesfully stablished an SSL connection"; | ||
294 | 294 | | |||
295 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | 295 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | ||
296 | if (!socket) return; | 296 | if (!socket) return; | ||
297 | disconnect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | 297 | disconnect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | ||
298 | disconnect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | 298 | disconnect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | ||
299 | 299 | | |||
300 | Q_ASSERT(socket->mode() != QSslSocket::UnencryptedMode); | 300 | Q_ASSERT(socket->mode() != QSslSocket::UnencryptedMode); | ||
301 | LanDeviceLink::ConnectionStarted connectionOrigin = (socket->mode() == QSslSocket::SslClientMode)? LanDeviceLink::Locally : LanDeviceLink::Remotely; | 301 | LanDeviceLink::ConnectionStarted connectionOrigin = (socket->mode() == QSslSocket::SslClientMode)? LanDeviceLink::Locally : LanDeviceLink::Remotely; | ||
302 | 302 | | |||
303 | NetworkPackage* receivedPackage = m_receivedIdentityPackages[socket].np; | 303 | NetworkPacket* receivedPacket = m_receivedIdentityPackets[socket].np; | ||
304 | const QString& deviceId = receivedPackage->get<QString>(QStringLiteral("deviceId")); | 304 | const QString& deviceId = receivedPacket->get<QString>(QStringLiteral("deviceId")); | ||
305 | 305 | | |||
306 | addLink(deviceId, socket, receivedPackage, connectionOrigin); | 306 | addLink(deviceId, socket, receivedPacket, connectionOrigin); | ||
307 | 307 | | |||
308 | // Copied from connected slot, now delete received package | 308 | // Copied from connected slot, now delete received packet | ||
309 | delete m_receivedIdentityPackages.take(socket).np; | 309 | delete m_receivedIdentityPackets.take(socket).np; | ||
310 | } | 310 | } | ||
311 | 311 | | |||
312 | void LanLinkProvider::sslErrors(const QList<QSslError>& errors) | 312 | void LanLinkProvider::sslErrors(const QList<QSslError>& errors) | ||
313 | { | 313 | { | ||
314 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | 314 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | ||
315 | if (!socket) return; | 315 | if (!socket) return; | ||
316 | 316 | | |||
317 | disconnect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | 317 | disconnect(socket, &QSslSocket::encrypted, this, &LanLinkProvider::encrypted); | ||
318 | disconnect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | 318 | disconnect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | ||
319 | 319 | | |||
320 | qCDebug(KDECONNECT_CORE) << "Failing due to " << errors; | 320 | qCDebug(KDECONNECT_CORE) << "Failing due to " << errors; | ||
321 | Device* device = Daemon::instance()->getDevice(socket->peerVerifyName()); | 321 | Device* device = Daemon::instance()->getDevice(socket->peerVerifyName()); | ||
322 | if (device) { | 322 | if (device) { | ||
323 | device->unpair(); | 323 | device->unpair(); | ||
324 | } | 324 | } | ||
325 | 325 | | |||
326 | delete m_receivedIdentityPackages.take(socket).np; | 326 | delete m_receivedIdentityPackets.take(socket).np; | ||
327 | // Socket disconnects itself on ssl error and will be deleted by deleteLater slot, no need to delete manually | 327 | // Socket disconnects itself on ssl error and will be deleted by deleteLater slot, no need to delete manually | ||
328 | } | 328 | } | ||
329 | 329 | | |||
330 | //I'm the new device and this is the answer to my UDP identity package (no data received yet). They are connecting to us through TCP, and they should send an identity. | 330 | //I'm the new device and this is the answer to my UDP identity packet (no data received yet). They are connecting to us through TCP, and they should send an identity. | ||
331 | void LanLinkProvider::newConnection() | 331 | void LanLinkProvider::newConnection() | ||
332 | { | 332 | { | ||
333 | //qCDebug(KDECONNECT_CORE) << "LanLinkProvider newConnection"; | 333 | //qCDebug(KDECONNECT_CORE) << "LanLinkProvider newConnection"; | ||
334 | 334 | | |||
335 | while (m_server->hasPendingConnections()) { | 335 | while (m_server->hasPendingConnections()) { | ||
336 | QSslSocket* socket = m_server->nextPendingConnection(); | 336 | QSslSocket* socket = m_server->nextPendingConnection(); | ||
337 | configureSocket(socket); | 337 | configureSocket(socket); | ||
338 | //This socket is still managed by us (and child of the QTcpServer), if | 338 | //This socket is still managed by us (and child of the QTcpServer), if | ||
339 | //it disconnects before we manage to pass it to a LanDeviceLink, it's | 339 | //it disconnects before we manage to pass it to a LanDeviceLink, it's | ||
340 | //our responsibility to delete it. We do so with this connection. | 340 | //our responsibility to delete it. We do so with this connection. | ||
341 | connect(socket, &QAbstractSocket::disconnected, | 341 | connect(socket, &QAbstractSocket::disconnected, | ||
342 | socket, &QObject::deleteLater); | 342 | socket, &QObject::deleteLater); | ||
343 | connect(socket, &QIODevice::readyRead, | 343 | connect(socket, &QIODevice::readyRead, | ||
344 | this, &LanLinkProvider::dataReceived); | 344 | this, &LanLinkProvider::dataReceived); | ||
345 | 345 | | |||
346 | } | 346 | } | ||
347 | } | 347 | } | ||
348 | 348 | | |||
349 | //I'm the new device and this is the answer to my UDP identity package (data received) | 349 | //I'm the new device and this is the answer to my UDP identity packet (data received) | ||
350 | void LanLinkProvider::dataReceived() | 350 | void LanLinkProvider::dataReceived() | ||
351 | { | 351 | { | ||
352 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | 352 | QSslSocket* socket = qobject_cast<QSslSocket*>(sender()); | ||
353 | 353 | | |||
354 | const QByteArray data = socket->readLine(); | 354 | const QByteArray data = socket->readLine(); | ||
355 | 355 | | |||
356 | //qCDebug(KDECONNECT_CORE) << "LanLinkProvider received reply:" << data; | 356 | //qCDebug(KDECONNECT_CORE) << "LanLinkProvider received reply:" << data; | ||
357 | 357 | | |||
358 | NetworkPackage* np = new NetworkPackage(QLatin1String("")); | 358 | NetworkPacket* np = new NetworkPacket(QLatin1String("")); | ||
359 | bool success = NetworkPackage::unserialize(data, np); | 359 | bool success = NetworkPacket::unserialize(data, np); | ||
360 | 360 | | |||
361 | if (!success) { | 361 | if (!success) { | ||
362 | delete np; | 362 | delete np; | ||
363 | return; | 363 | return; | ||
364 | } | 364 | } | ||
365 | 365 | | |||
366 | if (np->type() != PACKAGE_TYPE_IDENTITY) { | 366 | if (np->type() != PACKET_TYPE_IDENTITY) { | ||
367 | qCWarning(KDECONNECT_CORE) << "LanLinkProvider/newConnection: Expected identity, received " << np->type(); | 367 | qCWarning(KDECONNECT_CORE) << "LanLinkProvider/newConnection: Expected identity, received " << np->type(); | ||
368 | delete np; | 368 | delete np; | ||
369 | return; | 369 | return; | ||
370 | } | 370 | } | ||
371 | 371 | | |||
372 | // Needed in "encrypted" if ssl is used, similar to "connected" | 372 | // Needed in "encrypted" if ssl is used, similar to "connected" | ||
373 | m_receivedIdentityPackages[socket].np = np; | 373 | m_receivedIdentityPackets[socket].np = np; | ||
374 | 374 | | |||
375 | const QString& deviceId = np->get<QString>(QStringLiteral("deviceId")); | 375 | const QString& deviceId = np->get<QString>(QStringLiteral("deviceId")); | ||
376 | //qCDebug(KDECONNECT_CORE) << "Handshaking done (i'm the new device)"; | 376 | //qCDebug(KDECONNECT_CORE) << "Handshaking done (i'm the new device)"; | ||
377 | 377 | | |||
378 | //This socket will now be owned by the LanDeviceLink or we don't want more data to be received, forget about it | 378 | //This socket will now be owned by the LanDeviceLink or we don't want more data to be received, forget about it | ||
379 | disconnect(socket, &QIODevice::readyRead, this, &LanLinkProvider::dataReceived); | 379 | disconnect(socket, &QIODevice::readyRead, this, &LanLinkProvider::dataReceived); | ||
380 | 380 | | |||
381 | if (np->get<int>(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { | 381 | if (np->get<int>(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { | ||
Show All 9 Lines | 390 | if (isDeviceTrusted) { | |||
391 | connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | 391 | connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); | ||
392 | } | 392 | } | ||
393 | 393 | | |||
394 | socket->startClientEncryption(); | 394 | socket->startClientEncryption(); | ||
395 | 395 | | |||
396 | } else { | 396 | } else { | ||
397 | qWarning() << np->get<QString>(QStringLiteral("deviceName")) << "uses an old protocol version, this won't work"; | 397 | qWarning() << np->get<QString>(QStringLiteral("deviceName")) << "uses an old protocol version, this won't work"; | ||
398 | //addLink(deviceId, socket, np, LanDeviceLink::Locally); | 398 | //addLink(deviceId, socket, np, LanDeviceLink::Locally); | ||
399 | delete m_receivedIdentityPackages.take(socket).np; | 399 | delete m_receivedIdentityPackets.take(socket).np; | ||
400 | } | 400 | } | ||
401 | } | 401 | } | ||
402 | 402 | | |||
403 | void LanLinkProvider::deviceLinkDestroyed(QObject* destroyedDeviceLink) | 403 | void LanLinkProvider::deviceLinkDestroyed(QObject* destroyedDeviceLink) | ||
404 | { | 404 | { | ||
405 | const QString id = destroyedDeviceLink->property("deviceId").toString(); | 405 | const QString id = destroyedDeviceLink->property("deviceId").toString(); | ||
406 | //qCDebug(KDECONNECT_CORE) << "deviceLinkDestroyed" << id; | 406 | //qCDebug(KDECONNECT_CORE) << "deviceLinkDestroyed" << id; | ||
407 | Q_ASSERT(m_links.key(static_cast<LanDeviceLink*>(destroyedDeviceLink)) == id); | 407 | Q_ASSERT(m_links.key(static_cast<LanDeviceLink*>(destroyedDeviceLink)) == id); | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | 457 | void LanLinkProvider::configureSocket(QSslSocket* socket) { | |||
475 | #ifdef TCP_KEEPCNT | 475 | #ifdef TCP_KEEPCNT | ||
476 | // number of missed keepalive packets before disconnecting | 476 | // number of missed keepalive packets before disconnecting | ||
477 | int count = 3; | 477 | int count = 3; | ||
478 | setsockopt(socket->socketDescriptor(), IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count)); | 478 | setsockopt(socket->socketDescriptor(), IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count)); | ||
479 | #endif | 479 | #endif | ||
480 | 480 | | |||
481 | } | 481 | } | ||
482 | 482 | | |||
483 | void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, LanDeviceLink::ConnectionStarted connectionOrigin) | 483 | void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPacket* receivedPacket, LanDeviceLink::ConnectionStarted connectionOrigin) | ||
484 | { | 484 | { | ||
485 | // Socket disconnection will now be handled by LanDeviceLink | 485 | // Socket disconnection will now be handled by LanDeviceLink | ||
486 | disconnect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater); | 486 | disconnect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater); | ||
487 | 487 | | |||
488 | LanDeviceLink* deviceLink; | 488 | LanDeviceLink* deviceLink; | ||
489 | //Do we have a link for this device already? | 489 | //Do we have a link for this device already? | ||
490 | QMap< QString, LanDeviceLink* >::iterator linkIterator = m_links.find(deviceId); | 490 | QMap< QString, LanDeviceLink* >::iterator linkIterator = m_links.find(deviceId); | ||
491 | if (linkIterator != m_links.end()) { | 491 | if (linkIterator != m_links.end()) { | ||
492 | //qCDebug(KDECONNECT_CORE) << "Reusing link to" << deviceId; | 492 | //qCDebug(KDECONNECT_CORE) << "Reusing link to" << deviceId; | ||
493 | deviceLink = linkIterator.value(); | 493 | deviceLink = linkIterator.value(); | ||
494 | deviceLink->reset(socket, connectionOrigin); | 494 | deviceLink->reset(socket, connectionOrigin); | ||
495 | } else { | 495 | } else { | ||
496 | deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin); | 496 | deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin); | ||
497 | connect(deviceLink, &QObject::destroyed, this, &LanLinkProvider::deviceLinkDestroyed); | 497 | connect(deviceLink, &QObject::destroyed, this, &LanLinkProvider::deviceLinkDestroyed); | ||
498 | m_links[deviceId] = deviceLink; | 498 | m_links[deviceId] = deviceLink; | ||
499 | if (m_pairingHandlers.contains(deviceId)) { | 499 | if (m_pairingHandlers.contains(deviceId)) { | ||
500 | //We shouldn't have a pairinghandler if we didn't have a link. | 500 | //We shouldn't have a pairinghandler if we didn't have a link. | ||
501 | //Crash if debug, recover if release (by setting the new devicelink to the old pairinghandler) | 501 | //Crash if debug, recover if release (by setting the new devicelink to the old pairinghandler) | ||
502 | Q_ASSERT(m_pairingHandlers.contains(deviceId)); | 502 | Q_ASSERT(m_pairingHandlers.contains(deviceId)); | ||
503 | m_pairingHandlers[deviceId]->setDeviceLink(deviceLink); | 503 | m_pairingHandlers[deviceId]->setDeviceLink(deviceLink); | ||
504 | } | 504 | } | ||
505 | } | 505 | } | ||
506 | Q_EMIT onConnectionReceived(*receivedPackage, deviceLink); | 506 | Q_EMIT onConnectionReceived(*receivedPacket, deviceLink); | ||
507 | } | 507 | } | ||
508 | 508 | | |||
509 | LanPairingHandler* LanLinkProvider::createPairingHandler(DeviceLink* link) | 509 | LanPairingHandler* LanLinkProvider::createPairingHandler(DeviceLink* link) | ||
510 | { | 510 | { | ||
511 | LanPairingHandler* ph = m_pairingHandlers.value(link->deviceId()); | 511 | LanPairingHandler* ph = m_pairingHandlers.value(link->deviceId()); | ||
512 | if (!ph) { | 512 | if (!ph) { | ||
513 | ph = new LanPairingHandler(link); | 513 | ph = new LanPairingHandler(link); | ||
514 | qCDebug(KDECONNECT_CORE) << "creating pairing handler for" << link->deviceId(); | 514 | qCDebug(KDECONNECT_CORE) << "creating pairing handler for" << link->deviceId(); | ||
Show All 10 Lines | |||||
525 | } | 525 | } | ||
526 | 526 | | |||
527 | void LanLinkProvider::userRequestsUnpair(const QString& deviceId) | 527 | void LanLinkProvider::userRequestsUnpair(const QString& deviceId) | ||
528 | { | 528 | { | ||
529 | LanPairingHandler* ph = createPairingHandler(m_links.value(deviceId)); | 529 | LanPairingHandler* ph = createPairingHandler(m_links.value(deviceId)); | ||
530 | ph->unpair(); | 530 | ph->unpair(); | ||
531 | } | 531 | } | ||
532 | 532 | | |||
533 | void LanLinkProvider::incomingPairPackage(DeviceLink* deviceLink, const NetworkPackage& np) | 533 | void LanLinkProvider::incomingPairPacket(DeviceLink* deviceLink, const NetworkPacket& np) | ||
534 | { | 534 | { | ||
535 | LanPairingHandler* ph = createPairingHandler(deviceLink); | 535 | LanPairingHandler* ph = createPairingHandler(deviceLink); | ||
536 | ph->packageReceived(np); | 536 | ph->packetReceived(np); | ||
537 | } | 537 | } | ||
538 | 538 | |