mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
add option --dev to bind to a specific interface
This commit is contained in:
parent
736c3f46b6
commit
fef76af3a5
23
main.cpp
23
main.cpp
@ -69,14 +69,6 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
||||
|
||||
mylog(log_info,"source_addr is now %s\n",new_addr.get_ip());
|
||||
|
||||
if(new_addr.get_type()==AF_INET)
|
||||
{
|
||||
send_info.src_ip=new_addr.inner.ipv4.sin_addr.s_addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0==1);
|
||||
}
|
||||
/*
|
||||
if(new_ip!=source_ip_uint32)
|
||||
{
|
||||
@ -92,6 +84,15 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
||||
new_addr=source_addr;
|
||||
}
|
||||
|
||||
if(new_addr.get_type()==AF_INET)
|
||||
{
|
||||
send_info.src_ip=new_addr.inner.ipv4.sin_addr.s_addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0==1);
|
||||
}
|
||||
|
||||
if (force_source_port == 0)
|
||||
{
|
||||
send_info.src_port = client_bind_to_a_new_port2(bind_fd,new_addr);
|
||||
@ -1090,7 +1091,7 @@ int client_event_loop()
|
||||
if(lower_level_manual)
|
||||
{
|
||||
int index;
|
||||
init_ifindex(if_name,index);
|
||||
init_ifindex(if_name,raw_send_fd,index);
|
||||
//init_ifindex(if_name);
|
||||
memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
|
||||
send_info.addr_ll.sll_family = AF_PACKET;
|
||||
@ -1148,7 +1149,7 @@ int client_event_loop()
|
||||
|
||||
//mylog(log_fatal,"--lower-level auto for client hasnt been implemented\n");
|
||||
int index;
|
||||
init_ifindex(if_name_string.c_str(),index);
|
||||
init_ifindex(if_name_string.c_str(),raw_send_fd,index);
|
||||
|
||||
memset(&send_info.addr_ll, 0, sizeof(send_info.addr_ll));
|
||||
send_info.addr_ll.sll_family = AF_PACKET;
|
||||
@ -1462,7 +1463,7 @@ int server_event_loop()
|
||||
{
|
||||
if(lower_level_manual)
|
||||
{
|
||||
init_ifindex(if_name,ifindex);
|
||||
init_ifindex(if_name,raw_send_fd,ifindex);
|
||||
mylog(log_info,"we are running at lower-level (manual) mode\n");
|
||||
}
|
||||
else
|
||||
|
6
misc.cpp
6
misc.cpp
@ -264,6 +264,7 @@ void process_arg(int argc, char *argv[]) //process all options
|
||||
{"gen-rule", no_argument, 0, 'g'},
|
||||
{"gen-add", no_argument, 0, 1},
|
||||
{"debug", no_argument, 0, 1},
|
||||
{"dev", required_argument, 0, 1},
|
||||
{"retry-on-error", no_argument, 0, 1},
|
||||
{"clear", no_argument, 0, 1},
|
||||
{"simple-rule", no_argument, 0, 1},
|
||||
@ -567,6 +568,11 @@ void process_arg(int argc, char *argv[]) //process all options
|
||||
debug_flag=1;
|
||||
//enable_log_color=0;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"dev")==0)
|
||||
{
|
||||
sscanf(optarg,"%s",dev);
|
||||
//enable_log_color=0;
|
||||
}
|
||||
else if(strcmp(long_options[option_index].name,"debug-resend")==0)
|
||||
{
|
||||
//debug_resend=1;
|
||||
|
24
network.cpp
24
network.cpp
@ -28,6 +28,8 @@ int lower_level_manual=0;
|
||||
int ifindex=-1;
|
||||
char if_name[100]="";
|
||||
|
||||
char dev[100]="";
|
||||
|
||||
unsigned short g_ip_id_counter=0;
|
||||
|
||||
unsigned char dest_hw_addr[sizeof(sockaddr_ll::sll_addr)]=
|
||||
@ -235,6 +237,24 @@ int init_raw_socket()
|
||||
//perror("");
|
||||
myexit(1);
|
||||
}
|
||||
if(strlen(dev)!=0)
|
||||
{
|
||||
struct sockaddr_ll bind_address;
|
||||
memset(&bind_address, 0, sizeof(bind_address));
|
||||
|
||||
int index=-1;
|
||||
assert(init_ifindex(dev,raw_recv_fd,index)==0);
|
||||
|
||||
bind_address.sll_family = AF_PACKET;
|
||||
bind_address.sll_protocol = htons(ETH_P_ALL);
|
||||
bind_address.sll_ifindex = index;
|
||||
|
||||
if(bind(raw_recv_fd, (struct sockaddr *)&bind_address, sizeof(bind_address))==-1)
|
||||
{
|
||||
mylog(log_fatal,"bind to dev [%s] failed\n",dev);
|
||||
myexit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(force_socket_buf)
|
||||
{
|
||||
@ -319,7 +339,7 @@ void remove_filter()
|
||||
//exit(-1);
|
||||
}
|
||||
}
|
||||
int init_ifindex(const char * if_name,int &index)
|
||||
int init_ifindex(const char * if_name,int fd,int &index)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
size_t if_name_len=strlen(if_name);
|
||||
@ -330,7 +350,7 @@ int init_ifindex(const char * if_name,int &index)
|
||||
mylog(log_fatal,"interface name is too long\n");
|
||||
myexit(-1);
|
||||
}
|
||||
if (ioctl(raw_send_fd,SIOCGIFINDEX,&ifr)==-1) {
|
||||
if (ioctl(fd,SIOCGIFINDEX,&ifr)==-1) {
|
||||
|
||||
mylog(log_fatal,"SIOCGIFINDEX fail ,%s\n",strerror(errno));
|
||||
myexit(-1);
|
||||
|
@ -19,6 +19,7 @@ extern int disable_bpf_filter;
|
||||
extern int lower_level;
|
||||
extern int lower_level_manual;
|
||||
extern char if_name[100];
|
||||
extern char dev[100];
|
||||
extern unsigned char dest_hw_addr[];
|
||||
|
||||
extern int random_drop;
|
||||
@ -94,7 +95,7 @@ void init_filter(int port);
|
||||
|
||||
void remove_filter();
|
||||
|
||||
int init_ifindex(const char * if_name,int &index);
|
||||
int init_ifindex(const char * if_name,int fd,int &index);
|
||||
|
||||
int find_lower_level_info(u32_t ip,u32_t &dest_ip,string &if_name,string &hw);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user