【修改】更新依赖的stellar头文件

This commit is contained in:
niubinghui
2024-10-14 19:04:00 +08:00
parent 024db869aa
commit 965403fbad
20 changed files with 917 additions and 245 deletions

View File

@@ -0,0 +1,31 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#define MESSAGE_MAGIC 0x12345678
#define MAX_APP_ID_NUM 8
#define APP_ID_MESSAGE_TOPIC "TOPIC_APP_ID"
enum APP_IDENTIFY_ORIGIN
{
ORIGIN_PROTO_IDENTIFY=0,
ORIGIN_APP_SKETCH_USER_DEFINE,
ORIGIN_PROTO_ENGINE,
ORIGIN_APP_SKETCH_BUILT_IN,
ORIGIN_PROTO_DECODED,
ORIGIN_EXCEED_PACKET_LIMIT,
ORIGIN_TUNNEL,
ORIGIN_MAX
};
struct app_id_message
{
int magic;
enum APP_IDENTIFY_ORIGIN origin;
uint32_t app_id_num;
int32_t app_id[MAX_APP_ID_NUM];
uint32_t surrogate_id[MAX_APP_ID_NUM];
uint32_t packet_sequence[MAX_APP_ID_NUM];
};

View File

@@ -0,0 +1,31 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
typedef void exdata_free(int idx, void *ex_ptr, void *arg);
struct exdata_schema;
struct exdata_schema *exdata_schema_new();
void exdata_schema_free(struct exdata_schema *schemas);
int exdata_schema_new_index(struct exdata_schema *schema, const char *name, exdata_free *free_func,void *free_arg);
int exdata_schema_get_idx_by_name(struct exdata_schema *schema, const char *name);
struct exdata_runtime;
struct exdata_runtime *exdata_runtime_new(struct exdata_schema *schemas);
void exdata_runtime_free(struct exdata_runtime *rt);
void exdata_runtime_reset(struct exdata_runtime *rt);//call free_func, and set ex_ptr to NULL
int exdata_set(struct exdata_runtime *rt, int idx, void *ex_ptr);
void *exdata_get(struct exdata_runtime *rt, int idx);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,113 @@
#pragma once
#include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define HTTP_TOPIC "HTTP_MESSAGE"
struct http_message;
enum http_message_type
{
HTTP_TRANSACTION_START,
HTTP_MESSAGE_REQ_LINE,
HTTP_MESSAGE_REQ_HEADER,
HTTP_MESSAGE_REQ_HEADER_END, // todo, delete END, push all fileds at once
HTTP_MESSAGE_REQ_BODY_START,
HTTP_MESSAGE_REQ_BODY,
HTTP_MESSAGE_REQ_BODY_END,
HTTP_MESSAGE_RES_LINE,
HTTP_MESSAGE_RES_HEADER,
HTTP_MESSAGE_RES_HEADER_END, // todo, delete END, push all fileds at once
HTTP_MESSAGE_RES_BODY_START,
HTTP_MESSAGE_RES_BODY,
HTTP_MESSAGE_RES_BODY_END,
HTTP_TRANSACTION_END,
HTTP_MESSAGE_MAX
};
struct http_header_field
{
char *name;
size_t name_len;
char *value;
size_t value_len;
};
struct http_request_line
{
char *method;
size_t method_len;
char *uri;
size_t uri_len;
char *version;
size_t version_len;
int major_version;
int minor_version;
};
struct http_response_line
{
char *version;
size_t version_len;
char *status;
size_t status_len;
int major_version;
int minor_version;
int status_code;
};
enum http_message_type http_message_get_type(const struct http_message *msg);
void http_message_get0_request_line(const struct http_message *msg, struct http_request_line *line);
void http_message_get0_response_line(const struct http_message *msg, struct http_response_line *line);
/*
* Pay attention: key->iov_base is case-insensitive.
*/
void http_message_get0_header(const struct http_message *msg, const char *name, size_t name_len, struct http_header_field *field_result);
/**
* @brief loop reading all headers.
*
* @retval succeed( >= 0) failed(-1)
*/
int http_message_get0_next_header(const struct http_message *msg, struct http_header_field *header);
/**
* @retval succeed( >= 0) failed(-1)
*/
int http_message_reset_header_iter(struct http_message *msg); // to do , obsoleted
void http_message_get0_uncompressed_body(const struct http_message *msg, const char **body, size_t *body_len);
/**
* @brief If the body hasn't been compressed, same as http_message_get0_uncompressed_body().
*
*/
void http_message_get0_decompressed_body(const struct http_message *msg, const char **body, size_t *body_len);
void http_message_get0_raw_url(const struct http_message *msg, const char **url, size_t *url_len);
/*
return value:
0: failed
>1: success, length of decoded_url_buffer, not C string( no EOF with '\0' )
*/
size_t http_url_decode(const char *raw_url, size_t raw_url_len, char *decoded_url_buffer, size_t decoded_url_buffer_len);
/**
* @retval succeed( >= 0) failed(-1)
*/
int http_message_get_transaction_seq(const struct http_message *msg);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,60 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
enum log_level
{
LOG_TRACE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
};
#define STELLAR_LOG_TRACE(logger, module, format, ...) \
if (log_check_level((logger), LOG_TRACE)) \
{ \
log_print((logger), LOG_TRACE, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_DEBUG(logger, module, format, ...) \
if (log_check_level((logger), LOG_DEBUG)) \
{ \
log_print((logger), LOG_DEBUG, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_INFO(logger, module, format, ...) \
if (log_check_level((logger), LOG_INFO)) \
{ \
log_print((logger), LOG_INFO, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_WARN(logger, module, format, ...) \
if (log_check_level((logger), LOG_WARN)) \
{ \
log_print((logger), LOG_WARN, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_ERROR(logger, module, format, ...) \
if (log_check_level((logger), LOG_ERROR)) \
{ \
log_print((logger), LOG_ERROR, (module), (format), ##__VA_ARGS__); \
}
#define STELLAR_LOG_FATAL(logger, module, format, ...) \
if (log_check_level((logger), LOG_FATAL)) \
{ \
log_print((logger), LOG_FATAL, (module), (format), ##__VA_ARGS__); \
}
struct logger;
int log_check_level(struct logger *logger, enum log_level level);
void log_print(struct logger *logger, enum log_level level, const char *module, const char *fmt, ...);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,49 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/mq.h"
#include "stellar/log.h"
#define PACKET_MANAGER_MODULE_NAME "packet_manager_module"
#define SESSION_MANAGER_MODULE_NAME "session_manager_module"
struct stellar_module;
struct stellar_module *stellar_module_new(const char *name, void *ctx);
void stellar_module_free(struct stellar_module *mod);
void * stellar_module_get_ctx(struct stellar_module *mod);
void stellar_module_set_ctx(struct stellar_module *mod, void *ctx);
const char *stellar_module_get_name(struct stellar_module* mod);
void stellar_module_set_name(struct stellar_module* mod, const char *name);
struct stellar_module_manager;
typedef struct stellar_module *module_on_init_func(struct stellar_module_manager *mod_mgr);
typedef void module_on_exit_func(struct stellar_module_manager *mod_mgr, struct stellar_module *mod);
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema, struct logger *logger);
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr);
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr, int thread_id, struct mq_runtime *mq_rt);
// return -1 on error
int stellar_module_manager_get_thread_id(struct stellar_module_manager* mod_mgr);
struct mq_runtime *stellar_module_manager_get_mq_runtime(struct stellar_module_manager *mod_mgr);
struct stellar_module *stellar_module_manager_get_module(struct stellar_module_manager *mod_mgr, const char *module_name);
int stellar_module_manager_get_max_thread_num(struct stellar_module_manager* mod_mgr);
const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr);
struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_manager *mod_mgr);
struct logger *stellar_module_manager_get_logger(struct stellar_module_manager *mod_mgr);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,54 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
struct mq_schema;
struct mq_schema *mq_schema_new();
void mq_schema_free(struct mq_schema *s);
typedef void mq_msg_free_cb_func(void *msg, void *msg_free_arg);
typedef void on_msg_cb_func(int topic_id, void *msg, void *on_msg_arg);
typedef void on_msg_dispatch_cb_func(int topic_id,
void *msg,
on_msg_cb_func* on_msg_cb,
void *on_msg_cb_arg,
void *dispatch_arg);
//return topic_id
int mq_schema_create_topic(struct mq_schema *s,
const char *topic_name,
on_msg_dispatch_cb_func *on_dispatch_cb,
void *on_dispatch_arg,
mq_msg_free_cb_func *msg_free_cb,
void *msg_free_arg);
int mq_schema_get_topic_id(struct mq_schema *s, const char *topic_name);
int mq_schema_update_topic(struct mq_schema *s,
int topic_id,
on_msg_dispatch_cb_func *on_dispatch_cb,
void *on_dispatch_arg,
mq_msg_free_cb_func *msg_free_cb,
void *msg_free_arg);
int mq_schema_destroy_topic(struct mq_schema *s, int topic_id);
//return 0 if success, otherwise return -1.
int mq_schema_subscribe(struct mq_schema *s, int topic_id, on_msg_cb_func *on_msg_cb, void * on_msg_cb_arg);
struct mq_runtime;
struct mq_runtime *mq_runtime_new(struct mq_schema *s);
void mq_runtime_free(struct mq_runtime *s);
// return 0 if success, otherwise return -1
int mq_runtime_publish_message(struct mq_runtime *rt, int topic_id, void *msg);// append message to pending queue
void mq_runtime_dispatch(struct mq_runtime *rt);// dispatch all message in pending queue, dispatched message will be append to dlq
void mq_runtime_clean(struct mq_runtime *rt); // free all message in dlq and pending queue, during this period, publish will be disabled
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,193 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <netinet/ip_icmp.h>
#include <linux/if_ether.h>
#include <linux/mpls.h>
struct packet;
/******************************************************************************
* layer
******************************************************************************/
enum layer_proto
{
LAYER_PROTO_NONE = 0,
// L2 -- data link layer
LAYER_PROTO_ETHER = 1,
LAYER_PROTO_PWETH = 2,
LAYER_PROTO_PPP = 3,
LAYER_PROTO_L2TP = 4,
// L2 -- tunnel
LAYER_PROTO_VLAN = 21,
LAYER_PROTO_PPPOE = 22,
LAYER_PROTO_MPLS = 23,
// L3 -- network layer
LAYER_PROTO_IPV4 = 31,
LAYER_PROTO_IPV6 = 32,
LAYER_PROTO_IPAH = 33,
// L3 -- tunnel
LAYER_PROTO_GRE = 41,
// L4 -- transport layer
LAYER_PROTO_UDP = 51,
LAYER_PROTO_TCP = 52,
LAYER_PROTO_ICMP = 53,
LAYER_PROTO_ICMP6 = 54,
// L4 -- tunnel
LAYER_PROTO_VXLAN = 61,
LAYER_PROTO_GTP_U = 62,
LAYER_PROTO_GTP_C = 63,
};
struct layer
{
enum layer_proto proto;
uint16_t hdr_len;
union
{
// all hdr ptr refer to raw packet, read-only
const struct ethhdr *eth;
const struct ip *ip4;
const struct ip6_hdr *ip6;
const struct tcphdr *tcp;
const struct udphdr *udp;
const struct icmphdr *icmp4;
const struct icmp6_hdr *icmp6;
const struct mpls_label *mpls;
const char *raw; // e.g. pppoe, l2tp, gre, gtp, etc.
} hdr;
};
int packet_get_layer_count(const struct packet *pkt);
const struct layer *packet_get_layer_by_idx(const struct packet *pkt, int idx);
// // example: foreach layer in packet (inorder)
// int count = packet_get_layer_count(pkt);
// for (int i = 0; i < count; i++)
// {
// const struct layer *layer = packet_get_layer_by_idx(pkt, i);
// // do something with layer
// }
//
//
// // example: foreach layer in packet (reverse)
// int count = packet_get_layer_count(pkt);
// for (int i = count - 1; i >= 0; i--)
// {
// const struct layer *layer = packet_get_layer_by_idx(pkt, i);
// // do something with layer
// }
/******************************************************************************
* tunnel
******************************************************************************/
enum tunnel_type
{
TUNNEL_IPV4 = 1, // contain layers: IPv4, (next inner layer must be IPv4 / IPv6)
TUNNEL_IPV6 = 2, // contain layers: IPv6, (next inner layer must be IPv4 / IPv6)
TUNNEL_GRE = 3, // contain layers: IPv4 + GRE
// contain layers: IPv6 + GRE
TUNNEL_GTP = 4, // contain layers: IPv4 + UDP + GTP
// contain layers: IPv6 + UDP + GTP
TUNNEL_VXLAN = 5, // contain layers: IPv4 + UDP + VXLAN
// contain layers: IPv6 + UDP + VXLAN
TUNNEL_L2TP = 6, // contain layers: IPv4 + UDP + L2TP
// contain layers: IPv6 + UDP + L2TP
TUNNEL_TEREDO = 7, // contain layers: IPv4 + UDP, (next inner layer must be IPv6)
};
#define MAX_LAYERS_PER_TUNNEL 3
struct tunnel
{
enum tunnel_type type;
int layer_count;
const struct layer *layers[MAX_LAYERS_PER_TUNNEL];
};
int packet_get_tunnel_count(const struct packet *pkt);
// return 0: success 
// return -1: failed
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out);
/******************************************************************************
* build
******************************************************************************/
/*
* tcp_seq: the sequence number of the new TCP packet (in host byte order)
* tcp_ack: the acknowledgment number of the new TCP packet (in host byte order)
* tcp_options_len: the length of the options (must be a multiple of 4)
*/
struct packet *packet_build_tcp(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags,
const char *tcp_options, uint16_t tcp_options_len,
const char *tcp_payload, uint16_t tcp_payload_len);
struct packet *packet_build_udp(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
struct packet *packet_build_l3(const struct packet *origin_pkt, uint8_t ip_proto, const char *l3_payload, uint16_t l3_payload_len);
/******************************************************************************
* utils
******************************************************************************/
#define MAX_SIDS 8
struct sids
{
uint16_t sid[MAX_SIDS];
int used;
};
void packet_prepend_sids(struct packet *pkt, const struct sids *sids);
enum packet_direction
{
PACKET_DIRECTION_OUTGOING = 0, // Internal -> External: 0
PACKET_DIRECTION_INCOMING = 1, // External -> Internal: 1
};
enum packet_direction packet_get_direction(const struct packet *pkt);
enum packet_action
{
PACKET_ACTION_FORWARD = 0,
PACKET_ACTION_DROP = 1,
};
void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt);
void packet_set_timeval(struct packet *pkt, const struct timeval *tv);
const struct timeval *packet_get_timeval(const struct packet *pkt);
const char *packet_get_raw_data(const struct packet *pkt);
uint16_t packet_get_raw_len(const struct packet *pkt);
const char *packet_get_payload(const struct packet *pkt);
uint16_t packet_get_payload_len(const struct packet *pkt);
void packet_set_exdata(struct packet *pkt, int idx, void *ex_ptr);
void *packet_get_exdata(struct packet *pkt, int idx);
#ifdef __cplusplus
}
#endif

View File

@@ -1,8 +0,0 @@
#pragma once
#include "stellar.h"
typedef void packet_exdata_free(struct packet *pkt, int idx, void *ex_ptr, void *arg);
int stellar_packet_exdata_new_index(struct stellar *st, const char *name, packet_exdata_free *free_func,void *arg);
int packet_exdata_set(struct packet *pkt, int idx, void *ex_ptr);
void *packet_exdata_get(struct packet *pkt, int idx);

View File

@@ -0,0 +1,37 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/exdata.h"
#include "stellar/packet.h"
enum packet_stage
{
PACKET_STAGE_PREROUTING,
PACKET_STAGE_INPUT,
PACKET_STAGE_FORWARD,
PACKET_STAGE_OUTPUT,
PACKET_STAGE_POSTROUTING,
PACKET_STAGE_MAX,
};
struct packet_manager;
int packet_manager_new_packet_exdata_index(struct packet_manager *pkt_mgr, const char *name, exdata_free *func, void *arg);
typedef void on_packet_stage_callback(enum packet_stage stage, struct packet *pkt, void *args);
int packet_manager_subscribe(struct packet_manager *pkt_mgr, enum packet_stage stage, on_packet_stage_callback *cb, void *args);
// if two modules claim the same packet at the same stage, the second 'claim' fails.
// return 0 on success
// return -1 on failure
typedef void on_packet_claimed_callback(struct packet *pkt, void *args);
int packet_manager_claim_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, on_packet_claimed_callback cb, void *args);
void packet_manager_schedule_packet(struct packet_manager *pkt_mgr, uint16_t thread_id, struct packet *pkt, enum packet_stage stage);
#ifdef __cplusplus
}
#endif

View File

@@ -1,22 +0,0 @@
#pragma once
#include "stellar.h"
//session mq
typedef void packet_msg_free_cb_func(struct packet *pkt, void *msg, void *msg_free_arg);
typedef void on_packet_msg_cb_func(struct packet *pkt, int topic_id, const void *msg, void *plugin_env);
//return topic_id
int stellar_packet_mq_create_topic(struct stellar *st, const char *topic_name, packet_msg_free_cb_func *msg_free_cb, void *msg_free_arg);
int stellar_packet_mq_get_topic_id(struct stellar *st, const char *topic_name);
int stellar_packet_mq_update_topic(struct stellar *st, int topic_id, packet_msg_free_cb_func *msg_free_cb, void *msg_free_arg);
int stellar_packet_mq_destroy_topic(struct stellar *st, int topic_id);
//return 0 if success, otherwise return -1.
int stellar_packet_mq_subscribe(struct stellar *st, int topic_id, on_packet_msg_cb_func *plugin_on_msg_cb, int plugin_id); //packet plugin only
int packet_mq_publish_message(struct packet *pkt, int topic_id, void *msg);

View File

@@ -0,0 +1,21 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/module_manager.h"
struct stellar_polling_manager;
struct stellar_polling_manager *stellar_module_get_polling_manager(struct stellar_module_manager *mod_mgr);
typedef void module_on_polling_func(struct stellar_polling_manager* mod_mgr, void *polling_arg);
//return 0 if success, otherwise return -1.
int stellar_polling_subscribe(struct stellar_polling_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg);
void stellar_polling_active(struct stellar_polling_manager *mod_mgr);
#ifdef __cplusplus
}
#endif

