2014-10-20 22:49:37 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-10-20 22:49:37 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-10-20 22:49:37 +00:00
|
|
|
*/
|
2019-11-22 09:15:38 +00:00
|
|
|
#include "mtproto/details/mtproto_serialized_request.h"
|
2014-10-20 22:49:37 +00:00
|
|
|
|
2021-09-15 10:21:45 +00:00
|
|
|
#include "base/random.h"
|
2019-11-22 09:15:38 +00:00
|
|
|
|
|
|
|
namespace MTP::details {
|
2018-06-25 18:55:27 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-06-24 05:55:57 +00:00
|
|
|
uint32 CountPaddingPrimesCount(
|
|
|
|
uint32 requestSize,
|
|
|
|
bool forAuthKeyInner) {
|
|
|
|
if (forAuthKeyInner) {
|
2019-11-14 13:34:58 +00:00
|
|
|
return ((8 + requestSize) & 0x03)
|
|
|
|
? (4 - ((8 + requestSize) & 0x03))
|
|
|
|
: 0;
|
|
|
|
}
|
2018-06-25 18:55:27 +00:00
|
|
|
auto result = ((8 + requestSize) & 0x03)
|
|
|
|
? (4 - ((8 + requestSize) & 0x03))
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
// At least 12 bytes of random padding.
|
|
|
|
if (result < 3) {
|
|
|
|
result += 4;
|
|
|
|
}
|
|
|
|
|
2021-06-24 12:29:48 +00:00
|
|
|
// Some more random padding.
|
2021-09-15 10:21:45 +00:00
|
|
|
return result + ((base::RandomValue<uchar>() & 0x0F) << 2);
|
2018-06-25 18:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest::SerializedRequest(const RequestConstructHider::Tag &tag)
|
|
|
|
: _data(std::make_shared<RequestData>(tag)) {
|
2018-06-25 18:55:27 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest SerializedRequest::Prepare(
|
|
|
|
uint32 size,
|
|
|
|
uint32 reserveSize) {
|
|
|
|
Expects(size > 0);
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
const auto finalSize = std::max(size, reserveSize);
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
auto result = SerializedRequest(RequestConstructHider::Tag{});
|
2018-06-25 18:55:27 +00:00
|
|
|
result->reserve(kMessageBodyPosition + finalSize);
|
|
|
|
result->resize(kMessageBodyPosition);
|
|
|
|
result->back() = (size << 2);
|
2019-11-22 09:15:38 +00:00
|
|
|
result->lastSentTime = crl::now();
|
2018-06-25 18:55:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
RequestData *SerializedRequest::operator->() const {
|
2018-06-25 18:55:27 +00:00
|
|
|
Expects(_data != nullptr);
|
|
|
|
|
|
|
|
return _data.get();
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
RequestData &SerializedRequest::operator*() const {
|
2018-06-25 18:55:27 +00:00
|
|
|
Expects(_data != nullptr);
|
|
|
|
|
|
|
|
return *_data;
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
SerializedRequest::operator bool() const {
|
2018-06-25 18:55:27 +00:00
|
|
|
return (_data != nullptr);
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
void SerializedRequest::setMsgId(mtpMsgId msgId) {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
|
|
|
memcpy(_data->data() + kMessageIdPosition, &msgId, sizeof(mtpMsgId));
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
mtpMsgId SerializedRequest::getMsgId() const {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
|
|
|
return *(mtpMsgId*)(_data->constData() + kMessageIdPosition);
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
void SerializedRequest::setSeqNo(uint32 seqNo) {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
|
|
|
(*_data)[kSeqNoPosition] = mtpPrime(seqNo);
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
uint32 SerializedRequest::getSeqNo() const {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
|
|
|
return uint32((*_data)[kSeqNoPosition]);
|
|
|
|
}
|
|
|
|
|
2021-06-24 12:29:48 +00:00
|
|
|
void SerializedRequest::addPadding(bool forAuthKeyInner) {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2018-06-25 18:55:27 +00:00
|
|
|
|
2019-11-05 10:23:09 +00:00
|
|
|
const auto requestSize = (tl::count_length(*this) >> 2);
|
2021-06-24 05:55:57 +00:00
|
|
|
const auto padding = CountPaddingPrimesCount(
|
|
|
|
requestSize,
|
|
|
|
forAuthKeyInner);
|
2018-06-25 18:55:27 +00:00
|
|
|
const auto fullSize = kMessageBodyPosition + requestSize + padding;
|
|
|
|
if (uint32(_data->size()) != fullSize) {
|
|
|
|
_data->resize(fullSize);
|
|
|
|
if (padding > 0) {
|
2019-11-22 09:15:38 +00:00
|
|
|
bytes::set_random(bytes::make_span(*_data).subspan(
|
|
|
|
(fullSize - padding) * sizeof(mtpPrime)));
|
2018-06-25 18:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
uint32 SerializedRequest::messageSize() const {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
2019-11-05 10:23:09 +00:00
|
|
|
const auto ints = (tl::count_length(*this) >> 2);
|
2018-06-25 18:55:27 +00:00
|
|
|
return kMessageIdInts + kSeqNoInts + kMessageLengthInts + ints;
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
bool SerializedRequest::needAck() const {
|
2019-11-14 13:34:58 +00:00
|
|
|
Expects(_data != nullptr);
|
2019-11-22 09:15:38 +00:00
|
|
|
Expects(_data->size() > kMessageBodyPosition);
|
2019-11-14 13:34:58 +00:00
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
const auto type = mtpTypeId((*_data)[kMessageBodyPosition]);
|
|
|
|
switch (type) {
|
|
|
|
case mtpc_msg_container:
|
|
|
|
case mtpc_msgs_ack:
|
|
|
|
case mtpc_http_wait:
|
|
|
|
case mtpc_bad_msg_notification:
|
|
|
|
case mtpc_msgs_all_info:
|
|
|
|
case mtpc_msgs_state_info:
|
|
|
|
case mtpc_msg_detailed_info:
|
|
|
|
case mtpc_msg_new_detailed_info:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
size_t SerializedRequest::sizeInBytes() const {
|
|
|
|
Expects(!_data || _data->size() > kMessageBodyPosition);
|
|
|
|
return _data ? (*_data)[kMessageLengthPosition] : 0;
|
2017-08-12 07:17:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:15:38 +00:00
|
|
|
const void *SerializedRequest::dataInBytes() const {
|
|
|
|
Expects(!_data || _data->size() > kMessageBodyPosition);
|
|
|
|
return _data ? (_data->constData() + kMessageBodyPosition) : nullptr;
|
2017-08-12 07:17:33 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 10:23:09 +00:00
|
|
|
} // namespace MTP
|