tdesktop/Telegram/SourceFiles/mtproto/connection_abstract.h

214 lines
5.6 KiB
C
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "mtproto/mtproto_dc_options.h"
2019-11-13 14:12:04 +00:00
#include "mtproto/mtproto_proxy_data.h"
#include "base/bytes.h"
2019-11-13 14:12:04 +00:00
#include <QtCore/QObject>
#include <QtCore/QThread>
2022-11-01 16:57:36 +00:00
#include <deque>
namespace MTP {
2019-11-13 08:31:12 +00:00
class Instance;
2019-12-02 13:10:19 +00:00
namespace details {
struct ConnectionOptions;
class AbstractConnection;
2022-02-07 11:24:02 +00:00
inline constexpr auto kTestModeDcIdShift = 10000;
class ConnectionPointer {
public:
ConnectionPointer();
ConnectionPointer(std::nullptr_t);
ConnectionPointer(ConnectionPointer &&other);
ConnectionPointer &operator=(ConnectionPointer &&other);
template <typename ConnectionType, typename ...Args>
static ConnectionPointer New(Args &&...args) {
return ConnectionPointer(new ConnectionType(
std::forward<Args>(args)...
));
}
AbstractConnection *get() const;
void reset(AbstractConnection *value = nullptr);
operator AbstractConnection*() const;
AbstractConnection *operator->() const;
AbstractConnection &operator*() const;
explicit operator bool() const;
~ConnectionPointer();
private:
explicit ConnectionPointer(AbstractConnection *value);
AbstractConnection *_value = nullptr;
};
class AbstractConnection : public QObject {
Q_OBJECT
public:
AbstractConnection(QThread *thread, const ProxyData &proxy);
AbstractConnection(const AbstractConnection &other) = delete;
AbstractConnection &operator=(const AbstractConnection &other) = delete;
virtual ~AbstractConnection() = default;
// virtual constructor
2019-11-13 14:12:04 +00:00
[[nodiscard]] static ConnectionPointer Create(
not_null<Instance*> instance,
DcOptions::Variants::Protocol protocol,
QThread *thread,
const bytes::vector &secret,
const ProxyData &proxy);
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual ConnectionPointer clone(const ProxyData &proxy) = 0;
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual crl::time pingTime() const = 0;
[[nodiscard]] virtual crl::time fullConnectTimeout() const = 0;
virtual void sendData(mtpBuffer &&buffer) = 0;
virtual void disconnectFromServer() = 0;
virtual void connectToServer(
const QString &ip,
int port,
const bytes::vector &protocolSecret,
int16 protocolDcId,
bool protocolForFiles) = 0;
virtual void timedOut() {
}
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual bool isConnected() const = 0;
[[nodiscard]] virtual bool usingHttpWait() {
return false;
}
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual bool needHttpWait() {
return false;
}
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual int32 debugState() const = 0;
2019-11-13 14:12:04 +00:00
[[nodiscard]] virtual QString transport() const = 0;
[[nodiscard]] virtual QString tag() const = 0;
void setSentEncryptedWithKeyId(uint64 keyId) {
_sentEncryptedWithKeyId = keyId;
}
[[nodiscard]] uint64 sentEncryptedWithKeyId() const {
return _sentEncryptedWithKeyId;
}
using BuffersQueue = std::deque<mtpBuffer>;
2019-11-13 14:12:04 +00:00
[[nodiscard]] BuffersQueue &received() {
return _receivedQueue;
}
template <typename Request>
2019-11-13 14:12:04 +00:00
[[nodiscard]] mtpBuffer prepareNotSecurePacket(
const Request &request,
mtpMsgId newId) const;
2019-11-13 14:12:04 +00:00
[[nodiscard]] mtpBuffer prepareSecurePacket(
uint64 keyId,
MTPint128 msgKey,
uint32 size) const;
2019-11-13 14:12:04 +00:00
[[nodiscard]] gsl::span<const mtpPrime> parseNotSecureResponse(
const mtpBuffer &buffer) const;
2022-02-07 11:24:02 +00:00
[[nodiscard]] static QString ProtocolDcDebugId(int16 protocolDcId);
[[nodiscard]] QString debugId() const {
return _debugId;
}
void logInfo(const QString &message);
void logError(const QString &message);
// Used to emit error(...) with no real code from the server.
static constexpr auto kErrorCodeOther = -499;
2021-03-03 18:22:42 +00:00
Q_SIGNALS:
void receivedData();
void receivedSome(); // to stop restart timer
void error(qint32 errorCodebool);
void connected();
void disconnected();
void syncTimeRequest();
protected:
BuffersQueue _receivedQueue; // list of received packets, not processed yet
int _pingTime = 0;
ProxyData _proxy;
2022-02-07 11:24:02 +00:00
QString _debugId;
// 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
2019-11-13 14:12:04 +00:00
[[nodiscard]] mtpBuffer preparePQFake(const MTPint128 &nonce) const;
[[nodiscard]] std::optional<MTPResPQ> readPQFakeReply(
const mtpBuffer &buffer) const;
private:
[[nodiscard]] uint32 extendedNotSecurePadding() const;
uint64 _sentEncryptedWithKeyId = 0;
};
template <typename Request>
mtpBuffer AbstractConnection::prepareNotSecurePacket(
const Request &request,
mtpMsgId newId) const {
2019-11-05 10:23:09 +00:00
const auto intsSize = tl::count_length(request) >> 2;
2019-11-13 14:12:04 +00:00
const auto intsPadding = extendedNotSecurePadding();
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];
*reinterpret_cast<mtpMsgId*>(messageId) = newId;
request.write(result);
const auto messageLength = messageId + kMessageIdInts;
*messageLength = (result.size() - kPrefixInts + intsPadding) << 2;
if (intsPadding > 0) {
2019-11-13 14:12:04 +00:00
const auto skipPrimes = result.size();
result.resize(skipPrimes + intsPadding);
const auto skipBytes = skipPrimes * sizeof(mtpPrime);
bytes::set_random(bytes::make_span(result).subspan(skipBytes));
}
return result;
}
2022-02-07 11:24:02 +00:00
#define CONNECTION_LOG_INFO(x) if (Logs::DebugEnabled()) { logInfo(x); }
#define CONNECTION_LOG_ERROR(x) if (Logs::DebugEnabled()) { logError(x); }
2019-12-02 13:10:19 +00:00
} // namespace details
} // namespace MTP