#include #include #include #include #include #include #include #include #include "packet.h" #include "timestamp.h" #include "session_manager.h" #ifndef STELLAR_LOG_ERROR #define STELLAR_LOG_ERROR(format, ...) \ fprintf(stderr, "ERROR (stellar), " format "\n", ##__VA_ARGS__); #endif #ifndef STELLAR_LOG_DEBUG #define STELLAR_LOG_DEBUG(format, ...) \ fprintf(stderr, "DEBUG (stellar), " format "\n", ##__VA_ARGS__); #endif #define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED) #define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED) struct thread_ctx { pthread_t tid; uint16_t index; uint64_t need_exit; uint64_t is_runing; struct session_manager *sess_mgr; }; struct stellar_ctx { uint64_t need_exit; uint16_t max_worker_num; struct session_manager_config sess_mgr_cfg; struct thread_ctx thread_ctx[128]; } g_stellar_ctx = { .need_exit = 0, .max_worker_num = 1, // TODO session manager config }; static int recv_packet(const char **data) { static unsigned char packet_data[] = { 0x5c, 0x5e, 0xab, 0x2a, 0xa2, 0x00, 0x2c, 0x6b, 0xf5, 0x45, 0x88, 0x29, 0x08, 0x00, 0x45, 0x00, 0x00, 0x5c, 0x0b, 0x4d, 0x00, 0x00, 0x3b, 0x29, 0x09, 0xc8, 0xd2, 0x4d, 0x58, 0xa3, 0x3b, 0x42, 0x04, 0x32, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x20, 0x01, 0x0d, 0xa8, 0x02, 0x00, 0x90, 0x0e, 0x02, 0x00, 0x5e, 0xfe, 0xd2, 0x4d, 0x58, 0xa3, 0x26, 0x00, 0x14, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x02, 0x10, 0x58, 0xcd, 0x4c, 0x00, 0x50, 0x81, 0x80, 0x5c, 0x76, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00, 0xf7, 0x57, 0x00, 0x00, 0x02, 0x04, 0x04, 0xc4, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01, 0x04, 0x02}; *data = (const char *)packet_data; return sizeof(packet_data); } static void send_packet(const char *data, uint16_t len) { } static void signal_handler(int signo) { if (signo == SIGINT) { STELLAR_LOG_DEBUG("recv SIGINT, exit !!!"); ATOMIC_SET(&g_stellar_ctx.need_exit, 1); } if (signo == SIGQUIT) { STELLAR_LOG_DEBUG("recv SIGQUIT, exit !!!"); ATOMIC_SET(&g_stellar_ctx.need_exit, 1); } if (signo == SIGTERM) { STELLAR_LOG_DEBUG("recv SIGTERM, exit !!!"); ATOMIC_SET(&g_stellar_ctx.need_exit, 1); } } static void __session_dispatch(struct session *sess) { printf("\n"); printf("=> session dispatch: %p\n", sess); session_dump(sess); printf("<= session dispatch\n"); session_set0_cur_pkt(sess, NULL); session_set_cur_dir(sess, SESSION_DIR_NONE); } static void thread_ctx_init(struct stellar_ctx *ctx) { for (uint16_t i = 0; i < ctx->max_worker_num; i++) { struct thread_ctx *thd_ctx = &ctx->thread_ctx[i]; thd_ctx->index = i; thd_ctx->need_exit = 0; thd_ctx->is_runing = 0; thd_ctx->sess_mgr = session_manager_create(&ctx->sess_mgr_cfg); assert(thd_ctx->sess_mgr != NULL); } } static void thread_ctx_free(struct stellar_ctx *ctx) { for (uint16_t i = 0; i < ctx->max_worker_num; i++) { struct thread_ctx *thd_ctx = &ctx->thread_ctx[i]; if (ATOMIC_READ(&thd_ctx->is_runing) == 0) { session_manager_destroy(thd_ctx->sess_mgr); } } } static void *thread_cycle(void *arg) { uint16_t len = 0; const char *data = NULL; struct packet pkt; struct session *sess = NULL; struct thread_ctx *thd_ctx = (struct thread_ctx *)arg; struct session_manager *sess_mgr = thd_ctx->sess_mgr; char thread_name[16]; ATOMIC_SET(&thd_ctx->is_runing, 1); snprintf(thread_name, sizeof(thread_name), "stellar:%d", thd_ctx->index); prctl(PR_SET_NAME, (unsigned long long)thread_name, NULL, NULL, NULL); STELLAR_LOG_DEBUG("worker thread %s runing\n", thread_name); while (ATOMIC_READ(&thd_ctx->need_exit) == 0) { len = recv_packet(&data); if (data == NULL) { goto poll_wait; } packet_parse(&pkt, data, len); sess = session_manager_update_session(sess_mgr, &pkt); if (sess == NULL) { goto fast_forward; } __session_dispatch(sess); sess = session_manager_get_evicted_session(sess_mgr); if (sess) { __session_dispatch(sess); } fast_forward: send_packet(data, len); sess = session_manager_get_expired_session(sess_mgr); if (sess) { __session_dispatch(sess); } continue; poll_wait: sleep(session_manager_get_expire_interval(sess_mgr)); } ATOMIC_SET(&thd_ctx->is_runing, 0); STELLAR_LOG_DEBUG("worker thread %s exit\n", thread_name); return NULL; } int main(int argc, char **argv) { // TODO parse command line // TODO init log // TODO init plugin signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); signal(SIGTERM, signal_handler); timestamp_update(); thread_ctx_init(&g_stellar_ctx); for (uint16_t i = 0; i < g_stellar_ctx.max_worker_num; i++) { struct thread_ctx *thd_ctx = &g_stellar_ctx.thread_ctx[i]; if (pthread_create(&thd_ctx->tid, NULL, thread_cycle, (void *)thd_ctx) < 0) { STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno)); goto error_out; } } while (!g_stellar_ctx.need_exit) { timestamp_update(); sleep(1); } for (uint16_t i = 0; i < g_stellar_ctx.max_worker_num; i++) { struct thread_ctx *thd_ctx = &g_stellar_ctx.thread_ctx[i]; ATOMIC_SET(&thd_ctx->need_exit, 1); while (ATOMIC_READ(&thd_ctx->is_runing) == 1) { sleep(1); } } error_out: thread_ctx_free(&g_stellar_ctx); // TODO free plugin // TODO free log return 0; }