mirror of
https://github.com/wangyu-/UDPspeeder.git
synced 2025-09-17 20:54:33 +08:00
Compare commits
141 Commits
v2@2017102
...
v2@2017111
Author | SHA1 | Date | |
---|---|---|---|
|
6f55b8a2fc | ||
|
85122b7155 | ||
|
0cef4503be | ||
|
744c8f56ae | ||
|
8680b06882 | ||
|
57cce3a424 | ||
|
6ab4294bd9 | ||
|
ebdabb4598 | ||
|
c254545290 | ||
|
1301fdc96d | ||
|
f58dfe4fdd | ||
|
3fd198a1e2 | ||
|
017e851c2d | ||
|
2929eead0e | ||
|
e2c2f2dfec | ||
|
d0ebd67dbd | ||
|
211fcee8db | ||
|
6b4a2dfe21 | ||
|
65939eb9e7 | ||
|
9c00a2f3a6 | ||
|
4706e51fad | ||
|
c3786a91d8 | ||
|
680f127350 | ||
|
7713cf0c15 | ||
|
5024a02d20 | ||
|
f2d9deb2a3 | ||
|
c5c440245c | ||
|
c95e2db1af | ||
|
82a2b3cb9a | ||
|
8ecc696da5 | ||
|
6e6fc62fb8 | ||
|
ea03e5f8ec | ||
|
3be2642397 | ||
|
d05bcfc2f8 | ||
|
e50292bd4a | ||
|
363aec12d4 | ||
|
4364a7b789 | ||
|
17a42a1218 | ||
|
9152fef474 | ||
|
eb7406e6f3 | ||
|
296a84be3c | ||
|
69558ab16c | ||
|
37b7eb7282 | ||
|
4ca57cf656 | ||
|
1a744d69ea | ||
|
86b68697e1 | ||
|
63a2f923ab | ||
|
5774b478fc | ||
|
e91dcf6647 | ||
|
f6c97af854 | ||
|
2f097d720d | ||
|
c6de39acc8 | ||
|
0f425a3a98 | ||
|
2cc560088b | ||
|
b83b21f2eb | ||
|
f526046846 | ||
|
1be5fb5fc1 | ||
|
27874aa413 | ||
|
fde2329528 | ||
|
30b78c1339 | ||
|
17a3074b34 | ||
|
cce7d18163 | ||
|
90029e1ea7 | ||
|
c26a4708ee | ||
|
fdbf4ab205 | ||
|
7ee72ee84c | ||
|
cac5f8271c | ||
|
219e8e2592 | ||
|
02b4d59500 | ||
|
894bf7cd0d | ||
|
a8cab43139 | ||
|
369a9d8e1b | ||
|
0f7894f42f | ||
|
d7daae25bf | ||
|
d7271b74b4 | ||
|
1eeef14470 | ||
|
9d30caffc9 | ||
|
e50e383ede | ||
|
c73a197971 | ||
|
ba44617124 | ||
|
19e718f0a3 | ||
|
bd323be9c4 | ||
|
35a85e38ae | ||
|
4871983b8f | ||
|
fec3c39cad | ||
|
4d55348a4d | ||
|
36eef02d8a | ||
|
3fd48dd20e | ||
|
766aa274d9 | ||
|
d961537682 | ||
|
4dcfe87bd3 | ||
|
2aa0ece465 | ||
|
05fd84b675 | ||
|
1e00ff7c87 | ||
|
c76818c6a8 | ||
|
e3e2fe6d36 | ||
|
7cd87e7b59 | ||
|
c3aae9579b | ||
|
d9f80781a2 | ||
|
203604e1b5 | ||
|
5ac6e30c8e | ||
|
6728d5a561 | ||
|
f9eaae172f | ||
|
a0eeedb12a | ||
|
324d34c87a | ||
|
1fe64b5689 | ||
|
d5e900e91c | ||
|
8e00273db4 | ||
|
73d68a2baf | ||
|
d1c88bbc07 | ||
|
caa8320743 | ||
|
f64108867c | ||
|
716e5458f9 | ||
|
91c9a9e705 | ||
|
e633b5b3ce | ||
|
47d762958f | ||
|
0a34cb47ab | ||
|
cdba2f8c2c | ||
|
8f9deafce7 | ||
|
d6e445f220 | ||
|
3d2ec1bdf2 | ||
|
c0450160d6 | ||
|
b23dd44aa7 | ||
|
fc540c6fe2 | ||
|
c0b57e66fd | ||
|
00f8ddf48e | ||
|
63cc9a3c5d | ||
|
47c5823f52 | ||
|
5351095f42 | ||
|
64b0420ff4 | ||
|
695b0e46a8 | ||
|
1798529d28 | ||
|
f50e0d4dc6 | ||
|
c0817fba47 | ||
|
0a6e9f500a | ||
|
c8fb35cf53 | ||
|
9d62ea410f | ||
|
d1c36cc197 | ||
|
a99791c98a | ||
|
0e1b86b237 | ||
|
a1cddaf671 |
44
README.md
44
README.md
@@ -1,7 +1,8 @@
|
||||
# UDPspeeder
|
||||
Network Speed-Up Tool. Boost your Connection on a High Lantency High Packet-Loss Link by using Forward Error Correction.
|
||||
|
||||
When used alone,UDPspeeder speeds-up only UDP connection.Nevertheless,if you used UDPspeeder + any UDP-based VPN together,you can speed-up any traffic(include TCP/UDP/ICMP),currently OpenVPN/L2TP/ShadowVPN are confirmed to be supported。
|
||||
When used alone, UDPspeeder speeds-up only UDP connection. Nevertheless, if you used UDPspeeder + any UDP-based VPN together,
|
||||
you can speed-up any traffic(include TCP/UDP/ICMP), currently OpenVPN/L2TP/ShadowVPN are confirmed to be supported。
|
||||
|
||||

|
||||
|
||||
@@ -9,11 +10,22 @@ or
|
||||
|
||||

|
||||
|
||||
Assume your local network to your server is lossy. Just establish a VPN connection to your server with UDPspeeder + any UDP-based VPN, access your server via this VPN connection, then your connection quality will be significantly improved. With well-tuned parameters , you can easily reduce IP or UDP/ICMP packet-loss-rate to less than 0.01% . Besides reducing packet-loss-rate, UDPspeeder can also significantly improve your TCP latency and TCP single-thread download speed.
|
||||
|
||||
[简体中文](/doc/README.zh-cn.md)(内容更丰富)
|
||||
|
||||
###### Note
|
||||
You can use udp2raw with UDPspeeder together to bypass UDP firewalls.
|
||||
udp2raw:https://github.com/wangyu-/udp2raw-tunnel
|
||||
You can use udp2raw with UDPspeeder together to get better speed on some ISP with UDP QoS(UDP throttling).
|
||||
|
||||
udp2raw's repo:
|
||||
|
||||
https://github.com/wangyu-/udp2raw-tunnel
|
||||
|
||||
You can also try tinyFecVPN, a lightweight high-performance VPN with build-in FEC support:
|
||||
|
||||
tinyFecVPN's repo:
|
||||
|
||||
https://github.com/wangyu-/tinyFecVPN
|
||||
|
||||
|
||||
# Efficacy
|
||||
@@ -26,13 +38,13 @@ tested on a link with 100ms latency and 10% packet loss at both direction
|
||||

|
||||
|
||||
# Supported Platforms
|
||||
Linux host (including desktop Linux,Android phone/tablet,OpenWRT router,or Raspberry PI).
|
||||
Linux host (including desktop Linux,Android phone/tablet, OpenWRT router, or Raspberry PI).
|
||||
|
||||
For Windows and MacOS You can run UDPspeeder inside [this](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip) 7.5mb virtual machine image.
|
||||
For Windows and MacOS You can run UDPspeeder inside [this](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20171108.0/lede-17.01.2-x86_virtual_machine_image.zip) 7.5mb virtual machine image.
|
||||
|
||||
# How does it work
|
||||
|
||||
UDPspeeder uses FEC(Forward Error Correction) to reduce packet loss rate,at the cost of addtional bandwidth.The algorithm for FEC is called Reed-Solomon.
|
||||
UDPspeeder uses FEC(Forward Error Correction) to reduce packet loss rate, at the cost of addtional bandwidth. The algorithm for FEC is called Reed-Solomon.
|
||||
|
||||

