2018-10-03 14:34:43 +00:00
|
|
|
//
|
|
|
|
// Created by Grishka on 29.03.17.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBTGVOIP_NETWORKSOCKET_H
|
|
|
|
#define LIBTGVOIP_NETWORKSOCKET_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-02-06 18:22:38 +00:00
|
|
|
#include "utils.h"
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
namespace tgvoip {
|
|
|
|
|
|
|
|
enum NetworkProtocol{
|
|
|
|
PROTO_UDP=0,
|
|
|
|
PROTO_TCP
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TCPO2State{
|
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char iv[16];
|
|
|
|
unsigned char ecount[16];
|
|
|
|
uint32_t num;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkAddress{
|
|
|
|
public:
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual std::string ToString() const =0;
|
|
|
|
bool operator==(const NetworkAddress& other) const;
|
|
|
|
bool operator!=(const NetworkAddress& other) const;
|
2018-10-03 14:34:43 +00:00
|
|
|
virtual ~NetworkAddress()=default;
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual bool IsEmpty() const =0;
|
2019-02-14 07:27:45 +00:00
|
|
|
virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const =0;
|
2018-10-03 14:34:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class IPv4Address : public NetworkAddress{
|
|
|
|
public:
|
|
|
|
IPv4Address(std::string addr);
|
|
|
|
IPv4Address(uint32_t addr);
|
|
|
|
IPv4Address();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual std::string ToString() const override;
|
|
|
|
uint32_t GetAddress() const;
|
|
|
|
virtual bool IsEmpty() const override;
|
2019-02-14 07:27:45 +00:00
|
|
|
virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const override;
|
|
|
|
|
|
|
|
static const IPv4Address Broadcast(){
|
|
|
|
return IPv4Address(0xFFFFFFFF);
|
|
|
|
}
|
2018-10-03 14:34:43 +00:00
|
|
|
private:
|
|
|
|
uint32_t address;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IPv6Address : public NetworkAddress{
|
|
|
|
public:
|
|
|
|
IPv6Address(std::string addr);
|
|
|
|
IPv6Address(const uint8_t* addr);
|
|
|
|
IPv6Address();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual std::string ToString() const override;
|
|
|
|
const uint8_t* GetAddress() const;
|
|
|
|
virtual bool IsEmpty() const override;
|
2019-02-14 07:27:45 +00:00
|
|
|
virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const override;
|
2018-10-03 14:34:43 +00:00
|
|
|
private:
|
|
|
|
uint8_t address[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NetworkPacket{
|
|
|
|
unsigned char* data;
|
|
|
|
size_t length;
|
|
|
|
NetworkAddress* address;
|
|
|
|
uint16_t port;
|
|
|
|
NetworkProtocol protocol;
|
|
|
|
};
|
|
|
|
typedef struct NetworkPacket NetworkPacket;
|
|
|
|
|
|
|
|
class SocketSelectCanceller{
|
|
|
|
public:
|
|
|
|
virtual ~SocketSelectCanceller();
|
|
|
|
virtual void CancelSelect()=0;
|
|
|
|
static SocketSelectCanceller* Create();
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocket{
|
|
|
|
public:
|
2019-02-06 18:22:38 +00:00
|
|
|
friend class NetworkSocketPosix;
|
|
|
|
friend class NetworkSocketWinsock;
|
|
|
|
|
|
|
|
TGVOIP_DISALLOW_COPY_AND_ASSIGN(NetworkSocket);
|
2018-10-03 14:34:43 +00:00
|
|
|
NetworkSocket(NetworkProtocol protocol);
|
|
|
|
virtual ~NetworkSocket();
|
|
|
|
virtual void Send(NetworkPacket* packet)=0;
|
|
|
|
virtual void Receive(NetworkPacket* packet)=0;
|
|
|
|
size_t Receive(unsigned char* buffer, size_t len);
|
|
|
|
size_t Send(unsigned char* buffer, size_t len);
|
|
|
|
virtual void Open()=0;
|
|
|
|
virtual void Close()=0;
|
|
|
|
virtual uint16_t GetLocalPort(){ return 0; };
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual void Connect(const NetworkAddress* address, uint16_t port)=0;
|
2018-10-03 14:34:43 +00:00
|
|
|
virtual std::string GetLocalInterfaceInfo(IPv4Address* inet4addr, IPv6Address* inet6addr);
|
|
|
|
virtual void OnActiveInterfaceChanged(){};
|
|
|
|
virtual NetworkAddress* GetConnectedAddress(){ return NULL; };
|
|
|
|
virtual uint16_t GetConnectedPort(){ return 0; };
|
|
|
|
virtual void SetTimeouts(int sendTimeout, int recvTimeout){};
|
|
|
|
|
|
|
|
virtual bool IsFailed();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual bool IsReadyToSend(){
|
|
|
|
return readyToSend;
|
|
|
|
}
|
|
|
|
virtual bool OnReadyToSend(){ readyToSend=true; return true; };
|
|
|
|
virtual bool OnReadyToReceive(){ return true; };
|
|
|
|
void SetTimeout(double timeout){
|
|
|
|
this->timeout=timeout;
|
|
|
|
};
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
static NetworkSocket* Create(NetworkProtocol protocol);
|
|
|
|
static IPv4Address* ResolveDomainName(std::string name);
|
2019-02-06 18:22:38 +00:00
|
|
|
static bool Select(std::vector<NetworkSocket*>& readFds, std::vector<NetworkSocket*>& writeFds, std::vector<NetworkSocket*>& errorFds, SocketSelectCanceller* canceller);
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual uint16_t GenerateLocalPort();
|
|
|
|
virtual void SetMaxPriority();
|
2019-02-06 18:22:38 +00:00
|
|
|
|
2018-10-03 14:34:43 +00:00
|
|
|
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];
|
|
|
|
bool failed;
|
2019-02-06 18:22:38 +00:00
|
|
|
bool readyToSend=false;
|
|
|
|
double lastSuccessfulOperationTime=0.0;
|
|
|
|
double timeout=0.0;
|
2018-10-03 14:34:43 +00:00
|
|
|
NetworkProtocol protocol;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketWrapper : public NetworkSocket{
|
|
|
|
public:
|
|
|
|
NetworkSocketWrapper(NetworkProtocol protocol) : NetworkSocket(protocol){};
|
|
|
|
virtual ~NetworkSocketWrapper(){};
|
|
|
|
virtual NetworkSocket* GetWrapped()=0;
|
|
|
|
virtual void InitConnection()=0;
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual void SetNonBlocking(bool){};
|
2018-10-03 14:34:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketTCPObfuscated : public NetworkSocketWrapper{
|
|
|
|
public:
|
|
|
|
NetworkSocketTCPObfuscated(NetworkSocket* wrapped);
|
|
|
|
virtual ~NetworkSocketTCPObfuscated();
|
|
|
|
virtual NetworkSocket* GetWrapped();
|
|
|
|
virtual void InitConnection();
|
|
|
|
virtual void Send(NetworkPacket *packet);
|
|
|
|
virtual void Receive(NetworkPacket *packet);
|
|
|
|
virtual void Open();
|
|
|
|
virtual void Close();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual void Connect(const NetworkAddress *address, uint16_t port);
|
|
|
|
virtual bool OnReadyToSend();
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
virtual bool IsFailed();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual bool IsReadyToSend(){
|
|
|
|
return readyToSend && wrapped->IsReadyToSend();
|
|
|
|
};
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
NetworkSocket* wrapped;
|
|
|
|
TCPO2State recvState;
|
|
|
|
TCPO2State sendState;
|
2019-02-06 18:22:38 +00:00
|
|
|
bool initialized=false;
|
2018-10-03 14:34:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NetworkSocketSOCKS5Proxy : public NetworkSocketWrapper{
|
|
|
|
public:
|
|
|
|
NetworkSocketSOCKS5Proxy(NetworkSocket* tcp, NetworkSocket* udp, std::string username, std::string password);
|
|
|
|
virtual ~NetworkSocketSOCKS5Proxy();
|
|
|
|
virtual void Send(NetworkPacket *packet);
|
|
|
|
virtual void Receive(NetworkPacket *packet);
|
|
|
|
virtual void Open();
|
|
|
|
virtual void Close();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual void Connect(const NetworkAddress *address, uint16_t port);
|
2018-10-03 14:34:43 +00:00
|
|
|
virtual NetworkSocket *GetWrapped();
|
|
|
|
virtual void InitConnection();
|
|
|
|
virtual bool IsFailed();
|
|
|
|
virtual NetworkAddress *GetConnectedAddress();
|
|
|
|
virtual uint16_t GetConnectedPort();
|
2019-02-06 18:22:38 +00:00
|
|
|
virtual bool OnReadyToSend();
|
|
|
|
virtual bool OnReadyToReceive();
|
|
|
|
|
|
|
|
bool NeedSelectForSending();
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
private:
|
2019-02-06 18:22:38 +00:00
|
|
|
void SendConnectionCommand();
|
|
|
|
enum ConnectionState{
|
|
|
|
Initial,
|
|
|
|
WaitingForAuthMethod,
|
|
|
|
WaitingForAuthResult,
|
|
|
|
WaitingForCommandResult,
|
|
|
|
Connected
|
|
|
|
};
|
2018-10-03 14:34:43 +00:00
|
|
|
NetworkSocket* tcp;
|
|
|
|
NetworkSocket* udp;
|
|
|
|
std::string username;
|
|
|
|
std::string password;
|
|
|
|
NetworkAddress* connectedAddress;
|
|
|
|
uint16_t connectedPort;
|
2019-02-06 18:22:38 +00:00
|
|
|
ConnectionState state=ConnectionState::Initial;
|
2018-10-03 14:34:43 +00:00
|
|
|
|
|
|
|
IPv4Address lastRecvdV4;
|
|
|
|
IPv6Address lastRecvdV6;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //LIBTGVOIP_NETWORKSOCKET_H
|