View File

@@ -1,134 +1,151 @@
#pragma once
#include "stellar.h"
#include <stdint.h>
#include <stddef.h>
enum session_type
#ifdef __cplusplus
extern "C"
{
SESSION_TYPE_TCP,
SESSION_TYPE_UDP,
__SESSION_TYPE_MAX,
};
#endif
#include "stellar/packet.h"
enum session_state
{
SESSION_STATE_INVALID = 0,
SESSION_STATE_OPENING = 1 ,
SESSION_STATE_ACTIVE = 2,
SESSION_STATE_CLOSING = 3,
SESSION_STATE_CONTROL = 6,
__SESSION_STATE_MAX,
SESSION_STATE_INIT = 0,
SESSION_STATE_OPENING = 1,
SESSION_STATE_ACTIVE = 2,
SESSION_STATE_CLOSING = 3,
SESSION_STATE_DISCARD = 4,
SESSION_STATE_CLOSED = 5,
MAX_STATE = 6,
};
enum session_type session_get_type(struct session *sess);
enum session_type
{
SESSION_TYPE_TCP = 0x1,
SESSION_TYPE_UDP = 0x2,
};
enum session_direction
{
SESSION_DIRECTION_INBOUND = 0,
SESSION_DIRECTION_OUTBOUND = 1,
};
enum flow_type
{
FLOW_TYPE_NONE = -1,
FLOW_TYPE_C2S = 0,
FLOW_TYPE_S2C = 1,
MAX_FLOW_TYPE = 2,
};
enum closing_reason
{
CLOSING_BY_TIMEOUT = 0x1,
CLOSING_BY_LRU_EVICTED = 0x2,
CLOSING_BY_PORT_REUSE_EVICTED = 0x3,
CLOSING_BY_CLIENT_FIN = 0x4,
CLOSING_BY_CLIENT_RST = 0x5,
CLOSING_BY_SERVER_FIN = 0x6,
CLOSING_BY_SERVER_RST = 0x7,
};
enum session_stat
{
// raw packet
STAT_RAW_PACKETS_RECEIVED,
STAT_RAW_BYTES_RECEIVED,
STAT_RAW_PACKETS_TRANSMITTED,
STAT_RAW_BYTES_TRANSMITTED,
STAT_RAW_PACKETS_DROPPED,
STAT_RAW_BYTES_DROPPED,
// duplicate packets
STAT_DUPLICATE_PACKETS_BYPASS,
STAT_DUPLICATE_BYTES_BYPASS,
// injected packet
STAT_INJECTED_PACKETS_FAILED,
STAT_INJECTED_PACKETS_SUCCESS,
STAT_INJECTED_BYTES_SUCCESS,
// control packet
STAT_CONTROL_PACKETS_RECEIVED, // TODO
STAT_CONTROL_BYTES_RECEIVED, // TODO
STAT_CONTROL_PACKETS_TRANSMITTED,
STAT_CONTROL_BYTES_TRANSMITTED,
STAT_CONTROL_PACKETS_DROPPED,
STAT_CONTROL_BYTES_DROPPED,
// TCP segment
STAT_TCP_SEGMENTS_RECEIVED,
STAT_TCP_PAYLOADS_RECEIVED,
STAT_TCP_SEGMENTS_EXPIRED,
STAT_TCP_PAYLOADS_EXPIRED,
STAT_TCP_SEGMENTS_RETRANSMIT,
STAT_TCP_PAYLOADS_RETRANSMIT,
STAT_TCP_SEGMENTS_OVERLAP,
STAT_TCP_PAYLOADS_OVERLAP,
STAT_TCP_SEGMENTS_NOSPACE,
STAT_TCP_PAYLOADS_NOSPACE,
STAT_TCP_SEGMENTS_INORDER,
STAT_TCP_PAYLOADS_INORDER,
STAT_TCP_SEGMENTS_REORDERED,
STAT_TCP_PAYLOADS_REORDERED,
STAT_TCP_SEGMENTS_BUFFERED,
STAT_TCP_PAYLOADS_BUFFERED,
STAT_TCP_SEGMENTS_RELEASED,
STAT_TCP_PAYLOADS_RELEASED,
MAX_STAT,
};
// realtime in milliseconds
enum session_timestamp
{
SESSION_TIMESTAMP_START,
SESSION_TIMESTAMP_LAST,
MAX_TIMESTAMP,
};
struct session;
#define SESSION_SEEN_C2S_FLOW (1 << 0)
#define SESSION_SEEN_S2C_FLOW (1 << 1)
int session_is_symmetric(struct session *sess, unsigned char *flag);
int session_is_symmetric(const struct session *sess, unsigned char *flag);
long long session_get_client_isn(struct session *sess);
long long session_get_server_isn(struct session *sess);
int session_has_duplicate_traffic(const struct session *sess);
enum session_type session_get_type(const struct session *sess);
#define SESSION_IS_TUNNLE_NON (0) /* default is 0, not tunnel; */
#define SESSION_IS_TUNNLE_6OVER4 (1 << 0)
#define SESSION_IS_TUNNLE_4OVER6 (1 << 1)
#define SESSION_IS_TUNNLE_GRE (1 << 2)
#define SESSION_IS_TUNNLE_IP_IN_IP (1 << 3)
#define SESSION_IS_TUNNLE_PPTP (1 << 4)
#define SESSION_IS_TUNNLE_L2TP (1 << 5)
#define SESSION_IS_TUNNLE_TEREDO (1 << 6)
#define SESSION_IS_TUNNLE_GTP (1 << 7)
#define SESSION_IS_TUNNLE_SOCKS (1 << 8)
#define SESSION_IS_TUNNLE_HTTP_PROXY (1 << 9)
enum session_state session_get_current_state(const struct session *sess);
const struct packet *session_get0_current_packet(const struct session *sess);
int session_is_outmost(struct session *sess, uint64_t *flag);
int session_is_innermost(struct session *sess, uint64_t *flag);
enum closing_reason session_get_closing_reason(const struct session *sess);
enum session_direction session_get_direction(const struct session *sess);
enum flow_type session_get_flow_type(const struct session *sess);
const struct packet *session_get_first_packet(const struct session *sess, enum flow_type type);
uint64_t session_get_id(const struct session *sess);
uint64_t session_get_timestamp(const struct session *sess, enum session_timestamp type);
uint64_t session_get_stat(const struct session *sess, enum flow_type type, enum session_stat stat);
#define SESSION_DIRECTION_IN 0
#define SESSION_DIRECTION_OUT 1
int session_get_direction(struct session *sess);
const char *session_get0_readable_addr(const struct session *sess);
enum session_addr_type
{
SESSION_ADDR_TYPE_IPV4_TCP,
SESSION_ADDR_TYPE_IPV4_UDP,
SESSION_ADDR_TYPE_IPV6_TCP,
SESSION_ADDR_TYPE_IPV6_UDP,
SESSION_ADDR_TYPE_UNKNOWN,
__SESSION_ADDR_TYPE_MAX,
};
struct session_addr_ipv4{
uint32_t saddr; /* network order */
uint32_t daddr; /* network order */
uint16_t sport; /* network order */
uint16_t dport; /* network order */
};
void session_set_discard(struct session *sess);
#include <netinet/in.h>
#ifndef IPV6_ADDR_LEN
#define IPV6_ADDR_LEN (sizeof(struct in6_addr))
void session_set_exdata(struct session *sess, int idx, void *ex_ptr);
void *session_get_exdata(const struct session *sess, int idx);
#ifdef __cplusplus
}
#endif
struct session_addr_ipv6
{
uint8_t saddr[IPV6_ADDR_LEN] ;
uint8_t daddr[IPV6_ADDR_LEN] ;
uint16_t sport; /* network order */
uint16_t dport; /* network order */
};
struct session_addr
{
union
{
struct session_addr_ipv4 ipv4;
struct session_addr_ipv6 ipv6;
};
};
struct session_addr *session_get0_addr(struct session *sess, enum session_addr_type *addr_type);
const char *session_get0_readable_addr(struct session *sess);
const char *session_get0_current_payload(struct session *sess, size_t *payload_len);
enum session_state session_get_current_state(struct session *sess);
int session_get_current_thread_id(struct session *sess);
int session_get_current_plugin_id(struct session *sess);
/* ------------session------------------*/
/* |l2|l3|l4|session payload| */
const char *session_get0_current_l3_header(struct session *sess);
const char *session_get0_current_l4_header(struct session *sess);
const char *session_get0_l2_l3_hdr(struct session *sess, int session_direction, size_t *l2_l3_hdr_len);
uint16_t *session_get0_segment_id_list(struct session *sess, int session_direction, size_t *sid_num);
const char *session_get0_route_ctx(struct session *sess, int session_direction, size_t *route_ctx_len);
int session_set_session_id(struct session *sess, uint64_t session_id);
int session_set_preappend_segment_id_list(struct session *sess, uint16_t *sid, size_t sid_num);
const struct packet *session_get0_current_packet(struct session *sess);
//flow direction
#define PACKET_DIRECTION_C2S 0
#define PACKET_DIRECTION_S2C 1
#define PACKET_DIRECTION_UNKNOWN 2
int packet_get_direction(const struct packet *pkt);
//route direction
#define PACKET_DIRECTION_INCOMING 0
#define PACKET_DIRECTION_OUTGOING 1
int packet_get_route_direction(const struct packet *pkt);
const char *packet_get0_data(const struct packet *pkt, size_t *data_len);
int packet_arrive_time(const struct packet *pkt, struct timeval *ts);
unsigned char packet_get_ip_protocol(struct packet *pkt);
void packet_drop(const struct packet *pkt);
const char *packet_get0_readable_addr(struct packet *pkt);
int packet_get_current_thread_id(struct packet *pkt);
struct session *packet_get_session(const struct packet *pkt);

