2020-01-22 12:43:51 +01:00
|
|
|
//
|
|
|
|
// Created by Grishka on 29.03.17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_NETWORKSOCKET_H
|
|
|
|
#define LIBTGVOIP_NETWORKSOCKET_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
#include <atomic>
|
|
|
|
#include "tools/utils.h"
|
|
|
|
#include "tools/Buffers.h"
|
|
|
|
|
|
|
|
namespace tgvoip
|
|
|
|
{
|
|
|
|
|
2020-01-23 16:45:53 +01:00
|
|
|
|
|
|
|
// API compatibility
|
|
|
|
struct IPv4Address
|
|
|
|
{
|
|
|
|
IPv4Address(std::string addr) : addr(addr){};
|
|
|
|
std::string addr;
|
|
|
|
};
|
|
|
|
struct IPv6Address
|
|
|
|
{
|
|
|
|
IPv6Address(std::string addr) : addr(addr){};
|
|
|
|
std::string addr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-22 12:43:51 +01:00
|
|
|
enum class NetworkProtocol
|
|
|
|
{
|
|
|
|
UDP = 0,
|
|
|
|
TCP
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TCPO2State
|
|
|
|
{
|
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
|
|
|
unsigned char ecount[16];
|
|
|
|
uint32_t num;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkAddress
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual std::string ToString() const;
|
|
|
|
bool operator==(const NetworkAddress &other) const;
|
|
|
|
bool operator!=(const NetworkAddress &other) const;
|
|
|
|
virtual ~NetworkAddress() = default;
|
|
|
|
virtual bool IsEmpty() const;
|
|
|
|
virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress &other) const;
|
|
|
|
|
|
|
|
static NetworkAddress Empty();
|
|
|
|
static NetworkAddress IPv4(std::string str);
|
|
|
|
static NetworkAddress IPv4(uint32_t addr);
|
2020-03-17 20:11:27 +01:00
|
|
|
static NetworkAddress IPv4(const BufferInputStream &in);
|
2020-01-22 12:43:51 +01:00
|
|
|
static NetworkAddress IPv6(std::string str);
|
|
|
|
static NetworkAddress IPv6(const uint8_t addr[16]);
|
2020-03-17 20:11:27 +01:00
|
|
|
static NetworkAddress IPv6(const BufferInputStream &in);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
bool isIPv6 = false;
|
|
|
|
union {
|
|
|
|
uint32_t ipv4;
|
|
|
|
uint8_t ipv6[16];
|
|
|
|
} addr;
|
|
|
|
|
2020-03-21 15:08:03 +01:00
|
|
|
public:
|
2020-01-22 12:43:51 +01:00
|
|
|
NetworkAddress(){};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NetworkPacket
|
|
|
|
{
|
|
|
|
TGVOIP_MOVE_ONLY(NetworkPacket);
|
|
|
|
Buffer data;
|
|
|
|
NetworkAddress address;
|
|
|
|
uint16_t port;
|
|
|
|
NetworkProtocol protocol;
|
|
|
|
|
|
|
|
static NetworkPacket Empty()
|
|
|
|
{
|
|
|
|
return NetworkPacket{Buffer(), NetworkAddress::Empty(), 0, NetworkProtocol::UDP};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
return data.IsEmpty() || (protocol == NetworkProtocol::UDP && (port == 0 || address.IsEmpty()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SocketSelectCanceller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~SocketSelectCanceller();
|
|
|
|
virtual void CancelSelect() = 0;
|
2020-01-24 20:26:34 +01:00
|
|
|
static std::unique_ptr<SocketSelectCanceller> Create();
|
2020-01-22 12:43:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
friend class NetworkSocketPosix;
|
|
|
|
friend class NetworkSocketWinsock;
|
|
|
|
|
|
|
|
TGVOIP_DISALLOW_COPY_AND_ASSIGN(NetworkSocket);
|
|
|
|
NetworkSocket(NetworkProtocol protocol);
|
|
|
|
virtual ~NetworkSocket();
|
|
|
|
virtual void Send(NetworkPacket packet) = 0;
|
|
|
|
virtual NetworkPacket Receive(size_t maxLen = 0) = 0;
|
|
|
|
size_t Receive(unsigned char *buffer, size_t len);
|
|
|
|
virtual void Open() = 0;
|
|
|
|
virtual void Close() = 0;
|
|
|
|
virtual uint16_t GetLocalPort() { return 0; };
|
|
|
|
virtual void Connect(const NetworkAddress address, uint16_t port) = 0;
|
|
|
|
virtual std::string GetLocalInterfaceInfo(NetworkAddress *inet4addr, NetworkAddress *inet6addr);
|
|
|
|
virtual void OnActiveInterfaceChanged(){};
|
|
|
|
virtual NetworkAddress GetConnectedAddress() { return NetworkAddress::Empty(); };
|
|
|
|
virtual uint16_t GetConnectedPort() { return 0; };
|
|
|
|
virtual void SetTimeouts(int sendTimeout, int recvTimeout){};
|
|
|
|
|
|
|
|
virtual bool IsFailed();
|
|
|
|
virtual bool IsReadyToSend()
|
|
|
|
{
|
|
|
|
return readyToSend;
|
|
|
|
}
|
|
|
|
virtual bool OnReadyToSend()
|
|
|
|
{
|
|
|
|
readyToSend = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
virtual bool OnReadyToReceive() { return true; };
|
|
|
|
void SetTimeout(double timeout)
|
|
|
|
{
|
|
|
|
this->timeout = timeout;
|
|
|
|
};
|
|
|
|
|
2020-01-24 20:26:34 +01:00
|
|
|
static std::shared_ptr<NetworkSocket> Create(NetworkProtocol protocol);
|
2020-01-22 12:43:51 +01:00
|
|
|
static NetworkAddress ResolveDomainName(std::string name);
|
2020-01-24 20:26:34 +01:00
|
|
|
static bool Select(std::vector<std::shared_ptr<NetworkSocket>> &readFds, std::vector<std::shared_ptr<NetworkSocket>> &writeFds, std::vector<std::shared_ptr<NetworkSocket>> &errorFds, const std::unique_ptr<SocketSelectCanceller> &canceller);
|
2020-01-22 12:43:51 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual uint16_t GenerateLocalPort();
|
|
|
|
virtual void SetMaxPriority();
|
|
|
|
|
|
|
|
static void GenerateTCPO2States(unsigned char *buffer, TCPO2State *recvState, TCPO2State *sendState);
|
|
|
|
static void EncryptForTCPO2(unsigned char *buffer, size_t len, TCPO2State *state);
|
|
|
|
double ipv6Timeout;
|
|
|
|
unsigned char nat64Prefix[12];
|
|
|
|
std::atomic<bool> failed;
|
|
|
|
bool readyToSend = false;
|
|
|
|
double lastSuccessfulOperationTime = 0.0;
|
|
|
|
double timeout = 0.0;
|
|
|
|
NetworkProtocol protocol;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketWrapper : public NetworkSocket
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NetworkSocketWrapper(NetworkProtocol protocol) : NetworkSocket(protocol){};
|
|
|
|
virtual ~NetworkSocketWrapper(){};
|
2020-01-24 20:26:34 +01:00
|
|
|
virtual std::shared_ptr<NetworkSocket> GetWrapped() = 0;
|
2020-01-22 12:43:51 +01:00
|
|
|
virtual void InitConnection() = 0;
|
|
|
|
virtual void SetNonBlocking(bool){};
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketTCPObfuscated : public NetworkSocketWrapper
|
|
|
|
{
|
|
|
|
public:
|
2020-01-24 20:26:34 +01:00
|
|
|
NetworkSocketTCPObfuscated(const std::shared_ptr<NetworkSocket> &wrapped);
|
2020-01-22 12:43:51 +01:00
|
|
|
virtual ~NetworkSocketTCPObfuscated();
|
2020-01-24 20:26:34 +01:00
|
|
|
virtual std::shared_ptr<NetworkSocket> GetWrapped();
|
2020-01-22 12:43:51 +01:00
|
|
|
virtual void InitConnection();
|
|
|
|
virtual void Send(NetworkPacket packet) override;
|
|
|
|
virtual NetworkPacket Receive(size_t maxLen) override;
|
|
|
|
virtual void Open();
|
|
|
|
virtual void Close();
|
|
|
|
virtual void Connect(const NetworkAddress address, uint16_t port);
|
|
|
|
virtual bool OnReadyToSend();
|
|
|
|
|
|
|
|
virtual bool IsFailed();
|
|
|
|
virtual bool IsReadyToSend()
|
|
|
|
{
|
|
|
|
return readyToSend && wrapped->IsReadyToSend();
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2020-01-24 20:26:34 +01:00
|
|
|
std::shared_ptr<NetworkSocket> wrapped;
|
2020-01-22 12:43:51 +01:00
|
|
|
TCPO2State recvState;
|
|
|
|
TCPO2State sendState;
|
|
|
|
bool initialized = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketSOCKS5Proxy : public NetworkSocketWrapper
|
|
|
|
{
|
|
|
|
public:
|
2020-01-24 20:26:34 +01:00
|
|
|
NetworkSocketSOCKS5Proxy(const std::shared_ptr<NetworkSocket> &tcp, const std::shared_ptr<NetworkSocket> &udp, std::string username, std::string password);
|
2020-01-22 12:43:51 +01:00
|
|
|
virtual ~NetworkSocketSOCKS5Proxy();
|
|
|
|
virtual void Send(NetworkPacket packet) override;
|
|
|
|
virtual NetworkPacket Receive(size_t maxLen) override;
|
|
|
|
virtual void Open() override;
|
|
|
|
virtual void Close();
|
|
|
|
virtual void Connect(const NetworkAddress address, uint16_t port);
|
2020-01-24 20:26:34 +01:00
|
|
|
virtual std::shared_ptr<NetworkSocket> GetWrapped();
|
2020-01-22 12:43:51 +01:00
|
|
|
virtual void InitConnection();
|
|
|
|
virtual bool IsFailed();
|
|
|
|
virtual NetworkAddress GetConnectedAddress();
|
|
|
|
virtual uint16_t GetConnectedPort();
|
|
|
|
virtual bool OnReadyToSend();
|
|
|
|
virtual bool OnReadyToReceive();
|
|
|
|
|
|
|
|
bool NeedSelectForSending();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SendConnectionCommand();
|
|
|
|
enum ConnectionState
|
|
|
|
{
|
|
|
|
Initial,
|
|
|
|
WaitingForAuthMethod,
|
|
|
|
WaitingForAuthResult,
|
|
|
|
WaitingForCommandResult,
|
|
|
|
Connected
|
|
|
|
};
|
2020-01-24 20:26:34 +01:00
|
|
|
std::shared_ptr<NetworkSocket> tcp;
|
|
|
|
std::shared_ptr<NetworkSocket> udp;
|
2020-01-22 12:43:51 +01:00
|
|
|
std::string username;
|
|
|
|
std::string password;
|
|
|
|
NetworkAddress connectedAddress = NetworkAddress::Empty();
|
|
|
|
uint16_t connectedPort;
|
|
|
|
ConnectionState state = ConnectionState::Initial;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tgvoip
|
|
|
|
|
|
|
|
#endif //LIBTGVOIP_NETWORKSOCKET_H
|