From b00d8e6927e3fa9ad06cb52fe613c3af86fe416b Mon Sep 17 00:00:00 2001 From: root Date: Thu, 5 Jul 2018 07:53:19 +0000 Subject: [PATCH] add expire time for anti_replay --- common.cpp | 5 +++++ common.h | 1 + fec_manager.h | 36 ++++++++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/common.cpp b/common.cpp index 2cb0953..b1631d9 100644 --- a/common.cpp +++ b/common.cpp @@ -211,6 +211,11 @@ u64_t get_current_time()//ms return (u64_t)(ev_time()*1000); } +u64_t get_current_time_rough()//ms +{ + return (u64_t)(ev_now(ev_default_loop(0))*1000); +} + u64_t get_current_time_us() { //timespec tmp_time; diff --git a/common.h b/common.h index 31ff2a1..697b92d 100644 --- a/common.h +++ b/common.h @@ -220,6 +220,7 @@ struct pseudo_header { }; u64_t get_current_time(); +u64_t get_current_time_rough(); u64_t get_current_time_us(); u64_t pack_u64(u32_t a,u32_t b); diff --git a/fec_manager.h b/fec_manager.h index bbb5ce5..46ef704 100644 --- a/fec_manager.h +++ b/fec_manager.h @@ -29,12 +29,19 @@ extern int g_fec_mode; extern int dynamic_update_fec; /*end for first time init or dynamic update*/ +const int anti_replay_timeout=60*1000;// 60s struct anti_replay_t { + struct info_t + { + my_time_t my_time; + int index; + }; + u64_t replay_buffer[anti_replay_buff_size]; - unordered_set st; + unordered_map mp; int index; anti_replay_t() { @@ -43,34 +50,47 @@ struct anti_replay_t int clear() { memset(replay_buffer,-1,sizeof(replay_buffer)); - st.clear(); - st.rehash(anti_replay_buff_size*3); + mp.clear(); + mp.rehash(anti_replay_buff_size*3); index=0; return 0; } void set_invaild(u32_t seq) { - if(st.find(seq)!=st.end() ) + if(is_vaild(seq)==0) { mylog(log_trace,"seq %u exist\n",seq); + assert(mp.find(seq)!=mp.end()); + mp[seq].my_time=get_current_time_rough(); return; //return 0; } if(replay_buffer[index]!=u64_t(i64_t(-1))) { - assert(st.find(replay_buffer[index])!=st.end()); - st.erase(replay_buffer[index]); + assert(mp.find(replay_buffer[index])!=mp.end()); + mp.erase(replay_buffer[index]); } replay_buffer[index]=seq; - st.insert(seq); + assert(mp.find(seq)==mp.end()); + mp[seq].my_time=get_current_time_rough(); + mp[seq].index=index; index++; if(index==int(anti_replay_buff_size)) index=0; //return 1; //for complier check } int is_vaild(u32_t seq) { - return st.find(seq)==st.end(); + if(mp.find(seq)==mp.end()) return 1; + + if(get_current_time_rough()-mp[seq].my_time>anti_replay_timeout) + { + replay_buffer[mp[seq].index]=u64_t(i64_t(-1)); + mp.erase(seq); + return 1; + } + + return 0; } };