-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
216 lines (205 loc) · 7.41 KB
/
client.cpp
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
211
212
213
214
215
216
#include "client.h"
#define SWITCH_THRESHOLD 5000
extern bool isWinsockInitialized;
Client::Client(const char* serverIPv6Addr, uint16_t serverPort, uint16_t remotePort, uint16_t localPort) {
sendUdpPacketCount = 0;
recvUdpPacketCount = 0;
keepRunning = true;
// init Winsock
WSADATA wsaData;
if (!isWinsockInitialized) {
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
LogError("WSAStartup failed");
keepRunning = false;
} else {
isWinsockInitialized = true;
LogDebug("WSAStartup success");
}
}
initSockAddr(serverIPv6Addr, serverPort);
if (initUdpSockets(remotePort, localPort)) {
LogDebug("Init udpSockets success.");
} else {
keepRunning = false;
LogError("Init udpSockets failed.");
}
}
Client::~Client() {
for (const SOCKET& sock: udpSockets) {
closesocket(sock);
}
}
void Client::initSockAddr(const char* serverIPv6Addr, uint16_t serverPort) {
/* init serverAddrVector */ {
sockaddr_in6 serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin6_family = AF_INET6;
inet_pton(AF_INET6, serverIPv6Addr, &serverAddr.sin6_addr);
for (int i = 0; i < 4; ++i) {
// init serverAddr
serverAddr.sin6_port = htons(serverPort + i);
serverAddrVector.push_back(serverAddr);
}
}
/* init clientAddr */ {
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(0);
clientAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
/* init ctrlSockAddr */ {
memset(&ctrlSockAddr, 0, sizeof(ctrlSockAddr));
ctrlSockAddr.sin_family = AF_INET;
ctrlSockAddr.sin_port = htons(0);
ctrlSockAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
}
bool Client::initUdpSockets(uint16_t remotePort, uint16_t localPort) {
bool ret = true;
/* init udpSokcetsConnectWithRemoteHost */ {
sockaddr_in6 remoteAddr;
memset(&remoteAddr, 0, sizeof(remoteAddr));
remoteAddr.sin6_family = AF_INET6;
remoteAddr.sin6_addr = in6addr_any;
remotePort -= remotePort % 4;
for (int i = 0; i < 4; ++i) {
// init remoteAddr
remoteAddr.sin6_port = htons(remotePort + i);
// create udp socket
SOCKET sock = createUdpSocketV6(remoteAddr);
if (sock == SOCKET_ERROR) {
keepRunning = false;
LogError("Can not create remote socket %d.", i);
ret = false;
}
udpSokcetsConnectWithRemoteHost.push_back(sock);
udpSockets.push_back(sock);
}
}
/* init udpSokcetConnectWithLocalHost */ {
// init localAddr
sockaddr_in localAddr;
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
localAddr.sin_port = htons(localPort);
// create udp socket
SOCKET sock = createUdpSocket(localAddr);
if (sock == SOCKET_ERROR) {
keepRunning = false;
LogError("Can not create local socket.");
ret = false;
}
udpSokcetConnectWithLocalHost = sock;
udpSockets.push_back(sock);
}
/* init ctrl socket */ {
ctrlSocket = createUdpSocket(ctrlSockAddr);
if (ctrlSocket == SOCKET_ERROR) {
LogError("Can not create ctrl socket.");
ret = false;
}
udpSockets.push_back(ctrlSocket);
}
return ret;
}
int Client::receiveUdpPacket() {
while (keepRunning) {
FD_ZERO(&udpSokcetsFDSet);
for (const SOCKET& sock: udpSockets) {
FD_SET(sock, &udpSokcetsFDSet);
}
if (select(0, &udpSokcetsFDSet, NULL, NULL, NULL) == SOCKET_ERROR) {
LogError("Select failed with WSAGetLastError %d", WSAGetLastError());
continue;
}
for (const SOCKET& sock: udpSockets) {
if (FD_ISSET(sock, &udpSokcetsFDSet)) {
char packetBuffer[65535];
UdpPacketPtr upp = make_shared<UdpPacket>();
int sockAddrLen = sizeof(upp->sockAddr);
upp->dataBytes = recvfrom(
sock, packetBuffer, 65535, 0,
reinterpret_cast<sockaddr*>(&upp->sockAddr), &sockAddrLen
);
if (upp->dataBytes == SOCKET_ERROR) {
LogError("Recv failed with WSAGetLastError %d", WSAGetLastError());
} else {
if (sock != ctrlSocket && sock != udpSokcetConnectWithLocalHost) ++recvUdpPacketCount;
upp->data = new char[upp->dataBytes];
upp->sock = sock;
memcpy(upp->data, packetBuffer, upp->dataBytes);
/* push to queue */ {
lock_guard<mutex> lock(udpPacketQueueMutex);
udpPacketQueue.push(upp);
}
udpPacketQueueCV.notify_one();
}
}
}
}
return 0;
}
int Client::forwardUdpPacket() {
while (keepRunning) {
unique_lock<mutex> lock(udpPacketQueueMutex);
udpPacketQueueCV.wait(
lock, [this] {
return !udpPacketQueue.empty();
}
);
UdpPacketPtr udpPacket;
udpPacket = udpPacketQueue.front();
udpPacketQueue.pop();
lock.unlock();
if (udpPacket->sock == ctrlSocket) {
continue;
}
if (udpPacket->sock == udpSokcetConnectWithLocalHost) {
SOCKET sendSocket = udpSokcetsConnectWithRemoteHost[sendUdpPacketCount / SWITCH_THRESHOLD % 4];
sockaddr_in6 sendAddr = serverAddrVector[sendUdpPacketCount / SWITCH_THRESHOLD % 4];
sockaddr_in recvSockAddr = *(reinterpret_cast<sockaddr_in*>(&udpPacket->sockAddr));
if (clientAddr.sin_port != recvSockAddr.sin_port) {
clientAddr = recvSockAddr;
LogDebug("New client connected, port: %u", ntohs(clientAddr.sin_port));
}
++sendUdpPacketCount;
sendUdpPacketV6(sendSocket, sendAddr, udpPacket);
} else {
SOCKET sendSocket = udpSokcetConnectWithLocalHost;
sockaddr_in sendAddr = clientAddr;
sendUdpPacket(sendSocket, sendAddr, udpPacket);
}
}
return 0;
}
int Client::sendUdpPacketToCtrl()
{
if (ctrlSockAddr.sin_port == htons(0)) {
int addrLen = sizeof(ctrlSockAddr);
if (getsockname(ctrlSocket, reinterpret_cast<sockaddr*>(&ctrlSockAddr), &addrLen) != 0) {
LogError("Can not get sockname.");
return SOCKET_ERROR;
}
}
int sendResult = sendto(ctrlSocket, "zzDragon", sizeof("zzDragon"), 0,
reinterpret_cast<sockaddr*>(&ctrlSockAddr), sizeof(ctrlSockAddr));
if (sendResult == SOCKET_ERROR) {
LogError("Send failed with WSAGetLastError %d.", WSAGetLastError());
return SOCKET_ERROR;
}
return 0;
}
int Client::start() {
thread recvUdpPacketFromRemoteHostThread(Client::receiveUdpPacket, this);
thread forwardPacketThread(Client::forwardUdpPacket, this);
LogDebug("Client is running");
recvUdpPacketFromRemoteHostThread.join();
forwardPacketThread.join();
LogDebug("Client exit");
return 0;
}
int Client::stop() {
keepRunning = false;
sendUdpPacketToCtrl();
return 0;
}