diff --git a/misc.cpp b/misc.cpp index 09daa45..0a1a6b2 100644 --- a/misc.cpp +++ b/misc.cpp @@ -173,7 +173,11 @@ int from_fec_to_normal(conn_info_t & conn_info,char *data,int len,int & out_n,ch inner_stat.output_packet_num++; inner_stat.output_packet_size+=len; - if(data==0) return 0; + if(data==0) + { + out_n=0; + return 0; + } out_n=1; static char *data_static; data_static=data; @@ -880,11 +884,11 @@ void process_arg(int argc, char *argv[]) mylog(log_info,"fifo_file =%s \n",fifo_file); } - else if(strcmp(long_options[option_index].name,"tun-dev")==0) + else if(strcmp(long_options[option_index].name,"sub-net")==0) { - //sscanf(optarg,"%s",fifo_file); - mylog(log_info,"enabled tun-dev mode\n"); - working_mode=tun_dev_mode; + sscanf(optarg,"%s",sub_net); + mylog(log_info,"sub_net %s\n",sub_net); + } else if(strcmp(long_options[option_index].name,"tun-dev")==0) { @@ -894,6 +898,7 @@ void process_arg(int argc, char *argv[]) mylog(log_info,"tun_dev =%s \n",tun_dev); } mylog(log_info,"running at tun-dev mode\n"); + working_mode=tun_dev_mode; } else { diff --git a/tun_dev.cpp b/tun_dev.cpp index 504325c..2282ef8 100644 --- a/tun_dev.cpp +++ b/tun_dev.cpp @@ -48,38 +48,27 @@ int set_if(char *if_name,u32_t local_ip,u32_t remote_ip,int mtu) sai.sin_family = AF_INET; sai.sin_port = 0; - - sai.sin_addr.s_addr = local_ip; memcpy(&ifr.ifr_addr,&sai, sizeof(struct sockaddr)); - assert(ioctl(sockfd, SIOCSIFADDR, &ifr)==0); - - - - //sai.sin_addr.s_addr = local_ip; - //memcpy(&ifr.ifr_addr,&sai, sizeof(struct sockaddr)); - //assert(ioctl(sockfd, SIOCSIFADDR, &ifr)==0); - - + assert(ioctl(sockfd, SIOCSIFADDR, &ifr)==0); //set source ip sai.sin_addr.s_addr = remote_ip; memcpy(&ifr.ifr_addr,&sai, sizeof(struct sockaddr)); - assert(ioctl(sockfd, SIOCSIFDSTADDR, &ifr)==0); + assert(ioctl(sockfd, SIOCSIFDSTADDR, &ifr)==0);//set dest ip ifr.ifr_mtu=mtu; - assert(ioctl(sockfd, SIOCSIFMTU, &ifr)==0); + assert(ioctl(sockfd, SIOCSIFMTU, &ifr)==0);//set mtu assert(ioctl(sockfd, SIOCGIFFLAGS, &ifr)==0); // ifr.ifr_flags |= ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST ); - ifr.ifr_flags |= ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST ); + ifr.ifr_flags = ( IFF_UP|IFF_POINTOPOINT|IFF_RUNNING|IFF_NOARP|IFF_MULTICAST );//set interface flags assert(ioctl(sockfd, SIOCSIFFLAGS, &ifr)==0); //printf("i m here2\n"); return 0; } -//enum tun_header_t {header_reserved=0,header_normal=1,header_new=2,header_reject=3}; const char header_normal=1; const char header_new_connect=2; const char header_reject=3; @@ -89,9 +78,6 @@ int put_header(char header,char * data,int &len) assert(len>=0); data[len]=header; len+=1; - //data=data-1; - //data[0]=header; - //len+=1; return 0; } int get_header(char &header,char * data,int &len) @@ -111,15 +97,15 @@ int from_normal_to_fec2(conn_info_t & conn_info,dest_t &dest,char * data,int len for(int i=0;i0); assert(new_listen_socket(local_listen_fd,local_ip_uint32,local_port)==0); - // local_listen_fd64=fd_manager.create(local_listen_fd); - - //assert(set_if("tun11","10.0.0.1","10.0.0.2",1000)==0); assert(set_if(tun_dev,htonl((ntohl(sub_net_uint32)&0xFFFFFF00)|1),htonl((ntohl(sub_net_uint32)&0xFFFFFF00 )|2),g_fec_mtu)==0); epoll_fd = epoll_create1(0); @@ -464,23 +432,20 @@ int tun_dev_server_event_loop() } - - conn_info_t *conn_info_p=new conn_info_t; conn_info_t &conn_info=*conn_info_p; //huge size of conn_info,do not allocate on stack - u64_t fd64=conn_info.fec_encode_manager.get_timer_fd64(); + u64_t tmp_timer_fd64=conn_info.fec_encode_manager.get_timer_fd64(); ev.events = EPOLLIN; - ev.data.u64 = fd64; + ev.data.u64 = tmp_timer_fd64; mylog(log_debug,"conn_info.fec_encode_manager.get_timer_fd64()=%llu\n",conn_info.fec_encode_manager.get_timer_fd64()); - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(fd64), &ev); + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(tmp_timer_fd64), &ev); if (ret!= 0) { mylog(log_fatal,"add fec_encode_manager.get_timer_fd64() error\n"); myexit(-1); } - //my_timer_t timer; conn_info.timer.add_fd_to_epoll(epoll_fd); conn_info.timer.set_timer_repeat_us(timer_interval*1000); @@ -549,8 +514,9 @@ int tun_dev_server_event_loop() } else if(events[idx].data.u64==conn_info.fec_encode_manager.get_timer_fd64()) { + assert(udp_dest.inner.fd64_ip_port.ip_port.to_u64()!=0); mylog(log_trace,"events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()\n"); - + uint64_t fd64=events[idx].data.u64; //mylog(log_info,"timer!!!\n"); uint64_t value; if(!fd_manager.exist(fd64)) //fd64 has been closed @@ -653,7 +619,11 @@ int tun_dev_server_event_loop() else if(events[idx].data.u64==(u64_t)tun_fd) { len=read(tun_fd,data,max_data_len); - assert(len>=0); + if(len<0) + { + mylog(log_warn,"read from tun_fd return %d,errno=%s\n",len,strerror(errno)); + continue; + } mylog(log_trace,"Received packet from tun,len: %d\n",len); @@ -686,7 +656,7 @@ int tun_dev_server_event_loop() } else { - //assert(0==1); + assert(0==1); } } delay_manager.check(); diff --git a/tunnel.cpp b/tunnel.cpp index d90d17f..bbb2eef 100644 --- a/tunnel.cpp +++ b/tunnel.cpp @@ -70,12 +70,12 @@ int tunnel_client_event_loop() myexit(-1); } - u64_t fd64=conn_info.fec_encode_manager.get_timer_fd64(); + u64_t tmp_fd64=conn_info.fec_encode_manager.get_timer_fd64(); ev.events = EPOLLIN; - ev.data.u64 = fd64; + ev.data.u64 = tmp_fd64; mylog(log_debug,"conn_info.fec_encode_manager.get_timer_fd64()=%llu\n",conn_info.fec_encode_manager.get_timer_fd64()); - ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(fd64), &ev); + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd_manager.to_fd(tmp_fd64), &ev); if (ret!= 0) { mylog(log_fatal,"add fec_encode_manager.get_timer_fd64() error\n"); myexit(-1); @@ -173,6 +173,7 @@ int tunnel_client_event_loop() if(events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()) { + fd64_t fd64=events[idx].data.u64; mylog(log_trace,"events[idx].data.u64 == conn_info.fec_encode_manager.get_timer_fd64()\n"); //mylog(log_info,"timer!!!\n");