refactor ex_data_runtime & fix all leak memory
This commit is contained in:
@@ -13,10 +13,8 @@
|
||||
#include "log/log.h"
|
||||
#include "maat_utils.h"
|
||||
#include "maat_ip_plugin.h"
|
||||
#include "maat_ex_data.h"
|
||||
#include "ip_matcher.h"
|
||||
#include "maat_rule.h"
|
||||
#include "maat.h"
|
||||
#include "maat_garbage_collection.h"
|
||||
|
||||
#define MODULE_IP_PLUGIN module_name_str("maat.ip_plugin")
|
||||
@@ -29,9 +27,10 @@ struct ip_plugin_schema {
|
||||
int end_ip_column;
|
||||
int addr_format_column;
|
||||
int rule_tag_column;
|
||||
struct ex_data_schema *ex_schema;
|
||||
struct ex_container_schema container_schema;
|
||||
int table_id; //ugly
|
||||
struct table_manager *ref_tbl_mgr;
|
||||
struct log_handle *logger;
|
||||
|
||||
unsigned long long update_err_cnt;
|
||||
unsigned long long unmatch_tag_cnt;
|
||||
@@ -52,6 +51,7 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
||||
{
|
||||
size_t read_cnt = 0;
|
||||
struct ip_plugin_schema *schema = ALLOC(struct ip_plugin_schema, 1);
|
||||
schema->logger = logger;
|
||||
|
||||
cJSON *custom_item = NULL;
|
||||
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
||||
@@ -122,24 +122,8 @@ void ip_plugin_schema_free(void *ip_plugin_schema)
|
||||
if (NULL == ip_plugin_schema) {
|
||||
return;
|
||||
}
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
if (schema->ex_schema != NULL) {
|
||||
ex_data_schema_free(schema->ex_schema);
|
||||
schema->ex_schema = NULL;
|
||||
}
|
||||
|
||||
free(schema);
|
||||
}
|
||||
|
||||
struct ex_data_schema *ip_plugin_table_get_ex_data_schema(void *ip_plugin_schema)
|
||||
{
|
||||
if (NULL == ip_plugin_schema) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
|
||||
return schema->ex_schema;
|
||||
FREE(ip_plugin_schema);
|
||||
}
|
||||
|
||||
int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line,
|
||||
@@ -298,27 +282,44 @@ void ip_plugin_rule_free(struct ip_rule *rule)
|
||||
FREE(rule);
|
||||
}
|
||||
|
||||
void garbage_ip_plugin_rule_free(void *ip_rule, void *arg)
|
||||
{
|
||||
struct ip_rule *rule = (struct ip_rule *)ip_rule;
|
||||
ip_plugin_rule_free(rule);
|
||||
}
|
||||
|
||||
void ip_plugin_table_set_ex_data_schema(void *ip_plugin_schema,
|
||||
maat_ex_new_func_t *new_func,
|
||||
maat_ex_free_func_t *free_func,
|
||||
maat_ex_dup_func_t *dup_func,
|
||||
long argl, void *argp,
|
||||
struct log_handle *logger)
|
||||
int ip_plugin_table_set_ex_container_schema(void *ip_plugin_schema, int table_id,
|
||||
maat_ex_new_func_t *new_func,
|
||||
maat_ex_free_func_t *free_func,
|
||||
maat_ex_dup_func_t *dup_func,
|
||||
void (*custom_data_free)(void *),
|
||||
long argl, void *argp)
|
||||
{
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
|
||||
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp);
|
||||
if (1 == schema->container_schema.set_flag) {
|
||||
log_error(schema->logger, MODULE_IP_PLUGIN,
|
||||
"[%s:%d] ip_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
|
||||
__FUNCTION__, __LINE__, table_id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
schema->container_schema.table_id = table_id;
|
||||
schema->container_schema.custom_data_free = custom_data_free;
|
||||
schema->container_schema.ex_schema.new_func = new_func;
|
||||
schema->container_schema.ex_schema.free_func = free_func;
|
||||
schema->container_schema.ex_schema.dup_func = dup_func;
|
||||
schema->container_schema.ex_schema.argl = argl;
|
||||
schema->container_schema.ex_schema.argp = argp;
|
||||
schema->container_schema.set_flag = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ex_container_schema *ip_plugin_table_get_ex_container_schema(void *ip_plugin_schema)
|
||||
{
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
|
||||
return &(schema->container_schema);
|
||||
}
|
||||
|
||||
int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
|
||||
struct ex_data_schema *ex_schema, const char *table_name,
|
||||
const char *row, const char *key, size_t key_len,
|
||||
const char *table_name, const char *row,
|
||||
const char *key, size_t key_len,
|
||||
struct ip_rule *ip_plugin_rule, int is_valid)
|
||||
{
|
||||
int ret = -1;
|
||||
@@ -335,7 +336,7 @@ int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt,
|
||||
}
|
||||
} else {
|
||||
// add
|
||||
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, table_name, row, key, key_len);
|
||||
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
|
||||
struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule);
|
||||
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
|
||||
if (ret < 0) {
|
||||
@@ -357,8 +358,11 @@ void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num,
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
struct ip_plugin_runtime *ip_plugin_rt = ALLOC(struct ip_plugin_runtime, 1);
|
||||
|
||||
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, ex_container_free,
|
||||
logger);
|
||||
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id, logger);
|
||||
if (1 == schema->container_schema.set_flag) {
|
||||
ex_data_runtime_set_ex_container_schema(ip_plugin_rt->ex_data_rt, &(schema->container_schema));
|
||||
}
|
||||
|
||||
ip_plugin_rt->ref_garbage_bin = garbage_bin;
|
||||
ip_plugin_rt->logger = logger;
|
||||
|
||||
@@ -409,8 +413,7 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ex_data_schema *ex_schema = schema->ex_schema;
|
||||
if (ex_schema != NULL) {
|
||||
if (1 == schema->container_schema.set_flag) {
|
||||
if (1 == is_valid) {
|
||||
// add
|
||||
ip_plugin_rule = ip_plugin_rule_new(line, schema, table_name, ip_plugin_rt->logger);
|
||||
@@ -421,7 +424,7 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
|
||||
|
||||
const char *key = line + item_id_offset;
|
||||
size_t key_len = item_id_len;
|
||||
ret = ip_plugin_runtime_update_row(ip_plugin_rt, ex_schema, table_name, line, key, key_len,
|
||||
ret = ip_plugin_runtime_update_row(ip_plugin_rt, table_name, line, key, key_len,
|
||||
ip_plugin_rule, is_valid);
|
||||
if (ret < 0) {
|
||||
if (ip_plugin_rule != NULL) {
|
||||
@@ -520,17 +523,11 @@ struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime
|
||||
return ip_plugin_rt->ex_data_rt;
|
||||
}
|
||||
|
||||
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schema,
|
||||
const struct ip_addr *ip_addr, void **ex_data_array,
|
||||
size_t n_ex_data)
|
||||
int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr,
|
||||
void **ex_data_array, size_t n_ex_data)
|
||||
{
|
||||
if (NULL == ip_plugin_runtime || NULL == ip_plugin_schema) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
||||
struct ex_data_schema *ex_schema = schema->ex_schema;
|
||||
if (NULL == ex_schema) {
|
||||
if (NULL == ip_plugin_runtime || NULL == ip_addr ||
|
||||
NULL == ex_data_array || 0 == n_ex_data) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -552,7 +549,7 @@ int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schem
|
||||
assert(ip_plugin_rt->ip_matcher != NULL);
|
||||
int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data, results, n_ex_data);
|
||||
for (int i = 0; i < n_result; i++) {
|
||||
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt, ex_schema,
|
||||
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt,
|
||||
(struct ex_container *)results[i].tag);
|
||||
}
|
||||
return n_result;
|
||||
|
||||
Reference in New Issue
Block a user