export the logging API symbol

This commit is contained in:
luwenpeng
2024-08-26 19:01:10 +08:00
parent 83eb9d9c5c
commit 11bf852c15
4 changed files with 114 additions and 108 deletions

View File

@@ -6,19 +6,16 @@
#include <arpa/inet.h>
#include "toml.h"
#include "stellar/stellar.h"
#include "stellar/log.h"
#include "stellar/packet.h"
#include "stellar/session.h"
#include "stellar/stellar.h"
#include "stellar/stellar_mq.h"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#define LOG_ERR(fmt, ...) printf("ERROR [packet inject] " fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) printf("INFO [packet inject] " fmt, ##__VA_ARGS__)
/******************************************************************************
* Config
******************************************************************************/
#define INJTECT_PLUGIN_LOG_ERROR(logger, format, ...) STELLAR_LOG_ERROR(logger, "inject plugin", format, ##__VA_ARGS__)
#define INJTECT_PLUGIN_LOG_INFO(logger, format, ...) STELLAR_LOG_INFO(logger, "inject plugin", format, ##__VA_ARGS__)
enum inject_type
{
@@ -44,7 +41,43 @@ struct config
enum flow_direction direction;
};
static const char *inject_type_to_str(enum inject_type type)
struct inject_plugin_ctx
{
struct config config;
struct stellar *st;
struct logger *logger;
int sess_plug_id;
int tcp_topic_id;
int udp_topic_id;
};
struct packet_exdata
{
enum flow_direction flow_dir;
union
{
struct in_addr v4;
struct in6_addr v6;
} src_addr, dst_addr;
uint16_t src_port; // host byte order
uint16_t dst_port; // host byte order
uint16_t tcp_payload_len;
uint32_t tcp_seq; // host byte order
uint32_t tcp_ack; // host byte order
uint8_t tcp_flags;
uint32_t inc_seq;
uint32_t inc_ack;
};
/******************************************************************************
* Config
******************************************************************************/
static const char *type_to_str(enum inject_type type)
{
switch (type)
{
@@ -65,7 +98,7 @@ static const char *inject_type_to_str(enum inject_type type)
}
}
static int load_config(struct config *config, const char *file)
static int load_config(struct logger *logger, struct config *config, const char *file)
{
int ret = -1;
char errbuf[200];
@@ -78,28 +111,28 @@ static int load_config(struct config *config, const char *file)
fp = fopen(file, "r");
if (fp == NULL)
{
LOG_ERR("open config file %s failed, %s\n", file, strerror(errno));
INJTECT_PLUGIN_LOG_ERROR(logger, "open config file %s failed, %s", file, strerror(errno));
goto error_out;
}
root = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (root == NULL)
{
LOG_ERR("parse config file %s failed, %s\n", file, errbuf);
INJTECT_PLUGIN_LOG_ERROR(logger, "parse config file %s failed, %s", file, errbuf);
goto error_out;
}
sub = toml_table_in(root, "packet_inject");
if (sub == NULL)
{
LOG_ERR("config file missing packet_inject section\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject section");
goto error_out;
}
ptr = toml_raw_in(sub, "filter_ip");
if (ptr == NULL)
{
LOG_ERR("config file missing packet_inject->filter_ip\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->filter_ip");
goto error_out;
}
if (strcmp(ptr, "any") == 0)
@@ -116,14 +149,14 @@ static int load_config(struct config *config, const char *file)
}
else
{
LOG_ERR("parse packet_inject->filter_ip failed, invalid ip address: %s\n", ptr);
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->filter_ip failed, invalid ip address: %s", ptr);
goto error_out;
}
ptr = toml_raw_in(sub, "filter_port");
if (ptr == NULL)
{
LOG_ERR("config file missing packet_inject->filter_port\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->filter_port");
goto error_out;
}
config->port = atoi(ptr);
@@ -131,7 +164,7 @@ static int load_config(struct config *config, const char *file)
ptr = toml_raw_in(sub, "filter_dir");
if (ptr == NULL)
{
LOG_ERR("config file missing packet_inject->filter_dir\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->filter_dir");
goto error_out;
}
if (strcmp(ptr, "C2S") == 0)
@@ -144,27 +177,27 @@ static int load_config(struct config *config, const char *file)
}
else
{
LOG_ERR("parse packet_inject->filter_dir failed, invalid direction: %s\n", ptr);
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->filter_dir failed, invalid direction: %s", ptr);
goto error_out;
}
ptr = toml_raw_in(sub, "filter_pkts");
if (ptr == NULL)
{
LOG_ERR("config file missing packet_inject->filter_pkts\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->filter_pkts");
goto error_out;
}
config->number = atoi(ptr);
if (config->number == 0)
{
LOG_ERR("parse packet_inject->filter_pkts failed, invalid number: %s\n", ptr);
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->filter_pkts failed, invalid number: %s", ptr);
goto error_out;
}
ptr = toml_raw_in(sub, "inject_type");
if (ptr == NULL)
{
LOG_ERR("config file missing packet_inject->inject_type\n");
INJTECT_PLUGIN_LOG_ERROR(logger, "config file missing packet_inject->inject_type");
goto error_out;
}
if (strcmp(ptr, "TCP-RST") == 0)
@@ -193,7 +226,7 @@ static int load_config(struct config *config, const char *file)
}
else
{
LOG_ERR("parse packet_inject->inject_type failed, invalid inject type: %s\n", ptr);
INJTECT_PLUGIN_LOG_ERROR(logger, "parse packet_inject->inject_type failed, invalid inject type: %s", ptr);
goto error_out;
}
@@ -213,7 +246,7 @@ error_out:
return ret;
}
static void print_config(const struct config *config)
static void print_config(struct logger *logger, const struct config *config)
{
char addr_str[INET6_ADDRSTRLEN] = {0};
@@ -230,39 +263,17 @@ static void print_config(const struct config *config)
break;
}
LOG_INFO("config->filter_ip : %s\n", addr_str);
LOG_INFO("config->filter_port : %d\n", config->port);
LOG_INFO("config->filter_dir : %s\n", config->direction == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
LOG_INFO("config->filter_pkts : %lu\n", config->number);
LOG_INFO("config->inject_type : %s\n", inject_type_to_str(config->type));
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_ip : %s", addr_str);
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_port : %d", config->port);
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_dir : %s", config->direction == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
INJTECT_PLUGIN_LOG_INFO(logger, "config->filter_pkts : %lu", config->number);
INJTECT_PLUGIN_LOG_INFO(logger, "config->inject_type : %s", type_to_str(config->type));
}
/******************************************************************************
* Utils
******************************************************************************/
struct packet_exdata
{
enum flow_direction flow_dir;
union
{
struct in_addr v4;
struct in6_addr v6;
} src_addr, dst_addr;
uint16_t src_port; // host byte order
uint16_t dst_port; // host byte order
uint16_t tcp_payload_len;
uint32_t tcp_seq; // host byte order
uint32_t tcp_ack; // host byte order
uint8_t tcp_flags;
uint32_t inc_seq;
uint32_t inc_ack;
};
static inline void packet_exdata_init(const struct packet *pkt, enum flow_direction dir, struct packet_exdata *pkt_exdata)
{
memset(pkt_exdata, 0, sizeof(struct packet_exdata));
@@ -323,27 +334,27 @@ static inline uint32_t uint32_add(uint32_t seq, uint32_t inc)
return seq;
}
static void craft_and_send_udp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata,
static void build_and_send_udp_packet(struct inject_plugin_ctx *ctx, struct session *sess, struct packet_exdata *pkt_exdata,
enum flow_direction inject_dir, const char *udp_payload, uint16_t udp_payload_len)
{
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
if (origin_pkt == NULL)
{
LOG_ERR("craft UDP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build UDP packet failed, %s origin packet is NULL", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
return;
}
struct packet *craft_pkt = packet_build_udp(origin_pkt, udp_payload, udp_payload_len);
if (craft_pkt == NULL)
struct packet *build_pkt = packet_build_udp(origin_pkt, udp_payload, udp_payload_len);
if (build_pkt == NULL)
{
LOG_ERR("craft UDP packet failed\n");
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build UDP packet failed");
return;
}
stellar_send_build_packet(st, craft_pkt);
stellar_send_build_packet(ctx->st, build_pkt);
}
static void craft_and_send_tcp_packet(struct stellar *st, struct session *sess, struct packet_exdata *pkt_exdata,
static void build_and_send_tcp_packet(struct inject_plugin_ctx *ctx, struct session *sess, struct packet_exdata *pkt_exdata,
enum flow_direction inject_dir, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len)
{
uint32_t tcp_seq = 0;
@@ -390,44 +401,35 @@ static void craft_and_send_tcp_packet(struct stellar *st, struct session *sess,
const struct packet *origin_pkt = session_get_first_packet(sess, inject_dir);
if (origin_pkt == NULL)
{
LOG_ERR("craft TCP packet failed, %s origin packet is NULL\n", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build TCP packet failed, %s origin packet is NULL", inject_dir == FLOW_DIRECTION_C2S ? "C2S" : "S2C");
return;
}
struct packet *craft_pkt = packet_build_tcp(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len);
if (craft_pkt == NULL)
struct packet *build_pkt = packet_build_tcp(origin_pkt, tcp_seq, tcp_ack, tcp_flags, NULL, 0, tcp_payload, tcp_payload_len);
if (build_pkt == NULL)
{
LOG_ERR("craft TCP packet failed\n");
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "build TCP packet failed");
return;
}
stellar_send_build_packet(st, craft_pkt);
stellar_send_build_packet(ctx->st, build_pkt);
}
/******************************************************************************
* Core logic
******************************************************************************/
struct plugin_ctx
{
struct config config;
struct stellar *st;
int sess_plug_id;
int tcp_topic_id;
int udp_topic_id;
};
static void *on_sess_new(struct session *sess, void *plugin_ctx)
{
// struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
LOG_INFO("handle session new: %s\n", session_get0_readable_addr(sess));
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx;
INJTECT_PLUGIN_LOG_INFO(ctx->logger, "handle session new: %s", session_get0_readable_addr(sess));
return NULL;
}
static void on_sess_free(struct session *sess, void *sess_ctx, void *plugin_ctx)
{
// struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
LOG_INFO("handle session free: %s\n", session_get0_readable_addr(sess));
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx;
INJTECT_PLUGIN_LOG_INFO(ctx->logger, "handle session free: %s", session_get0_readable_addr(sess));
}
static void on_sess_msg(struct session *sess, int topic_id, const void *msg, void *sess_ctx, void *plugin_ctx)
@@ -439,14 +441,13 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
char buffer[1024] = {0};
struct packet *pkt = (struct packet *)msg;
struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
struct stellar *st = ctx->st;
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx;
struct config *config = &ctx->config;
enum flow_direction flow_dir = session_get_current_flow_direction(sess);
LOG_INFO("handle session msg: %s (C2S received packets: %lu, S2C received packets: %lu)\n",
session_get0_readable_addr(sess),
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED),
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED));
INJTECT_PLUGIN_LOG_INFO(ctx->logger, "handle session msg: %s (C2S received packets: %lu, S2C received packets: %lu)",
session_get0_readable_addr(sess),
session_get_stat(sess, FLOW_DIRECTION_C2S, STAT_RAW_PACKETS_RECEIVED),
session_get_stat(sess, FLOW_DIRECTION_S2C, STAT_RAW_PACKETS_RECEIVED));
struct packet_exdata pkt_exdata;
packet_exdata_init(pkt, flow_dir, &pkt_exdata);
@@ -491,36 +492,36 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
switch (config->type)
{
case INJECT_TCP_RST:
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0);
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0);
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0);
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0);
session_set_discard(sess);
break;
case INJECT_TCP_FIN:
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0);
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0);
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0);
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0);
session_set_discard(sess);
break;
case INJECT_TCP_PAYLOAD:
snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello");
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
session_set_discard(sess);
break;
case INJECT_TCP_PAYLOAD_FIN_RST:
snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s", 5 + 5 + 2, "Hello");
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server
craft_and_send_tcp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, buffer, strlen(buffer)); // inject payload to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_ACK, "World\r\n", 7); // inject payload to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_FIN | TH_ACK, NULL, 0); // inject FIN to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, TH_RST | TH_ACK, NULL, 0); // inject RST to client
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_FIN | TH_ACK, NULL, 0); // inject FIN to server
build_and_send_tcp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, TH_RST | TH_ACK, NULL, 0); // inject RST to server
session_set_discard(sess);
break;
case INJECT_UDP_PAYLOAD:
craft_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12);
craft_and_send_udp_packet(st, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12);
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_C2S, "Hello Server", 12);
build_and_send_udp_packet(ctx, sess, &pkt_exdata, FLOW_DIRECTION_S2C, "Hello Client", 12);
session_set_discard(sess);
break;
case INJECT_CTRL_MSG:
@@ -537,24 +538,26 @@ static void on_sess_msg(struct session *sess, int topic_id, const void *msg, voi
extern "C"
{
void *packet_inject_init(struct stellar *st)
{
struct plugin_ctx *ctx = (struct plugin_ctx *)calloc(1, sizeof(struct plugin_ctx));
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)calloc(1, sizeof(struct inject_plugin_ctx));
if (ctx == NULL)
{
return NULL;
}
if (load_config(&ctx->config, "./plugin/inject.toml") == -1)
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)
{
LOG_ERR("load config failed\n");
INJTECT_PLUGIN_LOG_ERROR(ctx->logger, "load config failed");
free(ctx);
return NULL;
}
print_config(&ctx->config);
print_config(ctx->logger, &ctx->config);
ctx->st = st;
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);
@@ -562,17 +565,15 @@ extern "C"
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);
LOG_INFO("init\n");
return ctx;
}
void packet_inject_exit(void *plugin_ctx)
{
struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
struct inject_plugin_ctx *ctx = (struct inject_plugin_ctx *)plugin_ctx;
if (ctx)
{
LOG_INFO("exit\n");
INJTECT_PLUGIN_LOG_INFO(ctx->logger, "packet inject plugin exit");
free(ctx);
}
}