From 78f9aa6601f8f09979b8001397526d0f33b7a36b Mon Sep 17 00:00:00 2001 From: liuxueli Date: Tue, 6 Jun 2023 17:07:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0L7=20protocol=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E7=BB=93=E6=9E=9C=E9=80=9A=E7=9F=A5=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tsg_entry.cpp | 17 ++++++++++------- test/src/gtest_common.cpp | 13 ++++++++++++- test/src/gtest_common.h | 4 ++++ test/src/gtest_master.cpp | 34 ++++++++++++++++++++++++++++++++++ test/src/gtest_protocol.cpp | 13 ++++++++++++- 5 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/tsg_entry.cpp b/src/tsg_entry.cpp index 5abf504..f26f705 100644 --- a/src/tsg_entry.cpp +++ b/src/tsg_entry.cpp @@ -1128,15 +1128,18 @@ int session_application_metrics_update(const struct streaminfo *a_stream, struct static int session_app_gather_results_set_l7_protocol(const struct streaminfo *a_stream, enum TSG_PROTOCOL protocol) { struct gather_app_result *gather_result=(struct gather_app_result *)session_gather_app_results_get(a_stream); - if(gather_result!=NULL) + if(gather_result==NULL) + { + gather_result=(struct gather_app_result *)dictator_malloc(a_stream->threadnum, sizeof(struct gather_app_result)*ORIGIN_MAX); + memset(gather_result, 0, sizeof(struct gather_app_result)*ORIGIN_MAX); + session_gather_app_results_async(a_stream, (void *)gather_result); + } + + if(gather_result[ORIGIN_BASIC_PROTOCOL].app_num>0) { return 0; } - gather_result=(struct gather_app_result *)dictator_malloc(a_stream->threadnum, sizeof(struct gather_app_result)*ORIGIN_MAX); - memset(gather_result, 0, sizeof(struct gather_app_result)*ORIGIN_MAX); - session_gather_app_results_async(a_stream, (void *)gather_result); - int app_id=tsg_l7_protocol_name2id(g_tsg_proto_name2id[protocol].name); if(app_id>0) { @@ -1530,7 +1533,7 @@ struct maat_rule *matched_rules_decision_criteria(struct maat_rule *rules, size_ return p_result; } -static int session_l7_protocol_identify(const struct streaminfo *a_stream, struct session_runtime_process_context *srt_process_context, void *a_packet) +int session_l7_protocol_identify(const struct streaminfo *a_stream, struct session_runtime_process_context *srt_process_context, void *a_packet) { int ret=0; @@ -2043,7 +2046,7 @@ int session_flags_identify_result_cb(const struct streaminfo *a_stream, int brid return 0; } -static size_t session_pending_state_deal(const struct streaminfo *a_stream, struct session_runtime_process_context *srt_process_context, struct maat_rule *results, int n_results, void *a_packet) +size_t session_pending_state_deal(const struct streaminfo *a_stream, struct session_runtime_process_context *srt_process_context, struct maat_rule *results, int n_results, void *a_packet) { size_t hit_num=0; int ret=session_l7_protocol_identify(a_stream, srt_process_context, a_packet); diff --git a/test/src/gtest_common.cpp b/test/src/gtest_common.cpp index 71eebfd..f3e3718 100644 --- a/test/src/gtest_common.cpp +++ b/test/src/gtest_common.cpp @@ -250,9 +250,20 @@ extern "C" int ftp_control_identify(struct streaminfo *a_tcp) return 0; } +int g_gtest_ftp_data_flag=0; +void gtest_set_ftp_data_flag(void) +{ + g_gtest_ftp_data_flag=1; +} + +void gtest_clean_ftp_data_flag(void) +{ + g_gtest_ftp_data_flag=0; +} + extern "C" int ftp_data_identify(struct streaminfo *a_tcp) { - return 0; + return g_gtest_ftp_data_flag; } extern "C" int mail_protocol_identify_by_first_payload(struct streaminfo *a_tcp, char *payload, int payload_len, int thread_seq) diff --git a/test/src/gtest_common.h b/test/src/gtest_common.h index 31dd827..e32ab01 100644 --- a/test/src/gtest_common.h +++ b/test/src/gtest_common.h @@ -9,4 +9,8 @@ int TLD_convert_json(struct TLD_handle_t *_handle, char *buff, unsigned int buff int set_app_id(struct tsg_log_instance_t *_instance, struct TLD_handle_t *_handle, const struct streaminfo *a_stream); int set_shaping_rule_ids(struct tsg_log_instance_t *_instance, struct TLD_handle_t *_handle, const struct streaminfo *a_stream); +void gtest_set_ftp_data_flag(void); +void gtest_clean_ftp_data_flag(void); +extern "C" int ftp_data_identify(struct streaminfo *a_tcp); + diff --git a/test/src/gtest_master.cpp b/test/src/gtest_master.cpp index 90febc9..cd95e95 100644 --- a/test/src/gtest_master.cpp +++ b/test/src/gtest_master.cpp @@ -968,6 +968,40 @@ TEST(TSGMaster, ShapingPolicySendLog) EXPECT_EQ(nullptr, hited_shaping); } +extern size_t session_pending_state_deal(const struct streaminfo *a_stream, struct session_runtime_process_context *srt_process_context, struct maat_rule *results, int n_results, void *a_packet); + +TEST(TSGMaster, SessionDealStatePending) +{ + struct streaminfo a_stream={0}; + struct tcpdetail pdetail={0}; + a_stream.type=STREAM_TYPE_TCP; + a_stream.ptcpdetail=&pdetail; + + struct maat_rule matched_rules[MAX_RESULT_NUM]={0}; + struct session_runtime_process_context srt_process_context={0}; + + gtest_set_ftp_data_flag(); + + size_t n_matched_rules=session_pending_state_deal((const struct streaminfo *)&a_stream, &srt_process_context, matched_rules, MAX_RESULT_NUM, NULL); + EXPECT_EQ(0, n_matched_rules); + EXPECT_EQ(PROTO_FTP, srt_process_context.proto); + + struct gather_app_result *gather_result=(struct gather_app_result *)session_gather_app_results_get(&a_stream); + EXPECT_NE(nullptr, gather_result); + + EXPECT_EQ(1, gather_result[ORIGIN_BASIC_PROTOCOL].app_num); + EXPECT_EQ(45, gather_result[ORIGIN_BASIC_PROTOCOL].attributes[0].app_id); + EXPECT_EQ(0, gather_result[ORIGIN_BASIC_PROTOCOL].attributes[0].surrogate_id); + + free(gather_result); + session_gather_app_results_async(&a_stream, NULL); + EXPECT_EQ(nullptr, session_gather_app_results_get(&a_stream)); + + gtest_clean_ftp_data_flag(); + EXPECT_EQ(0, ftp_data_identify(&a_stream)); + +} + int main(int argc, char *argv[]) { TSG_MASTER_INIT(); diff --git a/test/src/gtest_protocol.cpp b/test/src/gtest_protocol.cpp index 2103ba6..14a2266 100644 --- a/test/src/gtest_protocol.cpp +++ b/test/src/gtest_protocol.cpp @@ -19,9 +19,20 @@ extern "C" int ftp_control_identify(struct streaminfo *a_tcp) return 0; } +int g_gtest_ftp_data_flag=0; +void gtest_set_ftp_data_flag(void) +{ + g_gtest_ftp_data_flag=1; +} + +void gtest_clean_ftp_data_flag(void) +{ + g_gtest_ftp_data_flag=0; +} + extern "C" int ftp_data_identify(struct streaminfo *a_tcp) { - return 0; + return g_gtest_ftp_data_flag; } extern "C" int mail_protocol_identify_by_first_payload(struct streaminfo *a_tcp, char *payload, int payload_len, int thread_seq)