#ifndef _SESSION_PRIV_H
#define _SESSION_PRIV_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "list.h"
#include "packet_priv.h"
#include "timeout.h"
#include "uthash.h"
#include "stellar/tuple.h"
#include "stellar/session.h"
#include "tcp_reassembly.h"
#include "session_manager.h"

#define EX_DATA_MAX_COUNT 4

// output format: "${src_addr}:${src_port}-${dst_addr}:${dst_port}-${ip_proto}-${domain}"
// output max len: (46 + 1 + 5) + 1 + (46 + 1 + 5) + 1 + 1 + 1 + 20 = 129
#define TUPLE6_STR_SIZE 130

struct tcp_half
{
    struct tcp_reassembly *assembler;
    struct tcp_segment in_order; // current packet in order segment
    uint32_t in_order_ref;       // reference count of current packet in order segment

    uint32_t inject_inc_seq_offset; // inject packet base on current packet increase seq offset
    uint32_t inject_inc_ack_offset; // inject packet base on current packet increase ack offset

    uint32_t seq;  // current packet sequence number
    uint32_t ack;  // current packet ack number
    uint16_t len;  // current packet payload length
    uint8_t flags; // current packet flags

    uint32_t isn;    // current direction initial sequence number
    uint8_t history; // current direction received flags
};

/*
 * sizeof(struct session) > 1024 bytes
 * max thread number = 128
 * per thread max tcp session number = 50000
 * per thread max udp session number = 50000
 *
 * session memory usage = 128 * (50000 + 50000) * 1024 = 13107200000 bytes = 12.2 GB
 */
struct session
{
    uint64_t id;
    uint64_t stats[MAX_FLOW_DIRECTION][MAX_STAT];
    uint64_t timestamps[MAX_TIMESTAMP]; // realtime seconds
    struct tcp_half tcp_halfs[MAX_FLOW_DIRECTION];
    struct timeout timeout;
    struct list_head lru;
    struct list_head free;
    struct list_head evicte;
    UT_hash_handle hh1;
    UT_hash_handle hh2;
    UT_hash_handle hh3;
    struct tuple6 tuple;
    char tuple_str[TUPLE6_STR_SIZE];
    struct sid_list sids[MAX_FLOW_DIRECTION];
    struct route_ctx route_ctx[MAX_FLOW_DIRECTION];
    const struct packet *first_pkt[MAX_FLOW_DIRECTION];
    const struct packet *curr_pkt;
    void *ex_data[EX_DATA_MAX_COUNT];
    void *user_data;
    int is_symmetric;
    int dup;
    enum session_direction sess_dir;
    enum flow_direction tuple_dir;
    enum flow_direction flow_dir;
    enum session_type type;
    enum session_state state;
    enum closing_reason reason;
    struct session_manager *mgr;
    struct session_manager_stat *mgr_stat;
};

void session_init(struct session *sess);
void session_set_id(struct session *sess, uint64_t id);

void session_set_tuple(struct session *sess, const struct tuple6 *key);
void session_set_tuple_direction(struct session *sess, enum flow_direction dir);

void session_set_direction(struct session *sess, enum session_direction dir);
void session_set_current_flow_direction(struct session *sess, enum flow_direction dir);
void session_set_current_state(struct session *sess, enum session_state state);
void session_set_type(struct session *sess, enum session_type type);
void session_set_duplicate_traffic(struct session *sess);
void session_set_closing_reason(struct session *sess, enum closing_reason reason);
void session_inc_stat(struct session *sess, enum flow_direction dir, enum session_stat stat, uint64_t val);
void session_set_timestamp(struct session *sess, enum session_timestamp type, uint64_t value);

void session_clear_sid_list(struct session *sess, enum flow_direction dir);
void session_set_sid_list(struct session *sess, enum flow_direction dir, const struct sid_list *list);
void session_get_sid_list(const struct session *sess, enum flow_direction dir, struct sid_list *list);

void session_clear_route_ctx(struct session *sess, enum flow_direction dir);
void session_set_route_ctx(struct session *sess, enum flow_direction dir, const struct route_ctx *ctx);
void session_get_route_ctx(const struct session *sess, enum flow_direction dir, struct route_ctx *ctx);

void session_set_first_packet(struct session *sess, enum flow_direction dir, const struct packet *pkt);
void session_set_current_packet(struct session *sess, const struct packet *pkt);

void session_set_user_data(struct session *sess, void *user_data);
void *session_get_user_data(const struct session *sess);

struct tcp_segment *session_get_tcp_segment(struct session *sess);
void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg);

/******************************************************************************
 * 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_all_ex_data(struct session *sess);

/******************************************************************************
 * debug
 ******************************************************************************/

const char *session_type_to_str(enum session_type type);
const char *session_state_to_str(enum session_state state);
const char *flow_direction_to_str(enum flow_direction dir);
const char *closing_reason_to_str(enum closing_reason reason);
void session_print(struct session *sess);
int session_to_json(struct session *sess, char *buff, int size);

#ifdef __cplusplus
}
#endif

#endif