|
||||
|
||||
@@ -66,15 +78,15 @@ Assume your server ip is 44.55.66.77, you have a service listening on udp port 7
|
||||
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd"
|
||||
```
|
||||
|
||||
Now connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side,and the connection is boosted by UDPspeeder.
|
||||
Now connecting to UDP port 3333 at the client side is equivalent to connecting to port 7777 at the server side, and the connection is boosted by UDPspeeder.
|
||||
|
||||
##### Note
|
||||
|
||||
`-f20:10` means sending 10 redundant packets for every 20 original packets.
|
||||
|
||||
`-k` enables simple XOR encryption to confuse DPI(Deep Packet Inspection)
|
||||
`-k` enables simple XOR encryption
|
||||
|
||||
To run stably,pay attention to MTU.
|
||||
To run stably, pay attention to MTU. You can also try `--mode 0`,there will be no MTU problem in this mode.
|
||||
|
||||
# Advanced Topic
|
||||
### Full Options
|
||||
@@ -125,14 +137,14 @@ log and help options:
|
||||
|
||||
```
|
||||
#### `--fifo` option
|
||||
Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`,you can use following commands to change parameters dynamically:
|
||||
Use a fifo(named pipe) for sending commands to the running program. For example `--fifo fifo.file`, you can use following commands to change parameters dynamically:
|
||||
```
|
||||
echo fec 19:9 >fifo.file
|
||||
echo mtu 1100 >fifo.file
|
||||
echo timeout 5 >fifo.file
|
||||
echo queue-len 100 >fifo.file
|
||||
echo mode 0 >fifo.file
|
||||
echo fec 19:9 > fifo.file
|
||||
echo mtu 1100 > fifo.file
|
||||
echo timeout 5 > fifo.file
|
||||
echo queue-len 100 > fifo.file
|
||||
echo mode 0 > fifo.file
|
||||
```
|
||||
### Speed-Up any traffic with OpenVPN+UDPspeeder
|
||||
### Speed-Up any traffic with OpenVPN + UDPspeeder
|
||||
|
||||
Check [UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).
|
||||
|
100
common.cpp
100
common.cpp
@@ -15,12 +15,16 @@ int about_to_exit=0;
|
||||
raw_mode_t raw_mode=mode_faketcp;
|
||||
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
||||
|
||||
int delay_capacity=0;
|
||||
|
||||
//static int random_number_fd=-1;
|
||||
char iptables_rule[200]="";
|
||||
//int is_client = 0, is_server = 0;
|
||||
|
||||
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
||||
program_mode_t client_or_server=unset_mode;//0 unset; 1client 2server
|
||||
|
||||
working_mode_t working_mode=tunnel_mode;
|
||||
|
||||
int socket_buf_size=1024*1024;
|
||||
|
||||
|
||||
|
||||
@@ -256,6 +260,39 @@ unsigned short csum(const unsigned short *ptr,int nbytes) {
|
||||
|
||||
return(answer);
|
||||
}
|
||||
|
||||
|
||||
unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes) {//works both for big and little endian
|
||||
|
||||
register long sum;
|
||||
unsigned short oddbyte;
|
||||
register short answer;
|
||||
|
||||
sum=0;
|
||||
unsigned short * tmp= (unsigned short *)&ph;
|
||||
for(int i=0;i<6;i++)
|
||||
{
|
||||
sum+=*tmp++;
|
||||
}
|
||||
|
||||
|
||||
while(nbytes>1) {
|
||||
sum+=*ptr++;
|
||||
nbytes-=2;
|
||||
}
|
||||
if(nbytes==1) {
|
||||
oddbyte=0;
|
||||
*((u_char*)&oddbyte)=*(u_char*)ptr;
|
||||
sum+=oddbyte;
|
||||
}
|
||||
|
||||
sum = (sum>>16)+(sum & 0xffff);
|
||||
sum = sum + (sum>>16);
|
||||
answer=(short)~sum;
|
||||
|
||||
return(answer);
|
||||
}
|
||||
|
||||
int set_buf_size(int fd,int socket_buf_size,int force_socket_buf)
|
||||
{
|
||||
if(force_socket_buf)
|
||||
@@ -526,3 +563,62 @@ int create_fifo(char * file)
|
||||
return fifo_fd;
|
||||
}
|
||||
|
||||
|
||||
int new_listen_socket(int &fd,u32_t ip,int port)
|
||||
{
|
||||
fd =socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
|
||||
int yes = 1;
|
||||
//setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
|
||||
|
||||
struct sockaddr_in local_me={0};
|
||||
|
||||
socklen_t slen = sizeof(sockaddr_in);
|
||||
//memset(&local_me, 0, sizeof(local_me));
|
||||
local_me.sin_family = AF_INET;
|
||||
local_me.sin_port = htons(port);
|
||||
local_me.sin_addr.s_addr = ip;
|
||||
|
||||
if (bind(fd, (struct sockaddr*) &local_me, slen) == -1) {
|
||||
mylog(log_fatal,"socket bind error\n");
|
||||
//perror("socket bind error");
|
||||
myexit(1);
|
||||
}
|
||||
setnonblocking(fd);
|
||||
set_buf_size(fd,socket_buf_size);
|
||||
|
||||
mylog(log_debug,"local_listen_fd=%d\n,",fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
int new_connected_socket(int &fd,u32_t ip,int port)
|
||||
{
|
||||
char ip_port[40];
|
||||
sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||
|
||||
struct sockaddr_in remote_addr_in = { 0 };
|
||||
|
||||
socklen_t slen = sizeof(sockaddr_in);
|
||||
//memset(&remote_addr_in, 0, sizeof(remote_addr_in));
|
||||
remote_addr_in.sin_family = AF_INET;
|
||||
remote_addr_in.sin_port = htons(port);
|
||||
remote_addr_in.sin_addr.s_addr = ip;
|
||||
|
||||
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (fd < 0) {
|
||||
mylog(log_warn, "[%s]create udp_fd error\n", ip_port);
|
||||
return -1;
|
||||
}
|
||||
setnonblocking(fd);
|
||||
set_buf_size(fd, socket_buf_size);
|
||||
|
||||
mylog(log_debug, "[%s]created new udp_fd %d\n", ip_port, fd);
|
||||
int ret = connect(fd, (struct sockaddr *) &remote_addr_in, slen);
|
||||
if (ret != 0) {
|
||||
mylog(log_warn, "[%s]fd connect fail\n",ip_port);
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
55
common.h
55
common.h
@@ -44,7 +44,7 @@
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
#include <linux/if_packet.h>
|
||||
|
||||
#include <linux/if_tun.h>
|
||||
|
||||
#include<unordered_map>
|
||||
#include<unordered_set>
|
||||
@@ -67,10 +67,11 @@ typedef u64_t my_time_t;
|
||||
const int max_data_len=2200;
|
||||
const int buf_len=max_data_len+200;
|
||||
|
||||
const u32_t conv_clear_interval=200;
|
||||
|
||||
//const u32_t timer_interval=400;
|
||||
////const u32_t conv_timeout=180000;
|
||||
const u32_t conv_timeout=40000;//for test
|
||||
//const u32_t conv_timeout=40000;//for test
|
||||
const u32_t conv_timeout=180000;
|
||||
const int max_conv_num=10000;
|
||||
const int max_conn_num=200;
|
||||
|
||||
@@ -85,12 +86,12 @@ const u32_t client_retry_interval=1000;
|
||||
|
||||
const u32_t server_handshake_timeout=10000;// this should be much longer than clients. client retry initially ,server retry passtively*/
|
||||
|
||||
const int conv_clear_ratio=10; //conv grabage collecter check 1/10 of all conv one time
|
||||
const int conn_clear_ratio=10;
|
||||
const int conv_clear_min=5;
|
||||
const int conv_clear_ratio=30; //conv grabage collecter check 1/30 of all conv one time
|
||||
const int conn_clear_ratio=50;
|
||||
const int conv_clear_min=1;
|
||||
const int conn_clear_min=1;
|
||||
|
||||
|
||||
const u32_t conv_clear_interval=1000;
|
||||
const u32_t conn_clear_interval=1000;
|
||||
|
||||
|
||||
@@ -98,7 +99,7 @@ const i32_t max_fail_time=0;//disable
|
||||
|
||||
const u32_t heartbeat_interval=1000;
|
||||
|
||||
const u32_t timer_interval=500;//this should be smaller than heartbeat_interval and retry interval;
|
||||
const u32_t timer_interval=400;//this should be smaller than heartbeat_interval and retry interval;
|
||||
|
||||
//const uint32_t conv_timeout=120000; //120 second
|
||||
//const u32_t conv_timeout=120000; //for test
|
||||
@@ -115,10 +116,13 @@ extern int about_to_exit;
|
||||
enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
|
||||
extern raw_mode_t raw_mode;
|
||||
enum program_mode_t {unset_mode=0,client_mode,server_mode};
|
||||
extern program_mode_t program_mode;
|
||||
extern program_mode_t client_or_server;
|
||||
extern unordered_map<int, const char*> raw_mode_tostring ;
|
||||
|
||||
extern int delay_capacity;
|
||||
enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode};
|
||||
extern working_mode_t working_mode;
|
||||
|
||||
extern int socket_buf_size;
|
||||
|
||||
|
||||
typedef u32_t id_t;
|
||||
@@ -131,8 +135,8 @@ typedef u64_t anti_replay_seq_t;
|
||||
|
||||
typedef u64_t fd64_t;
|
||||
|
||||
enum dest_type{none=0,type_ip_port,type_fd64,type_ip_port_conv,type_fd64_conv/*,type_fd*/};
|
||||
|
||||
//enum dest_type{none=0,type_fd64_ip_port,type_fd64,type_fd64_ip_port_conv,type_fd64_conv/*,type_fd*/};
|
||||
enum dest_type{none=0,type_fd64_ip_port,type_fd64,type_fd,type_write_fd,type_fd_ip_port/*,type_fd*/};
|
||||
|
||||
struct ip_port_t
|
||||
{
|
||||
@@ -143,11 +147,22 @@ struct ip_port_t
|
||||
char * to_s();
|
||||
};
|
||||
|
||||
struct fd64_ip_port_t
|
||||
{
|
||||
fd64_t fd64;
|
||||
ip_port_t ip_port;
|
||||
};
|
||||
struct fd_ip_port_t
|
||||
{
|
||||
int fd;
|
||||
ip_port_t ip_port;
|
||||
};
|
||||
union inner_t
|
||||
{
|
||||
ip_port_t ip_port;
|
||||
//int fd;
|
||||
fd64_t fd64;
|
||||
int fd;
|
||||
fd64_ip_port_t fd64_ip_port;
|
||||
fd_ip_port_t fd_ip_port;
|
||||
};
|
||||
struct dest_t
|
||||
{
|
||||
@@ -162,6 +177,13 @@ struct fd_info_t
|
||||
ip_port_t ip_port;
|
||||
};
|
||||
|
||||
struct pseudo_header {
|
||||
u_int32_t source_address;
|
||||
u_int32_t dest_address;
|
||||
u_int8_t placeholder;
|
||||
u_int8_t protocol;
|
||||
u_int16_t tcp_length;
|
||||
};
|
||||
|
||||
u64_t get_current_time();
|
||||
u64_t get_current_time_us();
|
||||
@@ -195,6 +217,7 @@ void setnonblocking(int sock);
|
||||
int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0);
|
||||
|
||||
unsigned short csum(const unsigned short *ptr,int nbytes);
|
||||
unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes);
|
||||
|
||||
void signal_handler(int sig);
|
||||
int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len);
|
||||
@@ -217,4 +240,8 @@ int create_fifo(char * file);
|
||||
int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);
|
||||
*/
|
||||
|
||||
int new_listen_socket(int &fd,u32_t ip,int port);
|
||||
|
||||
int new_connected_socket(int &fd,u32_t ip,int port);
|
||||
|
||||
#endif /* COMMON_H_ */
|
||||
|
@@ -47,7 +47,7 @@ void conv_manager_t::clear()
|
||||
{
|
||||
//if(disable_conv_clear) return ;//////what was the purpose of this code?
|
||||
|
||||
if(program_mode==server_mode)
|
||||
if(client_or_server==server_mode)
|
||||
{
|
||||
for(auto it=conv_to_u64.begin();it!=conv_to_u64.end();it++)
|
||||
{
|
||||
@@ -111,7 +111,7 @@ int conv_manager_t::erase_conv(u32_t conv)
|
||||
//if(disable_conv_clear) return 0;
|
||||
assert(conv_last_active_time.find(conv)!=conv_last_active_time.end());
|
||||
u64_t u64=conv_to_u64[conv];
|
||||
if(program_mode==server_mode)
|
||||
if(client_or_server==server_mode)
|
||||
{
|
||||
server_clear_function(u64);
|
||||
}
|
||||
|
@@ -107,7 +107,7 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o
|
||||
fec_encode_manager_t fec_encode_manager;
|
||||
fec_decode_manager_t fec_decode_manager;
|
||||
my_timer_t timer;
|
||||
ip_port_t ip_port;
|
||||
//ip_port_t ip_port;
|
||||
u64_t last_active_time;
|
||||
stat_t stat;
|
||||
conn_info_t()
|
||||
|
@@ -66,7 +66,11 @@ int delay_manager_t::add(my_time_t delay,const dest_t &dest,char *data,int len)
|
||||
|
||||
delay_data_t tmp=delay_data;
|
||||
tmp.data=(char *)malloc(delay_data.len+100);
|
||||
|
||||
if(!tmp.data)
|
||||
{
|
||||
mylog(log_warn, "malloc() returned null in delay_manager_t::add()");
|
||||
return -1;
|
||||
}
|
||||
memcpy(tmp.data,data,delay_data.len);
|
||||
|
||||
my_time_t tmp_time=get_current_time_us();
|
||||
|
@@ -11,13 +11,25 @@
|
||||
|
||||

|
||||
|
||||
另外,可以和udp2raw串联使用,在加速的同时把UDP伪装成TCP,防止UDP被运营商QOS或屏蔽。udp2raw: https://github.com/wangyu-/udp2raw-tunnel
|
||||
对于某些运营商,UDPspeeder跟udp2raw配合可以达到更好的速度,udp2raw负责把UDP伪装成TCP,来绕过运营商的UDP限速。
|
||||
|
||||
udp2raw的repo:
|
||||
|
||||
https://github.com/wangyu-/udp2raw-tunnel
|
||||
|
||||
如果你嫌UDPspeeder+OpenVPN麻烦,你可以尝试tinyFecVPN,一个集成了UDPspeeder功能的VPN:
|
||||
|
||||
tinyFecVPN的repo:
|
||||
|
||||
https://github.com/wangyu-/tinyFecVPN
|
||||
|
||||
|
||||
#### 效果
|
||||

|
||||

|
||||
|
||||

|
||||
#### 原理简介
|
||||
主要原理是通过冗余数据来对抗网络的丢包,发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包,其中FEC算法是Reed-Solomon。
|
||||
主要原理是通过冗余数据来对抗网络的丢包,发送冗余数据的方式支持FEC(Forward Error Correction)和多倍发包,其中FEC算法是Reed-Solomon。
|
||||
|
||||
FEC方式的原理图:
|
||||
|
||||
@@ -28,7 +40,7 @@ FEC方式的原理图:
|
||||
|
||||
在多个冗余包之间引入延迟(时间可配)来对抗突发性的丢包,避开中间路由器因为瞬时buffer长度过长而连续丢掉所有副本。
|
||||
|
||||
模拟一定的延迟抖动(时间可配),这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
|
||||
模拟一定的延迟抖动(时间可配),这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
|
||||
|
||||
输出UDP收发情况报告,可以看出丢包率。
|
||||
|
||||
@@ -39,11 +51,13 @@ client支持多个udp连接,server也支持多个client
|
||||
# 简明操作说明
|
||||
|
||||
### 环境要求
|
||||
Linux主机,可以是桌面版,可以是android手机/平板,可以是openwrt路由器,也可以是树莓派。在windows和mac上配合虚拟机可以稳定使用(speeder跑在Linux里,其他应用照常跑在window里,桥接模式测试可用),可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20170918.0/lede-17.01.2-x86_virtual_machine_image_with_udp2raw_pre_installed.zip)虚拟机镜像,大小只有7.5mb,免去在虚拟机里装系统的麻烦。
|
||||
Linux主机,可以是桌面版,可以是android手机/平板,可以是openwrt路由器,也可以是树莓派。Release中提供了`amd64`、`x86`、`arm`、`mips_be`、`mips_le`的预编译binary.
|
||||
|
||||
对于windows和mac用户,在虚拟机中可以稳定使用(speeder跑在Linux里,其他应用照常跑在window里,桥接模式测试可用)。可以使用[这个](https://github.com/wangyu-/udp2raw-tunnel/releases/download/20171108.0/lede-17.01.2-x86_virtual_machine_image.zip)虚拟机镜像,大小只有7.5mb,免去在虚拟机里装系统的麻烦;虚拟机自带ssh server,可以scp拷贝文件,可以ssh进去,可以复制粘贴,root密码123456。
|
||||
|
||||
android版需要通过terminal运行。
|
||||
|
||||
###### 注意
|
||||
##### 注意
|
||||
在使用虚拟机时,建议手动指定桥接到哪个网卡,不要设置成自动。否则可能会桥接到错误的网卡。
|
||||
|
||||
### 安装
|
||||
@@ -55,25 +69,25 @@ https://github.com/wangyu-/UDPspeeder/releases
|
||||
假设你有一个server,ip为44.55.66.77,有一个服务监听在udp 7777端口。 假设你需要加速本地到44.55.66.77:7777的流量。
|
||||
```
|
||||
在server端运行:
|
||||
./speederv2 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -f20:10 -k "passwd"
|
||||
./speederv2 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -f20:10 -k "passwd" --mode 0
|
||||
|
||||
在client端运行:
|
||||
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd"
|
||||
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f20:10 -k "passwd" --mode 0
|
||||
```
|
||||
|
||||
现在client和server之间建立起了tunnel。想要连接44.55.66.77:7777,只需要连接 127.0.0.1:3333。来回的所有的udp流量会被加速。
|
||||
|
||||
###### 备注:
|
||||
##### 备注:
|
||||
|
||||
-f20:10 表示对每20个原始数据发送10个冗余包。
|
||||
`-f20:10`表示对每20个原始数据发送10个冗余包。`-f20:10` 和`-f 20:10`都是可以的,空格可以省略,对于所有的单字节option都是如此。对于双字节option,例如后面会提到的`--mode 0`,空格不可以省略。
|
||||
|
||||
-k 指定一个字符串,server/client间所有收发的包都会被异或,改变协议特征,防止UDPspeeder的协议被运营商针对。
|
||||
`-k` 指定一个字符串,开启简单的异或加密
|
||||
|
||||
###### 注意
|
||||
推荐使用`--mode 0`选项,否则你可能需要考虑MTU问题。
|
||||
|
||||
要为UDPspeeder加速的应用设置好MTU(不是在UDPspeeder中,是在被加速的应用中),建议设置为1200。
|
||||
##### 注意
|
||||
|
||||
另外,如果被加速的应用不能调整MTU,也可以在UDPspeeder中通过`--mode 0 --mtu 1200`设置MTU。关于`--mode 0`和`--mtu`的具体含义请看下文。
|
||||
这里推荐的参数是给日常/非游戏情况下使用的;玩游戏请用 [使用经验](https://github.com/wangyu-/UDPspeeder/wiki/使用经验) 里推荐的参数。
|
||||
|
||||
# 进阶操作说明
|
||||
|
||||
@@ -128,138 +142,103 @@ log and help options:
|
||||
##### `-f` 选项
|
||||
设置fec参数,影响数据的冗余度。
|
||||
##### `--timeout` 选项
|
||||
指定fec编码器在编码时候最多可以引入多大的延迟。越高fec越有效率,加速游戏时调低可以降低延迟。
|
||||
|
||||
指定fec编码器在编码时候最多可以引入多大的延迟。越高fec越有效率,调低可以降低延迟,但是会牺牲效率。
|
||||
##### `--mode` 选项 和 `--mtu`选项
|
||||
fec编码器的工作模式。对于mode 0,编码器会积攒一定数量的packet,然后把他们合并再切成等长的片段(切分长度由--mtu指定)。对于mode 1,编码器不会做任何切分,而是会把packet按最大长度对齐,fec冗余包的长度为对齐后的长度(最大长度)。
|
||||
|
||||
mode 0更省流量,在丢包率正常的情况下效果和mode 1是一样的;mode 1延迟更低,在极高丢包的情况下表现更好。
|
||||
简单来说`--mode 0`更省流量,没有mtu问题;`--mode 1`可以稍微降低一点延迟,需要考虑mtu;另外还有个`--mode 0 -q1`模式,多倍发包专用,没有延迟,也没有mtu问题,适合游戏,但是最耗流量。
|
||||
|
||||
mode 0使用起来可以不用关注mtu,因为fec编码器会帮你把包切分到合理的大小;用mode 1时必须合理设置上层应用的mtu。mode 0模式中--mtu选项决定切分的片段的长度,mode 1模式中--mtu选项只起检查作用,如果超过了--mtu指定的值,数据包会被丢弃。
|
||||
具体见,https://github.com/wangyu-/UDPspeeder/wiki/mode和mtu选项
|
||||
|
||||
mode 0模式的流量消耗基本完全透明。mode 1因为涉及到数据按最大长度对齐,所以流量消耗不是完全可预期。不过就实际使用来看,mode 1消耗的额外流量不多。
|
||||
|
||||
mode 0模式数据包一般不会乱序,除非网络本身有严重乱序;mode 1模式被恢复的数据包可能会乱序,不过UDP本来就允许乱序,对绝大多数应用没有影响。mode 0模式反而可以纠正一些乱序情况。
|
||||
对于新手,建议不要纠结这些参数的具体含义,就用我在`使用经验`里推荐的设置,不要乱改参数,尤其是不要改`--mtu`。
|
||||
|
||||
##### `--report` 选项
|
||||
数据发送和接受报告。开启后可以根据此数据推测出包速和丢包率等特征。
|
||||
数据发送和接受报告。开启后可以根据此数据推测出包速和丢包率等特征。`--report 10`表示每10秒生成一次报告。
|
||||
|
||||
##### `-i` 选项
|
||||
指定一个时间窗口,长度为n毫秒。同一个fec分组的数据在发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0,因为这个功能需要用到时钟,在某些虚拟机里时钟不稳定,可能会导致个别包出现非常大的延迟,所以默认关掉了。这个功能很有用,默认参数效果不理想时可以尝试打开。
|
||||
指定一个时间窗口,长度为n毫秒。同一个fec分组的数据在发送时候会被均匀分散到这n毫秒中。可以对抗突发性的丢包。默认值是0,因为这个功能需要用到时钟,在某些虚拟机里时钟不稳定,可能会导致个别包出现非常大的延迟,所以默认关掉了。这个功能很有用,默认参数效果不理想时可以尝试打开,比如用`-i 10`。这个选项的跟通信原理上常说的`交错fec` `交织fec`的原理是差不多的。
|
||||
|
||||
##### `-j` 选项
|
||||
为原始数据的发送,增加一个延迟抖动值。这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。配合-t选项使用。正常情况下跨国网络本身的延迟抖动就很大,可以不用设-j。这个功能也需要时钟,默认关掉了,不过一般情况应该不需要这个功能。
|
||||
|
||||
-j选项不但可以模拟延迟抖动,也可以模拟延迟。
|
||||
|
||||
##### `--random-drop` 选项
|
||||
随机丢包。模拟恶劣的网络环境时使用。如果你的网络现在没有多大丢包,但是你想测试一下高丢包环境下各种FEC参数的表现,可以开这个选项。
|
||||
随机丢包。模拟高丢包的网络环境时使用。 `--random-drop`和`-j`选项一起用,可以模拟高延迟(或者高延迟抖动)高丢包的网络,可用于测试FEC参数在各种网络环境下的表现。
|
||||
|
||||
##### `-q` 选项
|
||||
仅对mode 0模式有用。设置fec编码器的最大队列长度。 比如`-q5`的意思是,在编码器积攒了5个数据包后,就立即发送。合理使用可以改善延迟。在下文的`使用经验`里有提到用`--mode 0 -q1` 来多倍发包。
|
||||
|
||||
`-q`和 `--timeout`的作用类似。`-q`决定fec编码器积攒了多少个数据包之后,立即发送。`--timeout`决定编码器收到第一个数据包以后,最多延迟多少毫秒后发送。
|
||||
|
||||
默认值是200,也就是尽可能多得积攒数据。
|
||||
|
||||
建议不要自己调整这个参数,除非是用我在`使用经验`里推荐给你的形式。
|
||||
|
||||
#### `--fifo` option
|
||||
用fifo(命名管道)向运行中的程序发送command。例如`--fifo fifo.file`,可用的command有:
|
||||
```
|
||||
echo fec 19:9 > fifo.file
|
||||
echo mtu 1100 > fifo.file
|
||||
echo timeout 5 > fifo.file
|
||||
echo queue-len 100 > fifo.file
|
||||
echo mode 0 > fifo.file
|
||||
```
|
||||
可以动态改变fec编码器参数。可以从程序的log里看到command是否发送成功。
|
||||
|
||||
### 以下设置两端必须相同。
|
||||
|
||||
##### `-k`选项
|
||||
指定一个字符串,server/client间所有收发的包都会被异或,改变协议特征,防止UDPspeeder的协议被运营商针对。
|
||||
|
||||
##### `--disable-obscure`
|
||||
UDPspeeder默认情况下会对每个发出的数据包随机填充和异或一些字节(4~32字节),这样通过抓包难以发现你发了冗余数据,防止VPS被封。这个功能只是为了小心谨慎,即使你关掉这个功能,基本上也没问题,关掉可以省一些带宽和CPU。
|
||||
|
||||
##### `-q,--queue-len`
|
||||
编码器在做FEC前最多积攒多少个数据包,只对mode 0有效。除非是使用下文`V2版如何多倍发包`里面提到的用法,不建议改动。
|
||||
#### `--fifo` option
|
||||
用fifo(命名管道)向运行中的程序发送command. 例如`--fifo fifo.file`,可用的command有:
|
||||
```
|
||||
echo fec 19:9 >fifo.file
|
||||
echo mtu 1100 >fifo.file
|
||||
echo timeout 5 >fifo.file
|
||||
echo queue-len 100 >fifo.file
|
||||
echo mode 0 >fifo.file
|
||||
```
|
||||
可以动态改变fec编码器参数。
|
||||
UDPspeeder默认情况下会对每个发出的数据包随机填充和异或一些字节(4~32字节),这样通过抓包难以发现你发了冗余数据,防止VPS被封。这个功能只是为了小心谨慎,即使你关掉这个功能,基本上也没问题,关掉可以省一些带宽和CPU。`--disable-obscure`可以关掉这个功能。
|
||||
|
||||
# 使用经验
|
||||
|
||||
### 在FEC和多倍发包之间如何选择
|
||||
|
||||
对于游戏,游戏的流量本身不大,延迟很重要,多倍发包是最佳解决方案,多倍发包不会引入额外的延迟。FEC编码器需要先积攒一些数据,才可以做FEC,延迟无法避免;对于多倍发包,没有这个问题,所以没有延迟。
|
||||
|
||||
对于其他日常应用(延迟要求一般),在合理配置的情况下,FEC的效果肯定好过多倍发包。不过需要根据网络的最大丢包来配置FEC参数,才能有稳定的效果。如果配置不当,对于--mode 1可能会完全没有效果;对于--mode 0,可能效果会比不用UDPspeeder还差。
|
||||
|
||||
对于游戏以外的应用,推荐使用FEC。但是,如果FEC版的默认参数在你那边效果很差,而你又不会调,可以先用多倍发包。
|
||||
|
||||
### V2版如何多倍发包
|
||||
|
||||
只要在设置-f参数时把x设置为1,fec算法就退化为多倍发包了。例如-f1:1,表示2倍发包,-f1:2表示3倍发包,以此类推。另外可以加上`--mode 0 -q1`参数,防止fec编码器试图积攒和合并数据,获得最低的延迟。
|
||||
|
||||
2倍发包的完整参数:
|
||||
|
||||
```
|
||||
./speederv2 -s -l0.0.0.0:4096 -r127.0.0.1:7777 -f1:1 -k "passwd" --mode 0 -q1
|
||||
./speederv2 -c -l0.0.0.0:3333 -r44.55.66.77:4096 -f1:1 -k "passwd" --mode 0 -q1
|
||||
```
|
||||
|
||||
使用了`--mode 0 -q1`以后,`--timeout`选项不起作用,所以不用调。
|
||||
|
||||
如果你只需要多倍发包,可以直接用回V1版,V1版配置更简单,占用内存更小,而且经过了几个月的考验,很稳定。
|
||||
|
||||
### 根据网络丢包合理设置FEC参数
|
||||
|
||||
默认的FEC参数为-f20:10,对每20个包,额外发送10个冗余包,也就是1.5倍发包。已经可以适应绝大多数的网络情况了,对于10%的网络丢包,可以降低到0.01%以下;对于20%的网络丢包,可以降低到2.5%。
|
||||
|
||||
如果你的网络丢包很低,比如在3%以下,可以尝试调低参数。比如-f20:5,也就是1.2倍发包,这个参数已经足够把3%的丢包降低到0.01%以下了。
|
||||
|
||||
如果网络丢包超过20%,需要把-f20:10调大。
|
||||
|
||||
如果你实在不会配,那么也可以用回V1版。
|
||||
|
||||
### 根据CPU处理能力来调整FEC参数
|
||||
|
||||
FEC算法很吃CPU,初次使用建议关注UDPspeeder的CPU占用。如果CPU被打满,可以在冗余度不变的情况下把FEC分组大小调小,否则的话效果可能很差。
|
||||
|
||||
比如-f20:10和-f10:5,都是1.5倍的冗余度,而-f20:10的FEC分组大小是30个包,-f10:5的FEC分组大小是15个包。-f20:10更费CPU,但是在一般情况下效果更稳定。把分组调小可以节省CPU。
|
||||
|
||||
另外,fec分组大小不宜过大,否则不但很耗CPU,还有其他副作用,建议x+y<50。
|
||||
|
||||
### 改变FEC参数而不断线
|
||||
|
||||
`--fifo`选项可以在运行时无缝改变FEC参数,无需重启程序,也不会断线。
|
||||
|
||||
### 为什么使用之后效果反而变差了?
|
||||
|
||||
有可能是你用了mode 0参数,而又没调好参数。
|
||||
|
||||
如果你没有使用mode 0,而确实效果变差了,那很可能是因为你的运营商对UDP有限制。一般看视频和下载都是TCP流量,而用UDPspeeder中转后流量变成了UDP流量,如果运营商对UDP做了限制,就可能导致效果比不用还差。用udp2raw可以解决,udp2raw: https://github.com/wangyu-/udp2raw-tunnel
|
||||
|
||||
|
||||
### UDPspeeder和BBR/锐速配合
|
||||
|
||||
UDPspeeder和BBR/锐速可以配合使用,UDPspeeder工作在IP层负责降低丢包率,BBR/锐速工作在TCP层负责优化拥塞和重传。这种情况下,可以调低UDPspeeder的冗余度,能把丢包率降低到5%以内就可以了,剩下的交给BBR/锐速解决,这样预计可以节省一些流量。如果是UDPspeeder跟Linux默认的Cubic一起用,最少也要把丢包率降低到1%以下才能流畅使用TCP。
|
||||
|
||||
对下文的`UDPspeeder + openvpn`和`UDPspeeder + openvpn + $***`方法有效。不过有一点区别,具体见下文。
|
||||
|
||||
### UDPspeeder和Kcptun配合
|
||||
|
||||
UDPspeeder和Kcptun配合,UDPspeeder和Kcptun可以并联也可以串联。
|
||||
|
||||
并联的情况下,让kcptun负责加速TCP,UDPspeeder负责加速UDP。见下文的`UDPspeeder + kcptun + $*** 同时加速tcp和udp流量`。
|
||||
|
||||
串联的情况。UDPspeeder的FEC跟Kcptun自带的相比:可以对两个方向设置不同的FEC参数、有一个更省流量的mode 0模式、可以动态改变FEC参数;但是UDPspeeder本身不优化拥塞和重传算法。所以UDPspeeder和Kcptun也可以配合使用,结合两者的优点。
|
||||
|
||||
串联时可以关掉Kcptun的FEC,让UDPspeeder接管FEC功能。这样UDPspeeder工作在UDP层负责降低丢包率,Kcptun工作在应用层用kcp算法负责优化拥塞和重传,能起到和`UDPspeeder+BBR/锐速`类似的效果。
|
||||
|
||||
如果发Issue问Kcptun+UDPspeeder相关的问题,一定要说明是并联还是串联。
|
||||
https://github.com/wangyu-/UDPspeeder/wiki/使用经验
|
||||
|
||||
# 应用
|
||||
|
||||
#### UDPspeeder + openvpn加速任何流量
|
||||
#### UDPspeeder + OpenVPN加速任何流量,也适用于其他VPN
|
||||

|
||||
|
||||
具体配置见,[UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).
|
||||
|
||||
可以和BBR/锐速叠加,不过BBR/锐速部署在VPS上只对从本地到VPS的流量有效,对从本地到第三方服务器的流量无效。
|
||||
可以和BBR/锐速叠加,不过BBR/锐速部署在VPS上只对本地和VPS间的流量有效,对从本地和第三方服务器间的流量无效。
|
||||
|
||||
需要在服务端开启ipforward和NAT。在客户端改路由表(可以手动修改,也可以由OpenVPN的redirect-gateway选项自动加好)。
|
||||
|
||||
Linux具体配置: [UDPspeeder + openvpn config guide](/doc/udpspeeder_openvpn.md).
|
||||
|
||||
Windows具体配置: [win10系统UDPspeeder+OpenVPN的完整设置](https://github.com/wangyu-/UDPspeeder/wiki/win10系统UDPspeeder-OpenVPN的完整设置)
|
||||
|
||||
如果UDPspeeder + OpenVPN对你来说显得太麻烦了,你可以尝试一下tinyFecVPN,一个集成了UDPspeeder功能的VPN:
|
||||
|
||||
https://github.com/wangyu-/tinyFecVPN/
|
||||
|
||||
#### UDPspeeder + kcptun/finalspeed + $*** 同时加速tcp和udp流量
|
||||
如果你需要用加速的tcp看视频和下载文件,这样效果比UDPspeeder+vpn方案更好(在没有BBR的情况下)。
|
||||
如果你需要用加速的tcp看视频和下载文件,这样效果可能比没有BBR的UDPspeeder+vpn方案更好。另外,如果你需要玩游戏,但是嫌配VPN麻烦,也可以用这种方案。
|
||||

|
||||
|
||||
#### UDPspeeder + openvpn + $*** 混合方案
|
||||
具体配置方法简介:
|
||||
|
||||
假设$\*\*\* server监听在在44.55.66.77的443端口(tcp和udp同时)。用kcptun把tcp 443映射到本地的tcp 1234;用UDPspeeder把udp 443的映射到本地的udp 1234。
|
||||
然后让$\*\*\* client 去连127.0.0.1:1234就可以了,tcp和udp都被加速了。完整命令:
|
||||
```
|
||||
run at server side:
|
||||
./kcp_server -l ":4000" -t "127.0.0.1:443" -mode fast2
|
||||
./speederv2 -s -l0.0.0.0:4001 -r127.0.0.1:443 -f20:10 -k "passwd"
|
||||
|
||||
run at client side:
|
||||
./kcp_client -l ":1234" -r "44.55.66.77:4000" -mode fast2
|
||||
./speederv2 -c -l0.0.0.0:1234 -r44.55.66.77:4001 -f20:10 -k "passwd"
|
||||
```
|
||||
|
||||
这就是全部的命令了。issue里有很多人困惑于怎么把tcp和udp流量"分开",其实很简单就可以做到。
|
||||
|
||||
如果只需要加速UDP,不需要加速TCP,可以把kcptun换成其他的任意端口转发方式,比如ncat/socat/ssh tunnel/iptables/[tinyPortMapper](https://github.com/wangyu-/tinyPortMapper/releases)。
|
||||
|
||||
另外,如果没有kcptun只有BBR/锐速的话,也可以把kcptun换成ncat/socat/ssh tunnel/iptables/[tinyPortMapper](https://github.com/wangyu-/tinyPortMapper/releases)。这样,TCP流量由锐速/BBR加速,UDP由UDPspeeder加速。
|
||||
|
||||
#### UDPspeeder + openvpn + $*** 混合方案,也适用于其他VPN
|
||||
也是我正在用的方案。优点是可以随时在vpn和$\*\*\*方案间快速切换。
|
||||
实际部署起来比图中看起来的还要简单。不需要改路由表,不需要写iptables规则和开启NAT,需要做的只是用openvpn分配的ip访问$*** server。
|
||||
|
||||
@@ -269,6 +248,20 @@ UDPspeeder和Kcptun配合,UDPspeeder和Kcptun可以并联也可以串联。
|
||||
|
||||
可以和BBR/锐速叠加,BBR/锐速只要部署在VPS上就有效。
|
||||
|
||||
也可以用[tinyFecVPN](https://github.com/wangyu-/tinyFecVPN/) + $\*\*\* ,配置起来更简单。
|
||||
|
||||
# 应用实例
|
||||
|
||||
#### win10系统UDPspeeder+OpenVPN的完整设置
|
||||
|
||||
https://github.com/wangyu-/UDPspeeder/wiki/win10系统UDPspeeder-OpenVPN的完整设置
|
||||
|
||||
|
||||
#### 用树莓派做路由器,搭建透明代理,加速游戏主机的网络
|
||||
|
||||
https://github.com/wangyu-/UDPspeeder/wiki/用树莓派做路由器,搭建透明代理,加速游戏主机的网络
|
||||
|
||||
|
||||
# 编译教程
|
||||
暂时先参考udp2raw的这篇教程,几乎一样的过程。
|
||||
|
||||
|
@@ -15,7 +15,7 @@ UDP双边加速工具,降低丢包率,配合vpn可以加速任何协议,
|
||||
|
||||
每个冗余数据包都是间隔数毫秒(可配置)以后延迟发出的,可以避开中间路由器因为瞬时buffer长度过长而连续丢掉所有副本。
|
||||
|
||||
模拟一定的延迟抖动,这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
|
||||
模拟一定的延迟抖动,这样上层应用计算出来的RTT方差会更大,以等待后续冗余包的到达,不至于发生在冗余包到达之前就触发重传的尴尬。
|
||||
|
||||
#### 适用场景
|
||||
绝大部分流量不高的情况。程序本身加速udp,但是配合openvpn可以加速任何流量。网络状况不好时,游戏卡得没法玩,或者网页卡得没法打开,使用起来效果最好。对于解决语音通话的断断续续效果也不错。不适合大流量的场景,比如BT下载和在线看视频。 无论从自己使用效果的角度,还是从国际出口带宽占用的角度,都建议不要在大流量环境使用。
|
||||
@@ -134,7 +134,7 @@ other options:
|
||||
|
||||
跟openvpn via kcptun方式的对比:
|
||||
|
||||
kcptun在udp层有RS code,也是一种冗余传输,通过openvpn把流量转成tcp,再通过kcptun加速是有一定效果的。但是tcp只支持按序到达。按序到达的意思是,如果你发了1 2 3 4 5 6 ,6个包,如果第一个包丢了,那么必须等第一个包重传成功以后 2 3 4 5 6 才能到达;只要有一个包不到,后续数据包就要一直等待。用tcp承载udp流量会破坏udp的实时性。会造成游戏卡顿更严重。
|
||||
kcptun在udp层有RS code,也是一种冗余传输,通过openvpn把流量转成tcp,再通过kcptun加速是有一定效果的。但是tcp只支持按序到达。按序到达的意思是,如果你发了1 2 3 4 5 6,6个包,如果第一个包丢了,那么必须等第一个包重传成功以后 2 3 4 5 6 才能到达;只要有一个包不到,后续数据包就要一直等待。用tcp承载udp流量会破坏udp的实时性。会造成游戏卡顿更严重。
|
||||
|
||||
udp协议本身是ip协议加上了端口之后的直接封装,udp继承了ip协议的实时/乱序到达特性,更适合中转vpn。
|
||||
|
||||
|
@@ -39,7 +39,7 @@ mute 20
|
||||
|
||||
comp-lzo no
|
||||
|
||||
fragment 1200 ##### very important you can turn it up a bit. but,the lower the safer
|
||||
fragment 1200 ##### very important you can turn it up a bit. but, the lower the safer
|
||||
mssfix 1200 ##### very important
|
||||
|
||||
sndbuf 2000000 ##### important
|
||||
@@ -79,7 +79,7 @@ mute 20
|
||||
|
||||
comp-lzo no
|
||||
|
||||
fragment 1200 ##### very important you can turn it up a bit. but,the lower the safer
|
||||
fragment 1200 ##### very important you can turn it up a bit. but, the lower the safer
|
||||
mssfix 1200 ##### very important
|
||||
|
||||
sndbuf 2000000 ##### important
|
||||
@@ -88,9 +88,9 @@ txqueuelen 4000 ##### suggested
|
||||
```
|
||||
|
||||
##### Note:
|
||||
If you use the `redirect-gateway` option of OpenVPN,you may need to add a route exception for your remote server ip at client side.Otherwise OpenVPN may hijack UDPspeeder 's traffic.
|
||||
If you use the `redirect-gateway` option of OpenVPN, you may need to add a route exception for your remote server ip at client side.Otherwise OpenVPN may hijack UDPspeeder 's traffic.
|
||||
|
||||
For example,depend on your network environment,the command may looks like:
|
||||
For example, depend on your network environment, the command may looks like:
|
||||
```
|
||||
ip route add 44.55.66.77 via 44.55.66.1
|
||||
```
|
||||
@@ -100,3 +100,8 @@ or
|
||||
ip route add 44.55.66.77 dev XXX
|
||||
```
|
||||
(run at client side)
|
||||
|
||||
##### Other Info
|
||||
You can also use tinyFecVPN,a lightweight VPN with build-in FEC support:
|
||||
|
||||
https://github.com/wangyu-/tinyFecVPN
|
||||
|
@@ -16,7 +16,7 @@ int g_fec_redundant_num=10;
|
||||
int g_fec_mtu=1250;
|
||||
int g_fec_queue_len=200;
|
||||
int g_fec_timeout=8*1000; //8ms
|
||||
int g_fec_mode=1;
|
||||
int g_fec_mode=0;
|
||||
|
||||
int dynamic_update_fec=1;
|
||||
|
||||
@@ -146,11 +146,11 @@ fec_encode_manager_t::fec_encode_manager_t()
|
||||
}
|
||||
timer_fd64=fd_manager.create(timer_fd);
|
||||
|
||||
re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_queue_len,fec_timeout,fec_mode);
|
||||
reset_fec_parameter(g_fec_data_num,g_fec_redundant_num,g_fec_mtu,g_fec_queue_len,g_fec_timeout,g_fec_mode);
|
||||
|
||||
|
||||
seq=(u32_t)get_true_random_number(); //TODO temp solution for a bug.
|
||||
}
|
||||
int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu,int queue_len,int timeout,int mode)
|
||||
int fec_encode_manager_t::reset_fec_parameter(int data_num,int redundant_num,int mtu,int queue_len,int timeout,int mode)
|
||||
{
|
||||
fec_data_num=data_num;
|
||||
fec_redundant_num=redundant_num;
|
||||
@@ -160,15 +160,8 @@ int fec_encode_manager_t::re_init(int data_num,int redundant_num,int mtu,int que
|
||||
fec_mode=mode;
|
||||
|
||||
assert(data_num+redundant_num<max_fec_packet_num);
|
||||
counter=0;
|
||||
blob_encode.clear();
|
||||
ready_for_output=0;
|
||||
//seq=0;
|
||||
|
||||
itimerspec zero_its;
|
||||
memset(&zero_its, 0, sizeof(zero_its));
|
||||
|
||||
timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &zero_its, 0);
|
||||
clear();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -192,7 +185,7 @@ int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/)
|
||||
{
|
||||
mylog(log_trace,"counter=%d\n",counter);
|
||||
assert(len<=65535&&len>=0);
|
||||
assert(len<=fec_mtu);
|
||||
//assert(len<=fec_mtu);//relax this limitation
|
||||
char * p=input_buf[counter]+sizeof(u32_t)+4*sizeof(char);//copy directly to final position,avoid unnecessary copy.
|
||||
//remember to change this,if protocol is modified
|
||||
|
||||
@@ -225,15 +218,19 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
|
||||
//int counter_back=counter;
|
||||
assert(fec_mode==0||fec_mode==1);
|
||||
|
||||
if(fec_mode==0&& s!=0 &&counter==0&&blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu)
|
||||
if(fec_mode==0&& s!=0 &&counter==0)
|
||||
{
|
||||
mylog(log_warn,"message too long len=%d,ignored\n",len);
|
||||
int out_len=blob_encode.get_shard_len(fec_data_num,len);
|
||||
if(out_len>fec_mtu)
|
||||
{
|
||||
mylog(log_warn,"message too long ori_len=%d out_len=%d fec_mtu=%d,ignored\n",len,out_len,fec_mtu);
|
||||
return -1;
|
||||
}
|
||||
if(fec_mode==1&&s!=0&&len>=fec_mtu)
|
||||
}
|
||||
if(fec_mode==1&&s!=0&&len>fec_mtu)
|
||||
{
|
||||
mylog(log_warn,"message too long len=%d fec_mtu=%d,ignored\n",len,fec_mtu);
|
||||
return -1;
|
||||
mylog(log_warn,"mode==1,message len=%d,len>fec_mtu,fec_mtu=%d,packet may not be delivered\n",len,fec_mtu);
|
||||
//return -1;
|
||||
}
|
||||
if(s==0&&counter==0)
|
||||
{
|
||||
@@ -242,7 +239,7 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
|
||||
}
|
||||
if(s==0) about_to_fec=1;//now
|
||||
|
||||
if(fec_mode==0&& blob_encode.get_shard_len(fec_data_num,len)>=fec_mtu) {about_to_fec=1; delayed_append=1;}//fec then add packet
|
||||
if(fec_mode==0&& blob_encode.get_shard_len(fec_data_num,len)>fec_mtu) {about_to_fec=1; delayed_append=1;}//fec then add packet
|
||||
|
||||
if(fec_mode==0) assert(counter<fec_queue_len);//counter will never equal fec_pending_num,if that happens fec should already been done.
|
||||
if(fec_mode==1) assert(counter<fec_data_num);
|
||||
@@ -285,7 +282,10 @@ int fec_encode_manager_t::input(char *s,int len/*,int &is_first_packet*/)
|
||||
int best_data_num=fec_data_num;
|
||||
for(int i=1;i<actual_data_num;i++)
|
||||
{
|
||||
u32_t new_len=(blob_encode.get_shard_len(i,0)+header_overhead)*(i+fec_redundant_num);
|
||||
u32_t shard_len=blob_encode.get_shard_len(i,0);
|
||||
if(shard_len>(u32_t)fec_mtu) continue;
|
||||
|
||||
u32_t new_len=(shard_len+header_overhead)*(i+fec_redundant_num);
|
||||
if(new_len<best_len)
|
||||
{
|
||||
best_len=new_len;
|
||||
@@ -489,15 +489,12 @@ int fec_encode_manager_t::output(int &n,char ** &s_arr,int *&len)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
int fec_decode_manager_t::re_init()
|
||||
{
|
||||
for(int i=0;i<(int)fec_buff_num;i++)
|
||||
fec_data[i].used=0;
|
||||
ready_for_output=0;
|
||||
index=0;
|
||||
clear();
|
||||
return 0;
|
||||
}
|
||||
}*/
|
||||
|
||||
int fec_decode_manager_t::input(char *s,int len)
|
||||
{
|
||||
|
@@ -37,10 +37,16 @@ struct anti_replay_t
|
||||
unordered_set<u32_t> st;
|
||||
int index;
|
||||
anti_replay_t()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
int clear()
|
||||
{
|
||||
memset(replay_buffer,-1,sizeof(replay_buffer));
|
||||
st.clear();
|
||||
st.rehash(anti_replay_buff_size*3);
|
||||
index=0;
|
||||
return 0;
|
||||
}
|
||||
void set_invaild(u32_t seq)
|
||||
{
|
||||
@@ -140,6 +146,22 @@ public:
|
||||
fec_encode_manager_t();
|
||||
~fec_encode_manager_t();
|
||||
|
||||
int clear()
|
||||
{
|
||||
counter=0;
|
||||
blob_encode.clear();
|
||||
ready_for_output=0;
|
||||
|
||||
itimerspec zero_its;
|
||||
memset(&zero_its, 0, sizeof(zero_its));
|
||||
|
||||
timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &zero_its, 0);
|
||||
|
||||
seq=(u32_t)get_true_random_number(); //TODO temp solution for a bug.
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
my_time_t get_first_packet_time()
|
||||
{
|
||||
return first_packet_time_for_output;
|
||||
@@ -155,7 +177,7 @@ public:
|
||||
return fec_mode;
|
||||
}
|
||||
u64_t get_timer_fd64();
|
||||
int re_init(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type);
|
||||
int reset_fec_parameter(int data_num,int redundant_num,int mtu,int pending_num,int pending_time,int type);
|
||||
int input(char *s,int len/*,int &is_first_packet*/);
|
||||
int output(int &n,char ** &s_arr,int *&len);
|
||||
};
|
||||
@@ -200,7 +222,7 @@ public:
|
||||
fec_decode_manager_t()
|
||||
{
|
||||
fec_data=new fec_data_t[fec_buff_num+5];
|
||||
re_init();
|
||||
clear();
|
||||
}
|
||||
fec_decode_manager_t(const fec_decode_manager_t &b)
|
||||
{
|
||||
@@ -210,7 +232,21 @@ public:
|
||||
{
|
||||
delete fec_data;
|
||||
}
|
||||
int re_init();
|
||||
int clear()
|
||||
{
|
||||
anti_replay.clear();
|
||||
mp.clear();
|
||||
mp.rehash(fec_buff_num*3);
|
||||
|
||||
for(int i=0;i<(int)fec_buff_num;i++)
|
||||
fec_data[i].used=0;
|
||||
ready_for_output=0;
|
||||
index=0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int re_init();
|
||||
int input(char *s,int len);
|
||||
int output(int &n,char ** &s_arr,int* &len_arr);
|
||||
};
|
||||
|
BIN
images/cn/ping_compare_cn.PNG
Normal file
BIN
images/cn/ping_compare_cn.PNG
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 MiB |
4
lib/rs.c
4
lib/rs.c
@@ -51,6 +51,10 @@ void* get_code(int k,int n)
|
||||
if (table==0)
|
||||
{
|
||||
table=(void* (*)[256]) malloc(sizeof(void*)*256*256);
|
||||
if(!table)
|
||||
{
|
||||
return table;
|
||||
}
|
||||
memset(table,0,sizeof(void*)*256*256);
|
||||
}
|
||||
if(table[k][n]==0)
|
||||
|
5
makefile
5
makefile
@@ -8,12 +8,12 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
||||
#cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++
|
||||
FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers -ggdb
|
||||
|
||||
SOURCES=main.cpp log.cpp common.cpp lib/fec.c lib/rs.c packet.cpp delay_manager.cpp fd_manager.cpp connection.cpp fec_manager.cpp
|
||||
SOURCES=main.cpp log.cpp common.cpp lib/fec.c lib/rs.c packet.cpp delay_manager.cpp fd_manager.cpp connection.cpp fec_manager.cpp misc.cpp tunnel.cpp
|
||||
|
||||
NAME=speederv2
|
||||
TARGETS=amd64 arm mips24kc_be x86 mips24kc_le
|
||||
|
||||
TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/speederv2_\1/g'`
|
||||
TAR=${NAME}_binaries.tar.gz `echo ${TARGETS}|sed -r 's/([^ ]+)/speederv2_\1/g'` version.txt
|
||||
|
||||
all:git_version
|
||||
rm -f ${NAME}
|
||||
@@ -56,6 +56,7 @@ cross3:git_version
|
||||
${cc_cross} -o ${NAME}_cross -I. ${SOURCES} ${FLAGS} -lrt -static -O3
|
||||
|
||||
release: ${TARGETS}
|
||||
cp git_version.h version.txt
|
||||
tar -zcvf ${TAR}
|
||||
|
||||
clean:
|
||||
|
967
misc.cpp
Normal file
967
misc.cpp
Normal file
@@ -0,0 +1,967 @@
|
||||
/*
|
||||
* misc.cpp
|
||||
*
|
||||
* Created on: Oct 26, 2017
|
||||
* Author: root
|
||||
*/
|
||||
|
||||
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
char fifo_file[1000]="";
|
||||
|
||||
int mtu_warn=1350;
|
||||
|
||||
int disable_mtu_warn=1;
|
||||
int disable_fec=0;
|
||||
|
||||
int debug_force_flush_fec=0;
|
||||
|
||||
|
||||
int jitter_min=0*1000;
|
||||
int jitter_max=0*1000;
|
||||
|
||||
int output_interval_min=0*1000;
|
||||
int output_interval_max=0*1000;
|
||||
|
||||
int fix_latency=0;
|
||||
|
||||
u32_t local_ip_uint32,remote_ip_uint32=0;
|
||||
char local_ip[100], remote_ip[100];
|
||||
int local_port = -1, remote_port = -1;
|
||||
|
||||
conn_manager_t conn_manager;
|
||||
delay_manager_t delay_manager;
|
||||
fd_manager_t fd_manager;
|
||||
|
||||
int time_mono_test=0;
|
||||
|
||||
int delay_capacity=0;
|
||||
|
||||
|
||||
char sub_net[100]="10.22.22.0";
|
||||
u32_t sub_net_uint32=0;
|
||||
|
||||
char tun_dev[100]="";
|
||||
|
||||
|
||||
int keep_reconnect=0;
|
||||
|
||||
int tun_mtu=1500;
|
||||
|
||||
int mssfix=1;
|
||||
|
||||
|
||||
int from_normal_to_fec(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay)
|
||||
{
|
||||
|
||||
static my_time_t out_delay_buf[max_fec_packet_num+100]={0};
|
||||
//static int out_len_buf[max_fec_packet_num+100]={0};
|
||||
//static int counter=0;
|
||||
out_delay=out_delay_buf;
|
||||
//out_len=out_len_buf;
|
||||
inner_stat_t &inner_stat=conn_info.stat.normal_to_fec;
|
||||
if(disable_fec)
|
||||
{
|
||||
if(data==0)
|
||||
{
|
||||
out_n=0;
|
||||
return 0;
|
||||
}
|
||||
//assert(data!=0);
|
||||
inner_stat.input_packet_num++;
|
||||
inner_stat.input_packet_size+=len;
|
||||
inner_stat.output_packet_num++;
|
||||
inner_stat.output_packet_size+=len;
|
||||
|
||||
out_n=1;
|
||||
static char *data_static;
|
||||
data_static=data;
|
||||
static int len_static;
|
||||
len_static=len;
|
||||
out_arr=&data_static;
|
||||
out_len=&len_static;
|
||||
out_delay[0]=0;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if(data!=0)
|
||||
{
|
||||
inner_stat.input_packet_num++;
|
||||
inner_stat.input_packet_size+=len;
|
||||
}
|
||||
//counter++;
|
||||
|
||||
conn_info.fec_encode_manager.input(data,len);
|
||||
|
||||
//if(counter%5==0)
|
||||
//conn_info.fec_encode_manager.input(0,0);
|
||||
|
||||
//int n;
|
||||
//char **s_arr;
|
||||
//int s_len;
|
||||
|
||||
|
||||
conn_info.fec_encode_manager.output(out_n,out_arr,out_len);
|
||||
|
||||
if(out_n>0)
|
||||
{
|
||||
my_time_t common_latency=0;
|
||||
my_time_t first_packet_time=conn_info.fec_encode_manager.get_first_packet_time();
|
||||
|
||||
if(fix_latency==1&&conn_info.fec_encode_manager.get_type()==0)
|
||||
{
|
||||
my_time_t current_time=get_current_time_us();
|
||||
my_time_t tmp;
|
||||
assert(first_packet_time!=0);
|
||||
//mylog(log_info,"current_time=%llu first_packlet_time=%llu fec_pending_time=%llu\n",current_time,first_packet_time,(my_time_t)fec_pending_time);
|
||||
if((my_time_t)conn_info.fec_encode_manager.get_pending_time() >=(current_time - first_packet_time))
|
||||
{
|
||||
tmp=(my_time_t)conn_info.fec_encode_manager.get_pending_time()-(current_time - first_packet_time);
|
||||
//mylog(log_info,"tmp=%llu\n",tmp);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp=0;
|
||||
//mylog(log_info,"0\n");
|
||||
}
|
||||
common_latency+=tmp;
|
||||
}
|
||||
|
||||
common_latency+=random_between(jitter_min,jitter_max);
|
||||
|
||||
out_delay_buf[0]=common_latency;
|
||||
|
||||
for(int i=1;i<out_n;i++)
|
||||
{
|
||||
out_delay_buf[i]=out_delay_buf[i-1]+ (my_time_t)( random_between(output_interval_min,output_interval_max)/(out_n-1) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
mylog(log_trace,"from_normal_to_fec input_len=%d,output_n=%d\n",len,out_n);
|
||||
|
||||
if(out_n>0)
|
||||
{
|
||||
log_bare(log_trace,"seq= %u ",read_u32(out_arr[0]));
|
||||
}
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
inner_stat.output_packet_num++;
|
||||
inner_stat.output_packet_size+=out_len[i];
|
||||
|
||||
log_bare(log_trace,"%d ",out_len[i]);
|
||||
|
||||
}
|
||||
|
||||
log_bare(log_trace,"\n");
|
||||
//for(int i=0;i<n;i++)
|
||||
//{
|
||||
//delay_send(0,dest,s_arr[i],s_len);
|
||||
//}
|
||||
//delay_send(0,dest,data,len);
|
||||
//delay_send(1000*1000,dest,data,len);
|
||||
return 0;
|
||||
}
|
||||
int from_fec_to_normal(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay)
|
||||
{
|
||||
static my_time_t out_delay_buf[max_blob_packet_num+100]={0};
|
||||
out_delay=out_delay_buf;
|
||||
inner_stat_t &inner_stat=conn_info.stat.fec_to_normal;
|
||||
if(disable_fec)
|
||||
{
|
||||
assert(data!=0);
|
||||
inner_stat.input_packet_num++;
|
||||
inner_stat.input_packet_size+=len;
|
||||
inner_stat.output_packet_num++;
|
||||
inner_stat.output_packet_size+=len;
|
||||
|
||||
if(data==0)
|
||||
{
|
||||
out_n=0;
|
||||
return 0;
|
||||
}
|
||||
out_n=1;
|
||||
static char *data_static;
|
||||
data_static=data;
|
||||
static int len_static;
|
||||
len_static=len;
|
||||
out_arr=&data_static;
|
||||
out_len=&len_static;
|
||||
out_delay[0]=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if(data!=0)
|
||||
{
|
||||
inner_stat.input_packet_num++;
|
||||
inner_stat.input_packet_size+=len;
|
||||
}
|
||||
|
||||
conn_info.fec_decode_manager.input(data,len);
|
||||
|
||||
//int n;char ** s_arr;int* len_arr;
|
||||
conn_info.fec_decode_manager.output(out_n,out_arr,out_len);
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
out_delay_buf[i]=0;
|
||||
|
||||
inner_stat.output_packet_num++;
|
||||
inner_stat.output_packet_size+=out_len[i];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
mylog(log_trace,"from_fec_to_normal input_len=%d,output_n=%d,input_seq=%u\n",len,out_n,read_u32(data));
|
||||
|
||||
|
||||
// printf("<n:%d>",n);
|
||||
/*
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
delay_send(0,dest,s_arr[i],len_arr[i]);
|
||||
//s_arr[i][len_arr[i]]=0;
|
||||
//printf("<%s>\n",s_arr[i]);
|
||||
}*/
|
||||
//my_send(dest,data,len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int delay_send(my_time_t delay,const dest_t &dest,char *data,int len)
|
||||
{
|
||||
//int rand=random()%100;
|
||||
//mylog(log_info,"rand = %d\n",rand);
|
||||
|
||||
if (dest.cook&&random_drop != 0) {
|
||||
if (get_true_random_number() % 10000 < (u32_t) random_drop) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return delay_manager.add(delay,dest,data,len);;
|
||||
}
|
||||
|
||||
int print_parameter()
|
||||
{
|
||||
mylog(log_info,"jitter_min=%d jitter_max=%d output_interval_min=%d output_interval_max=%d fec_timeout=%d fec_data_num=%d fec_redundant_num=%d fec_mtu=%d fec_queue_len=%d fec_mode=%d\n",
|
||||
jitter_min/1000,jitter_max/1000,output_interval_min/1000,output_interval_max/1000,g_fec_timeout/1000,
|
||||
g_fec_data_num,g_fec_redundant_num,g_fec_mtu,g_fec_queue_len,g_fec_mode);
|
||||
return 0;
|
||||
}
|
||||
int handle_command(char *s)
|
||||
{
|
||||
int len=strlen(s);
|
||||
while(len>=1&&s[len-1]=='\n')
|
||||
s[len-1]=0;
|
||||
mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,s);
|
||||
int a=-1,b=-1;
|
||||
if(strncmp(s,"fec",strlen("fec"))==0)
|
||||
{
|
||||
mylog(log_info,"got command [fec]\n");
|
||||
sscanf(s,"fec %d:%d",&a,&b);
|
||||
if(a<1||b<0||a+b>254)
|
||||
{
|
||||
mylog(log_warn,"invaild value\n");
|
||||
return -1;
|
||||
}
|
||||
g_fec_data_num=a;
|
||||
g_fec_redundant_num=b;
|
||||
}
|
||||
else if(strncmp(s,"mtu",strlen("mtu"))==0)
|
||||
{
|
||||
mylog(log_info,"got command [mtu]\n");
|
||||
sscanf(s,"mtu %d",&a);
|
||||
if(a<100||a>2000)
|
||||
{
|
||||
mylog(log_warn,"invaild value\n");
|
||||
return -1;
|
||||
}
|
||||
g_fec_mtu=a;
|
||||
}
|
||||
else if(strncmp(s,"queue-len",strlen("queue-len"))==0)
|
||||
{
|
||||
mylog(log_info,"got command [queue-len]\n");
|
||||
sscanf(s,"queue-len %d",&a);
|
||||
if(a<1||a>10000)
|
||||
{
|
||||
mylog(log_warn,"invaild value\n");
|
||||
return -1;
|
||||
}
|
||||
g_fec_queue_len=a;
|
||||
}
|
||||
else if(strncmp(s,"mode",strlen("mode"))==0)
|
||||
{
|
||||
mylog(log_info,"got command [mode]\n");
|
||||
sscanf(s,"mode %d",&a);
|
||||
if(a!=0&&a!=1)
|
||||
{
|
||||
mylog(log_warn,"invaild value\n");
|
||||
return -1;
|
||||
}
|
||||
g_fec_mode=a;
|
||||
}
|
||||
else if(strncmp(s,"timeout",strlen("timeout"))==0)
|
||||
{
|
||||
mylog(log_info,"got command [timeout]\n");
|
||||
sscanf(s,"timeout %d",&a);
|
||||
if(a<0||a>1000)
|
||||
{
|
||||
mylog(log_warn,"invaild value\n");
|
||||
return -1;
|
||||
}
|
||||
g_fec_timeout=a*1000;
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_info,"unknown command\n");
|
||||
}
|
||||
print_parameter();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unit_test()
|
||||
{
|
||||
int i,j,k;
|
||||
void *code=fec_new(3,6);
|
||||
char arr[6][100]=
|
||||
{
|
||||
"aaa","bbb","ccc"
|
||||
,"ddd","eee","fff"
|
||||
};
|
||||
char *data[6];
|
||||
for(i=0;i<6;i++)
|
||||
{
|
||||
data[i]=arr[i];
|
||||
}
|
||||
rs_encode2(3,6,data,3);
|
||||
//printf("%d %d",(int)(unsigned char)arr[5][0],(int)('a'^'b'^'c'^'d'^'e'));
|
||||
|
||||
for(i=0;i<6;i++)
|
||||
{
|
||||
printf("<%s>",data[i]);
|
||||
}
|
||||
|
||||
data[0]=0;
|
||||
//data[1]=0;
|
||||
//data[5]=0;
|
||||
|
||||
int ret=rs_decode2(3,6,data,3);
|
||||
printf("ret:%d\n",ret);
|
||||
|
||||
for(i=0;i<6;i++)
|
||||
{
|
||||
printf("<%s>",data[i]);
|
||||
}
|
||||
fec_free(code);
|
||||
|
||||
|
||||
char arr2[6][100]=
|
||||
{
|
||||
"aaa11111","","ccc333333333"
|
||||
,"ddd444","eee5555","ff6666"
|
||||
};
|
||||
blob_encode_t blob_encode;
|
||||
for(int i=0;i<6;i++)
|
||||
blob_encode.input(arr2[i],strlen(arr2[i]));
|
||||
|
||||
char **output;
|
||||
int shard_len;
|
||||
blob_encode.output(7,output,shard_len);
|
||||
|
||||
|
||||
printf("<shard_len:%d>",shard_len);
|
||||
blob_decode_t blob_decode;
|
||||
for(int i=0;i<7;i++)
|
||||
{
|
||||
blob_decode.input(output[i],shard_len);
|
||||
}
|
||||
|
||||
char **decode_output;
|
||||
int * len_arr;
|
||||
int num;
|
||||
|
||||
|
||||
ret=blob_decode.output(num,decode_output,len_arr);
|
||||
|
||||
printf("<num:%d,ret:%d>\n",num,ret);
|
||||
for(int i=0;i<num;i++)
|
||||
{
|
||||
char buf[1000]={0};
|
||||
memcpy(buf,decode_output[i],len_arr[i]);
|
||||
printf("<%d:%s>",len_arr[i],buf);
|
||||
}
|
||||
printf("\n");
|
||||
static fec_encode_manager_t fec_encode_manager;
|
||||
static fec_decode_manager_t fec_decode_manager;
|
||||
|
||||
{
|
||||
|
||||
string a = "11111";
|
||||
string b = "22";
|
||||
string c = "33333333";
|
||||
|
||||
fec_encode_manager.input((char *) a.c_str(), a.length());
|
||||
fec_encode_manager.input((char *) b.c_str(), b.length());
|
||||
fec_encode_manager.input((char *) c.c_str(), c.length());
|
||||
fec_encode_manager.input(0, 0);
|
||||
|
||||
int n;
|
||||
char **s_arr;
|
||||
int *len;
|
||||
|
||||
|
||||
fec_encode_manager.output(n,s_arr,len);
|
||||
printf("<n:%d,len:%d>",n,len[0]);
|
||||
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
fec_decode_manager.input(s_arr[i],len[i]);
|
||||
}
|
||||
|
||||
{
|
||||
int n;char ** s_arr;int* len_arr;
|
||||
fec_decode_manager.output(n,s_arr,len_arr);
|
||||
printf("<n:%d>",n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
s_arr[i][len_arr[i]]=0;
|
||||
printf("<%s>\n",s_arr[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
string a = "aaaaaaa";
|
||||
string b = "bbbbbbbbbbbbb";
|
||||
string c = "ccc";
|
||||
|
||||
|
||||
fec_encode_manager.input((char *) a.c_str(), a.length());
|
||||
fec_encode_manager.input((char *) b.c_str(), b.length());
|
||||
fec_encode_manager.input((char *) c.c_str(), c.length());
|
||||
fec_encode_manager.input(0, 0);
|
||||
|
||||
int n;
|
||||
char **s_arr;
|
||||
int * len;
|
||||
|
||||
|
||||
fec_encode_manager.output(n,s_arr,len);
|
||||
printf("<n:%d,len:%d>",n,len[0]);
|
||||
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
if(i==1||i==3||i==5||i==0)
|
||||
fec_decode_manager.input(s_arr[i],len[i]);
|
||||
}
|
||||
|
||||
{
|
||||
int n;char ** s_arr;int* len_arr;
|
||||
fec_decode_manager.output(n,s_arr,len_arr);
|
||||
printf("<n:%d>",n);
|
||||
for(int i=0;i<n;i++)
|
||||
{
|
||||
s_arr[i][len_arr[i]]=0;
|
||||
printf("<%s>\n",s_arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0;i<10;i++)
|
||||
{
|
||||
string a = "aaaaaaaaaaaaaaaaaaaaaaa";
|
||||
string b = "bbbbbbbbbbbbb";
|
||||
string c = "cccccccccccccccccc";
|
||||
|
||||
|
||||
|
||||
printf("======\n");
|
||||
int n;
|
||||
char **s_arr;
|
||||
int * len;
|
||||
fec_decode_manager.output(n,s_arr,len);
|
||||
|
||||
fec_encode_manager.reset_fec_parameter(3,2,g_fec_mtu,g_fec_queue_len,g_fec_timeout,1);
|
||||
|
||||
fec_encode_manager.input((char *) a.c_str(), a.length());
|
||||
fec_encode_manager.output(n,s_arr,len);
|
||||
assert(n==1);
|
||||
|
||||
fec_decode_manager.input(s_arr[0],len[0]);
|
||||
|
||||
fec_decode_manager.output(n,s_arr,len);
|
||||
assert(n==1);
|
||||
printf("%s\n",s_arr[0]);
|
||||
|
||||
fec_encode_manager.input((char *) b.c_str(), b.length());
|
||||
fec_encode_manager.output(n,s_arr,len);
|
||||
assert(n==1);
|
||||
//fec_decode_manager.input(s_arr[0],len[0]);
|
||||
|
||||
|
||||
fec_encode_manager.input((char *) c.c_str(), c.length());
|
||||
fec_encode_manager.output(n,s_arr,len);
|
||||
|
||||
assert(n==3);
|
||||
|
||||
fec_decode_manager.input(s_arr[0],len[0]);
|
||||
//printf("n=%d\n",n);
|
||||
|
||||
|
||||
{
|
||||
int n;
|
||||
char **s_arr;
|
||||
int * len;
|
||||
fec_decode_manager.output(n,s_arr,len);
|
||||
assert(n==1);
|
||||
printf("%s\n",s_arr[0]);
|
||||
}
|
||||
|
||||
fec_decode_manager.input(s_arr[1],len[1]);
|
||||
|
||||
{
|
||||
int n;
|
||||
char **s_arr;
|
||||
int * len;
|
||||
fec_decode_manager.output(n,s_arr,len);
|
||||
assert(n==1);
|
||||
printf("n=%d\n",n);
|
||||
s_arr[0][len[0]]=0;
|
||||
printf("%s\n",s_arr[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void process_arg(int argc, char *argv[])
|
||||
{
|
||||
int is_client=0,is_server=0;
|
||||
int i, j, k;
|
||||
int opt;
|
||||
static struct option long_options[] =
|
||||
{
|
||||
{"log-level", required_argument, 0, 1},
|
||||
{"log-position", no_argument, 0, 1},
|
||||
{"disable-color", no_argument, 0, 1},
|
||||
{"disable-filter", no_argument, 0, 1},
|
||||
{"disable-fec", no_argument, 0, 1},
|
||||
{"disable-obscure", no_argument, 0, 1},
|
||||
{"disable-xor", no_argument, 0, 1},
|
||||
{"fix-latency", no_argument, 0, 1},
|
||||
{"sock-buf", required_argument, 0, 1},
|
||||
{"random-drop", required_argument, 0, 1},
|
||||
{"report", required_argument, 0, 1},
|
||||
{"delay-capacity", required_argument, 0, 1},
|
||||
{"mtu", required_argument, 0, 1},
|
||||
{"mode", required_argument, 0,1},
|
||||
{"timeout", required_argument, 0,1},
|
||||
{"decode-buf", required_argument, 0,1},
|
||||
{"queue-len", required_argument, 0,'q'},
|
||||
{"fec", required_argument, 0,'f'},
|
||||
{"jitter", required_argument, 0,'j'},
|
||||
{"fifo", required_argument, 0, 1},
|
||||
{"sub-net", required_argument, 0, 1},
|
||||
{"tun-dev", required_argument, 0, 1},
|
||||
{"tun-mtu", required_argument, 0, 1},
|
||||
{"disable-mssfix", no_argument, 0, 1},
|
||||
{"keep-reconnect", no_argument, 0, 1},
|
||||
{NULL, 0, 0, 0}
|
||||
};
|
||||
int option_index = 0;
|
||||
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
if(strcmp(argv[i],"--unit-test")==0)
|
||||
{
|
||||
unit_test();
|
||||
myexit(0);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
if(strcmp(argv[i],"--log-level")==0)
|
||||
{
|
||||
if(i<argc -1)
|
||||
{
|
||||
sscanf(argv[i+1],"%d",&log_level);
|
||||
if(0<=log_level&&log_level<log_end)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
log_bare(log_fatal,"invalid log_level\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(strcmp(argv[i],"--disable-color")==0)
|
||||
{
|
||||
enable_log_color=0;
|
||||
}
|
||||
}
|
||||
|
||||
mylog(log_info,"argc=%d ", argc);
|
||||
|
||||
for (i = 0; i < argc; i++) {
|
||||
log_bare(log_info, "%s ", argv[i]);
|
||||
}
|
||||
log_bare(log_info, "\n");
|
||||
|
||||
|
||||
int no_l = 1, no_r = 1;
|
||||
while ((opt = getopt_long(argc, argv, "l:r:hcsk:j:f:p:n:i:q:",long_options,&option_index)) != -1)
|
||||
{
|
||||
//string opt_key;
|
||||
//opt_key+=opt;
|
||||
switch (opt)
|
||||
{
|
||||
case 'k':
|
||||
sscanf(optarg,"%s\n",key_string);
|
||||
mylog(log_debug,"key=%s\n",key_string);
|
||||
if(strlen(key_string)==0)
|
||||
{
|
||||
mylog(log_fatal,"key len=0??\n");
|
||||
myexit(-1);
|
||||
}
|
||||
break;
|
||||
case 'j':
|
||||
if (strchr(optarg, ':') == 0)
|
||||
{
|
||||
int jitter;
|
||||
sscanf(optarg,"%d\n",&jitter);
|
||||
if(jitter<0 ||jitter>1000*10)
|
||||
{
|
||||
mylog(log_fatal,"jitter must be between 0 and 10,000(10 second)\n");
|
||||
myexit(-1);
|
||||
}
|
||||
jitter_min=0;
|
||||
jitter_max=jitter;
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(optarg,"%d:%d\n",&jitter_min,&jitter_max);
|
||||
if(jitter_min<0 ||jitter_max<0||jitter_min>jitter_max)
|
||||
{
|
||||
mylog(log_fatal," must satisfy 0<=jmin<=jmax\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
jitter_min*=1000;
|
||||
jitter_max*=1000;
|
||||
break;
|
||||
case 'i':
|
||||
if (strchr(optarg, ':') == 0)
|
||||
{
|
||||
int output_interval=-1;
|
||||
sscanf(optarg,"%d\n",&output_interval);
|
||||
if(output_interval<0||output_interval>1000*10)
|
||||
{
|
||||
mylog(log_fatal,"output_interval must be between 0 and 10,000(10 second)\n");
|
||||
myexit(-1);
|
||||
}
|
||||
output_interval_min=output_interval_max=output_interval;
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(optarg,"%d:%d\n",&output_interval_min,&output_interval_max);
|
||||
if(output_interval_min<0 ||output_interval_max<0||output_interval_min>output_interval_max)
|
||||
{
|
||||
mylog(log_fatal," must satisfy 0<=output_interval_min<=output_interval_max\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
output_interval_min*=1000;
|
||||
output_interval_max*=1000;
|
||||
break;
|
||||
case 'f':
|
||||
if (strchr(optarg, ':') == 0)
|
||||
{
|
||||
mylog(log_fatal,"invalid format for f");
|
||||
myexit(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(optarg,"%d:%d\n",&g_fec_data_num,&g_fec_redundant_num);
|
||||
if(g_fec_data_num<1 ||g_fec_redundant_num<0||g_fec_data_num+g_fec_redundant_num>254)
|
||||
{
|
||||
mylog(log_fatal,"fec_data_num<1 ||fec_redundant_num<0||fec_data_num+fec_redundant_num>254\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
sscanf(optarg,"%d",&g_fec_queue_len);
|
||||
if(g_fec_queue_len<1||g_fec_queue_len>10000)
|
||||
{
|
||||
|
||||
mylog(log_fatal,"fec_pending_num should be between 1 and 10000\n");
|
||||
myexit(-1);
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
is_client = 1;
|
||||
break;
|
||||
case 's':
|
||||
is_server = 1;
|
||||
break;
|
||||
case 'l':
|
||||
no_l = 0;
|
||||
if (strchr(optarg, ':') != 0)
|
||||
{
|
||||
sscanf(optarg, "%[^:]:%d", local_ip, &local_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal," -r ip:port\n");
|
||||
myexit(1);
|
||||
strcpy(local_ip, "127.0.0.1");
|
||||
sscanf(optarg, "%d", &local_port);
|
||||
}
|
||||
break;
|
||||
case 'r':
|
||||
no_r = 0;
|
||||
if (strchr(optarg, ':') != 0)
|
||||
{
|
||||
//printf("in :\n");
|
||||
//printf("%s\n",optarg);
|
||||
sscanf(optarg, "%[^:]:%d", remote_ip, &remote_port);
|
||||
//printf("%d\n",remote_port);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal," -r ip:port\n");
|
||||
myexit(1);
|
||||
strcpy(remote_ip, "127.0.0.1");
|
||||
sscanf(optarg, "%d", &remote_port);
|
||||
}
|
||||
break;
|
||||
case 'h':
|
||||
break;
|
||||
case 1:
|
||||
if(strcmp(long_options[option_index].name,"log-level")==0)
|
||||
{
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-filter")==0)
|
||||
{
|
||||
disable_replay_filter=1;
|
||||
//enable_log_color=0;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-color")==0)
|
||||
{
|
||||
//enable_log_color=0;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-fec")==0)
|
||||
{
|
||||
disable_fec=1;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-obscure")==0)
|
||||
{
|
||||
mylog(log_info,"obscure disabled\n");
|
||||
disable_obscure=1;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-xor")==0)
|
||||
{
|
||||
mylog(log_info,"xor disabled\n");
|
||||
disable_xor=1;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"fix-latency")==0)
|
||||
{
|
||||
mylog(log_info,"fix-latency enabled\n");
|
||||
fix_latency=1;
|
||||
}
|
||||
|
||||
else if(strcmp(long_options[option_index].name,"log-position")==0)
|
||||
{
|
||||
enable_log_position=1;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"random-drop")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&random_drop);
|
||||
if(random_drop<0||random_drop>10000)
|
||||
{
|
||||
mylog(log_fatal,"random_drop must be between 0 10000 \n");
|
||||
myexit(-1);
|
||||
}
|
||||
mylog(log_info,"random_drop=%d\n",random_drop);
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"delay-capacity")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&delay_capacity);
|
||||
|
||||
if(delay_capacity<0)
|
||||
{
|
||||
mylog(log_fatal,"delay_capacity must be >=0 \n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"report")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&report_interval);
|
||||
|
||||
if(report_interval<=0)
|
||||
{
|
||||
mylog(log_fatal,"report_interval must be >0 \n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"sock-buf")==0)
|
||||
{
|
||||
int tmp=-1;
|
||||
sscanf(optarg,"%d",&tmp);
|
||||
if(10<=tmp&&tmp<=10*1024)
|
||||
{
|
||||
socket_buf_size=tmp*1024;
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"sock-buf value must be between 1 and 10240 (kbyte) \n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"decode-buf")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&fec_buff_num);
|
||||
if(fec_buff_num<300 || fec_buff_num>20000)
|
||||
{
|
||||
mylog(log_fatal,"decode-buf value must be between 300 and 20000 (kbyte) \n");
|
||||
myexit(-1);
|
||||
}
|
||||
mylog(log_info,"decode-buf=%d\n",fec_buff_num);
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"mode")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&g_fec_mode);
|
||||
if(g_fec_mode!=0&&g_fec_mode!=1)
|
||||
{
|
||||
mylog(log_fatal,"mode should be 0 or 1\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"mtu")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&g_fec_mtu);
|
||||
if(g_fec_mtu<100||g_fec_mtu>2000)
|
||||
{
|
||||
mylog(log_fatal,"fec_mtu should be between 100 and 2000\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"timeout")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&g_fec_timeout);
|
||||
if(g_fec_timeout<0||g_fec_timeout>1000)
|
||||
{
|
||||
|
||||
mylog(log_fatal,"fec_pending_time should be between 0 and 1000(1s)\n");
|
||||
myexit(-1);
|
||||
}
|
||||
g_fec_timeout*=1000;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"fifo")==0)
|
||||
{
|
||||
sscanf(optarg,"%s",fifo_file);
|
||||
|
||||
mylog(log_info,"fifo_file =%s \n",fifo_file);
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"keep-reconnect")==0)
|
||||
{
|
||||
keep_reconnect=1;
|
||||
mylog(log_info,"keep_reconnect enabled\n");
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"sub-net")==0)
|
||||
{
|
||||
sscanf(optarg,"%s",sub_net);
|
||||
mylog(log_info,"sub_net %s\n",sub_net);
|
||||
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"tun-dev")==0)
|
||||
{
|
||||
sscanf(optarg,"%s",tun_dev);
|
||||
mylog(log_info,"tun_dev=%s\n",tun_dev);
|
||||
|
||||
mylog(log_info,"running at tun-dev mode\n");
|
||||
working_mode=tun_dev_mode;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"tun-mtu")==0)
|
||||
{
|
||||
sscanf(optarg,"%d",&tun_mtu);
|
||||
mylog(log_warn,"changed tun_mtu,tun_mtu=%d\n",tun_mtu);
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"disable-mssfix")==0)
|
||||
{
|
||||
mssfix=0;
|
||||
mylog(log_warn,"mssfix disabled\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"unknown option\n");
|
||||
myexit(-1);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
mylog(log_fatal,"unknown option <%x>", opt);
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_client == 0 && is_server == 0)
|
||||
{
|
||||
mylog(log_fatal,"-s -c hasnt been set\n");
|
||||
myexit(-1);
|
||||
}
|
||||
if (is_client == 1 && is_server == 1)
|
||||
{
|
||||
mylog(log_fatal,"-s -c cant be both set\n");
|
||||
myexit(-1);
|
||||
}
|
||||
if(is_client==1)
|
||||
{
|
||||
client_or_server=client_mode;
|
||||
}
|
||||
else
|
||||
{
|
||||
client_or_server=server_mode;
|
||||
}
|
||||
|
||||
|
||||
if(working_mode==tunnel_mode)
|
||||
{
|
||||
if (no_l)
|
||||
mylog(log_fatal,"error: -l not found\n");
|
||||
if (no_r)
|
||||
mylog(log_fatal,"error: -r not found\n");
|
||||
if (no_l || no_r)
|
||||
myexit(-1);
|
||||
}
|
||||
else if(working_mode==tun_dev_mode)
|
||||
{
|
||||
if(client_or_server==client_mode&&no_r)
|
||||
{
|
||||
mylog(log_fatal,"error: -r not found\n");
|
||||
myexit(-1);
|
||||
}
|
||||
else if(client_or_server==server_mode&&no_l)
|
||||
{
|
||||
mylog(log_fatal,"error: -l not found\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
print_parameter();
|
||||
|
||||
}
|
||||
|
73
misc.h
Normal file
73
misc.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* misc.h
|
||||
*
|
||||
* Created on: Oct 26, 2017
|
||||
* Author: root
|
||||
*/
|
||||
|
||||
#ifndef MISC_H_
|
||||
#define MISC_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "connection.h"
|
||||
#include "fd_manager.h"
|
||||
#include "delay_manager.h"
|
||||
#include "fec_manager.h"
|
||||
|
||||
|
||||
|
||||
extern char fifo_file[1000];
|
||||
|
||||
extern int mtu_warn;
|
||||
|
||||
extern int disable_mtu_warn;
|
||||
extern int disable_fec;
|
||||
|
||||
extern int debug_force_flush_fec;
|
||||
|
||||
|
||||
extern int jitter_min;
|
||||
extern int jitter_max;
|
||||
|
||||
extern int output_interval_min;
|
||||
extern int output_interval_max;
|
||||
|
||||
extern int fix_latency;
|
||||
|
||||
extern u32_t local_ip_uint32,remote_ip_uint32;
|
||||
extern char local_ip[100], remote_ip[100];
|
||||
extern int local_port, remote_port;
|
||||
|
||||
extern conn_manager_t conn_manager;
|
||||
extern delay_manager_t delay_manager;
|
||||
extern fd_manager_t fd_manager;
|
||||
|
||||
extern int time_mono_test;
|
||||
|
||||
extern int delay_capacity;
|
||||
|
||||
extern int keep_reconnect;
|
||||
|
||||
extern int tun_mtu;
|
||||
|
||||
extern int mssfix;
|
||||
|
||||
|
||||
int from_normal_to_fec(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay);
|
||||
int from_fec_to_normal(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay);
|
||||
|
||||
int delay_send(my_time_t delay,const dest_t &dest,char *data,int len);
|
||||
int print_parameter();
|
||||
int handle_command(char *s);
|
||||
|
||||
int unit_test();
|
||||
|
||||
//void print_help();
|
||||
|
||||
void process_arg(int argc, char *argv[]);
|
||||
|
||||
extern char sub_net[100];
|
||||
extern u32_t sub_net_uint32;
|
||||
extern char tun_dev[100];
|
||||
|
||||
#endif /* MISC_H_ */
|
62
packet.cpp
62
packet.cpp
@@ -27,7 +27,7 @@ int random_drop=0;
|
||||
|
||||
char key_string[1000]= "";
|
||||
|
||||
int local_listen_fd=-1;
|
||||
//int local_listen_fd=-1;
|
||||
|
||||
|
||||
void encrypt_0(char * input,int &len,char *key)
|
||||
@@ -162,10 +162,11 @@ int sendto_fd_ip_port (int fd,u32_t ip,int port,char * buf, int len,int flags)
|
||||
(struct sockaddr *) &tmp_sockaddr,
|
||||
sizeof(tmp_sockaddr));
|
||||
}
|
||||
/*
|
||||
int sendto_ip_port (u32_t ip,int port,char * buf, int len,int flags)
|
||||
{
|
||||
return sendto_fd_ip_port(local_listen_fd,ip,port,buf,len,flags);
|
||||
}
|
||||
}*/
|
||||
|
||||
int send_fd (int fd,char * buf, int len,int flags)
|
||||
{
|
||||
@@ -176,23 +177,31 @@ int my_send(const dest_t &dest,char *data,int len)
|
||||
{
|
||||
if(dest.cook)
|
||||
{
|
||||
put_crc32(data,len);
|
||||
if(!disable_obscure)do_obscure(data,len);
|
||||
if(!disable_xor)encrypt_0(data,len,key_string);
|
||||
do_cook(data,len);
|
||||
}
|
||||
switch(dest.type)
|
||||
{
|
||||
case type_ip_port:
|
||||
case type_fd_ip_port:
|
||||
{
|
||||
return sendto_ip_port(dest.inner.ip_port.ip,dest.inner.ip_port.port,data,len,0);
|
||||
return sendto_fd_ip_port(dest.inner.fd,dest.inner.fd_ip_port.ip_port.ip,dest.inner.fd_ip_port.ip_port.port,data,len,0);
|
||||
break;
|
||||
}
|
||||
case type_ip_port_conv:
|
||||
case type_fd64_ip_port:
|
||||
{
|
||||
char *new_data;
|
||||
int new_len;
|
||||
put_conv(dest.conv,data,len,new_data,new_len);
|
||||
return sendto_ip_port(dest.inner.ip_port.ip,dest.inner.ip_port.port,new_data,new_len,0);
|
||||
if(!fd_manager.exist(dest.inner.fd64)) return -1;
|
||||
int fd=fd_manager.to_fd(dest.inner.fd64);
|
||||
|
||||
return sendto_fd_ip_port(fd,dest.inner.fd64_ip_port.ip_port.ip,dest.inner.fd64_ip_port.ip_port.port,data,len,0);
|
||||
break;
|
||||
}
|
||||
case type_fd:
|
||||
{
|
||||
return send_fd(dest.inner.fd,data,len,0);
|
||||
break;
|
||||
}
|
||||
case type_write_fd:
|
||||
{
|
||||
return write(dest.inner.fd,data,len);
|
||||
break;
|
||||
}
|
||||
case type_fd64:
|
||||
@@ -200,9 +209,25 @@ int my_send(const dest_t &dest,char *data,int len)
|
||||
|
||||
if(!fd_manager.exist(dest.inner.fd64)) return -1;
|
||||
int fd=fd_manager.to_fd(dest.inner.fd64);
|
||||
|
||||
return send_fd(fd,data,len,0);
|
||||
break;
|
||||
}
|
||||
/*
|
||||
case type_fd64_ip_port_conv:
|
||||
{
|
||||
if(!fd_manager.exist(dest.inner.fd64)) return -1;
|
||||
int fd=fd_manager.to_fd(dest.inner.fd64);
|
||||
|
||||
char *new_data;
|
||||
int new_len;
|
||||
|
||||
put_conv(dest.conv,data,len,new_data,new_len);
|
||||
return sendto_fd_ip_port(fd,dest.inner.fd64_ip_port.ip_port.ip,dest.inner.fd64_ip_port.ip_port.port,new_data,new_len,0);
|
||||
break;
|
||||
}*/
|
||||
|
||||
/*
|
||||
case type_fd64_conv:
|
||||
{
|
||||
char *new_data;
|
||||
@@ -212,7 +237,7 @@ int my_send(const dest_t &dest,char *data,int len)
|
||||
if(!fd_manager.exist(dest.inner.fd64)) return -1;
|
||||
int fd=fd_manager.to_fd(dest.inner.fd64);
|
||||
return send_fd(fd,new_data,new_len,0);
|
||||
}
|
||||
}*/
|
||||
/*
|
||||
case type_fd:
|
||||
{
|
||||
@@ -300,6 +325,14 @@ int put_crc32(char * s,int &len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int do_cook(char * data,int &len)
|
||||
{
|
||||
put_crc32(data,len);
|
||||
if(!disable_obscure)do_obscure(data,len);
|
||||
if(!disable_xor)encrypt_0(data,len,key_string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int de_cook(char * s,int &len)
|
||||
{
|
||||
if(!disable_xor)decrypt_0(s,len,key_string);
|
||||
@@ -362,3 +395,6 @@ int get_conv(u32_t &conv,const char *input,int len_in,char *&output,int &len_out
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
2
packet.h
2
packet.h
@@ -21,7 +21,6 @@ extern u64_t dup_packet_recv_count;
|
||||
extern char key_string[1000];
|
||||
extern int disable_replay_filter;
|
||||
extern int random_drop;
|
||||
extern int local_listen_fd;
|
||||
extern int disable_obscure;
|
||||
extern int disable_xor;
|
||||
|
||||
@@ -43,5 +42,6 @@ int put_conv(u32_t conv,const char * input,int len_in,char *&output,int &len_out
|
||||
int get_conv(u32_t &conv,const char *input,int len_in,char *&output,int &len_out );
|
||||
int put_crc32(char * s,int &len);
|
||||
int rm_crc32(char * s,int &len);
|
||||
int do_cook(char * data,int &len);
|
||||
int de_cook(char * s,int &len);
|
||||
#endif /* PACKET_H_ */
|
||||
|
705
tunnel.cpp
Normal file
705
tunnel.cpp
Normal file
@@ -0,0 +1,705 @@
|
||||
/*
|
||||
* tunnel.cpp
|
||||
*
|
||||
* Created on: Oct 26, 2017
|
||||
* Author: root
|
||||
*/
|
||||
|
||||
#include "tunnel.h"
|
||||
|
||||
|
||||
int tunnel_client_event_loop()
|
||||
{
|
||||
//char buf[buf_len];
|
||||
int i, j, k;int ret;
|
||||
int yes = 1;
|
||||
int epoll_fd;
|
||||
int remote_fd;
|
||||
fd64_t remote_fd64;
|
||||
|
||||
conn_info_t *conn_info_p=new conn_info_t;
|
||||
conn_info_t &conn_info=*conn_info_p; //huge size of conn_info,do not allocate on stack
|
||||
//conn_info.conv_manager.reserve();
|
||||
//conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
|
||||
|
||||
|
||||
int local_listen_fd;
|
||||
//fd64_t local_listen_fd64;
|
||||
new_listen_socket(local_listen_fd,local_ip_uint32,local_port);
|
||||
//local_listen_fd64=fd_manager.create(local_listen_fd);
|
||||
|
||||
epoll_fd = epoll_create1(0);
|
||||
assert(epoll_fd>0);
|
||||
|
||||
const int max_events = 4096;
|
||||
struct epoll_event ev, events[max_events];
|
||||
if (epoll_fd < 0) {
|
||||
mylog(log_fatal,"epoll return %d\n", epoll_fd);
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = local_listen_fd;
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, local_listen_fd, &ev);
|
||||
if (ret!=0) {
|
||||
mylog(log_fatal,"add udp_listen_fd error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
assert(new_connected_socket(remote_fd,remote_ip_uint32,remote_port)==0);
|
||||
remote_fd64=fd_manager.create(remote_fd);
|
||||
|
||||
mylog(log_debug,"remote_fd64=%llu\n",remote_fd64);
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = remote_fd64;
|
||||
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, remote_fd, &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add raw_fd error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = delay_manager.get_timer_fd();
|
||||
|
||||
mylog(log_debug,"delay_manager.get_timer_fd()=%d\n",delay_manager.get_timer_fd());
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add delay_manager.get_timer_fd() error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
u64_t tmp_fd64=conn_info.fec_encode_manager.get_timer_fd64();
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = tmp_fd64;
|
||||
|
||||
mylog(log_debug,"conn_info.fec_encode_manager.get_timer_fd64()=%llu\n",conn_info.fec_encode_manager.get_timer_fd64());
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(tmp_fd64), &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add fec_encode_manager.get_timer_fd64() error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
//my_timer_t timer;
|
||||
conn_info.timer.add_fd_to_epoll(epoll_fd);
|
||||
conn_info.timer.set_timer_repeat_us(timer_interval*1000);
|
||||
|
||||
mylog(log_debug,"conn_info.timer.get_timer_fd()=%d\n",conn_info.timer.get_timer_fd());
|
||||
|
||||
|
||||
|
||||
int fifo_fd=-1;
|
||||
|
||||
if(fifo_file[0]!=0)
|
||||
{
|
||||
fifo_fd=create_fifo(fifo_file);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = fifo_fd;
|
||||
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno));
|
||||
myexit(-1);
|
||||
}
|
||||
mylog(log_info,"fifo_file=%s\n",fifo_file);
|
||||
}
|
||||
|
||||
while(1)////////////////////////
|
||||
{
|
||||
if(about_to_exit) myexit(0);
|
||||
|
||||
int nfds = epoll_wait(epoll_fd, events, max_events, 180 * 1000);
|
||||
if (nfds < 0) { //allow zero
|
||||
if(errno==EINTR )
|
||||
{
|
||||
mylog(log_info,"epoll interrupted by signal\n");
|
||||
myexit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"epoll_wait return %d\n", nfds);
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
int idx;
|
||||
for (idx = 0; idx < nfds; ++idx) {
|
||||
if(events[idx].data.u64==(u64_t)conn_info.timer.get_timer_fd())
|
||||
{
|
||||
uint64_t value;
|
||||
read(conn_info.timer.get_timer_fd(), &value, 8);
|
||||
conn_info.conv_manager.clear_inactive();
|
||||
mylog(log_trace,"events[idx].data.u64==(u64_t)conn_info.timer.get_timer_fd()\n");
|
||||
|
||||
conn_info.stat.report_as_client();
|
||||
|
||||
if(debug_force_flush_fec)
|
||||
{
|
||||
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
|
||||
dest_t dest;
|
||||
dest.type=type_fd64;
|
||||
dest.inner.fd64=remote_fd64;
|
||||
dest.cook=1;
|
||||
from_normal_to_fec(conn_info,0,0,out_n,out_arr,out_len,out_delay);
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
delay_send(out_delay[i],dest,out_arr[i],out_len[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (events[idx].data.u64 == (u64_t)fifo_fd)
|
||||
{
|
||||
char buf[buf_len];
|
||||
int len=read (fifo_fd, buf, sizeof (buf));
|
||||
if(len<0)
|
||||
{
|
||||
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
|
||||
continue;
|
||||
}
|
||||
buf[len]=0;
|
||||
handle_command(buf);
|
||||
}
|
||||
else if (events[idx].data.u64 == (u64_t)local_listen_fd||events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64())
|
||||
{
|
||||
char data[buf_len];
|
||||
int data_len;
|
||||
ip_port_t ip_port;
|
||||
u32_t conv;
|
||||
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
|
||||
dest_t dest;
|
||||
dest.type=type_fd64;
|
||||
dest.inner.fd64=remote_fd64;
|
||||
dest.cook=1;
|
||||
|
||||
if(events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64())
|
||||
{
|
||||
fd64_t fd64=events[idx].data.u64;
|
||||
mylog(log_trace,"events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()\n");
|
||||
|
||||
//mylog(log_info,"timer!!!\n");
|
||||
uint64_t value;
|
||||
if(!fd_manager.exist(fd64)) //fd64 has been closed
|
||||
{
|
||||
mylog(log_trace,"!fd_manager.exist(fd64)");
|
||||
continue;
|
||||
}
|
||||
if((ret=read(fd_manager.to_fd(fd64), &value, 8))!=8)
|
||||
{
|
||||
mylog(log_trace,"(ret=read(fd_manager.to_fd(fd64), &value, 8))!=8,ret=%d\n",ret);
|
||||
continue;
|
||||
}
|
||||
if(value==0)
|
||||
{
|
||||
mylog(log_debug,"value==0\n");
|
||||
continue;
|
||||
}
|
||||
assert(value==1);
|
||||
from_normal_to_fec(conn_info,0,0,out_n,out_arr,out_len,out_delay);
|
||||
//from_normal_to_fec(conn_info,0,0,out_n,out_arr,out_len,out_delay);
|
||||
}
|
||||
else//events[idx].data.u64 == (u64_t)local_listen_fd
|
||||
{
|
||||
mylog(log_trace,"events[idx].data.u64 == (u64_t)local_listen_fd\n");
|
||||
struct sockaddr_in udp_new_addr_in={0};
|
||||
socklen_t udp_new_addr_len = sizeof(sockaddr_in);
|
||||
if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0,
|
||||
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
|
||||
mylog(log_error,"recv_from error,this shouldnt happen,err=%s,but we can try to continue\n",strerror(errno));
|
||||
continue;
|
||||
//mylog(log_error,"recv_from error,this shouldnt happen at client\n");
|
||||
//myexit(1);
|
||||
};
|
||||
|
||||
if(!disable_mtu_warn&&data_len>=mtu_warn)
|
||||
{
|
||||
mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
|
||||
}
|
||||
mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
|
||||
ntohs(udp_new_addr_in.sin_port),data_len);
|
||||
|
||||
ip_port.ip=udp_new_addr_in.sin_addr.s_addr;
|
||||
ip_port.port=ntohs(udp_new_addr_in.sin_port);
|
||||
|
||||
u64_t u64=ip_port.to_u64();
|
||||
|
||||
if(!conn_info.conv_manager.is_u64_used(u64))
|
||||
{
|
||||
if(conn_info.conv_manager.get_size() >=max_conv_num)
|
||||
{
|
||||
mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n");
|
||||
continue;
|
||||
}
|
||||
conv=conn_info.conv_manager.get_new_conv();
|
||||
conn_info.conv_manager.insert_conv(conv,u64);
|
||||
mylog(log_info,"new packet from %s:%d,conv_id=%x\n",inet_ntoa(udp_new_addr_in.sin_addr),ntohs(udp_new_addr_in.sin_port),conv);
|
||||
}
|
||||
else
|
||||
{
|
||||
conv=conn_info.conv_manager.find_conv_by_u64(u64);
|
||||
mylog(log_trace,"conv=%d\n",conv);
|
||||
}
|
||||
conn_info.conv_manager.update_active_time(conv);
|
||||
char * new_data;
|
||||
int new_len;
|
||||
put_conv(conv,data,data_len,new_data,new_len);
|
||||
|
||||
|
||||
mylog(log_trace,"data_len=%d new_len=%d\n",data_len,new_len);
|
||||
//dest.conv=conv;
|
||||
from_normal_to_fec(conn_info,new_data,new_len,out_n,out_arr,out_len,out_delay);
|
||||
|
||||
}
|
||||
mylog(log_trace,"out_n=%d\n",out_n);
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
delay_send(out_delay[i],dest,out_arr[i],out_len[i]);
|
||||
}
|
||||
//my_send(dest,data,data_len);
|
||||
}
|
||||
else if (events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()) {
|
||||
uint64_t value;
|
||||
read(delay_manager.get_timer_fd(), &value, 8);
|
||||
mylog(log_trace,"events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()\n");
|
||||
//printf("<timerfd_triggered, %d>",delay_mp.size());
|
||||
//fflush(stdout);
|
||||
}
|
||||
else if(events[idx].data.u64>u32_t(-1) )
|
||||
{
|
||||
char data[buf_len];
|
||||
if(!fd_manager.exist(events[idx].data.u64)) //fd64 has been closed
|
||||
{
|
||||
mylog(log_trace,"!fd_manager.exist(events[idx].data.u64)");
|
||||
continue;
|
||||
}
|
||||
assert(events[idx].data.u64==remote_fd64);
|
||||
int fd=fd_manager.to_fd(remote_fd64);
|
||||
int data_len =recv(fd,data,max_data_len,0);
|
||||
mylog(log_trace, "received data from udp fd %d, len=%d\n", remote_fd,data_len);
|
||||
if(data_len<0)
|
||||
{
|
||||
if(errno==ECONNREFUSED)
|
||||
{
|
||||
//conn_manager.clear_list.push_back(udp_fd);
|
||||
mylog(log_debug, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,remote_fd,strerror(errno));
|
||||
}
|
||||
|
||||
mylog(log_warn, "recv failed %d ,udp_fd%d,errno:%s\n", data_len,remote_fd,strerror(errno));
|
||||
continue;
|
||||
}
|
||||
if(!disable_mtu_warn&&data_len>mtu_warn)
|
||||
{
|
||||
mylog(log_warn,"huge packet,data len=%d (>%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
|
||||
}
|
||||
|
||||
if(de_cook(data,data_len)!=0)
|
||||
{
|
||||
mylog(log_debug,"de_cook error");
|
||||
continue;
|
||||
}
|
||||
|
||||
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
|
||||
from_fec_to_normal(conn_info,data,data_len,out_n,out_arr,out_len,out_delay);
|
||||
|
||||
mylog(log_trace,"out_n=%d\n",out_n);
|
||||
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
u32_t conv;
|
||||
char *new_data;
|
||||
int new_len;
|
||||
if(get_conv(conv,out_arr[i],out_len[i],new_data,new_len)!=0)
|
||||
{
|
||||
mylog(log_debug,"get_conv(conv,out_arr[i],out_len[i],new_data,new_len)!=0");
|
||||
continue;
|
||||
}
|
||||
if(!conn_info.conv_manager.is_conv_used(conv))
|
||||
{
|
||||
mylog(log_trace,"!conn_info.conv_manager.is_conv_used(conv)");
|
||||
continue;
|
||||
}
|
||||
|
||||
conn_info.conv_manager.update_active_time(conv);
|
||||
|
||||
u64_t u64=conn_info.conv_manager.find_u64_by_conv(conv);
|
||||
dest_t dest;
|
||||
dest.inner.fd_ip_port.fd=local_listen_fd;
|
||||
dest.inner.fd_ip_port.ip_port.from_u64(u64);
|
||||
dest.type=type_fd_ip_port;
|
||||
//dest.conv=conv;
|
||||
|
||||
delay_send(out_delay[i],dest,new_data,new_len);
|
||||
}
|
||||
//mylog(log_trace,"[%s] send packet\n",dest.inner.ip_port.to_s());
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"unknown fd,this should never happen\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
delay_manager.check();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tunnel_server_event_loop()
|
||||
{
|
||||
//char buf[buf_len];
|
||||
int i, j, k;int ret;
|
||||
int yes = 1;
|
||||
int epoll_fd;
|
||||
int remote_fd;
|
||||
|
||||
// conn_info_t conn_info;
|
||||
int local_listen_fd;
|
||||
// fd64_t local_listen_fd64;
|
||||
new_listen_socket(local_listen_fd,local_ip_uint32,local_port);
|
||||
// local_listen_fd64=fd_manager.create(local_listen_fd);
|
||||
|
||||
epoll_fd = epoll_create1(0);
|
||||
assert(epoll_fd>0);
|
||||
|
||||
const int max_events = 4096;
|
||||
struct epoll_event ev, events[max_events];
|
||||
if (epoll_fd < 0) {
|
||||
mylog(log_fatal,"epoll return %d\n", epoll_fd);
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = local_listen_fd;
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, local_listen_fd, &ev);
|
||||
if (ret!=0) {
|
||||
mylog(log_fatal,"add udp_listen_fd error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = delay_manager.get_timer_fd();
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add delay_manager.get_timer_fd() error\n");
|
||||
myexit(-1);
|
||||
}
|
||||
|
||||
mylog(log_debug," delay_manager.get_timer_fd() =%d\n", delay_manager.get_timer_fd());
|
||||
|
||||
mylog(log_info,"now listening at %s:%d\n",my_ntoa(local_ip_uint32),local_port);
|
||||
|
||||
my_timer_t timer;
|
||||
timer.add_fd_to_epoll(epoll_fd);
|
||||
timer.set_timer_repeat_us(timer_interval*1000);
|
||||
|
||||
mylog(log_debug," timer.get_timer_fd() =%d\n",timer.get_timer_fd());
|
||||
|
||||
int fifo_fd=-1;
|
||||
|
||||
if(fifo_file[0]!=0)
|
||||
{
|
||||
fifo_fd=create_fifo(fifo_file);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = fifo_fd;
|
||||
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev);
|
||||
if (ret!= 0) {
|
||||
mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno));
|
||||
myexit(-1);
|
||||
}
|
||||
mylog(log_info,"fifo_file=%s\n",fifo_file);
|
||||
}
|
||||
|
||||
while(1)////////////////////////
|
||||
{
|
||||
|
||||
if(about_to_exit) myexit(0);
|
||||
|
||||
int nfds = epoll_wait(epoll_fd, events, max_events, 180 * 1000);
|
||||
if (nfds < 0) { //allow zero
|
||||
if(errno==EINTR )
|
||||
{
|
||||
mylog(log_info,"epoll interrupted by signal\n");
|
||||
myexit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"epoll_wait return %d\n", nfds);
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
int idx;
|
||||
for (idx = 0; idx < nfds; ++idx)
|
||||
{
|
||||
/*
|
||||
if ((events[idx].data.u64 ) == (u64_t)timer_fd)
|
||||
{
|
||||
conn_manager.clear_inactive();
|
||||
u64_t dummy;
|
||||
read(timer_fd, &dummy, 8);
|
||||
//current_time_rough=get_current_time();
|
||||
}
|
||||
else */
|
||||
if(events[idx].data.u64==(u64_t)timer.get_timer_fd())
|
||||
{
|
||||
uint64_t value;
|
||||
read(timer.get_timer_fd(), &value, 8);
|
||||
conn_manager.clear_inactive();
|
||||
mylog(log_trace,"events[idx].data.u64==(u64_t)timer.get_timer_fd()\n");
|
||||
//conn_info.conv_manager.clear_inactive();
|
||||
}
|
||||
|
||||
else if (events[idx].data.u64 == (u64_t)fifo_fd)
|
||||
{
|
||||
char buf[buf_len];
|
||||
int len=read (fifo_fd, buf, sizeof (buf));
|
||||
if(len<0)
|
||||
{
|
||||
mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno));
|
||||
continue;
|
||||
}
|
||||
buf[len]=0;
|
||||
handle_command(buf);
|
||||
}
|
||||
|
||||
else if (events[idx].data.u64 == (u64_t)local_listen_fd)
|
||||
{
|
||||
|
||||
mylog(log_trace,"events[idx].data.u64 == (u64_t)local_listen_fd\n");
|
||||
//int recv_len;
|
||||
char data[buf_len];
|
||||
int data_len;
|
||||
struct sockaddr_in udp_new_addr_in={0};
|
||||
socklen_t udp_new_addr_len = sizeof(sockaddr_in);
|
||||
if ((data_len = recvfrom(local_listen_fd, data, max_data_len, 0,
|
||||
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
|
||||
mylog(log_error,"recv_from error,this shouldnt happen,err=%s,but we can try to continue\n",strerror(errno));
|
||||
continue;
|
||||
//myexit(1);
|
||||
};
|
||||
mylog(log_trace,"Received packet from %s:%d,len: %d\n", inet_ntoa(udp_new_addr_in.sin_addr),
|
||||
ntohs(udp_new_addr_in.sin_port),data_len);
|
||||
|
||||
if(!disable_mtu_warn&&data_len>=mtu_warn)///////////////////////delete this for type 0 in furture
|
||||
{
|
||||
mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
|
||||
}
|
||||
|
||||
|
||||
if(de_cook(data,data_len)!=0)
|
||||
{
|
||||
mylog(log_debug,"de_cook error");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
ip_port_t ip_port;
|
||||
ip_port.ip=udp_new_addr_in.sin_addr.s_addr;
|
||||
ip_port.port=ntohs(udp_new_addr_in.sin_port);
|
||||
mylog(log_trace,"ip_port= %s\n",ip_port.to_s());
|
||||
if(!conn_manager.exist(ip_port))
|
||||
{
|
||||
if(conn_manager.mp.size() >=max_conn_num)
|
||||
{
|
||||
mylog(log_warn,"new connection %s ignored bc max_conn_num exceed\n",ip_port.to_s());
|
||||
continue;
|
||||
}
|
||||
|
||||
conn_manager.insert(ip_port);
|
||||
conn_info_t &conn_info=conn_manager.find(ip_port);
|
||||
//conn_info.fec_encode_manager.re_init(fec_data_num,fec_redundant_num,fec_mtu,fec_pending_num,fec_pending_time,fec_type);
|
||||
//conn_info.conv_manager.reserve(); //already reserved in constructor
|
||||
|
||||
u64_t fec_fd64=conn_info.fec_encode_manager.get_timer_fd64();
|
||||
mylog(log_debug,"fec_fd64=%llu\n",fec_fd64);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = fec_fd64;
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(fec_fd64), &ev);
|
||||
|
||||
fd_manager.get_info(fec_fd64).ip_port=ip_port;
|
||||
|
||||
conn_info.timer.add_fd64_to_epoll(epoll_fd);
|
||||
conn_info.timer.set_timer_repeat_us(timer_interval*1000);
|
||||
|
||||
mylog(log_debug,"conn_info.timer.get_timer_fd64()=%llu\n",conn_info.timer.get_timer_fd64());
|
||||
|
||||
u64_t timer_fd64=conn_info.timer.get_timer_fd64();
|
||||
fd_manager.get_info(timer_fd64).ip_port=ip_port;
|
||||
|
||||
mylog(log_info,"new connection from %s\n",ip_port.to_s());
|
||||
|
||||
}
|
||||
conn_info_t &conn_info=conn_manager.find(ip_port);
|
||||
|
||||
conn_info.update_active_time();
|
||||
int out_n;char **out_arr;int *out_len;my_time_t *out_delay;
|
||||
from_fec_to_normal(conn_info,data,data_len,out_n,out_arr,out_len,out_delay);
|
||||
|
||||
mylog(log_trace,"out_n= %d\n",out_n);
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
u32_t conv;
|
||||
char *new_data;
|
||||
int new_len;
|
||||
if(get_conv(conv,out_arr[i],out_len[i],new_data,new_len)!=0)
|
||||
{
|
||||
mylog(log_debug,"get_conv failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
id_t tmp_conv_id;
|
||||
memcpy(&tmp_conv_id,&data_[0],sizeof(tmp_conv_id));
|
||||
tmp_conv_id=ntohl(tmp_conv_id);*/
|
||||
|
||||
if (!conn_info.conv_manager.is_conv_used(conv))
|
||||
{
|
||||
if(conn_info.conv_manager.get_size() >=max_conv_num)
|
||||
{
|
||||
mylog(log_warn,"ignored new udp connect bc max_conv_num exceed\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
int new_udp_fd;
|
||||
ret=new_connected_socket(new_udp_fd,remote_ip_uint32,remote_port);
|
||||
|
||||
if (ret != 0) {
|
||||
mylog(log_warn, "[%s]new_connected_socket failed\n",ip_port.to_s());
|
||||
continue;
|
||||
}
|
||||
|
||||
fd64_t fd64 = fd_manager.create(new_udp_fd);
|
||||
ev.events = EPOLLIN;
|
||||
ev.data.u64 = fd64;
|
||||
ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_udp_fd, &ev);
|
||||
|
||||
conn_info.conv_manager.insert_conv(conv, fd64);
|
||||
fd_manager.get_info(fd64).ip_port=ip_port;
|
||||
|
||||
|
||||
mylog(log_info,"[%s]new conv %x,fd %d created,fd64=%llu\n",ip_port.to_s(),conv,new_udp_fd,fd64);
|
||||
//assert(!conn_manager.exist_fd64(fd64));
|
||||
|
||||
//conn_manager.insert_fd64(fd64,ip_port);
|
||||
}
|
||||
conn_info.conv_manager.update_active_time(conv);
|
||||
fd64_t fd64= conn_info.conv_manager.find_u64_by_conv(conv);
|
||||
//int fd=fd_manager.fd64_to_fd(fd64);
|
||||
dest_t dest;
|
||||
dest.type=type_fd64;
|
||||
dest.inner.fd64=fd64;
|
||||
//dest.conv=conv;
|
||||
delay_send(out_delay[i],dest,new_data,new_len);
|
||||
}
|
||||
}
|
||||
else if (events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()) {
|
||||
uint64_t value;
|
||||
read(delay_manager.get_timer_fd(), &value, 8);
|
||||
mylog(log_trace,"events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()\n");
|
||||
}
|
||||
else if (events[idx].data.u64 >u32_t(-1))
|
||||
{
|
||||
char data[buf_len];
|
||||
int data_len;
|
||||
u32_t conv;
|
||||
fd64_t fd64=events[idx].data.u64;
|
||||
mylog(log_trace,"events[idx].data.u64 >u32_t(-1),%llu\n",(u64_t)events[idx].data.u64);
|
||||
if(!fd_manager.exist(fd64)) //fd64 has been closed
|
||||
{
|
||||
mylog(log_trace,"!fd_manager.exist(fd64)\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
assert(fd_manager.exist_info(fd64));
|
||||
ip_port_t ip_port=fd_manager.get_info(fd64).ip_port;
|
||||
assert(conn_manager.exist(ip_port));
|
||||
|
||||
conn_info_t &conn_info=conn_manager.find(ip_port);
|
||||
//conn_info.update_active_time(); //cant put it here
|
||||
|
||||
int out_n=-2;char **out_arr;int *out_len;my_time_t *out_delay;
|
||||
|
||||
dest_t dest;
|
||||
dest.inner.fd_ip_port.fd=local_listen_fd;
|
||||
dest.inner.fd_ip_port.ip_port=ip_port;
|
||||
dest.type=type_fd_ip_port;
|
||||
dest.cook=1;
|
||||
|
||||
if(fd64==conn_info.fec_encode_manager.get_timer_fd64())
|
||||
{
|
||||
//mylog(log_infol,"timer!!!\n");
|
||||
uint64_t value;
|
||||
if((ret=read(fd_manager.to_fd(fd64), &value, 8))!=8)
|
||||
{
|
||||
mylog(log_trace,"fd_manager.to_fd(fd64), &value, 8)!=8 ,%d\n",ret);
|
||||
continue;
|
||||
}
|
||||
if(value==0)
|
||||
{
|
||||
mylog(log_trace,"value==0\n");
|
||||
continue;
|
||||
}
|
||||
assert(value==1);
|
||||
from_normal_to_fec(conn_info,0,0,out_n,out_arr,out_len,out_delay);
|
||||
}
|
||||
else if(fd64==conn_info.timer.get_timer_fd64())
|
||||
{
|
||||
uint64_t value;
|
||||
read(conn_info.timer.get_timer_fd(), &value, 8);
|
||||
conn_info.conv_manager.clear_inactive();
|
||||
if(debug_force_flush_fec)
|
||||
{
|
||||
from_normal_to_fec(conn_info,0,0,out_n,out_arr,out_len,out_delay);
|
||||
}
|
||||
|
||||
conn_info.stat.report_as_server(ip_port);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(conn_info.conv_manager.is_u64_used(fd64));
|
||||
|
||||
conv=conn_info.conv_manager.find_conv_by_u64(fd64);
|
||||
conn_info.conv_manager.update_active_time(conv);
|
||||
conn_info.update_active_time();
|
||||
|
||||
int fd=fd_manager.to_fd(fd64);
|
||||
data_len=recv(fd,data,max_data_len,0);
|
||||
|
||||
mylog(log_trace,"received a packet from udp_fd,len:%d,conv=%d\n",data_len,conv);
|
||||
|
||||
if(data_len<0)
|
||||
{
|
||||
mylog(log_debug,"udp fd,recv_len<0 continue,%s\n",strerror(errno));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!disable_mtu_warn&&data_len>=mtu_warn)
|
||||
{
|
||||
mylog(log_warn,"huge packet,data len=%d (>=%d).strongly suggested to set a smaller mtu at upper level,to get rid of this warn\n ",data_len,mtu_warn);
|
||||
}
|
||||
|
||||
char * new_data;
|
||||
int new_len;
|
||||
put_conv(conv,data,data_len,new_data,new_len);
|
||||
|
||||
from_normal_to_fec(conn_info,new_data,new_len,out_n,out_arr,out_len,out_delay);
|
||||
}
|
||||
|
||||
mylog(log_trace,"out_n=%d\n",out_n);
|
||||
for(int i=0;i<out_n;i++)
|
||||
{
|
||||
delay_send(out_delay[i],dest,out_arr[i],out_len[i]);
|
||||
}
|
||||
//mylog(log_trace,"[%s] send packet\n",ip_port.to_s());
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_fatal,"unknown fd,this should never happen\n");
|
||||
myexit(-1);
|
||||
}
|
||||
}
|
||||
delay_manager.check();
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user