main.cpp 7.1 KB

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