From 6ccf6ce3dc7bde9961bd7f8aae56e853a7cec72b Mon Sep 17 00:00:00 2001 From: wangyu- Date: Wed, 29 Aug 2018 14:26:34 -0500 Subject: [PATCH] delete unused codes, reduce diff with non-mp version --- client.cpp | 49 +++---------------------------------------------- common.cpp | 38 +++++++++++++++++++++++++++++++++----- main.cpp | 3 ++- network.h | 2 +- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/client.cpp b/client.cpp index 8539616..5ef323a 100644 --- a/client.cpp +++ b/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 connected in,ignored,bc last connection is still active\n"); - continue; - } - else - { - printf("new 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; diff --git a/common.cpp b/common.cpp index 4aa39ba..e5dcf05 100644 --- a/common.cpp +++ b/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 */ diff --git a/main.cpp b/main.cpp index bbf5088..d61fc4f 100755 --- a/main.cpp +++ b/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(); diff --git a/network.h b/network.h index 55acabb..5d925e1 100644 --- a/network.h +++ b/network.h @@ -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);