mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
delete unused codes, reduce diff with non-mp version
This commit is contained in:
parent
a3535364fa
commit
6ccf6ce3dc
49
client.cpp
49
client.cpp
@ -496,11 +496,9 @@ int client_on_raw_recv(conn_info_t &conn_info) //called when raw fd received a p
|
||||
|
||||
if(ret<0)
|
||||
{
|
||||
mylog(log_warn,"sento returned %d,%s,%02x,%s\n",ret,strerror(errno),int(tmp_addr.get_type()),tmp_addr.get_str());
|
||||
mylog(log_warn,"sento returned %d,%s,%02x,%s\n",ret,get_sock_error(),int(tmp_addr.get_type()),tmp_addr.get_str());
|
||||
//perror("ret<0");
|
||||
}
|
||||
//mylog(log_trace,"%s :%d\n",inet_ntoa(tmp_sockaddr.sin_addr),ntohs(tmp_sockaddr.sin_port));
|
||||
mylog(log_trace,"%d byte sent\n",ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -525,7 +523,7 @@ int client_on_udp_recv(conn_info_t &conn_info)
|
||||
socklen_t udp_new_addr_len = sizeof(address_t::storage_t);
|
||||
if ((recv_len = recvfrom(udp_fd, buf, max_data_len+1, 0,
|
||||
(struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) {
|
||||
mylog(log_warn,"recv_from error,%s\n",strerror(errno));
|
||||
mylog(log_warn,"recv_from error,%s\n",get_sock_error());
|
||||
//myexit(1);
|
||||
};
|
||||
|
||||
@ -539,34 +537,9 @@ int client_on_udp_recv(conn_info_t &conn_info)
|
||||
{
|
||||
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 ",recv_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),recv_len);
|
||||
|
||||
/*
|
||||
if(udp_old_addr_in.sin_addr.s_addr==0&&udp_old_addr_in.sin_port==0)
|
||||
{
|
||||
memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
|
||||
}
|
||||
else if(udp_new_addr_in.sin_addr.s_addr!=udp_old_addr_in.sin_addr.s_addr
|
||||
||udp_new_addr_in.sin_port!=udp_old_addr_in.sin_port)
|
||||
{
|
||||
if(get_current_time()- last_udp_recv_time <udp_timeout)
|
||||
{
|
||||
printf("new <ip,port> connected in,ignored,bc last connection is still active\n");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("new <ip,port> connected in,accpeted\n");
|
||||
memcpy(&udp_old_addr_in,&udp_new_addr_in,sizeof(udp_new_addr_in));
|
||||
conv_id++;
|
||||
}
|
||||
}*/
|
||||
|
||||
//last_udp_recv_time=get_current_time();
|
||||
address_t tmp_addr;
|
||||
tmp_addr.from_sockaddr((sockaddr *)&udp_new_addr_in,udp_new_addr_len);
|
||||
//u64_t u64=((u64_t(udp_new_addr_in.sin_addr.s_addr))<<32u)+ntohs(udp_new_addr_in.sin_port);
|
||||
u32_t conv;
|
||||
|
||||
//u64_t u64;//////todo
|
||||
@ -590,14 +563,6 @@ int client_on_udp_recv(conn_info_t &conn_info)
|
||||
|
||||
if(conn_info.state.client_current_state==client_ready)
|
||||
{
|
||||
/*
|
||||
char buf2[6000];
|
||||
int ret1=send_raw(conn_info.raw_info,buf2,40);
|
||||
int ret2=send_raw(conn_info.raw_info,buf2,500);
|
||||
int ret3=send_raw(conn_info.raw_info,buf2,1000);
|
||||
int ret4=send_raw(conn_info.raw_info,buf2,2000);
|
||||
mylog(log_warn,"ret= %d %d %d %d\n",ret1,ret2,ret3,ret4);*/
|
||||
|
||||
send_data_safer(conn_info,buf,recv_len,conv);
|
||||
}
|
||||
return 0;
|
||||
@ -741,7 +706,6 @@ int client_event_loop()
|
||||
|
||||
|
||||
send_info.new_dst_ip.from_address_t(remote_addr);
|
||||
|
||||
send_info.dst_port=remote_addr.get_port();
|
||||
|
||||
|
||||
@ -749,7 +713,7 @@ int client_event_loop()
|
||||
set_buf_size(udp_fd,socket_buf_size);
|
||||
|
||||
|
||||
if (bind(udp_fd, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) == -1) {
|
||||
if (::bind(udp_fd, (struct sockaddr*) &local_addr.inner, local_addr.get_len()) == -1) {
|
||||
mylog(log_fatal,"socket bind error\n");
|
||||
//perror("socket bind error");
|
||||
myexit(1);
|
||||
@ -799,13 +763,6 @@ int client_event_loop()
|
||||
ev_io_init(&raw_recv_watcher, raw_recv_cb, raw_recv_fd, EV_READ);
|
||||
ev_io_start(loop, &raw_recv_watcher);
|
||||
|
||||
////add_timer for fake_tcp_keep_connection_client
|
||||
|
||||
//sleep(10);
|
||||
|
||||
//memset(&udp_old_addr_in,0,sizeof(sockaddr_in));
|
||||
int unbind=1;
|
||||
|
||||
//set_timer(epollfd,timer_fd);
|
||||
struct ev_timer clear_timer;
|
||||
|
||||
|
36
common.cpp
36
common.cpp
@ -10,7 +10,7 @@
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
static int random_number_fd=-1;
|
||||
//static int random_number_fd=-1;
|
||||
|
||||
int force_socket_buf=0;
|
||||
|
||||
@ -413,7 +413,7 @@ char * my_ntoa(u32_t ip)
|
||||
a.s_addr=ip;
|
||||
return inet_ntoa(a);
|
||||
}
|
||||
|
||||
/*
|
||||
void init_random_number_fd()
|
||||
{
|
||||
|
||||
@ -425,11 +425,30 @@ void init_random_number_fd()
|
||||
myexit(-1);
|
||||
}
|
||||
setnonblocking(random_number_fd);
|
||||
}*/
|
||||
struct random_fd_t
|
||||
{
|
||||
int random_number_fd;
|
||||
random_fd_t()
|
||||
{
|
||||
random_number_fd=open("/dev/urandom",O_RDONLY);
|
||||
|
||||
if(random_number_fd==-1)
|
||||
{
|
||||
mylog(log_fatal,"error open /dev/urandom\n");
|
||||
myexit(-1);
|
||||
}
|
||||
setnonblocking(random_number_fd);
|
||||
}
|
||||
int get_fd()
|
||||
{
|
||||
return random_number_fd;
|
||||
}
|
||||
}random_fd;
|
||||
u64_t get_true_random_number_64()
|
||||
{
|
||||
u64_t ret;
|
||||
int size=read(random_number_fd,&ret,sizeof(ret));
|
||||
int size=read(random_fd.get_fd(),&ret,sizeof(ret));
|
||||
if(size!=sizeof(ret))
|
||||
{
|
||||
mylog(log_fatal,"get random number failed %d\n",size);
|
||||
@ -441,7 +460,7 @@ u64_t get_true_random_number_64()
|
||||
u32_t get_true_random_number()
|
||||
{
|
||||
u32_t ret;
|
||||
int size=read(random_number_fd,&ret,sizeof(ret));
|
||||
int size=read(random_fd.get_fd(),&ret,sizeof(ret));
|
||||
if(size!=sizeof(ret))
|
||||
{
|
||||
mylog(log_fatal,"get random number failed %d\n",size);
|
||||
@ -518,6 +537,7 @@ u64_t read_u64(char * s)
|
||||
}
|
||||
|
||||
void setnonblocking(int sock) {
|
||||
#if !defined(__MINGW32__)
|
||||
int opts;
|
||||
opts = fcntl(sock, F_GETFL);
|
||||
|
||||
@ -532,9 +552,17 @@ void setnonblocking(int sock) {
|
||||
//perror("fcntl(sock,SETFL,opts)");
|
||||
myexit(1);
|
||||
}
|
||||
#else
|
||||
int iResult;
|
||||
u_long iMode = 1;
|
||||
iResult = ioctlsocket(sock, FIONBIO, &iMode);
|
||||
if (iResult != NO_ERROR)
|
||||
printf("ioctlsocket failed with error: %d\n", iResult);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Generic checksum calculation function
|
||||
*/
|
||||
|
3
main.cpp
3
main.cpp
@ -53,6 +53,7 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
|
||||
pre_process_arg(argc,argv);
|
||||
|
||||
if(program_mode==client_mode)
|
||||
{
|
||||
struct ev_loop* loop=ev_default_loop(0);
|
||||
@ -93,7 +94,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
mylog(log_info,"remote_ip=[%s], make sure this is a vaild IP address\n",remote_addr.get_ip());
|
||||
|
||||
init_random_number_fd();
|
||||
//init_random_number_fd();
|
||||
srand(get_true_random_number_nz());
|
||||
const_id=get_true_random_number_nz();
|
||||
|
||||
|
@ -247,10 +247,10 @@ 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);
|
||||
|
||||
int get_src_adress(u32_t &ip,u32_t remote_ip_uint32,int remote_port); //a trick to get src adress for a dest adress,so that we can use the src address in raw socket as source ip
|
||||
|
||||
int get_src_adress2(address_t &output_addr,address_t remote_addr);
|
||||
|
||||
int try_to_list_and_bind(int & bind_fd,u32_t local_ip_uint32,int port); //try to bind to a port,may fail.
|
||||
int try_to_list_and_bind2(int &fd,address_t address);
|
||||
|
||||
int client_bind_to_a_new_port(int & bind_fd,u32_t local_ip_uint32);//find a free port and bind to it.
|
||||
int client_bind_to_a_new_port2(int &fd,const address_t& address);
|
||||
|
Loading…
x
Reference in New Issue
Block a user