From 11730a8cbfa51e2a11763f6c7e65924ad275f836 Mon Sep 17 00:00:00 2001 From: wangyu- Date: Fri, 13 Oct 2017 11:27:32 -0500 Subject: [PATCH] add xor and obscure --- main.cpp | 9 +++--- packet.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++----------- packet.h | 1 + 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/main.cpp b/main.cpp index 472b02e..a48b333 100644 --- a/main.cpp +++ b/main.cpp @@ -442,9 +442,10 @@ int client_event_loop() 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 ",data_len,mtu_warn); } - if(rm_crc32(data,data_len)!=0) + if(cook_rm_crc32(data,data_len)!=0) { - mylog(log_debug,"crc32 check error"); + mylog(log_debug,"cook_rm_crc32 check error"); + continue; } int out_n;char **out_arr;int *out_len;int *out_delay; @@ -601,9 +602,9 @@ int server_event_loop() } - if(rm_crc32(data,data_len)!=0) + if(cook_rm_crc32(data,data_len)!=0) { - mylog(log_debug,"crc32 check error"); + mylog(log_debug,"cook_rm_crc32 check error"); continue; } diff --git a/packet.cpp b/packet.cpp index 8aefd3a..655ab08 100644 --- a/packet.cpp +++ b/packet.cpp @@ -11,14 +11,18 @@ #include "packet.h" int iv_min=2; -int iv_max=16;//< 256; +int iv_max=18;//< 256; u64_t packet_send_count=0; u64_t dup_packet_send_count=0; u64_t packet_recv_count=0; u64_t dup_packet_recv_count=0; + typedef u64_t anti_replay_seq_t; int disable_replay_filter=0; +int disable_obscure=0; +int disable_xor=0; + int random_drop=0; char key_string[1000]= "secret key"; @@ -36,6 +40,7 @@ void encrypt_0(char * input,int &len,char *key) input[i]^=key[j]; } } + void decrypt_0(char * input,int &len,char *key) { @@ -47,7 +52,7 @@ void decrypt_0(char * input,int &len,char *key) input[i]^=key[j]; } } -int do_obscure(const char * input, int in_len,char *output,int &out_len) +int do_obscure_old(const char * input, int in_len,char *output,int &out_len) { //memcpy(output,input,in_len); // out_len=in_len; @@ -77,7 +82,42 @@ int do_obscure(const char * input, int in_len,char *output,int &out_len) out_len=iv_len+in_len+1; return 0; } -int de_obscure(const char * input, int in_len,char *output,int &out_len) + +int do_obscure(char * data,int &len) +{ + assert(len>=0); + assert(len=0); int i, crc; unsigned int byte, c; const unsigned int g0 = 0xEDB88320, g1 = g0>>1, @@ -220,6 +254,7 @@ unsigned int crc32h(unsigned char *message,int len) { int put_conv0(u32_t conv,const char * input,int len_in,char *&output,int &len_out) { + assert(len_in>=0); static char buf[buf_len]; output=buf; u32_t n_conv=htonl(conv); @@ -233,6 +268,7 @@ int put_conv0(u32_t conv,const char * input,int len_in,char *&output,int &len_ou } int get_conv0(u32_t &conv,const char *input,int len_in,char *&output,int &len_out ) { + assert(len_in>=0); u32_t n_conv; memcpy(&n_conv,input,sizeof(n_conv)); conv=ntohl(n_conv); @@ -255,14 +291,34 @@ int get_conv0(u32_t &conv,const char *input,int len_in,char *&output,int &len_ou } int put_crc32(char * s,int &len) { - if(len<0) return -1; + assert(len>=0); + //if(len<0) return -1; u32_t crc32=crc32h((unsigned char *)s,len); write_u32(s+len,crc32); len+=sizeof(u32_t); + + return 0; } + +int cook_rm_crc32(char * s,int &len) +{ + if(!disable_xor)decrypt_0(s,len,key_string); + if(!disable_obscure) + { + int ret=de_obscure(s,len); + if(ret!=0) + { + mylog(log_debug,"de_obscure fail\n"); + return ret; + } + } + return rm_crc32(s,len); +} int rm_crc32(char * s,int &len) { + assert(len>=0); + len-=sizeof(u32_t); if(len<0) return -1; u32_t crc32_in=read_u32(s+len); @@ -284,6 +340,7 @@ int put_conv(u32_t conv,const char * input,int len_in,char *&output,int &len_out memcpy(output,&n_conv,sizeof(n_conv)); memcpy(output+sizeof(n_conv),input,len_in); len_out=len_in+(int)(sizeof(n_conv)); + return 0; } int get_conv(u32_t &conv,const char *input,int len_in,char *&output,int &len_out ) diff --git a/packet.h b/packet.h index b628e73..4f98d80 100644 --- a/packet.h +++ b/packet.h @@ -41,4 +41,5 @@ 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 cook_rm_crc32(char * s,int &len); #endif /* PACKET_H_ */