-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy paths5_conn.c
98 lines (80 loc) · 1.78 KB
/
s5_conn.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
#include "s5_conn.h"
#include "s5_conn_negotiate.h"
#include "s5_conn_relay.h"
#include "s5_conn_shutdown.h"
#define BUF_SIZE 4096 // 必须要足够大到放下每条negotiation消息
#define BUF_SIZE_MIN (256 * 2 + 1) // Username/Password Authentication Req
static s5_epoll_item_hdlr hdlrs[] = {
// SELECT_METHOD_REQ
h_select_method_req,
NULL,
// SELECT_METHOD_REPLY
NULL,
h_select_method_reply,
// CONNECT_REQ
h_connect_req,
NULL,
// RESOLVE_DOMAIN_NAME
NULL,
NULL,
// CONNECT_TARGET
NULL,
h_target_connected,
// CONNECT_REPLY
NULL,
NULL,
// RELAY
h_relay_in,
h_relay_out,
// SHUTDOWN
h_shutdown,
h_shutdown,
};
static s5_conn_t *s5_conn_create0(int fd, s5_epoll_t *ep, s5_server_t *s) {
s5_conn_t *c = s5_epoll_item_create(sizeof(s5_conn_t), hdlrs, -1, ep);
if (!c) {
return NULL;
}
c->fd = fd;
assert(BUF_SIZE >= BUF_SIZE_MIN);
c->buf = s5_buf_create(BUF_SIZE);
if (!c->buf) {
s5_epoll_item_destroy(c, ep);
return NULL;
}
c->peer = NULL;
c->s = s;
c->dn2ip_req_l = 0;
return c;
}
static void s5_conn_destroy0(s5_conn_t *c, s5_epoll_t *ep) {
if (c->fd >= 0) {
s5_close(c->fd);
c->fd = -1;
}
if (c->buf) {
s5_buf_destroy(c->buf);
c->buf = NULL;
}
c->peer = NULL;
c->s = NULL;
s5_epoll_item_destroy(c, ep);
}
s5_conn_t *s5_conn_create(int fd, s5_epoll_t *ep, s5_server_t *s) {
s5_conn_t *c1 = s5_conn_create0(fd, ep, s);
if (!c1) {
return NULL;
}
s5_conn_t *c2 = s5_conn_create0(-1, ep, s);
if (!c2) {
s5_epoll_item_destroy(c1, NULL);
return NULL;
}
c1->peer = c2;
c2->peer = c1;
return c1;
}
void s5_conn_destroy(s5_conn_t *c, s5_epoll_t *ep) {
s5_conn_destroy0(c->peer, ep);
s5_conn_destroy0(c, ep);
}