new function tcp csum

This commit is contained in:
wangyu- 2017-10-31 19:43:25 -05:00
parent 2dbb26e394
commit b0614beffa
4 changed files with 62 additions and 5 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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");

4
misc.h
View File

@ -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);