mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
changed src_ip and dst_ip in conn_info to new data structure
This commit is contained in:
parent
52d540df90
commit
0682757631
23
common.h
23
common.h
@ -129,6 +129,25 @@ struct address_t //TODO scope id
|
|||||||
inner.ipv4.sin_addr.s_addr=ip;
|
inner.ipv4.sin_addr.s_addr=ip;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int from_ip_port_new(int type, void * ip, int port)
|
||||||
|
{
|
||||||
|
clear();
|
||||||
|
if(type==AF_INET)
|
||||||
|
{
|
||||||
|
inner.ipv4.sin_family=AF_INET;
|
||||||
|
inner.ipv4.sin_port=htons(port);
|
||||||
|
inner.ipv4.sin_addr.s_addr=*((u32_t *)ip);
|
||||||
|
}
|
||||||
|
else if(type==AF_INET6)
|
||||||
|
{
|
||||||
|
inner.ipv6.sin6_family=AF_INET;
|
||||||
|
inner.ipv6.sin6_port=htons(port);
|
||||||
|
inner.ipv6.sin6_addr=*((in6_addr*)ip);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int from_str(char * str);
|
int from_str(char * str);
|
||||||
|
|
||||||
int from_sockaddr(sockaddr *,socklen_t);
|
int from_sockaddr(sockaddr *,socklen_t);
|
||||||
@ -138,7 +157,9 @@ struct address_t //TODO scope id
|
|||||||
|
|
||||||
inline u32_t get_type()
|
inline u32_t get_type()
|
||||||
{
|
{
|
||||||
return ((sockaddr*)&inner)->sa_family;
|
u32_t ret=((sockaddr*)&inner)->sa_family;
|
||||||
|
assert(ret==AF_INET||ret==AF_INET6);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline u32_t get_len()
|
inline u32_t get_len()
|
||||||
|
@ -333,7 +333,7 @@ int conn_manager_t::clear_inactive0()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_info,"[%s:%d]inactive conn cleared \n",my_ntoa(it->second->raw_info.recv_info.src_ip),it->second->raw_info.recv_info.src_port);
|
mylog(log_info,"[%s:%d]inactive conn cleared \n",it->second->raw_info.recv_info.new_src_ip.get_str1(),it->second->raw_info.recv_info.src_port);
|
||||||
old_it=it;
|
old_it=it;
|
||||||
it++;
|
it++;
|
||||||
erase(old_it);
|
erase(old_it);
|
||||||
|
100
main.cpp
100
main.cpp
@ -52,11 +52,11 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
address_t new_addr;
|
address_t tmp_addr;
|
||||||
//u32_t new_ip=0;
|
//u32_t new_ip=0;
|
||||||
if(!force_source_ip)
|
if(!force_source_ip)
|
||||||
{
|
{
|
||||||
if(get_src_adress2(new_addr,remote_addr)!=0)
|
if(get_src_adress2(tmp_addr,remote_addr)!=0)
|
||||||
{
|
{
|
||||||
mylog(log_warn,"get_src_adress() failed\n");
|
mylog(log_warn,"get_src_adress() failed\n");
|
||||||
return -1;
|
return -1;
|
||||||
@ -64,7 +64,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
|||||||
//source_addr=new_addr;
|
//source_addr=new_addr;
|
||||||
//source_addr.set_port(0);
|
//source_addr.set_port(0);
|
||||||
|
|
||||||
mylog(log_info,"source_addr is now %s\n",new_addr.get_ip());
|
mylog(log_info,"source_addr is now %s\n",tmp_addr.get_ip());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if(new_ip!=source_ip_uint32)
|
if(new_ip!=source_ip_uint32)
|
||||||
@ -78,12 +78,16 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
new_addr=source_addr;
|
tmp_addr=source_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(new_addr.get_type()==AF_INET)
|
if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET)
|
||||||
{
|
{
|
||||||
send_info.src_ip=new_addr.inner.ipv4.sin_addr.s_addr;
|
send_info.new_src_ip.v4=tmp_addr.inner.ipv4.sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
else if(tmp_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET6)
|
||||||
|
{
|
||||||
|
send_info.new_src_ip.v6=tmp_addr.inner.ipv6.sin6_addr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -92,7 +96,7 @@ int client_on_timer(conn_info_t &conn_info) //for client. called when a timer is
|
|||||||
|
|
||||||
if (force_source_port == 0)
|
if (force_source_port == 0)
|
||||||
{
|
{
|
||||||
send_info.src_port = client_bind_to_a_new_port2(bind_fd,new_addr);
|
send_info.src_port = client_bind_to_a_new_port2(bind_fd,tmp_addr);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -319,9 +323,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
|
|||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
|
if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
|
||||||
{
|
{
|
||||||
mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
|
mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(data_len==0&&raw_info.recv_info.syn==1&&raw_info.recv_info.ack==1)
|
if(data_len==0&&raw_info.recv_info.syn==1&&raw_info.recv_info.ack==1)
|
||||||
@ -352,9 +356,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
|
|||||||
mylog(log_debug,"recv_bare failed!\n");
|
mylog(log_debug,"recv_bare failed!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
|
if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
|
||||||
{
|
{
|
||||||
mylog(log_debug,"unexpected adress %x %x %d %d\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
|
mylog(log_debug,"unexpected adress %s %s %d %d\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(data_len<int( 3*sizeof(id_t)))
|
if(data_len<int( 3*sizeof(id_t)))
|
||||||
@ -417,9 +421,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
|
|||||||
mylog(log_debug,"recv_safer failed!\n");
|
mylog(log_debug,"recv_safer failed!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(recv_info.src_ip!=send_info.dst_ip||recv_info.src_port!=send_info.dst_port)
|
if(!recv_info.new_src_ip.equal(send_info.new_dst_ip)||recv_info.src_port!=send_info.dst_port)
|
||||||
{
|
{
|
||||||
mylog(log_warn,"unexpected adress %x %x %d %d,this shouldnt happen.\n",recv_info.src_ip,send_info.dst_ip,recv_info.src_port,send_info.dst_port);
|
mylog(log_warn,"unexpected adress %s %s %d %d,this shouldnt happen.\n",recv_info.new_src_ip.get_str1(),send_info.new_dst_ip.get_str2(),recv_info.src_port,send_info.dst_port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(conn_info.state.client_current_state==client_handshake2)
|
if(conn_info.state.client_current_state==client_handshake2)
|
||||||
@ -593,14 +597,17 @@ int server_on_raw_recv_handshake1(conn_info_t &conn_info,id_t tmp_oppsite_id )
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}*/
|
}*/
|
||||||
int server_on_timer(conn_info_t &conn_info) //for server. called when a timer is ready in epoll.for server,there will be one timer for every connection
|
int server_on_timer_multi(conn_info_t &conn_info) //for server. called when a timer is ready in epoll.for server,there will be one timer for every connection
|
||||||
// there is also a global timer for server,but its not handled here
|
// there is also a global timer for server,but its not handled here
|
||||||
{
|
{
|
||||||
char ip_port[40];
|
char ip_port[40];
|
||||||
u32_t ip=conn_info.raw_info.send_info.dst_ip;
|
//u32_t ip=conn_info.raw_info.send_info.dst_ip;
|
||||||
u32_t port=conn_info.raw_info.send_info.dst_port;
|
//u32_t port=conn_info.raw_info.send_info.dst_port;
|
||||||
|
|
||||||
sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
address_t tmp_addr;
|
||||||
|
tmp_addr.from_ip_port_new(raw_ip_version,&conn_info.raw_info.send_info.new_dst_ip,conn_info.raw_info.send_info.dst_port);
|
||||||
|
//sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
tmp_addr.to_str(ip_port);
|
||||||
|
|
||||||
//keep_iptables_rule();
|
//keep_iptables_rule();
|
||||||
mylog(log_trace,"server timer!\n");
|
mylog(log_trace,"server timer!\n");
|
||||||
@ -863,13 +870,13 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp
|
|||||||
conn_info.oppsite_const_id=0;
|
conn_info.oppsite_const_id=0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
address_t addr1;addr1.from_ip_port(ori_conn_info.raw_info.recv_info.src_ip,ori_conn_info.raw_info.recv_info.src_port);
|
address_t addr1;addr1.from_ip_port_new(raw_ip_version,&ori_conn_info.raw_info.recv_info.new_src_ip,ori_conn_info.raw_info.recv_info.src_port);
|
||||||
if(!conn_manager.exist(addr1))//TODO remove this
|
if(!conn_manager.exist(addr1))//TODO remove this
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"[%s]this shouldnt happen\n",ip_port);
|
mylog(log_fatal,"[%s]this shouldnt happen\n",ip_port);
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
address_t addr2;addr2.from_ip_port(conn_info.raw_info.recv_info.src_ip,conn_info.raw_info.recv_info.src_port);
|
address_t addr2;addr2.from_ip_port_new(raw_ip_version,&conn_info.raw_info.recv_info.new_src_ip,conn_info.raw_info.recv_info.src_port);
|
||||||
if(!conn_manager.exist(addr2))//TODO remove this
|
if(!conn_manager.exist(addr2))//TODO remove this
|
||||||
{
|
{
|
||||||
mylog(log_fatal,"[%s]this shouldnt happen2\n",ip_port);
|
mylog(log_fatal,"[%s]this shouldnt happen2\n",ip_port);
|
||||||
@ -1005,15 +1012,19 @@ int server_on_raw_recv_multi() //called when server received an raw packet
|
|||||||
{
|
{
|
||||||
mylog(log_trace,"peek_raw success\n");
|
mylog(log_trace,"peek_raw success\n");
|
||||||
}
|
}
|
||||||
u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
|
//u32_t ip=peek_info.src_ip;uint16_t port=peek_info.src_port;
|
||||||
|
|
||||||
|
|
||||||
char ip_port[40];
|
|
||||||
sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
|
||||||
mylog(log_trace,"[%s]peek_raw\n",ip_port);
|
|
||||||
int data_len; char *data;
|
int data_len; char *data;
|
||||||
|
|
||||||
address_t addr;
|
address_t addr;
|
||||||
addr.from_ip_port(ip,port);
|
addr.from_ip_port_new(raw_ip_version,&peek_info.new_src_ip,peek_info.src_port);
|
||||||
|
|
||||||
|
char ip_port[40];
|
||||||
|
addr.to_str(ip_port);
|
||||||
|
//sprintf(ip_port,"%s:%d",my_ntoa(ip),port);
|
||||||
|
mylog(log_trace,"[%s]peek_raw\n",ip_port);
|
||||||
|
|
||||||
if(raw_mode==mode_faketcp&&peek_info.syn==1)
|
if(raw_mode==mode_faketcp&&peek_info.syn==1)
|
||||||
{
|
{
|
||||||
if(!conn_manager.exist(addr)||conn_manager.find_insert(addr).state.server_current_state!=server_ready)
|
if(!conn_manager.exist(addr)||conn_manager.find_insert(addr).state.server_current_state!=server_ready)
|
||||||
@ -1030,11 +1041,11 @@ int server_on_raw_recv_multi() //called when server received an raw packet
|
|||||||
packet_info_t &send_info=raw_info.send_info;
|
packet_info_t &send_info=raw_info.send_info;
|
||||||
packet_info_t &recv_info=raw_info.recv_info;
|
packet_info_t &recv_info=raw_info.recv_info;
|
||||||
|
|
||||||
send_info.src_ip=recv_info.dst_ip;
|
send_info.new_src_ip=recv_info.new_dst_ip;
|
||||||
send_info.src_port=recv_info.dst_port;
|
send_info.src_port=recv_info.dst_port;
|
||||||
|
|
||||||
send_info.dst_port = recv_info.src_port;
|
send_info.dst_port = recv_info.src_port;
|
||||||
send_info.dst_ip = recv_info.src_ip;
|
send_info.new_dst_ip = recv_info.new_src_ip;
|
||||||
|
|
||||||
if(lower_level)
|
if(lower_level)
|
||||||
{
|
{
|
||||||
@ -1077,7 +1088,7 @@ int server_on_raw_recv_multi() //called when server received an raw packet
|
|||||||
|
|
||||||
if(raw_mode==mode_icmp)
|
if(raw_mode==mode_icmp)
|
||||||
{
|
{
|
||||||
tmp_raw_info.send_info.dst_port=tmp_raw_info.send_info.src_port=port;
|
tmp_raw_info.send_info.dst_port=tmp_raw_info.send_info.src_port=addr.get_port();
|
||||||
}
|
}
|
||||||
if(recv_bare(tmp_raw_info,data,data_len)<0)
|
if(recv_bare(tmp_raw_info,data,data_len)<0)
|
||||||
{
|
{
|
||||||
@ -1114,11 +1125,11 @@ int server_on_raw_recv_multi() //called when server received an raw packet
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
send_info.src_ip=recv_info.dst_ip;
|
send_info.new_src_ip=recv_info.new_dst_ip;
|
||||||
send_info.src_port=recv_info.dst_port;
|
send_info.src_port=recv_info.dst_port;
|
||||||
|
|
||||||
send_info.dst_port = recv_info.src_port;
|
send_info.dst_port = recv_info.src_port;
|
||||||
send_info.dst_ip = recv_info.src_ip;
|
send_info.new_dst_ip = recv_info.new_src_ip;
|
||||||
|
|
||||||
if(lower_level)
|
if(lower_level)
|
||||||
{
|
{
|
||||||
@ -1369,13 +1380,26 @@ int client_event_loop()
|
|||||||
myexit(-1);
|
myexit(-1);
|
||||||
}*/
|
}*/
|
||||||
send_info.src_port=0;
|
send_info.src_port=0;
|
||||||
send_info.src_ip = 0;
|
memset(&send_info.new_src_ip,0,sizeof(send_info.new_src_ip));
|
||||||
|
|
||||||
int i, j, k;int ret;
|
int i, j, k;int ret;
|
||||||
|
|
||||||
|
|
||||||
//init_filter(source_port);
|
//init_filter(source_port);
|
||||||
send_info.dst_ip=remote_addr.inner.ipv4.sin_addr.s_addr;
|
|
||||||
|
if(remote_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET)
|
||||||
|
{
|
||||||
|
send_info.new_dst_ip.v4=remote_addr.inner.ipv4.sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
else if(remote_addr.get_type()==raw_ip_version&&raw_ip_version==AF_INET6)
|
||||||
|
{
|
||||||
|
send_info.new_dst_ip.v6=remote_addr.inner.ipv6.sin6_addr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assert(0==1);
|
||||||
|
}
|
||||||
|
|
||||||
send_info.dst_port=remote_addr.get_port();
|
send_info.dst_port=remote_addr.get_port();
|
||||||
|
|
||||||
//g_packet_info.src_ip=source_address_uint32;
|
//g_packet_info.src_ip=source_address_uint32;
|
||||||
@ -1436,8 +1460,7 @@ int client_event_loop()
|
|||||||
|
|
||||||
set_timer(epollfd,timer_fd);
|
set_timer(epollfd,timer_fd);
|
||||||
|
|
||||||
mylog(log_debug,"send_raw : from %x %d to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
|
mylog(log_debug,"send_raw : from %s %d to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
|
||||||
|
|
||||||
int fifo_fd=-1;
|
int fifo_fd=-1;
|
||||||
|
|
||||||
if(fifo_file[0]!=0)
|
if(fifo_file[0]!=0)
|
||||||
@ -1718,7 +1741,7 @@ int server_event_loop()
|
|||||||
u64_t dummy;
|
u64_t dummy;
|
||||||
read(fd, &dummy, 8);
|
read(fd, &dummy, 8);
|
||||||
assert(conn_info.state.server_current_state == server_ready); //TODO remove this for peformance
|
assert(conn_info.state.server_current_state == server_ready); //TODO remove this for peformance
|
||||||
server_on_timer(conn_info);
|
server_on_timer_multi(conn_info);
|
||||||
if(debug_flag)
|
if(debug_flag)
|
||||||
{
|
{
|
||||||
end_time=get_current_time();
|
end_time=get_current_time();
|
||||||
@ -1815,15 +1838,10 @@ int main(int argc, char *argv[])
|
|||||||
//}
|
//}
|
||||||
//else
|
//else
|
||||||
|
|
||||||
#else
|
|
||||||
{
|
|
||||||
//strncpy(remote_ip,remote_address,sizeof(remote_ip)-1);
|
|
||||||
//strcpy(remote_ip,remote_address);
|
|
||||||
//remote_ip_uint32=inet_addr(remote_ip);
|
|
||||||
mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
|
||||||
|
|
||||||
//current_time_rough=get_current_time();
|
//current_time_rough=get_current_time();
|
||||||
|
|
||||||
init_random_number_fd();
|
init_random_number_fd();
|
||||||
|
10
misc.cpp
10
misc.cpp
@ -74,7 +74,7 @@ int iptables_rule_keep_index=0;
|
|||||||
|
|
||||||
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
|
||||||
raw_mode_t raw_mode=mode_faketcp;
|
raw_mode_t raw_mode=mode_faketcp;
|
||||||
ip_version_t ip_version=version_unset;
|
u32_t raw_ip_version=(u32_t)-1;
|
||||||
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"}, {mode_udp, "udp"}, {mode_icmp, "icmp"}};
|
||||||
|
|
||||||
int about_to_exit=0;
|
int about_to_exit=0;
|
||||||
@ -714,6 +714,14 @@ void process_arg(int argc, char *argv[]) //process all options
|
|||||||
print_help();
|
print_help();
|
||||||
myexit(-1);
|
myexit(-1);
|
||||||
}
|
}
|
||||||
|
if(program_mode==client_mode)
|
||||||
|
{
|
||||||
|
raw_ip_version=remote_addr.get_type();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
raw_ip_version=local_addr.get_type();
|
||||||
|
}
|
||||||
|
|
||||||
//if(lower_level)
|
//if(lower_level)
|
||||||
//process_lower_level_arg();
|
//process_lower_level_arg();
|
||||||
|
3
misc.h
3
misc.h
@ -65,7 +65,6 @@ enum server_current_state_t {server_idle=0,server_handshake1,server_ready}; //s
|
|||||||
enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine
|
enum client_current_state_t {client_idle=0,client_tcp_handshake,client_handshake1,client_handshake2,client_ready};//client state machine
|
||||||
|
|
||||||
enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
|
enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
|
||||||
enum ip_version_t{version_unset=0,version_ipv4,version_ipv6,version_end};
|
|
||||||
enum program_mode_t {unset_mode=0,client_mode,server_mode};
|
enum program_mode_t {unset_mode=0,client_mode,server_mode};
|
||||||
|
|
||||||
union current_state_t
|
union current_state_t
|
||||||
@ -111,7 +110,7 @@ extern char fifo_file[1000];
|
|||||||
|
|
||||||
|
|
||||||
extern raw_mode_t raw_mode;
|
extern raw_mode_t raw_mode;
|
||||||
extern ip_version_t ip_version;
|
extern u32_t raw_ip_version;
|
||||||
|
|
||||||
extern program_mode_t program_mode;
|
extern program_mode_t program_mode;
|
||||||
extern unordered_map<int, const char*> raw_mode_tostring ;
|
extern unordered_map<int, const char*> raw_mode_tostring ;
|
||||||
|
85
network.cpp
85
network.cpp
@ -154,6 +154,45 @@ tcpdump -i eth1 ip and icmp -dd
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
bool packet_info_t::tmp_ip_t::equal (const tmp_ip_t &b) const
|
||||||
|
{
|
||||||
|
//extern int raw_ip_version;
|
||||||
|
if(raw_ip_version==AF_INET)
|
||||||
|
{
|
||||||
|
return v4==b.v4;
|
||||||
|
}else if(raw_ip_version==AF_INET)
|
||||||
|
{
|
||||||
|
return memcmp(&v6,&b.v6,sizeof(v6));
|
||||||
|
}
|
||||||
|
assert(0==1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
char * packet_info_t::tmp_ip_t::get_str1() const
|
||||||
|
{
|
||||||
|
static char res[max_addr_len];
|
||||||
|
if(raw_ip_version==AF_INET6)
|
||||||
|
{
|
||||||
|
assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0);
|
||||||
|
}
|
||||||
|
else if(raw_ip_version==AF_INET)
|
||||||
|
{
|
||||||
|
assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
char * packet_info_t::tmp_ip_t::get_str2() const
|
||||||
|
{
|
||||||
|
static char res[max_addr_len];
|
||||||
|
if(raw_ip_version==AF_INET6)
|
||||||
|
{
|
||||||
|
assert(inet_ntop(AF_INET6, &v6, res,max_addr_len)!=0);
|
||||||
|
}
|
||||||
|
else if(raw_ip_version==AF_INET)
|
||||||
|
{
|
||||||
|
assert(inet_ntop(AF_INET, &v4, res,max_addr_len)!=0);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
packet_info_t::packet_info_t()
|
packet_info_t::packet_info_t()
|
||||||
{
|
{
|
||||||
src_port=0;
|
src_port=0;
|
||||||
@ -607,7 +646,7 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
|
|||||||
|
|
||||||
if(raw_info.disabled)
|
if(raw_info.disabled)
|
||||||
{
|
{
|
||||||
mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",my_ntoa(recv_info.src_ip),recv_info.src_port);
|
mylog(log_debug,"[%s,%d]connection disabled, no packet will be sent\n",recv_info.new_src_ip.get_str1(),recv_info.src_port);
|
||||||
assert(max_rst_allowed>=0);
|
assert(max_rst_allowed>=0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -633,8 +672,8 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
|
|||||||
iph->ttl = (unsigned char)ttl_value;
|
iph->ttl = (unsigned char)ttl_value;
|
||||||
iph->protocol = send_info.protocol;
|
iph->protocol = send_info.protocol;
|
||||||
iph->check = 0; //Set to 0 before calculating checksum
|
iph->check = 0; //Set to 0 before calculating checksum
|
||||||
iph->saddr = send_info.src_ip; //Spoof the source ip address
|
iph->saddr = send_info.new_src_ip.v4; //Spoof the source ip address
|
||||||
iph->daddr = send_info.dst_ip;
|
iph->daddr = send_info.new_dst_ip.v4;
|
||||||
|
|
||||||
uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
|
uint16_t ip_tot_len=sizeof (struct iphdr)+payloadlen;
|
||||||
if(lower_level)iph->tot_len = htons(ip_tot_len); //this is not necessary ,kernel will always auto fill this //http://man7.org/linux/man-pages/man7/raw.7.html
|
if(lower_level)iph->tot_len = htons(ip_tot_len); //this is not necessary ,kernel will always auto fill this //http://man7.org/linux/man-pages/man7/raw.7.html
|
||||||
@ -654,7 +693,7 @@ int send_raw_ip(raw_info_t &raw_info,const char * payload,int payloadlen)
|
|||||||
struct sockaddr_in sin={0};
|
struct sockaddr_in sin={0};
|
||||||
sin.sin_family = AF_INET;
|
sin.sin_family = AF_INET;
|
||||||
//sin.sin_port = htons(info.dst_port); //dont need this
|
//sin.sin_port = htons(info.dst_port); //dont need this
|
||||||
sin.sin_addr.s_addr = send_info.dst_ip;
|
sin.sin_addr.s_addr = send_info.new_dst_ip.v4;
|
||||||
ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
|
ret = sendto(raw_send_fd, send_raw_ip_buf, ip_tot_len , 0, (struct sockaddr *) &sin, sizeof (sin));
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -699,7 +738,7 @@ int peek_raw(packet_info_t &peek_info)
|
|||||||
mylog(log_trace,"%s\n ",strerror(errno));
|
mylog(log_trace,"%s\n ",strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
peek_info.src_ip=iph->saddr;
|
peek_info.new_src_ip.v4=iph->saddr;
|
||||||
unsigned short iphdrlen =iph->ihl*4;
|
unsigned short iphdrlen =iph->ihl*4;
|
||||||
char *payload=ip_begin+iphdrlen;
|
char *payload=ip_begin+iphdrlen;
|
||||||
|
|
||||||
@ -811,8 +850,8 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
|
|
||||||
iph = (struct iphdr *) (ip_begin);
|
iph = (struct iphdr *) (ip_begin);
|
||||||
|
|
||||||
recv_info.src_ip=iph->saddr;
|
recv_info.new_src_ip.v4=iph->saddr;
|
||||||
recv_info.dst_ip=iph->daddr;
|
recv_info.new_dst_ip.v4=iph->daddr;
|
||||||
recv_info.protocol=iph->protocol;
|
recv_info.protocol=iph->protocol;
|
||||||
|
|
||||||
if(lower_level)
|
if(lower_level)
|
||||||
@ -821,7 +860,7 @@ int recv_raw_ip(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(bind_addr_used && recv_info.dst_ip!=bind_addr.inner.ipv4.sin_addr.s_addr)
|
if(bind_addr_used && recv_info.new_dst_ip.v4!=bind_addr.inner.ipv4.sin_addr.s_addr)
|
||||||
{
|
{
|
||||||
mylog(log_trace,"bind adress doenst match, dropped\n");
|
mylog(log_trace,"bind adress doenst match, dropped\n");
|
||||||
//printf(" bind adress doenst match, dropped\n");
|
//printf(" bind adress doenst match, dropped\n");
|
||||||
@ -933,8 +972,8 @@ int send_raw_udp(raw_info_t &raw_info, const char * payload, int payloadlen)
|
|||||||
|
|
||||||
memcpy(send_raw_udp_buf+sizeof(udphdr),payload,payloadlen);
|
memcpy(send_raw_udp_buf+sizeof(udphdr),payload,payloadlen);
|
||||||
|
|
||||||
psh->source_address = send_info.src_ip;
|
psh->source_address = send_info.new_src_ip.v4;
|
||||||
psh->dest_address = send_info.dst_ip;
|
psh->dest_address = send_info.new_dst_ip.v4;
|
||||||
psh->placeholder = 0;
|
psh->placeholder = 0;
|
||||||
psh->protocol = IPPROTO_UDP;
|
psh->protocol = IPPROTO_UDP;
|
||||||
psh->tcp_length = htons(uint16_t(udp_tot_len));
|
psh->tcp_length = htons(uint16_t(udp_tot_len));
|
||||||
@ -1054,8 +1093,8 @@ int send_raw_tcp(raw_info_t &raw_info,const char * payload, int payloadlen) {
|
|||||||
|
|
||||||
memcpy(tcp_data, payload, payloadlen);
|
memcpy(tcp_data, payload, payloadlen);
|
||||||
|
|
||||||
psh->source_address = send_info.src_ip;
|
psh->source_address = send_info.new_src_ip.v4;
|
||||||
psh->dest_address = send_info.dst_ip;
|
psh->dest_address = send_info.new_dst_ip.v4;
|
||||||
psh->placeholder = 0;
|
psh->placeholder = 0;
|
||||||
psh->protocol = IPPROTO_TCP;
|
psh->protocol = IPPROTO_TCP;
|
||||||
psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
|
psh->tcp_length = htons(tcph->doff * 4 + payloadlen);
|
||||||
@ -1358,8 +1397,8 @@ int recv_raw_udp(raw_info_t &raw_info, char *&payload, int &payloadlen)
|
|||||||
pseudo_header tmp_header={0};
|
pseudo_header tmp_header={0};
|
||||||
struct pseudo_header *psh=&tmp_header ;
|
struct pseudo_header *psh=&tmp_header ;
|
||||||
|
|
||||||
psh->source_address = recv_info.src_ip;
|
psh->source_address = recv_info.new_src_ip.v4;
|
||||||
psh->dest_address = recv_info.dst_ip;
|
psh->dest_address = recv_info.new_dst_ip.v4;
|
||||||
psh->placeholder = 0;
|
psh->placeholder = 0;
|
||||||
psh->protocol = IPPROTO_UDP;
|
psh->protocol = IPPROTO_UDP;
|
||||||
psh->tcp_length = htons(ip_payloadlen);
|
psh->tcp_length = htons(ip_payloadlen);
|
||||||
@ -1506,8 +1545,8 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
pseudo_header tmp_header;
|
pseudo_header tmp_header;
|
||||||
struct pseudo_header *psh=&tmp_header ;
|
struct pseudo_header *psh=&tmp_header ;
|
||||||
|
|
||||||
psh->source_address = recv_info.src_ip;
|
psh->source_address = recv_info.new_src_ip.v4;
|
||||||
psh->dest_address = recv_info.dst_ip;
|
psh->dest_address = recv_info.new_dst_ip.v4;
|
||||||
psh->placeholder = 0;
|
psh->placeholder = 0;
|
||||||
psh->protocol = IPPROTO_TCP;
|
psh->protocol = IPPROTO_TCP;
|
||||||
psh->tcp_length = htons(ip_payloadlen);
|
psh->tcp_length = htons(ip_payloadlen);
|
||||||
@ -1615,29 +1654,29 @@ int recv_raw_tcp(raw_info_t &raw_info,char * &payload,int &payloadlen)
|
|||||||
{
|
{
|
||||||
if(raw_info.rst_received < max_rst_to_show)
|
if(raw_info.rst_received < max_rst_to_show)
|
||||||
{
|
{
|
||||||
mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
|
mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
|
||||||
}
|
}
|
||||||
else if(raw_info.rst_received == max_rst_to_show)
|
else if(raw_info.rst_received == max_rst_to_show)
|
||||||
{
|
{
|
||||||
mylog(log_warn,"[%s,%d]rst==1,cnt=%d >=max_rst_to_show, this log will be muted for current connection\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
|
mylog(log_warn,"[%s,%d]rst==1,cnt=%d >=max_rst_to_show, this log will be muted for current connection\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
|
mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(max_rst_to_show==0)
|
else if(max_rst_to_show==0)
|
||||||
{
|
{
|
||||||
mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
|
mylog(log_debug,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received);
|
mylog(log_warn,"[%s,%d]rst==1,cnt=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(max_rst_allowed>=0 && raw_info.rst_received==max_rst_allowed+1 )
|
if(max_rst_allowed>=0 && raw_info.rst_received==max_rst_allowed+1 )
|
||||||
{
|
{
|
||||||
mylog(log_warn,"[%s,%d]connection disabled because of rst_received=%d > max_rst_allow=%d\n",my_ntoa(recv_info.src_ip),recv_info.src_port,(int)raw_info.rst_received,(int)max_rst_allowed );
|
mylog(log_warn,"[%s,%d]connection disabled because of rst_received=%d > max_rst_allow=%d\n",recv_info.new_src_ip.get_str1(),recv_info.src_port,(int)raw_info.rst_received,(int)max_rst_allowed );
|
||||||
raw_info.disabled=1;
|
raw_info.disabled=1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1832,7 +1871,7 @@ int send_raw0(raw_info_t &raw_info,const char * payload,int payloadlen)
|
|||||||
|
|
||||||
packet_info_t &send_info=raw_info.send_info;
|
packet_info_t &send_info=raw_info.send_info;
|
||||||
packet_info_t &recv_info=raw_info.recv_info;
|
packet_info_t &recv_info=raw_info.recv_info;
|
||||||
mylog(log_trace,"send_raw : from %x %d to %x %d\n",send_info.src_ip,send_info.src_port,send_info.dst_ip,send_info.dst_port);
|
mylog(log_trace,"send_raw : from %s %d to %s %d\n",send_info.new_src_ip.get_str1(),send_info.src_port,send_info.new_dst_ip.get_str2(),send_info.dst_port);
|
||||||
switch(raw_mode)
|
switch(raw_mode)
|
||||||
{
|
{
|
||||||
case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
|
case mode_faketcp:return send_raw_tcp(raw_info,payload,payloadlen);
|
||||||
|
19
network.h
19
network.h
@ -50,11 +50,22 @@ struct pseudo_header {
|
|||||||
struct packet_info_t //todo change this to union
|
struct packet_info_t //todo change this to union
|
||||||
{
|
{
|
||||||
uint8_t protocol;
|
uint8_t protocol;
|
||||||
//ip_part:
|
|
||||||
u32_t src_ip;
|
|
||||||
uint16_t src_port;
|
|
||||||
|
|
||||||
u32_t dst_ip;
|
union tmp_ip_t
|
||||||
|
{
|
||||||
|
u32_t v4;
|
||||||
|
in6_addr v6;
|
||||||
|
|
||||||
|
bool equal (const tmp_ip_t &b) const;
|
||||||
|
|
||||||
|
char * get_str1() const;
|
||||||
|
char * get_str2() const;
|
||||||
|
}ip;
|
||||||
|
|
||||||
|
tmp_ip_t new_src_ip;
|
||||||
|
tmp_ip_t new_dst_ip;
|
||||||
|
|
||||||
|
uint16_t src_port;
|
||||||
uint16_t dst_port;
|
uint16_t dst_port;
|
||||||
|
|
||||||
//tcp_part:
|
//tcp_part:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user