2016-03-24 12:57:10 +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.
|
2016-03-24 12:57:10 +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
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
#include "mtproto/dc_options.h"
|
2018-04-24 19:09:20 +00:00
|
|
|
#include "base/bytes.h"
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
2018-04-24 08:46:27 +00:00
|
|
|
struct ConnectionOptions;
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
class AbstractConnection;
|
|
|
|
|
|
|
|
class ConnectionPointer {
|
|
|
|
public:
|
|
|
|
ConnectionPointer();
|
|
|
|
ConnectionPointer(std::nullptr_t);
|
|
|
|
ConnectionPointer(ConnectionPointer &&other);
|
|
|
|
ConnectionPointer &operator=(ConnectionPointer &&other);
|
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
template <typename ConnectionType, typename ...Args>
|
|
|
|
static ConnectionPointer New(Args &&...args) {
|
|
|
|
return ConnectionPointer(new ConnectionType(
|
|
|
|
std::forward<Args>(args)...
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *get() const;
|
|
|
|
void reset(AbstractConnection *value = nullptr);
|
|
|
|
operator AbstractConnection*() const;
|
|
|
|
AbstractConnection *operator->() const;
|
|
|
|
AbstractConnection &operator*() const;
|
|
|
|
explicit operator bool() const;
|
|
|
|
|
|
|
|
~ConnectionPointer();
|
|
|
|
|
|
|
|
private:
|
2018-05-17 19:58:00 +00:00
|
|
|
explicit ConnectionPointer(AbstractConnection *value);
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
AbstractConnection *_value = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
class AbstractConnection : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2019-07-05 07:54:53 +00:00
|
|
|
AbstractConnection(QThread *thread, const ProxyData &proxy);
|
2016-03-24 12:57:10 +00:00
|
|
|
AbstractConnection(const AbstractConnection &other) = delete;
|
|
|
|
AbstractConnection &operator=(const AbstractConnection &other) = delete;
|
2018-06-25 18:55:27 +00:00
|
|
|
virtual ~AbstractConnection() = default;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// virtual constructor
|
2018-05-17 19:58:00 +00:00
|
|
|
static ConnectionPointer Create(
|
|
|
|
not_null<Instance*> instance,
|
2018-04-24 19:09:20 +00:00
|
|
|
DcOptions::Variants::Protocol protocol,
|
2018-05-17 19:58:00 +00:00
|
|
|
QThread *thread,
|
2019-07-05 07:54:53 +00:00
|
|
|
const bytes::vector &secret,
|
2018-05-17 19:58:00 +00:00
|
|
|
const ProxyData &proxy);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
virtual ConnectionPointer clone(const ProxyData &proxy) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
virtual crl::time pingTime() const = 0;
|
|
|
|
virtual crl::time fullConnectTimeout() const = 0;
|
2018-06-25 18:55:27 +00:00
|
|
|
virtual void sendData(mtpBuffer &&buffer) = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
virtual void disconnectFromServer() = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
virtual void connectToServer(
|
|
|
|
const QString &ip,
|
|
|
|
int port,
|
|
|
|
const bytes::vector &protocolSecret,
|
|
|
|
int16 protocolDcId) = 0;
|
2019-07-10 15:03:48 +00:00
|
|
|
virtual void timedOut() {
|
|
|
|
}
|
2016-03-24 12:57:10 +00:00
|
|
|
virtual bool isConnected() const = 0;
|
|
|
|
virtual bool usingHttpWait() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual bool needHttpWait() {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-25 18:55:27 +00:00
|
|
|
virtual bool requiresExtendedPadding() const {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
virtual int32 debugState() const = 0;
|
|
|
|
|
|
|
|
virtual QString transport() const = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
virtual QString tag() const = 0;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-05-17 19:58:00 +00:00
|
|
|
void setSentEncrypted() {
|
|
|
|
_sentEncrypted = true;
|
|
|
|
}
|
|
|
|
|
2017-02-27 09:51:03 +00:00
|
|
|
using BuffersQueue = std::deque<mtpBuffer>;
|
2016-03-24 12:57:10 +00:00
|
|
|
BuffersQueue &received() {
|
2017-02-27 09:51:03 +00:00
|
|
|
return _receivedQueue;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
template <typename Request>
|
2019-07-10 17:28:33 +00:00
|
|
|
mtpBuffer prepareNotSecurePacket(
|
|
|
|
const Request &request,
|
|
|
|
mtpMsgId newId) const;
|
2018-06-25 18:55:27 +00:00
|
|
|
mtpBuffer prepareSecurePacket(
|
|
|
|
uint64 keyId,
|
|
|
|
MTPint128 msgKey,
|
|
|
|
uint32 size) const;
|
|
|
|
|
2018-06-25 21:18:27 +00:00
|
|
|
gsl::span<const mtpPrime> parseNotSecureResponse(
|
|
|
|
const mtpBuffer &buffer) const;
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// Used to emit error(...) with no real code from the server.
|
|
|
|
static constexpr auto kErrorCodeOther = -499;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
signals:
|
2016-03-24 12:57:10 +00:00
|
|
|
void receivedData();
|
|
|
|
void receivedSome(); // to stop restart timer
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
void error(qint32 errorCodebool);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
void connected();
|
|
|
|
void disconnected();
|
|
|
|
|
2019-07-10 15:03:48 +00:00
|
|
|
void syncTimeRequest();
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
protected:
|
2017-02-27 09:51:03 +00:00
|
|
|
BuffersQueue _receivedQueue; // list of received packets, not processed yet
|
2018-04-30 15:49:03 +00:00
|
|
|
bool _sentEncrypted = false;
|
|
|
|
int _pingTime = 0;
|
2018-05-17 19:58:00 +00:00
|
|
|
ProxyData _proxy;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
// first we always send fake MTPReq_pq to see if connection works at all
|
|
|
|
// we send them simultaneously through TCP/HTTP/IPv4/IPv6 to choose the working one
|
2018-06-25 18:55:27 +00:00
|
|
|
mtpBuffer preparePQFake(const MTPint128 &nonce) const;
|
2019-07-18 14:06:38 +00:00
|
|
|
std::optional<MTPResPQ> readPQFakeReply(const mtpBuffer &buffer) const;
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-06-25 18:55:27 +00:00
|
|
|
template <typename Request>
|
2019-07-10 17:28:33 +00:00
|
|
|
mtpBuffer AbstractConnection::prepareNotSecurePacket(
|
|
|
|
const Request &request,
|
|
|
|
mtpMsgId newId) const {
|
2018-06-25 18:55:27 +00:00
|
|
|
const auto intsSize = request.innerLength() >> 2;
|
|
|
|
const auto intsPadding = requiresExtendedPadding()
|
|
|
|
? uint32(rand_value<uchar>() & 0x3F)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
auto result = mtpBuffer();
|
|
|
|
constexpr auto kTcpPrefixInts = 2;
|
|
|
|
constexpr auto kAuthKeyIdInts = 2;
|
|
|
|
constexpr auto kMessageIdInts = 2;
|
|
|
|
constexpr auto kMessageLengthInts = 1;
|
|
|
|
constexpr auto kPrefixInts = kTcpPrefixInts
|
|
|
|
+ kAuthKeyIdInts
|
|
|
|
+ kMessageIdInts
|
|
|
|
+ kMessageLengthInts;
|
|
|
|
constexpr auto kTcpPostfixInts = 4;
|
|
|
|
|
|
|
|
result.reserve(kPrefixInts + intsSize + intsPadding + kTcpPostfixInts);
|
|
|
|
result.resize(kPrefixInts);
|
|
|
|
|
|
|
|
const auto messageId = &result[kTcpPrefixInts + kAuthKeyIdInts];
|
2019-07-10 17:28:33 +00:00
|
|
|
*reinterpret_cast<mtpMsgId*>(messageId) = newId;
|
2018-06-25 18:55:27 +00:00
|
|
|
|
|
|
|
request.write(result);
|
|
|
|
|
|
|
|
const auto messageLength = messageId + kMessageIdInts;
|
|
|
|
*messageLength = (result.size() - kPrefixInts + intsPadding) << 2;
|
|
|
|
|
|
|
|
if (intsPadding > 0) {
|
|
|
|
result.resize(result.size() + intsPadding);
|
|
|
|
memset_rand(
|
|
|
|
result.data() + result.size() - intsPadding,
|
|
|
|
intsPadding * sizeof(mtpPrime));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-24 12:57:10 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|