-
Notifications
You must be signed in to change notification settings - Fork 0
/
nzProx.cpp
371 lines (316 loc) · 8.67 KB
/
nzProx.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
/*
nzProx.cpp
Noah Betzen
A simple reverse proxy and load balancer in C++.
Compile with:
g++ nzProx.cpp -o nzProx.out
Run with:
./nzProx.out -l PORT -h HOST0 -j HOST1 -p PORT [-f]
*/
#include <arpa/inet.h>
#include <libgen.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <wait.h>
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ofstream;
#include <sstream>
using std::stringstream;
#define BUFFERSIZE 1024
#define PIPEREAD 0
#define PIPEWRITE 1
// forward declarations
void forwardData(int sourceSock, int destinationSock);
void sigchldHandler(int signal);
void sigtermHandler(int signal);
void handleClient(int clientSock, struct sockaddr_in clientAddr, bool sendToFirst);
int createSocket(int port);
int createConnection(int num);
int parseArguments(int argc, char *argv[]);
void serverLoop();
int serverSock;
int clientSock;
int remoteSock0;
int remoteSock1;
int remotePort = 0;
char *remoteHost0;
char *remoteHost1;
bool foreground = false;
void logIt(stringstream &logStream)
{
ofstream logFile("nzProx.log", std::ios_base::out | std::ios_base::app);
logFile << logStream.rdbuf();
logStream.str("");
}
stringstream logStream;
int main(int argc, char *argv[])
{
int localPort;
pid_t pid;
localPort = parseArguments(argc, argv);
if (localPort < 0)
{
logStream << "Usage: " << argv[0] << " -l localPort -h remoteHost0 -j remoteHost1 -p remotePort [-f (stay in foreground)]" << endl;
logIt(logStream);
exit(1);
}
// start server
if ((serverSock = createSocket(localPort)) < 0)
{
logStream << "ERROR: Cannot run server!" << endl;
logIt(logStream);
exit(1);
}
signal(SIGCHLD, sigchldHandler); // prevent ended children from becoming zombies
signal(SIGTERM, sigtermHandler); // handle KILL signal
if (foreground)
{
serverLoop();
}
else // run in background
{
switch(pid = fork())
{
case 0: // child
serverLoop();
break;
case -1: // error
logStream << "ERROR: Cannot spawn child!" << endl;
logIt(logStream);
exit(1);
default: // parent
close(serverSock);
}
}
return 0;
}
int parseArguments(int argc, char *argv[])
{
int argument;
int localPort = 0;
while ((argument = getopt(argc, argv, "l:h:j:p:f")) != -1)
{
switch(argument)
{
case 'l':
localPort = atoi(optarg);
break;
case 'h':
remoteHost0 = optarg;
break;
case 'j':
remoteHost1 = optarg;
break;
case 'p':
remotePort = atoi(optarg);
break;
case 'f':
foreground = true;
}
}
if (localPort && remoteHost0 && remoteHost1 && remotePort)
{
return localPort;
}
else
{
logStream << "ERROR: Bad syntax!" << endl;
logIt(logStream);
return -1;
}
}
int createSocket(int port)
{
int serverSock;
int optval;
struct sockaddr_in serverAddr;
if ((serverSock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
logStream << "ERROR: Bad server socket!" << endl;
logIt(logStream);
return -1;
}
if (setsockopt(serverSock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
{
logStream << "ERROR: Cannot set server socket options!" << endl;
logIt(logStream);
return -1;
}
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = INADDR_ANY;
if (bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) != 0)
{
logStream << "ERROR: Bad server bind!" << endl;
logIt(logStream);
return -1;
}
if (listen(serverSock, 20) < 0)
{
logStream << "ERROR: Server cannot listen!" << endl;
logIt(logStream);
return -1;
}
return serverSock;
}
void sigchldHandler(int signal)
{
while (waitpid(-1, NULL, WNOHANG) > 0);
}
void sigtermHandler(int signal)
{
close(clientSock);
close(serverSock);
exit(0);
}
void serverLoop()
{
struct sockaddr_in clientAddr;
socklen_t addrLength = sizeof(clientAddr);
bool sendToFirst = true;
while (true)
{
clientSock = accept(serverSock, (struct sockaddr*)&clientAddr, &addrLength);
logStream << "LOOP CHOICE: " << (sendToFirst ? "first" : "second") << endl;
logIt(logStream);
// handle client connection in a separate process
if (fork() == 0)
{
close(serverSock);
logStream << "FORK CHOICE: " << getpid() << " " << (sendToFirst ? "first" : "second") << endl;
logIt(logStream);
handleClient(clientSock, clientAddr, sendToFirst);
exit(0);
}
sendToFirst = !sendToFirst;
close(clientSock);
}
}
void handleClient(int clientSock, struct sockaddr_in clientAddr, bool sendToFirst)
{
if ((remoteSock0 = createConnection(0)) < 0)
{
logStream << "ERROR: Cannot connect to host!" << endl;
logIt(logStream);
return;
}
if ((remoteSock1 = createConnection(1)) < 0)
{
logStream << "ERROR: Cannot connect to host!" << endl;
logIt(logStream);
return;
}
// a process forwarding data from client to remote socket
if (fork() == 0)
{
logStream << "FORK2 CHOICE: " << getpid() << " " << (sendToFirst ? "first" : "second") << endl;
logIt(logStream);
if(sendToFirst)
{
forwardData(clientSock, remoteSock0);
exit(0);
}
else
{
forwardData(clientSock, remoteSock1);
exit(0);
}
}
// a process forwarding data from remote socket to client
if (fork() == 0)
{
logStream << "FORK2 CHOICE: " << getpid() << " " << (sendToFirst ? "first" : "second") << endl;
logIt(logStream);
if(sendToFirst)
{
forwardData(remoteSock0, clientSock);
exit(0);
}
else
{
forwardData(remoteSock1, clientSock);
exit(0);
}
}
close(remoteSock0);
close(remoteSock1);
close(clientSock);
}
void forwardData(int sourceSock, int destinationSock)
{
char buffer[BUFFERSIZE];
int retVal;
// read data from input socket
while ((retVal = recv(sourceSock, buffer, BUFFERSIZE, 0)) > 0)
{
send(destinationSock, buffer, retVal, 0); // send data to output socket
}
shutdown(destinationSock, SHUT_RDWR); // stop other processes from using socket
close(destinationSock);
shutdown(sourceSock, SHUT_RDWR); // stop other processes from using socket
close(sourceSock);
}
int createConnection(int num)
{
struct sockaddr_in serverAddr0;
struct sockaddr_in serverAddr1;
struct hostent *server0;
struct hostent *server1;
bool first = true;
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
logStream << "ERROR: Bad client socket!" << endl;
logIt(logStream);
return -1;
}
if(num == 0)
{
if ((server0 = gethostbyname(remoteHost0)) == NULL)
{
logStream << "ERROR: Bad client resolve!" << endl;
logIt(logStream);
return -1;
}
memset(&serverAddr0, 0, sizeof(serverAddr0));
serverAddr0.sin_family = AF_INET;
memcpy(&serverAddr0.sin_addr.s_addr, server0->h_addr, server0->h_length);
serverAddr0.sin_port = htons(remotePort);
if (connect(sock, (struct sockaddr *) &serverAddr0, sizeof(serverAddr0)) < 0)
{
logStream << "ERROR: Bad client connection!" << endl;
logIt(logStream);
return -1;
}
}
else
{
if ((server1 = gethostbyname(remoteHost1)) == NULL)
{
logStream << "ERROR: Bad client resolve!" << endl;
logIt(logStream);
return -1;
}
memset(&serverAddr1, 0, sizeof(serverAddr1));
serverAddr1.sin_family = AF_INET;
memcpy(&serverAddr1.sin_addr.s_addr, server1->h_addr, server1->h_length);
serverAddr1.sin_port = htons(remotePort);
if (connect(sock, (struct sockaddr *) &serverAddr1, sizeof(serverAddr1)) < 0)
{
logStream << "ERROR: Bad client connection!" << endl;
logIt(logStream);
return -1;
}
}
return sock;
}