2023-01-30 21:59:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_ip_plugin.cpp
|
|
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu wentan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_utils.h"
|
2023-02-03 17:28:14 +08:00
|
|
|
#include "maat_ip_plugin.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
#include "maat_ex_data.h"
|
|
|
|
|
#include "IPMatcher.h"
|
|
|
|
|
#include "maat_rule.h"
|
2023-01-31 20:39:53 +08:00
|
|
|
#include "maat_garbage_collection.h"
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
#define MODULE_IP_PLUGIN module_name_str("maat.ip_plugin")
|
|
|
|
|
#define MAX_IP_STR 128
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_item {
|
|
|
|
|
int item_id;
|
|
|
|
|
int ip_type;
|
|
|
|
|
char start_ip[MAX_IP_STR];
|
|
|
|
|
char end_ip[MAX_IP_STR];
|
|
|
|
|
int rule_tag;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_schema {
|
|
|
|
|
int item_id_column;
|
|
|
|
|
int ip_type_column;
|
|
|
|
|
int start_ip_column;
|
|
|
|
|
int end_ip_column;
|
|
|
|
|
int rule_tag_column;
|
|
|
|
|
struct ex_data_schema *ex_schema;
|
|
|
|
|
int table_id; //ugly
|
2023-02-09 22:13:15 +08:00
|
|
|
struct table_manager *ref_tbl_mgr;
|
2023-02-03 17:28:14 +08:00
|
|
|
|
|
|
|
|
unsigned long long update_err_cnt;
|
|
|
|
|
unsigned long long unmatch_tag_cnt;
|
2023-01-30 21:59:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_runtime {
|
|
|
|
|
struct ip_matcher *ip_matcher;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt;
|
|
|
|
|
|
|
|
|
|
uint32_t rule_num;
|
|
|
|
|
uint32_t updating_rule_num;
|
|
|
|
|
|
|
|
|
|
struct maat_garbage_bin *ref_garbage_bin;
|
|
|
|
|
struct log_handle *logger;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
size_t read_cnt = 0;
|
2023-02-09 22:13:15 +08:00
|
|
|
struct ip_plugin_schema *schema = ALLOC(struct ip_plugin_schema, 1);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
cJSON *custom_item = NULL;
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
2023-01-31 20:39:53 +08:00
|
|
|
if (item != NULL && item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->table_id = item->valueint;
|
2023-01-31 20:39:53 +08:00
|
|
|
read_cnt++;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item = cJSON_GetObjectItem(json, "custom");
|
|
|
|
|
if (NULL == item || item->type != cJSON_Object) {
|
2023-02-03 17:28:14 +08:00
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"table %s has no custom column", table_name);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "item_id");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->item_id_column = custom_item->valueint;
|
2023-01-30 21:59:35 +08:00
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "ip_type");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->ip_type_column = custom_item->valueint;
|
2023-01-30 21:59:35 +08:00
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "start_ip");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->start_ip_column = custom_item->valueint;
|
2023-01-30 21:59:35 +08:00
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "end_ip");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->end_ip_column = custom_item->valueint;
|
2023-01-30 21:59:35 +08:00
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
// rule_tag is optional
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "rule_tag");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->rule_tag_column = custom_item->valueint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
schema->ref_tbl_mgr = tbl_mgr;
|
2023-02-03 17:28:14 +08:00
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
if (read_cnt < 5) {
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
return schema;
|
2023-01-30 21:59:35 +08:00
|
|
|
error:
|
2023-02-09 22:13:15 +08:00
|
|
|
FREE(schema);
|
2023-01-30 21:59:35 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
int ip_plugin_accept_tag_match(struct ip_plugin_schema *schema, const char *line,
|
|
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
2023-02-09 22:13:15 +08:00
|
|
|
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
|
2023-02-03 17:28:14 +08:00
|
|
|
|
|
|
|
|
if (schema->rule_tag_column > 0 && n_tag > 0) {
|
|
|
|
|
int ret = get_column_pos(line, schema->rule_tag_column,
|
|
|
|
|
&column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip_plugin table(table_id:%d) has no rule_tag, line:%s",
|
|
|
|
|
schema->table_id, line);
|
|
|
|
|
schema->update_err_cnt++;
|
|
|
|
|
return TAG_MATCH_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (column_len > 2) {
|
|
|
|
|
char *tag_str = ALLOC(char, column_len + 1);
|
|
|
|
|
memcpy(tag_str, (line + column_offset), column_len);
|
2023-02-09 22:13:15 +08:00
|
|
|
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
|
2023-02-03 17:28:14 +08:00
|
|
|
FREE(tag_str);
|
|
|
|
|
if (TAG_MATCH_ERR == ret) {
|
|
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip_plugin table(table_id:%d) has invalid tag format, line:%s",
|
|
|
|
|
schema->table_id, line);
|
|
|
|
|
schema->update_err_cnt++;
|
|
|
|
|
return TAG_MATCH_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TAG_MATCH_UNMATCHED == ret) {
|
|
|
|
|
schema->unmatch_tag_cnt++;
|
|
|
|
|
return TAG_MATCH_UNMATCHED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TAG_MATCH_MATCHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_item *
|
|
|
|
|
ip_plugin_item_new(const char *line, struct ip_plugin_schema *schema,
|
|
|
|
|
struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-02-03 17:28:14 +08:00
|
|
|
int ret = ip_plugin_accept_tag_match(schema, line, logger);
|
|
|
|
|
if (ret == TAG_MATCH_UNMATCHED) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
|
|
|
|
struct ip_plugin_item *ip_plugin_item = ALLOC(struct ip_plugin_item, 1);
|
2023-02-03 17:28:14 +08:00
|
|
|
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
|
2023-01-30 21:59:35 +08:00
|
|
|
if (ret < 0) {
|
2023-02-03 17:28:14 +08:00
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip plugin table(table_id:%d) line:%s has no item_id",
|
|
|
|
|
schema->table_id, line);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
ip_plugin_item->item_id = atoi(line + column_offset);
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
ret = get_column_pos(line, schema->ip_type_column, &column_offset, &column_len);
|
2023-01-30 21:59:35 +08:00
|
|
|
if (ret < 0) {
|
2023-02-03 17:28:14 +08:00
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip plugin table(table_id:%d) line:%s has no ip_type",
|
|
|
|
|
schema->table_id, line);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
ip_plugin_item->ip_type = atoi(line + column_offset);
|
|
|
|
|
if (ip_plugin_item->ip_type != 4 && ip_plugin_item->ip_type != 6) {
|
|
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip_plugin table(table_id:%d) line:%s ip_type[%d] invalid",
|
2023-02-03 17:28:14 +08:00
|
|
|
schema->table_id, line, ip_plugin_item->ip_type);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
ret = get_column_pos(line, schema->start_ip_column, &column_offset, &column_len);
|
2023-01-30 21:59:35 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip_plugin table(table_id:%d) line:%s has no start_ip",
|
2023-02-03 17:28:14 +08:00
|
|
|
schema->table_id, line);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
strncpy(ip_plugin_item->start_ip, line + column_offset,
|
|
|
|
|
MIN(column_len, sizeof(ip_plugin_item->start_ip)));
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
ret = get_column_pos(line, schema->end_ip_column, &column_offset, &column_len);
|
2023-01-30 21:59:35 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"ip_plugin table(table_id:%d) line:%s has no end_ip",
|
2023-02-03 17:28:14 +08:00
|
|
|
schema->table_id, line);
|
2023-01-30 21:59:35 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-03 17:28:14 +08:00
|
|
|
strncpy(ip_plugin_item->end_ip, line + column_offset,
|
|
|
|
|
MIN(column_len, sizeof(ip_plugin_item->end_ip)));
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
return ip_plugin_item;
|
|
|
|
|
error:
|
|
|
|
|
FREE(ip_plugin_item);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ip_plugin_item_free(struct ip_plugin_item *item)
|
|
|
|
|
{
|
|
|
|
|
FREE(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ip_plugin_table_ex_data_schema_flag(struct ip_plugin_schema *ip_plugin_schema)
|
|
|
|
|
{
|
2023-01-31 20:39:53 +08:00
|
|
|
return 0;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ip_plugin_table_set_ex_data_schema(void *ip_plugin_schema,
|
|
|
|
|
maat_plugin_ex_new_func_t *new_func,
|
|
|
|
|
maat_plugin_ex_free_func_t *free_func,
|
|
|
|
|
maat_plugin_ex_dup_func_t *dup_func,
|
2023-01-31 20:39:53 +08:00
|
|
|
long argl, void *argp,
|
|
|
|
|
struct log_handle *logger)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-01-31 20:39:53 +08:00
|
|
|
if (NULL == ip_plugin_schema) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
|
|
|
|
if (schema->ex_schema != NULL) {
|
2023-01-30 21:59:35 +08:00
|
|
|
assert(0);
|
2023-01-31 20:39:53 +08:00
|
|
|
log_error(logger, MODULE_IP_PLUGIN,
|
|
|
|
|
"Error: %s, EX data schema already registed", __FUNCTION__);
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
schema->ex_schema = ex_data_schema_new(new_func, free_func, dup_func, argl, argp);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ip_plugin_runtime_update_row(struct ip_plugin_runtime *rt, struct ip_plugin_schema *schema,
|
2023-01-31 20:39:53 +08:00
|
|
|
const char *row, char *key, size_t key_len,
|
|
|
|
|
struct ip_plugin_item *ip_plugin_item, int is_valid)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
int ret = -1;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = rt->ex_data_rt;
|
2023-01-31 20:39:53 +08:00
|
|
|
int set_flag = ip_plugin_table_ex_data_schema_flag(schema);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
|
|
|
|
if (1 == set_flag) {
|
|
|
|
|
if (0 == is_valid) {
|
|
|
|
|
//delete
|
|
|
|
|
ret = ex_data_runtime_del_ex_container(ex_data_rt, key, key_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//add
|
|
|
|
|
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row, key, key_len);
|
2023-01-31 20:39:53 +08:00
|
|
|
struct ex_data_container *ex_container = ex_data_container_new(ex_data, (void *)ip_plugin_item);
|
2023-01-30 21:59:35 +08:00
|
|
|
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ex_data_runtime_cache_row_put(ex_data_rt, row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
void *ip_plugin_runtime_new(void *ip_plugin_schema, int max_thread_num,
|
|
|
|
|
struct maat_garbage_bin *garbage_bin,
|
2023-01-30 21:59:35 +08:00
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == ip_plugin_schema) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
ip_plugin_rt->ex_data_rt = ex_data_runtime_new(schema->table_id,
|
|
|
|
|
ex_data_container_free, logger);
|
2023-01-30 21:59:35 +08:00
|
|
|
ip_plugin_rt->ref_garbage_bin = garbage_bin;
|
|
|
|
|
ip_plugin_rt->logger = logger;
|
|
|
|
|
|
|
|
|
|
return ip_plugin_rt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ip_plugin_runtime_free(void *ip_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == ip_plugin_runtime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
|
|
|
|
|
if (ip_plugin_rt->ip_matcher != NULL) {
|
|
|
|
|
ip_matcher_free(ip_plugin_rt->ip_matcher);
|
2023-01-31 20:39:53 +08:00
|
|
|
ip_plugin_rt->ip_matcher = NULL;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ip_plugin_rt->ex_data_rt != NULL) {
|
|
|
|
|
ex_data_runtime_free(ip_plugin_rt->ex_data_rt);
|
2023-01-31 20:39:53 +08:00
|
|
|
ip_plugin_rt->ex_data_rt = NULL;
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(ip_plugin_rt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 20:39:53 +08:00
|
|
|
void ip_plugin_item_to_ip_rule(struct ip_plugin_item *item, struct ip_rule *rule)
|
|
|
|
|
{
|
|
|
|
|
if (4 == item->ip_type) {
|
|
|
|
|
rule->type = IPv4;
|
|
|
|
|
ip_format2range(item->ip_type, IP_FORMAT_RANGE, item->start_ip, item->end_ip,
|
|
|
|
|
&(rule->ipv4_rule.start_ip), &(rule->ipv4_rule.end_ip));
|
|
|
|
|
} else {
|
|
|
|
|
rule->type = IPv6;
|
|
|
|
|
ip_format2range(item->ip_type, IP_FORMAT_RANGE, item->start_ip, item->end_ip,
|
|
|
|
|
rule->ipv6_rule.start_ip, rule->ipv6_rule.end_ip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule->rule_id = item->item_id;
|
|
|
|
|
rule->user_tag = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema,
|
|
|
|
|
const char *line, int valid_column)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
2023-02-03 17:28:14 +08:00
|
|
|
if (NULL == ip_plugin_runtime || NULL == ip_plugin_schema ||
|
|
|
|
|
NULL == line) {
|
2023-01-30 21:59:35 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_item *ip_plugin_item = NULL;
|
|
|
|
|
struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema;
|
|
|
|
|
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
|
|
|
|
|
int item_id = get_column_value(line, schema->item_id_column);
|
|
|
|
|
int is_valid = get_column_value(line, valid_column);
|
|
|
|
|
if (is_valid < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
if (schema->ex_schema != NULL) {
|
|
|
|
|
if (1 == is_valid) {
|
|
|
|
|
// add
|
|
|
|
|
ip_plugin_item = ip_plugin_item_new(line, schema, ip_plugin_rt->logger);
|
|
|
|
|
if (NULL == ip_plugin_item) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
char *key = (char *)&item_id;
|
|
|
|
|
int ret = ip_plugin_runtime_update_row(ip_plugin_rt, schema, line, key,
|
|
|
|
|
sizeof(int), ip_plugin_item, is_valid);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
if (ip_plugin_item != NULL) {
|
|
|
|
|
FREE(ip_plugin_item);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
2023-01-30 21:59:35 +08:00
|
|
|
} else {
|
2023-02-09 22:13:15 +08:00
|
|
|
if (0 == is_valid) {
|
|
|
|
|
ip_plugin_rt->rule_num--;
|
|
|
|
|
} else {
|
|
|
|
|
ip_plugin_rt->rule_num++;
|
|
|
|
|
}
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
} else {
|
|
|
|
|
//ex_schema not set
|
|
|
|
|
ex_data_runtime_cache_row_put(ip_plugin_rt->ex_data_rt, line);
|
|
|
|
|
ip_plugin_rt->rule_num = ex_data_runtime_cached_row_count(ip_plugin_rt->ex_data_rt);
|
2023-01-30 21:59:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name)
|
2023-01-30 21:59:35 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == ip_plugin_runtime) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
struct ex_data_container **ex_container = NULL;
|
|
|
|
|
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = ip_plugin_rt->ex_data_rt;
|
|
|
|
|
|
|
|
|
|
size_t rule_cnt = ex_data_runtime_list_updating_ex_container(ex_data_rt, &ex_container);
|
|
|
|
|
if (0 == rule_cnt) {
|
|
|
|
|
FREE(ex_container);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_rule *rules = ALLOC(struct ip_rule, rule_cnt);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rule_cnt; i++) {
|
|
|
|
|
struct ip_plugin_item *item = (struct ip_plugin_item *)ex_container[i]->custom_data;
|
|
|
|
|
ip_plugin_item_to_ip_rule(item, &rules[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_matcher *new_ip_matcher = NULL;
|
|
|
|
|
struct ip_matcher *old_ip_matcher = NULL;
|
|
|
|
|
size_t mem_used = 0;
|
|
|
|
|
|
|
|
|
|
if (rule_cnt > 0) {
|
2023-01-31 20:39:53 +08:00
|
|
|
log_info(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
|
2023-02-09 22:13:15 +08:00
|
|
|
"table[%s] committing %zu ip_plugin rules for rebuilding ip_matcher engine",
|
|
|
|
|
table_name, rule_cnt);
|
2023-01-30 21:59:35 +08:00
|
|
|
new_ip_matcher = ip_matcher_new(rules, rule_cnt, &mem_used);
|
|
|
|
|
if (NULL == new_ip_matcher) {
|
2023-01-31 20:39:53 +08:00
|
|
|
log_error(ip_plugin_rt->logger, MODULE_IP_PLUGIN,
|
2023-02-09 22:13:15 +08:00
|
|
|
"table[%s] rebuild ip_matcher engine failed when update %zu ip_plugin rules",
|
|
|
|
|
table_name, rule_cnt);
|
2023-01-30 21:59:35 +08:00
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
old_ip_matcher = ip_plugin_rt->ip_matcher;
|
|
|
|
|
ip_plugin_rt->ip_matcher = new_ip_matcher;
|
2023-02-03 17:28:14 +08:00
|
|
|
maat_garbage_bagging(ip_plugin_rt->ref_garbage_bin, old_ip_matcher,
|
|
|
|
|
(void (*)(void*))ip_matcher_free);
|
2023-01-30 21:59:35 +08:00
|
|
|
ex_data_runtime_commit(ex_data_rt);
|
|
|
|
|
|
|
|
|
|
FREE(rules);
|
|
|
|
|
FREE(ex_container);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == ip_plugin_runtime) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ip_plugin_runtime *ip_plugin_rt = (struct ip_plugin_runtime *)ip_plugin_runtime;
|
|
|
|
|
|
|
|
|
|
return ip_plugin_rt->ex_data_rt;
|
|
|
|
|
}
|