-
Notifications
You must be signed in to change notification settings - Fork 10
/
ProxyServer.cpp
executable file
·375 lines (312 loc) · 11.2 KB
/
ProxyServer.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
tcp_proxy
ProxyServer.cpp
Copyright 2011 Ramsey Kant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ProxyServer.h"
/**
* Server Constructor
* Initialize state and server variables
*/
ProxyServer::ProxyServer() {
canRun = false;
listenSocket = INVALID_SOCKET;
memset(&serverAddr, 0, sizeof(serverAddr)); // Clear the address struct
// Zero the file descriptor sets
FD_ZERO(&fd_master);
FD_ZERO(&fd_read);
// Instance the clientMap, relates Socket Descriptor to pointer to Client object
clientMap = new map<SOCKET, Client*>();
}
/**
* Server Destructor
* Closes all active connections and deletes the clientMap
*/
ProxyServer::~ProxyServer() {
if(listenSocket != INVALID_SOCKET)
closeSockets();
delete clientMap;
}
/**
* Init Socket
* Initialize the Server Socket by requesting a socket handle, binding, and going into a listening state
*
* @param port Port to listen on
* @return True if initialization succeeded. False if otherwise
*/
bool ProxyServer::initSocket(int port) {
// Request a handle for the listening socket, TCP
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenSocket == INVALID_SOCKET){
printf("ProxyServer: Could not create socket.\n");
return false;
}
// Populate the server address structure
serverAddr.sin_family = AF_INET; // Family: IP protocol
serverAddr.sin_port = htons(port); // Set the port (convert from host to netbyte order
serverAddr.sin_addr.s_addr = INADDR_ANY; // Let the OS select our address
// Bind: assign the address to the socket
if(bind(listenSocket, (sockaddr*)&serverAddr, sizeof(serverAddr)) != 0){
printf("ProxyServer: Failed to bind to the address\n");
return false;
}
// Listen: put the socket in a listening state, ready to accept connections
// (SOMAXCONN) Accept a backlog of the OS Maximum connections in the queue
if(listen(listenSocket, SOMAXCONN) != 0){
printf("ProxyServer: Failed to put the socket in a listening state\n");
return false;
}
// Add the listenSocket to the master fd list
FD_SET(listenSocket, &fd_master);
// Set max fd to listenSocket
fdmax = listenSocket;
canRun = true;
return true;
}
/**
* Accept Connection
* When a new connection is detected in runServer() this function is called. This attempts to accept the pending connection, instance a Client object, and add to the client Map
*/
void ProxyServer::acceptConnection() {
// Setup new client variables
sockaddr_in clientAddr;
int clientAddrLen = sizeof(clientAddr);
SOCKET clfd = INVALID_SOCKET;
// Accept pending connection and retrieve the client socket descriptor. Bail if accept failed.
clfd = accept(listenSocket, (sockaddr*)&clientAddr, (socklen_t*)&clientAddrLen);
if (clfd == INVALID_SOCKET)
return;
// Create a new Client object
Client *cl = new Client(clfd, clientAddr);
// Initiate the Proxy connection
// If the ProxyClient failed to connect, reject this client's connection
if(!cl->proxyConnect(PROXYCLIENT_HOST, PROXYCLIENT_PORT)) {
printf("ProxyServer: New Client's ClientProxy couldn't connect to target host, booting client\n");
close(clfd);
delete cl;
return;
}
// Add the client's socket and the proxyclient's socket to the master FD set
FD_SET(clfd, &fd_master);
FD_SET(cl->getProxySocket(), &fd_master);
// If the client's handle is greater than the max, set the new max
if(clfd > fdmax)
fdmax = clfd;
// If the proxyclient's handle is greater than the max, set the new max
if(cl->getProxySocket() > fdmax)
fdmax = cl->getProxySocket();
// Add the client object to the client map
clientMap->insert(pair<int, Client*>(clfd, cl));
// Print connection message
printf("ProxyServer: %s has connected\n", cl->getClientIP());
}
/**
* Run Server
* Main server loop where the socket is initialized and the loop is started, checking for new messages or clients to be read with select()
* and handling them appropriately
*/
void ProxyServer::runServer() {
//Initializing the socket
if (!initSocket(PROXYSERVER_PORT)) {
printf("ProxyServer: Failed to set up the server\n");
return;
}
printf("ProxyServer: ProxyServer has started successfully!\n\n");
while(canRun) {
usleep(1000);
// Copy the master set into fd_read for processing
fd_read = fd_master;
// Populate fd_read with client & clientProxy descriptors that are ready to be read
// timeout param is NULL, select will block until there is data to be read
if(select(fdmax+1, &fd_read, NULL, NULL, NULL) < 0)
continue; // Nothing to be read
// Loop through all the descriptors in both fd_read and fd_proxy_read sets and check to see if data needs to be processed
for(int i=0; i <= fdmax; i++) {
// Socket isnt in the read set, continue
if(!FD_ISSET(i, &fd_read))
continue;
// If a socket is within the fd_read set, it's determine the type (client or proxyclient) and handle:
// A new client is waiting to be accepted on the listenSocket
if(i == listenSocket) {
acceptConnection();
continue;
}
// If it's in the client map, handleClient. Otherwise it's a ProxyClient
Client *cl = getClient(i);
if(cl != NULL) {
handleClient(getClient(i));
} else {
Client* cl = getProxyClientOwner(i);
if(cl == NULL)
continue; // Shouldn't ever happen...but just in case
ProxyClient* pCl = cl->getProxyClient();
// Run the ProxyClient processing method, if there is a returned ByteBuffer, forward that to the Client
ByteBuffer* bfor = pCl->clientProcess();
// Proxy Client is no longer connected, disconnect the Client
if(!pCl->isClientRunning()) {
disconnectClient(cl);
continue;
}
// Data was recieved by the ProxyClient and needs to be passed onto the Client
if(bfor != NULL) {
sendData(cl, bfor);
delete bfor;
}
}
}
}
closeSockets(); //Closes all connections to the server
}
/**
* Handle Client
* Recieve data from a client that has indicated (via select()) that it has data waiting. Pass recv'd data to handleData()
* Also detect any errors in the state of the socket
*
* @param cl Pointer to Client that sent the data
*/
void ProxyServer::handleClient(Client *cl) {
if (cl == NULL)
return;
size_t dataLen = SO_RCVBUF;
char *pData = new char[dataLen];
// Receive data on the wire into pData
/* TODO: Figure out what flags need to be set */
int flags = 0;
ssize_t lenRecv = recv(cl->getSocket(), pData, dataLen, flags);
// Determine state of client socket and act on it
if(lenRecv == 0) {
// Client closed the connection
printf("ProxyServer: Client[%s] has disconnected\n", cl->getClientIP());
disconnectClient(cl);
} else if(lenRecv < 0) {
// Some error occured
disconnectClient(cl);
} else {
printf("ProxyServer: Recieved data of size %u\n", lenRecv);
ByteBuffer *buf = new ByteBuffer((byte *)pData, (unsigned int)lenRecv);
handleData(cl, buf);
delete buf;
}
delete [] pData;
}
/**
* Handle Data
* Handle a Packet from a Client recv'd over the wire. Called from handleClient()
*
* @param cl Pointer to Client that sent the Packet
* @param buf Pointer to ByteBuffer containing data recv'd
*/
void ProxyServer::handleData(Client *cl, ByteBuffer *buf) {
// Simply forward the recieved data to the ProxyClient
ProxyClient* pCl = cl->getProxyClient();
pCl->sendData(buf);
}
/**
* Send Data
* Send Packet data to a paricular Client
*
* @param cl Client to send data to
* @param buf ByteBuffer containing data to be sent
*/
void ProxyServer::sendData(Client* cl, ByteBuffer* buf) {
size_t totalSent = 0, bytesLeft = buf->size(), dataLen = buf->size();
ssize_t n = 0;
// Get raw data
byte* pData = new byte[dataLen];
buf->getBytes(pData, dataLen);
// Solution to deal with partials sends...loop till totalSent matches dataLen
while(totalSent < dataLen) {
n = send(cl->getSocket(), pData+totalSent, bytesLeft, 0);
// Client closed the connection
if(n < 0) {
printf("ProxyServer: Client[%s] has disconnected\n", cl->getClientIP());
disconnectClient(cl);
break;
}
// Adjust byte count after a successful send
totalSent += n;
bytesLeft -= n;
}
}
/**
* Disconnect Client
* Close the client's socket descriptor and release it from the FD map, client map, and memory
*
* @param cl Pointer to Client object
*/
void ProxyServer::disconnectClient(Client *cl) {
if (cl == NULL)
return;
// Close the socket descriptor
close(cl->getSocket());
// Remove from the FD set (used in select())
FD_CLR(cl->getSocket(), &fd_master);
FD_CLR(cl->getProxySocket(), &fd_master);
// Remove from the clientMap
clientMap->erase(cl->getSocket());
// Free client object from memory
delete cl;
}
/**
* Get Client
* Lookup client based on the socket descriptor number in the clientMap
*
* @param clfd Client Descriptor
* @return Pointer to Client object if found. NULL otherwise
*/
Client* ProxyServer::getClient(SOCKET clfd) {
// Lookup in map
map<int, Client*>::const_iterator it;
it = clientMap->find(clfd);
// Client wasn't found, return NULL
if (it == clientMap->end())
return NULL;
// Return a pointer to the Client corresponding to the SOCKET that was found in clientMap
return it->second;
}
/**
* Get Proxy Client Owner
* Given a Proxy Client's FD, grab the Client associated with it
*
* @param fd ProxyClient Descriptor
* @return Pointer to Client object if found. NULL otherwise
*/
Client* ProxyServer::getProxyClientOwner(SOCKET fd) {
map<int, Client*>::const_iterator it;
Client* cl = NULL;
for (it = clientMap->begin(); it != clientMap->end(); it++) {
cl = it->second;
if((cl != NULL) && (cl->getProxySocket() == fd))
return cl;
}
return NULL;
}
/**
* Close Sockets
* Close all sockets found in the client map. Called on server shutdown
*/
void ProxyServer::closeSockets() {
printf("ProxyServer: Closing all connections and shutting down the listening socket..\n");
// Loop through all client's in the map and disconnect them
map<int, Client*>::const_iterator it;
for (it = clientMap->begin(); it != clientMap->end(); it++) {
Client *cl = it->second;
disconnectClient(cl);
}
// Clear the map
clientMap->clear();
// Shutdown the listening socket
shutdown(listenSocket, SHUT_RDWR);
// Release the listenSocket to the OS
close(listenSocket);
listenSocket = INVALID_SOCKET;
}