main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * tun.cpp
  3. *
  4. * Created on: Oct 26, 2017
  5. * Author: root
  6. */
  7. #include "common.h"
  8. #include "log.h"
  9. #include "misc.h"
  10. #include "tun_dev.h"
  11. #include "git_version.h"
  12. using namespace std;
  13. static void print_help()
  14. {
  15. char git_version_buf[100]={0};
  16. strncpy(git_version_buf,gitversion,10);
  17. printf("tinyFecVPN\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-/tinyFecVPN/\n");
  21. printf("\n");
  22. printf("usage:\n");
  23. printf(" run as client: ./this_program -c -r server_ip:server_port [options]\n");
  24. printf(" run as server: ./this_program -s -l server_listen_ip:server_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(" --sub-net <number> specify sub-net, for example: 192.168.1.0 , default: 10.22.22.0\n");
  30. printf(" --tun-dev <number> sepcify tun device name, for example: tun10, default: a random name such as tun987\n");
  31. printf(" -f,--fec x:y forward error correction, send y redundant packets for every x packets\n");
  32. printf(" --timeout <number> how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n");
  33. printf(" --mode <number> fec-mode,available values: 0, 1; 0 cost less bandwidth, 1 cost less latency;default: 0)\n");
  34. printf(" --report <number> turn on send/recv report, and set a period for reporting, unit: s\n");
  35. printf(" --keep-reconnect re-connect after lost connection,only for client. \n");
  36. printf("advanced options:\n");
  37. printf(" --mtu <number> mtu for fec. for mode 0, the program will split packet to segment smaller than mtu.\n");
  38. printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n");
  39. printf(" default value: 1250\n");
  40. printf(" -j,--jitter <number> simulated jitter. randomly delay first packet for 0~<number> ms, default value: 0.\n");
  41. printf(" do not use if you dont know what it means.\n");
  42. printf(" -i,--interval <number> scatter each fec group to a interval of <number> ms, to protect burst packet loss.\n");
  43. printf(" default value: 0. do not use if you dont know what it means.\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 encryption to save a bit cpu\n");
  48. printf("developer options:\n");
  49. printf(" --tun-mtu <number> mtu of the tun interface,most time you shouldnt change this\n");
  50. printf(" --mssfix <number> do mssfix for tcp connection, use 0 to disable. default value: 1250\n");
  51. printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\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> max fec queue len, only for mode 0\n");
  58. printf(" --decode-buf <number> size of buffer of fec decoder,u nit: packet, default: 2000\n");
  59. printf(" --fix-latency <number> try to stabilize latency, only for mode 0\n");
  60. printf(" --delay-capacity <number> max number of delayed packets\n");
  61. printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
  62. printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
  63. printf("log and help options:\n");
  64. printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
  65. printf(" 4: info (default) 5: debug 6: trace\n");
  66. printf(" --log-position enable file name, function name, line number in log\n");
  67. printf(" --disable-color disable log color\n");
  68. printf(" -h,--help print this help message\n");
  69. //printf("common options,these options must be same on both side\n");
  70. }
  71. int main(int argc, char *argv[])
  72. {
  73. working_mode=tun_dev_mode;
  74. assert(sizeof(u64_t)==8);
  75. assert(sizeof(i64_t)==8);
  76. assert(sizeof(u32_t)==4);
  77. assert(sizeof(i32_t)==4);
  78. assert(sizeof(u16_t)==2);
  79. assert(sizeof(i16_t)==2);
  80. dup2(1, 2); //redirect stderr to stdout
  81. int i, j, k;
  82. if (argc == 1)
  83. {
  84. print_help();
  85. myexit( -1);
  86. }
  87. for (i = 0; i < argc; i++)
  88. {
  89. if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
  90. {
  91. print_help();
  92. myexit(0);
  93. }
  94. }
  95. //g_fec_mode=0;
  96. process_arg(argc,argv);
  97. delay_manager.set_capacity(delay_capacity);
  98. //local_ip_uint32=inet_addr(local_ip);
  99. //remote_ip_uint32=inet_addr(remote_ip);
  100. sub_net_uint32=inet_addr(sub_net);
  101. if(strlen(tun_dev)==0)
  102. {
  103. sprintf(tun_dev,"tun%u",get_fake_random_number()%1000);
  104. }
  105. mylog(log_info,"using interface %s\n",tun_dev);
  106. /*if(tun_mtu==0)
  107. {
  108. tun_mtu=g_fec_mtu;
  109. }*/
  110. if(program_mode==client_mode)
  111. {
  112. tun_dev_client_event_loop();
  113. }
  114. else
  115. {
  116. tun_dev_server_event_loop();
  117. }
  118. return 0;
  119. }