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