-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.c
358 lines (277 loc) · 7.65 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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netdb.h>
#include "handler/error.h"
#include "handler/socket.h"
#include "handler/atomic.h"
#include "handler/signal.h"
#define BLOG (10) //back-log
#define EPOLL_SIZE (50)
#define BUFFER_SIZE (4096)
#define HEADER_SIZE (4096) // 4kb
#define MAX_LINE (1024)
struct event_data {
int pipe_fd[2];
struct event_data *target;
};
struct thread_args {
int buffer_size, header_size;
int type;
struct epoll_handler *handler[2];
};
inline int receive_header(int , char *, int);
inline int parse_header(const char *, int , void *);
inline int connect_server(struct event_data *, char *, int );
inline struct event_data *create_event_data(int , int , struct event_data *);
void *worker_thread(void *);
int main(int argc, char *argv[])
{
int proxy_sock, serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
struct event_data *ev_data;
struct epoll_handler ep_stoc, ep_ctos;
struct thread_args serv_arg, clnt_arg;
int ret;
if (argc != 2) {
err_msg("usage: %s <proxy_port>", ERR_DNG, argv[0]);
} else {
if (init_atomic_alloc() == -1)
err_msg("init_atomic_alloc() error", ERR_CTC);
}
proxy_sock = listen_socket(atoi(argv[1]), BLOG);
if (proxy_sock < 0)
err_msg("listen_sock() error: %d", ERR_CTC, proxy_sock);
if ( (ret = create_epoll_handler(&ep_ctos, EPOLL_SIZE)) != 0)
err_msg("create_epoll_handler(ctos) error: %d", ERR_CTC, ret);
if ( (ret = create_epoll_handler(&ep_stoc, EPOLL_SIZE)) != 0)
err_msg("create_epoll_handler(stoc) error: %d", ERR_CTC, ret);
serv_arg.type = 0;
serv_arg.header_size = 0;
serv_arg.buffer_size = BUFFER_SIZE;
serv_arg.handler[0] = &ep_stoc;
serv_arg.handler[1] = &ep_ctos;
if (pthread_create((pthread_t [1]) { 0 }, NULL, worker_thread, (void *)&serv_arg) != 0)
err_msg("pthread_create() error", ERR_CTC);
clnt_arg.type = 1;
clnt_arg.header_size = HEADER_SIZE;
clnt_arg.buffer_size = BUFFER_SIZE;
clnt_arg.handler[0] = &ep_ctos;
clnt_arg.handler[1] = &ep_stoc;
if (pthread_create((pthread_t [1]) { 0 }, NULL, worker_thread, (void *)&clnt_arg) != 0)
err_msg("pthread_create() error", ERR_CTC);
printf("start proxy server [%hu] \n", atoi(argv[1]));
for (;;)
{
clnt_sock = accept(
proxy_sock,
(struct sockaddr*)&clnt_adr,
(int []) { sizeof(struct sockaddr_in) }
);
if (clnt_sock == -1) {
err_msg("accept() error", ERR_CHK);
continue;
} else {
if ( (ret = nonblocking(clnt_sock)) != 0) {
close(clnt_sock);
err_msg("nonblocking() error: %d ", ERR_CHK, ret);
continue;
}
}
ev_data = create_event_data(clnt_sock, -1, NULL);
if (ev_data == NULL) {
close(clnt_sock);
err_msg("create_event_data() error", ERR_NRM);
continue;
}
if (register_epoll_handler(
&ep_ctos, clnt_sock,
EPOLLIN | EPOLLET, ev_data) != 0) {
close(clnt_sock);
atomic_free(ev_data);
err_msg("register_epoll_handler() error", ERR_NRM);
continue;
}
printf("connect client: %d \n", clnt_sock);
}
release_atomic_alloc();
return 0;
}
void *worker_thread(void *args)
{
struct event_data *ev_data;
struct thread_args *thd_arg = (struct thread_args *)args;
struct epoll_handler *handler = thd_arg->handler[0],
*target_handler = thd_arg->handler[1];
int buffer_size = thd_arg->buffer_size;
int header_size = thd_arg->header_size;
int count;
for(;;)
{
count = wait_epoll_handler(handler);
if (count == -1) {
err_msg("wait_epoll_handler() error: %d", ERR_CHK, count);
continue;
}
for (int i = 0; i < count; i++)
{
ev_data = get_epoll_handler(handler);
if (ev_data == NULL) {
err_msg("get_epoll_handler() error", ERR_CHK);
continue;
}
if (ev_data->pipe_fd[1] == -1)
{
int read_len;
char header[header_size];
if ( (read_len = connect_server(ev_data, header, header_size)) < 0) {
err_msg("connect_server() error", ERR_CHK);
goto CLEANUP;
}
struct event_data *serv_data =
create_event_data(ev_data->pipe_fd[1], ev_data->pipe_fd[0], ev_data);
serv_data->target = ev_data;
if (serv_data == NULL) {
err_msg("create_event_data() error", ERR_CHK);
goto CLEANUP;
}
if (register_epoll_handler(
target_handler, serv_data->pipe_fd[0],
EPOLLIN | EPOLLET, serv_data) != 0) {
err_msg("register_epoll_handler() error", ERR_NRM);
atomic_free(serv_data);
close(serv_data->pipe_fd[1]);
goto CLEANUP;
}
ev_data->target = serv_data;
write(ev_data->pipe_fd[1], header, read_len);
printf("establish connection [%d <-> %d] \n",
ev_data->pipe_fd[0], ev_data->pipe_fd[1]);
}
else
{
char buffer[buffer_size];
for (;;)
{
int str_len = read(ev_data->pipe_fd[0], buffer, buffer_size);
if (str_len == 0) {
printf("disconnect signal [%d -> %d] \n", ev_data->pipe_fd[0], ev_data->pipe_fd[1]);
close(ev_data->pipe_fd[1]);
goto CLEANUP;
} else if (str_len < 0) {
if (errno == EAGAIN) {
break;
} else {
err_msg("read() error", ERR_CHK);
close(ev_data->pipe_fd[1]);
goto CLEANUP;
}
} else {
printf("send data [%d -> %d] \n", ev_data->pipe_fd[0], ev_data->pipe_fd[1]);
write(ev_data->pipe_fd[1], buffer, str_len);
}
}
}
continue;
CLEANUP: ; {
release_epoll_handler(handler, ev_data->pipe_fd[0]);
close(ev_data->pipe_fd[0]);
atomic_free(ev_data);
}
}
}
return (void *)0;
}
int receive_header(int sock, char *header, int header_size)
{
int rd_len, str_len;
rd_len = 0;
for (;;)
{
str_len = read(sock, &header[rd_len], header_size - rd_len);
if (str_len == -1) {
if (errno == EAGAIN)
return -1;
else
return -2;
}
if (str_len == 0) return -3;
rd_len += str_len;
if (rd_len == header_size) {
header[header_size - 1] = '\0';
break;
}
header[rd_len] = '\0';
if (rd_len > 4)
if ( strstr(&header[rd_len - 5], "\r\n\r\n") != NULL)
break;
}
return rd_len;
}
int parse_header(const char *header, int type, void *ret)
{
char line[MAX_LINE];
switch(type)
{
case 0: ; //sockaddr_in
struct sockaddr_in sock_adr;
struct hostent *hostent;
const char *target, *endl;
char *host, *end;
target = "Host: ", endl = "\r\n";
host = strstr(header, target);
if (host == NULL) return -1;
else host += strlen(target);
end = strstr(host, endl);
if (end == NULL) return -2;
else strncpy(line, host, end - host);
line[end - host] = 0;
memset(&sock_adr, 0, sizeof(struct sockaddr_in));
if (translate_host(line, &sock_adr) < 0) {
printf("failed to translate_host() \n");
return -3;
}
*(struct sockaddr_in *)ret = sock_adr;
break;
case 1: // Method
break;
}
return 1;
}
int connect_server(struct event_data *ev_data, char *header, int header_size)
{
int read_len;
struct sockaddr_in serv_adr;
if ( (read_len = receive_header(ev_data->pipe_fd[0], header, header_size)) < 0) {
printf("failed to receive_header() \n");
return -1;
}
if (parse_header(header, 0, &serv_adr) < 0) {
printf("failed to parse_header() \n");
return -2;
}
if ( (ev_data->pipe_fd[1] = connect_socket2(&serv_adr, 0, 1)) < 0) {
printf("failed to connect_socket2() \n");
return -3;
}
return read_len;
}
struct event_data *create_event_data(int sock1, int sock2, struct event_data *target)
{
struct event_data *ev_data = atomic_alloc(sizeof(struct event_data));
if (ev_data == NULL)
return NULL;
ev_data->pipe_fd[0] = sock1;
ev_data->pipe_fd[1] = sock2;
ev_data->target = target;
return ev_data;
}