-
Notifications
You must be signed in to change notification settings - Fork 153
/
proxy.c
478 lines (399 loc) · 13.8 KB
/
proxy.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Tiny TCP proxy server
*
* Author: Krzysztof Kliś <[email protected]>
* Fixes and improvements: Jérôme Poulin <[email protected]>
* IPv6 support: 04/2019 Rafael Ferrari <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version with the following modification:
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules,
* and to copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms
* and conditions of the license of that module. An independent module is a
* module which is not derived from or based on this library. If you modify this
* library, you may extend this exception to your version of the library, but
* you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <netdb.h>
#include <resolv.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/wait.h>
#ifdef USE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
#define BUF_SIZE 16384
#define READ 0
#define WRITE 1
#define SERVER_SOCKET_ERROR -1
#define SERVER_SETSOCKOPT_ERROR -2
#define SERVER_BIND_ERROR -3
#define SERVER_LISTEN_ERROR -4
#define CLIENT_SOCKET_ERROR -5
#define CLIENT_RESOLVE_ERROR -6
#define CLIENT_CONNECT_ERROR -7
#define CREATE_PIPE_ERROR -8
#define BROKEN_PIPE_ERROR -9
#define SYNTAX_ERROR -10
typedef enum {TRUE = 1, FALSE = 0} bool;
int check_ipversion(char * address);
int create_socket(int port);
void sigchld_handler(int signal);
void sigterm_handler(int signal);
void server_loop();
void handle_client(int client_sock, struct sockaddr_storage client_addr);
void forward_data(int source_sock, int destination_sock);
void forward_data_ext(int source_sock, int destination_sock, char *cmd);
int create_connection();
int parse_options(int argc, char *argv[]);
void plog(int priority, const char *format, ...);
int server_sock, client_sock, remote_sock, remote_port = 0;
int connections_processed = 0;
char *bind_addr, *remote_host, *cmd_in, *cmd_out;
bool foreground = FALSE;
bool use_syslog = FALSE;
#define BACKLOG 20 // how many pending connections queue will hold
/* Program start */
int main(int argc, char *argv[]) {
int local_port;
pid_t pid;
bind_addr = NULL;
local_port = parse_options(argc, argv);
if (local_port < 0) {
printf("Syntax: %s [-b bind_address] -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)] [-s (use syslog)]\n", argv[0]);
return local_port;
}
if (use_syslog) {
openlog("proxy", LOG_PID, LOG_DAEMON);
}
if ((server_sock = create_socket(local_port)) < 0) { // start server
plog(LOG_CRIT, "Cannot run server: %m");
return server_sock;
}
signal(SIGCHLD, sigchld_handler); // prevent ended children from becoming zombies
signal(SIGTERM, sigterm_handler); // handle KILL signal
if (foreground) {
server_loop();
} else {
switch(pid = fork()) {
case 0: // deamonized child
server_loop();
break;
case -1: // error
plog(LOG_CRIT, "Cannot daemonize: %m");
return pid;
default: // parent
close(server_sock);
}
}
if (use_syslog) {
closelog();
}
return EXIT_SUCCESS;
}
/* Parse command line options */
int parse_options(int argc, char *argv[]) {
int c, local_port = 0;
while ((c = getopt(argc, argv, "b:l:h:p:i:o:fs")) != -1) {
switch(c) {
case 'l':
local_port = atoi(optarg);
break;
case 'b':
bind_addr = optarg;
break;
case 'h':
remote_host = optarg;
break;
case 'p':
remote_port = atoi(optarg);
break;
case 'i':
cmd_in = optarg;
break;
case 'o':
cmd_out = optarg;
break;
case 'f':
foreground = TRUE;
break;
case 's':
use_syslog = TRUE;
break;
}
}
if (local_port && remote_host && remote_port) {
return local_port;
} else {
return SYNTAX_ERROR;
}
}
int check_ipversion(char * address)
{
/* Check for valid IPv4 or Iv6 string. Returns AF_INET for IPv4, AF_INET6 for IPv6 */
struct in6_addr bindaddr;
if (inet_pton(AF_INET, address, &bindaddr) == 1) {
return AF_INET;
} else {
if (inet_pton(AF_INET6, address, &bindaddr) == 1) {
return AF_INET6;
}
}
return 0;
}
/* Create server socket */
int create_socket(int port) {
int server_sock, optval = 1;
int validfamily=0;
struct addrinfo hints, *res=NULL;
char portstr[12];
memset(&hints, 0x00, sizeof(hints));
server_sock = -1;
hints.ai_flags = AI_NUMERICSERV; /* numeric service number, not resolve */
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* prepare to bind on specified numeric address */
if (bind_addr != NULL) {
/* check for numeric IP to specify IPv6 or IPv4 socket */
if (validfamily = check_ipversion(bind_addr)) {
hints.ai_family = validfamily;
hints.ai_flags |= AI_NUMERICHOST; /* bind_addr is a valid numeric ip, skip resolve */
}
} else {
/* if bind_address is NULL, will bind to IPv6 wildcard */
hints.ai_family = AF_INET6; /* Specify IPv6 socket, also allow ipv4 clients */
hints.ai_flags |= AI_PASSIVE; /* Wildcard address */
}
sprintf(portstr, "%d", port);
/* Check if specified socket is valid. Try to resolve address if bind_address is a hostname */
if (getaddrinfo(bind_addr, portstr, &hints, &res) != 0) {
return CLIENT_RESOLVE_ERROR;
}
if ((server_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
freeaddrinfo(res); // Free memory on failure
return SERVER_SOCKET_ERROR;
}
if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
freeaddrinfo(res);
return SERVER_SETSOCKOPT_ERROR;
}
if (bind(server_sock, res->ai_addr, res->ai_addrlen) == -1) {
close(server_sock);
freeaddrinfo(res);
return SERVER_BIND_ERROR;
}
if (listen(server_sock, BACKLOG) < 0) {
close(server_sock);
freeaddrinfo(res);
return SERVER_LISTEN_ERROR;
}
if (res != NULL) {
freeaddrinfo(res);
}
return server_sock;
}
/* Send log message to stderr or syslog */
void plog(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (use_syslog) {
vsyslog(priority, format, ap);
} else {
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
}
va_end(ap);
}
/* Update systemd status with connection count */
void update_connection_count()
{
#ifdef USE_SYSTEMD
sd_notifyf(0, "STATUS=Ready. %d connections processed.\n", connections_processed);
#endif
}
/* Handle finished child process */
void sigchld_handler(int signal) {
while (waitpid(-1, NULL, WNOHANG) > 0);
}
/* Handle term signal */
void sigterm_handler(int signal) {
close(client_sock);
close(server_sock);
exit(0);
}
/* Main server loop */
void server_loop() {
struct sockaddr_storage client_addr;
socklen_t addrlen = sizeof(client_addr);
#ifdef USE_SYSTEMD
sd_notify(0, "READY=1\n");
#endif
while (TRUE) {
update_connection_count();
client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
if (fork() == 0) { // handle client connection in a separate process
close(server_sock);
handle_client(client_sock, client_addr);
exit(0);
} else {
connections_processed++;
}
close(client_sock);
}
}
/* Handle client connection */
void handle_client(int client_sock, struct sockaddr_storage client_addr)
{
if ((remote_sock = create_connection()) < 0) {
plog(LOG_ERR, "Cannot connect to host: %m");
goto cleanup;
}
if (fork() == 0) { // a process forwarding data from client to remote socket
if (cmd_out) {
forward_data_ext(client_sock, remote_sock, cmd_out);
} else {
forward_data(client_sock, remote_sock);
}
exit(0);
}
if (fork() == 0) { // a process forwarding data from remote socket to client
if (cmd_in) {
forward_data_ext(remote_sock, client_sock, cmd_in);
} else {
forward_data(remote_sock, client_sock);
}
exit(0);
}
cleanup:
close(remote_sock);
close(client_sock);
}
/* Forward data between sockets */
void forward_data(int source_sock, int destination_sock) {
ssize_t n;
#ifdef USE_SPLICE
int buf_pipe[2];
if (pipe(buf_pipe) == -1) {
plog(LOG_ERR, "pipe: %m");
exit(CREATE_PIPE_ERROR);
}
while ((n = splice(source_sock, NULL, buf_pipe[WRITE], NULL, SSIZE_MAX, SPLICE_F_NONBLOCK|SPLICE_F_MOVE)) > 0) {
if (splice(buf_pipe[READ], NULL, destination_sock, NULL, SSIZE_MAX, SPLICE_F_MOVE) < 0) {
plog(LOG_ERR, "write: %m");
exit(BROKEN_PIPE_ERROR);
}
}
#else
char buffer[BUF_SIZE];
while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket
send(destination_sock, buffer, n, 0); // send data to output socket
}
#endif
if (n < 0) {
plog(LOG_ERR, "read: %m");
exit(BROKEN_PIPE_ERROR);
}
#ifdef USE_SPLICE
close(buf_pipe[0]);
close(buf_pipe[1]);
#endif
shutdown(destination_sock, SHUT_RDWR); // stop other processes from using socket
close(destination_sock);
shutdown(source_sock, SHUT_RDWR); // stop other processes from using socket
close(source_sock);
}
/* Forward data between sockets through external command */
void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
char buffer[BUF_SIZE];
int n, i, pipe_in[2], pipe_out[2];
if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes
plog(LOG_CRIT, "Cannot create pipe: %m");
exit(CREATE_PIPE_ERROR);
}
if (fork() == 0) {
dup2(pipe_in[READ], STDIN_FILENO); // replace standard input with input part of pipe_in
dup2(pipe_out[WRITE], STDOUT_FILENO); // replace standard output with output part of pipe_out
close(pipe_in[WRITE]); // close unused end of pipe_in
close(pipe_out[READ]); // close unused end of pipe_out
n = system(cmd); // execute command
exit(n);
} else {
close(pipe_in[READ]); // no need to read from input pipe here
close(pipe_out[WRITE]); // no need to write to output pipe here
while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket
if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command
plog(LOG_ERR, "Cannot write to pipe: %m");
exit(BROKEN_PIPE_ERROR);
}
if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output
send(destination_sock, buffer, i, 0); // send data to output socket
}
}
shutdown(destination_sock, SHUT_RDWR); // stop other processes from using socket
close(destination_sock);
shutdown(source_sock, SHUT_RDWR); // stop other processes from using socket
close(source_sock);
}
}
/* Create client connection */
int create_connection() {
struct addrinfo hints, *res=NULL;
int sock;
int validfamily=0;
char portstr[12];
memset(&hints, 0x00, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV; /* numeric service number, not resolve */
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
sprintf(portstr, "%d", remote_port);
/* check for numeric IP to specify IPv6 or IPv4 socket */
if (validfamily = check_ipversion(remote_host)) {
hints.ai_family = validfamily;
hints.ai_flags |= AI_NUMERICHOST; /* remote_host is a valid numeric ip, skip resolve */
}
/* Check if specified host is valid. Try to resolve address if remote_host is a hostname */
if (getaddrinfo(remote_host,portstr , &hints, &res) != 0) {
errno = EFAULT;
return CLIENT_RESOLVE_ERROR;
}
if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
freeaddrinfo(res);
return CLIENT_SOCKET_ERROR;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) {
freeaddrinfo(res); // Free memory on failure
return CLIENT_CONNECT_ERROR;
}
if (res != NULL) {
freeaddrinfo(res);
}
return sock;
}