Nagram/TMessagesProj/jni/tgnet/Datacenter.h

151 lines
5.8 KiB
C
Raw Normal View History

2015-09-24 20:52:02 +00:00
/*
2018-07-30 02:07:02 +00:00
* This is the source code of tgnet library v. 1.1
2015-09-24 20:52:02 +00:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2018-07-30 02:07:02 +00:00
* Copyright Nikolai Kudashov, 2015-2018.
2015-09-24 20:52:02 +00:00
*/
#ifndef DATACENTER_H
#define DATACENTER_H
#include <stdint.h>
#include <vector>
#include <map>
#include <bits/unique_ptr.h>
#include "Defines.h"
class TL_future_salt;
class Connection;
class NativeByteBuffer;
class TL_future_salt;
2017-07-08 16:32:04 +00:00
class TL_help_configSimple;
2015-09-24 20:52:02 +00:00
class ByteArray;
class TLObject;
class Config;
2018-07-30 02:07:02 +00:00
class Handshake;
2015-09-24 20:52:02 +00:00
2018-07-30 02:07:02 +00:00
class Datacenter : public HandshakeDelegate {
2015-09-24 20:52:02 +00:00
public:
2018-07-30 02:07:02 +00:00
Datacenter(int32_t instance, uint32_t id);
Datacenter(int32_t instance, NativeByteBuffer *data);
2015-09-24 20:52:02 +00:00
uint32_t getDatacenterId();
2018-07-30 02:07:02 +00:00
TcpAddress *getCurrentAddress(uint32_t flags);
2015-09-24 20:52:02 +00:00
int32_t getCurrentPort(uint32_t flags);
2018-07-30 02:07:02 +00:00
void addAddressAndPort(std::string address, uint32_t port, uint32_t flags, std::string secret);
2015-09-24 20:52:02 +00:00
void nextAddressOrPort(uint32_t flags);
2019-01-23 17:03:33 +00:00
bool isCustomPort(uint32_t flags);
2015-09-24 20:52:02 +00:00
void storeCurrentAddressAndPortNum();
2017-07-08 16:32:04 +00:00
void replaceAddresses(std::vector<TcpAddress> &newAddresses, uint32_t flags);
2015-09-24 20:52:02 +00:00
void serializeToStream(NativeByteBuffer *stream);
2018-07-30 02:07:02 +00:00
void clearAuthKey(HandshakeType type);
2015-09-24 20:52:02 +00:00
void clearServerSalts();
int64_t getServerSalt();
void mergeServerSalts(std::vector<std::unique_ptr<TL_future_salt>> &salts);
void addServerSalt(std::unique_ptr<TL_future_salt> &serverSalt);
bool containsServerSalt(int64_t value);
2019-01-23 17:03:33 +00:00
void suspendConnections(bool suspendPush);
2015-09-24 20:52:02 +00:00
void getSessions(std::vector<int64_t> &sessions);
2018-07-30 02:07:02 +00:00
void recreateSessions(HandshakeType type);
2017-03-30 23:58:05 +00:00
void resetAddressAndPortNum();
2018-07-30 02:07:02 +00:00
bool isHandshakingAny();
bool isHandshaking(bool media);
bool isHandshaking(HandshakeType type);
bool hasAuthKey(ConnectionType connectionTyoe, int32_t allowPendingKey);
bool hasPermanentAuthKey();
2015-09-24 20:52:02 +00:00
bool isExportingAuthorization();
2018-07-30 02:07:02 +00:00
bool hasMediaAddress();
void resetInitVersion();
2015-09-24 20:52:02 +00:00
2017-03-30 23:58:05 +00:00
Connection *getDownloadConnection(uint8_t num, bool create);
2018-07-30 02:07:02 +00:00
Connection *getProxyConnection(uint8_t num, bool create);
2017-03-30 23:58:05 +00:00
Connection *getUploadConnection(uint8_t num, bool create);
2018-07-30 02:07:02 +00:00
Connection *getGenericConnection(bool create, int32_t allowPendingKey);
Connection *getGenericMediaConnection(bool create, int32_t allowPendingKey);
2015-09-24 20:52:02 +00:00
Connection *getPushConnection(bool create);
2017-07-08 16:32:04 +00:00
Connection *getTempConnection(bool create);
2018-07-30 02:07:02 +00:00
Connection *getConnectionByType(uint32_t connectionType, bool create, int32_t allowPendingKey);
2017-03-30 23:58:05 +00:00
2018-07-30 02:07:02 +00:00
static inline void aesIgeEncryption(uint8_t *buffer, uint8_t *key, uint8_t *iv, bool encrypt, bool changeIv, uint32_t length);
2015-09-24 20:52:02 +00:00
private:
void onHandshakeConnectionClosed(Connection *connection);
void onHandshakeConnectionConnected(Connection *connection);
2018-07-30 02:07:02 +00:00
void onHandshakeComplete(Handshake *handshake, int64_t keyId, ByteArray *authKey, int32_t timeDifference);
void processHandshakeResponse(bool media, TLObject *message, int64_t messageId);
NativeByteBuffer *createRequestsData(std::vector<std::unique_ptr<NetworkMessage>> &requests, int32_t *quickAckId, Connection *connection, bool pfsInit);
bool decryptServerResponse(int64_t keyId, uint8_t *key, uint8_t *data, uint32_t length, Connection *connection);
TLObject *getCurrentHandshakeRequest(bool media);
ByteArray *getAuthKey(ConnectionType connectionType, bool perm, int64_t *authKeyId, int32_t allowPendingKey);
2015-09-24 20:52:02 +00:00
2018-07-30 02:07:02 +00:00
const int32_t *defaultPorts = new int32_t[4] {-1, 443, 5222, -1};
2015-09-24 20:52:02 +00:00
2018-07-30 02:07:02 +00:00
int32_t instanceNum;
2015-09-24 20:52:02 +00:00
uint32_t datacenterId;
Connection *genericConnection = nullptr;
2018-07-30 02:07:02 +00:00
Connection *genericMediaConnection = nullptr;
2017-07-08 16:32:04 +00:00
Connection *tempConnection = nullptr;
2018-07-30 02:07:02 +00:00
Connection *proxyConnection[PROXY_CONNECTIONS_COUNT];
2017-07-08 16:32:04 +00:00
Connection *downloadConnection[DOWNLOAD_CONNECTIONS_COUNT];
2017-03-30 23:58:05 +00:00
Connection *uploadConnection[UPLOAD_CONNECTIONS_COUNT];
2015-09-24 20:52:02 +00:00
Connection *pushConnection = nullptr;
uint32_t lastInitVersion = 0;
2018-07-30 02:07:02 +00:00
uint32_t lastInitMediaVersion = 0;
2015-09-24 20:52:02 +00:00
bool authorized = false;
2017-07-08 16:32:04 +00:00
std::vector<TcpAddress> addressesIpv4;
std::vector<TcpAddress> addressesIpv6;
std::vector<TcpAddress> addressesIpv4Download;
std::vector<TcpAddress> addressesIpv6Download;
std::vector<TcpAddress> addressesIpv4Temp;
2015-09-24 20:52:02 +00:00
std::vector<std::unique_ptr<TL_future_salt>> serverSalts;
uint32_t currentPortNumIpv4 = 0;
uint32_t currentAddressNumIpv4 = 0;
2017-07-08 16:32:04 +00:00
uint32_t currentPortNumIpv4Temp = 0;
uint32_t currentAddressNumIpv4Temp = 0;
2015-09-24 20:52:02 +00:00
uint32_t currentPortNumIpv6 = 0;
uint32_t currentAddressNumIpv6 = 0;
uint32_t currentPortNumIpv4Download = 0;
uint32_t currentAddressNumIpv4Download = 0;
uint32_t currentPortNumIpv6Download = 0;
uint32_t currentAddressNumIpv6Download = 0;
2018-07-30 02:07:02 +00:00
ByteArray *authKeyPerm = nullptr;
int64_t authKeyPermId = 0;
ByteArray *authKeyTemp = nullptr;
int64_t authKeyTempId = 0;
ByteArray *authKeyMediaTemp = nullptr;
int64_t authKeyMediaTempId = 0;
2015-09-24 20:52:02 +00:00
Config *config = nullptr;
2017-07-08 16:32:04 +00:00
bool isCdnDatacenter = false;
2015-09-24 20:52:02 +00:00
2018-07-30 02:07:02 +00:00
std::vector<std::unique_ptr<Handshake>> handshakes;
const uint32_t configVersion = 10;
2015-09-24 20:52:02 +00:00
const uint32_t paramsConfigVersion = 1;
2018-07-30 02:07:02 +00:00
Connection *createProxyConnection(uint8_t num);
2017-03-30 23:58:05 +00:00
Connection *createDownloadConnection(uint8_t num);
Connection *createUploadConnection(uint8_t num);
2015-09-24 20:52:02 +00:00
Connection *createGenericConnection();
2018-07-30 02:07:02 +00:00
Connection *createGenericMediaConnection();
2017-07-08 16:32:04 +00:00
Connection *createTempConnection();
2015-09-24 20:52:02 +00:00
Connection *createPushConnection();
Connection *createConnectionByType(uint32_t connectionType);
2018-07-30 02:07:02 +00:00
void beginHandshake(HandshakeType handshakeType, bool reconnect);
2015-09-24 20:52:02 +00:00
bool exportingAuthorization = false;
void exportAuthorization();
2017-07-08 16:32:04 +00:00
static TL_help_configSimple *decodeSimpleConfig(NativeByteBuffer *buffer);
2015-09-24 20:52:02 +00:00
friend class ConnectionsManager;
2018-07-30 02:07:02 +00:00
friend class Connection;
friend class Handshake;
friend class Request;
2015-09-24 20:52:02 +00:00
};
#endif