2017-08-08 23:34:35 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "log.h"
|
2017-10-26 08:38:52 -05:00
|
|
|
|
2017-09-14 07:01:39 -05:00
|
|
|
#include "lib/rs.h"
|
2017-09-15 03:34:29 -05:00
|
|
|
#include "packet.h"
|
2017-09-25 10:38:39 -05:00
|
|
|
#include "connection.h"
|
|
|
|
#include "fd_manager.h"
|
2017-09-23 02:00:44 -05:00
|
|
|
#include "delay_manager.h"
|
2017-10-05 12:21:06 -05:00
|
|
|
#include "fec_manager.h"
|
2017-10-26 08:38:52 -05:00
|
|
|
#include "misc.h"
|
|
|
|
#include "tunnel.h"
|
2017-10-28 01:02:58 -05:00
|
|
|
//#include "tun_dev.h"
|
2017-10-28 04:36:37 -05:00
|
|
|
#include "git_version.h"
|
2017-10-21 11:27:39 +08:00
|
|
|
using namespace std;
|
2017-08-08 17:10:38 +08:00
|
|
|
|
2017-09-14 12:22:47 -05:00
|
|
|
|
2017-10-28 02:05:14 -05:00
|
|
|
static void print_help()
|
|
|
|
{
|
|
|
|
char git_version_buf[100]={0};
|
|
|
|
strncpy(git_version_buf,gitversion,10);
|
|
|
|
|
|
|
|
printf("UDPspeeder V2\n");
|
|
|
|
printf("git version: %s ",git_version_buf);
|
|
|
|
printf("build date: %s %s\n",__DATE__,__TIME__);
|
|
|
|
printf("repository: https://github.com/wangyu-/UDPspeeder\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("usage:\n");
|
|
|
|
printf(" run as client: ./this_program -c -l local_listen_ip:local_port -r server_ip:server_port [options]\n");
|
|
|
|
printf(" run as server: ./this_program -s -l server_listen_ip:server_port -r remote_ip:remote_port [options]\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("common options, must be same on both sides:\n");
|
|
|
|
printf(" -k,--key <string> key for simple xor encryption. if not set, xor is disabled\n");
|
|
|
|
|
|
|
|
printf("main options:\n");
|
|
|
|
printf(" -f,--fec x:y forward error correction, send y redundant packets for every x packets\n");
|
|
|
|
printf(" --timeout <number> how long could a packet be held in queue before doing fec, unit: ms, default: 8ms\n");
|
|
|
|
printf(" --report <number> turn on send/recv report, and set a period for reporting, unit: s\n");
|
|
|
|
|
|
|
|
printf("advanced options:\n");
|
2017-11-19 06:02:20 -06:00
|
|
|
printf(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
|
|
|
|
printf(" mode 1 usually introduces less latency, but you have to care about mtu.\n");
|
|
|
|
printf(" --mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu value.\n");
|
2017-10-28 02:05:14 -05:00
|
|
|
printf(" for mode 1, no packet will be split, the program just check if the mtu is exceed.\n");
|
2017-11-19 06:02:20 -06:00
|
|
|
printf(" default value: 1250. you typically shouldnt change this value.\n");
|
|
|
|
printf(" -q,--queue-len <number> fec queue len, only for mode 0, fec will be performed immediately after queue is full.\n");
|
|
|
|
printf(" default value: 200. \n");
|
2018-08-05 22:30:22 -05:00
|
|
|
|
2017-10-28 02:05:14 -05:00
|
|
|
printf(" -j,--jitter <number> simulated jitter. randomly delay first packet for 0~<number> ms, default value: 0.\n");
|
|
|
|
printf(" do not use if you dont know what it means.\n");
|
|
|
|
printf(" -i,--interval <number> scatter each fec group to a interval of <number> ms, to protect burst packet loss.\n");
|
|
|
|
printf(" default value: 0. do not use if you dont know what it means.\n");
|
2018-08-05 22:30:22 -05:00
|
|
|
printf(" -f,--fec x1:y1,x2:y2,.. similiar to -f/--fec above,fine-grained fec parameters,may help save bandwidth.\n");
|
|
|
|
printf(" example: \"-f 1:3,2:4,10:6,20:10\". check repo for details\n");
|
2017-11-19 06:02:20 -06:00
|
|
|
printf(" --random-drop <number> simulate packet loss, unit: 0.01%%. default value: 0.\n");
|
|
|
|
printf(" --disable-obscure <number> disable obscure, to save a bit bandwidth and cpu.\n");
|
2018-08-05 22:30:22 -05:00
|
|
|
//printf(" --disable-xor <number> disable xor\n");
|
2017-10-28 02:05:14 -05:00
|
|
|
|
|
|
|
printf("developer options:\n");
|
|
|
|
printf(" --fifo <string> use a fifo(named pipe) for sending commands to the running program, so that you\n");
|
|
|
|
printf(" can change fec encode parameters dynamically, check readme.md in repository for\n");
|
|
|
|
printf(" supported commands.\n");
|
|
|
|
printf(" -j ,--jitter jmin:jmax similiar to -j above, but create jitter randomly between jmin and jmax\n");
|
|
|
|
printf(" -i,--interval imin:imax similiar to -i above, but scatter randomly between imin and imax\n");
|
|
|
|
printf(" --decode-buf <number> size of buffer of fec decoder,u nit: packet, default: 2000\n");
|
|
|
|
printf(" --fix-latency <number> try to stabilize latency, only for mode 0\n");
|
|
|
|
printf(" --delay-capacity <number> max number of delayed packets\n");
|
|
|
|
printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
|
|
|
|
printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
|
|
|
|
printf("log and help options:\n");
|
|
|
|
printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
|
|
|
|
printf(" 4: info (default) 5: debug 6: trace\n");
|
|
|
|
printf(" --log-position enable file name, function name, line number in log\n");
|
|
|
|
printf(" --disable-color disable log color\n");
|
|
|
|
printf(" -h,--help print this help message\n");
|
|
|
|
|
|
|
|
//printf("common options,these options must be same on both side\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-03 20:39:31 -07:00
|
|
|
void sigpipe_cb(struct ev_loop *l, ev_signal *w, int revents)
|
|
|
|
{
|
|
|
|
mylog(log_info, "got sigpipe, ignored");
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigterm_cb(struct ev_loop *l, ev_signal *w, int revents)
|
|
|
|
{
|
|
|
|
mylog(log_info, "got sigterm, exit");
|
|
|
|
myexit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigint_cb(struct ev_loop *l, ev_signal *w, int revents)
|
|
|
|
{
|
|
|
|
mylog(log_info, "got sigint, exit");
|
|
|
|
myexit(0);
|
|
|
|
}
|
|
|
|
|
2017-10-28 02:05:14 -05:00
|
|
|
|
2018-06-04 00:36:23 -05:00
|
|
|
|
2017-08-08 23:34:35 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-06-19 18:06:28 +08:00
|
|
|
init_ws();
|
2018-06-04 00:39:06 -05:00
|
|
|
//unit_test();
|
2018-06-04 00:36:23 -05:00
|
|
|
|
2018-06-03 20:39:31 -07:00
|
|
|
struct ev_loop* loop=ev_default_loop(0);
|
2018-06-19 18:06:28 +08:00
|
|
|
#if !defined(__MINGW32__)
|
2018-06-03 20:39:31 -07:00
|
|
|
ev_signal signal_watcher_sigpipe;
|
|
|
|
ev_signal_init(&signal_watcher_sigpipe, sigpipe_cb, SIGPIPE);
|
|
|
|
ev_signal_start(loop, &signal_watcher_sigpipe);
|
2018-06-19 21:29:03 +08:00
|
|
|
#else
|
|
|
|
enable_log_color=0;
|
2018-06-19 18:06:28 +08:00
|
|
|
#endif
|
2018-06-03 20:39:31 -07:00
|
|
|
|
|
|
|
ev_signal signal_watcher_sigterm;
|
|
|
|
ev_signal_init(&signal_watcher_sigterm, sigterm_cb, SIGTERM);
|
|
|
|
ev_signal_start(loop, &signal_watcher_sigterm);
|
|
|
|
|
|
|
|
ev_signal signal_watcher_sigint;
|
|
|
|
ev_signal_init(&signal_watcher_sigint, sigint_cb, SIGINT);
|
|
|
|
ev_signal_start(loop, &signal_watcher_sigint);
|
|
|
|
|
2017-10-27 00:03:35 -05:00
|
|
|
//working_mode=tunnel_mode;
|
2017-09-23 02:00:44 -05:00
|
|
|
|
2017-08-13 10:19:17 +08:00
|
|
|
assert(sizeof(u64_t)==8);
|
|
|
|
assert(sizeof(i64_t)==8);
|
|
|
|
assert(sizeof(u32_t)==4);
|
|
|
|
assert(sizeof(i32_t)==4);
|
2017-10-13 10:26:05 -05:00
|
|
|
assert(sizeof(u16_t)==2);
|
|
|
|
assert(sizeof(i16_t)==2);
|
2017-08-08 23:34:35 +08:00
|
|
|
dup2(1, 2); //redirect stderr to stdout
|
|
|
|
int i, j, k;
|
2017-10-28 02:05:14 -05:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
print_help();
|
|
|
|
myexit( -1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0)
|
|
|
|
{
|
|
|
|
print_help();
|
|
|
|
myexit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 23:34:35 +08:00
|
|
|
process_arg(argc,argv);
|
2017-10-15 14:12:21 -05:00
|
|
|
|
2017-10-17 12:35:03 -05:00
|
|
|
delay_manager.set_capacity(delay_capacity);
|
2018-07-25 03:39:48 -05:00
|
|
|
//local_ip_uint32=inet_addr(local_ip);
|
|
|
|
//remote_ip_uint32=inet_addr(remote_ip);
|
|
|
|
sub_net_uint32=inet_addr(sub_net);// used only in tinyfecVPN
|
2017-10-27 21:16:59 -05:00
|
|
|
|
|
|
|
if(strlen(tun_dev)==0)
|
|
|
|
{
|
2018-06-04 00:36:23 -05:00
|
|
|
sprintf(tun_dev,"tun%u",get_fake_random_number()%1000);
|
2017-10-27 21:16:59 -05:00
|
|
|
}
|
2017-08-08 23:34:35 +08:00
|
|
|
|
2018-07-28 03:48:57 -05:00
|
|
|
if(program_mode==client_mode)
|
2017-09-25 10:38:39 -05:00
|
|
|
{
|
2017-10-28 01:02:58 -05:00
|
|
|
tunnel_client_event_loop();
|
2017-09-25 10:38:39 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-10-28 01:02:58 -05:00
|
|
|
tunnel_server_event_loop();
|
2017-09-25 10:38:39 -05:00
|
|
|
}
|
2017-08-08 23:34:35 +08:00
|
|
|
|
2017-08-08 17:10:38 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-08-08 23:34:35 +08:00
|
|
|
|