-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocks5_bind_processor.c
326 lines (272 loc) · 11.1 KB
/
socks5_bind_processor.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
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "logging.h"
#include "socks5_bind_processor.h"
#include "util.h"
static void socks5bind_processor_incref(struct s5bind_processor *processor);
static void socks5bind_processor_decref(struct s5bind_processor *processor);
static void socks5bind_processor_reset_all_cbs(struct s5bind_processor *processor);
static void socks5bind_processor_real_free(struct s5bind_processor *processor);
struct s5bind_processor {
struct evconnlistener *listener;
struct bufferevent *bev;
int write_is_shutdown;
struct event_base *evbase;
long tunnel_id;
struct socks5tunnel *tunnel;
int refcnt;
s5bind_processor_on_bind_success_cb on_bind_success_cb;
s5bind_processor_on_bind_error_cb on_bind_error_cb;
s5bind_processor_on_connection_success_cb on_connection_success_cb;
s5bind_processor_on_connection_error_cb on_connection_error_cb;
s5bind_processor_on_data_received_cb on_data_received_cb;
s5bind_processor_on_data_write_completed_cb on_data_write_completed_cb;
s5bind_processor_on_eof_cb on_eof_cb;
s5bind_processor_on_read_error_cb on_read_error_cb;
s5bind_processor_on_write_error_cb on_write_error_cb;
};
struct s5bind_processor *s5bind_processor_new(struct event_base *evbase,
struct socks5tunnel *tunnel, long tunnel_id)
{
struct s5bind_processor *processor = NULL;
processor = malloc(sizeof(struct s5bind_processor));
if (processor == NULL) {
error("malloc() for struct s5bind_processor failed: %s (errno=%d)", strerror(errno), errno);
return NULL;
}
memset(processor, 0, sizeof(struct s5bind_processor));
processor->evbase = evbase;
processor->tunnel_id = tunnel_id;
processor->tunnel = tunnel;
processor->refcnt = 1;
return processor;
}
void s5bind_processor_free(struct s5bind_processor *processor)
{
socks5bind_processor_reset_all_cbs(processor);
socks5bind_processor_decref(processor);
}
static void socks5bind_processor_incref(struct s5bind_processor *processor)
{
processor->refcnt++;
}
static void socks5bind_processor_decref(struct s5bind_processor *processor)
{
processor->refcnt--;
if (processor->refcnt == 0)
socks5bind_processor_real_free(processor);
}
static void socks5bind_processor_reset_all_cbs(struct s5bind_processor *processor)
{
processor->on_bind_success_cb = NULL;
processor->on_bind_error_cb = NULL;
processor->on_connection_success_cb = NULL;
processor->on_connection_error_cb = NULL;
processor->on_data_received_cb = NULL;
processor->on_data_write_completed_cb = NULL;
processor->on_eof_cb = NULL;
processor->on_read_error_cb = NULL;
processor->on_write_error_cb = NULL;
}
static void socks5bind_processor_real_free(struct s5bind_processor *processor)
{
if (processor->listener)
evconnlistener_free(processor->listener);
if (processor->bev)
bufferevent_free(processor->bev);
free(processor);
}
/* callbacks for listener */
void _listener_cb(struct evconnlistener *listener, evutil_socket_t connfd,
struct sockaddr *sa, int socklen, void *user_arg);
void _listener_error_cb(struct evconnlistener *listener, void *user_arg);
/* callbacks for accepted connection */
void _conn_read_cb(struct bufferevent *bev, void *user_arg);
void _conn_write_cb(struct bufferevent *bev, void *user_arg);
void _conn_event_cb(struct bufferevent *bev, short events, void *user_arg);
void s5bind_processor_start(struct s5bind_processor *processor)
{
debug("tunnel#%ld s5bind_processor_start", processor->tunnel_id);
socks5bind_processor_incref(processor);
struct sockaddr_in sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET; /* support IPv4 only */
sockaddr.sin_addr.s_addr = INADDR_ANY;
sockaddr.sin_port = htons(0);
processor->listener = evconnlistener_new_bind(processor->evbase, _listener_cb,
processor, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, 1,
(struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (processor->listener == NULL) {
error("tunnel#%ld evconnlistener_new_bind() failed: %s (errno=%d)",
processor->tunnel_id, strerror(errno), errno);
if (processor->on_bind_error_cb)
processor->on_bind_error_cb(processor->tunnel);
} else {
evconnlistener_set_error_cb(processor->listener, _listener_error_cb);
memset(&sockaddr, 0, sizeof(sockaddr));
socklen_t addrlen = sizeof(sockaddr);
int n = getsockname(evconnlistener_get_fd(processor->listener),
(struct sockaddr *)&sockaddr, &addrlen);
if (n == 0) {
if (processor->on_bind_success_cb)
processor->on_bind_success_cb(processor->tunnel, (struct sockaddr *)&sockaddr, addrlen);
} else {
error("tunnel#%ld getsockname() failed: %s (errno=%d)",
processor->tunnel_id, strerror(errno), errno);
if (processor->on_bind_error_cb)
processor->on_bind_error_cb(processor->tunnel);
}
}
socks5bind_processor_decref(processor);
}
int s5bind_processor_write(struct s5bind_processor *processor, struct evbuffer *buffer)
{
debug("tunnel#%ld s5bind_processor_write", processor->tunnel_id);
assert(processor->bev != NULL);
assert(processor->write_is_shutdown == 0);
return bufferevent_write_buffer(processor->bev, buffer);
}
void s5bind_processor_shutdown_write(struct s5bind_processor *processor)
{
debug("s5bind_processor_shutdown_write");
assert(processor->bev != NULL);
processor->write_is_shutdown = 1;
}
void _listener_cb(struct evconnlistener *listener, evutil_socket_t connfd,
struct sockaddr *sa, int socklen, void *user_arg)
{
struct s5bind_processor *processor = user_arg;
debug("tunnel#%ld _listener_cb", processor->tunnel_id);
socks5bind_processor_incref(processor);
/* We only need to accept 1 connection */
evconnlistener_free(listener);
processor->listener = NULL;
processor->bev = bufferevent_socket_new(processor->evbase, connfd, BEV_OPT_CLOSE_ON_FREE);
if (processor->bev == NULL) {
error("tunnel#%ld bufferevent_socket_new() failed: %s (errno=%d)",
processor->tunnel_id, strerror(errno), errno);
if (processor->on_connection_error_cb) {
debug("tunnel#%ld on_connection_error_cb", processor->tunnel_id);
processor->on_connection_error_cb(processor->tunnel);
}
} else {
bufferevent_setcb(processor->bev, _conn_read_cb, _conn_write_cb, _conn_event_cb, processor);
if (processor->on_connection_success_cb) {
debug("tunnel#%ld on_connection_success_cb", processor->tunnel_id);
processor->on_connection_success_cb(processor->tunnel, sa, socklen);
}
}
socks5bind_processor_decref(processor);
}
void _listener_error_cb(struct evconnlistener *listener, void *user_arg)
{
(void)listener;
struct s5bind_processor *processor = user_arg;
debug("tunnel#%ld _listener_error_cb", processor->tunnel_id);
socks5bind_processor_incref(processor);
if (processor->on_connection_error_cb) {
error("tunnel#%ld on_connection_error_cb", processor->tunnel_id);
processor->on_connection_error_cb(processor->tunnel);
}
socks5bind_processor_decref(processor);
}
void _conn_read_cb(struct bufferevent *bev, void *user_arg)
{
struct s5bind_processor *processor = user_arg;
debug("tunnel#%ld _conn_readcb: data received", processor->tunnel_id);
socks5bind_processor_incref(processor);
if (processor->on_data_received_cb)
processor->on_data_received_cb(processor->tunnel, bufferevent_get_input(bev));
socks5bind_processor_decref(processor);
}
void _conn_write_cb(struct bufferevent *bev, void *user_arg)
{
(void)bev;
struct s5bind_processor *processor = user_arg;
debug("tunnel#%ld _conn_write_cb: data write completed", processor->tunnel_id);
socks5bind_processor_incref(processor);
if (processor->write_is_shutdown) {
/* do real shutdown only when buffer is emptied */
int n = shutdown(bufferevent_getfd(processor->bev), SHUT_WR);
if (n == -1)
error("tunnel#%ld shutdown failed: %s (errno=%d)",
processor->tunnel_id, strerror(errno), errno);
}
if (processor->on_data_write_completed_cb)
processor->on_data_write_completed_cb(processor->tunnel);
socks5bind_processor_decref(processor);
}
void _conn_event_cb(struct bufferevent *bev, short event, void *user_arg)
{
(void)bev;
struct s5bind_processor *processor = user_arg;
char eventstr[128];
str_bufferevent_event(event, eventstr, sizeof(eventstr));
debug("tunnel#%ld s5bind_processor: event: %s", processor->tunnel_id, eventstr);
socks5bind_processor_incref(processor);
if (event & BEV_EVENT_EOF) {
info("tunnel#%ld bind processor: connection EOF encountered", processor->tunnel_id);
if (processor->on_eof_cb)
processor->on_eof_cb(processor->tunnel);
}
if (event & BEV_EVENT_ERROR) {
if (event & BEV_EVENT_READING) {
error("tunnel#%ld bind processor: connection read error", processor->tunnel_id);
if (processor->on_read_error_cb)
processor->on_read_error_cb(processor->tunnel);
} else {
error("tunnel#%ld bind processor: connection write error", processor->tunnel_id);
if (processor->on_write_error_cb)
processor->on_write_error_cb(processor->tunnel);
}
}
/* no possible: BEV_EVENT_CONNECTED|BEV_EVENT_TIMEOUT*/
socks5bind_processor_decref(processor);
}
void s5bind_processor_set_on_bind_success_cb(struct s5bind_processor *processor,
s5bind_processor_on_bind_success_cb cb)
{
processor->on_bind_success_cb = cb;
}
void s5bind_processor_set_on_bind_error_cb(struct s5bind_processor *processor,
s5bind_processor_on_bind_error_cb cb)
{
processor->on_bind_error_cb = cb;
}
void s5bind_processor_set_on_connection_success_cb(struct s5bind_processor *processor,
s5bind_processor_on_connection_success_cb cb)
{
processor->on_connection_success_cb = cb;
}
void s5bind_processor_set_on_connection_error_cb(struct s5bind_processor *processor,
s5bind_processor_on_connection_error_cb cb)
{
processor->on_connection_error_cb = cb;
}
void s5bind_processor_set_on_data_received_cb(struct s5bind_processor *processor,
s5bind_processor_on_data_received_cb cb)
{
processor->on_data_received_cb = cb;
}
void s5bind_processor_set_on_data_write_completed_cb(struct s5bind_processor *processor,
s5bind_processor_on_data_write_completed_cb cb)
{
processor->on_data_write_completed_cb = cb;
}
void s5bind_processor_set_on_eof_cb(struct s5bind_processor *processor,
s5bind_processor_on_eof_cb cb)
{
processor->on_eof_cb = cb;
}
void s5bind_processor_set_on_read_error_cb(struct s5bind_processor *processor,
s5bind_processor_on_read_error_cb cb)
{
processor->on_read_error_cb = cb;
}
void s5bind_processor_set_on_write_error_cb(struct s5bind_processor *processor,
s5bind_processor_on_write_error_cb cb)
{
processor->on_write_error_cb = cb;
}