From bc04a6898d72d22bea102321bcb8e8163cff4a89 Mon Sep 17 00:00:00 2001 From: wangyu Date: Fri, 4 Aug 2017 12:46:46 +0800 Subject: [PATCH] ack increase monotonically --- common.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++- common.h | 3 ++- main.cpp | 1 + network.cpp | 6 ++++-- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/common.cpp b/common.cpp index 7c637ae..c638a27 100644 --- a/common.cpp +++ b/common.cpp @@ -14,6 +14,7 @@ unordered_map raw_mode_tostring = {{mode_faketcp, "faketcp"}, int socket_buf_size=1024*1024; static int random_number_fd=-1; char iptables_rule[200]; +program_mode_t program_mode=unset_mode;//0 unset; 1client 2server uint64_t get_current_time() { @@ -218,5 +219,64 @@ int char_to_numbers(const char * data,int len,id_t &id1,id_t &id2,id_t &id3) return 0; } +bool larger_than_u32(uint32_t a,uint32_t b) +{ -program_mode_t program_mode=unset_mode;//0 unset; 1client 2server + uint32_t smaller,bigger; + smaller=min(a,b);//smaller in normal sense + bigger=max(a,b); + uint32_t distance=min(bigger-smaller,smaller+(0xffffffff-bigger+1)); + if(distance==bigger-smaller) + { + if(bigger==a) + { + return 1; + } + else + { + return 0; + } + } + else + { + if(smaller==b) + { + return 0; + } + else + { + return 1; + } + } +} + +bool larger_than_u16(uint16_t a,uint16_t b) +{ + + uint16_t smaller,bigger; + smaller=min(a,b);//smaller in normal sense + bigger=max(a,b); + uint16_t distance=min(bigger-smaller,smaller+(0xffff-bigger+1)); + if(distance==bigger-smaller) + { + if(bigger==a) + { + return 1; + } + else + { + return 0; + } + } + else + { + if(smaller==b) + { + return 0; + } + else + { + return 1; + } + } +} diff --git a/common.h b/common.h index 3ec9d83..398bb35 100644 --- a/common.h +++ b/common.h @@ -117,7 +117,8 @@ uint32_t get_true_random_number(); uint32_t get_true_random_number_nz(); uint64_t ntoh64(uint64_t a); uint64_t hton64(uint64_t a); - +bool larger_than_u16(uint16_t a,uint16_t b); +bool larger_than_u32(uint32_t a,uint32_t b); void setnonblocking(int sock); int set_buf_size(int fd); diff --git a/main.cpp b/main.cpp index 8af5e7c..9becd66 100755 --- a/main.cpp +++ b/main.cpp @@ -2763,6 +2763,7 @@ void iptables_warn() } int main(int argc, char *argv[]) { + //printf("%d %d %d %d",larger_than_u32(1,2),larger_than_u32(2,1),larger_than_u32(0xeeaaeebb,2),larger_than_u32(2,0xeeaaeebb)); //assert(0==1); dup2(1, 2);//redirect stderr to stdout signal(SIGINT, INThandler); diff --git a/network.cpp b/network.cpp index d6bd409..de4ef05 100644 --- a/network.cpp +++ b/network.cpp @@ -1318,14 +1318,16 @@ int after_recv_raw0(raw_info_t &raw_info) send_info.ts_ack=recv_info.ts; if (recv_info.syn == 0 && recv_info.ack == 1 && raw_info.last_recv_len != 0) //only modify send_info when the packet is not part of handshake { - send_info.ack_seq = recv_info.seq+raw_info.last_recv_len;//TODO only update if its larger + if(larger_than_u32(recv_info.seq+raw_info.last_recv_len,send_info.ack_seq)) + send_info.ack_seq = recv_info.seq+raw_info.last_recv_len;//TODO only update if its larger } } if(raw_mode==mode_icmp) { if(program_mode==server_mode) { - send_info.icmp_seq = recv_info.icmp_seq; //TODO only update if its larger + if(larger_than_u16(recv_info.icmp_seq,send_info.icmp_seq)) + send_info.icmp_seq = recv_info.icmp_seq; //TODO only update if its larger } } return 0;