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