From 73d68a2bafad19de6760ab73e19d5dc0441e01bf Mon Sep 17 00:00:00 2001 From: wangyu- Date: Fri, 27 Oct 2017 05:23:46 -0500 Subject: [PATCH] just commit --- packet.cpp | 12 +++-- packet.h | 1 + tun_dev.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 136 insertions(+), 6 deletions(-) diff --git a/packet.cpp b/packet.cpp index d6c0599..6b133d1 100644 --- a/packet.cpp +++ b/packet.cpp @@ -177,9 +177,7 @@ int my_send(const dest_t &dest,char *data,int len) { if(dest.cook) { - put_crc32(data,len); - if(!disable_obscure)do_obscure(data,len); - if(!disable_xor)encrypt_0(data,len,key_string); + do_cook(data,len); } switch(dest.type) { @@ -322,6 +320,14 @@ int put_crc32(char * s,int &len) return 0; } +int do_cook(char * data,int &len) +{ + put_crc32(data,len); + if(!disable_obscure)do_obscure(data,len); + if(!disable_xor)encrypt_0(data,len,key_string); + return 0; +} + int de_cook(char * s,int &len) { if(!disable_xor)decrypt_0(s,len,key_string); diff --git a/packet.h b/packet.h index 99fb288..3693660 100644 --- a/packet.h +++ b/packet.h @@ -42,5 +42,6 @@ int put_conv(u32_t conv,const char * input,int len_in,char *&output,int &len_out int get_conv(u32_t &conv,const char *input,int len_in,char *&output,int &len_out ); int put_crc32(char * s,int &len); int rm_crc32(char * s,int &len); +int do_cook(char * data,int &len); int de_cook(char * s,int &len); #endif /* PACKET_H_ */ diff --git a/tun_dev.cpp b/tun_dev.cpp index d2bbb30..f0a3aee 100644 --- a/tun_dev.cpp +++ b/tun_dev.cpp @@ -145,6 +145,33 @@ int tun_dev_client_event_loop() myexit(-1); } + + ev.events = EPOLLIN; + ev.data.u64 = delay_manager.get_timer_fd(); + + mylog(log_debug,"delay_manager.get_timer_fd()=%d\n",delay_manager.get_timer_fd()); + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev); + if (ret!= 0) { + mylog(log_fatal,"add delay_manager.get_timer_fd() error\n"); + myexit(-1); + } + + int fifo_fd=-1; + + if(fifo_file[0]!=0) + { + fifo_fd=create_fifo(fifo_file); + ev.events = EPOLLIN; + ev.data.u64 = fifo_fd; + + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev); + if (ret!= 0) { + mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno)); + myexit(-1); + } + mylog(log_info,"fifo_file=%s\n",fifo_file); + } + dest_t dest; dest.type=type_fd64; dest.inner.fd64=remote_fd64; @@ -188,7 +215,9 @@ int tun_dev_client_event_loop() else put_header(header_normal,data,len); - delay_manager.add(0,dest,data,len);; + do_cook(data,len); + + delay_manager.add(0,dest,data,len); } else if(events[idx].data.u64==(u64_t)remote_fd64) { @@ -203,6 +232,14 @@ int tun_dev_client_event_loop() continue; } + if(de_cook(data,len)<0) + { + mylog(log_warn,"de_cook(data,len)failed \n"); + continue; + + } + + char header=0; if(get_header(header,data,len)!=0) { @@ -230,6 +267,30 @@ int tun_dev_client_event_loop() assert(write(tun_fd,data,len)>=0); } + else if (events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()) + { + uint64_t value; + read(delay_manager.get_timer_fd(), &value, 8); + mylog(log_trace,"events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()\n"); + //printf("",delay_mp.size()); + //fflush(stdout); + } + else if (events[idx].data.u64 == (u64_t)fifo_fd) + { + char buf[buf_len]; + int len=read (fifo_fd, buf, sizeof (buf)); + if(len<0) + { + mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno)); + continue; + } + buf[len]=0; + handle_command(buf); + } + else + { + assert(0==1); + } } delay_manager.check(); } @@ -283,6 +344,32 @@ int tun_dev_server_event_loop() myexit(-1); } + ev.events = EPOLLIN; + ev.data.u64 = delay_manager.get_timer_fd(); + + mylog(log_debug,"delay_manager.get_timer_fd()=%d\n",delay_manager.get_timer_fd()); + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, delay_manager.get_timer_fd(), &ev); + if (ret!= 0) { + mylog(log_fatal,"add delay_manager.get_timer_fd() error\n"); + myexit(-1); + } + + int fifo_fd=-1; + + if(fifo_file[0]!=0) + { + fifo_fd=create_fifo(fifo_file); + ev.events = EPOLLIN; + ev.data.u64 = fifo_fd; + + ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fifo_fd, &ev); + if (ret!= 0) { + mylog(log_fatal,"add fifo_fd to epoll error %s\n",strerror(errno)); + myexit(-1); + } + mylog(log_info,"fifo_file=%s\n",fifo_file); + } + //ip_port_t dest_ip_port; dest_t dest; @@ -322,11 +409,20 @@ int tun_dev_server_event_loop() struct sockaddr_in udp_new_addr_in={0}; socklen_t udp_new_addr_len = sizeof(sockaddr_in); if ((len = recvfrom(local_listen_fd, data, max_data_len, 0, - (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) == -1) { + (struct sockaddr *) &udp_new_addr_in, &udp_new_addr_len)) < 0) { mylog(log_error,"recv_from error,this shouldnt happen,err=%s,but we can try to continue\n",strerror(errno)); continue; //myexit(1); }; + + + if(de_cook(data,len)<0) + { + mylog(log_warn,"de_cook(data,len)failed \n"); + continue; + + } + char header=0; if(get_header(header,data,len)!=0) { @@ -359,6 +455,8 @@ int tun_dev_server_event_loop() len=1; data[0]=header_reject; + do_cook(data,len); + dest_t tmp_dest; tmp_dest.type=type_fd_ip_port; @@ -398,9 +496,34 @@ int tun_dev_server_event_loop() put_header(header_normal,data,len); + do_cook(data,len); + delay_manager.add(0,dest,data,len);; - + } + else if (events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()) + { + uint64_t value; + read(delay_manager.get_timer_fd(), &value, 8); + mylog(log_trace,"events[idx].data.u64 == (u64_t)delay_manager.get_timer_fd()\n"); + //printf("",delay_mp.size()); + //fflush(stdout); + } + else if (events[idx].data.u64 == (u64_t)fifo_fd) + { + char buf[buf_len]; + int len=read (fifo_fd, buf, sizeof (buf)); + if(len<0) + { + mylog(log_warn,"fifo read failed len=%d,errno=%s\n",len,strerror(errno)); + continue; + } + buf[len]=0; + handle_command(buf); + } + else + { + assert(0==1); } } delay_manager.check();