#pragma once #include #include #include #include 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 , 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); struct promise * tfe_stream_suspend(const struct tfe_stream * stream); void tfe_stream_resume(struct promisc * promisc); //close both sides of the stream. int stream_shutdown(const struct tfe_stream * stream); int stream_shutdown_dir(const struct tfe_stream * stream, enum tfe_conn_dir dir); 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; };