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