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