mirror of
https://github.com/wangyu-/UDPspeeder.git
synced 2025-01-18 22:09:35 +08:00
refactor --bind and --interface, fix --key
This commit is contained in:
parent
5bb65af72c
commit
55606b63f9
@ -964,7 +964,7 @@ int new_listen_socket2(int &fd,address_t &addr)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int new_connected_socket2(int &fd,address_t &addr,bool bind_enabled,address_t &bind_addr,char interface_string[])
|
int new_connected_socket2(int &fd,address_t &addr,address_t *bind_addr,char * interface_string)
|
||||||
{
|
{
|
||||||
fd = socket(addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
|
fd = socket(addr.get_type(), SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
@ -972,17 +972,19 @@ int new_connected_socket2(int &fd,address_t &addr,bool bind_enabled,address_t &b
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind_enabled && ::bind(fd, (struct sockaddr*) &bind_addr.inner, bind_addr.get_len()) == -1) {
|
if (bind_addr && ::bind(fd, (struct sockaddr*) &bind_addr->inner, bind_addr->get_len()) == -1) {
|
||||||
mylog(log_fatal,"socket bind error=%s\n", get_sock_error());
|
mylog(log_fatal,"socket bind error=%s\n", get_sock_error());
|
||||||
//perror("socket bind error");
|
//perror("socket bind error");
|
||||||
myexit(1);
|
myexit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(interface_string) > 0 && ::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, interface_string, strlen(interface_string)) < 0) {
|
#ifdef __linux__
|
||||||
|
if (interface_string && ::setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, interface_string, strlen(interface_string)) < 0) {
|
||||||
mylog(log_fatal,"socket interface bind error=%s\n", get_sock_error());
|
mylog(log_fatal,"socket interface bind error=%s\n", get_sock_error());
|
||||||
//perror("socket bind error");
|
//perror("socket bind error");
|
||||||
myexit(1);
|
myexit(1);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
setnonblocking(fd);
|
setnonblocking(fd);
|
||||||
set_buf_size(fd, socket_buf_size);
|
set_buf_size(fd, socket_buf_size);
|
||||||
|
2
common.h
2
common.h
@ -451,7 +451,7 @@ int new_listen_socket(int &fd,u32_t ip,int port);
|
|||||||
int new_connected_socket(int &fd,u32_t ip,int port);
|
int new_connected_socket(int &fd,u32_t ip,int port);
|
||||||
|
|
||||||
int new_listen_socket2(int &fd,address_t &addr);
|
int new_listen_socket2(int &fd,address_t &addr);
|
||||||
int new_connected_socket2(int &fd,address_t &addr,bool bind_enabled,address_t &bind_addr,char interface_string[]);
|
int new_connected_socket2(int &fd,address_t &addr,address_t *bind_addr,char *out_interface);
|
||||||
|
|
||||||
struct not_copy_able_t
|
struct not_copy_able_t
|
||||||
{
|
{
|
||||||
|
6
main.cpp
6
main.cpp
@ -37,8 +37,6 @@ static void print_help()
|
|||||||
printf(" --report <number> turn on send/recv report, and set a period for reporting, unit: s\n");
|
printf(" --report <number> turn on send/recv report, and set a period for reporting, unit: s\n");
|
||||||
|
|
||||||
printf("advanced options:\n");
|
printf("advanced options:\n");
|
||||||
printf(" -b,--bind ip:port force all output packets to go through this address. Set port to 0 to use a random one.\n");
|
|
||||||
printf(" --interface <string> force all output packets to go through this interface.\n");
|
|
||||||
printf(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
|
printf(" --mode <number> fec-mode,available values: 0,1; mode 0(default) costs less bandwidth,no mtu problem.\n");
|
||||||
printf(" mode 1 usually introduces less latency, but you have to care about mtu.\n");
|
printf(" mode 1 usually introduces less latency, but you have to care about mtu.\n");
|
||||||
printf(" --mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu value.\n");
|
printf(" --mtu <number> mtu. for mode 0, the program will split packet to segment smaller than mtu value.\n");
|
||||||
@ -68,6 +66,10 @@ static void print_help()
|
|||||||
printf(" --delay-capacity <number> max number of delayed packets, 0 means unlimited, default: 0\n");
|
printf(" --delay-capacity <number> max number of delayed packets, 0 means unlimited, default: 0\n");
|
||||||
printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
|
printf(" --disable-fec <number> completely disable fec, turn the program into a normal udp tunnel\n");
|
||||||
printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
|
printf(" --sock-buf <number> buf size for socket, >=10 and <=10240, unit: kbyte, default: 1024\n");
|
||||||
|
printf(" --out-addr ip:port force all output packets to go through this address. Set port to 0 to use a random one.\n");
|
||||||
|
#ifdef __linux__
|
||||||
|
printf(" --out-interface <string> force all output packets to go through this interface.\n");
|
||||||
|
#endif
|
||||||
printf("log and help options:\n");
|
printf("log and help options:\n");
|
||||||
printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
|
printf(" --log-level <number> 0: never 1: fatal 2: error 3: warn \n");
|
||||||
printf(" 4: info (default) 5: debug 6: trace\n");
|
printf(" 4: info (default) 5: debug 6: trace\n");
|
||||||
|
2
makefile
2
makefile
@ -4,7 +4,7 @@ cc_mips24kc_be=/toolchains/lede-sdk-17.01.2-ar71xx-generic_gcc-5.4.0_musl-1.1.16
|
|||||||
cc_mips24kc_le=/toolchains/lede-sdk-17.01.2-ramips-mt7621_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-musl-g++
|
cc_mips24kc_le=/toolchains/lede-sdk-17.01.2-ramips-mt7621_gcc-5.4.0_musl-1.1.16.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-musl-g++
|
||||||
cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++
|
||||||
cc_mingw_cross=i686-w64-mingw32-g++-posix
|
cc_mingw_cross=i686-w64-mingw32-g++-posix
|
||||||
cc_mac_cross=o64-clang++ -stdlib=libc++ -std=c++11
|
cc_mac_cross=o64-clang++ -stdlib=libc++
|
||||||
#cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-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 ${OPT}
|
FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers ${OPT}
|
||||||
|
33
misc.cpp
33
misc.cpp
@ -28,10 +28,10 @@ int output_interval_max=0*1000;
|
|||||||
|
|
||||||
int fix_latency=0;
|
int fix_latency=0;
|
||||||
|
|
||||||
char interface_string[16];
|
|
||||||
|
|
||||||
bool has_b=false;
|
address_t local_addr,remote_addr;
|
||||||
address_t local_addr,remote_addr,bind_addr;
|
address_t *out_addr=0;
|
||||||
|
char *out_interface=0;
|
||||||
//u32_t local_ip_uint32,remote_ip_uint32=0;
|
//u32_t local_ip_uint32,remote_ip_uint32=0;
|
||||||
//char local_ip[100], remote_ip[100];
|
//char local_ip[100], remote_ip[100];
|
||||||
//int local_port = -1, remote_port = -1;
|
//int local_port = -1, remote_port = -1;
|
||||||
@ -651,14 +651,15 @@ void process_arg(int argc, char *argv[])
|
|||||||
{"report", required_argument, 0, 1},
|
{"report", required_argument, 0, 1},
|
||||||
{"delay-capacity", required_argument, 0, 1},
|
{"delay-capacity", required_argument, 0, 1},
|
||||||
{"mtu", required_argument, 0, 1},
|
{"mtu", required_argument, 0, 1},
|
||||||
{"interface", required_argument, 0, 1},
|
|
||||||
{"mode", required_argument, 0,1},
|
{"mode", required_argument, 0,1},
|
||||||
{"timeout", required_argument, 0,1},
|
{"timeout", required_argument, 0,1},
|
||||||
{"decode-buf", required_argument, 0,1},
|
{"decode-buf", required_argument, 0,1},
|
||||||
{"queue-len", required_argument, 0,'q'},
|
{"queue-len", required_argument, 0,'q'},
|
||||||
{"fec", required_argument, 0,'f'},
|
{"fec", required_argument, 0,'f'},
|
||||||
{"jitter", required_argument, 0,'j'},
|
{"jitter", required_argument, 0,'j'},
|
||||||
{"bind", required_argument, 0,'b'},
|
{"out-addr", required_argument, 0,1},
|
||||||
|
{"out-interface", required_argument, 0, 1},
|
||||||
|
{"key", required_argument, 0,'k'},
|
||||||
{"header-overhead", required_argument, 0, 1},
|
{"header-overhead", required_argument, 0, 1},
|
||||||
//{"debug-fec", no_argument, 0, 1},
|
//{"debug-fec", no_argument, 0, 1},
|
||||||
{"debug-fec-enc", no_argument, 0, 1},
|
{"debug-fec-enc", no_argument, 0, 1},
|
||||||
@ -829,10 +830,6 @@ void process_arg(int argc, char *argv[])
|
|||||||
no_r = 0;
|
no_r = 0;
|
||||||
remote_addr.from_str(optarg);
|
remote_addr.from_str(optarg);
|
||||||
break;
|
break;
|
||||||
case 'b':
|
|
||||||
has_b = true;
|
|
||||||
bind_addr.from_str(optarg);
|
|
||||||
break;
|
|
||||||
case 'h':
|
case 'h':
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
@ -953,13 +950,21 @@ void process_arg(int argc, char *argv[])
|
|||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(strcmp(long_options[option_index].name,"interface")==0)
|
else if(strcmp(long_options[option_index].name,"out-addr")==0)
|
||||||
{
|
{
|
||||||
sscanf(optarg,"%s\n",interface_string);
|
//has_b = true;
|
||||||
mylog(log_debug,"interface=%s\n",interface_string);
|
mylog(log_debug,"out-addr=%s\n",optarg);
|
||||||
if(strlen(interface_string)==0)
|
out_addr=new address_t();
|
||||||
|
out_addr->from_str(optarg);
|
||||||
|
}
|
||||||
|
else if(strcmp(long_options[option_index].name,"out-interface")==0)
|
||||||
|
{
|
||||||
|
out_interface=new char[strlen(optarg)+10];
|
||||||
|
sscanf(optarg,"%s\n",out_interface);
|
||||||
|
mylog(log_debug,"out-interface=%s\n",out_interface);
|
||||||
|
if(strlen(out_interface)==0)
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"interface_string len=0??\n");
|
mylog(log_fatal,"out_interface string len=0??\n");
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
misc.h
7
misc.h
@ -39,10 +39,11 @@ extern int fix_latency;
|
|||||||
//extern char local_ip[100], remote_ip[100];
|
//extern char local_ip[100], remote_ip[100];
|
||||||
//extern int local_port, remote_port;
|
//extern int local_port, remote_port;
|
||||||
|
|
||||||
extern char interface_string[16];
|
|
||||||
|
|
||||||
extern bool has_b;
|
extern address_t local_addr,remote_addr;
|
||||||
extern address_t local_addr,remote_addr,bind_addr;
|
|
||||||
|
extern address_t *out_addr;
|
||||||
|
extern char *out_interface;
|
||||||
|
|
||||||
extern conn_manager_t conn_manager;
|
extern conn_manager_t conn_manager;
|
||||||
extern delay_manager_t delay_manager;
|
extern delay_manager_t delay_manager;
|
||||||
|
@ -295,7 +295,7 @@ int tunnel_client_event_loop()
|
|||||||
int & remote_fd=conn_info.remote_fd;
|
int & remote_fd=conn_info.remote_fd;
|
||||||
fd64_t &remote_fd64=conn_info.remote_fd64;
|
fd64_t &remote_fd64=conn_info.remote_fd64;
|
||||||
|
|
||||||
assert(new_connected_socket2(remote_fd,remote_addr,has_b,bind_addr,interface_string)==0);
|
assert(new_connected_socket2(remote_fd,remote_addr,out_addr,out_interface)==0);
|
||||||
remote_fd64=fd_manager.create(remote_fd);
|
remote_fd64=fd_manager.create(remote_fd);
|
||||||
|
|
||||||
mylog(log_debug,"remote_fd64=%llu\n",remote_fd64);
|
mylog(log_debug,"remote_fd64=%llu\n",remote_fd64);
|
||||||
|
@ -232,7 +232,7 @@ static void local_listen_cb(struct ev_loop *loop, struct ev_io *watcher, int rev
|
|||||||
}
|
}
|
||||||
|
|
||||||
int new_udp_fd;
|
int new_udp_fd;
|
||||||
ret=new_connected_socket2(new_udp_fd,remote_addr,has_b,bind_addr,interface_string);
|
ret=new_connected_socket2(new_udp_fd,remote_addr,out_addr,out_interface);
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
mylog(log_warn, "[%s]new_connected_socket failed\n",addr.get_str());
|
mylog(log_warn, "[%s]new_connected_socket failed\n",addr.get_str());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user