1
0

main.cpp 11 KB

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