2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/mtproto/SessionConnection.h"
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthData.h"
|
2019-11-26 19:57:48 +01:00
|
|
|
#include "td/mtproto/AuthKey.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/CryptoStorer.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/mtproto/mtproto_api.h"
|
|
|
|
#include "td/mtproto/mtproto_api.hpp"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/mtproto/PacketStorer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/Transport.h"
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/utils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-19 22:57:56 +01:00
|
|
|
#include "td/utils/as.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/Gzip.h"
|
|
|
|
#include "td/utils/logging.h"
|
2021-03-28 21:33:22 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
2021-09-12 18:46:12 +02:00
|
|
|
#include "td/utils/TlDowncastHelper.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2021-09-13 14:55:01 +02:00
|
|
|
#include <type_traits>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2020-10-05 17:07:23 +02:00
|
|
|
|
|
|
|
int VERBOSITY_NAME(mtproto) = VERBOSITY_NAME(DEBUG) + 7;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace mtproto_api {
|
|
|
|
|
|
|
|
const int32 msg_container::ID;
|
|
|
|
|
|
|
|
class rpc_result {
|
|
|
|
public:
|
|
|
|
static const int32 ID = -212046591;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mtproto_api
|
|
|
|
|
|
|
|
namespace mtproto {
|
|
|
|
/**
|
|
|
|
* TODO-list.
|
|
|
|
*
|
|
|
|
* 1. Should I check input salt?
|
|
|
|
*
|
|
|
|
* 1. Cancellation of rpc request
|
|
|
|
* input:
|
|
|
|
* - rpc_drop_answer#58e4a740 req_msg_id:long = RpcDropAnswer;
|
|
|
|
* output:
|
|
|
|
* - rpc_answer_unknown#5e2ad36e = RpcDropAnswer;
|
|
|
|
* no ack
|
|
|
|
* - rpc_answer_dropped_running#cd78e586 = RpcDropAnswer;
|
|
|
|
* same answer will be returned to original query
|
|
|
|
* ack
|
|
|
|
* - rpc_answer_dropped#a43ad8b7 msg_id:long seq_no:int bytes:int = RpcDropAnswer;
|
|
|
|
* ack
|
|
|
|
* Alternative is destroy session
|
|
|
|
*
|
|
|
|
* 5. Destroy session
|
|
|
|
* (?) when session will be destroyed otherwise
|
|
|
|
* Must be call in different session
|
|
|
|
* input:
|
|
|
|
* - destroy_session#e7512126 session_id:long = DestroySessionRes;
|
|
|
|
* output:
|
|
|
|
* - destroy_session_ok#e22045fc session_id:long = DestroySessionRes;
|
|
|
|
* - destroy_session_none#62d350c9 session_id:long = DestroySessionRes;
|
|
|
|
*
|
|
|
|
|
|
|
|
* DONE:
|
|
|
|
* 3. Ping pong
|
|
|
|
* input:
|
|
|
|
* - ping#7abe77ec ping_id:long = Pong;
|
|
|
|
* - pong#347773c5 msg_id:long ping_id:long = Pong;
|
|
|
|
*
|
|
|
|
* 4. Ping + deferred connection closure
|
|
|
|
* input:
|
|
|
|
* - ping_delay_disconnect#f3427b8c ping_id:long disconnect_delay:int = Pong;
|
|
|
|
*
|
|
|
|
* 6. New session creation
|
2018-12-27 20:24:44 +01:00
|
|
|
* A notification about new session.
|
2023-02-26 11:41:49 +01:00
|
|
|
* It is reasonable to store unique_id with current session in order to process duplicated notifications only once.
|
2018-12-31 20:04:05 +01:00
|
|
|
*
|
2023-01-02 13:38:04 +01:00
|
|
|
* Causes all messages older than first_msg_id to be re-sent and notifies about a gap in updates
|
2018-12-31 20:04:05 +01:00
|
|
|
* output:
|
|
|
|
* - new_session_created#9ec20908 first_msg_id:long unique_id:long server_salt:long = NewSession
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 7. Containers
|
|
|
|
* I should pack output messages as containers
|
|
|
|
* - msg_container#73f1f8dc messages:vector message = MessageContainer;
|
|
|
|
* message msg_id:long seqno:int bytes:int body:Object = Message;
|
|
|
|
*
|
|
|
|
* 8. Packed Object
|
|
|
|
* I should pack big output messages with gzip
|
|
|
|
* - gzip_packed#3072cfa1 packed_data:string = Object;
|
|
|
|
*
|
|
|
|
* 9. Ack
|
|
|
|
* I should actually send acks
|
|
|
|
* (?) Does updates need ack
|
|
|
|
* - msgs_ack#62d6b459 msg_ids:Vector long = MsgsAck;
|
|
|
|
*
|
|
|
|
* 10. Errors
|
|
|
|
* output:
|
|
|
|
* - bad_msg_notification#a7eff811 bad_msg_id:long bad_msg_seqno:int error_code:int = BadMsgNotification;
|
|
|
|
* - bad_server_salt#edab447b bad_msg_id:long bad_msg_seqno:int error_code:int new_server_salt:long =
|
|
|
|
* BadMsgNotification;
|
|
|
|
*
|
|
|
|
* error codes:
|
|
|
|
* 16: msg_id is too low. -- lite resend. It will be automatially packed in a container. I hope.
|
|
|
|
* 17: msg_id is too high. -- fail connection.
|
|
|
|
* 18: msg_id % 4 != 0. -- Error and fail connection.
|
|
|
|
* 19: container msg_id is the same as msg_id of a previously received message. MUST NEVER HAPPENS. Error and fail
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* 20: message is to old -- full resend. (or fail query, if we are afraid of double send)
|
|
|
|
*
|
|
|
|
* 32: seq_no is too low. (msg_id1 < msg_id2 <==> seq_no1 < seq_no2). Error and fail connection
|
2018-03-13 20:45:00 +01:00
|
|
|
* 33: seq_no is too high. Error and fail connection.
|
2018-12-31 20:04:05 +01:00
|
|
|
* 34: (?) an even msg_seqno expected (irrelevant message), but odd received. (Fail and call a developer...)
|
|
|
|
* 35: (?) odd msg_seqno expected (relevant message), but even received (Fail and call a developer)
|
|
|
|
*
|
|
|
|
* 48: incorrect server salt (in bad_server_salt message)
|
|
|
|
*
|
|
|
|
* 64: (?) invalid container
|
|
|
|
*
|
|
|
|
* 2. Get future salts
|
|
|
|
* input:
|
|
|
|
* - get_future_salts#b921bd04 num:int = FutureSalts;
|
|
|
|
* output:
|
|
|
|
* - future_salts#ae500895 req_msg_id:long now:int salts:vector future_salt = FutureSalts;
|
|
|
|
* future_salt#0949d9dc valid_since:int valid_until:int salt:long = FutureSalt;
|
|
|
|
*
|
|
|
|
* 1. ping-pong
|
|
|
|
* 3. Delayed ack.
|
|
|
|
* 4. Delayed packet.
|
|
|
|
* HTTP transport (support of several connections)
|
|
|
|
* get future salts
|
|
|
|
*
|
|
|
|
* 11. Explicit request of messages states
|
|
|
|
* input:
|
|
|
|
* - msgs_state_req#da69fb52 msg_ids:Vector long = MsgsStateReq;
|
|
|
|
* 12. States of messages in answer
|
|
|
|
* output:
|
|
|
|
* - msgs_state_info#04deb57d req_msg_id:long info:string = MsgsStateInfo;
|
|
|
|
* info contain one byte for each queired msg_id
|
|
|
|
* bytes:
|
|
|
|
* 1: unknown message_id
|
|
|
|
* 2: message not received (msg_id in stored range)
|
|
|
|
* 3: message not receiver (msg_id is too high)
|
|
|
|
* 4: message received. No extra ack will be sent
|
2018-02-17 00:54:44 +01:00
|
|
|
* +8: message is already acknowledged
|
|
|
|
* +16: message don't requires acknowledgement
|
|
|
|
* +32: RPC query contained in message being processed or the processing has already been completed
|
|
|
|
* +64: content-related response to message has already been generated
|
2018-12-31 20:04:05 +01:00
|
|
|
* +128: other party knows for a fact that message is already received
|
|
|
|
*
|
|
|
|
* 13. Voluntary Communication of Status of Messages
|
|
|
|
* output:
|
|
|
|
* - msgs_all_info#8cc0d131 msg_ids:Vector long info:string = MsgsAllInfo
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-09-09 23:30:49 +02:00
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, const SessionConnection::MsgInfo &info) {
|
2023-09-21 16:52:33 +02:00
|
|
|
return string_builder << "with " << info.message_id << " and seq_no " << info.seq_no;
|
2023-09-09 23:30:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<RawConnection> SessionConnection::move_as_raw_connection() {
|
2021-10-24 12:23:47 +02:00
|
|
|
was_moved_ = true;
|
2019-05-06 18:59:49 +02:00
|
|
|
return std::move(raw_connection_);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice SessionConnection::as_buffer_slice(Slice packet) {
|
2018-12-11 21:34:46 +01:00
|
|
|
return current_buffer_slice_->from_slice(packet);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::parse_message(TlParser &parser, MsgInfo *info, Slice *packet, bool crypto_flag) {
|
|
|
|
// msg_id:long seqno:int bytes:int
|
|
|
|
parser.check_len(sizeof(int64) + (crypto_flag ? sizeof(int32) : 0) + sizeof(int32));
|
|
|
|
if (parser.get_error() != nullptr) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::message: " << parser.get_error());
|
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
info->message_id = MessageId(static_cast<uint64>(parser.fetch_long_unsafe()));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (crypto_flag) {
|
|
|
|
info->seq_no = parser.fetch_int_unsafe();
|
|
|
|
}
|
|
|
|
uint32 bytes = parser.fetch_int_unsafe();
|
|
|
|
|
|
|
|
if (bytes % sizeof(int32) != 0) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::message: size of message [" << bytes
|
|
|
|
<< "] is not divisible by 4");
|
|
|
|
}
|
|
|
|
|
2024-03-28 18:03:31 +01:00
|
|
|
*packet = parser.template fetch_string_raw<Slice>(bytes);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (parser.get_error() != nullptr) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::message: " << parser.get_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
info->size = bytes;
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet_container(const MsgInfo &info, Slice packet) {
|
2023-09-21 14:44:26 +02:00
|
|
|
auto old_container_message_id = container_message_id_;
|
|
|
|
container_message_id_ = info.message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
2023-09-21 14:44:26 +02:00
|
|
|
container_message_id_ = old_container_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TlParser parser(packet);
|
|
|
|
int32 size = parser.fetch_int();
|
|
|
|
if (parser.get_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::rpc_container: " << parser.get_error());
|
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive container " << container_message_id_ << " of size " << size;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
TRY_STATUS(parse_packet(parser));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::reset_server_time_difference(MessageId message_id) {
|
2023-05-14 22:11:23 +02:00
|
|
|
VLOG(mtproto) << "Reset server time difference";
|
2023-09-21 16:52:33 +02:00
|
|
|
auth_data_->reset_server_time_difference(static_cast<uint32>(message_id.get() >> 32) - Time::now());
|
2023-05-14 22:11:23 +02:00
|
|
|
callback_->on_server_time_difference_updated(true);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SessionConnection::on_packet_rpc_result(const MsgInfo &info, Slice packet) {
|
|
|
|
TlParser parser(packet);
|
2023-09-21 18:01:17 +02:00
|
|
|
auto req_msg_id = static_cast<uint64>(parser.fetch_long());
|
2018-12-31 20:04:05 +01:00
|
|
|
if (parser.get_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::rpc_result: " << parser.get_error());
|
|
|
|
}
|
2021-08-22 12:23:21 +02:00
|
|
|
if (req_msg_id == 0) {
|
2023-09-08 23:13:42 +02:00
|
|
|
LOG(ERROR) << "Receive an update in rpc_result " << info;
|
2021-08-22 12:23:21 +02:00
|
|
|
return Status::Error("Receive an update in rpc_result");
|
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive result for request with " << MessageId(req_msg_id) << ' ' << info;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
if (info.message_id.get() < req_msg_id - (static_cast<uint64>(15) << 32)) {
|
2023-05-14 22:11:23 +02:00
|
|
|
reset_server_time_difference(info.message_id);
|
|
|
|
}
|
|
|
|
|
2021-09-10 15:08:46 +02:00
|
|
|
switch (parser.fetch_int()) {
|
|
|
|
case mtproto_api::rpc_error::ID: {
|
|
|
|
mtproto_api::rpc_error rpc_error(parser);
|
|
|
|
if (parser.get_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::rpc_error: " << parser.get_error());
|
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
callback_->on_message_result_error(MessageId(req_msg_id), rpc_error.error_code_, rpc_error.error_message_.str());
|
2021-09-10 15:12:56 +02:00
|
|
|
return Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-10 15:08:46 +02:00
|
|
|
case mtproto_api::gzip_packed::ID: {
|
|
|
|
mtproto_api::gzip_packed gzip(parser);
|
|
|
|
if (parser.get_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse mtproto_api::gzip_packed: " << parser.get_error());
|
|
|
|
}
|
|
|
|
// yep, gzip in rpc_result
|
|
|
|
BufferSlice object = gzdecode(gzip.packed_data_);
|
|
|
|
// send header no more optimization
|
2023-09-21 16:52:33 +02:00
|
|
|
return callback_->on_message_result_ok(MessageId(req_msg_id), std::move(object), info.size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-10 15:08:46 +02:00
|
|
|
default:
|
2021-09-12 18:46:12 +02:00
|
|
|
packet.remove_prefix(sizeof(req_msg_id));
|
2023-09-21 16:52:33 +02:00
|
|
|
return callback_->on_message_result_ok(MessageId(req_msg_id), as_buffer_slice(packet), info.size);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const T &packet) {
|
|
|
|
LOG(ERROR) << "Unsupported: " << to_string(packet);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::destroy_auth_key_ok &destroy_auth_key) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive destroy_auth_key_ok " << info;
|
2017-12-29 21:34:39 +01:00
|
|
|
return on_destroy_auth_key(destroy_auth_key);
|
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::destroy_auth_key_none &destroy_auth_key) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive destroy_auth_key_none " << info;
|
2017-12-29 21:34:39 +01:00
|
|
|
return on_destroy_auth_key(destroy_auth_key);
|
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::destroy_auth_key_fail &destroy_auth_key) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive destroy_auth_key_fail " << info;
|
2017-12-29 21:34:39 +01:00
|
|
|
return on_destroy_auth_key(destroy_auth_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_destroy_auth_key(const mtproto_api::DestroyAuthKeyRes &destroy_auth_key) {
|
2023-10-15 20:25:07 +02:00
|
|
|
if (!need_destroy_auth_key_) {
|
|
|
|
LOG(ERROR) << "Receive unexpected " << oneline(to_string(destroy_auth_key));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
return callback_->on_destroy_auth_key();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::new_session_created &new_session_created) {
|
2023-09-21 16:52:33 +02:00
|
|
|
auto first_message_id = MessageId(static_cast<uint64>(new_session_created.first_msg_id_));
|
|
|
|
VLOG(mtproto) << "Receive new_session_created " << info << ": [first " << first_message_id
|
|
|
|
<< "] [unique_id:" << new_session_created.unique_id_ << ']';
|
2023-09-03 01:07:12 +02:00
|
|
|
|
|
|
|
auto it = service_queries_.find(first_message_id);
|
|
|
|
if (it != service_queries_.end()) {
|
2023-09-21 15:52:21 +02:00
|
|
|
first_message_id = it->second.container_message_id_;
|
2023-09-21 16:52:33 +02:00
|
|
|
LOG(INFO) << "Update first_message_id to container's " << first_message_id;
|
2023-09-03 01:07:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback_->on_new_session_created(new_session_created.unique_id_, first_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::bad_msg_notification &bad_msg_notification) {
|
2023-09-21 16:52:33 +02:00
|
|
|
MsgInfo bad_info{MessageId(static_cast<uint64>(bad_msg_notification.bad_msg_id_)),
|
|
|
|
bad_msg_notification.bad_msg_seqno_, 0};
|
2018-12-31 20:04:05 +01:00
|
|
|
enum Code {
|
|
|
|
MsgIdTooLow = 16,
|
|
|
|
MsgIdTooHigh = 17,
|
|
|
|
MsgIdMod4 = 18,
|
|
|
|
MsgIdCollision = 19,
|
|
|
|
|
|
|
|
MsgIdTooOld = 20,
|
|
|
|
|
|
|
|
SeqNoTooLow = 32,
|
|
|
|
SeqNoTooHigh = 33,
|
|
|
|
SeqNoNotEven = 34,
|
|
|
|
SeqNoNotOdd = 35,
|
|
|
|
|
|
|
|
InvalidContainer = 64
|
|
|
|
};
|
2020-01-08 21:41:03 +01:00
|
|
|
Slice common = ". BUG! CALL FOR A DEVELOPER! Session will be closed";
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (bad_msg_notification.error_code_) {
|
2023-09-08 23:13:42 +02:00
|
|
|
case MsgIdTooLow:
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << bad_info << ": MessageId is too low. Message will be re-sent";
|
|
|
|
// time will be updated automagically
|
|
|
|
on_message_failed(bad_info.message_id, Status::Error("MessageId is too low"));
|
|
|
|
break;
|
2023-09-08 23:13:42 +02:00
|
|
|
case MsgIdTooHigh:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(WARNING) << bad_info << ": MessageId is too high. Session will be closed";
|
2018-12-31 20:04:05 +01:00
|
|
|
// All this queries will be re-sent by parent
|
|
|
|
to_send_.clear();
|
2023-05-14 22:11:23 +02:00
|
|
|
reset_server_time_difference(info.message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_->on_session_failed(Status::Error("MessageId is too high"));
|
|
|
|
return Status::Error("MessageId is too high");
|
2023-09-08 23:13:42 +02:00
|
|
|
case MsgIdMod4:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": MessageId is not divisible by 4" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("MessageId is not divisible by 4");
|
2023-09-08 23:13:42 +02:00
|
|
|
case MsgIdCollision:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": Container and older message MessageId collision" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Container and older message MessageId collision");
|
2023-09-08 23:13:42 +02:00
|
|
|
case MsgIdTooOld:
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << bad_info << ": MessageId is too old. Message will be re-sent";
|
|
|
|
on_message_failed(bad_info.message_id, Status::Error("MessageId is too old"));
|
|
|
|
break;
|
2023-09-08 23:13:42 +02:00
|
|
|
case SeqNoTooLow:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": SeqNo is too low" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("SeqNo is too low");
|
2023-09-08 23:13:42 +02:00
|
|
|
case SeqNoTooHigh:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": SeqNo is too high" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("SeqNo is too high");
|
2023-09-08 23:13:42 +02:00
|
|
|
case SeqNoNotEven:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": SeqNo is not even for an irrelevant message" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("SeqNo is not even for an irrelevant message");
|
2023-09-08 23:13:42 +02:00
|
|
|
case SeqNoNotOdd:
|
2023-05-14 00:41:23 +02:00
|
|
|
LOG(ERROR) << bad_info << ": SeqNo is not odd for a relevant message" << common;
|
|
|
|
return Status::Error("SeqNo is not odd for a relevant message");
|
2023-09-08 23:13:42 +02:00
|
|
|
case InvalidContainer:
|
2023-05-16 20:53:55 +02:00
|
|
|
LOG(ERROR) << bad_info << ": Invalid Container" << common;
|
|
|
|
return Status::Error("Invalid Container");
|
2023-09-08 23:13:42 +02:00
|
|
|
default:
|
2020-01-08 21:41:03 +01:00
|
|
|
LOG(ERROR) << bad_info << ": Unknown error [code:" << bad_msg_notification.error_code_ << "]" << common;
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Unknown error code");
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::bad_server_salt &bad_server_salt) {
|
2023-09-21 16:52:33 +02:00
|
|
|
MsgInfo bad_info{MessageId(static_cast<uint64>(bad_server_salt.bad_msg_id_)), bad_server_salt.bad_msg_seqno_, 0};
|
|
|
|
VLOG(mtproto) << "Receive bad_server_salt " << info << ": " << bad_info;
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_data_->set_server_salt(bad_server_salt.new_server_salt_, Time::now_cached());
|
|
|
|
callback_->on_server_salt_updated();
|
|
|
|
|
|
|
|
on_message_failed(bad_info.message_id, Status::Error("Bad server salt"));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::msgs_ack &msgs_ack) {
|
2023-09-21 16:52:33 +02:00
|
|
|
auto message_ids = transform(msgs_ack.msg_ids_, [](int64 msg_id) { return MessageId(static_cast<uint64>(msg_id)); });
|
|
|
|
VLOG(mtproto) << "Receive msgs_ack " << info << ": " << message_ids;
|
|
|
|
for (auto message_id : message_ids) {
|
2023-09-21 15:09:19 +02:00
|
|
|
callback_->on_message_ack(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::gzip_packed &gzip_packed) {
|
|
|
|
BufferSlice res = gzdecode(gzip_packed.packed_data_);
|
|
|
|
auto guard = set_buffer_slice(&res);
|
|
|
|
return on_slice_packet(info, res.as_slice());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::pong &pong) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive pong " << info;
|
|
|
|
if (info.message_id.get() < static_cast<uint64>(pong.msg_id_) - (static_cast<uint64>(15) << 32)) {
|
2023-05-14 22:11:23 +02:00
|
|
|
reset_server_time_difference(info.message_id);
|
|
|
|
}
|
2023-10-23 12:02:04 +02:00
|
|
|
|
|
|
|
if (sent_destroy_auth_key_ && destroy_auth_key_send_time_ < Time::now() - 60) {
|
|
|
|
return Status::Error(PSLICE() << "No response for destroy_auth_key for "
|
|
|
|
<< (Time::now() - destroy_auth_key_send_time_) << " seconds from auth key "
|
|
|
|
<< auth_data_->get_auth_key().id());
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
last_pong_at_ = Time::now_cached();
|
2023-03-14 13:45:01 +01:00
|
|
|
real_last_pong_at_ = last_pong_at_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return callback_->on_pong();
|
|
|
|
}
|
2023-04-03 18:55:12 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::future_salts &salts) {
|
2021-08-22 12:36:47 +02:00
|
|
|
vector<ServerSalt> new_salts;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &it : salts.salts_) {
|
|
|
|
new_salts.push_back(
|
|
|
|
ServerSalt{it->salt_, static_cast<double>(it->valid_since_), static_cast<double>(it->valid_until_)});
|
|
|
|
}
|
2023-04-03 18:55:12 +02:00
|
|
|
auto now = Time::now_cached();
|
|
|
|
auth_data_->set_future_salts(new_salts, now);
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive future_salts " << info << ": is_valid = " << auth_data_->is_server_salt_valid(now)
|
2023-04-03 18:55:12 +02:00
|
|
|
<< ", has_salt = " << auth_data_->has_salt(now)
|
|
|
|
<< ", need_future_salts = " << auth_data_->need_future_salts(now);
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_->on_server_salt_updated();
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:52:21 +02:00
|
|
|
Status SessionConnection::on_msgs_state_info(const vector<int64> &msg_ids, Slice info) {
|
|
|
|
if (msg_ids.size() != info.size()) {
|
|
|
|
return Status::Error(PSLICE() << tag("message count", msg_ids.size()) << " != " << tag("info.size()", info.size()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
size_t i = 0;
|
2023-09-21 15:52:21 +02:00
|
|
|
for (auto msg_id : msg_ids) {
|
2023-09-21 16:52:33 +02:00
|
|
|
callback_->on_message_info(MessageId(static_cast<uint64>(msg_id)), info[i], MessageId(), 0, 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::msgs_state_info &msgs_state_info) {
|
2023-09-21 16:52:33 +02:00
|
|
|
auto message_id = MessageId(static_cast<uint64>(msgs_state_info.req_msg_id_));
|
2023-09-21 15:52:21 +02:00
|
|
|
auto it = service_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == service_queries_.end()) {
|
|
|
|
return Status::Error("Unknown msgs_state_info");
|
|
|
|
}
|
2022-02-09 14:05:53 +01:00
|
|
|
auto query = std::move(it->second);
|
|
|
|
service_queries_.erase(it);
|
|
|
|
|
2023-09-21 15:52:21 +02:00
|
|
|
if (query.type_ != ServiceQuery::GetStateInfo) {
|
2023-04-03 11:15:44 +02:00
|
|
|
return Status::Error("Receive msgs_state_info in response not to GetStateInfo");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive msgs_state_info " << info;
|
2023-09-21 15:52:21 +02:00
|
|
|
return on_msgs_state_info(query.msg_ids_, msgs_state_info.info_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::msgs_all_info &msgs_all_info) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive msgs_all_info " << info;
|
2018-12-31 20:04:05 +01:00
|
|
|
return on_msgs_state_info(msgs_all_info.msg_ids_, msgs_all_info.info_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info, const mtproto_api::msg_detailed_info &msg_detailed_info) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive msg_detailed_info " << info;
|
|
|
|
callback_->on_message_info(MessageId(static_cast<uint64>(msg_detailed_info.msg_id_)), msg_detailed_info.status_,
|
|
|
|
MessageId(static_cast<uint64>(msg_detailed_info.answer_msg_id_)), msg_detailed_info.bytes_,
|
|
|
|
2);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_packet(const MsgInfo &info,
|
|
|
|
const mtproto_api::msg_new_detailed_info &msg_new_detailed_info) {
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive msg_new_detailed_info " << info;
|
|
|
|
callback_->on_message_info(MessageId(), 0, MessageId(static_cast<uint64>(msg_new_detailed_info.answer_msg_id_)),
|
|
|
|
msg_new_detailed_info.bytes_, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_slice_packet(const MsgInfo &info, Slice packet) {
|
|
|
|
if (info.seq_no & 1) {
|
|
|
|
send_ack(info.message_id);
|
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
if (packet.size() < 4) {
|
|
|
|
callback_->on_session_failed(Status::Error("Receive too small packet"));
|
|
|
|
return Status::Error(PSLICE() << "Receive packet of size " << packet.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 constructor_id = as<int32>(packet.begin());
|
|
|
|
if (constructor_id == mtproto_api::msg_container::ID) {
|
|
|
|
return on_packet_container(info, packet.substr(4));
|
|
|
|
}
|
|
|
|
if (constructor_id == mtproto_api::rpc_result::ID) {
|
|
|
|
return on_packet_rpc_result(info, packet.substr(4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TlDowncastHelper<mtproto_api::Object> helper(constructor_id);
|
|
|
|
Status status;
|
|
|
|
bool is_mtproto_api = downcast_call(static_cast<mtproto_api::Object &>(helper), [&](auto &dummy) {
|
|
|
|
// a constructor from mtproto_api
|
|
|
|
using Type = std::decay_t<decltype(dummy)>;
|
|
|
|
TlParser parser(packet.substr(4));
|
|
|
|
auto object = Type::fetch(parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
if (parser.get_error()) {
|
|
|
|
status = parser.get_status();
|
|
|
|
} else {
|
|
|
|
status = this->on_packet(info, static_cast<const Type &>(*object));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
});
|
|
|
|
if (is_mtproto_api) {
|
|
|
|
return status;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-12-10 23:20:13 +01:00
|
|
|
auto get_update_description = [&] {
|
2023-08-20 20:00:42 +02:00
|
|
|
return PSTRING() << "update from " << get_name() << " with auth key " << auth_data_->get_auth_key().id()
|
2023-09-21 14:44:26 +02:00
|
|
|
<< " active for " << (Time::now() - created_at_) << " seconds in container "
|
2023-09-21 16:52:33 +02:00
|
|
|
<< container_message_id_ << " from session " << auth_data_->get_session_id() << ' ' << info
|
|
|
|
<< ", main " << main_message_id_ << " and original size = " << info.size;
|
2022-12-10 23:20:13 +01:00
|
|
|
};
|
|
|
|
|
2021-09-12 18:46:12 +02:00
|
|
|
// It is an update... I hope.
|
|
|
|
status = auth_data_->check_update(info.message_id);
|
|
|
|
auto recheck_status = auth_data_->recheck_update(info.message_id);
|
|
|
|
if (recheck_status.is_error() && recheck_status.code() == 2) {
|
2022-12-10 23:20:13 +01:00
|
|
|
LOG(WARNING) << "Receive very old " << get_update_description() << ": " << status << ' ' << recheck_status;
|
2021-09-12 18:46:12 +02:00
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (status.code() == 2) {
|
2022-12-10 23:20:13 +01:00
|
|
|
LOG(WARNING) << "Receive too old " << get_update_description() << ": " << status;
|
2021-09-12 18:46:12 +02:00
|
|
|
callback_->on_session_failed(Status::Error("Receive too old update"));
|
|
|
|
return status;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-12-10 23:20:13 +01:00
|
|
|
VLOG(mtproto) << "Skip " << get_update_description() << ": " << status;
|
2021-09-12 18:46:12 +02:00
|
|
|
return Status::OK();
|
|
|
|
} else {
|
2022-12-10 23:20:13 +01:00
|
|
|
VLOG(mtproto) << "Receive " << get_update_description();
|
2021-09-12 18:46:12 +02:00
|
|
|
return callback_->on_update(as_buffer_slice(packet));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::parse_packet(TlParser &parser) {
|
|
|
|
MsgInfo info;
|
|
|
|
Slice packet;
|
|
|
|
TRY_STATUS(parse_message(parser, &info, &packet, true));
|
|
|
|
return on_slice_packet(info, packet);
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
Status SessionConnection::on_main_packet(const PacketInfo &packet_info, Slice packet) {
|
2023-03-14 13:45:01 +01:00
|
|
|
// Update pong here too. Real pong can be delayed by many big packets
|
2018-12-31 20:04:05 +01:00
|
|
|
last_pong_at_ = Time::now_cached();
|
2023-03-14 13:45:01 +01:00
|
|
|
real_last_pong_at_ = last_pong_at_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!connected_flag_) {
|
|
|
|
connected_flag_ = true;
|
|
|
|
callback_->on_connected();
|
|
|
|
}
|
|
|
|
|
2023-09-08 23:13:42 +02:00
|
|
|
VLOG(raw_mtproto) << "Receive packet of size " << packet.size() << ':' << format::as_hex_dump<4>(packet);
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Receive packet with " << packet_info.message_id << " and seq_no " << packet_info.seq_no
|
|
|
|
<< " of size " << packet.size();
|
2023-09-08 23:13:42 +02:00
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
if (packet_info.no_crypto_flag) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Unencrypted packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
TlParser parser(packet);
|
|
|
|
TRY_STATUS(parse_packet(parser));
|
|
|
|
parser.fetch_end();
|
|
|
|
if (parser.get_error()) {
|
|
|
|
return Status::Error(PSLICE() << "Failed to parse packet: " << parser.get_error());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::on_message_failed(MessageId message_id, Status status) {
|
2023-09-21 15:09:19 +02:00
|
|
|
callback_->on_message_failed(message_id, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
sent_destroy_auth_key_ = false;
|
2023-10-23 12:02:04 +02:00
|
|
|
destroy_auth_key_send_time_ = 0.0;
|
2017-12-29 21:34:39 +01:00
|
|
|
|
2023-09-21 15:09:19 +02:00
|
|
|
if (message_id == last_ping_message_id_ || message_id == last_ping_container_message_id_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// restart ping immediately
|
|
|
|
last_ping_at_ = 0;
|
2023-09-21 16:52:33 +02:00
|
|
|
last_ping_message_id_ = {};
|
|
|
|
last_ping_container_message_id_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 15:52:21 +02:00
|
|
|
auto cit = container_to_service_message_id_.find(message_id);
|
|
|
|
if (cit != container_to_service_message_id_.end()) {
|
2022-02-09 14:05:53 +01:00
|
|
|
auto message_ids = cit->second;
|
2023-09-21 15:09:19 +02:00
|
|
|
for (auto inner_message_id : message_ids) {
|
|
|
|
on_message_failed_inner(inner_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-21 15:09:19 +02:00
|
|
|
on_message_failed_inner(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-12 18:46:12 +02:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::on_message_failed_inner(MessageId message_id) {
|
2023-09-21 15:09:19 +02:00
|
|
|
auto it = service_queries_.find(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == service_queries_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-09 14:05:53 +01:00
|
|
|
auto query = std::move(it->second);
|
|
|
|
service_queries_.erase(it);
|
|
|
|
|
2023-09-21 15:52:21 +02:00
|
|
|
switch (query.type_) {
|
2023-09-03 01:07:12 +02:00
|
|
|
case ServiceQuery::ResendAnswer:
|
2023-09-21 15:52:21 +02:00
|
|
|
for (auto msg_id : query.msg_ids_) {
|
2023-09-21 16:52:33 +02:00
|
|
|
resend_answer(MessageId(static_cast<uint64>(msg_id)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-09-03 01:07:12 +02:00
|
|
|
case ServiceQuery::GetStateInfo:
|
2023-09-21 15:52:21 +02:00
|
|
|
for (auto msg_id : query.msg_ids_) {
|
2023-09-21 16:52:33 +02:00
|
|
|
get_state_info(MessageId(static_cast<uint64>(msg_id)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
break;
|
2022-02-09 14:05:53 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SessionConnection::must_flush_packet() {
|
|
|
|
flush_packet_at_ = 0;
|
|
|
|
|
|
|
|
// we need key to send just something
|
|
|
|
if (!auth_data_->has_auth_key(Time::now_cached())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transport is ready
|
|
|
|
if (!raw_connection_->can_send()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_salt = auth_data_->has_salt(Time::now_cached());
|
|
|
|
// do not send anything in long poll connection before we have salt
|
|
|
|
if (mode_ == Mode::HttpLongPoll && !has_salt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// http_wait
|
|
|
|
if (mode_ == Mode::HttpLongPoll) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// queries and acks (+ resend & get_info)
|
|
|
|
if (has_salt && force_send_at_ != 0) {
|
|
|
|
if (Time::now_cached() > force_send_at_) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
relax_timeout_at(&flush_packet_at_, force_send_at_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ping
|
|
|
|
if (has_salt) {
|
|
|
|
if (must_ping()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
relax_timeout_at(&flush_packet_at_, last_ping_at_ + ping_must_delay());
|
|
|
|
}
|
|
|
|
// get_future_salt
|
|
|
|
if (!has_salt) {
|
2018-09-04 16:01:01 +02:00
|
|
|
if (last_get_future_salt_at_ == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
auto get_future_salts_at = last_get_future_salt_at_ + 60;
|
|
|
|
if (get_future_salts_at < Time::now_cached()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
relax_timeout_at(&flush_packet_at_, get_future_salts_at);
|
|
|
|
}
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
if (has_salt && need_destroy_auth_key_ && !sent_destroy_auth_key_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::before_write() {
|
2021-09-16 15:20:07 +02:00
|
|
|
CHECK(raw_connection_);
|
2018-12-31 20:04:05 +01:00
|
|
|
while (must_flush_packet()) {
|
|
|
|
flush_packet();
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
Status SessionConnection::on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto old_main_message_id = main_message_id_;
|
2023-09-09 23:04:55 +02:00
|
|
|
main_message_id_ = packet_info.message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
SCOPE_EXIT {
|
|
|
|
main_message_id_ = old_main_message_id;
|
|
|
|
};
|
|
|
|
|
2023-09-09 23:04:55 +02:00
|
|
|
if (packet_info.no_crypto_flag) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error("Unexpected unencrypted packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool time_difference_was_updated = false;
|
2023-09-09 23:04:55 +02:00
|
|
|
auto status = auth_data_->check_packet(packet_info.session_id, packet_info.message_id, Time::now_cached(),
|
|
|
|
time_difference_was_updated);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (time_difference_was_updated) {
|
2023-05-14 20:58:54 +02:00
|
|
|
callback_->on_server_time_difference_updated(false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (status.is_error()) {
|
|
|
|
if (status.code() == 1) {
|
2023-09-08 23:13:42 +02:00
|
|
|
LOG(INFO) << "Packet is ignored: " << status;
|
2023-09-09 23:04:55 +02:00
|
|
|
send_ack(packet_info.message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
2018-12-06 22:01:55 +01:00
|
|
|
} else if (status.code() == 2) {
|
|
|
|
LOG(WARNING) << "Receive too old packet: " << status;
|
|
|
|
callback_->on_session_failed(Status::Error("Receive too old packet"));
|
|
|
|
return status;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto guard = set_buffer_slice(&packet);
|
2023-09-09 23:04:55 +02:00
|
|
|
TRY_STATUS(on_main_packet(packet_info, packet.as_slice()));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::on_quick_ack(uint64 quick_ack_token) {
|
2023-09-21 16:52:33 +02:00
|
|
|
callback_->on_message_ack(MessageId(quick_ack_token));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-11-20 13:07:27 +01:00
|
|
|
|
|
|
|
void SessionConnection::on_read(size_t size) {
|
|
|
|
last_read_at_ = Time::now_cached();
|
2023-03-14 13:45:01 +01:00
|
|
|
real_last_read_at_ = last_read_at_;
|
2022-09-20 16:46:10 +02:00
|
|
|
last_read_size_ += size;
|
2018-11-20 13:07:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 21:34:46 +01:00
|
|
|
SessionConnection::SessionConnection(Mode mode, unique_ptr<RawConnection> raw_connection, AuthData *auth_data)
|
2022-08-27 23:49:56 +02:00
|
|
|
: random_delay_(Random::fast(0, 5000000) * 1e-6)
|
|
|
|
, state_(Init)
|
|
|
|
, mode_(mode)
|
|
|
|
, created_at_(Time::now())
|
|
|
|
, raw_connection_(std::move(raw_connection))
|
|
|
|
, auth_data_(auth_data) {
|
2021-09-16 15:20:07 +02:00
|
|
|
CHECK(raw_connection_);
|
2023-10-23 12:02:04 +02:00
|
|
|
CHECK(auth_data_ != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 09:42:40 +02:00
|
|
|
PollableFdInfo &SessionConnection::get_poll_info() {
|
2021-09-16 15:20:07 +02:00
|
|
|
CHECK(raw_connection_);
|
2018-08-14 09:42:40 +02:00
|
|
|
return raw_connection_->get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::init() {
|
|
|
|
CHECK(state_ == Init);
|
|
|
|
last_pong_at_ = Time::now_cached();
|
2018-11-20 13:07:27 +01:00
|
|
|
last_read_at_ = Time::now_cached();
|
2018-12-31 20:04:05 +01:00
|
|
|
state_ = Run;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-11-20 13:07:27 +01:00
|
|
|
void SessionConnection::set_online(bool online_flag, bool is_main) {
|
2023-03-08 16:10:07 +01:00
|
|
|
LOG(DEBUG) << "Set online to " << online_flag;
|
2019-03-31 21:29:34 +02:00
|
|
|
bool need_ping = online_flag || !online_flag_;
|
2018-12-31 20:04:05 +01:00
|
|
|
online_flag_ = online_flag;
|
2018-11-20 13:07:27 +01:00
|
|
|
is_main_ = is_main;
|
|
|
|
auto now = Time::now();
|
2019-03-31 21:29:34 +02:00
|
|
|
if (need_ping) {
|
2018-11-20 13:07:27 +01:00
|
|
|
last_pong_at_ = now - ping_disconnect_delay() + rtt();
|
|
|
|
last_read_at_ = now - read_disconnect_delay() + rtt();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-11-20 13:07:27 +01:00
|
|
|
last_pong_at_ = now;
|
|
|
|
last_read_at_ = now;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
last_ping_at_ = 0;
|
2023-09-21 16:52:33 +02:00
|
|
|
last_ping_message_id_ = {};
|
|
|
|
last_ping_container_message_id_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionConnection::do_close(Status status) {
|
|
|
|
state_ = Closed;
|
|
|
|
// NB: this could be destroyed after on_closed
|
|
|
|
callback_->on_closed(std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionConnection::send_crypto(const Storer &storer, uint64 quick_ack_token) {
|
|
|
|
CHECK(state_ != Closed);
|
2022-09-20 16:46:10 +02:00
|
|
|
last_write_size_ += raw_connection_->send_crypto(storer, auth_data_->get_session_id(),
|
|
|
|
auth_data_->get_server_salt(Time::now_cached()),
|
|
|
|
auth_data_->get_auth_key(), quick_ack_token);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
Result<MessageId> SessionConnection::send_query(BufferSlice buffer, bool gzip_flag, MessageId message_id,
|
|
|
|
vector<MessageId> invoke_after_message_ids, bool use_quick_ack) {
|
2019-02-12 22:47:48 +01:00
|
|
|
CHECK(mode_ != Mode::HttpLongPoll); // "LongPoll connection is only for http_wait"
|
2023-09-21 16:52:33 +02:00
|
|
|
if (message_id == MessageId()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
message_id = auth_data_->next_message_id(Time::now_cached());
|
|
|
|
}
|
|
|
|
auto seq_no = auth_data_->next_seq_no(true);
|
|
|
|
if (to_send_.empty()) {
|
|
|
|
send_before(Time::now_cached() + QUERY_DELAY);
|
|
|
|
}
|
2023-09-21 14:40:56 +02:00
|
|
|
to_send_.push_back(MtprotoQuery{message_id, seq_no, std::move(buffer), gzip_flag, std::move(invoke_after_message_ids),
|
|
|
|
use_quick_ack});
|
2023-09-21 16:52:33 +02:00
|
|
|
VLOG(mtproto) << "Invoke query with " << message_id << " and seq_no " << seq_no << " of size "
|
2023-09-21 14:40:56 +02:00
|
|
|
<< to_send_.back().packet.size() << " after " << invoke_after_message_ids
|
2023-09-08 23:13:42 +02:00
|
|
|
<< (use_quick_ack ? " with quick ack" : "");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return message_id;
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::get_state_info(MessageId message_id) {
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_get_state_info_message_ids_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_before(Time::now_cached());
|
|
|
|
}
|
2023-09-21 15:47:12 +02:00
|
|
|
to_get_state_info_message_ids_.push_back(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::resend_answer(MessageId message_id) {
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_resend_answer_message_ids_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_before(Time::now_cached() + RESEND_ANSWER_DELAY);
|
|
|
|
}
|
2023-09-21 15:47:12 +02:00
|
|
|
to_resend_answer_message_ids_.push_back(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 15:52:21 +02:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::cancel_answer(MessageId message_id) {
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_cancel_answer_message_ids_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_before(Time::now_cached() + RESEND_ANSWER_DELAY);
|
|
|
|
}
|
2023-09-21 15:47:12 +02:00
|
|
|
to_cancel_answer_message_ids_.push_back(message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
void SessionConnection::destroy_key() {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(INFO) << "Set need_destroy_auth_key to true";
|
2017-12-29 21:34:39 +01:00
|
|
|
need_destroy_auth_key_ = true;
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
std::pair<MessageId, BufferSlice> SessionConnection::encrypted_bind(int64 perm_key, int64 nonce, int32 expires_at) {
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 temp_key = auth_data_->get_tmp_auth_key().id();
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
mtproto_api::bind_auth_key_inner object(nonce, temp_key, perm_key, auth_data_->get_session_id(), expires_at);
|
2023-09-21 20:31:59 +02:00
|
|
|
auto object_storer = TLObjectStorer<mtproto_api::bind_auth_key_inner>(object);
|
2018-07-06 22:33:11 +02:00
|
|
|
auto size = object_storer.size();
|
|
|
|
auto object_packet = BufferWriter{size, 0, 0};
|
2023-02-02 15:40:47 +01:00
|
|
|
auto real_size = object_storer.store(object_packet.as_mutable_slice().ubegin());
|
2018-07-06 22:33:11 +02:00
|
|
|
CHECK(size == real_size);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-31 03:37:59 +01:00
|
|
|
MtprotoQuery query{
|
2022-01-28 15:06:54 +01:00
|
|
|
auth_data_->next_message_id(Time::now_cached()), 0, object_packet.as_buffer_slice(), false, {}, false};
|
2018-12-31 20:04:05 +01:00
|
|
|
PacketStorer<QueryImpl> query_storer(query, Slice());
|
|
|
|
|
2023-09-09 21:49:45 +02:00
|
|
|
const AuthKey &main_auth_key = auth_data_->get_main_auth_key();
|
2023-09-09 23:04:55 +02:00
|
|
|
PacketInfo packet_info;
|
|
|
|
packet_info.version = 1;
|
|
|
|
packet_info.no_crypto_flag = false;
|
|
|
|
packet_info.salt = Random::secure_int64();
|
|
|
|
packet_info.session_id = Random::secure_int64();
|
|
|
|
auto packet = Transport::write(query_storer, main_auth_key, &packet_info);
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::make_pair(query.message_id, packet.as_buffer_slice());
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:17:14 +02:00
|
|
|
void SessionConnection::force_ack() {
|
2023-09-21 15:47:12 +02:00
|
|
|
if (!to_ack_message_ids_.empty()) {
|
2022-05-03 15:17:14 +02:00
|
|
|
send_before(Time::now_cached());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
void SessionConnection::send_ack(MessageId message_id) {
|
|
|
|
VLOG(mtproto) << "Send ack for " << message_id;
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_ack_message_ids_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_before(Time::now_cached() + ACK_DELAY);
|
|
|
|
}
|
2023-02-26 11:41:49 +01:00
|
|
|
// an easiest way to eliminate duplicated acknowledgements for gzipped packets
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_ack_message_ids_.empty() || to_ack_message_ids_.back() != message_id) {
|
|
|
|
to_ack_message_ids_.push_back(message_id);
|
2020-09-02 14:40:47 +02:00
|
|
|
|
|
|
|
constexpr size_t MAX_UNACKED_PACKETS = 100;
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_ack_message_ids_.size() >= MAX_UNACKED_PACKETS) {
|
2020-09-02 14:40:47 +02:00
|
|
|
send_before(Time::now_cached());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
// don't send ping in poll mode
|
2018-12-31 20:04:05 +01:00
|
|
|
bool SessionConnection::may_ping() const {
|
|
|
|
return last_ping_at_ == 0 || (mode_ != Mode::HttpLongPoll && last_ping_at_ + ping_may_delay() < Time::now_cached());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SessionConnection::must_ping() const {
|
|
|
|
return last_ping_at_ == 0 || (mode_ != Mode::HttpLongPoll && last_ping_at_ + ping_must_delay() < Time::now_cached());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionConnection::flush_packet() {
|
|
|
|
bool has_salt = auth_data_->has_salt(Time::now_cached());
|
|
|
|
// ping
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId container_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 ping_id = 0;
|
|
|
|
if (has_salt && may_ping()) {
|
|
|
|
ping_id = ++cur_ping_id_;
|
|
|
|
last_ping_at_ = Time::now_cached();
|
|
|
|
}
|
|
|
|
|
|
|
|
// http_wait
|
|
|
|
int max_delay = -1;
|
|
|
|
int max_after = -1;
|
|
|
|
int max_wait = -1;
|
|
|
|
if (mode_ == Mode::HttpLongPoll) {
|
|
|
|
max_delay = HTTP_MAX_DELAY;
|
|
|
|
max_after = HTTP_MAX_AFTER;
|
2018-11-20 13:07:27 +01:00
|
|
|
auto time_to_disconnect =
|
2019-02-22 21:15:43 +01:00
|
|
|
min(ping_disconnect_delay() + last_pong_at_, read_disconnect_delay() + last_read_at_) - Time::now_cached();
|
2021-03-28 21:33:22 +02:00
|
|
|
max_wait = static_cast<int>(1000 * clamp(time_to_disconnect - rtt(), 0.1, http_max_wait()));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (mode_ == Mode::Http) {
|
|
|
|
max_delay = HTTP_MAX_DELAY;
|
|
|
|
max_after = HTTP_MAX_AFTER;
|
|
|
|
max_wait = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// future salts
|
|
|
|
int future_salt_n = 0;
|
|
|
|
if (mode_ != Mode::HttpLongPoll) {
|
2018-09-04 16:01:01 +02:00
|
|
|
if (auth_data_->need_future_salts(Time::now_cached()) &&
|
|
|
|
(last_get_future_salt_at_ == 0 || last_get_future_salt_at_ + 60 < Time::now_cached())) {
|
2018-12-31 20:04:05 +01:00
|
|
|
last_get_future_salt_at_ = Time::now_cached();
|
|
|
|
future_salt_n = 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 20:22:33 +02:00
|
|
|
static constexpr size_t MAX_QUERY_COUNT = 1000;
|
2021-10-18 18:26:14 +02:00
|
|
|
size_t send_till = 0;
|
|
|
|
size_t send_size = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (has_salt) {
|
2023-09-21 20:22:33 +02:00
|
|
|
// send at most MAX_QUERY_COUNT queries, of total size up to 2^15
|
|
|
|
while (send_till < to_send_.size() && send_till < MAX_QUERY_COUNT && send_size < (1 << 15)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_size += to_send_[send_till].packet.size();
|
|
|
|
send_till++;
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 12:36:47 +02:00
|
|
|
vector<MtprotoQuery> queries;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (send_till == to_send_.size()) {
|
|
|
|
queries = std::move(to_send_);
|
|
|
|
} else if (send_till != 0) {
|
|
|
|
queries.reserve(send_till);
|
|
|
|
std::move(to_send_.begin(), to_send_.begin() + send_till, std::back_inserter(queries));
|
|
|
|
to_send_.erase(to_send_.begin(), to_send_.begin() + send_till);
|
|
|
|
}
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
bool destroy_auth_key = need_destroy_auth_key_ && !sent_destroy_auth_key_;
|
|
|
|
|
2023-09-21 15:47:12 +02:00
|
|
|
if (queries.empty() && to_ack_message_ids_.empty() && ping_id == 0 && max_delay < 0 && future_salt_n == 0 &&
|
|
|
|
to_resend_answer_message_ids_.empty() && to_cancel_answer_message_ids_.empty() &&
|
|
|
|
to_get_state_info_message_ids_.empty() && !destroy_auth_key) {
|
2018-12-31 20:04:05 +01:00
|
|
|
force_send_at_ = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-23 12:02:04 +02:00
|
|
|
if (destroy_auth_key && !sent_destroy_auth_key_) {
|
|
|
|
sent_destroy_auth_key_ = true;
|
|
|
|
destroy_auth_key_send_time_ = Time::now();
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
|
2023-09-21 15:47:12 +02:00
|
|
|
VLOG(mtproto) << "Sent packet: " << tag("query_count", queries.size()) << tag("ack_count", to_ack_message_ids_.size())
|
2018-12-31 20:04:05 +01:00
|
|
|
<< tag("ping", ping_id != 0) << tag("http_wait", max_delay >= 0)
|
2023-09-21 15:47:12 +02:00
|
|
|
<< tag("future_salt", future_salt_n > 0) << tag("get_info", to_get_state_info_message_ids_.size())
|
|
|
|
<< tag("resend", to_resend_answer_message_ids_.size())
|
|
|
|
<< tag("cancel", to_cancel_answer_message_ids_.size()) << tag("destroy_key", destroy_auth_key)
|
|
|
|
<< tag("auth_key_id", auth_data_->get_auth_key().id());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
auto cut_tail = [](vector<MessageId> &message_ids, size_t size, Slice name) {
|
|
|
|
if (size >= message_ids.size()) {
|
|
|
|
auto result = transform(message_ids, [](MessageId message_id) { return static_cast<int64>(message_id.get()); });
|
|
|
|
message_ids.clear();
|
2021-08-22 12:36:47 +02:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
LOG(WARNING) << "Too many message identifiers in container " << name << ": " << message_ids.size() << " instead of "
|
|
|
|
<< size;
|
|
|
|
auto new_size = message_ids.size() - size;
|
2023-09-21 15:47:12 +02:00
|
|
|
vector<int64> result(size);
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
2023-09-21 16:52:33 +02:00
|
|
|
result[i] = static_cast<int64>(message_ids[i + new_size].get());
|
2023-09-21 15:47:12 +02:00
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
message_ids.resize(new_size);
|
2021-08-22 12:36:47 +02:00
|
|
|
return result;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2023-04-03 00:24:43 +02:00
|
|
|
// no more than 8192 message identifiers per container..
|
2023-09-21 15:47:12 +02:00
|
|
|
auto to_resend_answer = cut_tail(to_resend_answer_message_ids_, 8192, "resend_answer");
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId resend_answer_message_id;
|
2023-09-21 20:22:33 +02:00
|
|
|
CHECK(queries.size() <= MAX_QUERY_COUNT);
|
|
|
|
auto to_cancel_answer = cut_tail(to_cancel_answer_message_ids_, MAX_QUERY_COUNT - queries.size(), "cancel_answer");
|
2023-09-21 15:47:12 +02:00
|
|
|
auto to_get_state_info = cut_tail(to_get_state_info_message_ids_, 8192, "get_state_info");
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId get_state_info_message_id;
|
2023-09-21 15:47:12 +02:00
|
|
|
auto to_ack = cut_tail(to_ack_message_ids_, 8192, "ack");
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId ping_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-10-21 19:41:31 +02:00
|
|
|
bool use_quick_ack = any_of(queries, [](const auto &query) { return query.use_quick_ack; });
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
{
|
2022-02-07 20:05:01 +01:00
|
|
|
// LOG(ERROR) << (auth_data_->get_header().empty() ? '-' : '+');
|
2023-09-21 16:52:33 +02:00
|
|
|
MessageId parent_message_id;
|
2023-09-03 00:47:50 +02:00
|
|
|
auto storer = PacketStorer<CryptoImpl>(
|
|
|
|
queries, auth_data_->get_header(), std::move(to_ack), ping_id, static_cast<int>(ping_disconnect_delay() + 2.0),
|
|
|
|
max_delay, max_after, max_wait, future_salt_n, to_get_state_info, to_resend_answer, to_cancel_answer,
|
2023-09-21 14:44:26 +02:00
|
|
|
destroy_auth_key, auth_data_, &container_message_id, &get_state_info_message_id, &resend_answer_message_id,
|
2023-09-03 00:47:50 +02:00
|
|
|
&ping_message_id, &parent_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
auto quick_ack_token = use_quick_ack ? parent_message_id.get() : 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
send_crypto(storer, quick_ack_token);
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
if (resend_answer_message_id != MessageId()) {
|
2023-09-21 14:44:26 +02:00
|
|
|
service_queries_.emplace(resend_answer_message_id, ServiceQuery{ServiceQuery::ResendAnswer, container_message_id,
|
|
|
|
std::move(to_resend_answer)});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
if (get_state_info_message_id != MessageId()) {
|
2023-09-21 14:44:26 +02:00
|
|
|
service_queries_.emplace(get_state_info_message_id, ServiceQuery{ServiceQuery::GetStateInfo, container_message_id,
|
|
|
|
std::move(to_get_state_info)});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (ping_id != 0) {
|
2023-09-21 14:44:26 +02:00
|
|
|
last_ping_container_message_id_ = container_message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
last_ping_message_id_ = ping_message_id;
|
|
|
|
}
|
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
if (container_message_id != MessageId()) {
|
|
|
|
auto message_ids = transform(queries, [](const MtprotoQuery &x) { return x.message_id; });
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// some acks may be lost here. Nobody will resend them if something goes wrong with query.
|
|
|
|
// It is mostly problem for server. We will just drop this answers in next connection
|
|
|
|
//
|
|
|
|
// get future salt too.
|
|
|
|
// So I will re-ask salt if have no answer in 60 second.
|
2023-09-21 14:44:26 +02:00
|
|
|
callback_->on_container_sent(container_message_id, std::move(message_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-09-21 16:52:33 +02:00
|
|
|
if (resend_answer_message_id != MessageId()) {
|
2023-09-21 15:52:21 +02:00
|
|
|
container_to_service_message_id_[container_message_id].push_back(resend_answer_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 16:52:33 +02:00
|
|
|
if (get_state_info_message_id != MessageId()) {
|
2023-09-21 15:52:21 +02:00
|
|
|
container_to_service_message_id_[container_message_id].push_back(get_state_info_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:47:12 +02:00
|
|
|
if (to_send_.empty() && to_ack_message_ids_.empty() && to_get_state_info_message_ids_.empty() &&
|
|
|
|
to_resend_answer_message_ids_.empty() && to_cancel_answer_message_ids_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
force_send_at_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionConnection::send_before(double tm) {
|
|
|
|
if (force_send_at_ == 0 || force_send_at_ > tm) {
|
|
|
|
force_send_at_ = tm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SessionConnection::do_flush() {
|
2021-10-24 12:23:47 +02:00
|
|
|
LOG_CHECK(raw_connection_) << was_moved_ << ' ' << state_ << ' ' << static_cast<int32>(mode_) << ' '
|
|
|
|
<< connected_flag_ << ' ' << is_main_ << ' ' << need_destroy_auth_key_ << ' '
|
|
|
|
<< sent_destroy_auth_key_ << ' ' << callback_ << ' ' << (Time::now() - created_at_) << ' '
|
|
|
|
<< (Time::now() - last_read_at_);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(state_ != Closed);
|
|
|
|
if (state_ == Init) {
|
|
|
|
TRY_STATUS(init());
|
|
|
|
}
|
|
|
|
if (!auth_data_->has_auth_key(Time::now_cached())) {
|
|
|
|
return Status::Error("No auth key");
|
|
|
|
}
|
|
|
|
|
2022-09-20 16:46:10 +02:00
|
|
|
last_read_size_ = 0;
|
|
|
|
last_write_size_ = 0;
|
|
|
|
auto start_time = Time::now();
|
|
|
|
auto result = raw_connection_->flush(auth_data_->get_auth_key(), *this);
|
|
|
|
auto elapsed_time = Time::now() - start_time;
|
2022-09-23 21:29:18 +02:00
|
|
|
if (elapsed_time >= 0.1) {
|
2023-07-22 13:06:57 +02:00
|
|
|
LOG(WARNING) << "RawConnection::flush took " << elapsed_time << " seconds, written " << last_write_size_
|
|
|
|
<< " bytes, read " << last_read_size_ << " bytes and returned " << result;
|
2022-09-20 16:46:10 +02:00
|
|
|
}
|
|
|
|
if (result.is_error()) {
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-11-20 18:52:42 +01:00
|
|
|
if (last_pong_at_ + ping_disconnect_delay() < Time::now_cached()) {
|
2019-03-13 02:00:45 +01:00
|
|
|
auto stats_callback = raw_connection_->stats_callback();
|
|
|
|
if (stats_callback != nullptr) {
|
|
|
|
stats_callback->on_error();
|
|
|
|
}
|
2023-03-14 13:45:01 +01:00
|
|
|
return Status::Error(PSLICE() << "Ping timeout of " << ping_disconnect_delay()
|
|
|
|
<< " seconds expired; last pong was received " << (Time::now() - real_last_pong_at_)
|
|
|
|
<< " seconds ago");
|
2018-11-20 13:07:27 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 18:52:42 +01:00
|
|
|
if (last_read_at_ + read_disconnect_delay() < Time::now_cached()) {
|
2019-03-13 02:00:45 +01:00
|
|
|
auto stats_callback = raw_connection_->stats_callback();
|
|
|
|
if (stats_callback != nullptr) {
|
|
|
|
stats_callback->on_error();
|
|
|
|
}
|
2023-03-14 13:45:01 +01:00
|
|
|
return Status::Error(PSLICE() << "Read timeout of " << read_disconnect_delay() << " seconds expired; last read was "
|
|
|
|
<< (Time::now() - real_last_read_at_) << " seconds ago");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
double SessionConnection::flush(SessionConnection::Callback *callback) {
|
|
|
|
callback_ = callback;
|
|
|
|
auto status = do_flush();
|
|
|
|
// check error
|
|
|
|
if (status.is_error()) {
|
|
|
|
do_close(std::move(status));
|
2023-04-20 16:19:38 +02:00
|
|
|
LOG(DEBUG) << "Close session because of an error";
|
2018-12-31 20:04:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:24:30 +02:00
|
|
|
double wakeup_at = 0;
|
2023-03-08 16:10:07 +01:00
|
|
|
// three independent timeouts
|
|
|
|
// 1. close connection after ping_disconnect_delay() after last pong
|
|
|
|
// 2. close connection after read_disconnect_delay() after last read
|
|
|
|
// 3. the one returned by must_flush_packet
|
2023-04-20 16:24:30 +02:00
|
|
|
relax_timeout_at(&wakeup_at, last_pong_at_ + ping_disconnect_delay() + 0.002);
|
|
|
|
relax_timeout_at(&wakeup_at, last_read_at_ + read_disconnect_delay() + 0.002);
|
|
|
|
relax_timeout_at(&wakeup_at, flush_packet_at_);
|
2023-03-08 16:10:07 +01:00
|
|
|
|
|
|
|
auto now = Time::now();
|
2023-03-14 13:45:01 +01:00
|
|
|
LOG(DEBUG) << "Last pong was in " << (now - last_pong_at_) << '/' << (now - real_last_pong_at_)
|
|
|
|
<< ", last read was in " << (now - last_read_at_) << '/' << (now - real_last_read_at_)
|
2023-03-08 16:10:07 +01:00
|
|
|
<< ", RTT = " << rtt() << ", ping timeout = " << ping_disconnect_delay()
|
2023-04-20 16:19:38 +02:00
|
|
|
<< ", read timeout = " << read_disconnect_delay() << ", flush packet in " << (flush_packet_at_ - now)
|
2023-04-20 16:24:30 +02:00
|
|
|
<< ", wakeup in " << (wakeup_at - now);
|
2023-03-08 16:10:07 +01:00
|
|
|
|
2023-04-20 16:24:30 +02:00
|
|
|
return wakeup_at;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionConnection::force_close(SessionConnection::Callback *callback) {
|
|
|
|
CHECK(state_ != Closed);
|
|
|
|
callback_ = callback;
|
|
|
|
do_close(Status::OK());
|
|
|
|
}
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|