From b0614beffae0c6590f34d59def2a3044ede27b65 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Tue, 31 Oct 2017 19:43:25 -0500 Subject: [PATCH] new function tcp csum --- common.cpp | 33 +++++++++++++++++++++++++++++++++ common.h | 8 ++++++++ misc.cpp | 22 +++++++++++++++++----- misc.h | 4 ++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/common.cpp b/common.cpp index eccd4d8..2af6699 100644 --- a/common.cpp +++ b/common.cpp @@ -260,6 +260,39 @@ unsigned short csum(const unsigned short *ptr,int nbytes) { return(answer); } + + +unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes) {//works both for big and little endian + + register long sum; + unsigned short oddbyte; + register short answer; + + sum=0; + unsigned short * tmp= (unsigned short *)&ph; + for(int i=0;i<6;i++) + { + sum+=*tmp++; + } + + + while(nbytes>1) { + sum+=*ptr++; + nbytes-=2; + } + if(nbytes==1) { + oddbyte=0; + *((u_char*)&oddbyte)=*(u_char*)ptr; + sum+=oddbyte; + } + + sum = (sum>>16)+(sum & 0xffff); + sum = sum + (sum>>16); + answer=(short)~sum; + + return(answer); +} + int set_buf_size(int fd,int socket_buf_size,int force_socket_buf) { if(force_socket_buf) diff --git a/common.h b/common.h index ee4267e..403b0e4 100644 --- a/common.h +++ b/common.h @@ -176,6 +176,13 @@ struct fd_info_t ip_port_t ip_port; }; +struct pseudo_header { + u_int32_t source_address; + u_int32_t dest_address; + u_int8_t placeholder; + u_int8_t protocol; + u_int16_t tcp_length; +}; u64_t get_current_time(); u64_t get_current_time_us(); @@ -209,6 +216,7 @@ void setnonblocking(int sock); int set_buf_size(int fd,int socket_buf_size,int force_socket_buf=0); unsigned short csum(const unsigned short *ptr,int nbytes); +unsigned short tcp_csum(const pseudo_header & ph,const unsigned short *ptr,int nbytes); void signal_handler(int sig); int numbers_to_char(id_t id1,id_t id2,id_t id3,char * &data,int &len); diff --git a/misc.cpp b/misc.cpp index c8182d2..c387599 100644 --- a/misc.cpp +++ b/misc.cpp @@ -48,7 +48,9 @@ char tun_dev[100]=""; int keep_reconnect=0; +int tun_mtu=1500; +int mssfix=1; int from_normal_to_fec(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay) @@ -571,6 +573,8 @@ void process_arg(int argc, char *argv[]) {"fifo", required_argument, 0, 1}, {"sub-net", required_argument, 0, 1}, {"tun-dev", required_argument, 0, 1}, + {"tun-mtu", required_argument, 0, 1}, + {"disable-mssfix", no_argument, 0, 1}, {"keep-reconnect", no_argument, 0, 1}, {NULL, 0, 0, 0} }; @@ -886,14 +890,22 @@ void process_arg(int argc, char *argv[]) } else if(strcmp(long_options[option_index].name,"tun-dev")==0) { - if(optarg!=0) - { - sscanf(optarg,"%s",tun_dev); - mylog(log_info,"tun_dev=%s\n",tun_dev); - } + sscanf(optarg,"%s",tun_dev); + mylog(log_info,"tun_dev=%s\n",tun_dev); + mylog(log_info,"running at tun-dev mode\n"); working_mode=tun_dev_mode; } + else if(strcmp(long_options[option_index].name,"tun-mtu")==0) + { + sscanf(optarg,"%d",&tun_mtu); + mylog(log_warn,"changed tun_mtu,tun_mtu=%d\n",tun_mtu); + } + else if(strcmp(long_options[option_index].name,"disable-mssfix")==0) + { + mssfix=0; + mylog(log_warn,"mssfix disabled\n"); + } else { mylog(log_fatal,"unknown option\n"); diff --git a/misc.h b/misc.h index 7ad236b..91322fa 100644 --- a/misc.h +++ b/misc.h @@ -48,6 +48,10 @@ extern int delay_capacity; extern int keep_reconnect; +extern int tun_mtu; + +extern int mssfix; + int from_normal_to_fec(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay); int from_fec_to_normal(conn_info_t & conn_info,char *data,int len,int & out_n,char **&out_arr,int *&out_len,my_time_t *&out_delay);