1
0

main.cpp 10 KB

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