Removed the ambiguous session_get0_current_payload() API. It is unclear whether the original packet payload or TCP reassembled payload is obtained.

It is recommended to:
* Use packet_get_payload() to obtain the original packet payload
* Use tcp_segment_get_data() to obtain the reassembled data
This commit is contained in:
luwenpeng
2024-08-01 15:14:47 +08:00
parent 776090331e
commit 12975e2da7
9 changed files with 40 additions and 21 deletions

View File

@@ -47,6 +47,10 @@ struct packet *craft_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_se
struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len); struct packet *craft_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len); struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len);
struct tcp_segment;
const char *tcp_segment_get_data(const struct tcp_segment *seg);
uint16_t tcp_segment_get_len(const struct tcp_segment *seg);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -132,7 +132,6 @@ enum session_type session_get_type(const struct session *sess);
enum session_state session_get_current_state(const struct session *sess); enum session_state session_get_current_state(const struct session *sess);
const struct packet *session_get0_current_packet(const struct session *sess); const struct packet *session_get0_current_packet(const struct session *sess);
const char *session_get0_current_payload(const struct session *sess, uint16_t *payload_len);
enum closing_reason session_get_closing_reason(const struct session *sess); enum closing_reason session_get_closing_reason(const struct session *sess);
enum session_direction session_get_direction(const struct session *sess); enum session_direction session_get_direction(const struct session *sess);

View File

@@ -205,7 +205,7 @@ static void *dumpfile_thread(void *arg)
ATOMIC_SET(&handle->io_thread_wait_exit, 1); ATOMIC_SET(&handle->io_thread_wait_exit, 1);
} }
PACKET_IO_LOG_STATE("dumpfile io thread waiting"); PACKET_IO_LOG_STATE("dumpfile io thread waiting exit");
sleep(1); sleep(1);
} }

View File

@@ -195,18 +195,6 @@ const struct packet *session_get0_current_packet(const struct session *sess)
return sess->curr_pkt; return sess->curr_pkt;
} }
const char *session_get0_current_payload(const struct session *sess, uint16_t *payload_len)
{
const struct packet *pkt = session_get0_current_packet(sess);
if (pkt)
{
*payload_len = packet_get_payload_len(pkt);
return packet_get_payload(pkt);
}
*payload_len = 0;
return NULL;
}
int session_is_symmetric(const struct session *sess, unsigned char *flag) int session_is_symmetric(const struct session *sess, unsigned char *flag)
{ {
int is_symmetric = 0; int is_symmetric = 0;

View File

@@ -78,7 +78,6 @@ const struct packet *session_get_first_packet(const struct session *sess, enum f
// session current packet // session current packet
void session_set_current_packet(struct session *sess, const struct packet *pkt); void session_set_current_packet(struct session *sess, const struct packet *pkt);
const struct packet *session_get0_current_packet(const struct session *sess); const struct packet *session_get0_current_packet(const struct session *sess);
const char *session_get0_current_payload(const struct session *sess, uint16_t *payload_len);
// session symmetric // session symmetric
int session_is_symmetric(const struct session *sess, unsigned char *flag); int session_is_symmetric(const struct session *sess, unsigned char *flag);

View File

@@ -18,6 +18,9 @@ global:
craft_udp_packet; craft_udp_packet;
craft_packet_from_scratch; craft_packet_from_scratch;
tcp_segment_get_data;
tcp_segment_get_len;
session_exdata_free; session_exdata_free;
stellar_session_exdata_new_index; stellar_session_exdata_new_index;
session_exdata_set; session_exdata_set;
@@ -37,7 +40,6 @@ global:
session_get_type; session_get_type;
session_get_current_state; session_get_current_state;
session_get0_current_packet; session_get0_current_packet;
session_get0_current_payload;
session_get_closing_reason; session_get_closing_reason;
session_get_direction; session_get_direction;
session_get_current_flow_direction; session_get_current_flow_direction;

View File

@@ -251,4 +251,28 @@ uint32_t tcp_reassembly_get_recv_next(struct tcp_reassembly *assembler)
} }
return assembler->recv_next; return assembler->recv_next;
}
const char *tcp_segment_get_data(const struct tcp_segment *seg)
{
if (seg == NULL)
{
return NULL;
}
else
{
return (const char *)seg->data;
}
}
uint16_t tcp_segment_get_len(const struct tcp_segment *seg)
{
if (seg == NULL)
{
return 0;
}
else
{
return seg->len;
}
} }

View File

@@ -33,6 +33,9 @@ void tcp_reassembly_inc_recv_next(struct tcp_reassembly *assembler, uint32_t off
void tcp_reassembly_set_recv_next(struct tcp_reassembly *assembler, uint32_t seq); void tcp_reassembly_set_recv_next(struct tcp_reassembly *assembler, uint32_t seq);
uint32_t tcp_reassembly_get_recv_next(struct tcp_reassembly *assembler); uint32_t tcp_reassembly_get_recv_next(struct tcp_reassembly *assembler);
const char *tcp_segment_get_data(const struct tcp_segment *seg);
uint16_t tcp_segment_get_len(const struct tcp_segment *seg);
/* /*
* The next routines deal with comparing 32 bit unsigned ints * The next routines deal with comparing 32 bit unsigned ints
* and worry about wraparound (automatic with unsigned arithmetic). * and worry about wraparound (automatic with unsigned arithmetic).

View File

@@ -9,13 +9,11 @@
#include <pthread.h> #include <pthread.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "log.h"
#include "hexdump.h" #include "hexdump.h"
#include "session_utils.h" #include "session_utils.h"
#include "tcp_reassembly.h"
#include "packet_dump.h" #include "packet_dump.h"
#include "packet_parse.h" #include "packet_parse.h"
#include "packet_utils.h" #include "stellar/packet.h"
#include "stellar/session_mq.h" #include "stellar/session_mq.h"
// NOTE: packet hexdump or tcp segment hexdump may be too long, so we need direct output to fd, instead of using log_print // NOTE: packet hexdump or tcp segment hexdump may be too long, so we need direct output to fd, instead of using log_print
@@ -131,15 +129,17 @@ static void on_sess_tcp_stream_msg(struct session *sess, int topic_id, const voi
char buff[4096]; char buff[4096];
struct tcp_segment *seg = (struct tcp_segment *)msg; struct tcp_segment *seg = (struct tcp_segment *)msg;
struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx; struct plugin_ctx *ctx = (struct plugin_ctx *)plugin_ctx;
const char *data = tcp_segment_get_data(seg);
uint16_t len = tcp_segment_get_len(seg);
memset(buff, 0, sizeof(buff)); memset(buff, 0, sizeof(buff));
session_to_str(sess, 1, buff, sizeof(buff) - 1); session_to_str(sess, 1, buff, sizeof(buff) - 1);
log_print(ctx->fd, "debug plugin", "on TCP stream msg: %s", buff); log_print(ctx->fd, "debug plugin", "on TCP stream msg: %s", buff);
log_print(ctx->fd, "debug plugin", "rx TCP segment: len: %d, data: %p\n", seg->len, seg->data); log_print(ctx->fd, "debug plugin", "rx TCP segment: len: %d, data: %p\n", len, data);
pthread_spin_lock(&ctx->lock); pthread_spin_lock(&ctx->lock);
hexdump_to_fd(ctx->fd, (const char *)seg->data, seg->len); hexdump_to_fd(ctx->fd, data, len);
pthread_spin_unlock(&ctx->lock); pthread_spin_unlock(&ctx->lock);
} }