View File

@@ -1,8 +0,0 @@
#pragma once
#include "stellar.h"
typedef void session_exdata_free(struct session *sess, int idx, void *ex_ptr, void *arg);
int stellar_session_exdata_new_index(struct stellar *st, const char *name, session_exdata_free *free_func,void *arg);
int session_exdata_set(struct session *sess, int idx, void *ex_ptr);
void *session_exdata_get(struct session *sess, int idx);

View File

@@ -0,0 +1,55 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#define MESSAGE_MAGIC 0x12345678
#define SESSION_FLAGS_MESSAGE_TOPIC "TOPIC_SESSION_FLAGS"
enum
{
session_flags_bulky_mask = 1,
session_flags_cbr_mask,
session_flags_local_client_mask,
session_flags_local_server_mask,
session_flags_download_mask,
session_flags_interactive_mask,
session_flags_inbound_mask,
session_flags_outbound_mask,
session_flags_pseudo_unidirectional_mask,
session_flags_streaming_mask,
session_flags_unidirectional_mask,
session_flags_random_looking_mask,
session_flags_c2s_mask,
session_flags_s2c_mask,
session_flags_bidirectional_mask,
session_flags_tunneling_mask,
session_flags_all_mask
};
#define SESSION_FLAGS_START (0x0000000000000001)
#define SESSION_FLAGS_BULKY (SESSION_FLAGS_START << session_flags_bulky_mask)
#define SESSION_FLAGS_CBR (SESSION_FLAGS_START << session_flags_cbr_mask)
#define SESSION_FLAGS_LOCAL_CLIENT (SESSION_FLAGS_START << session_flags_local_client_mask)
#define SESSION_FLAGS_LOCAL_SERVER (SESSION_FLAGS_START << session_flags_local_server_mask)
#define SESSION_FLAGS_DOWNLOAD (SESSION_FLAGS_START << session_flags_download_mask)
#define SESSION_FLAGS_INTERACTIVE (SESSION_FLAGS_START << session_flags_interactive_mask)
#define SESSION_FLAGS_INBOUND (SESSION_FLAGS_START << session_flags_inbound_mask)
#define SESSION_FLAGS_OUTBOUND (SESSION_FLAGS_START << session_flags_outbound_mask)
#define SESSION_FLAGS_PSEUDO_UNIDIRECTIONAL (SESSION_FLAGS_START << session_flags_pseudo_unidirectional_mask)
#define SESSION_FLAGS_STREAMING (SESSION_FLAGS_START << session_flags_streaming_mask)
#define SESSION_FLAGS_UNIDIRECTIONAL (SESSION_FLAGS_START << session_flags_unidirectional_mask)
#define SESSION_FLAGS_RANDOM_LOOKING (SESSION_FLAGS_START << session_flags_random_looking_mask)
#define SESSION_FLAGS_C2S (SESSION_FLAGS_START << session_flags_c2s_mask)
#define SESSION_FLAGS_S2C (SESSION_FLAGS_START << session_flags_s2c_mask)
#define SESSION_FLAGS_BIDIRECTIONAL (SESSION_FLAGS_START << session_flags_bidirectional_mask)
#define SESSION_FLAGS_TUNNELING (SESSION_FLAGS_START << session_flags_tunneling_mask)
struct session_flags_message
{
int magic;
uint64_t flags;
uint32_t array_num;
uint32_t *packet_sequence_array;
};

