Debian Codegraph @

third-party/libhv-v1.3.3/examples/kcptun/server/main.cpp

raw
1 /*
2 * kcptun server
3 *
4 * @build: ./configure --with-kcp && make clean && make kcptun examples
5 * @tcp_server: bin/tcp_echo_server 1234
6 * @kcptun_server: bin/kcptun_server -l :4000 -t 127.0.0.1:1234
7 * @kcptun_client: bin/kcptun_client -l :8388 -r 127.0.0.1:4000
8 * @tcp_client: bin/nc 127.0.0.1 8388
9 * > hello
10 * < hello
11 */
12
13 #define WITH_KCP 1
14 #include "hversion.h"
15 #include "hmain.h"
16 #include "hsocket.h"
17 #include "hloop.h"
18
19 #include "../smux/smux.h"
20
21 // config
22 static const char* localaddr = ":4000";
23 static const char* targetaddr = "127.0.0.1:8080";
24 static const char* mode = "fast";
25 static int mtu = 1350;
26 static int sndwnd = 1024;
27 static int rcvwnd = 1024;
28
29 // long options
30 static const option_t long_options[] = {
31 {'h', "help", NO_ARGUMENT, "Print this information"},
32 {'v', "version", NO_ARGUMENT, "Print version"},
33 {'d', "daemon", NO_ARGUMENT, "Daemonize"},
34 {'l', "listen", REQUIRED_ARGUMENT, "kcp server listen address (default: \":4000\")"},
35 {'t', "target", REQUIRED_ARGUMENT, "target server address (default: \"127.0.0.1:8080\")"},
36 {'m', "mode", REQUIRED_ARGUMENT, "profiles: fast3, fast2, fast, normal, (default: \"fast\")"},
37 { 0, "mtu", REQUIRED_ARGUMENT, "set maxinum transmission unit for UDP packets (default: 1350)"},
38 { 0, "sndwnd", REQUIRED_ARGUMENT, "set send window size(num of packets) (default: 1024)"},
39 { 0, "rcvwnd", REQUIRED_ARGUMENT, "set receive window size(num of packets) (default: 1024)"},
40 };
41
42 static void print_version() {
43 printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
44 }
45
46 static void print_help() {
47 char detail_options[1024] = {0};
48 dump_opt_long(long_options, ARRAY_SIZE(long_options), detail_options, sizeof(detail_options));
49 printf("%s\n", detail_options);
50 }
51
52 static kcp_setting_t s_kcp_setting;
53 static char kcp_host[64] = "0.0.0.0";
54 static int kcp_port = 4000;
55 static hio_t* kcp_io = NULL;
56
57 static char target_host[64] = "127.0.0.1";
58 static int target_port = 8080;
59
60 static smux_config_t smux_config;
61 static smux_session_t smux_session;
62
63 static int verbose = 1;
64
65 /* workflow:
66 *
67 * hloop_create_udp_server -> on_recvfrom ->
68 *
69 * SYN -> hloop_create_tcp_client ->
70 * on_connect -> hio_write(kcp_io, SYN) ->
71 * on_read -> hio_write(kcp_io) ->
72 * on_close -> hio_write(kcp_io, FIN) -> smux_session_close_stream
73 *
74 * PSH -> smux_session_get_stream -> hio_write(stream_io)
75 *
76 * FIN -> smux_session_get_stream -> hio_close(stream_io)
77 *
78 */
79
80 // hloop_create_udp_server -> hio_set_kcp -> on_recvfrom ->
81 // SYN -> hloop_create_tcp_client -> smux_session_open_stream ->
82 // PSH -> hio_write(io)
83
84 static void on_close(hio_t* io) {
85 // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
86 if (verbose) {
87 char localaddrstr[SOCKADDR_STRLEN] = {0};
88 char peeraddrstr[SOCKADDR_STRLEN] = {0};
89 printf("disconnected connfd=%d [%s] => [%s]\n", hio_fd(io),
90 SOCKADDR_STR(hio_localaddr(io), localaddrstr),
91 SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
92 }
93
94 smux_stream_t* smux_stream = (smux_stream_t*)hevent_userdata(io);
95 if (smux_stream == NULL) return;
96
97 // FIN
98 int packlen = smux_stream_output(smux_stream, SMUX_CMD_FIN);
99 if (packlen > 0) {
100 // printf("FIN %d\n", packlen);
101 hio_write(kcp_io, smux_stream->wbuf.base, packlen);
102 }
103
104 // kill timer
105 if (smux_stream->timer) {
106 htimer_del(smux_stream->timer);
107 smux_stream->timer = NULL;
108 }
109
110 // free buffer
111 HV_FREE(smux_stream->rbuf.base);
112 HV_FREE(smux_stream->wbuf.base);
113
114 smux_session_close_stream(&smux_session, smux_stream->stream_id);
115 hevent_set_userdata(io, NULL);
116 }
117
118 static void on_recv(hio_t* io, void* buf, int readbytes) {
119 // printf("on_recv %.*s \n", readbytes, (char*)buf);
120 smux_stream_t* smux_stream = (smux_stream_t*)hevent_userdata(io);
121 if (smux_stream == NULL) return;
122
123 // PSH
124 smux_frame_t frame;
125 smux_frame_init(&frame);
126 frame.head.sid = smux_stream->stream_id;
127 frame.head.cmd = SMUX_CMD_PSH;
128 frame.head.length = readbytes;
129 frame.data = (const char*)buf;
130 int packlen = smux_frame_pack(&frame, smux_stream->wbuf.base, smux_stream->wbuf.len);
131 if (packlen > 0) {
132 // printf("PSH %d\n", packlen);
133 int nwrite = hio_write(kcp_io, smux_stream->wbuf.base, packlen);
134 // printf("PSH ret=%d\n", nwrite);
135 }
136 }
137
138 static void on_connect(hio_t* io) {
139 // printf("on_connect fd=%d\n", hio_fd(io));
140 if (verbose) {
141 char localaddrstr[SOCKADDR_STRLEN] = {0};
142 char peeraddrstr[SOCKADDR_STRLEN] = {0};
143 printf("connected connfd=%d [%s] => [%s]\n", hio_fd(io),
144 SOCKADDR_STR(hio_localaddr(io), localaddrstr),
145 SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
146 }
147
148 smux_stream_t* smux_stream = (smux_stream_t*)hevent_userdata(io);
149 if (smux_stream == NULL) return;
150
151 // SYN
152 int packlen = smux_stream_output(smux_stream, SMUX_CMD_SYN);
153 if (packlen > 0) {
154 // printf("SYN %d\n", packlen);
155 hio_write(kcp_io, smux_stream->wbuf.base, packlen);
156 }
157
158 hio_setcb_read(io, on_recv);
159 hio_read(io);
160 }
161
162 static void on_kcp_recvfrom(hio_t* io, void* buf, int readbytes) {
163 // printf("on_kcp_recvfrom %d\n", readbytes);
164 smux_frame_t frame;
165 smux_frame_init(&frame);
166 int packlen = smux_frame_unpack(&frame, buf, readbytes);
167 assert(packlen == readbytes);
168 if (packlen < 0 ||
169 frame.head.version > 2 ||
170 frame.head.cmd > SMUX_CMD_UPD) {
171 fprintf(stderr, "smux_frame_unpack error: %d\n", packlen);
172 return;
173 }
174 // printf("smux sid=%u cmd=%d length=%d\n", frame.head.sid, (int)frame.head.cmd, (int)frame.head.length);
175
176 smux_stream_t* smux_stream = NULL;
177 if (frame.head.cmd == SMUX_CMD_SYN) {
178 hio_t* target_io = hio_create_socket(hevent_loop(kcp_io), target_host, target_port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
179 if (target_io == NULL) {
180 fprintf(stderr, "create tcp client error!\n");
181 return;
182 }
183 smux_stream = smux_session_open_stream(&smux_session, frame.head.sid, target_io);
184 // alloc buffer
185 smux_stream->rbuf.len = mtu;
186 smux_stream->wbuf.len = mtu;
187 HV_ALLOC(smux_stream->rbuf.base, smux_stream->rbuf.len);
188 HV_ALLOC(smux_stream->wbuf.base, smux_stream->wbuf.len);
189 hio_set_readbuf(target_io, smux_stream->rbuf.base, smux_config.max_frame_size);
190 hevent_set_userdata(target_io, smux_stream);
191
192 hio_setcb_connect(target_io, on_connect);
193 hio_setcb_close(target_io, on_close);
194 hio_connect(target_io);
195 } else {
196 smux_stream = smux_session_get_stream(&smux_session, frame.head.sid);
197 }
198 if (smux_stream == NULL) {
199 if (frame.head.sid != 0 && frame.head.cmd != SMUX_CMD_FIN) {
200 fprintf(stderr, "recvfrom invalid smux package!\n");
201 }
202 return;
203 }
204
205 switch (frame.head.cmd) {
206 case SMUX_CMD_FIN:
207 hio_close(smux_stream->io);
208 break;
209 case SMUX_CMD_PSH:
210 hio_write(smux_stream->io, frame.data, frame.head.length);
211 break;
212 case SMUX_CMD_NOP:
213 break;
214 default:
215 break;
216 }
217 }
218
219 int main(int argc, char** argv) {
220 if (argc < 2) {
221 print_help();
222 exit(0);
223 }
224
225 // g_main_ctx
226 main_ctx_init(argc, argv);
227 int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
228 if (ret != 0) {
229 print_help();
230 exit(ret);
231 }
232
233 // help
234 if (get_arg("h")) {
235 print_help();
236 exit(0);
237 }
238
239 // version
240 if (get_arg("v")) {
241 print_version();
242 exit(0);
243 }
244
245 #ifdef OS_UNIX
246 // daemon
247 if (get_arg("d")) {
248 // nochdir, noclose
249 int ret = daemon(1, 1);
250 if (ret != 0) {
251 printf("daemon error: %d\n", ret);
252 exit(-10);
253 }
254 }
255 #endif
256
257 const char* arg = get_arg("l");
258 if (arg) {
259 localaddr = arg;
260 }
261
262 arg = get_arg("t");
263 if (arg) {
264 targetaddr = arg;
265 }
266
267 arg = get_arg("m");
268 if (arg) {
269 mode = arg;
270 }
271
272 arg = get_arg("mtu");
273 if (arg) {
274 mtu = atoi(arg);
275 }
276
277 arg = get_arg("sndwnd");
278 if (arg) {
279 sndwnd = atoi(arg);
280 }
281
282 arg = get_arg("rcvwnd");
283 if (arg) {
284 rcvwnd = atoi(arg);
285 }
286
287 const char* pos = strchr(localaddr, ':');
288 int len = 0;
289 if (pos) {
290 len = pos - localaddr;
291 if (len > 0) {
292 memcpy(kcp_host, localaddr, len);
293 kcp_host[len] = '\0';
294 }
295 kcp_port = atoi(pos + 1);
296 }
297
298 pos = strchr(targetaddr, ':');
299 if (pos) {
300 len = pos - targetaddr;
301 if (len > 0) {
302 memcpy(target_host, targetaddr, len);
303 target_host[len] = '\0';
304 }
305 target_port = atoi(pos + 1);
306 }
307
308 if (strcmp(mode, "normal") == 0) {
309 kcp_setting_init_with_normal_mode(&s_kcp_setting);
310 } else if (strcmp(mode, "fast") == 0) {
311 kcp_setting_init_with_fast_mode(&s_kcp_setting);
312 } else if (strcmp(mode, "fast2") == 0) {
313 kcp_setting_init_with_fast2_mode(&s_kcp_setting);
314 } else if (strcmp(mode, "fast3") == 0) {
315 kcp_setting_init_with_fast3_mode(&s_kcp_setting);
316 } else {
317 fprintf(stderr, "Unknown mode '%s'\n", mode);
318 exit(-20);
319 }
320 s_kcp_setting.mtu = mtu;
321 s_kcp_setting.sndwnd = sndwnd;
322 s_kcp_setting.rcvwnd = rcvwnd;
323
324 printf("smux version: 1\n");
325 printf("%s:%d => %s:%d\n", kcp_host, kcp_port, target_host, target_port);
326 printf("mode: %s\n", mode);
327 printf("mtu: %d\n", mtu);
328 printf("sndwnd: %d rcvwnd: %d\n", sndwnd, rcvwnd);
329
330 hloop_t* loop = hloop_new(0);
331
332 kcp_io = hloop_create_udp_server(loop, kcp_host, kcp_port);
333 if (kcp_io == NULL) {
334 fprintf(stderr, "create udp server error!\n");
335 return -20;
336 }
337 hio_set_kcp(kcp_io, &s_kcp_setting);
338 hio_setcb_read(kcp_io, on_kcp_recvfrom);
339 hio_read(kcp_io);
340
341 // smux
342 smux_config.max_frame_size = 1024;
343 smux_session.next_stream_id = 0;
344
345 hloop_run(loop);
346 hloop_free(&loop);
347 return 0;
348 }