#include #include #include "sce.h" #include "log.h" #include "global_metrics.h" char *memdup(const char *src, int len) { if (src == NULL || len == 0) { return NULL; } char *dst = (char *)calloc(len + 1, sizeof(char)); memcpy(dst, src, len); return dst; } /****************************************************************************** * Struct Session Ctx ******************************************************************************/ struct session_ctx *session_ctx_new() { struct session_ctx *session_ctx = (struct session_ctx *)calloc(1, sizeof(struct session_ctx)); assert(session_ctx != NULL); mutable_array_init(&session_ctx->rule_ids); return session_ctx; } void session_ctx_free(struct session_ctx *session_ctx) { if (session_ctx) { if (session_ctx->session_addr) { free(session_ctx->session_addr); session_ctx->session_addr = NULL; } if (session_ctx->ctrl_pkt_hdr_ptr) { free(session_ctx->ctrl_pkt_hdr_ptr); session_ctx->ctrl_pkt_hdr_ptr = NULL; } if (session_ctx->chaining_raw) { selected_chaining_destory(session_ctx->chaining_raw); session_ctx->chaining_raw = NULL; } if (session_ctx->chaining_decrypted) { selected_chaining_destory(session_ctx->chaining_decrypted); session_ctx->chaining_decrypted = NULL; } free(session_ctx); session_ctx = 0; } } /****************************************************************************** * Struct SCE Ctx ******************************************************************************/ struct sce_ctx *sce_ctx_create(const char *profile) { struct sce_ctx *sce_ctx = (struct sce_ctx *)calloc(1, sizeof(struct sce_ctx)); MESA_load_profile_int_def(profile, "system", "enable_debug", (int *)&(sce_ctx->enable_debug), 0); MESA_load_profile_int_def(profile, "system", "enable_send_log", (int *)&(sce_ctx->enable_send_log), 0); MESA_load_profile_int_def(profile, "system", "firewall_sids", (int *)&(sce_ctx->firewall_sids), 1001); MESA_load_profile_int_def(profile, "system", "nr_worker_threads", (int *)&(sce_ctx->nr_worker_threads), 8); MESA_load_profile_uint_range(profile, "system", "cpu_affinity_mask", MAX_THREAD_NUM, (unsigned int *)sce_ctx->cpu_affinity_mask); MESA_load_profile_int_def(profile, "system", "ts_update_interval_ms", (int *)&(sce_ctx->ts_update_interval_ms), 1); sce_ctx->nr_worker_threads = MIN(sce_ctx->nr_worker_threads, MAX_THREAD_NUM); CPU_ZERO(&sce_ctx->coremask); for (int i = 0; i < sce_ctx->nr_worker_threads; i++) { int cpu_id = sce_ctx->cpu_affinity_mask[i]; CPU_SET(cpu_id, &sce_ctx->coremask); } sce_ctx->ts = timestamp_new(sce_ctx->ts_update_interval_ms); sce_ctx->metrics = global_metrics_create(profile, sce_ctx->nr_worker_threads); if (sce_ctx->metrics == NULL) { goto error_out; } sce_ctx->enforcer = policy_enforcer_create("SCE", profile, sce_ctx->nr_worker_threads); if (sce_ctx->enforcer == NULL) { goto error_out; } if (policy_enforcer_register(sce_ctx->enforcer) == -1) { goto error_out; } sce_ctx->io = packet_io_create(profile, sce_ctx->nr_worker_threads, &sce_ctx->coremask); if (sce_ctx->io == NULL) { goto error_out; } return sce_ctx; error_out: sce_ctx_destory(sce_ctx); return NULL; } void sce_ctx_destory(struct sce_ctx *sce_ctx) { if (sce_ctx) { packet_io_destory(sce_ctx->io); policy_enforcer_destory(sce_ctx->enforcer); global_metrics_destory(sce_ctx->metrics); timestamp_free(sce_ctx->ts); free(sce_ctx); sce_ctx = NULL; } }