main.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "common.h"
  2. #include "log.h"
  3. #include "lib/rs.h"
  4. #include "packet.h"
  5. #include "connection.h"
  6. #include "fd_manager.h"
  7. #include "delay_manager.h"
  8. #include "fec_manager.h"
  9. #include "misc.h"
  10. #include "tunnel.h"
  11. //#include "tun_dev.h"
  12. #include "git_version.h"
  13. using namespace std;
  14. static void print_help() {
  15. char git_version_buf[100] = {0};
  16. strncpy(git_version_buf, gitversion, 10);
  17. printf("UDPspeeder V2\n");
  18. printf("git version: %s ", git_version_buf);
  19. printf("build date: %s %s\n", __DATE__, __TIME__);
  20. printf("repository: https://github.com/wangyu-/UDPspeeder\n");
  21. printf("\n");
  22. printf("usage:\n");
  23. printf(" run as client: ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n");
  24. printf(" run as server: ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n");
  25. printf("\n");
  26. printf("common options, must be same on both sides:\n");
  27. printf(" -k,--key <string> key for simple xor encryption. if not set, xor is disabled\n");
  28. printf("main options:\n");
  29. printf(" -f,--fec x:y forward error correction, send y redundant packets for every x packets\n");
  30. printf(" --timeout <number> how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n");
  31. printf(" --report <number> turn on send/recv report, and set a period for reporting, unit: s\n");
  32. printf("advanced options:\n");
  33. printf(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
  34. printf(" mode 1 usually introduces less latency, but you have to care about mtu.\n");
  35. printf(" --mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu value.\n");
  36. printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n");
  37. printf(" default value: 1250. you typically shouldnt change this value.\n");
  38. printf(" -j,--jitter <number> simulated jitter. randomly delay first packet for 0~<number> ms, default value: 0.\n");
  39. printf(" do not use if you dont know what it means.\n");
  40. printf(" -i,--interval <number> scatter each fec group to a interval of <number> ms, to defend burst packet loss.\n");
  41. printf(" default value: 0. do not use if you dont know what it means.\n");
  42. printf(" -f,--fec x1:y1,x2:y2,.. similiar to -f/--fec above,fine-grained fec parameters,may help save bandwidth.\n");
  43. printf(" example: \"-f 1:3,2:4,10:6,20:10\". check repo for details\n");
  44. printf(" --random-drop <number> simulate packet loss, unit: 0.01%%. default value: 0.\n");
  45. printf(" --disable-obscure <number> disable obscure, to save a bit bandwidth and cpu\n");
  46. printf(" --disable-checksum <number> disable checksum to save a bit bandwdith and cpu\n");
  47. // printf(" --disable-xor <number> disable xor\n");
  48. printf("developer options:\n");
  49. printf(" --fifo <string> use a fifo(named pipe) for sending commands to the running program, so that you\n");
  50. printf(" can change fec encode parameters dynamically, check readme.md in repository for\n");
  51. printf(" supported commands.\n");
  52. printf(" -j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax\n");
  53. printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\n");
  54. printf(" -q,--queue-len <number> fec queue len, only for mode 0, fec will be performed immediately after queue is full.\n");
  55. printf(" default value: 200. \n");
  56. printf(" --decode-buf <number> size of buffer of fec decoder,unit: packet, default: 2000\n");
  57. // printf(" --fix-latency <number> try to stabilize latency, only for mode 0\n");
  58. printf(" --delay-capacity <number> max number of delayed packets, 0 means unlimited, default: 0\n");
  59. printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
  60. printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
  61. printf(" --out-addr ip:port force all output packets of '-r' end to go through this address, port 0 for random port.\n");
  62. #ifdef __linux__
  63. printf(" --out-interface <string> force all output packets of '-r' end to go through this interface.\n");
  64. #endif
  65. printf("log and help options:\n");
  66. printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
  67. printf(" 4: info (default) 5: debug 6: trace\n");
  68. printf(" --log-position enable file name, function name, line number in log\n");
  69. printf(" --disable-color disable log color\n");
  70. printf(" -h,--help print this help message\n");
  71. // printf("common options,these options must be same on both side\n");
  72. }
  73. void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) {
  74. mylog(log_info, "got sigpipe, ignored");
  75. }
  76. void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) {
  77. mylog(log_info, "got sigterm, exit");
  78. myexit(0);
  79. }
  80. void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) {
  81. mylog(log_info, "got sigint, exit");
  82. myexit(0);
  83. }
  84. int main(int argc, char *argv[]) {
  85. working_mode = tunnel_mode;
  86. init_ws();
  87. // unit_test();
  88. struct ev_loop *loop = ev_default_loop(0);
  89. #if !defined(__MINGW32__)
  90. ev_signal signal_watcher_sigpipe;
  91. ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
  92. ev_signal_start(loop, &signal_watcher_sigpipe);
  93. #else
  94. enable_log_color = 0;
  95. #endif
  96. ev_signal signal_watcher_sigterm;
  97. ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
  98. ev_signal_start(loop, &signal_watcher_sigterm);
  99. ev_signal signal_watcher_sigint;
  100. ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
  101. ev_signal_start(loop, &signal_watcher_sigint);
  102. assert(sizeof(u64_t) == 8);
  103. assert(sizeof(i64_t) == 8);
  104. assert(sizeof(u32_t) == 4);
  105. assert(sizeof(i32_t) == 4);
  106. assert(sizeof(u16_t) == 2);
  107. assert(sizeof(i16_t) == 2);
  108. dup2(1, 2); // redirect stderr to stdout
  109. int i, j, k;
  110. if (argc == 1) {
  111. print_help();
  112. myexit(-1);
  113. }
  114. for (i = 0; i < argc; i++) {
  115. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
  116. print_help();
  117. myexit(0);
  118. }
  119. }
  120. process_arg(argc, argv);
  121. delay_manager.set_capacity(delay_capacity);
  122. if (strlen(tun_dev) == 0) {
  123. sprintf(tun_dev, "tun%u", get_fake_random_number() % 1000);
  124. }
  125. if (program_mode == client_mode) {
  126. tunnel_client_event_loop();
  127. } else {
  128. tunnel_server_event_loop();
  129. }
  130. return 0;
  131. }