1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include <assert.h>
#include <MESA/MESA_prof_load.h>
#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", "stateless_sids", (int *)&(sce_ctx->stateless_sids), 2000);
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;
}
}
|