Skip to content

Commit

Permalink
Don't try to continue Handshake if timeout expired.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Jul 22, 2021
1 parent 7f61725 commit 0b74d17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion td/mtproto/Handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ static Result<typename T::ReturnType> fetch_result(Slice message, bool check_end
}

AuthKeyHandshake::AuthKeyHandshake(int32 dc_id, int32 expires_in)
: mode_(expires_in == 0 ? Mode::Main : Mode::Temp), dc_id_(dc_id), expires_in_(expires_in) {
: mode_(expires_in == 0 ? Mode::Main : Mode::Temp)
, dc_id_(dc_id)
, expires_in_(expires_in)
, timeout_at_(Time::now() + 1e9) {
}

void AuthKeyHandshake::set_timeout_in(double timeout_in) {
timeout_at_ = Time::now() + timeout_in;
}

void AuthKeyHandshake::clear() {
last_query_ = BufferSlice();
state_ = Start;
timeout_at_ = Time::now() + 1e9;
}

bool AuthKeyHandshake::is_ready_for_finish() const {
Expand Down Expand Up @@ -294,6 +302,10 @@ Status AuthKeyHandshake::on_start(Callback *connection) {

Status AuthKeyHandshake::on_message(Slice message, Callback *connection, AuthKeyHandshakeContext *context) {
Status status = [&] {
if (Time::now() >= timeout_at_) {
return Status::Error("Handshake timeout expired");
}

switch (state_) {
case ResPQ:
return on_res_pq(message, connection, context->get_public_rsa_key_interface());
Expand Down
4 changes: 4 additions & 0 deletions td/mtproto/Handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class AuthKeyHandshake {

AuthKeyHandshake(int32 dc_id, int32 expires_in);

void set_timeout_in(double timeout_in);

bool is_ready_for_finish() const;

void on_finish();
Expand Down Expand Up @@ -80,6 +82,8 @@ class AuthKeyHandshake {
int32 expires_in_ = 0;
double expires_at_ = 0;

double timeout_at_ = 0;

AuthKey auth_key_;
double server_time_diff_ = 0;
uint64 server_salt_ = 0;
Expand Down
1 change: 1 addition & 0 deletions td/mtproto/HandshakeActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void HandshakeActor::close() {
void HandshakeActor::start_up() {
Scheduler::subscribe(connection_->get_poll_info().extract_pollable_fd(this));
set_timeout_in(timeout_);
handshake_->set_timeout_in(timeout_);
yield();
}

Expand Down

0 comments on commit 0b74d17

Please sign in to comment.