-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp_proxy.c
324 lines (250 loc) · 6.62 KB
/
tcp_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
#include "tcp_proxy.h"
struct conn {
int client_sock;
int upstream_sock;
pthread_t sender;
pthread_t receiver;
struct sockaddr_in *address;
};
struct listener {
struct sockaddr_in *address;
int port;
};
int
main(int argc, char *argv[])
{
struct sockaddr_in *address_http;
int opt, i, num_ports;
char *ip_addr = NULL, *host = NULL;
char* ports_array[MAX_PORTS];
pthread_t threads[MAX_PORTS];
char ports_string[2048] = "80,443";
struct listener *listener;
struct option opts[] = {
{ "ip", required_argument, NULL, 'i' },
{ "host", required_argument, NULL, 'h' },
{ "ports", required_argument, NULL, 'p' },
{ NULL, 0, NULL, 0 }
};
/* Ignore broken pipes */
signal(SIGPIPE, SIG_IGN);
while ((opt = getopt_long(argc, argv, "i:h:p:", opts, NULL)) != -1) {
switch(opt) {
case 'i':
ip_addr = optarg;
break;
case 'h':
host = optarg;
break;
case 'p':
strcpy(ports_string, optarg);
break;
default: print_usage(argv[0]);
}
}
if (argc < 2) {
print_usage(argv[0]);
}
if (ip_addr != NULL && host != NULL) {
printf("You can't specify both ip and host\n");
return 1;
}
if (ip_addr == NULL && host == NULL) {
printf("You need to specify either ip or host\n");
return 1;
}
debug("Starting server");
char *stringp = ports_string;
for(num_ports = 0; num_ports < MAX_PORTS && stringp; num_ports++) {
const char *delim = ",";
char *token = strsep(&stringp, delim);
ports_array[num_ports] = token;
}
if (ip_addr) {
address_http = addr_from_ip(ip_addr);
}
if (host) {
address_http = addr_from_host(host);
}
for (i = 0; i < num_ports; i++) {
int port = atoi(ports_array[i]);
debug("Starting listener thread for port %d", port);
listener = malloc(sizeof(struct listener));
listener->address = malloc(sizeof(struct sockaddr_in));
listener->port = port;
memcpy((void *)listener->address, (void *)address_http, sizeof(struct sockaddr_in));
listener->address->sin_port=htons(port);
pthread_create(&threads[i], NULL, listen_conn, listener);
}
for (i = 0; i < num_ports; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
void
print_address(struct sockaddr_in* p)
{
char ipstr[INET6_ADDRSTRLEN];
struct in_addr *addr = &(p->sin_addr);
inet_ntop(AF_INET, addr, ipstr, sizeof ipstr);
}
struct sockaddr_in*
addr_from_host(char host[])
{
struct addrinfo hints, *res, *p;
int status;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(host, NULL, &hints, &res) != 0)) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
die("getaddrinfo failed");
}
for(p=res; p!=NULL; p=p->ai_next) {
print_address((struct sockaddr_in *)p->ai_addr);
}
return (struct sockaddr_in *)res->ai_addr;
}
struct sockaddr_in*
addr_from_ip(char ip[])
{
struct sockaddr_in *res;
res = malloc(sizeof(struct sockaddr_in));
memset(res, 0, sizeof(struct sockaddr_in));
inet_aton(ip, &(res->sin_addr));
res->sin_family = AF_INET;
return res;
}
void
sock_setreuse(int fd, int reuse)
{
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
die("Failed to set SO_REUSEADDR");
}
void
sock_settimeout(int sockfd, int timeout)
{
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
}
int
listen_to_port(int port)
{
char sport[100];
struct addrinfo hints, *res;
int sockfd;
sprintf(sport, "%d", port);
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, sport, &hints, &res);
if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
die("socket creation failed");
}
sock_setreuse(sockfd, 1);
if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
die("bind failed");
}
listen(sockfd, 5);
return sockfd;
}
void *
sender(void *args)
{
struct conn *session;
int read_size;
char message[MAX_MSG_SIZE];
session = (struct conn*) args;
while((read_size = recv(session->client_sock, message, MAX_MSG_SIZE, 0)) > 0 )
{
send(session->upstream_sock , message , read_size, 0);
memset(message, 0, MAX_MSG_SIZE);
}
if (errno != 0) {
printf("sender: %d %s\n", errno, strerror(errno));
}
return NULL;
}
void *
receiver(void *args)
{
struct conn *session;
int read_size;
char message[MAX_MSG_SIZE];
session = (struct conn*) args;
while((read_size = recv(session->upstream_sock, message, MAX_MSG_SIZE, 0)) > 0 )
{
send(session->client_sock , message , read_size, 0);
memset(message, 0, MAX_MSG_SIZE);
}
if (errno != 0) {
log_errno("receiver");
}
return NULL;
}
void *
client_handler(void *args)
{
struct conn *session;
struct sockaddr_in *p;
session = (struct conn*)args;
p = session->address;
if ((session->upstream_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
die("Couldn't open socket for connect");
}
sock_settimeout(session->upstream_sock, 60);
sock_settimeout(session->client_sock, 60);
if (connect(session->upstream_sock, (struct sockaddr *)p, sizeof(struct sockaddr_in))) {
die("Couldn't connect");
}
if (pthread_create( &session->sender, NULL, sender, session)) {
die("Can't create sender thread");
}
if (pthread_create( &session->receiver, NULL, receiver, session)) {
die("Can't create receiver thread");
}
pthread_join(session->receiver, NULL);
pthread_join(session->sender, NULL);
close(session->client_sock);
close(session->upstream_sock);
free(session);
return NULL;
}
void
accept_connections(int socketfd, struct sockaddr_in *address)
{
int client_sock;
struct sockaddr_storage *client_addr;
socklen_t addr_size;
struct conn *session;
pthread_t thread_id;
client_addr = malloc(sizeof(struct sockaddr_storage));
addr_size = sizeof(client_addr);
while ((client_sock = accept(socketfd, (struct sockaddr *)client_addr, &addr_size)))
{
session = malloc(sizeof(struct conn));
session->client_sock=client_sock;
session->address=address;
if (pthread_create( &thread_id, NULL, client_handler, session)) {
die("Couldn't create thread");
}
pthread_detach(thread_id);
}
}
void *
listen_conn(void *args)
{
struct listener* listener = (struct listener*)args;
int socket_http = listen_to_port(listener->port);
accept_connections(socket_http, listener->address);
return NULL;
}
void
print_usage(char* name)
{
printf("Usage: %s [--ip IP_ADDRESS] [--host HOST] [--ports PORT1,PORT2]\n", name);
exit(1);
}