View File

@@ -0,0 +1,27 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/exdata.h"
#include "stellar/session.h"
struct session_manager;
int session_manager_new_session_exdata_index(struct session_manager *sess_mgr, const char *name, exdata_free *func, void *arg);
typedef void on_session_callback(struct session *sess, struct packet *pkt, void *args);
typedef void on_tcp_stream_callback(struct session *sess, const char *tcp_payload, uint32_t tcp_payload_len, void *args);
int session_manager_subscribe_tcp(struct session_manager *sess_mgr, on_session_callback *cb, void *args);
int session_manager_subscribe_udp(struct session_manager *sess_mgr, on_session_callback *cb, void *args);
int session_manager_subscribe_control_packet(struct session_manager *sess_mgr, on_session_callback *cb, void *args);
int session_manager_subscribe_tcp_stream(struct session_manager *sess_mgr, on_tcp_stream_callback *cb, void *args);
#ifdef __cplusplus
}
#endif

View File

@@ -1,36 +0,0 @@
#pragma once
#include "stellar.h"
//session mq
typedef void session_msg_free_cb_func(struct session *sess, void *msg, void *msg_free_arg);
typedef void on_session_msg_cb_func(struct session *sess, int topic_id, const void *msg, void *per_session_ctx, void *plugin_env);
//return topic_id
int stellar_session_mq_create_topic(struct stellar *st, const char *topic_name, session_msg_free_cb_func *msg_free_cb, void *msg_free_arg);
int stellar_session_mq_get_topic_id(struct stellar *st, const char *topic_name);
int stellar_session_mq_update_topic(struct stellar *st, int topic_id, session_msg_free_cb_func *msg_free_cb, void *msg_free_arg);
int stellar_session_mq_destroy_topic(struct stellar *st, int topic_id);
//return 0 if success, otherwise return -1.
int stellar_session_mq_subscribe(struct stellar *st, int topic_id, on_session_msg_cb_func *plugin_on_msg_cb, int plugin_id);
int session_mq_publish_message(struct session *sess, int topic_id, void *msg);
int session_mq_ignore_message(struct session *sess, int topic_id, int plugin_id);
int session_mq_unignore_message(struct session *sess, int topic_id, int plugin_id);
int session_mq_topic_is_active(struct session *sess, int topic_id);
enum session_mq_priority
{
SESSION_MQ_PRIORITY_LOW,
SESSION_MQ_PRIORITY_NORMAL,
SESSION_MQ_PRIORITY_HIGH,
SESSION_MQ_PRIORITY_MAX,
};
int session_mq_publish_message_with_priority(struct session *sess, int topic_id, void *msg, enum session_mq_priority priority);

