|
|
|
|
@@ -17,10 +17,13 @@
|
|
|
|
|
#include "stellar/packet.h"
|
|
|
|
|
#include "stellar/session.h"
|
|
|
|
|
|
|
|
|
|
#include "lpi_plus_internal.h"
|
|
|
|
|
#include "stellar/lpi_plus.h"
|
|
|
|
|
|
|
|
|
|
#include "lpip_extend.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_APPID_NUM 8
|
|
|
|
|
|
|
|
|
|
const char *l7_protocol_file = (char *)"./tsgconf/tsg_l7_protocol.conf";
|
|
|
|
|
|
|
|
|
|
struct lpi_plus_appid_info
|
|
|
|
|
@@ -42,9 +45,10 @@ struct lpi_plus
|
|
|
|
|
{
|
|
|
|
|
unsigned int max_pkts;
|
|
|
|
|
int lpip_session_exdata_idx;
|
|
|
|
|
int topic_appid;
|
|
|
|
|
int lpip_packet_exdata_idx;
|
|
|
|
|
struct module_manager *mod_mgr;
|
|
|
|
|
struct lpi_plus_mapper *mapper;
|
|
|
|
|
struct session_manager *sess_mgr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct lpi_plus_per_session_ctx
|
|
|
|
|
@@ -57,13 +61,12 @@ struct lpi_plus_per_session_ctx
|
|
|
|
|
size_t appid_num;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void lpi_plus_get_host_order_port(struct session *sess __unused, unsigned short *sport, unsigned short *dport)
|
|
|
|
|
static void lpi_plus_get_host_order_port(struct packet *pkt, struct session *sess, unsigned short *sport, unsigned short *dport)
|
|
|
|
|
{
|
|
|
|
|
*sport=0;
|
|
|
|
|
*dport=0;
|
|
|
|
|
//get host order port from stellar session api
|
|
|
|
|
|
|
|
|
|
const struct packet *pkt = session_get_current_packet(sess);
|
|
|
|
|
enum flow_type flow_type=session_get_flow_type(sess);
|
|
|
|
|
if(pkt && (flow_type==FLOW_TYPE_C2S || flow_type==FLOW_TYPE_S2C))
|
|
|
|
|
{
|
|
|
|
|
@@ -90,19 +93,6 @@ static void lpi_plus_get_host_order_port(struct session *sess __unused, unsigned
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct lpi_plus_appid_message *lpi_plus_message_new(struct session *sess, int *id_array, int *packet_sequence_array, size_t id_num)
|
|
|
|
|
{
|
|
|
|
|
struct lpi_plus_appid_message *result=CALLOC(struct lpi_plus_appid_message, 1);
|
|
|
|
|
result->sess=sess;
|
|
|
|
|
result->appid_num=id_num;
|
|
|
|
|
for(unsigned int i=0; i<result->appid_num; i++)
|
|
|
|
|
{
|
|
|
|
|
result->appid[i]=id_array[i];
|
|
|
|
|
result->packet_sequence[i]=packet_sequence_array[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int lpi_plus_appid_update(int current_id_array[], size_t *current_id_num, int incoming_id)
|
|
|
|
|
{
|
|
|
|
|
@@ -204,7 +194,7 @@ static void lpi_plus_mapper_free(struct lpi_plus_mapper *mapper)
|
|
|
|
|
free(mapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void lpi_plus_context_update(struct session *sess, struct lpi_plus_detect_context *ctx,
|
|
|
|
|
void lpi_plus_context_update(struct packet *pkt, struct session *sess, struct lpi_plus_detect_context *ctx,
|
|
|
|
|
const char *scan_data, int scan_data_len)
|
|
|
|
|
{
|
|
|
|
|
lpi_data_t *data = &ctx->lpi_data;
|
|
|
|
|
@@ -252,7 +242,7 @@ void lpi_plus_context_update(struct session *sess, struct lpi_plus_detect_contex
|
|
|
|
|
|
|
|
|
|
uint16_t source=0;
|
|
|
|
|
uint16_t dest=0;
|
|
|
|
|
lpi_plus_get_host_order_port(sess,&source ,&dest);
|
|
|
|
|
lpi_plus_get_host_order_port(pkt, sess,&source ,&dest);
|
|
|
|
|
|
|
|
|
|
data->client_port = source;
|
|
|
|
|
data->server_port = dest;
|
|
|
|
|
@@ -285,14 +275,21 @@ static int lpi_plus_detect(struct lpi_plus_detect_context *ctx, struct lpi_plus_
|
|
|
|
|
return new_appid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void lpi_plus_on_session(struct session *sess, enum session_state state, struct packet *pkt, void *args)
|
|
|
|
|
void lpi_plus_on_packet(struct packet *pkt, struct module *mod)
|
|
|
|
|
{
|
|
|
|
|
if (state == SESSION_STATE_CLOSED)
|
|
|
|
|
if(pkt==NULL||mod==NULL)return;
|
|
|
|
|
|
|
|
|
|
struct lpi_plus *env=(struct lpi_plus *)module_get_ctx(mod);
|
|
|
|
|
struct session *sess=packet_exdata_to_session(env->sess_mgr, pkt);
|
|
|
|
|
|
|
|
|
|
if(sess==NULL)return;
|
|
|
|
|
|
|
|
|
|
if (session_get_current_state(sess) == SESSION_STATE_CLOSED)
|
|
|
|
|
{
|
|
|
|
|
assert(pkt == NULL);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
struct lpi_plus *env=(struct lpi_plus *)args;
|
|
|
|
|
|
|
|
|
|
struct lpi_plus_per_session_ctx *lpip_sess_ctx = (struct lpi_plus_per_session_ctx *)session_get_exdata(sess, env->lpip_session_exdata_idx);
|
|
|
|
|
if(lpip_sess_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
@@ -307,21 +304,29 @@ static void lpi_plus_on_session(struct session *sess, enum session_state state,
|
|
|
|
|
if (payload!=NULL && payload_len>0)//detect packet with payload only
|
|
|
|
|
{
|
|
|
|
|
if(lpip_sess_ctx->detector_ctx==NULL)lpip_sess_ctx->detector_ctx=CALLOC(struct lpi_plus_detect_context, 1);
|
|
|
|
|
lpi_plus_context_update(sess, lpip_sess_ctx->detector_ctx, payload, payload_len);
|
|
|
|
|
lpi_plus_context_update(pkt, sess, lpip_sess_ctx->detector_ctx, payload, payload_len);
|
|
|
|
|
int appid=lpi_plus_detect(lpip_sess_ctx->detector_ctx, env->mapper, payload, payload_len, &lpip_sess_ctx->stop_detect);
|
|
|
|
|
lpip_sess_ctx->detected_pkt_cnt+=1;
|
|
|
|
|
if(appid>0 && lpi_plus_appid_update(lpip_sess_ctx->appid, &(lpip_sess_ctx->appid_num), appid))
|
|
|
|
|
{
|
|
|
|
|
lpip_sess_ctx->packet_sequence[lpip_sess_ctx->appid_num-1]=lpip_sess_ctx->detected_pkt_cnt;
|
|
|
|
|
struct lpi_plus_appid_message *msg=lpi_plus_message_new(sess, lpip_sess_ctx->appid, lpip_sess_ctx->packet_sequence, lpip_sess_ctx->appid_num);
|
|
|
|
|
if(0 > mq_runtime_publish_message(module_manager_get_mq_runtime(env->mod_mgr),
|
|
|
|
|
env->topic_appid,
|
|
|
|
|
msg))FREE(msg);
|
|
|
|
|
packet_set_exdata(pkt, env->lpip_packet_exdata_idx, lpip_sess_ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t *packet_exdata_to_lpip_appid(struct lpi_plus *lpip, struct packet *pkt, size_t *appid_num)
|
|
|
|
|
{
|
|
|
|
|
if(lpip==NULL || pkt==NULL || appid_num==NULL)return NULL;
|
|
|
|
|
|
|
|
|
|
struct lpi_plus_per_session_ctx *lpip_sess_ctx=(struct lpi_plus_per_session_ctx *)packet_get_exdata(pkt, lpip->lpip_packet_exdata_idx);
|
|
|
|
|
if(lpip_sess_ctx==NULL)return NULL;
|
|
|
|
|
|
|
|
|
|
*appid_num=lpip_sess_ctx->appid_num;
|
|
|
|
|
return lpip_sess_ctx->appid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void lpi_plus_exdata_free(int idx __unused, void *ex_ptr, void *arg __unused)
|
|
|
|
|
{
|
|
|
|
|
if(ex_ptr==NULL)return;
|
|
|
|
|
@@ -343,86 +348,39 @@ 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)
|
|
|
|
|
{
|
|
|
|
|
lpi_plus_on_appid_callback *appid_cb = (lpi_plus_on_appid_callback *)on_msg_cb;
|
|
|
|
|
struct lpi_plus_appid_message *appid_msg=(struct lpi_plus_appid_message *)msg;
|
|
|
|
|
appid_cb(appid_msg->sess, appid_msg->appid, appid_msg->packet_sequence, appid_msg->appid_num, on_msg_cb_arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lpi_plus_appid_subscribe(struct lpi_plus *lpip, lpi_plus_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 module *sess_mgr_mod=module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME);
|
|
|
|
|
struct module *pkt_mgr_mod=module_manager_get_module(mod_mgr, PACKET_MANAGER_MODULE_NAME);
|
|
|
|
|
if(sess_mgr_mod==NULL || pkt_mgr_mod == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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);
|
|
|
|
|
struct mq_schema *mq_s=module_manager_get_mq_schema(mod_mgr);
|
|
|
|
|
|
|
|
|
|
if(sess_mgr==NULL || mq_s==NULL)
|
|
|
|
|
{
|
|
|
|
|
goto INIT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
env->sess_mgr=module_to_session_manager(sess_mgr_mod);
|
|
|
|
|
|
|
|
|
|
if(lpi_init_library()<0)
|
|
|
|
|
{
|
|
|
|
|
goto INIT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
env->mapper=lpi_plus_mapper_new(l7_protocol_file);// TODO: load path from toml
|
|
|
|
|
env->mapper=lpi_plus_mapper_new(l7_protocol_file);// TODO: load from toml
|
|
|
|
|
if(env->mapper == NULL)
|
|
|
|
|
{
|
|
|
|
|
goto INIT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session_manager_subscribe_tcp(sess_mgr,lpi_plus_on_session, env);
|
|
|
|
|
session_manager_subscribe_udp(sess_mgr, lpi_plus_on_session, env);
|
|
|
|
|
env->lpip_session_exdata_idx = session_manager_new_session_exdata_index(env->sess_mgr, "EXDATA_LPI", lpi_plus_exdata_free, NULL);
|
|
|
|
|
|
|
|
|
|
env->lpip_session_exdata_idx = session_manager_new_session_exdata_index(sess_mgr, "EXDATA_LPI", lpi_plus_exdata_free, NULL);
|
|
|
|
|
env->topic_appid=lpi_plus_create_appid_topic(mod_mgr);
|
|
|
|
|
if(env->topic_appid<0)
|
|
|
|
|
{
|
|
|
|
|
goto INIT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
struct packet_manager *pkt_mgr = module_to_packet_manager(pkt_mgr_mod);
|
|
|
|
|
env->lpip_packet_exdata_idx = packet_manager_new_packet_exdata_index(pkt_mgr, "EXDATA_LPI", NULL, NULL);
|
|
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
|
|
|
|
|
|
|