From 526c110868cec72677660fcc75dfa15dbb43fff0 Mon Sep 17 00:00:00 2001 From: yangwei Date: Wed, 6 Nov 2024 13:53:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor(appid=20api):=20remove?= =?UTF-8?q?=20appid=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- decoders/CMakeLists.txt | 1 - decoders/appid/CMakeLists.txt | 1 - decoders/appid/appid.c | 49 ------------------- decoders/appid/appid_internal.h | 25 ---------- decoders/lpi_plus/CMakeLists.txt | 2 +- decoders/lpi_plus/lpi_plus.h | 7 --- decoders/lpi_plus/lpi_plus_internal.h | 16 ++++++ decoders/lpi_plus/lpip_module.c | 70 +++++++++++++++++++++------ decoders/lpi_plus/version.map | 2 +- include/stellar/appid.h | 32 ------------ include/stellar/lpi_plus.h | 21 ++++++++ infra/CMakeLists.txt | 2 +- infra/version.map | 3 +- test/lpi_plus/gtest_lpip_module.c | 14 +++--- 14 files changed, 102 insertions(+), 143 deletions(-) delete mode 100644 decoders/appid/CMakeLists.txt delete mode 100644 decoders/appid/appid.c delete mode 100644 decoders/appid/appid_internal.h delete mode 100644 decoders/lpi_plus/lpi_plus.h create mode 100644 decoders/lpi_plus/lpi_plus_internal.h delete mode 100644 include/stellar/appid.h create mode 100644 include/stellar/lpi_plus.h diff --git a/decoders/CMakeLists.txt b/decoders/CMakeLists.txt index efad779..7946822 100644 --- a/decoders/CMakeLists.txt +++ b/decoders/CMakeLists.txt @@ -1,4 +1,3 @@ -add_subdirectory(appid) add_subdirectory(lpi_plus) #add_subdirectory(http) #add_subdirectory(socks) diff --git a/decoders/appid/CMakeLists.txt b/decoders/appid/CMakeLists.txt deleted file mode 100644 index acb1939..0000000 --- a/decoders/appid/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_library(appid appid.c) \ No newline at end of file diff --git a/decoders/appid/appid.c b/decoders/appid/appid.c deleted file mode 100644 index 6182dd8..0000000 --- a/decoders/appid/appid.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "appid_internal.h" - -#include "stellar/utils.h" -#include "stellar/mq.h" - - - -static void appid_message_free(void *msg, void *msg_free_arg __unused) -{ - if(msg==NULL)return; - FREE(msg); -} - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-function-type" - -static void stellar_appid_on_msg_dispatch(int topic_id __unused, - void *msg, - on_msg_cb_func* on_msg_cb, - void *on_msg_cb_arg, - void *dispatch_arg __unused) -{ - on_appid_callback *appid_cb = (on_appid_callback *)on_msg_cb; - struct appid_message *appid_msg=(struct appid_message *)msg; - appid_cb(appid_msg->sess, appid_msg->origin, appid_msg->appid, appid_msg->appid_num, on_msg_cb_arg); -} - -int stellar_appid_subscribe(struct module_manager *mod_mgr, on_appid_callback *cb, void *args) -{ - if(mod_mgr==NULL)return -1; - int appid_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), APPID_MESSAGE_TOPIC); - if(appid_topic_id<0) - { - appid_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), APPID_MESSAGE_TOPIC, stellar_appid_on_msg_dispatch, mod_mgr, appid_message_free, NULL); - } - return mq_schema_subscribe(module_manager_get_mq_schema(mod_mgr), appid_topic_id, (on_msg_cb_func *)cb, args); -} - -#pragma GCC diagnostic pop - -int stellar_appid_create_topic(struct module_manager *mod_mgr) -{ - int app_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), APPID_MESSAGE_TOPIC); - if(app_topic_id < 0) - { - app_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), APPID_MESSAGE_TOPIC, stellar_appid_on_msg_dispatch, NULL,appid_message_free, NULL); - } - return app_topic_id; -} \ No newline at end of file diff --git a/decoders/appid/appid_internal.h b/decoders/appid/appid_internal.h deleted file mode 100644 index b107806..0000000 --- a/decoders/appid/appid_internal.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "stellar/appid.h" - -#define MAX_APPID_NUM 8 -#define APPID_MESSAGE_TOPIC "TOPIC_APPID" - -struct appid_message -{ - struct session *sess; - enum APPID_ORIGIN origin; - uint32_t appid_num; - int32_t appid[MAX_APPID_NUM]; - uint32_t surrogate_id[MAX_APPID_NUM]; - uint32_t packet_sequence[MAX_APPID_NUM]; -}; - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/decoders/lpi_plus/CMakeLists.txt b/decoders/lpi_plus/CMakeLists.txt index b5da11a..91b097f 100644 --- a/decoders/lpi_plus/CMakeLists.txt +++ b/decoders/lpi_plus/CMakeLists.txt @@ -5,7 +5,7 @@ add_library(lpi_plus lpip_module.c lpip_extend.c) target_include_directories(lpi_plus PUBLIC ${CMAKE_SOURCE_DIR}/deps/) target_include_directories(lpi_plus PUBLIC ${CMAKE_SOURCE_DIR}/decoders/) -target_link_libraries(lpi_plus libprotoident appid) +target_link_libraries(lpi_plus libprotoident) set_target_properties(lpi_plus PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/version.map") diff --git a/decoders/lpi_plus/lpi_plus.h b/decoders/lpi_plus/lpi_plus.h deleted file mode 100644 index cb55a5f..0000000 --- a/decoders/lpi_plus/lpi_plus.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -#include "stellar/module.h" -#define LPI_PLUS_MODULE_NAME "LPI_PLUS" -struct lpi_plus_mapper; -struct lpi_plus_mapper *module_to_lpip_mapper(struct module *mod); -const char *lpi_plus_appid2name(struct lpi_plus_mapper *mapper, int appid); \ No newline at end of file diff --git a/decoders/lpi_plus/lpi_plus_internal.h b/decoders/lpi_plus/lpi_plus_internal.h new file mode 100644 index 0000000..87136bf --- /dev/null +++ b/decoders/lpi_plus/lpi_plus_internal.h @@ -0,0 +1,16 @@ +#pragma once + +#include "stellar/lpi_plus.h" + +#define MAX_APPID_NUM 8 +#define LPIP_APPID_MESSAGE_TOPIC "TOPIC_LPIP_APPID" + +struct appid_message +{ + struct session *sess; + uint32_t appid_num; + int32_t appid[MAX_APPID_NUM]; + uint32_t packet_sequence[MAX_APPID_NUM]; +}; + +const char *lpi_plus_appid2name(struct lpi_plus *lpip, int appid); \ No newline at end of file diff --git a/decoders/lpi_plus/lpip_module.c b/decoders/lpi_plus/lpip_module.c index 5fd4aa4..1d254c6 100644 --- a/decoders/lpi_plus/lpip_module.c +++ b/decoders/lpi_plus/lpip_module.c @@ -17,9 +17,7 @@ #include "stellar/packet.h" #include "stellar/session.h" -#include "appid/appid_internal.h" - -#include "lpi_plus.h" +#include "lpi_plus_internal.h" #include "lpip_extend.h" @@ -40,7 +38,7 @@ struct lpi_plus_mapper struct lpi_plus_appid_info *hash_appid2name; }; -struct lpi_plus_env +struct lpi_plus { unsigned int max_pkts; int lpip_session_exdata_idx; @@ -93,13 +91,11 @@ static void lpi_plus_get_host_order_port(struct session *sess __unused, unsigned static struct appid_message *lpi_plus_message_new(struct session *sess, int *id_array, size_t id_num) { struct appid_message *result=CALLOC(struct appid_message, 1); - result->origin=ORIGIN_LPI_PLUS; result->sess=sess; result->appid_num=id_num; for(unsigned int i=0; iappid_num; i++) { result->appid[i]=(int)(id_array[i]); - result->surrogate_id[i]=0; } return result; @@ -136,10 +132,10 @@ static int lpi_plus_name2appid(struct lpi_plus_mapper *mapper, const char *lpi_n return out->appid; } -const char *lpi_plus_appid2name(struct lpi_plus_mapper *mapper, int appid) +const char *lpi_plus_appid2name(struct lpi_plus *lpip, int appid) { struct lpi_plus_appid_info *out=NULL; - HASH_FIND(hh_appid, mapper->hash_appid2name, &appid, sizeof(int), out); + HASH_FIND(hh_appid, lpip->mapper->hash_appid2name, &appid, sizeof(int), out); if(out==NULL)return NULL; return out->app_name; } @@ -293,7 +289,7 @@ static void lpi_plus_on_session(struct session *sess, enum session_state state, assert(pkt == NULL); return; } - struct lpi_plus_env *env=(struct lpi_plus_env *)args; + struct lpi_plus *env=(struct lpi_plus *)args; struct lpi_plus_exdata *exdata = (struct lpi_plus_exdata *)session_get_exdata(sess, env->lpip_session_exdata_idx); if(exdata==NULL) { @@ -331,7 +327,7 @@ void lpi_plus_exit(struct module_manager *mod_mgr, struct module *mod) if(mod_mgr==NULL)return; if(mod) { - struct lpi_plus_env *env=(struct lpi_plus_env *)module_get_ctx(mod); + struct lpi_plus *env=(struct lpi_plus *)module_get_ctx(mod); lpi_free_library(); lpi_plus_mapper_free(env->mapper); FREE(env); @@ -339,11 +335,55 @@ void lpi_plus_exit(struct module_manager *mod_mgr, struct module *mod) } } +static void appid_message_free(void *msg, void *msg_free_arg __unused) +{ + if(msg==NULL)return; + FREE(msg); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-function-type" + +static void lpi_plus_appid_on_msg_dispatch(int topic_id __unused, + void *msg, + on_msg_cb_func* on_msg_cb, + void *on_msg_cb_arg, + void *dispatch_arg __unused) +{ + on_appid_callback *appid_cb = (on_appid_callback *)on_msg_cb; + struct appid_message *appid_msg=(struct appid_message *)msg; + appid_cb(appid_msg->sess, appid_msg->appid, appid_msg->appid_num, on_msg_cb_arg); +} + +int lpi_plus_appid_subscribe(struct lpi_plus *lpip, on_appid_callback *cb, void *args) +{ + if(lpip==NULL)return -1; + struct module_manager *mod_mgr=lpip->mod_mgr; + int appid_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC); + if(appid_topic_id<0) + { + appid_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC, lpi_plus_appid_on_msg_dispatch, mod_mgr, appid_message_free, NULL); + } + return mq_schema_subscribe(module_manager_get_mq_schema(mod_mgr), appid_topic_id, (on_msg_cb_func *)cb, args); +} + +#pragma GCC diagnostic pop + +int lpi_plus_create_appid_topic(struct module_manager *mod_mgr) +{ + int app_topic_id=mq_schema_get_topic_id(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC); + if(app_topic_id < 0) + { + app_topic_id=mq_schema_create_topic(module_manager_get_mq_schema(mod_mgr), LPIP_APPID_MESSAGE_TOPIC, lpi_plus_appid_on_msg_dispatch, NULL,appid_message_free, NULL); + } + return app_topic_id; +} + struct module *lpi_plus_init(struct module_manager *mod_mgr) { if(mod_mgr==NULL)return NULL; - struct lpi_plus_env *env=CALLOC(struct lpi_plus_env, 1); + struct lpi_plus *env=CALLOC(struct lpi_plus, 1); struct module *mod=module_new("LPI_PLUS", env); env->mod_mgr=mod_mgr; env->max_pkts=16;//TODO: load from toml @@ -370,7 +410,7 @@ struct module *lpi_plus_init(struct module_manager *mod_mgr) session_manager_subscribe_udp(sess_mgr, lpi_plus_on_session, env); env->lpip_session_exdata_idx = session_manager_new_session_exdata_index(sess_mgr, "EXDATA_LPI", lpi_plus_exdata_free, NULL); - env->topic_appid=stellar_appid_create_topic(mod_mgr); + env->topic_appid=lpi_plus_create_appid_topic(mod_mgr); if(env->topic_appid<0) { goto INIT_ERROR; @@ -385,11 +425,11 @@ INIT_ERROR: } -struct lpi_plus_mapper *module_to_lpip_mapper(struct module *mod) +struct lpi_plus *module_to_lpi_plus(struct module *mod) { if(mod==NULL)return NULL; assert(strcmp(module_get_name(mod), LPI_PLUS_MODULE_NAME) == 0); - struct lpi_plus_env *lpi_p=(struct lpi_plus_env *)module_get_ctx(mod); + struct lpi_plus *lpi_p=(struct lpi_plus *)module_get_ctx(mod); if(lpi_p==NULL)return NULL; - return lpi_p->mapper; + return lpi_p; } \ No newline at end of file diff --git a/decoders/lpi_plus/version.map b/decoders/lpi_plus/version.map index 179b07f..6028219 100644 --- a/decoders/lpi_plus/version.map +++ b/decoders/lpi_plus/version.map @@ -3,7 +3,7 @@ global: extern "C" { lpi_plus_init; lpi_plus_exit; - stellar_appid_subscribe; + lpi_plus_appid_subscribe; GIT_VERSION_*; }; diff --git a/include/stellar/appid.h b/include/stellar/appid.h deleted file mode 100644 index d7e2506..0000000 --- a/include/stellar/appid.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include -#include - -#include "stellar/session.h" -#include "stellar/module.h" - -enum APPID_ORIGIN -{ - ORIGIN_LPI_PLUS=0, - ORIGIN_APP_SKETCH_USER_DEFINE, - ORIGIN_PROTO_ENGINE, - ORIGIN_APP_SKETCH_BUILT_IN, - ORIGIN_PROTO_DECODED, - ORIGIN_EXCEED_PACKET_LIMIT, - ORIGIN_TUNNEL, - ORIGIN_MAX -}; - -typedef void on_appid_callback(struct session *sess, enum APPID_ORIGIN origin, int appid[], size_t appid_num, void *args); -int stellar_appid_create_topic(struct module_manager *mod_mgr); -int stellar_appid_subscribe(struct module_manager *mod_mgr, on_appid_callback *cb, void *args); - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/include/stellar/lpi_plus.h b/include/stellar/lpi_plus.h new file mode 100644 index 0000000..0e624fb --- /dev/null +++ b/include/stellar/lpi_plus.h @@ -0,0 +1,21 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "stellar/module.h" +#include "stellar/session.h" + +#define LPI_PLUS_MODULE_NAME "LPI_PLUS" + +struct lpi_plus; +struct lpi_plus *module_to_lpi_plus(struct module *mod); + +typedef void on_appid_callback(struct session *sess, int appid[], size_t appid_num, void *args); +int lpi_plus_appid_subscribe(struct lpi_plus *lpip, on_appid_callback *cb, void *args); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/infra/CMakeLists.txt b/infra/CMakeLists.txt index dbd62e9..e2e45ee 100644 --- a/infra/CMakeLists.txt +++ b/infra/CMakeLists.txt @@ -1,6 +1,6 @@ set(INFRA exdata mq tuple packet_manager packet_io ip_reassembly tcp_reassembly session_manager module_manager) set(DEPS bitmap dablooms interval_tree logger nmx_pool rbtree timeout toml) -set(DECODERS appid lpi_plus) +set(DECODERS lpi_plus) set(WHOLE_ARCHIVE ${DEPS} ${INFRA} ${DECODERS}) set(LIBS fieldstat4) diff --git a/infra/version.map b/infra/version.map index cf18ade..c66ef63 100644 --- a/infra/version.map +++ b/infra/version.map @@ -61,8 +61,6 @@ global: stellar_get_logger; stellar_reload_log_level; - stellar_appid_subscribe; - log_print; log_check_level; @@ -74,6 +72,7 @@ global: lpi_plus_init; lpi_plus_exit; + lpi_plus_appid_subscribe; local: *; }; diff --git a/test/lpi_plus/gtest_lpip_module.c b/test/lpi_plus/gtest_lpip_module.c index 854c270..33dbb76 100644 --- a/test/lpi_plus/gtest_lpip_module.c +++ b/test/lpi_plus/gtest_lpip_module.c @@ -6,20 +6,18 @@ #include #include -#include "stellar/appid.h" #include "stellar/module.h" #include "stellar/session.h" #include "stellar/utils.h" #include "cjson/cJSON.h" -#include "lpi_plus/lpi_plus.h" -#include "appid/appid_internal.h" +#include "lpi_plus/lpi_plus_internal.h" struct test_lpip_env { struct module_manager *mod_mgr; - struct lpi_plus_mapper *lpi_mapper; + struct lpi_plus *lpip; int l7_exdata_idx; int session_num; }; @@ -73,7 +71,7 @@ static void gtest_lpip_exdata_free(int idx __attribute__((unused)), void *ex_ptr const char *proto_names[MAX_APPID_NUM] = {}; for (unsigned int i = 0; i < test_appid_exdata->appid_num; i++) { - proto_names[i] = lpi_plus_appid2name(env->lpi_mapper ,test_appid_exdata->appid[i]); + proto_names[i] = lpi_plus_appid2name(env->lpip ,test_appid_exdata->appid[i]); } cJSON *label_ids = cJSON_CreateIntArray(test_appid_exdata->appid, test_appid_exdata->appid_num); cJSON_AddItemToObject(ctx, "l7_label_id", label_ids); @@ -113,7 +111,7 @@ static void gtest_lpip_exdata_free(int idx __attribute__((unused)), void *ex_ptr } -static void gtest_lpip_on_appid_msg(struct session *sess, enum APPID_ORIGIN origin, int appid[], size_t appid_num, void *args) +static void gtest_lpip_on_appid_msg(struct session *sess, int appid[], size_t appid_num, void *args) { if(sess==NULL || appid==NULL || args==NULL)return; struct test_lpip_env *env = (struct test_lpip_env *)args; @@ -156,7 +154,7 @@ struct module *gtest_lpip_module_init(struct module_manager *mod_mgr) { struct test_lpip_env *env = (struct test_lpip_env *)calloc(1, sizeof(struct test_lpip_env)); struct module *lpip_mod = module_manager_get_module(mod_mgr, LPI_PLUS_MODULE_NAME); - env->lpi_mapper=module_to_lpip_mapper(lpip_mod); + env->lpip=module_to_lpi_plus(lpip_mod); struct module *sess_mgr_mod=module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME); struct session_manager *sess_mgr = module_to_session_manager(sess_mgr_mod); if(sess_mgr == NULL) @@ -170,7 +168,7 @@ struct module *gtest_lpip_module_init(struct module_manager *mod_mgr) env->l7_exdata_idx = session_manager_new_session_exdata_index(sess_mgr, "EXDATA_L7", gtest_lpip_exdata_free, env); - stellar_appid_subscribe(mod_mgr, gtest_lpip_on_appid_msg, env); + lpi_plus_appid_subscribe(env->lpip, gtest_lpip_on_appid_msg, env); printf("gtest_lpip_module_init OK!\n"); return module_new("TEST_LPIP", env);