2023-12-11 16:35:26 +08:00
|
|
|
#ifndef _SESSION_H
|
|
|
|
|
#define _SESSION_H
|
|
|
|
|
|
|
|
|
|
#ifdef __cpluscplus
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2023-12-15 16:34:53 +08:00
|
|
|
|
|
|
|
|
#include "tuple.h"
|
2023-12-19 10:47:26 +08:00
|
|
|
#include "packet.h"
|
2023-12-13 17:56:27 +08:00
|
|
|
|
2024-01-23 14:30:46 +08:00
|
|
|
enum tcp_state
|
|
|
|
|
{
|
|
|
|
|
// HANDSHAKE
|
|
|
|
|
TCP_SYN_RECVED = 1 << 0,
|
|
|
|
|
TCP_SYNACK_RECVED = 1 << 1,
|
|
|
|
|
TCP_C2S_ACK_RECVED = 1 << 2,
|
|
|
|
|
TCP_S2C_ACK_RECVED = 1 << 3,
|
|
|
|
|
// ESTABLISHED
|
|
|
|
|
TCP_C2S_DATA_RECVED = 1 << 4,
|
|
|
|
|
TCP_S2C_DATA_RECVED = 1 << 5,
|
|
|
|
|
// FIN
|
|
|
|
|
TCP_C2S_FIN_RECVED = 1 << 6,
|
|
|
|
|
TCP_S2C_FIN_RECVED = 1 << 7,
|
|
|
|
|
// RST
|
|
|
|
|
TCP_C2S_RST_RECVED = 1 << 8,
|
|
|
|
|
TCP_S2C_RST_RECVED = 1 << 9,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum udp_state
|
|
|
|
|
{
|
|
|
|
|
UDP_C2S_RECVED = 1 << 0,
|
|
|
|
|
UDP_S2C_RECVED = 1 << 1,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-11 16:35:26 +08:00
|
|
|
enum session_state
|
|
|
|
|
{
|
2024-01-17 11:47:55 +08:00
|
|
|
SESSION_STATE_OPENING = 0x1,
|
|
|
|
|
SESSION_STATE_ACTIVE = 0x2,
|
|
|
|
|
SESSION_STATE_CLOSING = 0x3,
|
|
|
|
|
SESSION_STATE_CLOSED = 0x4,
|
2023-12-11 16:35:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum session_type
|
|
|
|
|
{
|
2024-01-12 12:01:21 +08:00
|
|
|
SESSION_TYPE_TCP = 0x1,
|
|
|
|
|
SESSION_TYPE_UDP = 0x2,
|
2023-12-11 16:35:26 +08:00
|
|
|
};
|
|
|
|
|
|
2023-12-15 11:20:05 +08:00
|
|
|
enum session_dir
|
|
|
|
|
{
|
2024-01-17 11:47:55 +08:00
|
|
|
SESSION_DIR_NONE = 0x0,
|
|
|
|
|
SESSION_DIR_C2S = 0x1,
|
|
|
|
|
SESSION_DIR_S2C = 0x2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum dup_traffic_flag
|
|
|
|
|
{
|
|
|
|
|
DUP_TRAFFIC_YES = 0x1,
|
|
|
|
|
DUP_TRAFFIC_NO = 0x2,
|
2023-12-15 11:20:05 +08:00
|
|
|
};
|
|
|
|
|
|
2024-01-16 14:10:13 +08:00
|
|
|
enum closing_reasion
|
|
|
|
|
{
|
2024-01-17 11:47:55 +08:00
|
|
|
CLOSING_BY_TIMEOUT = 0x1,
|
|
|
|
|
CLOSING_BY_EVICTED = 0x2,
|
|
|
|
|
CLOSING_BY_CLIENT_FIN = 0x3,
|
|
|
|
|
CLOSING_BY_CLIENT_RST = 0x4,
|
|
|
|
|
CLOSING_BY_SERVER_FIN = 0x5,
|
|
|
|
|
CLOSING_BY_SERVER_RST = 0x6,
|
2024-01-16 14:10:13 +08:00
|
|
|
};
|
|
|
|
|
|
2023-12-11 16:35:26 +08:00
|
|
|
struct session;
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2023-12-13 17:56:27 +08:00
|
|
|
* session base info
|
2023-12-11 16:35:26 +08:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
void session_init(struct session *sess);
|
|
|
|
|
|
|
|
|
|
// session id
|
|
|
|
|
void session_set_id(struct session *sess, uint64_t 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
|
|
|
|
2023-12-14 15:23:13 +08:00
|
|
|
// session key
|
2024-01-23 14:30:46 +08:00
|
|
|
void session_set_key(struct session *sess, const struct tuple6 *tuple);
|
|
|
|
|
const struct tuple6 *session_get0_key(const struct session *sess);
|
|
|
|
|
void session_set_key_dir(struct session *sess, enum session_dir dir);
|
|
|
|
|
enum session_dir session_get_key_dir(const struct session *sess);
|
2023-12-11 16:35:26 +08:00
|
|
|
|
|
|
|
|
// session state
|
|
|
|
|
void session_set_state(struct session *sess, enum session_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
|
|
|
|
|
|
|
|
// session type
|
|
|
|
|
void session_set_type(struct session *sess, enum session_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
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
// session dup traffic flag
|
|
|
|
|
void session_set_dup_traffic_flag(struct session *sess, enum dup_traffic_flag flag);
|
|
|
|
|
enum dup_traffic_flag session_get_dup_traffic_flag(const struct session *sess);
|
|
|
|
|
|
2024-01-16 14:10:13 +08:00
|
|
|
// closing reasion
|
|
|
|
|
void session_set_closing_reasion(struct session *sess, enum closing_reasion reasion);
|
|
|
|
|
enum closing_reasion session_get_closing_reasion(const struct session *sess);
|
|
|
|
|
|
2023-12-11 16:35:26 +08:00
|
|
|
// session metrics
|
|
|
|
|
void session_inc_c2s_metrics(struct session *sess, uint64_t packets, uint64_t bytes);
|
|
|
|
|
void session_inc_s2c_metrics(struct session *sess, uint64_t packets, uint64_t bytes);
|
2023-12-19 10:47:26 +08:00
|
|
|
uint64_t session_get_c2s_bytes(const struct session *sess);
|
|
|
|
|
uint64_t session_get_s2c_bytes(const struct session *sess);
|
|
|
|
|
uint64_t session_get_c2s_packets(const struct session *sess);
|
|
|
|
|
uint64_t session_get_s2c_packets(const struct session *sess);
|
2023-12-11 16:35:26 +08:00
|
|
|
|
|
|
|
|
// session timestamp
|
|
|
|
|
void session_set_create_time(struct session *sess, uint64_t timestamp);
|
|
|
|
|
void session_set_last_time(struct session *sess, uint64_t timestamp);
|
2023-12-19 10:47:26 +08:00
|
|
|
uint64_t session_get_create_time(const struct session *sess);
|
|
|
|
|
uint64_t session_get_last_time(const struct session *sess);
|
2023-12-15 11:20:05 +08:00
|
|
|
|
2024-01-23 14:30:46 +08:00
|
|
|
// session tcp state
|
|
|
|
|
void session_set_tcp_state(struct session *sess, enum tcp_state state);
|
|
|
|
|
enum tcp_state session_get_tcp_state(const struct session *sess);
|
|
|
|
|
|
|
|
|
|
// session udp state
|
|
|
|
|
void session_set_udp_state(struct session *sess, enum udp_state state);
|
|
|
|
|
enum udp_state session_get_udp_state(const struct session *sess);
|
|
|
|
|
|
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-23 14:30:46 +08:00
|
|
|
void session_set_c2s_1st_pkt(struct session *sess, const struct packet *pkt);
|
|
|
|
|
void session_set_s2c_1st_pkt(struct session *sess, const struct packet *pkt);
|
2024-01-11 16:46:33 +08:00
|
|
|
const struct packet *session_get0_c2s_1st_pkt(const struct session *sess);
|
|
|
|
|
const struct packet *session_get0_s2c_1st_pkt(const struct session *sess);
|
|
|
|
|
const struct packet *session_get0_1st_pkt(const struct session *sess);
|
|
|
|
|
|
2023-12-15 11:20:05 +08:00
|
|
|
// session current packet
|
|
|
|
|
void session_set0_cur_pkt(struct session *sess, const struct packet *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
|
|
|
|
|
|
|
|
// session current dir
|
|
|
|
|
void session_set_cur_dir(struct session *sess, enum session_dir dir);
|
2023-12-19 10:47:26 +08:00
|
|
|
enum session_dir session_get_cur_dir(const struct session *sess);
|
2023-12-11 16:35:26 +08:00
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
|
* session ex data
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
|
|
typedef void session_ex_free_cb(struct session *sess, uint8_t idx, void *ex_ptr, void *arg);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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);
|
|
|
|
|
/*
|
|
|
|
|
* 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);
|
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
|
|
|
/*
|
|
|
|
|
* 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);
|
2024-01-23 14:30:46 +08:00
|
|
|
void session_free(struct session *sess);
|
2023-12-11 16:35:26 +08:00
|
|
|
|
2023-12-12 18:41:53 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* session expire
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2023-12-13 19:20:34 +08:00
|
|
|
typedef void (*session_expire_cb)(struct session *sess, void *arg);
|
2023-12-12 18:41:53 +08:00
|
|
|
|
|
|
|
|
// session timer
|
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
|
|
|
void session_del_expirecb(struct session *sess);
|
|
|
|
|
void session_run_expirecb(struct session *sess);
|
|
|
|
|
|
2023-12-19 10:47:26 +08:00
|
|
|
/******************************************************************************
|
|
|
|
|
* session dump
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2024-01-16 14:10:13 +08:00
|
|
|
const char *session_closing_reasion_tostring(enum closing_reasion reasion);
|
2023-12-19 10:47:26 +08:00
|
|
|
const char *session_state_tostring(enum session_state state);
|
|
|
|
|
const char *session_type_tostring(enum session_type type);
|
|
|
|
|
const char *session_dir_tostring(enum session_dir dir);
|
|
|
|
|
void session_dump(struct session *sess);
|
|
|
|
|
|
2023-12-11 16:35:26 +08:00
|
|
|
#ifdef __cpluscplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|