1
0

main.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. char git_version_buf[100] = {0};
  15. strncpy(git_version_buf, gitversion, 10);
  16. printf("tinyFecVPN\n");
  17. printf("git version: %s ", git_version_buf);
  18. printf("build date: %s %s\n", __DATE__, __TIME__);
  19. printf("repository: https://github.com/wangyu-/tinyFecVPN/\n");
  20. printf("\n");
  21. printf("usage:\n");
  22. printf(" run as client: ./this_program -c -r server_ip:server_port [options]\n");
  23. printf(" run as server: ./this_program -s -l server_listen_ip:server_port [options]\n");
  24. printf("\n");
  25. printf("common options, must be same on both sides:\n");
  26. printf(" -k,--key <string> key for simple xor encryption. if not set, xor is disabled\n");
  27. printf("main options:\n");
  28. printf(" --sub-net <number> specify sub-net, for example: 192.168.1.0 , default: 10.22.22.0\n");
  29. printf(" --tun-dev <number> sepcify tun device name, for example: tun10, default: a random name such as tun987\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(" --keep-reconnect re-connect after lost connection,only for client. \n");
  34. printf("advanced options:\n");
  35. printf(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
  36. printf(" mode 1 usually introduces less latency, but you have to care about mtu.\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 defend burst packet loss.\n");
  43. printf(" default value: 0. do not use if you dont know what it means.\n");
  44. printf(" -f,--fec x1:y1,x2:y2,.. similiar to -f/--fec above,fine-grained fec parameters,may help save bandwidth.\n");
  45. printf(" example: \"-f 1:3,2:4,10:6,20:10\". check repo for details\n");
  46. printf(" --random-drop <number> simulate packet loss, unit: 0.01%%. default value: 0\n");
  47. printf(" --disable-obscure <number> disable obscure, to save a bit bandwidth and cpu\n");
  48. printf(" --disable-checksum <number> disable checksum to save a bit bandwdith and cpu\n");
  49. // printf(" --disable-xor <number> disable xor\n");
  50. printf(" --persist-tun <number> make the tun device persistent, so that it wont be deleted after exited.\n");
  51. printf(" --mssfix <number> do mssfix for tcp connection, use 0 to disable. default value: 1250\n");
  52. printf("developer options:\n");
  53. printf(" --tun-mtu <number> mtu of the tun interface,most time you shouldnt change this\n");
  54. printf(" --manual-set-tun <number> tell tinyfecvpn to not setup the tun device automatically(e.g. assign ip address),\n");
  55. printf(" so that you can do it manually later\n");
  56. printf(" --fifo <string> use a fifo(named pipe) for sending commands to the running program, so that you\n");
  57. printf(" can change fec encode parameters dynamically, check readme.md in repository for\n");
  58. printf(" supported commands.\n");
  59. printf(" -j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax\n");
  60. printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\n");
  61. printf(" -q,--queue-len <number> fec queue len, only for mode 0, fec will be performed immediately after queue is full.\n");
  62. printf(" default value: 200. \n");
  63. printf(" --decode-buf <number> size of buffer of fec decoder,u nit: packet, default: 2000\n");
  64. // printf(" --fix-latency <number> try to stabilize latency, only for mode 0\n");
  65. printf(" --delay-capacity <number> max number of delayed packets, 0 means unlimited, default: 0\n");
  66. printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
  67. printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
  68. printf(" --out-addr ip:port force all output packets of '-r' end to go through this address, port 0 for random port.\n");
  69. printf(" --out-interface <string> force all output packets of '-r' end to go through this interface.\n");
  70. printf("log and help options:\n");
  71. printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
  72. printf(" 4: info (default) 5: debug 6: trace\n");
  73. printf(" --log-position enable file name, function name, line number in log\n");
  74. printf(" --disable-color disable log color\n");
  75. printf(" -h,--help print this help message\n");
  76. // printf("common options,these options must be same on both side\n");
  77. }
  78. void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents) {
  79. mylog(log_info, "got sigpipe, ignored");
  80. }
  81. void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents) {
  82. mylog(log_info, "got sigterm, exit");
  83. myexit(0);
  84. }
  85. void sigint_cb(struct ev_loop *l, ev_signal *w, int revents) {
  86. mylog(log_info, "got sigint, exit");
  87. myexit(0);
  88. }
  89. int main(int argc, char *argv[]) {
  90. working_mode = tun_dev_mode;
  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. print_help();
  115. myexit(-1);
  116. }
  117. for (i = 0; i < argc; i++) {
  118. if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
  119. print_help();
  120. myexit(0);
  121. }
  122. }
  123. // g_fec_mode=0;
  124. process_arg(argc, argv);
  125. delay_manager.set_capacity(delay_capacity);
  126. // local_ip_uint32=inet_addr(local_ip);
  127. // remote_ip_uint32=inet_addr(remote_ip);
  128. sub_net_uint32 = inet_addr(sub_net);
  129. if (strlen(tun_dev) == 0) {
  130. sprintf(tun_dev, "tun%u", get_fake_random_number() % 1000);
  131. }
  132. mylog(log_info, "using interface %s\n", tun_dev);
  133. /*if(tun_mtu==0)
  134. {
  135. tun_mtu=g_fec_mtu;
  136. }*/
  137. if (program_mode == client_mode) {
  138. tun_dev_client_event_loop();
  139. } else {
  140. tun_dev_server_event_loop();
  141. }
  142. return 0;
  143. }