#ifndef __LTSM_H_ #define __LTSM_H_ #ifdef __cplusplus extern "C" { #endif /* LTSM : Light TCP State Machine. Version : 2018-01-16 */ /* RFC标准TCP协议栈状态机定义 */ enum full_tcp_state{ FTSM_VOID, /* 如果一个流的第一个包不是SYN包, 返回此值 */ FTSM_SYN_SENT, FTSM_SYN_RCVD, FTSM_ESTABLISHED, FTSM_FIN_WAIT, FTSM_CLOSE_WAIT, FTSM_LAST_ACK, FTSM_TIME_WAIT, FTSM_CLOSED, FTSM_LISTEN, __FTSM_MAX, }; /* 轻量TCP状态机定义, 实际只有三种状态:新建连接, 传输数据, 结束连接, 每个流必定包括1个START和1个CLOSE状态, 可能包括0或若干个DATA状态. VOID状态用于调用者快速判断是否需要创建新流. */ enum light_tcp_state{ LTSM_VOID, /* 如果一个流的第一个包不是SYN包, 返回此值 */ LTSM_START, LTSM_DATA, LTSM_CLOSE, }; struct ltsm_iphdr { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ #endif #if __BYTE_ORDER == __BIG_ENDIAN unsigned int ip_v:4; /* version */ unsigned int ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_short ip_len; /* total length */ u_short ip_id; /* identification */ u_short ip_off; /* fragment offset field */ #define IP_RF 0x8000 /* reserved fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_short ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ }; struct ltsm_tcphdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ u_int32_t th_seq; /* sequence number */ u_int32_t th_ack; /* acknowledgement number */ # if __BYTE_ORDER == __LITTLE_ENDIAN u_int8_t th_x2:4; /* (unused) */ u_int8_t th_off:4; /* data offset */ # endif # if __BYTE_ORDER == __BIG_ENDIAN u_int8_t th_off:4; /* data offset */ u_int8_t th_x2:4; /* (unused) */ # endif u_int8_t th_flags; # define TH_FIN 0x01 # define TH_SYN 0x02 # define TH_RST 0x04 # define TH_PUSH 0x08 # define TH_ACK 0x10 # define TH_URG 0x20 u_int16_t th_win; /* window */ u_int16_t th_sum; /* checksum */ u_int16_t th_urp; /* urgent pointer */ }; struct ltsm_result{ enum full_tcp_state fstate; enum light_tcp_state lstate; }; typedef void * ltsm_stream_handle; ltsm_stream_handle ltsm_create_handle(void); #define LTSM_DIR_C2S 0 #define LTSM_DIR_S2C 1 struct ltsm_result ltsm_get_current_state(ltsm_stream_handle pltsm, uint8_t tcp_flags, uint8_t dir); const char *ltsm_fstate_ntop(enum full_tcp_state fstate); const char *ltsm_lstate_ntop(enum light_tcp_state lstate); void ltsm_destroy_handle(ltsm_stream_handle pltsm); #ifdef __cplusplus } #endif #endif