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.h
2024-03-08 18:10:38 +08:00

210 lines
6.8 KiB
C

#ifndef _SESSION_H
#define _SESSION_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
#include "tuple.h"
#include "packet.h"
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,
};
enum session_state
{
SESSION_STATE_OPENING = 0x1,
SESSION_STATE_ACTIVE = 0x2,
SESSION_STATE_CLOSING = 0x3,
SESSION_STATE_CLOSED = 0x4,
};
enum session_type
{
SESSION_TYPE_TCP = 0x1,
SESSION_TYPE_UDP = 0x2,
};
enum session_dir
{
SESSION_DIR_NONE = 0x0,
SESSION_DIR_C2S = 0x1,
SESSION_DIR_S2C = 0x2,
};
enum dup_traffic_flag
{
DUP_TRAFFIC_NO = 0x0,
DUP_TRAFFIC_YES = 0x1,
};
enum closing_reason
{
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,
};
struct session;
/******************************************************************************
* session base info
******************************************************************************/
void session_init(struct session *sess);
// session id
void session_set_id(struct session *sess, uint64_t id);
uint64_t session_get_id(const struct session *sess);
// session key
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);
// session state
void session_set_state(struct session *sess, enum session_state state);
enum session_state session_get_state(const struct session *sess);
// session type
void session_set_type(struct session *sess, enum session_type type);
enum session_type session_get_type(const struct session *sess);
// 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);
// closing reason
void session_set_closing_reason(struct session *sess, enum closing_reason reason);
enum closing_reason session_get_closing_reason(const struct session *sess);
// 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);
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);
// session timestamp
void session_set_new_time(struct session *sess, uint64_t timestamp);
void session_set_last_time(struct session *sess, uint64_t timestamp);
uint64_t session_get_new_time(const struct session *sess);
uint64_t session_get_last_time(const struct session *sess);
// 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);
// session user data
void session_set_user_data(struct session *sess, void *user_data);
void *session_get_user_data(const struct session *sess);
/******************************************************************************
* session packet
******************************************************************************/
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);
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);
// session current packet
void session_set0_cur_pkt(struct session *sess, const struct packet *pkt);
const struct packet *session_get0_cur_pkt(const struct session *sess);
// session current dir
void session_set_cur_dir(struct session *sess, enum session_dir dir);
enum session_dir session_get_cur_dir(const struct session *sess);
/******************************************************************************
* 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);
void *session_get0_ex_data(const struct session *sess, uint8_t 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);
void session_free(struct session *sess);
/******************************************************************************
* session expire
******************************************************************************/
typedef void (*session_expire_cb)(struct session *sess, void *arg);
// session timer
void session_set_expirecb(struct session *sess, session_expire_cb expire_cb, void *expire_arg, uint64_t expire_abs_ts);
void session_del_expirecb(struct session *sess);
void session_run_expirecb(struct session *sess);
/******************************************************************************
* session dump
******************************************************************************/
const char *session_closing_reason_to_str(enum closing_reason reason);
const char *session_state_to_str(enum session_state state);
const char *session_type_to_str(enum session_type type);
const char *session_dir_to_str(enum session_dir dir);
void session_dump(struct session *sess);
#ifdef __cpluscplus
}
#endif
#endif