-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsocks5_tunnel.c
720 lines (625 loc) · 28.4 KB
/
socks5_tunnel.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include <assert.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#include "logging.h"
#include "socks5_authenticator.h"
#include "socks5_bind_processor.h"
#include "socks5_conn.h"
#include "socks5_protocol.h"
#include "socks5_proxy.h"
#include "socks5_proxy_dummy.h"
#include "socks5_server.h"
#include "socks5_tunnel.h"
#include "socks5_udp_associate_processor.h"
#include "util.h"
static void socks5tunnel_incref(struct socks5tunnel *tunnel);
static void socks5tunnel_decref(struct socks5tunnel *tunnel);
static void socks5tunnel_real_free(struct socks5tunnel *tunnel);
static void socks5tunnel_destroy_on_socks5conn_write_completed(struct socks5tunnel *tunnel);
/* called when socks5conn got EOF */
static void socks5tunnel_on_socks5conn_eof(struct socks5tunnel *tunnel);
/* for socks5conn */
/* called when received auth negotiation request from client */
static void socks5tunnel_on_socks5conn_auth_negotiation_req(struct socks5tunnel *tunnel,
const struct s5_auth_negotiation_request *req);
/* called when socks5conn authentication succeeded */
static void socks5tunnel_on_socks5conn_auth_success(struct socks5tunnel *tunnel);
/* called when socks5conn authentication failed */
static void socks5tunnel_on_socks5conn_auth_error(struct socks5tunnel *tunnel);
static void socks5tunnel_on_socks5conn_request(struct socks5tunnel *tunnel,
const struct s5_request *req);
static void socks5tunnel_on_socks5conn_data(struct socks5tunnel *tunnel, struct evbuffer *buffer);
/* for socks5 CMD=CONNECT proxy */
static void socks5tunnel_on_proxy_connection_success(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen);
static void socks5tunnel_on_proxy_connection_error(struct socks5tunnel *tunnel);
static void socks5tunnel_on_proxy_data_received(struct socks5tunnel *tunnel, struct evbuffer *buffer);
static void socks5tunnel_on_proxy_eof(struct socks5tunnel *tunnel);
static void socks5tunnel_on_proxy_read_error(struct socks5tunnel *tunnel);
static void socks5tunnel_on_proxy_write_error(struct socks5tunnel *tunnel);
static void socks5tunnel_on_proxy_data_write_completed(struct socks5tunnel *tunnel);
/* for socks5 CMD=BIND processor */
static void socks5tunnel_on_s5bind_processor_bind_success(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen);
static void socks5tunnel_on_s5bind_processor_bind_error_cb(struct socks5tunnel *tunnel);
static void socks5tunnel_on_s5bind_processor_connection_success_cb(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen);
static void socks5tunnel_on_s5bind_processor_connection_error_cb(struct socks5tunnel *tunnel);
static void socks5tunnel_on_s5bind_processor_data_received_cb(struct socks5tunnel *tunnel,
struct evbuffer *buffer);
static void socks5tunnel_on_s5bind_processor_data_write_completed_cb(struct socks5tunnel *tunnel);
static void socks5tunnel_on_s5bind_processor_eof_cb(struct socks5tunnel *tunnel);
static void socks5tunnel_on_s5bind_processor_read_error_cb(struct socks5tunnel *tunnel);
static void socks5tunnel_on_s5bind_processor_write_error_cb(struct socks5tunnel *tunnel);
/* for socks5 CMD=UDP_ASSOCIATE processor */
static void socks5tunnel_on_s5udp_associate_processor_success_cb(struct socks5tunnel *tunnel,
const struct sockaddr *serv_addr, socklen_t addrlen);
static void socks5tunnel_on_s5udp_associate_processor_error_cb(struct socks5tunnel *tunnel);
/* socks5conn; proxyconn */
struct socks5tunnel {
long id;
struct socks5server *serv;
struct socks5conn *socks5conn;
int socks5conn_eof;
uint8_t socks5cmd;
struct proxy *proxy; /* only when socks5cmd = SOCKS5_CMD_CONNECT */
int proxy_eof;
struct s5bind_processor *bind_processor; /* only when socks5cmd = SOCKS5_CMD_BIND */
int bind_processor_eof;
struct s5udp_associate_processor *udp_processor;
int refcnt;
};
struct socks5tunnel *socks5tunnel_new(struct socks5server *serv, evutil_socket_t connfd,
struct sockaddr *peeraddr, int addrlen)
{
(void)addrlen;
struct socks5conn *socks5conn = NULL;
struct socks5tunnel *tunnel = NULL;
socks5conn = socks5conn_new(socks5server_get_event_base(serv), connfd);
if (socks5conn == NULL) {
error("socks5conn_new() failed: %s (errno=%d)", strerror(errno), errno);
close(connfd);
goto FAIL;
}
tunnel = malloc(sizeof(struct socks5tunnel));
if (tunnel == NULL) {
error("malloc() for socks5tunnel failed: %s (errno=%d)", strerror(errno), errno);
goto FAIL;
}
memset(tunnel, 0, sizeof(struct socks5tunnel));
tunnel->id = socks5server_gen_id(serv);
tunnel->serv = serv;
tunnel->socks5conn = socks5conn;
tunnel->socks5conn_eof = 0;
tunnel->proxy = NULL;
tunnel->proxy_eof = 0;
tunnel->bind_processor = NULL;
tunnel->bind_processor_eof = 0;
tunnel->udp_processor = NULL;
tunnel->refcnt = 1;
socks5conn_set_tunnel(socks5conn, tunnel, tunnel->id);
/* set callbacks */
socks5conn_set_on_eof_cb(socks5conn, socks5tunnel_on_socks5conn_eof);
/* on read/write error, just destroy the tunnel */
socks5conn_set_on_read_error_cb(socks5conn, socks5tunnel_free);
socks5conn_set_on_write_error_cb(socks5conn, socks5tunnel_free);
socks5conn_set_on_auth_negotiation_req_cb(socks5conn, socks5tunnel_on_socks5conn_auth_negotiation_req);
socks5conn_set_on_auth_success_cb(socks5conn, socks5tunnel_on_socks5conn_auth_success);
socks5conn_set_on_auth_error_cb(socks5conn, socks5tunnel_on_socks5conn_auth_error);
socks5conn_set_on_s5_request_cb(socks5conn, socks5tunnel_on_socks5conn_request);
char addrstr[SOCKADDR_STRLEN];
sockaddr_ntop(peeraddr, addrstr, sizeof(addrstr));
debug("tunnel#%ld created, from %s", tunnel->id, addrstr);
return tunnel;
FAIL:
if (tunnel)
free(tunnel);
if (socks5conn)
socks5conn_free(socks5conn);
return NULL;
}
void socks5tunnel_free(struct socks5tunnel *tunnel)
{
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_incref(struct socks5tunnel *tunnel)
{
tunnel->refcnt++;
}
static void socks5tunnel_decref(struct socks5tunnel *tunnel)
{
tunnel->refcnt--;
if (tunnel->refcnt == 0)
socks5tunnel_real_free(tunnel);
}
static void socks5tunnel_real_free(struct socks5tunnel *tunnel)
{
debug("tunnel#%ld socks5tunnel destroy", tunnel->id);
socks5conn_free(tunnel->socks5conn);
if (tunnel->proxy)
proxy_free(tunnel->proxy);
if (tunnel->bind_processor)
s5bind_processor_free(tunnel->bind_processor);
if (tunnel->udp_processor)
s5udp_associate_processor_free(tunnel->udp_processor);
free(tunnel);
}
long socks5tunnel_get_id(struct socks5tunnel *tunnel)
{
return tunnel->id;
}
static void socks5tunnel_destroy_on_socks5conn_write_completed(struct socks5tunnel *tunnel)
{
socks5conn_set_on_write_completed_cb(tunnel->socks5conn, socks5tunnel_free);
}
static void socks5tunnel_on_socks5conn_eof(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld: socks5conn EOF", tunnel->id);
tunnel->socks5conn_eof = 1;
socks5conn_stop_read(tunnel->socks5conn);
if (socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY) {
if (tunnel->socks5cmd == SOCKS5_CMD_CONNECT) {
assert(tunnel->proxy != NULL);
if (tunnel->proxy_eof)
socks5tunnel_free(tunnel);
else
proxy_shutdown_write(tunnel->proxy);
} else if (tunnel->socks5cmd == SOCKS5_CMD_BIND) {
assert(tunnel->bind_processor != NULL);
if (tunnel->bind_processor_eof)
socks5tunnel_free(tunnel);
else {
s5bind_processor_shutdown_write(tunnel->bind_processor);
}
} else { /* SOCKS5_CMD_UDP_ASSOCIATE */
assert(tunnel->socks5cmd == SOCKS5_CMD_UDP_ASSOCIATE);
assert(tunnel->udp_processor != NULL);
socks5tunnel_free(tunnel);
}
} else {
/* for other stages, just destroy the tunnel */
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_socks5conn_auth_negotiation_req(struct socks5tunnel *tunnel,
const struct s5_auth_negotiation_request *req)
{
socks5tunnel_incref(tunnel);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_NEGOTIATION);
struct s5auth_manager *auth_manager = socks5server_get_auth_manager(tunnel->serv);
assert(auth_manager != NULL);
struct authenticator *authenticator = socks5conn_choose_authenticator(tunnel->socks5conn,
auth_manager, req);
struct s5_auth_negotiation_reply res;
res.version = SOCKS_V5;
if (req->version == SOCKS_V5 && authenticator)
res.method = authenticator->auth_method;
else
res.method = SOCKS5_AUTH_METHOD_NO_ACCEPTABLE_METHOD;
int n = socks5conn_write_s5_auth_negotiation_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
if (res.method == SOCKS5_AUTH_METHOD_NO_ACCEPTABLE_METHOD) {
/* do nothing, wait for the client to close connection */
} else {
/* go to next stage */
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_AUTHENTICATION);
if (res.method == SOCKS5_AUTH_METHOD_NO_AUTH_REQUIRED)
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_REQUEST_PROCESS);
}
} else { /* YM_ERROR */
error("socks5conn_write_s5_auth_negotiation_reply failed: %s (errno=%d)", strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_socks5conn_auth_success(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld authentication succeeded", tunnel->id);
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_REQUEST_PROCESS);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_socks5conn_auth_error(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld authentication failed", tunnel->id);
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_REQUEST_PROCESS);
socks5conn_stop_read(tunnel->socks5conn);
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
socks5tunnel_decref(tunnel);
}
/* Maybe we can make a separate rule set module */
static void socks5tunnel_on_socks5conn_request(struct socks5tunnel *tunnel,
const struct s5_request *req)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_socks5conn_request", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
struct s5_reply res;
res.version = SOCKS_V5;
res.rsv = 0;
uint8_t cmd = req->cmd;
if ((cmd != SOCKS5_CMD_CONNECT && cmd != SOCKS5_CMD_BIND && cmd != SOCKS5_CMD_UDP_ASSOCIATE)
|| req->version != SOCKS_V5 || req->rsv != 0) {
if (cmd != SOCKS5_CMD_CONNECT && cmd != SOCKS5_CMD_BIND && cmd != SOCKS5_CMD_UDP_ASSOCIATE)
res.reply = SOCKS5_REP_COMMAND_NOT_SUPPORTED;
else
res.reply = SOCKS5_REP_GENERAL_FAILURE;
goto FAIL;
}
tunnel->socks5cmd = req->cmd;
if (req->cmd == SOCKS5_CMD_CONNECT) { /* CONNECT */
assert(tunnel->proxy == NULL);
struct event_base *evbase = socks5server_get_event_base(tunnel->serv);
struct evdns_base *dns_base = socks5server_get_dns_base(tunnel->serv);
tunnel->proxy = dummy_proxy_new(tunnel, tunnel->id, evbase, dns_base);
if (tunnel->proxy == NULL) {
error("tunnel#%ld dummy_proxy_new() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
res.reply = SOCKS5_REP_GENERAL_FAILURE;
goto FAIL;
}
/* set callbacks for proxy */
proxy_set_connection_success_cb(tunnel->proxy, socks5tunnel_on_proxy_connection_success);
proxy_set_connection_error_cb(tunnel->proxy, socks5tunnel_on_proxy_connection_error);
proxy_set_data_received_cb(tunnel->proxy, socks5tunnel_on_proxy_data_received);
proxy_set_data_write_completed_cb(tunnel->proxy, socks5tunnel_on_proxy_data_write_completed);
proxy_set_eof_cb(tunnel->proxy, socks5tunnel_on_proxy_eof);
proxy_set_read_error_cb(tunnel->proxy, socks5tunnel_on_proxy_read_error);
proxy_set_write_error_cb(tunnel->proxy, socks5tunnel_on_proxy_write_error);
proxy_connect(tunnel->proxy, req->addr_type, &req->dest_addr, req->dest_port);
} else if (req->cmd == SOCKS5_CMD_BIND) { /* BIND */
assert(tunnel->bind_processor == NULL);
struct event_base *evbase = socks5server_get_event_base(tunnel->serv);
tunnel->bind_processor = s5bind_processor_new(evbase, tunnel, tunnel->id);
if (tunnel->bind_processor == NULL) {
error("tunnel#%ld s5bind_processor_new() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
res.reply = SOCKS5_REP_GENERAL_FAILURE;
goto FAIL;
}
struct s5bind_processor *processor = tunnel->bind_processor;
s5bind_processor_set_on_bind_success_cb(processor, socks5tunnel_on_s5bind_processor_bind_success);
s5bind_processor_set_on_bind_error_cb(processor, socks5tunnel_on_s5bind_processor_bind_error_cb);
s5bind_processor_set_on_connection_success_cb(processor, socks5tunnel_on_s5bind_processor_connection_success_cb);
s5bind_processor_set_on_connection_error_cb(processor, socks5tunnel_on_s5bind_processor_connection_error_cb);
s5bind_processor_set_on_data_received_cb(processor, socks5tunnel_on_s5bind_processor_data_received_cb);
s5bind_processor_set_on_data_write_completed_cb(processor, socks5tunnel_on_s5bind_processor_data_write_completed_cb);
s5bind_processor_set_on_eof_cb(processor, socks5tunnel_on_s5bind_processor_eof_cb);
s5bind_processor_set_on_read_error_cb(processor, socks5tunnel_on_s5bind_processor_read_error_cb);
s5bind_processor_set_on_write_error_cb(processor, socks5tunnel_on_s5bind_processor_write_error_cb);
s5bind_processor_start(tunnel->bind_processor);
} else { /* UDP ASSOCIATE */
assert(tunnel->udp_processor == NULL);
/* create udp processor here */
struct event_base *evbase = socks5server_get_event_base(tunnel->serv);
struct evdns_base *dns_base = socks5server_get_dns_base(tunnel->serv);
struct sockaddr_storage ss;
socklen_t addrlen = sizeof(ss);
if (socks5conn_getpeername(tunnel->socks5conn, (struct sockaddr *)&ss, &addrlen) == -1) {
error("tunnel#%ld socks5conn_getpeername() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
res.reply = SOCKS5_REP_GENERAL_FAILURE;
goto FAIL;
}
tunnel->udp_processor = s5udp_associate_processor_new(evbase, dns_base, tunnel, tunnel->id,
(struct sockaddr *)&ss, addrlen);
if (tunnel->udp_processor == NULL) {
error("tunnel#%ld s5udp_associate_processor_new() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
res.reply = SOCKS5_REP_GENERAL_FAILURE;
goto FAIL;
}
s5udp_associate_processor_start(tunnel->udp_processor,
socks5tunnel_on_s5udp_associate_processor_success_cb,
socks5tunnel_on_s5udp_associate_processor_error_cb);
}
socks5tunnel_decref(tunnel);
return;
FAIL:
assert(res.reply != SOCKS5_REP_SUCCEEDED);
socks5conn_stop_read(tunnel->socks5conn);
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS)
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
else {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_socks5conn_data(struct socks5tunnel *tunnel, struct evbuffer *buffer)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_socks5conn_data", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT || tunnel->socks5cmd == SOCKS5_CMD_BIND);
if (tunnel->socks5cmd == SOCKS5_CMD_CONNECT) {
assert(tunnel->proxy != NULL);
if (proxy_write(tunnel->proxy, buffer) == -1) {
error("tunnel#%ld proxy_write() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
} else { /* SOCKS5_CMD_BIND */
assert(tunnel->bind_processor != NULL);
if (s5bind_processor_write(tunnel->bind_processor, buffer) == -1) {
error("tunnel#%ld s5bind_processor_write() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_connection_success(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen)
{
(void)addrlen;
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_proxy_connection_success", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
struct s5_reply res;
s5_reply_init(&res, SOCKS5_REP_SUCCEEDED, addr);
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
debug("now go to next stage: SOCKS5_STAGE_PROXY");
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_PROXY);
socks5conn_set_on_data_cb(tunnel->socks5conn, socks5tunnel_on_socks5conn_data);
} else {
error("socks5conn_write_s5_reply() failed: %s (errno=%d)", strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_connection_error(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_proxy_connection_error", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
struct s5_reply res;
res.version = SOCKS_V5;
res.rsv = 0;
res.reply = SOCKS5_REP_GENERAL_FAILURE;
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
} else {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_data_received(struct socks5tunnel *tunnel, struct evbuffer *buffer)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_proxy_data_received", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
int n = socks5conn_write_data(tunnel->socks5conn, buffer);
if (n == YM_ERROR) {
error("tunnel#%ld socks5conn_write_data() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_eof(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_proxy_eof", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
tunnel->proxy_eof = 1;
if (tunnel->socks5conn_eof)
socks5tunnel_free(tunnel);
else {
socks5conn_shutdown_write(tunnel->socks5conn);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_read_error(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_proxy_read_error", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
socks5tunnel_free(tunnel);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_write_error(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_proxy_write_error", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
socks5tunnel_free(tunnel);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_proxy_data_write_completed(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_CONNECT);
debug("tunnel#%ld socks5tunnel_on_proxy_data_write_completed", tunnel->id);
socks5tunnel_decref(tunnel);
}
/* for socks5 CMD=BIND processor */
static void socks5tunnel_on_s5bind_processor_bind_success(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen)
{
(void)addrlen;
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_s5bind_processor_bind_success", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
/* the 1st reply */
struct s5_reply res;
s5_reply_init(&res, SOCKS5_REP_SUCCEEDED, addr);
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if(n == YM_ERROR) {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_bind_error_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_s5bind_processor_bind_error_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
struct s5_reply res;
res.version = SOCKS_V5;
res.rsv = 0;
res.reply = SOCKS5_REP_GENERAL_FAILURE;
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
} else {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_connection_success_cb(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen)
{
(void)addrlen;
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_s5bind_processor_connection_success_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
/* the 2nd reply */
struct s5_reply res;
s5_reply_init(&res, SOCKS5_REP_SUCCEEDED, addr);
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
debug("now go to next stage: SOCKS5_STAGE_PROXY");
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_PROXY);
socks5conn_set_on_data_cb(tunnel->socks5conn, socks5tunnel_on_socks5conn_data);
} else {
error("socks5conn_write_s5_reply() failed: %s (errno=%d)", strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_connection_error_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_s5bind_processor_connection_error_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
struct s5_reply res;
res.version = SOCKS_V5;
res.rsv = 0;
res.reply = SOCKS5_REP_GENERAL_FAILURE;
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
} else {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_data_received_cb(struct socks5tunnel *tunnel,
struct evbuffer *buffer)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_s5bind_processor_data_received_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
int n = socks5conn_write_data(tunnel->socks5conn, buffer);
if (n == YM_ERROR) {
error("tunnel#%ld socks5conn_write_data() failed: %s (errno=%d)", tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_data_write_completed_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
debug("tunnel#%ld socks5tunnel_on_s5bind_processor_data_write_completed_cb", tunnel->id);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_eof_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_s5bind_processor_eof_cb. we now close the tunnel.", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
tunnel->bind_processor_eof = 1;
if (tunnel->socks5conn_eof)
socks5tunnel_free(tunnel);
else {
socks5conn_shutdown_write(tunnel->socks5conn);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_read_error_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_s5bind_processor_read_error_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
socks5tunnel_free(tunnel);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5bind_processor_write_error_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_s5bind_processor_write_error_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_PROXY);
assert(tunnel->socks5cmd == SOCKS5_CMD_BIND);
socks5tunnel_free(tunnel);
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5udp_associate_processor_success_cb(struct socks5tunnel *tunnel,
const struct sockaddr *addr, socklen_t addrlen)
{
(void)addrlen;
socks5tunnel_incref(tunnel);
debug("tunnel#%ld socks5tunnel_on_s5udp_associate_processor_success_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_UDP_ASSOCIATE);
struct s5_reply res;
s5_reply_init(&res, SOCKS5_REP_SUCCEEDED, addr);
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
debug("now go to next stage: SOCKS5_STAGE_PROXY");
socks5conn_set_stage(tunnel->socks5conn, SOCKS5_STAGE_PROXY);
} else {
error("socks5conn_write_s5_reply() failed: %s (errno=%d)", strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}
static void socks5tunnel_on_s5udp_associate_processor_error_cb(struct socks5tunnel *tunnel)
{
socks5tunnel_incref(tunnel);
error("tunnel#%ld socks5tunnel_on_s5udp_associate_processor_error_cb", tunnel->id);
assert(socks5conn_get_stage(tunnel->socks5conn) == SOCKS5_STAGE_REQUEST_PROCESS);
assert(tunnel->socks5cmd == SOCKS5_CMD_UDP_ASSOCIATE);
struct s5_reply res;
res.version = SOCKS_V5;
res.rsv = 0;
res.reply = SOCKS5_REP_GENERAL_FAILURE;
int n = socks5conn_write_s5_reply(tunnel->socks5conn, &res);
if (n == YM_SUCCESS) {
socks5tunnel_destroy_on_socks5conn_write_completed(tunnel);
} else {
error("tunnel#%ld socks5conn_write_s5_reply() failed: %s (errno=%d)",
tunnel->id, strerror(errno), errno);
socks5tunnel_free(tunnel);
}
socks5tunnel_decref(tunnel);
}