2013-08-07 10:29:56 +01:00
/**
* Copyright 2013 Albert Vaca < albertvaka @ gmail . com >
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License or ( at your option ) version 3 or any later version
* accepted by the membership of KDE e . V . ( or its successor approved
* by the membership of KDE e . V . ) , which shall act as a proxy
* defined in Section 14 of version 3 of the license .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
2013-08-28 22:47:39 +01:00
# include "lanlinkprovider.h"
2014-09-21 23:59:34 +01:00
# include "core_debug.h"
2013-08-07 10:29:56 +01:00
2015-09-10 14:51:38 +01:00
# ifndef Q_OS_WIN
2013-10-30 00:18:25 +00:00
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <netdb.h>
2015-09-10 14:51:38 +01:00
# endif
2013-10-30 00:18:25 +00:00
2013-08-07 10:29:56 +01:00
# include <QHostInfo>
# include <QTcpServer>
2017-03-06 21:01:46 +00:00
# include <QNetworkProxy>
2013-08-07 10:29:56 +01:00
# include <QUdpSocket>
2015-06-14 21:29:00 +01:00
# include <QNetworkSession>
# include <QNetworkConfigurationManager>
2016-07-05 13:27:53 +01:00
# include <QSslCipher>
# include <QSslConfiguration>
2013-08-07 10:29:56 +01:00
2016-07-05 13:27:53 +01:00
# include "daemon.h"
2013-09-09 17:35:56 +01:00
# include "landevicelink.h"
2015-12-01 18:45:14 +00:00
# include "lanpairinghandler.h"
2016-07-05 13:27:53 +01:00
# include "kdeconnectconfig.h"
2013-08-12 15:09:52 +01:00
2016-06-16 18:47:52 +01:00
# define MIN_VERSION_WITH_SSL_SUPPORT 6
2015-12-02 17:49:00 +00:00
LanLinkProvider : : LanLinkProvider ( bool testMode )
2017-09-03 20:39:44 +01:00
: m_testMode ( testMode )
2018-10-29 15:47:49 +00:00
, m_udpSocket ( this )
, m_combineBroadcastsTimer ( this )
2013-08-07 10:29:56 +01:00
{
2017-09-03 20:39:44 +01:00
m_tcpPort = 0 ;
2013-08-07 10:29:56 +01:00
2017-09-03 20:39:44 +01:00
m_combineBroadcastsTimer . setInterval ( 0 ) ; // increase this if waiting a single event-loop iteration is not enough
m_combineBroadcastsTimer . setSingleShot ( true ) ;
connect ( & m_combineBroadcastsTimer , & QTimer : : timeout , this , & LanLinkProvider : : broadcastToNetwork ) ;
2016-06-02 11:18:51 +01:00
2017-09-03 20:39:44 +01:00
connect ( & m_udpSocket , & QIODevice : : readyRead , this , & LanLinkProvider : : newUdpConnection ) ;
2013-08-07 10:29:56 +01:00
2017-09-03 20:39:44 +01:00
m_server = new Server ( this ) ;
m_server - > setProxy ( QNetworkProxy : : NoProxy ) ;
connect ( m_server , & QTcpServer : : newConnection , this , & LanLinkProvider : : newConnection ) ;
2013-08-07 10:29:56 +01:00
2017-09-03 20:39:44 +01:00
m_udpSocket . setProxy ( QNetworkProxy : : NoProxy ) ;
2017-03-06 21:01:46 +00:00
2015-06-14 21:29:00 +01:00
//Detect when a network interface changes status, so we announce ourelves in the new network
2015-09-08 08:05:06 +01:00
QNetworkConfigurationManager * networkManager = new QNetworkConfigurationManager ( this ) ;
connect ( networkManager , & QNetworkConfigurationManager : : configurationChanged , this , & LanLinkProvider : : onNetworkConfigurationChanged ) ;
2016-06-02 11:18:51 +01:00
2015-09-08 08:05:06 +01:00
}
2017-09-03 20:39:44 +01:00
void LanLinkProvider : : onNetworkConfigurationChanged ( const QNetworkConfiguration & config )
2015-09-08 08:05:06 +01:00
{
2015-12-06 00:19:20 +00:00
if ( m_lastConfig ! = config & & config . state ( ) = = QNetworkConfiguration : : Active ) {
2015-09-08 08:05:06 +01:00
m_lastConfig = config ;
2015-06-14 21:29:00 +01:00
onNetworkChange ( ) ;
2015-09-08 08:05:06 +01:00
}
2013-08-07 10:29:56 +01:00
}
2015-04-04 18:05:55 +01:00
LanLinkProvider : : ~ LanLinkProvider ( )
{
}
2013-08-28 22:47:39 +01:00
void LanLinkProvider : : onStart ( )
2013-08-07 10:29:56 +01:00
{
2017-09-03 20:39:44 +01:00
const QHostAddress bindAddress = m_testMode ? QHostAddress : : LocalHost : QHostAddress : : Any ;
2015-12-02 17:49:00 +00:00
2017-09-03 20:39:44 +01:00
bool success = m_udpSocket . bind ( bindAddress , UDP_PORT , QUdpSocket : : ShareAddress ) ;
2015-12-02 17:49:00 +00:00
Q_ASSERT ( success ) ;
2013-08-08 03:11:20 +01:00
2016-03-08 00:01:44 +00:00
qCDebug ( KDECONNECT_CORE ) < < " onStart " ;
2015-12-02 19:04:35 +00:00
2017-09-03 20:39:44 +01:00
m_tcpPort = MIN_TCP_PORT ;
while ( ! m_server - > listen ( bindAddress , m_tcpPort ) ) {
m_tcpPort + + ;
if ( m_tcpPort > MAX_TCP_PORT ) { //No ports available?
2017-02-14 22:03:59 +00:00
qCritical ( KDECONNECT_CORE ) < < " Error opening a port in range " < < MIN_TCP_PORT < < " - " < < MAX_TCP_PORT ;
2017-09-03 20:39:44 +01:00
m_tcpPort = 0 ;
2015-03-22 06:11:50 +00:00
return ;
}
2014-04-14 20:45:41 +01:00
}
2013-08-07 10:29:56 +01:00
2015-04-05 00:32:15 +01:00
onNetworkChange ( ) ;
2013-08-07 10:29:56 +01:00
}
2013-08-28 22:47:39 +01:00
void LanLinkProvider : : onStop ( )
2013-08-07 10:29:56 +01:00
{
2016-03-08 00:01:44 +00:00
qCDebug ( KDECONNECT_CORE ) < < " onStop " ;
2017-09-03 20:39:44 +01:00
m_udpSocket . close ( ) ;
m_server - > close ( ) ;
2013-08-07 10:29:56 +01:00
}
2015-04-05 00:32:15 +01:00
void LanLinkProvider : : onNetworkChange ( )
2013-08-08 03:11:20 +01:00
{
2017-09-03 20:39:44 +01:00
if ( m_combineBroadcastsTimer . isActive ( ) ) {
2016-06-02 11:18:51 +01:00
qCDebug ( KDECONNECT_CORE ( ) ) < < " Preventing duplicate broadcasts " ;
return ;
}
2017-09-03 20:39:44 +01:00
m_combineBroadcastsTimer . start ( ) ;
2016-06-02 11:18:51 +01:00
}
//I'm in a new network, let's be polite and introduce myself
void LanLinkProvider : : broadcastToNetwork ( )
{
2017-09-03 20:39:44 +01:00
if ( ! m_server - > isListening ( ) ) {
2015-04-05 00:32:15 +01:00
//Not started
2014-04-14 20:45:41 +01:00
return ;
}
2013-10-03 16:25:17 +01:00
2017-09-03 20:39:44 +01:00
Q_ASSERT ( m_tcpPort ! = 0 ) ;
2015-04-04 18:05:55 +01:00
2015-06-14 21:29:00 +01:00
qCDebug ( KDECONNECT_CORE ( ) ) < < " Broadcasting identity packet " ;
2016-11-20 03:24:28 +00:00
2017-09-03 20:39:44 +01:00
QHostAddress destAddress = m_testMode ? QHostAddress : : LocalHost : QHostAddress ( QStringLiteral ( " 255.255.255.255 " ) ) ;
2016-11-20 03:24:28 +00:00
2018-03-04 19:48:51 +00:00
NetworkPacket np ( QLatin1String ( " " ) ) ;
NetworkPacket : : createIdentityPacket ( & np ) ;
2017-09-03 20:39:44 +01:00
np . set ( QStringLiteral ( " tcpPort " ) , m_tcpPort ) ;
2016-11-20 03:24:28 +00:00
# ifdef Q_OS_WIN
//On Windows we need to broadcast from every local IP address to reach all networks
QUdpSocket sendSocket ;
2017-03-06 21:01:46 +00:00
sendSocket . setProxy ( QNetworkProxy : : NoProxy ) ;
2017-09-03 20:39:44 +01:00
for ( const QNetworkInterface & iface : QNetworkInterface : : allInterfaces ( ) ) {
2016-11-20 03:24:28 +00:00
if ( ( iface . flags ( ) & QNetworkInterface : : IsUp )
& & ( iface . flags ( ) & QNetworkInterface : : IsRunning )
& & ( iface . flags ( ) & QNetworkInterface : : CanBroadcast ) ) {
2017-09-03 20:39:44 +01:00
for ( const QNetworkAddressEntry & ifaceAddress : iface . addressEntries ( ) ) {
2016-11-20 03:24:28 +00:00
QHostAddress sourceAddress = ifaceAddress . ip ( ) ;
if ( sourceAddress . protocol ( ) = = QAbstractSocket : : IPv4Protocol & & sourceAddress ! = QHostAddress : : LocalHost ) {
qCDebug ( KDECONNECT_CORE ( ) ) < < " Broadcasting as " < < sourceAddress ;
2017-02-14 22:03:59 +00:00
sendSocket . bind ( sourceAddress , UDP_PORT ) ;
sendSocket . writeDatagram ( np . serialize ( ) , destAddress , UDP_PORT ) ;
2016-11-20 03:24:28 +00:00
sendSocket . close ( ) ;
}
}
}
}
# else
2017-09-03 20:39:44 +01:00
m_udpSocket . writeDatagram ( np . serialize ( ) , destAddress , UDP_PORT ) ;
2016-11-20 03:24:28 +00:00
# endif
2013-08-07 10:29:56 +01:00
}
2015-01-07 05:48:25 +00:00
//I'm the existing device, a new device is kindly introducing itself.
//I will create a TcpSocket and try to connect. This can result in either connected() or connectError().
2015-09-09 11:30:39 +01:00
void LanLinkProvider : : newUdpConnection ( ) //udpBroadcastReceived
2013-08-07 10:29:56 +01:00
{
2017-09-03 20:39:44 +01:00
while ( m_udpSocket . hasPendingDatagrams ( ) ) {
2015-07-05 14:23:53 +01:00
2013-08-07 10:29:56 +01:00
QByteArray datagram ;
2017-09-03 20:39:44 +01:00
datagram . resize ( m_udpSocket . pendingDatagramSize ( ) ) ;
2013-08-07 10:29:56 +01:00
QHostAddress sender ;
2017-09-03 20:39:44 +01:00
m_udpSocket . readDatagram ( datagram . data ( ) , datagram . size ( ) , & sender ) ;
2013-08-07 10:29:56 +01:00
2017-09-03 20:39:44 +01:00
if ( sender . isLoopback ( ) & & ! m_testMode )
2015-12-06 00:19:55 +00:00
continue ;
2018-03-04 19:48:51 +00:00
NetworkPacket * receivedPacket = new NetworkPacket ( QLatin1String ( " " ) ) ;
bool success = NetworkPacket : : unserialize ( datagram , receivedPacket ) ;
2013-08-07 10:29:56 +01:00
2018-03-04 19:48:51 +00:00
//qCDebug(KDECONNECT_CORE) << "udp connection from " << receivedPacket->;
2015-12-02 19:04:35 +00:00
2016-05-31 19:09:52 +01:00
//qCDebug(KDECONNECT_CORE) << "Datagram " << datagram.data() ;
2015-07-05 14:23:53 +01:00
2018-03-04 19:48:51 +00:00
if ( ! success | | receivedPacket - > type ( ) ! = PACKET_TYPE_IDENTITY ) {
delete receivedPacket ;
2015-05-05 00:04:50 +01:00
continue ;
2014-04-14 20:45:41 +01:00
}
2013-08-07 12:40:39 +01:00
2018-03-04 19:48:51 +00:00
if ( receivedPacket - > get < QString > ( QStringLiteral ( " deviceId " ) ) = = KdeConnectConfig : : instance ( ) - > deviceId ( ) ) {
2015-03-22 06:12:04 +00:00
//qCDebug(KDECONNECT_CORE) << "Ignoring my own broadcast";
2018-03-04 19:48:51 +00:00
delete receivedPacket ;
2015-05-05 00:04:50 +01:00
continue ;
2014-04-14 20:45:41 +01:00
}
2013-08-16 04:35:00 +01:00
2018-03-04 19:48:51 +00:00
int tcpPort = receivedPacket - > get < int > ( QStringLiteral ( " tcpPort " ) ) ;
2013-08-08 03:11:20 +01:00
2018-03-04 19:48:51 +00:00
//qCDebug(KDECONNECT_CORE) << "Received Udp identity packet from" << sender << " asking for a tcp connection on port " << tcpPort;
2013-08-08 03:11:20 +01:00
2015-07-05 14:23:53 +01:00
QSslSocket * socket = new QSslSocket ( this ) ;
2017-03-06 21:01:46 +00:00
socket - > setProxy ( QNetworkProxy : : NoProxy ) ;
2018-03-04 19:48:51 +00:00
m_receivedIdentityPackets [ socket ] . np = receivedPacket ;
m_receivedIdentityPackets [ socket ] . sender = sender ;
2016-11-26 14:12:38 +00:00
connect ( socket , & QAbstractSocket : : connected , this , & LanLinkProvider : : connected ) ;
2014-04-14 20:45:41 +01:00
connect ( socket , SIGNAL ( error ( QAbstractSocket : : SocketError ) ) , this , SLOT ( connectError ( ) ) ) ;
socket - > connectToHost ( sender , tcpPort ) ;
2013-08-07 12:40:39 +01:00
}
}
2013-08-07 10:29:56 +01:00
2013-08-28 22:47:39 +01:00
void LanLinkProvider : : connectError ( )
2013-08-07 12:40:39 +01:00
{
2015-07-05 14:23:53 +01:00
QSslSocket * socket = qobject_cast < QSslSocket * > ( sender ( ) ) ;
2015-01-07 05:48:25 +00:00
if ( ! socket ) return ;
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QAbstractSocket : : connected , this , & LanLinkProvider : : connected ) ;
2015-01-07 05:48:25 +00:00
disconnect ( socket , SIGNAL ( error ( QAbstractSocket : : SocketError ) ) , this , SLOT ( connectError ( ) ) ) ;
2013-08-07 10:29:56 +01:00
2015-09-09 19:12:16 +01:00
qCDebug ( KDECONNECT_CORE ) < < " Fallback (1), try reverse connection (send udp packet) " < < socket - > errorString ( ) ;
2018-03-04 19:48:51 +00:00
NetworkPacket np ( QLatin1String ( " " ) ) ;
NetworkPacket : : createIdentityPacket ( & np ) ;
2017-09-03 20:39:44 +01:00
np . set ( QStringLiteral ( " tcpPort " ) , m_tcpPort ) ;
2018-03-04 19:48:51 +00:00
m_udpSocket . writeDatagram ( np . serialize ( ) , m_receivedIdentityPackets [ socket ] . sender , UDP_PORT ) ;
2013-08-07 12:40:39 +01:00
2015-01-07 05:48:25 +00:00
//The socket we created didn't work, and we didn't manage
//to create a LanDeviceLink from it, deleting everything.
2018-03-04 19:48:51 +00:00
delete m_receivedIdentityPackets . take ( socket ) . np ;
2015-01-07 05:48:25 +00:00
delete socket ;
2013-08-07 12:40:39 +01:00
}
2018-07-27 14:00:23 +01:00
//We received a UDP packet and answered by connecting to them by TCP. This gets called on a successful connection.
2013-08-28 22:47:39 +01:00
void LanLinkProvider : : connected ( )
2013-08-07 12:40:39 +01:00
{
2015-07-05 14:23:53 +01:00
QSslSocket * socket = qobject_cast < QSslSocket * > ( sender ( ) ) ;
2017-07-07 16:26:55 +01:00
2014-09-17 15:22:23 +01:00
if ( ! socket ) return ;
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QAbstractSocket : : connected , this , & LanLinkProvider : : connected ) ;
2013-08-07 12:40:39 +01:00
disconnect ( socket , SIGNAL ( error ( QAbstractSocket : : SocketError ) ) , this , SLOT ( connectError ( ) ) ) ;
2013-10-30 00:18:25 +00:00
configureSocket ( socket ) ;
2018-03-03 19:29:18 +00:00
// If socket disconnects due to any reason after connection, link on ssl failure
2016-11-26 14:12:38 +00:00
connect ( socket , & QAbstractSocket : : disconnected , socket , & QObject : : deleteLater ) ;
2015-07-07 19:52:10 +01:00
2018-03-04 19:48:51 +00:00
NetworkPacket * receivedPacket = m_receivedIdentityPackets [ socket ] . np ;
const QString & deviceId = receivedPacket - > get < QString > ( QStringLiteral ( " deviceId " ) ) ;
2014-09-21 22:54:27 +01:00
//qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable();
2013-08-07 12:40:39 +01:00
2015-07-05 14:23:53 +01:00
// If network is on ssl, do not believe when they are connected, believe when handshake is completed
2018-03-04 19:48:51 +00:00
NetworkPacket np2 ( QLatin1String ( " " ) ) ;
NetworkPacket : : createIdentityPacket ( & np2 ) ;
2015-07-05 14:23:53 +01:00
socket - > write ( np2 . serialize ( ) ) ;
bool success = socket - > waitForBytesWritten ( ) ;
2013-08-07 12:40:39 +01:00
if ( success ) {
2013-08-16 05:22:37 +01:00
2016-06-17 00:59:53 +01:00
qCDebug ( KDECONNECT_CORE ) < < " TCP connection done (i'm the existing device) " ;
2015-07-05 14:23:53 +01:00
// if ssl supported
2018-03-04 19:48:51 +00:00
if ( receivedPacket - > get < int > ( QStringLiteral ( " protocolVersion " ) ) > = MIN_VERSION_WITH_SSL_SUPPORT ) {
2016-06-22 13:25:12 +01:00
bool isDeviceTrusted = KdeConnectConfig : : instance ( ) - > trustedDevices ( ) . contains ( deviceId ) ;
configureSslSocket ( socket , deviceId , isDeviceTrusted ) ;
2016-03-08 15:29:34 +00:00
qCDebug ( KDECONNECT_CORE ) < < " Starting server ssl (I'm the client TCP socket) " ;
2015-07-05 14:23:53 +01:00
2016-11-26 14:12:38 +00:00
connect ( socket , & QSslSocket : : encrypted , this , & LanLinkProvider : : encrypted ) ;
2016-07-04 18:52:28 +01:00
if ( isDeviceTrusted ) {
connect ( socket , SIGNAL ( sslErrors ( QList < QSslError > ) ) , this , SLOT ( sslErrors ( QList < QSslError > ) ) ) ;
}
2015-07-05 14:23:53 +01:00
socket - > startServerEncryption ( ) ;
2016-06-22 13:25:12 +01:00
2018-03-04 19:48:51 +00:00
return ; // Return statement prevents from deleting received packet, needed in slot "encrypted"
2015-07-05 14:23:53 +01:00
} else {
2018-03-04 19:48:51 +00:00
qWarning ( ) < < receivedPacket - > get < QString > ( QStringLiteral ( " deviceName " ) ) < < " uses an old protocol version, this won't work " ;
//addLink(deviceId, socket, receivedPacket, LanDeviceLink::Remotely);
2013-08-16 05:22:37 +01:00
}
2013-08-07 12:40:39 +01:00
} else {
2015-01-07 05:48:25 +00:00
//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.
2015-04-05 00:32:15 +01:00
qCDebug ( KDECONNECT_CORE ) < < " Fallback (2), try reverse connection (send udp packet) " ;
2018-03-04 19:48:51 +00:00
m_udpSocket . writeDatagram ( np2 . serialize ( ) , m_receivedIdentityPackets [ socket ] . sender , UDP_PORT ) ;
2013-08-07 10:29:56 +01:00
}
2018-03-04 19:48:51 +00:00
delete m_receivedIdentityPackets . take ( socket ) . np ;
2015-01-07 05:48:25 +00:00
//We don't delete the socket because now it's owned by the LanDeviceLink
2013-08-07 10:29:56 +01:00
}
2015-07-14 13:04:04 +01:00
void LanLinkProvider : : encrypted ( )
{
2018-06-02 19:20:15 +01:00
qCDebug ( KDECONNECT_CORE ) < < " Socket successfully established an SSL connection " ;
2015-07-13 15:25:22 +01:00
2015-07-05 14:23:53 +01:00
QSslSocket * socket = qobject_cast < QSslSocket * > ( sender ( ) ) ;
2015-07-07 19:52:10 +01:00
if ( ! socket ) return ;
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QSslSocket : : encrypted , this , & LanLinkProvider : : encrypted ) ;
2015-07-05 14:23:53 +01:00
disconnect ( socket , SIGNAL ( sslErrors ( QList < QSslError > ) ) , this , SLOT ( sslErrors ( QList < QSslError > ) ) ) ;
2016-07-05 11:42:39 +01:00
Q_ASSERT ( socket - > mode ( ) ! = QSslSocket : : UnencryptedMode ) ;
LanDeviceLink : : ConnectionStarted connectionOrigin = ( socket - > mode ( ) = = QSslSocket : : SslClientMode ) ? LanDeviceLink : : Locally : LanDeviceLink : : Remotely ;
2018-03-04 19:48:51 +00:00
NetworkPacket * receivedPacket = m_receivedIdentityPackets [ socket ] . np ;
const QString & deviceId = receivedPacket - > get < QString > ( QStringLiteral ( " deviceId " ) ) ;
2015-07-05 14:23:53 +01:00
2018-03-04 19:48:51 +00:00
addLink ( deviceId , socket , receivedPacket , connectionOrigin ) ;
2015-07-05 14:23:53 +01:00
2018-03-04 19:48:51 +00:00
// Copied from connected slot, now delete received packet
delete m_receivedIdentityPackets . take ( socket ) . np ;
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
void LanLinkProvider : : sslErrors ( const QList < QSslError > & errors )
2015-07-05 14:23:53 +01:00
{
QSslSocket * socket = qobject_cast < QSslSocket * > ( sender ( ) ) ;
2015-07-07 19:52:10 +01:00
if ( ! socket ) return ;
2016-07-04 18:52:28 +01:00
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QSslSocket : : encrypted , this , & LanLinkProvider : : encrypted ) ;
2015-07-05 14:23:53 +01:00
disconnect ( socket , SIGNAL ( sslErrors ( QList < QSslError > ) ) , this , SLOT ( sslErrors ( QList < QSslError > ) ) ) ;
2017-07-13 23:23:37 +01:00
qCDebug ( KDECONNECT_CORE ) < < " Failing due to " < < errors ;
2017-09-03 20:39:44 +01:00
Device * device = Daemon : : instance ( ) - > getDevice ( socket - > peerVerifyName ( ) ) ;
2017-07-13 23:23:37 +01:00
if ( device ) {
device - > unpair ( ) ;
2015-07-07 19:52:10 +01:00
}
2018-03-04 19:48:51 +00:00
delete m_receivedIdentityPackets . take ( socket ) . np ;
2015-07-07 19:52:10 +01:00
// Socket disconnects itself on ssl error and will be deleted by deleteLater slot, no need to delete manually
2015-07-05 14:23:53 +01:00
}
2018-03-04 19:48:51 +00:00
//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.
2015-07-09 23:27:27 +01:00
void LanLinkProvider : : newConnection ( )
2013-08-07 10:29:56 +01:00
{
2018-10-23 08:30:48 +01:00
qCDebug ( KDECONNECT_CORE ) < < " LanLinkProvider newConnection " ;
2013-08-07 10:29:56 +01:00
2017-09-03 20:39:44 +01:00
while ( m_server - > hasPendingConnections ( ) ) {
QSslSocket * socket = m_server - > nextPendingConnection ( ) ;
2015-07-09 23:27:27 +01:00
configureSocket ( socket ) ;
//This socket is still managed by us (and child of the QTcpServer), if
//it disconnects before we manage to pass it to a LanDeviceLink, it's
2015-08-21 17:38:54 +01:00
//our responsibility to delete it. We do so with this connection.
2016-11-26 14:12:38 +00:00
connect ( socket , & QAbstractSocket : : disconnected ,
socket , & QObject : : deleteLater ) ;
connect ( socket , & QIODevice : : readyRead ,
this , & LanLinkProvider : : dataReceived ) ;
2013-08-07 10:29:56 +01:00
2015-07-09 23:27:27 +01:00
}
2013-08-07 10:29:56 +01:00
}
2018-03-04 19:48:51 +00:00
//I'm the new device and this is the answer to my UDP identity packet (data received)
2013-08-28 22:47:39 +01:00
void LanLinkProvider : : dataReceived ( )
2013-08-07 10:29:56 +01:00
{
2015-07-05 14:23:53 +01:00
QSslSocket * socket = qobject_cast < QSslSocket * > ( sender ( ) ) ;
2013-08-07 10:29:56 +01:00
2014-04-14 20:45:41 +01:00
const QByteArray data = socket - > readLine ( ) ;
2013-08-07 10:29:56 +01:00
2018-10-23 08:30:48 +01:00
qCDebug ( KDECONNECT_CORE ) < < " LanLinkProvider received reply: " < < data ;
2013-08-07 10:29:56 +01:00
2018-03-04 19:48:51 +00:00
NetworkPacket * np = new NetworkPacket ( QLatin1String ( " " ) ) ;
bool success = NetworkPacket : : unserialize ( data , np ) ;
2015-07-13 15:25:22 +01:00
2016-06-02 11:17:07 +01:00
if ( ! success ) {
delete np ;
return ;
}
2018-03-04 19:48:51 +00:00
if ( np - > type ( ) ! = PACKET_TYPE_IDENTITY ) {
2016-06-02 11:17:07 +01:00
qCWarning ( KDECONNECT_CORE ) < < " LanLinkProvider/newConnection: Expected identity, received " < < np - > type ( ) ;
delete np ;
2014-04-14 20:45:41 +01:00
return ;
}
2013-08-07 10:29:56 +01:00
2015-07-14 13:04:04 +01:00
// Needed in "encrypted" if ssl is used, similar to "connected"
2018-03-04 19:48:51 +00:00
m_receivedIdentityPackets [ socket ] . np = np ;
2015-07-14 13:04:04 +01:00
2016-11-26 14:38:08 +00:00
const QString & deviceId = np - > get < QString > ( QStringLiteral ( " deviceId " ) ) ;
2014-09-21 22:54:27 +01:00
//qCDebug(KDECONNECT_CORE) << "Handshaking done (i'm the new device)";
2013-08-16 05:22:37 +01:00
2015-07-07 19:52:10 +01:00
//This socket will now be owned by the LanDeviceLink or we don't want more data to be received, forget about it
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QIODevice : : readyRead , this , & LanLinkProvider : : dataReceived ) ;
2013-08-16 05:22:37 +01:00
2016-11-26 14:38:08 +00:00
if ( np - > get < int > ( QStringLiteral ( " protocolVersion " ) ) > = MIN_VERSION_WITH_SSL_SUPPORT ) {
2013-08-07 10:29:56 +01:00
2015-07-05 14:23:53 +01:00
bool isDeviceTrusted = KdeConnectConfig : : instance ( ) - > trustedDevices ( ) . contains ( deviceId ) ;
2016-06-22 13:25:12 +01:00
configureSslSocket ( socket , deviceId , isDeviceTrusted ) ;
2015-07-05 14:23:53 +01:00
2016-03-08 15:29:34 +00:00
qCDebug ( KDECONNECT_CORE ) < < " Starting client ssl (but I'm the server TCP socket) " ;
2013-08-07 10:29:56 +01:00
2016-11-26 14:12:38 +00:00
connect ( socket , & QSslSocket : : encrypted , this , & LanLinkProvider : : encrypted ) ;
2016-06-22 13:25:12 +01:00
2016-07-04 18:52:28 +01:00
if ( isDeviceTrusted ) {
connect ( socket , SIGNAL ( sslErrors ( QList < QSslError > ) ) , this , SLOT ( sslErrors ( QList < QSslError > ) ) ) ;
}
socket - > startClientEncryption ( ) ;
2016-06-22 13:25:12 +01:00
2015-07-05 14:23:53 +01:00
} else {
2016-11-26 14:38:08 +00:00
qWarning ( ) < < np - > get < QString > ( QStringLiteral ( " deviceName " ) ) < < " uses an old protocol version, this won't work " ;
2016-06-15 19:38:57 +01:00
//addLink(deviceId, socket, np, LanDeviceLink::Locally);
2018-03-04 19:48:51 +00:00
delete m_receivedIdentityPackets . take ( socket ) . np ;
2015-07-05 14:23:53 +01:00
}
2013-08-07 10:29:56 +01:00
}
2014-04-15 19:14:22 +01:00
void LanLinkProvider : : deviceLinkDestroyed ( QObject * destroyedDeviceLink )
2013-08-07 10:29:56 +01:00
{
2014-04-15 19:14:22 +01:00
const QString id = destroyedDeviceLink - > property ( " deviceId " ) . toString ( ) ;
2016-06-17 00:59:53 +01:00
//qCDebug(KDECONNECT_CORE) << "deviceLinkDestroyed" << id;
2017-09-03 20:39:44 +01:00
QMap < QString , LanDeviceLink * > : : iterator linkIterator = m_links . find ( id ) ;
2018-03-24 18:00:43 +00:00
Q_ASSERT ( linkIterator ! = m_links . end ( ) ) ;
2017-09-03 20:39:44 +01:00
if ( linkIterator ! = m_links . end ( ) ) {
2015-12-11 01:11:55 +00:00
Q_ASSERT ( linkIterator . value ( ) = = destroyedDeviceLink ) ;
2017-09-03 20:39:44 +01:00
m_links . erase ( linkIterator ) ;
2018-03-24 18:00:43 +00:00
auto pairingHandler = m_pairingHandlers . take ( id ) ;
if ( pairingHandler ) {
pairingHandler - > deleteLater ( ) ;
}
2013-08-16 05:22:37 +01:00
}
2016-08-03 21:01:04 +01:00
2013-08-07 10:29:56 +01:00
}
2016-06-22 13:25:12 +01:00
void LanLinkProvider : : configureSslSocket ( QSslSocket * socket , const QString & deviceId , bool isDeviceTrusted )
2013-08-07 10:29:56 +01:00
{
2016-04-30 16:09:34 +01:00
// Setting supported ciphers manually
2018-10-07 19:23:20 +01:00
// Top 3 ciphers are for new Android devices, bottom two are for old Android devices
2016-04-30 16:09:34 +01:00
// FIXME : These cipher suites should be checked whether they are supported or not on device
QList < QSslCipher > socketCiphers ;
2016-11-26 14:38:08 +00:00
socketCiphers . append ( QSslCipher ( QStringLiteral ( " ECDHE-ECDSA-AES256-GCM-SHA384 " ) ) ) ;
socketCiphers . append ( QSslCipher ( QStringLiteral ( " ECDHE-ECDSA-AES128-GCM-SHA256 " ) ) ) ;
socketCiphers . append ( QSslCipher ( QStringLiteral ( " ECDHE-RSA-AES128-SHA " ) ) ) ;
socketCiphers . append ( QSslCipher ( QStringLiteral ( " RC4-SHA " ) ) ) ;
socketCiphers . append ( QSslCipher ( QStringLiteral ( " RC4-MD5 " ) ) ) ;
2017-12-21 11:28:30 +00:00
socketCiphers . append ( QSslCipher ( QStringLiteral ( " DHE-RSA-AES256-SHA " ) ) ) ;
2016-04-30 16:09:34 +01:00
2015-07-05 14:23:53 +01:00
// Configure for ssl
2016-04-30 16:09:34 +01:00
QSslConfiguration sslConfig ;
sslConfig . setCiphers ( socketCiphers ) ;
sslConfig . setProtocol ( QSsl : : TlsV1_0 ) ;
socket - > setSslConfiguration ( sslConfig ) ;
2015-07-05 14:23:53 +01:00
socket - > setLocalCertificate ( KdeConnectConfig : : instance ( ) - > certificate ( ) ) ;
socket - > setPrivateKey ( KdeConnectConfig : : instance ( ) - > privateKeyPath ( ) ) ;
2016-06-22 13:25:12 +01:00
socket - > setPeerVerifyName ( deviceId ) ;
if ( isDeviceTrusted ) {
2016-11-26 14:38:08 +00:00
QString certString = KdeConnectConfig : : instance ( ) - > getDeviceProperty ( deviceId , QStringLiteral ( " certificate " ) , QString ( ) ) ;
2016-06-22 13:25:12 +01:00
socket - > addCaCertificate ( QSslCertificate ( certString . toLatin1 ( ) ) ) ;
socket - > setPeerVerifyMode ( QSslSocket : : VerifyPeer ) ;
} else {
socket - > setPeerVerifyMode ( QSslSocket : : QueryPeer ) ;
}
2016-07-12 11:49:21 +01:00
//Usually SSL errors are only bad for trusted devices. Uncomment this section to log errors in any case, for debugging.
//QObject::connect(socket, static_cast<void (QSslSocket::*)(const QList<QSslError>&)>(&QSslSocket::sslErrors), [](const QList<QSslError>& errors)
//{
2017-09-03 20:39:44 +01:00
// Q_FOREACH (const QSslError& error, errors) {
2016-07-12 11:49:21 +01:00
// qCDebug(KDECONNECT_CORE) << "SSL Error:" << error.errorString();
// }
//});
2016-06-22 13:25:12 +01:00
}
void LanLinkProvider : : configureSocket ( QSslSocket * socket ) {
2015-07-05 14:23:53 +01:00
2017-03-06 21:01:46 +00:00
socket - > setProxy ( QNetworkProxy : : NoProxy ) ;
2016-06-09 01:36:02 +01:00
socket - > setSocketOption ( QAbstractSocket : : KeepAliveOption , QVariant ( 1 ) ) ;
2016-06-22 13:25:12 +01:00
2015-04-04 18:05:55 +01:00
# ifdef TCP_KEEPIDLE
// time to start sending keepalive packets (seconds)
int maxIdle = 10 ;
2016-06-09 01:36:02 +01:00
setsockopt ( socket - > socketDescriptor ( ) , IPPROTO_TCP , TCP_KEEPIDLE , & maxIdle , sizeof ( maxIdle ) ) ;
2015-04-04 18:05:55 +01:00
# endif
# ifdef TCP_KEEPINTVL
// interval between keepalive packets after the initial period (seconds)
int interval = 5 ;
2016-06-09 01:36:02 +01:00
setsockopt ( socket - > socketDescriptor ( ) , IPPROTO_TCP , TCP_KEEPINTVL , & interval , sizeof ( interval ) ) ;
2015-04-04 18:05:55 +01:00
# endif
# ifdef TCP_KEEPCNT
// number of missed keepalive packets before disconnecting
int count = 3 ;
2016-06-09 01:36:02 +01:00
setsockopt ( socket - > socketDescriptor ( ) , IPPROTO_TCP , TCP_KEEPCNT , & count , sizeof ( count ) ) ;
2015-04-04 18:05:55 +01:00
# endif
2013-08-07 10:29:56 +01:00
}
2015-07-05 14:23:53 +01:00
2018-03-04 19:48:51 +00:00
void LanLinkProvider : : addLink ( const QString & deviceId , QSslSocket * socket , NetworkPacket * receivedPacket , LanDeviceLink : : ConnectionStarted connectionOrigin )
2015-11-30 18:36:01 +00:00
{
2015-07-07 19:52:10 +01:00
// Socket disconnection will now be handled by LanDeviceLink
2016-11-26 14:12:38 +00:00
disconnect ( socket , & QAbstractSocket : : disconnected , socket , & QObject : : deleteLater ) ;
2015-07-07 19:52:10 +01:00
2015-12-11 01:11:55 +00:00
LanDeviceLink * deviceLink ;
2015-12-11 01:11:48 +00:00
//Do we have a link for this device already?
2017-09-03 20:39:44 +01:00
QMap < QString , LanDeviceLink * > : : iterator linkIterator = m_links . find ( deviceId ) ;
if ( linkIterator ! = m_links . end ( ) ) {
2016-06-17 00:59:05 +01:00
//qCDebug(KDECONNECT_CORE) << "Reusing link to" << deviceId;
2015-12-11 01:11:48 +00:00
deviceLink = linkIterator . value ( ) ;
deviceLink - > reset ( socket , connectionOrigin ) ;
} else {
deviceLink = new LanDeviceLink ( deviceId , this , socket , connectionOrigin ) ;
2016-11-26 14:12:38 +00:00
connect ( deviceLink , & QObject : : destroyed , this , & LanLinkProvider : : deviceLinkDestroyed ) ;
2017-09-03 20:39:44 +01:00
m_links [ deviceId ] = deviceLink ;
if ( m_pairingHandlers . contains ( deviceId ) ) {
2015-12-17 13:54:24 +00:00
//We shouldn't have a pairinghandler if we didn't have a link.
//Crash if debug, recover if release (by setting the new devicelink to the old pairinghandler)
2017-09-03 20:39:44 +01:00
Q_ASSERT ( m_pairingHandlers . contains ( deviceId ) ) ;
m_pairingHandlers [ deviceId ] - > setDeviceLink ( deviceLink ) ;
2015-12-17 13:54:24 +00:00
}
2015-07-05 14:23:53 +01:00
}
2018-03-04 19:48:51 +00:00
Q_EMIT onConnectionReceived ( * receivedPacket , deviceLink ) ;
2015-09-11 16:25:23 +01:00
}
2015-12-01 18:45:14 +00:00
2015-12-17 14:53:29 +00:00
LanPairingHandler * LanLinkProvider : : createPairingHandler ( DeviceLink * link )
2015-12-01 18:45:14 +00:00
{
2017-09-03 20:39:44 +01:00
LanPairingHandler * ph = m_pairingHandlers . value ( link - > deviceId ( ) ) ;
2015-12-01 18:45:14 +00:00
if ( ! ph ) {
2015-12-17 13:54:24 +00:00
ph = new LanPairingHandler ( link ) ;
2016-03-08 00:01:44 +00:00
qCDebug ( KDECONNECT_CORE ) < < " creating pairing handler for " < < link - > deviceId ( ) ;
2015-12-17 14:53:29 +00:00
connect ( ph , & LanPairingHandler : : pairingError , link , & DeviceLink : : pairingError ) ;
2017-09-03 20:39:44 +01:00
m_pairingHandlers [ link - > deviceId ( ) ] = ph ;
2015-12-01 18:45:14 +00:00
}
2015-12-17 14:53:29 +00:00
return ph ;
}
2015-12-01 18:45:14 +00:00
2015-12-17 14:53:29 +00:00
void LanLinkProvider : : userRequestsPair ( const QString & deviceId )
{
2017-09-03 20:39:44 +01:00
LanPairingHandler * ph = createPairingHandler ( m_links . value ( deviceId ) ) ;
2015-12-01 18:45:14 +00:00
ph - > requestPairing ( ) ;
}
2015-12-02 19:04:35 +00:00
2016-03-03 00:17:38 +00:00
void LanLinkProvider : : userRequestsUnpair ( const QString & deviceId )
{
2017-09-03 20:39:44 +01:00
LanPairingHandler * ph = createPairingHandler ( m_links . value ( deviceId ) ) ;
2016-03-03 00:17:38 +00:00
ph - > unpair ( ) ;
}
2018-03-04 19:48:51 +00:00
void LanLinkProvider : : incomingPairPacket ( DeviceLink * deviceLink , const NetworkPacket & np )
2015-12-06 01:12:29 +00:00
{
2015-12-17 14:53:29 +00:00
LanPairingHandler * ph = createPairingHandler ( deviceLink ) ;
2018-03-04 19:48:51 +00:00
ph - > packetReceived ( np ) ;
2015-12-06 01:12:29 +00:00
}
2015-12-17 13:54:24 +00:00