| 6 |
| #define LIBTGVOIP_NETWORKSOCKET_H |
| 7 |
| 8 | #include <stdint.h> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | #include "utils.h" |
| 12 |
| 13 | namespace tgvoip { |
| 14 |
| 15 | enum NetworkProtocol{ |
| 16 | PROTO_UDP=0, |
| 17 | PROTO_TCP |
| 18 | }; |
| 19 |
| 20 | struct TCPO2State{ |
| 21 | unsigned char key[32]; |
| 22 | unsigned char iv[16]; |
| 23 | unsigned char ecount[16]; |
| 24 | uint32_t num; |
| 25 | }; |
| 26 |
| 27 | class NetworkAddress{ |
| 28 | public: |
| 29 | virtual std::string ToString() const =0; |
| 30 | bool operator==(const NetworkAddress& other) const; |
| 31 | bool operator!=(const NetworkAddress& other) const; |
| 32 | virtual ~NetworkAddress()=default; |
| 33 | virtual bool IsEmpty() const =0; |
| 34 | virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const =0; |
| 35 | }; |
| 36 |
| 37 | class IPv4Address : public NetworkAddress{ |
| 38 | public: |
| 39 | IPv4Address(std::string addr); |
| 40 | IPv4Address(uint32_t addr); |
| 41 | IPv4Address(); |
| 42 | virtual std::string ToString() const override; |
| 43 | uint32_t GetAddress() const; |
| 44 | virtual bool IsEmpty() const override; |
| 45 | virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const override; |
| 46 |
| 47 | static const IPv4Address Broadcast(){ |
| 48 | return IPv4Address(0xFFFFFFFF); |
| 49 | } |
| 50 | private: |
| 51 | uint32_t address; |
| 52 | }; |
| 53 |
| 54 | class IPv6Address : public NetworkAddress{ |
| 55 | public: |
| 56 | IPv6Address(std::string addr); |
| 57 | IPv6Address(const uint8_t* addr); |
| 58 | IPv6Address(); |
| 59 | virtual std::string ToString() const override; |
| 60 | const uint8_t* GetAddress() const; |
| 61 | virtual bool IsEmpty() const override; |
| 62 | virtual bool PrefixMatches(const unsigned int prefix, const NetworkAddress& other) const override; |
| 63 | private: |
| 64 | uint8_t address[16]; |
| 65 | }; |
| 66 |
| 67 | struct NetworkPacket{ |
| 68 | unsigned char* data; |
| 69 | size_t length; |
| 70 | NetworkAddress* address; |
| 71 | uint16_t port; |
| 72 | NetworkProtocol protocol; |
| 73 | }; |
| 74 | typedef struct NetworkPacket NetworkPacket; |
| 75 |
| 76 | class SocketSelectCanceller{ |
| 77 | public: |
| 78 | virtual ~SocketSelectCanceller(); |
| 79 | virtual void CancelSelect()=0; |
| 80 | static SocketSelectCanceller* Create(); |
| 81 | }; |
| 82 |
| 83 | class NetworkSocket{ |
| 84 | public: |
| 85 | friend class NetworkSocketPosix; |
| 86 | friend class NetworkSocketWinsock; |
| 87 |
| 88 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(NetworkSocket); |
| 89 | NetworkSocket(NetworkProtocol protocol); |
| 90 | virtual ~NetworkSocket(); |
| 91 | virtual void Send(NetworkPacket* packet)=0; |
| 92 | virtual void Receive(NetworkPacket* packet)=0; |
| 93 | size_t Receive(unsigned char* buffer, size_t len); |
| 94 | size_t Send(unsigned char* buffer, size_t len); |
| 95 | virtual void Open()=0; |
| 96 | virtual void Close()=0; |
| 97 | virtual uint16_t GetLocalPort(){ return 0; }; |
| 98 | virtual void Connect(const NetworkAddress* address, uint16_t port)=0; |
| 99 | virtual std::string GetLocalInterfaceInfo(IPv4Address* inet4addr, IPv6Address* inet6addr); |
| 100 | virtual void OnActiveInterfaceChanged(){}; |
| 101 | virtual NetworkAddress* GetConnectedAddress(){ return NULL; }; |
| 102 | virtual uint16_t GetConnectedPort(){ return 0; }; |
| 103 | virtual void SetTimeouts(int sendTimeout, int recvTimeout){}; |
| 104 |
| 105 | virtual bool IsFailed(); |
| 106 | virtual bool IsReadyToSend(){ |
| 107 | return readyToSend; |
| 108 | } |
| 109 | virtual bool OnReadyToSend(){ readyToSend=true; return true; }; |
| 110 | virtual bool OnReadyToReceive(){ return true; }; |
| 111 | void SetTimeout(double timeout){ |
| 112 | this->timeout=timeout; |
| 113 | }; |
| 114 |
| 115 | static NetworkSocket* Create(NetworkProtocol protocol); |
| 116 | static IPv4Address* ResolveDomainName(std::string name); |
| 117 | static bool Select(std::vector<NetworkSocket*>& readFds, std::vector<NetworkSocket*>& writeFds, std::vector<NetworkSocket*>& errorFds, SocketSelectCanceller* canceller); |
| 118 |
| 119 | protected: |
| 120 | virtual uint16_t GenerateLocalPort(); |
| 121 | virtual void SetMaxPriority(); |
| 122 |
| 123 | static void GenerateTCPO2States(unsigned char* buffer, TCPO2State* recvState, TCPO2State* sendState); |
| 124 | static void EncryptForTCPO2(unsigned char* buffer, size_t len, TCPO2State* state); |
| 125 | double ipv6Timeout; |
| 126 | unsigned char nat64Prefix[12]; |
| 127 | bool failed; |
| 128 | bool readyToSend=false; |
| 129 | double lastSuccessfulOperationTime=0.0; |
| 130 | double timeout=0.0; |
| 131 | NetworkProtocol protocol; |
| 132 | }; |
| 133 |
| 134 | class NetworkSocketWrapper : public NetworkSocket{ |
| 135 | public: |
| 136 | NetworkSocketWrapper(NetworkProtocol protocol) : NetworkSocket(protocol){}; |
| 137 | virtual ~NetworkSocketWrapper(){}; |
| 138 | virtual NetworkSocket* GetWrapped()=0; |
| 139 | virtual void InitConnection()=0; |
| 140 | virtual void SetNonBlocking(bool){}; |
| 141 | }; |
| 142 |
| 143 | class NetworkSocketTCPObfuscated : public NetworkSocketWrapper{ |
| 144 | public: |
| 145 | NetworkSocketTCPObfuscated(NetworkSocket* wrapped); |
| 146 | virtual ~NetworkSocketTCPObfuscated(); |
| 147 | virtual NetworkSocket* GetWrapped(); |
| 148 | virtual void InitConnection(); |
| 149 | virtual void Send(NetworkPacket *packet); |
| 150 | virtual void Receive(NetworkPacket *packet); |
| 151 | virtual void Open(); |
| 152 | virtual void Close(); |
| 153 | virtual void Connect(const NetworkAddress *address, uint16_t port); |
| 154 | virtual bool OnReadyToSend(); |
| 155 |
| 156 | virtual bool IsFailed(); |
| 157 | virtual bool IsReadyToSend(){ |
| 158 | return readyToSend && wrapped->IsReadyToSend(); |
| 159 | }; |
| 160 |
| 161 | private: |
| 162 | NetworkSocket* wrapped; |
| 163 | TCPO2State recvState; |
| 164 | TCPO2State sendState; |
| 165 | bool initialized=false; |
| 166 | }; |
| 167 |
| 168 | class NetworkSocketSOCKS5Proxy : public NetworkSocketWrapper{ |
| 169 | public: |
| 170 | NetworkSocketSOCKS5Proxy(NetworkSocket* tcp, NetworkSocket* udp, std::string username, std::string password); |
| 171 | virtual ~NetworkSocketSOCKS5Proxy(); |
| 172 | virtual void Send(NetworkPacket *packet); |
| 173 | virtual void Receive(NetworkPacket *packet); |
| 174 | virtual void Open(); |
| 175 | virtual void Close(); |
| 176 | virtual void Connect(const NetworkAddress *address, uint16_t port); |
| 177 | virtual NetworkSocket *GetWrapped(); |
| 178 | virtual void InitConnection(); |
| 179 | virtual bool IsFailed(); |
| 180 | virtual NetworkAddress *GetConnectedAddress(); |
| 181 | virtual uint16_t GetConnectedPort(); |
| 182 | virtual bool OnReadyToSend(); |
| 183 | virtual bool OnReadyToReceive(); |
| 184 |
| 185 | bool NeedSelectForSending(); |
| 186 |
| 187 | private: |
| 188 | void SendConnectionCommand(); |
| 189 | enum ConnectionState{ |
| 190 | Initial, |
| 191 | WaitingForAuthMethod, |
| 192 | WaitingForAuthResult, |
| 193 | WaitingForCommandResult, |
| 194 | Connected |
| 195 | }; |
| 196 | NetworkSocket* tcp; |
| 197 | NetworkSocket* udp; |
| 198 | std::string username; |
| 199 | std::string password; |
| 200 | NetworkAddress* connectedAddress; |
| 201 | uint16_t connectedPort; |
| 202 | ConnectionState state=ConnectionState::Initial; |
| 203 |
| 204 | IPv4Address lastRecvdV4; |
| 205 | IPv6Address lastRecvdV6; |
| 206 | }; |
| 207 |
| 208 | } |
| 209 |
| 210 | #endif //LIBTGVOIP_NETWORKSOCKET_H |
| 211 |