main.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
  35. printf(" mode 1 usually introduces less latency, but you have to care about mtu.\n");
  36. printf(" --mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu value.\n");
  37. printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n");
  38. printf(" default value: 1250. you typically shouldnt change this value.\n");
  39. printf(" -q,--queue-len <number> fec queue len, only for mode 0, fec will be performed immediately after queue is full.\n");
  40. printf(" default value: 200. \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 protect burst packet loss.\n");
  44. printf(" default value: 0. do not use if you dont know what it means.\n");
  45. printf(" --random-drop <number> simulate packet loss, unit: 0.01%%. default value: 0.\n");
  46. printf(" --disable-obscure <number> disable obscure, to save a bit bandwidth 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(" --decode-buf <number> size of buffer of fec decoder,u nit: packet, default: 2000\n");
  55. printf(" --fix-latency <number> try to stabilize latency, only for mode 0\n");
  56. printf(" --delay-capacity <number> max number of delayed packets\n");
  57. printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
  58. printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
  59. printf("log and help options:\n");
  60. printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
  61. printf(" 4: info (default) 5: debug 6: trace\n");
  62. printf(" --log-position enable file name, function name, line number in log\n");
  63. printf(" --disable-color disable log color\n");
  64. printf(" -h,--help print this help message\n");
  65. //printf("common options,these options must be same on both side\n");
  66. }
  67. int main(int argc, char *argv[])
  68. {
  69. //working_mode=tunnel_mode;
  70. assert(sizeof(u64_t)==8);
  71. assert(sizeof(i64_t)==8);
  72. assert(sizeof(u32_t)==4);
  73. assert(sizeof(i32_t)==4);
  74. assert(sizeof(u16_t)==2);
  75. assert(sizeof(i16_t)==2);
  76. dup2(1, 2); //redirect stderr to stdout
  77. int i, j, k;
  78. if (argc == 1)
  79. {
  80. print_help();
  81. myexit( -1);
  82. }
  83. for (i = 0; i < argc; i++)
  84. {
  85. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  86. {
  87. print_help();
  88. myexit(0);
  89. }
  90. }
  91. process_arg(argc,argv);
  92. delay_manager.set_capacity(delay_capacity);
  93. local_ip_uint32=inet_addr(local_ip);
  94. remote_ip_uint32=inet_addr(remote_ip);
  95. sub_net_uint32=inet_addr(sub_net);
  96. if(strlen(tun_dev)==0)
  97. {
  98. sprintf(tun_dev,"tun%u",get_true_random_number()%1000);
  99. }
  100. if(client_or_server==client_mode)
  101. {
  102. tunnel_client_event_loop();
  103. }
  104. else
  105. {
  106. tunnel_server_event_loop();
  107. }
  108. return 0;
  109. }