Files
pip/libs/cloud/picloudtcp.cpp
andrey 8bac29d655 fix(picloudtcp): snapshot header under mutex_send in send* and validate payload in parse*
sendStart/sendConnected/sendDisconnected/sendData/sendPing mutated
the shared header.type and serialized the header without holding
mutex_send (it only covered streampacker->send), so concurrent
senders (e.g. ping timer vs app thread on PICloudServer) could mix
type/role bytes of different frames. Snapshot the header into a
local copy under mutex_send in every send* and setRole now takes the
lock as well.

parseDataServer/parseConnect/parseDisconnect read a uint without
checking the payload size; PIByteArray does a partial memcpy on
undersized input, so a short frame produced a garbage client id.
Return the default value when the payload is shorter than sizeof(uint).
2026-08-23 17:17:34 +03:00

221 lines
4.8 KiB
C++

/*
PIP - Platform Independent Primitives
PICloud TCP transport
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "picloudtcp.h"
#include "pichunkstream.h"
#include "picrypt.h"
#include "piethernet.h"
#include "pistreampacker.h"
#include "pitranslator.h"
const char hash_cloud_key[] = "_picloud_";
PICloud::TCP::Header::Header() {
version = Version_2;
}
PICloud::TCP::TCP(PIStreamPacker * s): streampacker(s) {
streampacker->setMaxPacketSize(63 * 1024);
}
void PICloud::TCP::setRole(PICloud::TCP::Role r) {
PIMutexLocker locker(mutex_send);
header.role = r;
}
void PICloud::TCP::setServerName(const PIString & server_name_) {
server_name = server_name_;
suuid =
PICrypt::hash(PIByteArray(server_name_.data(), server_name_.size()), (const unsigned char *)hash_cloud_key, sizeof(hash_cloud_key));
}
PIString PICloud::TCP::serverName() const {
return server_name;
}
void PICloud::TCP::sendStart() {
// piCout << "sendStart";
if (suuid.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, server not set, invoke setServerName first";
return;
}
PIByteArray ba;
{
// snapshot the header under the lock: concurrent send*/setRole must not
// mix type/role bytes of different frames
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Connect;
ba << h;
}
ba.append(suuid);
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
}
void PICloud::TCP::sendConnected(uint client_id) {
PIByteArray ba;
{
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Connect;
ba << h << client_id;
}
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
}
void PICloud::TCP::sendDisconnected(uint client_id) {
PIByteArray ba;
{
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Disconnect;
ba << h << client_id;
}
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
}
int PICloud::TCP::sendData(const PIByteArray & data) {
PIByteArray ba;
{
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Data;
ba << h;
}
ba.append(data);
// piCout << "[PICloud::TCP] sendData" << ba.toHex();
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
return data.size_s();
}
int PICloud::TCP::sendData(const PIByteArray & data, uint client_id) {
PIByteArray ba;
{
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Data;
ba << h << client_id;
}
ba.append(data);
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
return data.size_s();
}
void PICloud::TCP::sendPing() {
PIByteArray ba;
{
PIMutexLocker locker(mutex_send);
Header h = header;
h.type = PICloud::TCP::Ping;
ba << h;
}
ba.append(suuid);
{
PIMutexLocker locker(mutex_send);
streampacker->send(ba);
}
}
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteArray & ba) {
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> ret;
ret.first = InvalidType;
ret.second = InvalidRole;
if (ba.size() < sizeof(Header)) return ret;
PICloud::TCP::Header hdr;
ba >> hdr;
if (hdr.version != header.version) {
piCout << "[PICloud]" << "Invalid PICloud::TCP version!"_tr("PICloud");
return ret;
}
ret.first = (Type)hdr.type;
ret.second = (Role)hdr.role;
return ret;
}
bool PICloud::TCP::canParseData(PIByteArray & ba) {
return header.role == Client;
}
PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
PIPair<uint, PIByteArray> ret;
ret.first = 0;
if (header.role == Server) {
if (ba.size() < sizeof(uint)) return ret;
ba >> ret.first;
ret.second.swap(ba);
}
return ret;
}
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
if (ba.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, invalid server uuid";
return PIByteArray();
} else {
return ba;
}
}
uint PICloud::TCP::parseConnect(PIByteArray & ba) {
uint ret = 0;
if (ba.size() < sizeof(uint)) return ret;
ba >> ret;
return ret;
}
uint PICloud::TCP::parseDisconnect(PIByteArray & ba) {
uint ret = 0;
if (ba.size() < sizeof(uint)) return ret;
ba >> ret;
return ret;
}