This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/session/session.cpp

509 lines
13 KiB
C++
Raw Normal View History

2023-12-11 16:35:26 +08:00
#include <assert.h>
#include "session_private.h"
#define EX_KEY_MAX_LEN 64
struct ex_schema
{
char key[EX_KEY_MAX_LEN];
session_ex_free_cb *free_cb;
void *args;
};
struct ex_manager
{
struct ex_schema schemas[EX_DATA_MAX_COUNT];
uint8_t count;
};
static struct ex_manager g_ex_manager = {0};
2023-12-19 10:47:26 +08:00
uint8_t tcp_builtin_ex = 0; // built-in ex_data index
uint8_t udp_builtin_ex = 0; // built-in ex_data index
2023-12-22 14:45:31 +08:00
uint8_t c2s_1st_md_ex = 0; // built-in ex_data index
uint8_t s2c_1st_md_ex = 0; // built-in ex_data index
uint8_t c2s_1st_pkt_ex = 0; // built-in ex_data index
uint8_t s2c_1st_pkt_ex = 0; // built-in ex_data index
2023-12-11 16:35:26 +08:00
/******************************************************************************
* session
******************************************************************************/
void session_init(struct session *sess)
{
memset(sess, 0, sizeof(struct session));
}
// session id
void session_set_id(struct session *sess, uint64_t id)
{
sess->id = id;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_id(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->id;
}
2023-12-15 16:34:53 +08:00
// session tuple6
void session_set_tuple6(struct session *sess, struct tuple6 *tuple)
2023-12-11 16:35:26 +08:00
{
2023-12-15 16:34:53 +08:00
memcpy(&sess->tuple, tuple, sizeof(struct tuple6));
2023-12-11 16:35:26 +08:00
}
2023-12-19 10:47:26 +08:00
const struct tuple6 *session_get0_tuple6(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
2023-12-15 16:34:53 +08:00
return &sess->tuple;
2023-12-11 16:35:26 +08:00
}
2023-12-19 10:47:26 +08:00
void session_set_tuple6_dir(struct session *sess, enum session_dir dir)
{
sess->tuple_dir = dir;
}
enum session_dir session_get_tuple6_dir(const struct session *sess)
{
return sess->tuple_dir;
}
2023-12-11 16:35:26 +08:00
// session state
void session_set_state(struct session *sess, enum session_state state)
{
sess->state = state;
}
2023-12-19 10:47:26 +08:00
enum session_state session_get_state(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->state;
}
// session type
void session_set_type(struct session *sess, enum session_type type)
{
sess->type = type;
}
2023-12-19 10:47:26 +08:00
enum session_type session_get_type(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->type;
}
// closing reasion
void session_set_closing_reasion(struct session *sess, enum closing_reasion reasion)
{
sess->closing_reasion = reasion;
}
enum closing_reasion session_get_closing_reasion(const struct session *sess)
{
return sess->closing_reasion;
}
2023-12-11 16:35:26 +08:00
// session metrics
void session_inc_c2s_metrics(struct session *sess, uint64_t packets, uint64_t bytes)
{
sess->c2s_bytes += bytes;
sess->c2s_packets += packets;
}
void session_inc_s2c_metrics(struct session *sess, uint64_t packets, uint64_t bytes)
{
sess->s2c_bytes += bytes;
sess->s2c_packets += packets;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_c2s_bytes(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->c2s_bytes;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_s2c_bytes(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->s2c_bytes;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_c2s_packets(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->c2s_packets;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_s2c_packets(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->s2c_packets;
}
// session timestamp
void session_set_create_time(struct session *sess, uint64_t timestamp)
{
sess->create_time = timestamp;
}
void session_set_last_time(struct session *sess, uint64_t timestamp)
{
sess->last_time = timestamp;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_create_time(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->create_time;
}
2023-12-19 10:47:26 +08:00
uint64_t session_get_last_time(const struct session *sess)
2023-12-11 16:35:26 +08:00
{
return sess->last_time;
}
2023-12-15 11:20:05 +08:00
/******************************************************************************
2024-01-11 16:46:33 +08:00
* session packet
2023-12-15 11:20:05 +08:00
******************************************************************************/
2024-01-11 16:46:33 +08:00
const struct packet *session_get0_c2s_1st_pkt(const struct session *sess)
{
return (const struct packet *)session_get0_ex_data(sess, c2s_1st_pkt_ex);
}
const struct packet *session_get0_s2c_1st_pkt(const struct session *sess)
{
return (const struct packet *)session_get0_ex_data(sess, s2c_1st_pkt_ex);
}
const struct packet *session_get0_1st_pkt(const struct session *sess)
{
const struct packet *c2s_1st_pkt = session_get0_c2s_1st_pkt(sess);
if (c2s_1st_pkt)
{
return c2s_1st_pkt;
}
else
{
return session_get0_s2c_1st_pkt(sess);
}
}
2023-12-15 11:20:05 +08:00
// session current packet
void session_set0_cur_pkt(struct session *sess, const struct packet *pkt)
{
sess->cur_pkt = pkt;
}
2023-12-19 10:47:26 +08:00
const struct packet *session_get0_cur_pkt(const struct session *sess)
2023-12-15 11:20:05 +08:00
{
return sess->cur_pkt;
}
// session current dir
void session_set_cur_dir(struct session *sess, enum session_dir dir)
{
sess->cur_dir = dir;
}
2023-12-19 10:47:26 +08:00
enum session_dir session_get_cur_dir(const struct session *sess)
2023-12-15 11:20:05 +08:00
{
return sess->cur_dir;
}
2023-12-11 16:35:26 +08:00
/******************************************************************************
* session ex data
******************************************************************************/
/*
* the exdata prodoced by user, and comsumed by same user.
* so, the exdata is not shared by different user.
* otherwise, the exdata need dup by refer count, and free by refer count.
*
* if key exist, not allow update, return original index.
*/
uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args)
{
if (g_ex_manager.count >= EX_DATA_MAX_COUNT)
{
abort();
return EX_DATA_MAX_COUNT;
}
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
if (strcmp(g_ex_manager.schemas[i].key, key) == 0)
{
return i;
}
}
uint8_t idx = g_ex_manager.count;
g_ex_manager.count++;
struct ex_schema *schema = &g_ex_manager.schemas[idx];
strncpy(schema->key, key, EX_KEY_MAX_LEN);
schema->free_cb = free_cb;
schema->args = args;
return idx;
}
/*
* Support update ex_data.
*
* if key exist: run free_cb free old value, then set new value.
* if not run free_cb, old value will be memory leak.
* if not allow update, new value will be memory leak.
* if key not exist: set new value.
*/
void session_set_ex_data(struct session *sess, uint8_t idx, void *val)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
session_free_ex_data(sess, idx);
sess->ex_data[idx] = val;
}
2023-12-19 10:47:26 +08:00
void *session_get0_ex_data(const struct session *sess, uint8_t idx)
2023-12-11 16:35:26 +08:00
{
if (idx >= g_ex_manager.count)
{
assert(0);
return NULL;
}
return sess->ex_data[idx];
}
/*
* after set ex_data, the owner of ex_data is session, so user should not free it directly.
* if user want to free ex_data, should use session_free_ex_data.
*/
void session_free_ex_data(struct session *sess, uint8_t idx)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
struct ex_schema *schema = &g_ex_manager.schemas[idx];
if (schema->free_cb != NULL && sess->ex_data[idx] != NULL)
{
2024-01-09 18:03:24 +08:00
printf("free ex_data, idx: %d, key: %s, val: %p\n", idx, schema->key, sess->ex_data[idx]);
2023-12-11 16:35:26 +08:00
schema->free_cb(sess, idx, sess->ex_data[idx], schema->args);
}
sess->ex_data[idx] = NULL;
}
2023-12-12 18:41:53 +08:00
/******************************************************************************
* session expire
******************************************************************************/
// session expire
2023-12-13 19:20:34 +08:00
void session_set_expirecb(struct session *sess, session_expire_cb expire_cb, void *expire_arg, uint64_t expire_abs_ts)
2023-12-12 18:41:53 +08:00
{
struct timeout *timeout = &sess->timeout;
timeout_init(timeout, TIMEOUT_ABS);
2023-12-13 19:20:34 +08:00
timeout_setcb(timeout, NULL, sess);
sess->expire_cb = expire_cb;
sess->expire_arg = expire_arg;
sess->expire_abs_ts = expire_abs_ts;
2023-12-12 18:41:53 +08:00
}
void session_del_expirecb(struct session *sess)
{
struct timeout *timeout = &sess->timeout;
timeout_init(timeout, 0);
2023-12-13 19:20:34 +08:00
sess->expire_cb = NULL;
sess->expire_arg = NULL;
sess->expire_abs_ts = 0;
2023-12-12 18:41:53 +08:00
}
void session_run_expirecb(struct session *sess)
{
2023-12-13 19:20:34 +08:00
if (sess->expire_cb)
2023-12-12 18:41:53 +08:00
{
2023-12-13 19:20:34 +08:00
sess->expire_cb(sess, sess->expire_arg);
2023-12-12 18:41:53 +08:00
}
2023-12-19 10:47:26 +08:00
}
/******************************************************************************
* session dump
******************************************************************************/
static void tcp_ex_data_tostring(uint64_t ex_data, char *buffer, size_t buffer_len)
{
if (ex_data == 0)
{
return;
}
int nused = 0;
if (ex_data & TCP_SYN_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_SYN_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_SYNACK_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_SYNACK_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_C2S_PAYLOAD_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_C2S_PAYLOAD_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_S2C_PAYLOAD_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_S2C_PAYLOAD_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_C2S_FIN_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_C2S_FIN_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_S2C_FIN_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_S2C_FIN_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_C2S_RST_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_C2S_RST_RECVED ");
2023-12-19 10:47:26 +08:00
}
if (ex_data & TCP_S2C_RST_RECVED)
{
nused += snprintf(buffer + nused, buffer_len - nused, "TCP_S2C_RST_RECVED ");
2023-12-19 10:47:26 +08:00
}
}
static void udp_ex_data_tostring(uint64_t ex_data, char *buffer, size_t buffer_len)
{
if (ex_data == 0)
{
return;
}
int nused = 0;
if (ex_data & UDP_C2S_RECVED)
{
snprintf(buffer + nused, buffer_len - nused, "UDP_C2S_RECVED ");
}
if (ex_data & UDP_S2C_RECVED)
{
snprintf(buffer + nused, buffer_len - nused, "UDP_S2C_RECVED ");
}
}
const char *session_closing_reasion_tostring(enum closing_reasion reasion)
{
switch (reasion)
{
case CLOSING_BY_TIMEOUT:
return "CLOSING_BY_TIMEOUT";
case CLOSING_BY_EVICTED:
return "CLOSING_BY_EVICTED";
case CLOSING_BY_CLIENT_FIN:
return "CLOSING_BY_CLIENT_FIN";
case CLOSING_BY_CLIENT_RST:
return "CLOSING_BY_CLIENT_RST";
case CLOSING_BY_SERVER_FIN:
return "CLOSING_BY_SERVER_FIN";
case CLOSING_BY_SERVER_RST:
return "CLOSING_BY_SERVER_RST";
default:
return "unknown";
}
}
2023-12-19 10:47:26 +08:00
const char *session_state_tostring(enum session_state state)
{
switch (state)
{
case SESSION_STATE_INIT:
return "init";
case SESSION_STATE_OPENING:
return "opening";
case SESSION_STATE_ACTIVE:
return "active";
case SESSION_STATE_CLOSING:
return "closing";
case SESSION_STATE_CLOSED:
return "closed";
default:
return "unknown";
}
}
const char *session_type_tostring(enum session_type type)
{
switch (type)
{
case SESSION_TYPE_TCP:
return "tcp";
case SESSION_TYPE_UDP:
return "udp";
default:
return "unknown";
}
}
const char *session_dir_tostring(enum session_dir dir)
{
switch (dir)
{
case SESSION_DIR_NONE:
return "none";
case SESSION_DIR_C2S:
return "c2s";
case SESSION_DIR_S2C:
return "s2c";
default:
return "unknown";
}
}
void session_dump(struct session *sess)
{
char buffer[128] = {0};
tuple6_tostring(session_get0_tuple6(sess), buffer, sizeof(buffer));
printf("session id : %" PRIu64 "\n", session_get_id(sess));
printf("session tuple6 key : %s\n", buffer);
printf("session tuple6 dir : %s\n", session_dir_tostring(session_get_tuple6_dir(sess)));
printf("session state : %s\n", session_state_tostring(session_get_state(sess)));
printf("session type : %s\n", session_type_tostring(session_get_type(sess)));
printf("session closing reasion : %s\n", session_closing_reasion_tostring(session_get_closing_reasion(sess)));
printf("session c2s packets : %" PRIu64 "\n", session_get_c2s_packets(sess));
printf("session c2s bytes : %" PRIu64 "\n", session_get_c2s_bytes(sess));
printf("session s2c packets : %" PRIu64 "\n", session_get_s2c_packets(sess));
printf("session s2c bytes : %" PRIu64 "\n", session_get_s2c_bytes(sess));
printf("session create time : %" PRIu64 "\n", session_get_create_time(sess));
printf("session last time : %" PRIu64 "\n", session_get_last_time(sess));
printf("session current packet ptr : %p\n", (void *)session_get0_cur_pkt(sess));
printf("session current packet dir : %s\n", session_dir_tostring(session_get_cur_dir(sess)));
2023-12-19 10:47:26 +08:00
printf("session ex data: \n");
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
if (i == tcp_builtin_ex)
{
memset(buffer, 0, sizeof(buffer));
tcp_ex_data_tostring((uint64_t)sess->ex_data[i], buffer, sizeof(buffer));
printf(" ex_idx: %d, ex_key: %s, ex_val: %s\n", i, g_ex_manager.schemas[i].key, buffer);
}
else if (i == udp_builtin_ex)
{
memset(buffer, 0, sizeof(buffer));
udp_ex_data_tostring((uint64_t)sess->ex_data[i], buffer, sizeof(buffer));
printf(" ex_idx: %d, ex_key: %s, ex_val: %s\n", i, g_ex_manager.schemas[i].key, buffer);
}
else
{
printf(" ex_idx: %d, ex_key: %s, ex_val: %p\n", i, g_ex_manager.schemas[i].key, sess->ex_data[i]);
}
}
2023-12-12 18:41:53 +08:00
}