Debian Codegraph @

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

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