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

180 lines
5.5 KiB
C
Raw Normal View History

2023-12-11 16:35:26 +08:00
#ifndef _SESSION_H
#define _SESSION_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdint.h>
#include <arpa/inet.h>
2023-12-11 16:35:26 +08:00
enum session_state
{
SESSION_STATE_INIT = 0,
2023-12-11 16:35:26 +08:00
SESSION_STATE_OPENING,
SESSION_STATE_ACTIVE,
SESSION_STATE_DISCARD,
SESSION_STATE_CLOSING,
SESSION_STATE_CLOSED,
2023-12-11 16:35:26 +08:00
};
enum session_type
{
SESSION_TYPE_NONE = 0,
2023-12-11 16:35:26 +08:00
SESSION_TYPE_TCP,
SESSION_TYPE_TCP_STREAM,
2023-12-11 16:35:26 +08:00
SESSION_TYPE_UDP,
};
enum session_event
{
SESSION_EVENT_NONE = 0,
SESSION_EVENT_OPENING,
SESSION_EVENT_ACTIVE,
SESSION_EVENT_CLOSING,
2023-12-11 16:35:26 +08:00
// Add new event before SESSION_EVENT_MAX
SESSION_EVENT_MAX,
};
union ip_address
{
struct in_addr v4; /* network order */
struct in6_addr v6; /* network order */
};
enum ip_type
{
IP_TYPE_V4,
IP_TYPE_V6,
};
struct session_key
{
enum ip_type ip_type;
// six tuple
union ip_address src_addr; /* network order */
union ip_address dst_addr; /* network order */
uint16_t src_port; /* network order */
uint16_t dst_port; /* network order */
uint16_t ip_proto; /* network order */
uint64_t security_zone;
};
2023-12-11 16:35:26 +08:00
struct metadata
{
char data[64]; // TODO
};
struct session;
/******************************************************************************
* session key
******************************************************************************/
uint32_t session_key_hash(const struct session_key *key);
int session_key_cmp(const struct session_key *key1, const struct session_key *key2);
void session_key_reverse(const struct session_key *in, struct session_key *out);
void session_key_tostring(const struct session_key *key, char *buf, uint32_t buf_len);
2023-12-11 16:35:26 +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);
uint64_t session_get_id(struct session *sess);
// session key
void session_set_key(struct session *sess, struct session_key *key);
struct session_key *session_get0_key(struct session *sess);
2023-12-11 16:35:26 +08:00
// session state
void session_set_state(struct session *sess, enum session_state state);
enum session_state session_get_state(struct session *sess);
// session type
void session_set_type(struct session *sess, enum session_type type);
enum session_type session_get_type(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(struct session *sess);
uint64_t session_get_s2c_bytes(struct session *sess);
uint64_t session_get_c2s_packets(struct session *sess);
uint64_t session_get_s2c_packets(struct session *sess);
// session metadata
void session_set_c2s_1st_md(struct session *sess, struct metadata *md);
void session_set_s2c_1st_md(struct session *sess, struct metadata *md);
struct metadata *session_get0_c2s_1st_md(struct session *sess);
struct metadata *session_get0_s2c_1st_md(struct session *sess);
// session timestamp
void session_set_create_time(struct session *sess, uint64_t timestamp);
void session_set_last_time(struct session *sess, uint64_t timestamp);
uint64_t session_get_create_time(struct session *sess);
uint64_t session_get_last_time(struct session *sess);
uint64_t session_get_expire_time(struct session *sess);
/******************************************************************************
* session event
******************************************************************************/
2023-12-11 16:35:26 +08:00
// session event
bool session_push_event(struct session *sess, uint32_t event);
bool session_pop_event(struct session *sess, uint32_t *event);
/******************************************************************************
* 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(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);
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-11 16:35:26 +08:00
#ifdef __cpluscplus
}
#endif
#endif