diff options
| author | luwenpeng <[email protected]> | 2024-08-28 19:00:32 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2024-08-28 19:00:32 +0800 |
| commit | 283d591e6b8dd460d19295519100472b78d3510a (patch) | |
| tree | c200047cdd6663c58da98e6cb17c4b9b611b87f6 /test | |
| parent | 3e6b837318d44297a24d50ede8b57c365ffa8222 (diff) | |
fix compile error
Diffstat (limited to 'test')
| -rw-r--r-- | test/debug_plugin/debug_plugin.c | 116 | ||||
| -rw-r--r-- | test/packet_inject/packet_inject.c | 59 |
2 files changed, 82 insertions, 93 deletions
diff --git a/test/debug_plugin/debug_plugin.c b/test/debug_plugin/debug_plugin.c index c354e5e..5456b57 100644 --- a/test/debug_plugin/debug_plugin.c +++ b/test/debug_plugin/debug_plugin.c @@ -7,13 +7,12 @@ #include "utils.h" #include "packet_dump.h" #include "session_private.h" -#include "stellar/log.h" #include "stellar/stellar_mq.h" #include "stellar/stellar_exdata.h" #pragma GCC diagnostic ignored "-Wunused-parameter" -static void log_print(int fd, const char *module, const char *fmt, ...) +static void debug_plugin_log(int fd, const char *module, const char *fmt, ...) { static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; @@ -52,7 +51,6 @@ static void log_print(int fd, const char *module, const char *fmt, ...) struct plugin_ctx { struct stellar *st; - struct logger *logger; int sess_exdata_idx; int sess_plug_id; int tcp_topic_id; @@ -102,7 +100,7 @@ static void *on_sess_new(struct session *sess, void *plugin_ctx) memset(buff, 0, sizeof(buff)); session_to_str(sess, 1, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "sess new: %s", buff); + debug_plugin_log(ctx->fd, "debug plugin", "sess new: %s", buff); return NULL; } @@ -115,17 +113,17 @@ static void on_sess_free(struct session *sess, void *sess_ctx, void *plugin_ctx) memset(buff, 0, sizeof(buff)); session_to_str(sess, 0, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "sess free: %s", buff); - log_print(ctx->fd, "debug plugin", "session %lu %s stat:\n" - "C2S rx packets: %6lu, C2S rx bytes: %6lu\n" - "S2C rx packets: %6lu, S2C rx bytes: %6lu\n" - "C2S rx TCP segments: %6lu, C2S rx TCP bytes: %6lu\n" - "S2C rx TCP segments: %6lu, S2C rx TCP bytes: %6lu\n", - session_get_id(sess), session_get0_readable_addr(sess), - exdata->c2s_rx_pkts, exdata->c2s_rx_bytes, - exdata->s2c_rx_pkts, exdata->s2c_rx_bytes, - exdata->c2s_rx_tcp_seg, exdata->c2s_rx_tcp_bytes, - exdata->s2c_rx_tcp_seg, exdata->s2c_rx_tcp_bytes); + debug_plugin_log(ctx->fd, "debug plugin", "sess free: %s", buff); + debug_plugin_log(ctx->fd, "debug plugin", "session %lu %s stat:\n" + "C2S rx packets: %6lu, C2S rx bytes: %6lu\n" + "S2C rx packets: %6lu, S2C rx bytes: %6lu\n" + "C2S rx TCP segments: %6lu, C2S rx TCP bytes: %6lu\n" + "S2C rx TCP segments: %6lu, S2C rx TCP bytes: %6lu\n", + session_get_id(sess), session_get0_readable_addr(sess), + exdata->c2s_rx_pkts, exdata->c2s_rx_bytes, + exdata->s2c_rx_pkts, exdata->s2c_rx_bytes, + exdata->c2s_rx_tcp_seg, exdata->c2s_rx_tcp_bytes, + exdata->s2c_rx_tcp_seg, exdata->s2c_rx_tcp_bytes); if (exdata->c2s_tcp_seg_hexdump_fd > 0) { close(exdata->c2s_tcp_seg_hexdump_fd); @@ -161,11 +159,11 @@ static void on_sess_udp_msg(struct session *sess, int topic_id, const void *msg, memset(buff, 0, sizeof(buff)); session_to_str(sess, 1, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "on UDP msg: %s", buff); + debug_plugin_log(ctx->fd, "debug plugin", "on UDP msg: %s", buff); memset(buff, 0, sizeof(buff)); packet_dump_str(pkt, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "rx UDP packet (tv_sec: %lu, tv_usec: %lu): \n%s", tv->tv_sec, tv->tv_usec, buff); + debug_plugin_log(ctx->fd, "debug plugin", "rx UDP packet (tv_sec: %lu, tv_usec: %lu): \n%s", tv->tv_sec, tv->tv_usec, buff); pthread_spin_lock(&ctx->lock); packet_dump_hex(pkt, ctx->fd); @@ -197,11 +195,11 @@ static void on_sess_tcp_msg(struct session *sess, int topic_id, const void *msg, memset(buff, 0, sizeof(buff)); session_to_str(sess, 1, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "on TCP msg: %s", buff); + debug_plugin_log(ctx->fd, "debug plugin", "on TCP msg: %s", buff); memset(buff, 0, sizeof(buff)); packet_dump_str(pkt, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "rx TCP packet (tv_sec: %lu, tv_usec: %lu): \n%s", tv->tv_sec, tv->tv_usec, buff); + debug_plugin_log(ctx->fd, "debug plugin", "rx TCP packet (tv_sec: %lu, tv_usec: %lu): \n%s", tv->tv_sec, tv->tv_usec, buff); pthread_spin_lock(&ctx->lock); packet_dump_hex(pkt, ctx->fd); @@ -224,12 +222,12 @@ static void on_sess_tcp_stream_msg(struct session *sess, int topic_id, const voi memset(buff, 0, sizeof(buff)); session_to_str(sess, 1, buff, sizeof(buff) - 1); - log_print(ctx->fd, "debug plugin", "on TCP stream msg: %s", buff); + debug_plugin_log(ctx->fd, "debug plugin", "on TCP stream msg: %s", buff); pthread_spin_lock(&ctx->lock); if (session_get_current_flow_direction(sess) == FLOW_DIRECTION_C2S) { - log_print(ctx->fd, "debug plugin", "rx C2S TCP segment: len: %d, data: %p", len, data); + debug_plugin_log(ctx->fd, "debug plugin", "rx C2S TCP segment: len: %d, data: %p", len, data); hexdump_to_fd(ctx->fd, exdata->c2s_rx_tcp_bytes, data, len); hexdump_to_fd(ctx->c2s_tcp_seg_hexdump_fd, exdata->c2s_rx_tcp_bytes, data, len); @@ -238,7 +236,7 @@ static void on_sess_tcp_stream_msg(struct session *sess, int topic_id, const voi } else { - log_print(ctx->fd, "debug plugin", "rx S2C TCP segment: len: %d, data: %p", len, data); + debug_plugin_log(ctx->fd, "debug plugin", "rx S2C TCP segment: len: %d, data: %p", len, data); hexdump_to_fd(ctx->fd, exdata->s2c_rx_tcp_bytes, data, len); hexdump_to_fd(ctx->s2c_tcp_seg_hexdump_fd, exdata->s2c_rx_tcp_bytes, data, len); @@ -252,57 +250,51 @@ static void on_sess_tcp_stream_msg(struct session *sess, int topic_id, const voi * Plugin API ******************************************************************************/ -extern "C" +void *debug_plugin_init(struct stellar *st) { - void *debug_plugin_init(struct stellar *st) + struct plugin_ctx *ctx = (struct plugin_ctx *)calloc(1, sizeof(struct plugin_ctx)); + if (ctx == NULL) { - struct plugin_ctx *ctx = (struct plugin_ctx *)calloc(1, sizeof(struct plugin_ctx)); - if (ctx == NULL) - { - return NULL; - } + return NULL; + } - ctx->fd = open("./log/debug_plugin.log", O_WRONLY | O_APPEND | O_CREAT, 0644); - if (ctx->fd == -1) - { - printf("[debug plugin] open log file failed: %s\n", strerror(errno)); - free(ctx); - return NULL; - } + ctx->fd = open("./log/debug_plugin.log", O_WRONLY | O_APPEND | O_CREAT, 0644); + if (ctx->fd == -1) + { + printf("[debug plugin] open log file failed: %s\n", strerror(errno)); + free(ctx); + return NULL; + } - pthread_spin_init(&ctx->lock, PTHREAD_PROCESS_PRIVATE); + pthread_spin_init(&ctx->lock, PTHREAD_PROCESS_PRIVATE); - ctx->st = st; - ctx->logger = stellar_get_logger(st); - ctx->sess_exdata_idx = stellar_exdata_new_index(st, "DEBUG_PLUGIN_SESS_EXDATA", stellar_exdata_free_default, NULL); - ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); - ctx->udp_topic_id = stellar_mq_get_topic_id(st, TOPIC_UDP); - ctx->tcp_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP); - ctx->tcp_stream_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP_STREAM); + ctx->st = st; + ctx->sess_exdata_idx = stellar_exdata_new_index(st, "DEBUG_PLUGIN_SESS_EXDATA", stellar_exdata_free_default, NULL); + ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); + ctx->udp_topic_id = stellar_mq_get_topic_id(st, TOPIC_UDP); + ctx->tcp_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP); + ctx->tcp_stream_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP_STREAM); - stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_udp_msg, ctx->sess_plug_id); - stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_tcp_msg, ctx->sess_plug_id); - stellar_session_mq_subscribe(st, ctx->tcp_stream_topic_id, on_sess_tcp_stream_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_udp_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_tcp_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->tcp_stream_topic_id, on_sess_tcp_stream_msg, ctx->sess_plug_id); - STELLAR_LOG_FATAL(ctx->logger, "debug plugin", "init"); - log_print(ctx->fd, "debug plugin", "init"); + debug_plugin_log(ctx->fd, "debug plugin", "init"); - return ctx; - } + return ctx; +} - void debug_plugin_exit(void *plugin_ctx) +void debug_plugin_exit(void *plugin_ctx) +{ + struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; + if (ctx) { - struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; - if (ctx) + debug_plugin_log(ctx->fd, "debug plugin", "exit"); + if (ctx->fd > 0) { - STELLAR_LOG_FATAL(ctx->logger, "debug plugin", "exit"); - log_print(ctx->fd, "debug plugin", "exit"); - if (ctx->fd > 0) - { - close(ctx->fd); - } - pthread_spin_destroy(&ctx->lock); - free(ctx); + close(ctx->fd); } + pthread_spin_destroy(&ctx->lock); + free(ctx); } }
\ No newline at end of file diff --git a/test/packet_inject/packet_inject.c b/test/packet_inject/packet_inject.c index 0ab2ca9..8b20f7f 100644 --- a/test/packet_inject/packet_inject.c +++ b/test/packet_inject/packet_inject.c @@ -536,45 +536,42 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi * Plugin API ******************************************************************************/ -extern "C" +void *packet_inject_init(struct stellar *st) { - void *packet_inject_init(struct stellar *st) + struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)calloc(1, sizeof(struct inject_plugin_ctx)); + if (ctx == NULL) { - struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)calloc(1, sizeof(struct inject_plugin_ctx)); - if (ctx == NULL) - { - return NULL; - } + return NULL; + } - ctx->st = st; - ctx->logger = stellar_get_logger(st); - INJTECT_PLUGIN_LOG_INFO(ctx->logger, "packet inject plugin init"); + ctx->st = st; + ctx->logger = stellar_get_logger(st); + INJTECT_PLUGIN_LOG_INFO(ctx->logger, "packet inject plugin init"); - if (load_config(ctx->logger, &ctx->config, "./plugin/inject.toml") == -1) - { - INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "load config failed"); - free(ctx); - return NULL; - } - print_config(ctx->logger, &ctx->config); + if (load_config(ctx->logger, &ctx->config, "./plugin/inject.toml") == -1) + { + INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "load config failed"); + free(ctx); + return NULL; + } + print_config(ctx->logger, &ctx->config); - ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); - ctx->tcp_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP); - ctx->udp_topic_id = stellar_mq_get_topic_id(st, TOPIC_UDP); + ctx->sess_plug_id = stellar_session_plugin_register(st, on_sess_new, on_sess_free, ctx); + ctx->tcp_topic_id = stellar_mq_get_topic_id(st, TOPIC_TCP); + ctx->udp_topic_id = stellar_mq_get_topic_id(st, TOPIC_UDP); - stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_msg, ctx->sess_plug_id); - stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->tcp_topic_id, on_sess_msg, ctx->sess_plug_id); + stellar_session_mq_subscribe(st, ctx->udp_topic_id, on_sess_msg, ctx->sess_plug_id); - return ctx; - } + return ctx; +} - void packet_inject_exit(void *plugin_ctx) +void packet_inject_exit(void *plugin_ctx) +{ + struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx; + if (ctx) { - struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx; - if (ctx) - { - INJTECT_PLUGIN_LOG_INFO(ctx->logger, "packet inject plugin exit"); - free(ctx); - } + INJTECT_PLUGIN_LOG_INFO(ctx->logger, "packet inject plugin exit"); + free(ctx); } }
\ No newline at end of file |