View File

@@ -0,0 +1,44 @@
#include <sys/uio.h>
#include <netinet/in.h>
#ifndef IPV6_ADDR_LEN
#define IPV6_ADDR_LEN (sizeof(struct in6_addr))
#endif
#define SOCKS_MESSAGE_TOPIC "TOPIC_SOCKS"
enum socks_addr_type
{
SOCKS_ADDR_IPV4,
SOCKS_ADDR_IPV6,
SOCKS_ADDR_FQDN
};
struct socks_addr
{
enum socks_addr_type type;
union
{
uint32_t ipv4; /* network order */
uint8_t ipv6[IPV6_ADDR_LEN] ;
struct iovec fqdn;
};
uint16_t port; /* network order */
};
enum socks_version
{
SOCKS_VERSION_4,
SOCKS_VERSION_5
};
struct socks_info
{
enum socks_version version;
struct socks_addr dst_addr;
struct iovec user_name;
struct iovec password;
};//message data

View File

@@ -1,48 +1,35 @@
#pragma once
struct session;
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include "stellar/log.h"
#include "stellar/packet.h"
struct stellar;
int stellar_get_worker_thread_num(struct stellar *st);
int stellar_get_current_thread_id(struct stellar *st);
//return plugin_env
typedef void *plugin_on_load_func(struct stellar *st);
typedef void plugin_on_unload_func(void *plugin_env);
//return per_session_ctx
typedef void *session_ctx_new_func(struct session *sess, void *plugin_env);
typedef void session_ctx_free_func(struct session *sess, void *session_ctx, void *plugin_env);
//intrinsic topic, publish packet as message
#define TOPIC_TCP "TCP"
#define TOPIC_TCP_STREAM "TCP_STREAM"
#define TOPIC_UDP "UDP"
#define TOPIC_EGRESS "EGRESS"
#define TOPIC_CONTROL_PACKET "CONTROL_PACKET"
//return session plugin_id
int stellar_session_plugin_register(struct stellar *st,
session_ctx_new_func session_ctx_new,
session_ctx_free_func session_ctx_free,
void *plugin_env);
void stellar_session_plugin_dettach_current_session(struct session *sess);
typedef void plugin_on_packet_func(struct packet *pkt, void *on_packet_cb_arg);
//return 0 if success, otherwise return -1.
int stellar_raw_packet_subscribe(struct stellar *st, plugin_on_packet_func *on_packet_cb, void *on_packet_cb_arg);
struct packet;
typedef void plugin_on_packet_func(struct packet *pkt, unsigned char ip_protocol, void *plugin_env);
void stellar_emit_datapath_telemetry(struct packet *pkt, const char * module, const char *str);
//return packet plugin_id
int stellar_packet_plugin_register(struct stellar *st, unsigned char ip_protocol, plugin_on_packet_func on_packet, void *plugin_env);
// only send user build packet, can't send packet which come from network
// void stellar_send_build_packet(struct stellar *st, struct packet *pkt);
struct stellar *stellar_new(const char *toml_file);
void stellar_run(struct stellar *st);
void stellar_free(struct stellar *st);
void stellar_loopbreak(struct stellar *st);
void stellar_reload_log_level(struct stellar *st);
struct logger *stellar_get_logger(struct stellar *st);
//return polling work result, 0: no work, 1: work
typedef int plugin_on_polling_func(void *plugin_env);
//return polling plugin_id
int stellar_polling_plugin_register(struct stellar *st, plugin_on_polling_func on_polling, void *plugin_env);
void stellar_emit_datapath_telemetry(struct packet *pkt, const char * module, const char *str);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,18 @@
#include <sys/uio.h>
#define STRATUM_MESSAGE_TOPIC "TOPIC_STRATUM"
enum cryptocurrency_type
{
ETH=1,
OTHER=2
};
struct stratum_field
{
enum cryptocurrency_type type;
struct iovec mining_pools;
struct iovec mining_program;
struct iovec mining_subscribe;
};//message data

View File

@@ -1,43 +1,52 @@
#pragma once
#include <stdlib.h> //calloc
#include <stddef.h> //NULL
#include <stdlib.h> //calloc
#include <stddef.h> //NULL
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define CALLOC(type, number) ((type *)calloc(sizeof(type), number))
#define REALLOC(type, ptr, number) ((type *)realloc(ptr, (number) * sizeof(type)))
#define FREE(p) {free(p); p = NULL;}
#define FREE(p) \
{ \
free(p); \
p = NULL; \
}
#define TRUE 1
#define FALSE 0
#define TRUE 1
#define FALSE 0
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
(type *)( (char *)__mptr - offsetof(type,member) ); })
#endif
#ifndef count_of
#define count_of(x) (sizeof(x) / sizeof(0 [x]))
#endif
#ifndef likely
#define likely(x) __builtin_expect((x), 1)
#endif /* likely */
#ifndef unlikely
#define unlikely(x) __builtin_expect((x), 0)
#endif /* unlikely */
#ifndef __unused
#define __unused __attribute__((__unused__))
#endif
#ifndef likely
#define likely(x) __builtin_expect((x),1)
#endif /* likely */
#ifndef unlikely
#define unlikely(x) __builtin_expect((x),0)
#endif /* unlikely */