整理目录结构,编写CMakeLists.txt文件
This commit is contained in:
20
common/include/tfe_future.h
Normal file
20
common/include/tfe_future.h
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
enum e_future_error
|
||||
{
|
||||
FUTURE_ERROR_CANCEL,
|
||||
FUTURE_ERROR_EXCEPTION,
|
||||
FUTURE_ERROR_TIMEOUT
|
||||
};
|
||||
struct promise;
|
||||
struct future;
|
||||
typedef void (*future_success_cb)(void * result, void * user);
|
||||
typedef void (*future_failed_cb)(enum e_future_error err, const char * what, void * user);
|
||||
typedef void (*promise_ctx_destroy_cb)(struct promise* p);
|
||||
|
||||
struct future* future_create(future_success_cb * cb_success, future_failed_cb * cb_failed, void * user);
|
||||
|
||||
struct future* promise_to_future(struct promise* p);
|
||||
struct promise* future_to_promise(struct future* f);
|
||||
|
||||
|
||||
|
||||
149
common/include/tfe_http.h
Normal file
149
common/include/tfe_http.h
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "tfe_connection.h"
|
||||
#include <stdint.h>
|
||||
|
||||
struct tfe_http_req_spec
|
||||
{
|
||||
int method;
|
||||
char* uri;
|
||||
char* host;
|
||||
char* url; //uri+host
|
||||
char* accept_encoding;
|
||||
};
|
||||
struct tfe_http_resp_spec
|
||||
{
|
||||
int resp_code;
|
||||
int major_version; //HTTP/1.x or HTTP/ 2
|
||||
int minor_version; //HTTP 1.1 or 1.0
|
||||
char* content_encoding;
|
||||
};
|
||||
#define HTTP_REQUEST 1
|
||||
#define HTTP_RESPONSE 2
|
||||
struct tfe_http_half
|
||||
{
|
||||
int direction; //HTTP_REQUEST or HTTP_RESPONSE
|
||||
union
|
||||
{
|
||||
struct tfe_http_req_spec req_spec;
|
||||
struct tfe_http_resp_spec resp_spec;
|
||||
};
|
||||
size_t field_cnt;
|
||||
uint64_t cont_len;
|
||||
uint64_t cont_range_from;
|
||||
uint64_t cont_range_to;
|
||||
struct evbuffer *body;
|
||||
void * fields; //hide by protocol layer
|
||||
};
|
||||
struct tfe_http_session
|
||||
{
|
||||
int session_sequence;//?
|
||||
int major_version;//1:HTTP 1.x, 2:HTTP 2
|
||||
struct tfe_http_half* req;
|
||||
struct tfe_http_half* resp;//value is NULL before response received.
|
||||
void* proto_spec;
|
||||
};
|
||||
enum tfe_http_std_field
|
||||
{
|
||||
HTTP_UNKNOWN_FIELD=0,
|
||||
HTTP_MESSAGE_URL,
|
||||
HTTP_URI,
|
||||
HTTP_HOST,
|
||||
HTTP_REFERER,
|
||||
HTTP_USER_AGENT,
|
||||
HTTP_COOKIE,
|
||||
HTTP_PROXY_AUTHORIZATION,
|
||||
HTTP_AUTHORIZATION,
|
||||
HTTP_LOCATION,
|
||||
HTTP_SERVER,
|
||||
HTTP_ETAG ,
|
||||
HTTP_DATE ,
|
||||
HTTP_TRAILER ,
|
||||
HTTP_TRANSFER_ENCODING,
|
||||
HTTP_VIA,
|
||||
HTTP_PRAGMA,
|
||||
HTTP_CONNECTION,
|
||||
HTTP_CONT_ENCODING,
|
||||
HTTP_CONT_LANGUAGE,
|
||||
HTTP_CONT_LOCATION,
|
||||
HTTP_CONT_RANGE,
|
||||
HTTP_CONT_LENGTH,
|
||||
HTTP_CONT_TYPE,
|
||||
HTTP_CONT_DISPOSITION,
|
||||
HTTP_CHARSET,
|
||||
HTTP_EXPIRES,
|
||||
HTTP_X_FLASH_VERSION,
|
||||
HTTP_TRANSFER_LENGTH
|
||||
};
|
||||
struct http_field_name
|
||||
{
|
||||
enum tfe_http_std_field field_id;
|
||||
char* field_name; //Non-NULL when field_id isHTTP_UNKNOWN_FIELD.
|
||||
};
|
||||
|
||||
const char* tfe_http_field_read(const struct tfe_http_half* half, const struct http_field_name* name);
|
||||
int tfe_http_field_to_range(const char* field_value, long* range_from, long* range_to);
|
||||
int tfe_http_field_to_digit(const char* field_value, long* digit);
|
||||
|
||||
//******obsolete
|
||||
//long tfe_http_read_num_field(const struct tfe_http_half* half, const struct http_field_name* name);
|
||||
|
||||
//@Input param interator: NULL to get the first field.
|
||||
//@Output param name:
|
||||
//@return: field value.
|
||||
const char* tfe_http_field_iterate(const struct tfe_http_half* half, void* interator, struct http_field_name* name);
|
||||
|
||||
struct tfe_http_half* tfe_http_allow_write(const struct tfe_http_half* half);
|
||||
|
||||
//@param value: NULL for delete
|
||||
//@param name: Could be CHUNK/ENCODING
|
||||
int tfe_http_field_write(struct tfe_http_half* half, const struct http_field_name* name, const char* value);
|
||||
|
||||
//******obsolete
|
||||
|
||||
//int tfe_http_field_read_func(const struct tfe_http_half* half, const char* name, char* value, size_t size);
|
||||
|
||||
enum http_ev_bit_number
|
||||
{
|
||||
REQ_HDR_BITNUM=0,
|
||||
REQ_BODY_BEGIN_BITNUM,
|
||||
REQ_BODY_CONT_BITNUM,
|
||||
REQ_BODY_END_BITNUM,
|
||||
RESP_HDR_BITNUM,
|
||||
RESP_BODY_BEGIN_BITNUM,
|
||||
RESP_BODY_CONT_BITNUM,
|
||||
RESP_BODY_END_BITNUM
|
||||
};
|
||||
#define EV_HTTP_REQ_HDR ((uint64_t)1<<REQ_HDR_BITNUM)
|
||||
#define EV_HTTP_SESSION_BEGIN EV_HTTP_REQ_HDR
|
||||
#define EV_HTTP_REQ_BODY_BEGIN ((uint64_t)1<<REQ_HDR_BITNUM)
|
||||
#define EV_HTTP_REQ_BODY_CONT ((uint64_t)1<<REQ_BODY_CONT_BITNUM)
|
||||
#define EV_HTTP_REQ_BODY_END ((uint64_t)1<<REQ_BODY_END_BITNUM)
|
||||
#define EV_HTTP_BODY_FULL (EV_HTTP_REQ_BODY_BEGIN|EV_HTTP_REQ_BODY_CONT|EV_HTTP_REQ_BODY_END)
|
||||
#define EV_HTTP_REQ_END EV_HTTP_REQ_BODY_END
|
||||
#define EV_HTTP_RESP_HDR ((uint64_t)1<<RESP_HDR_BITNUM)
|
||||
#define EV_HTTP_RESP_BODY_BEGIN ((uint64_t)1<<RESP_BODY_BEGIN_BITNUM)
|
||||
#define EV_HTTP_RESP_BODY_CONT ((uint64_t)1<<RESP_BODY_CONT_BITNUM)
|
||||
#define EV_HTTP_RESP_BODY_END ((uint64_t)1<<RESP_BODY_END_BITNUM)
|
||||
#define EV_HTTP_RESP_BODY_FULL (EV_HTTP_RESP_BODY_BEGIN|EV_HTTP_RESP_BODY_CONT|EV_HTTP_RESP_BODY_END)
|
||||
#define EV_HTTP_RESP_END EV_HTTP_RESP_BODY_END
|
||||
#define EV_HTTP_SESSION_END EV_HTTP_RESP_END
|
||||
|
||||
enum tfe_bussiness_action
|
||||
{
|
||||
BIZ_ACTION_FORWARD,
|
||||
BIZ_ACTION_MODIFIED,
|
||||
BIZ_ACTION_ANSWER,
|
||||
BIZ_ACTION_DROP,
|
||||
BIZ_ACTION_PASSTHROUGH
|
||||
};
|
||||
|
||||
//@param event: bit AND of EV_HTTP_**
|
||||
//@param body_frag: NULL for no body data.
|
||||
typedef tfe_bussiness_action (*http_read_func)(const struct tfe_stream* stream, const struct tfe_http_session* session, uint64_t event,struct evbuffer *body_frag, void **pme);
|
||||
|
||||
struct tfe_http_half *tfe_http_request_create(int major_version,int method, const char* uri, const char* host);
|
||||
struct tfe_http_half *tfe_http_response_create(int major_version,int resp_code, struct evbuff* body);
|
||||
int tfe_http_append_body(const struct tfe_http_half* half, char* buff, size_t size);
|
||||
void tfe_http_half_free(struct tfe_http_half *half);
|
||||
//@param half: ownership has been transfered to session, do NOT free half after setting.
|
||||
void tfe_http_session_set_half(const struct tfe_http_session* session, struct tfe_http_half* half);
|
||||
|
||||
23
common/include/tfe_stat.h
Normal file
23
common/include/tfe_stat.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "tfe_types.h"
|
||||
enum TFE_STAT_FIELD
|
||||
{
|
||||
STREAM_NUM=0,
|
||||
STREAM_OPEN,
|
||||
STREAM_CLOSE,
|
||||
STREAM_ERROR,
|
||||
SSL_NUM,
|
||||
SSL_OPEN,
|
||||
SSL_CLOSE,
|
||||
SSL_ERROR,
|
||||
SNI_PEAK_FAIL,
|
||||
IN_BYTES,
|
||||
OUT_BYTES,
|
||||
TFE_STAT_MAX
|
||||
};
|
||||
struct tfe_stats
|
||||
{
|
||||
long long value[TFE_STAT_MAX];
|
||||
void * fs_handle;
|
||||
|
||||
int fs_ids[TFE_STAT_MAX];
|
||||
};
|
||||
107
common/include/tfe_stream.h
Normal file
107
common/include/tfe_stream.h
Normal file
@@ -0,0 +1,107 @@
|
||||
|
||||
#define TFE_STRING_MAX 2048
|
||||
#define TFE_SYMBOL_MAX 64
|
||||
enum tfe_session_proto
|
||||
{
|
||||
SESSION_PROTO_PLAIN=0,
|
||||
SESSION_PROTO_SSL,
|
||||
SESSION_PROTO_QUIC,
|
||||
SESSION_PROTO_SPDY
|
||||
};
|
||||
enum tfe_app_proto
|
||||
{
|
||||
APP_PROTO_HTTP1,
|
||||
APP_PROTO_HTTP2,
|
||||
APP_PROTO_WS, //websocket
|
||||
APP_PROTO_QUIC //QUIC is a protocol that cross session layer and application layer.
|
||||
};
|
||||
enum tfe_conn_dir
|
||||
{
|
||||
CONN_DIR_DOWNSTREAM=0, //From client to proxy, aka client-side.
|
||||
CONN_DIR_UPSTREAM //From proxy to server, aka server-side.
|
||||
};
|
||||
enum tfe_conn_status
|
||||
{
|
||||
CONN_STATUS_NONE,
|
||||
CONN_STATUS_ESTABLISHED,
|
||||
CONN_STATUS_CLOSED,
|
||||
};
|
||||
|
||||
/* single dst or src socket bufferevent descriptor */
|
||||
struct tfe_conn
|
||||
{
|
||||
struct layer_addr addr;
|
||||
enum tfe_conn_status status;
|
||||
struct bufferevent *bev;
|
||||
} ;
|
||||
|
||||
struct tfe_stream
|
||||
{
|
||||
|
||||
struct tfe_conn upstream;
|
||||
struct tfe_conn downstream;
|
||||
};
|
||||
|
||||
|
||||
enum tfe_stream_action
|
||||
{
|
||||
ACTION_FORWARD_DATA,
|
||||
ACTION_DEFER_DATA,
|
||||
ACTION_DROP_DATA
|
||||
};
|
||||
enum tfe_stream_action_opt
|
||||
{
|
||||
ACTION_OPT_FOWARD_BYTES, //value is size_t, default: forward entire data
|
||||
ACTION_OPT_DEFER_TIME_TV, //value is "struct timeval " which defines in <time.h>, default: time defer is not enabled
|
||||
ACTION_OPT_DEFER_BYTES, //value is size_t, default: defer entire data
|
||||
ACTION_OPT_DROP_BYTES //value is size_t, default: drop entire data
|
||||
};
|
||||
enum tfe_stream_close_reason
|
||||
{
|
||||
REASON_PASSIVE_CLOSED,
|
||||
REASON_ACTIVE_CLOSED,
|
||||
REASON_ERROR
|
||||
};
|
||||
int tfe_stream_action_set_opt(const struct tfe_stream* stream,enum tfe_stream_action_opt type, void* value, size_t size);
|
||||
/*
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
*/
|
||||
|
||||
int tfe_stream_write(const struct tfe_stream* stream, enum tfe_conn_dir dir, const unsigned char *data, size_t len);
|
||||
|
||||
struct tfe_stream_write_ctx{};
|
||||
//following tfe_stream_write_xx functions are NOT thread safe, MUST be called in the stream process thread.
|
||||
struct tfe_stream_write_ctx* tfe_stream_write_frag_start(const struct tfe_stream* stream, enum tfe_conn_dir dir);
|
||||
/*
|
||||
@return 0 if successful, or -1 if an error occurred
|
||||
*/
|
||||
int tfe_stream_write_frag(struct tfe_stream_write_ctx* w_ctx,const unsigned char *data, size_t size);
|
||||
void tfe_stream_write_frag_end(struct tfe_stream_write_ctx* w_ctx);
|
||||
//Return 1 for identify as its traffic;
|
||||
//Return 0 for unknown traffic;
|
||||
typedef tfe_stream_action stream_open_cb_t(const struct tfe_stream* stream, unsigned int thread_id, enum tfe_conn_dir dir, const unsigned char *data, size_t len, void **pme);
|
||||
typedef tfe_stream_action stream_data_cb_t(const struct tfe_stream* stream, unsigned int thread_id, enum tfe_conn_dir dir, const unsigned char *data, size_t len, void **pme);
|
||||
typedef void stream_close_cb_t(const struct tfe_stream* stream, unsigned int thread_id, enum tfe_stream_close_reason reason, void **pme);
|
||||
|
||||
void tfe_stream_detach(const struct tfe_stream* stream);
|
||||
int tfe_stream_preempt(const struct tfe_stream* stream);
|
||||
|
||||
int stream_shutdown(const struct tfe_stream* stream);//close both sides of the stream.
|
||||
int stream_shutdown_dir(const struct tfe_stream* stream, enum tfe_conn_dir dir);
|
||||
//typedef int proto_onwrite_cb_t(struct tfe_stream*, struct evbuffer *data, void **pme);
|
||||
|
||||
struct tfe_plugin
|
||||
{
|
||||
char symbol[TFE_SYMBOL_MAX];
|
||||
enum tfe_app_proto proto;
|
||||
stream_open_cb_t* on_open;
|
||||
stream_data_cb_t* on_data;
|
||||
stream_close_cb_t* on_close;
|
||||
// proto_onwrite_cb_t *onwrite;
|
||||
|
||||
};
|
||||
int tfe_io_write(struct pxy_conn_desc* dest,int dir,struct evbuffer *data);
|
||||
|
||||
int tfe_xxx_proto_init(struct tfe_plugin*m);
|
||||
|
||||
|
||||
143
common/include/tfe_types.h
Normal file
143
common/include/tfe_types.h
Normal file
@@ -0,0 +1,143 @@
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h> //defines struct in_addr
|
||||
|
||||
#define __TFE_STRING_MAX 2048
|
||||
#define MAX_FILENAME_SIZE 256
|
||||
|
||||
/* network-order */
|
||||
struct stream_tuple4_v4{
|
||||
struct in_addr saddr; /* network order */
|
||||
struct in_addr daddr; /* network order */
|
||||
in_port_t source; /* network order */
|
||||
in_port_t dest; /* network order */
|
||||
};
|
||||
|
||||
|
||||
#ifndef IPV6_ADDR_LEN
|
||||
#define IPV6_ADDR_LEN (sizeof(struct in6_addr))
|
||||
#endif
|
||||
|
||||
struct stream_tuple4_v6
|
||||
{
|
||||
struct in6_addr saddr;
|
||||
struct in6_addr daddr;
|
||||
in_port_t source; /* network order */
|
||||
in_port_t dest; /* network order */
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define GRE_TAG_LEN (4)
|
||||
struct layer_addr_gre
|
||||
{
|
||||
uint16_t gre_id;
|
||||
};
|
||||
|
||||
|
||||
#define VLAN_ID_MASK (0x0FFF)
|
||||
#define VLAN_TAG_LEN (4)
|
||||
struct layer_addr_vlan
|
||||
{
|
||||
uint16_t vlan_id; /* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_pppoe_session
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
unsigned int ver:4;
|
||||
unsigned int type:4;
|
||||
#endif
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
unsigned int type:4;
|
||||
unsigned int ver:4;
|
||||
#endif
|
||||
unsigned char code;
|
||||
unsigned short session_id;
|
||||
};
|
||||
|
||||
#ifndef MAC_ADDR_LEN
|
||||
#define MAC_ADDR_LEN (6)
|
||||
#endif
|
||||
|
||||
struct layer_addr_mac
|
||||
{
|
||||
uint8_t src_mac[MAC_ADDR_LEN]; /* network order */
|
||||
uint8_t dst_mac[MAC_ADDR_LEN]; /* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_ipv4
|
||||
{
|
||||
struct in_addr saddr; /* network order */
|
||||
struct in_addr daddr; /* network order */
|
||||
in_port_t source; /* network order */
|
||||
in_port_t dest; /* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_ipv6
|
||||
{
|
||||
struct in6_addr saddr; /* network order */
|
||||
struct in6_addr daddr; /* network order */
|
||||
in_port_t source;/* network order */
|
||||
in_port_t dest;/* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_tcp
|
||||
{
|
||||
in_port_t source; /* network order */
|
||||
in_port_t dest; /* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_udp
|
||||
{
|
||||
in_port_t source; /* network order */
|
||||
in_port_t dest; /* network order */
|
||||
};
|
||||
|
||||
struct layer_addr_l2tp
|
||||
{
|
||||
uint32_t tunnelid; /* network order */
|
||||
uint32_t sessionid; /* network order */
|
||||
};
|
||||
//地址类型定义
|
||||
enum addr_type_t{
|
||||
__ADDR_TYPE_INIT = 0,
|
||||
ADDR_TYPE_IPV4, /* 1, 基于IPv4地址的四元组信息 */
|
||||
ADDR_TYPE_IPV6, /* 2, 基于IPv6地址的四元组信息 */
|
||||
ADDR_TYPE_VLAN, /* 3 */
|
||||
ADDR_TYPE_MAC, /* 4 */
|
||||
ADDR_TYPE_ARP = 5, /* 5 */
|
||||
ADDR_TYPE_GRE, /* 6 */
|
||||
ADDR_TYPE_MPLS, /* 7 */
|
||||
ADDR_TYPE_PPPOE_SES, /* 8 */
|
||||
ADDR_TYPE_TCP, /* 9 */
|
||||
ADDR_TYPE_UDP = 10, /* 10 */
|
||||
ADDR_TYPE_L2TP, /* 11 */
|
||||
//ADDR_TYPE_STREAM_TUPLE4_V4, /* 12, 混合地址类型, 基于IPv4地址的四元组信息 */
|
||||
//ADDR_TYPE_STREAM_TUPLE4_V6, /* 13, 混合地址类型, 基于IPv6地址的四元组信息 */
|
||||
__ADDR_TYPE_IP_PAIR_V4, /* 14, 纯IPv4地址对 */
|
||||
__ADDR_TYPE_IP_PAIR_V6, /* 15, 纯IPv6地址对 */
|
||||
__ADDR_TYPE_MAX, /* 16 */
|
||||
};
|
||||
|
||||
struct layer_addr
|
||||
{
|
||||
enum addr_type_t addrtype; /* 地址类型, 详见 enum addr_type_t */
|
||||
/* 为了方便应用插件取地址, 此处使用联合体, 省去指针类型强制转换步骤 */
|
||||
union
|
||||
{
|
||||
struct stream_tuple4_v4 *tuple4_v4;
|
||||
struct stream_tuple4_v6 *tuple4_v6;
|
||||
struct layer_addr_ipv4 *ipv4;
|
||||
struct layer_addr_ipv6 *ipv6;
|
||||
struct layer_addr_vlan *vlan;
|
||||
struct layer_addr_mac *mac;
|
||||
struct layer_addr_gre *gre;
|
||||
struct layer_addr_tcp *tcp;
|
||||
struct layer_addr_udp *udp;
|
||||
struct layer_addr_pppoe_session *pppoe_ses;
|
||||
struct layer_addr_l2tp *l2tp;
|
||||
void *paddr;
|
||||
};
|
||||
uint8_t addrlen; /* 地址结构长度 */
|
||||
};
|
||||
|
||||
20
common/include/tfe_util.h
Normal file
20
common/include/tfe_util.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//#define ALLOC(t,n) (t *)calloc(sizeof(t),(n))
|
||||
|
||||
/* Allocates an array of objects using malloc() */
|
||||
#define ALLOC(type, number) \
|
||||
((type *)calloc(sizeof(type), number))
|
||||
|
||||
#define log_err_printf(fmt, args...) \
|
||||
fprintf(stderr, "file %s, line %d, " fmt, \
|
||||
__FILE__, __LINE__, ##args)
|
||||
#define log_dbg_printf(fmt, args...) \
|
||||
fprintf(stdout, "file %s, line %d, " fmt, \
|
||||
__FILE__, __LINE__, ##args)
|
||||
#define likely(expr) __builtin_expect((expr), 1)
|
||||
#define unlikely(expr) __builtin_expect((expr), 0)
|
||||
|
||||
int addr_sock2layer(struct sockaddr * sock_addr, int sockaddrlen, struct layer_addr* layer_addr);
|
||||
int addr_layer2sock(struct layer_addr* layer_addr,struct sockaddr * sock_addr);
|
||||
char* tfe_strdup(const char* s);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user