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
|
|
|
|
2023-12-11 16:35:26 +08:00
|
|
|
enum session_state
|
|
|
|
|
{
|
2023-12-14 15:23:13 +08:00
|
|
|
SESSION_STATE_INIT = 0,
|
2023-12-11 16:35:26 +08:00
|
|
|
SESSION_STATE_OPENING,
|
|
|
|
|
SESSION_STATE_ACTIVE,
|
2023-12-14 15:23:13 +08:00
|
|
|
SESSION_STATE_CLOSING,
|
|
|
|
|
SESSION_STATE_CLOSED,
|
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
|
|
|
|
|
{
|
2023-12-19 10:47:26 +08:00
|
|
|
SESSION_DIR_NONE = 0,
|
|
|
|
|
SESSION_DIR_C2S = 1,
|
|
|
|
|
SESSION_DIR_S2C = 2,
|
2023-12-15 11:20:05 +08:00
|
|
|
};
|
|
|
|
|
|
2024-01-16 14:10:13 +08:00
|
|
|
enum closing_reasion
|
|
|
|
|
{
|
|
|
|
|
CLOSING_BY_TIMEOUT = 1,
|
|
|
|
|
CLOSING_BY_EVICTED = 2,
|
|
|
|
|
CLOSING_BY_CLIENT_FIN = 3,
|
|
|
|
|
CLOSING_BY_CLIENT_RST = 4,
|
|
|
|
|
CLOSING_BY_SERVER_FIN = 5,
|
|
|
|
|
CLOSING_BY_SERVER_RST = 6,
|
|
|
|
|
};
|
|
|
|
|
|
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
|
2023-12-15 16:34:53 +08:00
|
|
|
void session_set_tuple6(struct session *sess, struct tuple6 *tuple);
|
2023-12-19 10:47:26 +08:00
|
|
|
const struct tuple6 *session_get0_tuple6(const struct session *sess);
|
|
|
|
|
void session_set_tuple6_dir(struct session *sess, enum session_dir dir);
|
|
|
|
|
enum session_dir session_get_tuple6_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-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-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);
|
|
|
|
|
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);
|
|
|
|
|
|
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
|