summaryrefslogtreecommitdiff
path: root/decoders/mail/mail_decoder_module.c
diff options
context:
space:
mode:
Diffstat (limited to 'decoders/mail/mail_decoder_module.c')
-rw-r--r--decoders/mail/mail_decoder_module.c175
1 files changed, 73 insertions, 102 deletions
diff --git a/decoders/mail/mail_decoder_module.c b/decoders/mail/mail_decoder_module.c
index 343f792..39689ec 100644
--- a/decoders/mail/mail_decoder_module.c
+++ b/decoders/mail/mail_decoder_module.c
@@ -20,6 +20,14 @@
//#include "mail_decoder_imap.h"
#include "mail_decoder_module.h"
+const char * g_mail_topic_name[MAIL_TOPIC_MAX] = {
+ "MAIL_COMMAND",
+ "MAIL_HEADER",
+ "MAIL_BODY",
+ "MAIL_ATTACHMENT",
+ "MAIL_EML",
+};
+
static unsigned short get_session_dest_port(struct session *sess)
{
const struct layer *first_pkt_layer;
@@ -90,17 +98,11 @@ static int is_current_packet_c2s(struct session *sess)
return 1;
}
-static int mail_subscribe_command(struct mail_decoder *mail_env, mail_command_callback_func *cb, void *arg)
+static int mail_subscribe_command(struct mq_schema *schema, mail_command_callback_func *cb, void *arg)
{
int topic;
- struct mq_schema *schema;
-
- schema = module_manager_get_mq_schema(mail_env->mod_mgr_ref);
- if (schema == NULL) {
- return -1;
- }
- topic = mq_schema_get_topic_id(schema, MAIL_COMMAND_TOPIC_NAME);
+ topic = mq_schema_get_topic_id(schema, g_mail_topic_name[MAIL_TOPIC_COMMAND]);
if (topic < 0) {
return -1;
}
@@ -108,17 +110,11 @@ static int mail_subscribe_command(struct mail_decoder *mail_env, mail_command_ca
return mq_schema_subscribe(schema, topic, (on_msg_cb_func *)(void *)cb, arg);
}
-static int mail_subscribe_header(struct mail_decoder *mail_env, mail_header_callback_func *cb, void *arg)
+static int mail_subscribe_header(struct mq_schema *schema, mail_header_callback_func *cb, void *arg)
{
int topic;
- struct mq_schema *schema;
-
- schema = module_manager_get_mq_schema(mail_env->mod_mgr_ref);
- if (schema == NULL) {
- return -1;
- }
- topic = mq_schema_get_topic_id(schema, MAIL_HEADER_TOPIC_NAME);
+ topic = mq_schema_get_topic_id(schema, g_mail_topic_name[MAIL_TOPIC_HEADER]);
if (topic < 0) {
return -1;
}
@@ -126,17 +122,11 @@ static int mail_subscribe_header(struct mail_decoder *mail_env, mail_header_call
return mq_schema_subscribe(schema, topic, (on_msg_cb_func *)(void *)cb, arg);
}
-static int mail_subscribe_body(struct mail_decoder *mail_env, mail_body_callback_func *cb, void *arg)
+static int mail_subscribe_body(struct mq_schema *schema, mail_body_callback_func *cb, void *arg)
{
int topic;
- struct mq_schema *schema;
- schema = module_manager_get_mq_schema(mail_env->mod_mgr_ref);
- if (schema == NULL) {
- return -1;
- }
-
- topic = mq_schema_get_topic_id(schema, MAIL_BODY_TOPIC_NAME);
+ topic = mq_schema_get_topic_id(schema, g_mail_topic_name[MAIL_TOPIC_BODY]);
if (topic < 0) {
return -1;
}
@@ -144,17 +134,11 @@ static int mail_subscribe_body(struct mail_decoder *mail_env, mail_body_callback
return mq_schema_subscribe(schema, topic, (on_msg_cb_func *)(void *)cb, arg);
}
-static int mail_subscribe_attachment(struct mail_decoder *mail_env, mail_attachment_callback_func *cb, void *arg)
+static int mail_subscribe_attachment( struct mq_schema *schema, mail_attachment_callback_func *cb, void *arg)
{
int topic;
- struct mq_schema *schema;
- schema = module_manager_get_mq_schema(mail_env->mod_mgr_ref);
- if (schema == NULL) {
- return -1;
- }
-
- topic = mq_schema_get_topic_id(schema, MAIL_ATTACHMENT_TOPIC_NAME);
+ topic = mq_schema_get_topic_id(schema, g_mail_topic_name[MAIL_TOPIC_ATTACHMENT]);
if (topic < 0) {
return -1;
}
@@ -162,17 +146,11 @@ static int mail_subscribe_attachment(struct mail_decoder *mail_env, mail_attachm
return mq_schema_subscribe(schema, topic, (on_msg_cb_func *)(void *)cb, arg);
}
-static int mail_subscribe_eml(struct mail_decoder *mail_env, mail_eml_callback_func *cb, void *arg)
+static int mail_subscribe_eml(struct mq_schema *schema, mail_eml_callback_func *cb, void *arg)
{
int topic;
- struct mq_schema *schema;
- schema = module_manager_get_mq_schema(mail_env->mod_mgr_ref);
- if (schema == NULL) {
- return -1;
- }
-
- topic = mq_schema_get_topic_id(schema, MAIL_EML_TOPIC_NAME);
+ topic = mq_schema_get_topic_id(schema, g_mail_topic_name[MAIL_TOPIC_EML]);
if (topic < 0) {
return -1;
}
@@ -183,16 +161,18 @@ static int mail_subscribe_eml(struct mail_decoder *mail_env, mail_eml_callback_f
static void mail_dispatch(int topic, void *msg, on_msg_cb_func *msg_cb, void *arg, void *dispatch_arg)
{
struct mail_message *mail_msg;
- struct mail_decoder *mail_env;
+ struct mail_decoder *decoder;
+ struct mail_topics *topics;
mail_msg = (struct mail_message *)msg;
- mail_env = (struct mail_decoder *)dispatch_arg;
+ decoder = (struct mail_decoder *)dispatch_arg;
+ topics = &decoder->topics;
//char *msg_json = mail_message_to_json(mail_msg);
//printf("%s\n", msg_json);
//free(msg_json);
- if (topic == mail_env->command_topic_id) {
+ if (topic == topics->topic_ids[MAIL_TOPIC_COMMAND]) {
mail_command_callback_func *command_cb = (mail_command_callback_func *)(void *)msg_cb;
command_cb(mail_msg->sess_ref, mail_msg->mail_protocol, mail_msg->mail_seq,
mail_msg->command->cmd, (const char *)mail_msg->command->arg, mail_msg->command->arg_len,
@@ -201,13 +181,13 @@ static void mail_dispatch(int topic, void *msg, on_msg_cb_func *msg_cb, void *ar
return;
}
- if (topic == mail_env->header_topic_id) {
+ if (topic == topics->topic_ids[MAIL_TOPIC_HEADER]) {
mail_header_callback_func *header_cb = (mail_header_callback_func *)(void *)msg_cb;
header_cb(mail_msg->sess_ref, mail_msg->mail_protocol, mail_msg->mail_seq, (const struct mail_header *)mail_msg->header, arg);
return;
}
- if (topic == mail_env->body_topic_id) {
+ if (topic == topics->topic_ids[MAIL_TOPIC_BODY]) {
mail_body_callback_func *body_cb = (mail_body_callback_func *)(void *)msg_cb;
body_cb(mail_msg->sess_ref, mail_msg->mail_protocol, mail_msg->mail_seq,
(const char *)mail_msg->body->body, mail_msg->body->body_len,
@@ -216,7 +196,7 @@ static void mail_dispatch(int topic, void *msg, on_msg_cb_func *msg_cb, void *ar
return;
}
- if (topic == mail_env->attachment_topic_id) {
+ if (topic == topics->topic_ids[MAIL_TOPIC_ATTACHMENT]) {
mail_attachment_callback_func *attachment_cb = (mail_attachment_callback_func *)(void *)msg_cb;
attachment_cb(mail_msg->sess_ref, mail_msg->mail_protocol, mail_msg->mail_seq,
(const char *)mail_msg->attachment->attachment_name, mail_msg->attachment->attachment_name_len,
@@ -226,7 +206,7 @@ static void mail_dispatch(int topic, void *msg, on_msg_cb_func *msg_cb, void *ar
return;
}
- if (topic == mail_env->eml_topic_id) {
+ if (topic == topics->topic_ids[MAIL_TOPIC_EML]) {
mail_eml_callback_func *eml_cb = (mail_eml_callback_func *)(void *)msg_cb;
eml_cb(mail_msg->sess_ref, mail_msg->mail_protocol,
(const char *)mail_msg->eml->eml, mail_msg->eml->eml_len,
@@ -236,7 +216,7 @@ static void mail_dispatch(int topic, void *msg, on_msg_cb_func *msg_cb, void *ar
}
}
-int mail_subscribe(struct mail_decoder *mail_env,
+int mail_subscribe(struct mail_decoder *decoder,
mail_command_callback_func command_cb,
mail_header_callback_func *header_cb,
mail_body_callback_func *body_cb,
@@ -246,27 +226,27 @@ int mail_subscribe(struct mail_decoder *mail_env,
{
int ret;
- ret = mail_subscribe_command(mail_env, command_cb, arg);
+ ret = mail_subscribe_command(decoder->mq_schema, command_cb, arg);
if (ret < 0) {
return ret;
}
- ret = mail_subscribe_header(mail_env, header_cb, arg);
+ ret = mail_subscribe_header(decoder->mq_schema, header_cb, arg);
if (ret < 0) {
return ret;
}
- ret = mail_subscribe_body(mail_env, body_cb, arg);
+ ret = mail_subscribe_body(decoder->mq_schema, body_cb, arg);
if (ret < 0) {
return ret;
}
- ret = mail_subscribe_attachment(mail_env, attachment_cb, arg);
+ ret = mail_subscribe_attachment(decoder->mq_schema, attachment_cb, arg);
if (ret < 0) {
return ret;
}
- ret = mail_subscribe_eml(mail_env, eml_cb, arg);
+ ret = mail_subscribe_eml(decoder->mq_schema, eml_cb, arg);
if (ret < 0) {
return ret;
}
@@ -309,15 +289,18 @@ static struct mail_session_ctx *mail_decoder_session_ctx_new(void)
// return 0;
//}
-static int mail_decoder_smtp_process(struct mail_decoder *env, struct mail_session_ctx *session_ctx, const char *payload, size_t payload_len, int is_c2s)
+static int mail_decoder_smtp_process(struct mail_decoder *decoder, struct mail_session_ctx *session_ctx, const char *payload, size_t payload_len, int is_c2s)
{
- char ret;;
+ char ret;
+ struct mq_runtime *mq;
- if (session_ctx->smtp_parser == NULL) {
- session_ctx->smtp_parser = smtp_parser_new(env, is_c2s);
+ mq = module_manager_get_mq_runtime(decoder->mod_mgr);
+
+ if (session_ctx->smtp_parser == NULL && mq != NULL) {
+ session_ctx->smtp_parser = smtp_parser_new(mq, &decoder->topics, is_c2s);
}
- ret = smtp_parser_entry(session_ctx->smtp_parser, session_ctx->sess_ref, payload, payload_len,is_c2s);
+ ret = smtp_parser_entry(session_ctx->smtp_parser, session_ctx->sess_ref, payload, payload_len, is_c2s);
if (ret != 0) {
return -1;
}
@@ -331,7 +314,7 @@ static void mail_decoder_on_tcp_payload(struct session *sess, enum session_state
int ret;
int is_packet_c2s;
struct mail_session_ctx *session_ctx;
- struct mail_decoder *env = (struct mail_decoder *)arg;
+ struct mail_decoder *decoder = (struct mail_decoder *)arg;
if (payload == NULL || payload_len == 0) {
return;
@@ -339,14 +322,13 @@ static void mail_decoder_on_tcp_payload(struct session *sess, enum session_state
is_packet_c2s = is_current_packet_c2s(sess);
- session_ctx = (struct mail_session_ctx *)session_get_exdata(sess, env->exdata_id);
+ session_ctx = (struct mail_session_ctx *)session_get_exdata(sess, decoder->exdata_id);
if (session_ctx == NULL) {
session_ctx = mail_decoder_session_ctx_new();
- session_ctx->mail_env_ref = env;
session_ctx->sess_ref = sess;
session_ctx->protocol = MAIL_PROTOCOL_MAX;
session_ctx->is_droped = 0;
- session_set_exdata(sess, env->exdata_id, session_ctx);
+ session_set_exdata(sess, decoder->exdata_id, session_ctx);
unsigned short session_dest_port = get_session_dest_port(sess);
@@ -383,7 +365,7 @@ static void mail_decoder_on_tcp_payload(struct session *sess, enum session_state
switch (session_ctx->protocol) {
case MAIL_PROTOCOL_SMTP:
- ret = mail_decoder_smtp_process(env, session_ctx, payload, payload_len, is_packet_c2s);
+ ret = mail_decoder_smtp_process(decoder, session_ctx, payload, payload_len, is_packet_c2s);
break;
case MAIL_PROTOCOL_POP3:
//ret = mail_decoder_pop3_process(decoder, session_ctx, payload, payload_len , is_packet_c2s);
@@ -414,19 +396,18 @@ void mail_decoder_on_exdata_free(int idx, void *ex_ptr, void *arg)
void mail_exit(struct module_manager *mod_mgr, struct module *mod)
{
(void)(mod_mgr);
- struct mail_decoder *mail_env;
+ struct mail_decoder *decoder;
struct mq_schema *schema;
if (mod) {
- mail_env = (struct mail_decoder *)module_get_ctx(mod);
- if (mail_env) {
+ decoder = (struct mail_decoder *)module_get_ctx(mod);
+ if (decoder) {
schema = module_manager_get_mq_schema(mod_mgr);
- mq_schema_destroy_topic(schema, mail_env->command_topic_id);
- mq_schema_destroy_topic(schema, mail_env->header_topic_id);
- mq_schema_destroy_topic(schema, mail_env->body_topic_id);
- mq_schema_destroy_topic(schema, mail_env->attachment_topic_id);
- mq_schema_destroy_topic(schema, mail_env->eml_topic_id);
- free(mail_env);
+ int i;
+ for (i = 0; i < MAIL_TOPIC_MAX; i++) {
+ mq_schema_destroy_topic(schema, decoder->topics.topic_ids[i]);
+ }
+ free(decoder);
}
module_free(mod);
@@ -437,49 +418,39 @@ struct module* mail_init(struct module_manager *mod_mgr)
{
int ret;
struct module *mod;
- struct mq_schema *schema;
struct session_manager *sess_mgr;
- struct mail_decoder *env;
+ struct mail_decoder *decoder;
+ struct mail_topics *topics;
- env = (struct mail_decoder *)calloc(1, sizeof(struct mail_decoder));
- env->mod_mgr_ref = mod_mgr;
- mod = module_new(MAIL_MODULE_NAME, env);
- sess_mgr = module_to_session_manager(module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME));
- schema = module_manager_get_mq_schema(mod_mgr);
+ decoder = (struct mail_decoder *)calloc(1, sizeof(struct mail_decoder));
+ decoder->mod_mgr = mod_mgr;
+ mod = module_new(MAIL_MODULE_NAME, decoder);
- if (sess_mgr == NULL || schema == NULL) {
+ sess_mgr = module_to_session_manager(module_manager_get_module(mod_mgr, SESSION_MANAGER_MODULE_NAME));
+ if (sess_mgr == NULL) {
goto exit;
}
- ret = session_manager_subscribe_tcp_stream(sess_mgr, mail_decoder_on_tcp_payload, env);
+ ret = session_manager_subscribe_tcp_stream(sess_mgr, mail_decoder_on_tcp_payload, decoder);
if (ret < 0) {
goto exit;
}
- env->exdata_id = session_manager_new_session_exdata_index(sess_mgr, MAIL_EXDATA_NAME, mail_decoder_on_exdata_free, NULL);
- if (env->exdata_id < 0) {
+ decoder->exdata_id = session_manager_new_session_exdata_index(sess_mgr, MAIL_EXDATA_NAME, mail_decoder_on_exdata_free, NULL);
+ if (decoder->exdata_id < 0) {
goto exit;
}
- env->command_topic_id = mq_schema_create_topic(schema, MAIL_COMMAND_TOPIC_NAME, mail_dispatch, env, mail_message_free, NULL);
- if (env->command_topic_id < 0) {
- goto exit;
- }
- env->header_topic_id = mq_schema_create_topic(schema, MAIL_HEADER_TOPIC_NAME, mail_dispatch, env, mail_message_free, NULL);
- if (env->header_topic_id < 0) {
- goto exit;
- }
- env->body_topic_id = mq_schema_create_topic(schema, MAIL_BODY_TOPIC_NAME, mail_dispatch, env, mail_message_free, NULL);
- if (env->body_topic_id < 0) {
- goto exit;
- }
- env->attachment_topic_id = mq_schema_create_topic(schema, MAIL_ATTACHMENT_TOPIC_NAME, mail_dispatch, env, mail_message_free, NULL);
- if (env->attachment_topic_id < 0) {
- goto exit;
- }
- env->eml_topic_id = mq_schema_create_topic(schema, MAIL_EML_TOPIC_NAME, mail_dispatch, env, mail_message_free, NULL);
- if (env->eml_topic_id < 0) {
- goto exit;
+ decoder->mq_schema = module_manager_get_mq_schema(mod_mgr);
+
+ int i;
+ topics = &decoder->topics;
+ for (i = 0; i < MAIL_TOPIC_MAX; i++) {
+ topics->topic_ids[i] = mq_schema_create_topic(decoder->mq_schema, g_mail_topic_name[i], mail_dispatch, decoder, mail_message_free, NULL);
+ if (topics->topic_ids[i] < 0) {
+ goto exit;
+ }
+ topics->topic_names[i] = g_mail_topic_name[i];
}
return mod;