// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - The outgoing file transfer: read, announce, place the chunks. #include "FileTransferSender.h" #include "FileTransferHandler.h" #include "Protocol.h" #include #include #include #include #include #include namespace koutnet { namespace { // unguessable transfer ids: system() is the OS CSPRNG. QString randomHex(int bytes) { QByteArray buf(bytes, 0); QRandomGenerator::system()->generate(buf.begin(), buf.end()); return QString::fromLatin1(buf.toHex()); } double nowEpoch() { return QDateTime::currentMSecsSinceEpoch() / 1000.0; } } // namespace FileTransferSender::FileTransferSender(QObject *parent) : QObject(parent) { } void FileTransferSender::setRouter(std::function canUseTls, std::function place) { m_canUseTls = std::move(canUseTls); m_place = std::move(place); } void FileTransferSender::setLocalIdentity(const std::function &ownIdentityId, const std::function &ownAddress) { m_ownIdentityId = ownIdentityId; m_ownAddress = ownAddress; } bool FileTransferSender::send(const QString &toIp, const QString &filePath) { // bytes are read by the caller side that owns the file system; this path // exists for real files, the sendBytes path for things already loaded. The // path is the name the error messages carry, as NetworkManager always did. QFile file(filePath); if (!file.exists() || !file.open(QIODevice::ReadOnly)) { Q_EMIT errorOccurred(i18nc("@info:status %1 is a file path", "File not found: %1", filePath)); return false; } if (file.size() > FileTransferHandler::kMaxTransferBytes) { Q_EMIT errorOccurred(i18nc("@info:status %1 is a file path", "Refused to send %1: larger than the transfer limit.", filePath)); return false; } const QByteArray data = file.readAll(); if (file.error() != QFileDevice::NoError) { Q_EMIT errorOccurred(i18nc("@info:status %1 is a file path", "Could not read %1.", filePath)); return false; } return placeFrames(toIp, data, QFileInfo(filePath).fileName(), filePath); } bool FileTransferSender::sendBytes(const QString &toIp, const QByteArray &rawBytes, const QString &filename) { if (rawBytes.isEmpty()) return false; // an empty blob is not a file, and no frame would say otherwise return placeFrames(toIp, rawBytes, filename, filename); } bool FileTransferSender::placeFrames(const QString &toIp, const QByteArray &data, const QString &filename, const QString &errorLabel) { // The pinned channel is TCP over TLS: ordered, complete and replay-proof, // so the bytes go out plainly in the clear of their own transport and the // meta says so, which is what tells the receive side there is no seal to // open. A peer with no channel is just unreachable. if (!m_canUseTls || !m_canUseTls(toIp)) { Q_EMIT errorOccurred(i18nc("@info:status %1 is a file path", "No live TLS channel to the peer - %1 not sent.", errorLabel)); return false; } if (data.size() > FileTransferHandler::kMaxTransferBytes) { Q_EMIT errorOccurred(i18nc("@info:status %1 is a file path", "Refused to send %1: larger than the transfer limit.", errorLabel)); return false; } if (!m_place) return false; static const QSet kTlsImageExts = {QStringLiteral("png"), QStringLiteral("jpg"), QStringLiteral("jpeg"), QStringLiteral("gif"), QStringLiteral("bmp"), QStringLiteral("webp")}; const bool isImage = kTlsImageExts.contains(QFileInfo(filename).suffix().toLower()); const QString tid = randomHex(8); QCborMap meta; meta[QStringLiteral("type")] = protocol::kMsgFileMeta; meta[QStringLiteral("tid")] = tid; meta[QStringLiteral("filename")] = filename; meta[QStringLiteral("size")] = data.size(); meta[QStringLiteral("is_image")] = isImage; meta[QStringLiteral("encrypted")] = false; meta[QStringLiteral("from_ip")] = m_ownAddress ? m_ownAddress() : QString(); meta[QStringLiteral("from_id")] = m_ownIdentityId ? m_ownIdentityId() : QString(); meta[QStringLiteral("to")] = toIp; meta[QStringLiteral("ts")] = nowEpoch(); m_place(toIp, meta); // Keep each frame well below the channel's frame limit. The payload is a // CBOR byte string, not base64 text: the wire format already supports it. constexpr int kTlsChunkBytes = 256 * 1024; const int totalChunks = (data.size() + kTlsChunkBytes - 1) / kTlsChunkBytes; int idx = 0; for (int offset = 0; offset < data.size(); offset += kTlsChunkBytes, ++idx) { QCborMap c; c[QStringLiteral("type")] = protocol::kMsgFileData; c[QStringLiteral("tid")] = tid; c[QStringLiteral("idx")] = idx; c[QStringLiteral("total")] = totalChunks; c[QStringLiteral("data")] = data.mid(offset, kTlsChunkBytes); m_place(toIp, c); } return true; } } // namespace koutnet