2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthKey.h"
|
|
|
|
#include "td/mtproto/Handshake.h"
|
|
|
|
#include "td/mtproto/NoCryptoStorer.h"
|
2019-01-31 03:23:40 +01:00
|
|
|
#include "td/mtproto/PacketInfo.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/mtproto/PacketStorer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/RawConnection.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2019-02-12 17:48:52 +01:00
|
|
|
#include "td/utils/StorerBase.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class HandshakeConnection
|
|
|
|
: private RawConnection::Callback
|
|
|
|
, private AuthKeyHandshake::Callback {
|
|
|
|
public:
|
2018-09-27 03:19:03 +02:00
|
|
|
HandshakeConnection(unique_ptr<RawConnection> raw_connection, AuthKeyHandshake *handshake,
|
|
|
|
unique_ptr<AuthKeyHandshakeContext> context)
|
2018-12-31 20:04:05 +01:00
|
|
|
: raw_connection_(std::move(raw_connection)), handshake_(handshake), context_(std::move(context)) {
|
|
|
|
handshake_->resume(this);
|
|
|
|
}
|
|
|
|
|
2018-08-14 09:42:40 +02:00
|
|
|
PollableFdInfo &get_poll_info() {
|
|
|
|
return raw_connection_->get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<RawConnection> move_as_raw_connection() {
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::move(raw_connection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
raw_connection_->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status flush() {
|
|
|
|
auto status = raw_connection_->flush(AuthKey(), *this);
|
|
|
|
if (status.code() == -404) {
|
|
|
|
LOG(WARNING) << "Clear handshake " << tag("error", status);
|
|
|
|
handshake_->clear();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<RawConnection> raw_connection_;
|
2018-12-31 20:04:05 +01:00
|
|
|
AuthKeyHandshake *handshake_;
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<AuthKeyHandshakeContext> context_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void send_no_crypto(const Storer &storer) override {
|
|
|
|
raw_connection_->send_no_crypto(PacketStorer<NoCryptoImpl>(0, storer));
|
|
|
|
}
|
|
|
|
|
|
|
|
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) override {
|
|
|
|
if (packet_info.no_crypto_flag == false) {
|
|
|
|
return Status::Error("Expected not encrypted packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip header
|
|
|
|
if (packet.size() < 12) {
|
|
|
|
return Status::Error("Result is too small");
|
|
|
|
}
|
|
|
|
packet.confirm_read(12);
|
|
|
|
|
2018-06-15 20:11:07 +02:00
|
|
|
auto fixed_packet_size = packet.size() & ~3; // remove some padded data
|
|
|
|
TRY_STATUS(handshake_->on_message(packet.as_slice().truncate(fixed_packet_size), this, context_.get()));
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
};
|
2019-01-31 03:05:40 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|