2013-07-23 15:11:54 +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 "landevicelink.h"
|
2014-09-21 23:59:34 +01:00
|
|
|
#include "core_debug.h"
|
2013-08-12 15:09:52 +01:00
|
|
|
|
2013-09-09 17:35:56 +01:00
|
|
|
#include "../linkprovider.h"
|
2013-09-14 16:30:12 +01:00
|
|
|
#include "uploadjob.h"
|
|
|
|
#include "downloadjob.h"
|
2013-10-30 00:18:25 +00:00
|
|
|
#include "socketlinereader.h"
|
2015-07-25 12:45:19 +01:00
|
|
|
#include "lanpairinghandler.h"
|
2013-07-23 15:11:54 +01:00
|
|
|
|
2015-09-11 16:25:23 +01:00
|
|
|
LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource)
|
2015-09-09 11:30:39 +01:00
|
|
|
: DeviceLink(deviceId, parent, connectionSource)
|
2015-01-07 05:48:25 +00:00
|
|
|
, mSocketLineReader(new SocketLineReader(socket))
|
2015-07-05 14:23:53 +01:00
|
|
|
, onSsl(false)
|
2013-07-23 15:11:54 +01:00
|
|
|
{
|
2013-10-30 00:18:25 +00:00
|
|
|
connect(mSocketLineReader, SIGNAL(readyRead()),
|
2013-08-12 15:09:52 +01:00
|
|
|
this, SLOT(dataReceived()));
|
2015-01-07 05:48:25 +00:00
|
|
|
|
|
|
|
//We take ownership of the socket.
|
|
|
|
//When the link provider distroys us,
|
|
|
|
//the socket (and the reader) will be
|
|
|
|
//destroyed as well
|
|
|
|
connect(socket, SIGNAL(disconnected()),
|
|
|
|
this, SLOT(deleteLater()));
|
|
|
|
mSocketLineReader->setParent(this);
|
|
|
|
socket->setParent(this);
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
QString LanDeviceLink::name()
|
|
|
|
{
|
2015-08-11 04:34:02 +01:00
|
|
|
return "LanLink"; // Should be same in both android and kde version
|
2015-07-25 12:45:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanDeviceLink::setOnSsl(bool value)
|
|
|
|
{
|
2015-07-05 14:23:53 +01:00
|
|
|
onSsl = value;
|
|
|
|
}
|
|
|
|
|
2015-07-25 12:45:19 +01:00
|
|
|
PairingHandler* LanDeviceLink::createPairingHandler(Device* device)
|
|
|
|
{
|
2015-11-30 18:36:01 +00:00
|
|
|
return new LanPairingHandler(device->id());
|
2015-07-25 12:45:19 +01:00
|
|
|
}
|
2015-01-07 05:48:25 +00:00
|
|
|
|
2013-09-13 22:27:16 +01:00
|
|
|
bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np)
|
2013-07-24 17:42:33 +01:00
|
|
|
{
|
2013-09-14 16:30:12 +01:00
|
|
|
if (np.hasPayload()) {
|
2015-07-14 13:04:04 +01:00
|
|
|
np.setPayloadTransferInfo(sendPayload(np)->transferInfo());
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
2015-07-05 14:23:53 +01:00
|
|
|
if (!onSsl) {
|
|
|
|
np.encrypt(key);
|
|
|
|
}
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2013-10-30 00:18:25 +00:00
|
|
|
int written = mSocketLineReader->write(np.serialize());
|
2013-10-11 14:19:23 +01:00
|
|
|
|
|
|
|
//TODO: Actually detect if a package is received or not, now we keep TCP
|
|
|
|
//"ESTABLISHED" connections that look legit (return true when we use them),
|
2015-01-07 05:48:25 +00:00
|
|
|
//but that are actually broken (until keepalive detects that they are down).
|
2013-09-13 22:27:16 +01:00
|
|
|
return (written != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LanDeviceLink::sendPackage(NetworkPackage& np)
|
|
|
|
{
|
2013-09-14 16:30:12 +01:00
|
|
|
if (np.hasPayload()) {
|
2015-07-14 13:04:04 +01:00
|
|
|
np.setPayloadTransferInfo(sendPayload(np)->transferInfo());
|
2013-09-14 16:30:12 +01:00
|
|
|
}
|
|
|
|
|
2013-10-30 00:18:25 +00:00
|
|
|
int written = mSocketLineReader->write(np.serialize());
|
2013-09-13 22:27:16 +01:00
|
|
|
return (written != -1);
|
2013-07-24 17:42:33 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 19:52:10 +01:00
|
|
|
UploadJob* LanDeviceLink::sendPayload(NetworkPackage& np)
|
|
|
|
{
|
|
|
|
QVariantMap transferInfo;
|
|
|
|
if (onSsl) {
|
|
|
|
transferInfo.insert("useSsl", true);
|
|
|
|
transferInfo.insert("deviceId", deviceId());
|
|
|
|
}
|
2015-11-30 18:36:01 +00:00
|
|
|
UploadJob* job = new UploadJob(np.payload(), deviceId());
|
2015-07-07 19:52:10 +01:00
|
|
|
job->start();
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2013-08-28 22:47:39 +01:00
|
|
|
void LanDeviceLink::dataReceived()
|
2013-07-23 15:11:54 +01:00
|
|
|
{
|
2013-09-26 23:01:59 +01:00
|
|
|
|
2013-11-06 17:35:40 +00:00
|
|
|
if (mSocketLineReader->bytesAvailable() == 0) return;
|
|
|
|
|
2014-03-07 01:13:42 +00:00
|
|
|
const QByteArray package = mSocketLineReader->readLine();
|
2013-09-13 22:27:16 +01:00
|
|
|
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "LanDeviceLink dataReceived" << package;
|
2013-09-14 16:30:12 +01:00
|
|
|
|
2013-10-05 16:20:13 +01:00
|
|
|
NetworkPackage unserialized(QString::null);
|
|
|
|
NetworkPackage::unserialize(package, &unserialized);
|
|
|
|
if (unserialized.isEncrypted()) {
|
|
|
|
//mPrivateKey should always be set when device link is added to device, no null-checking done here
|
2015-07-14 13:04:04 +01:00
|
|
|
// TODO : Check this with old device since package through ssl in unencrypted
|
2015-07-05 22:49:58 +01:00
|
|
|
unserialized.decrypt(mPrivateKey, &unserialized);
|
|
|
|
}
|
2013-09-18 17:35:50 +01:00
|
|
|
|
2015-07-05 22:49:58 +01:00
|
|
|
if (unserialized.hasPayloadTransferInfo()) {
|
2015-07-14 13:04:04 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "HasPayloadTransferInfo";
|
2015-07-07 19:52:10 +01:00
|
|
|
QVariantMap transferInfo = unserialized.payloadTransferInfo();
|
2015-07-05 22:49:58 +01:00
|
|
|
if (onSsl) {
|
2015-07-07 19:52:10 +01:00
|
|
|
transferInfo.insert("useSsl", true);
|
|
|
|
transferInfo.insert("deviceId", deviceId());
|
2013-09-13 22:27:16 +01:00
|
|
|
}
|
2015-07-07 19:52:10 +01:00
|
|
|
DownloadJob* job = new DownloadJob(mSocketLineReader->peerAddress(), transferInfo);
|
2015-07-05 22:49:58 +01:00
|
|
|
job->start();
|
|
|
|
unserialized.setPayload(job->getPayload(), unserialized.payloadSize());
|
2013-10-05 16:20:13 +01:00
|
|
|
}
|
2013-09-26 23:01:59 +01:00
|
|
|
|
2015-07-05 22:49:58 +01:00
|
|
|
Q_EMIT receivedPackage(unserialized);
|
|
|
|
|
2013-10-30 00:18:25 +00:00
|
|
|
if (mSocketLineReader->bytesAvailable() > 0) {
|
2013-09-26 23:01:59 +01:00
|
|
|
QMetaObject::invokeMethod(this, "dataReceived", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:11:54 +01:00
|
|
|
}
|