2023-02-09 22:13:15 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
2023-05-15 07:24:36 +00:00
|
|
|
* File: maat_fqdn_plugin.c
|
2023-02-09 22:13:15 +08:00
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu wentan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
2023-05-15 07:24:36 +00:00
|
|
|
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
|
2023-02-09 22:13:15 +08:00
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
#include "alignment.h"
|
2023-02-09 22:13:15 +08:00
|
|
|
#include "maat_fqdn_plugin.h"
|
|
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "maat_table.h"
|
|
|
|
|
#include "maat_rule.h"
|
|
|
|
|
#include "maat_garbage_collection.h"
|
|
|
|
|
|
|
|
|
|
#define MODULE_FQDN_PLUGIN module_name_str("maat.bool_plugin")
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_schema {
|
|
|
|
|
int item_id_column;
|
|
|
|
|
int suffix_flag_column;
|
|
|
|
|
int fqdn_column;
|
|
|
|
|
int rule_tag_column;
|
2023-04-05 21:09:19 +08:00
|
|
|
struct ex_container_schema container_schema;
|
2023-02-09 22:13:15 +08:00
|
|
|
int table_id;
|
|
|
|
|
struct table_manager *ref_tbl_mgr;
|
2023-04-05 21:09:19 +08:00
|
|
|
struct log_handle *logger;
|
2023-02-09 22:13:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime {
|
|
|
|
|
struct FQDN_engine *engine;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt;
|
2023-04-13 14:56:35 +08:00
|
|
|
long long version;
|
|
|
|
|
long long rule_num;
|
2023-04-20 15:34:56 +08:00
|
|
|
long long update_err_cnt;
|
|
|
|
|
size_t n_worker_thread;
|
2023-02-09 22:13:15 +08:00
|
|
|
struct maat_garbage_bin *ref_garbage_bin;
|
|
|
|
|
struct log_handle *logger;
|
2023-04-20 15:34:56 +08:00
|
|
|
|
|
|
|
|
long long *scan_cnt;
|
|
|
|
|
long long *scan_cpu_time;
|
2023-02-09 22:13:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void *fqdn_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
struct fqdn_plugin_schema *schema = ALLOC(struct fqdn_plugin_schema, 1);
|
2023-04-05 21:09:19 +08:00
|
|
|
schema->logger = logger;
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
cJSON *custom_item = NULL;
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
2023-03-27 15:52:47 +08:00
|
|
|
if (item != NULL && item->type == cJSON_Number) {
|
2023-02-09 22:13:15 +08:00
|
|
|
schema->table_id = item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> schema has no table_id column",
|
2023-03-27 15:52:47 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item = cJSON_GetObjectItem(json, "custom");
|
|
|
|
|
if (NULL == item || item->type != cJSON_Object) {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> schema has no custom column",
|
2023-03-02 14:52:31 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-02-09 22:13:15 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "item_id");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->item_id_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> schema has no item_id column",
|
2023-03-27 15:52:47 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "suffix_match_method");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->suffix_flag_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> schema has no suffix_match_method column",
|
2023-03-27 15:52:47 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rule_tag is optional
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "fqdn");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->fqdn_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> schema has no fqdn column",
|
2023-03-27 15:52:47 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
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;
|
|
|
|
|
return schema;
|
|
|
|
|
error:
|
|
|
|
|
FREE(schema);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fqdn_plugin_schema_free(void *fqdn_plugin_schema)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_schema) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
FREE(fqdn_plugin_schema);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int fqdn_plugin_table_set_ex_container_schema(void *fqdn_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)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
2023-04-05 21:09:19 +08:00
|
|
|
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
|
|
|
|
|
|
|
|
|
|
if (1 == schema->container_schema.set_flag) {
|
|
|
|
|
log_error(schema->logger, MODULE_FQDN_PLUGIN,
|
|
|
|
|
"[%s:%d] fqdn_plugin table(table_id:%d) ex_container_schema has been set, can't set again",
|
|
|
|
|
__FUNCTION__, __LINE__, table_id);
|
|
|
|
|
return -1;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
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;
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
return 0;
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
struct ex_container_schema *fqdn_plugin_table_get_ex_container_schema(void *fqdn_plugin_schema)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
|
|
|
|
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
return &(schema->container_schema);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 15:59:34 +08:00
|
|
|
void fqdn_rule_free(struct FQDN_rule *fqdn_rule)
|
|
|
|
|
{
|
2023-05-23 03:23:39 +00:00
|
|
|
if (NULL == fqdn_rule) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
assert(fqdn_rule->user_tag == NULL);
|
2023-05-23 03:23:39 +00:00
|
|
|
if (fqdn_rule->FQDN != NULL) {
|
|
|
|
|
FREE(fqdn_rule->FQDN);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 17:50:53 +08:00
|
|
|
FREE(fqdn_rule);
|
2023-04-04 15:59:34 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
void *fqdn_plugin_runtime_new(void *fqdn_plugin_schema, size_t max_thread_num,
|
2023-04-13 14:56:35 +08:00
|
|
|
struct maat_garbage_bin *garbage_bin,
|
2023-02-09 22:13:15 +08:00
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_schema) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = ALLOC(struct fqdn_plugin_runtime, 1);
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
fqdn_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(fqdn_plugin_rt->ex_data_rt, &(schema->container_schema));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->n_worker_thread = max_thread_num;
|
2023-02-09 22:13:15 +08:00
|
|
|
fqdn_plugin_rt->ref_garbage_bin = garbage_bin;
|
|
|
|
|
fqdn_plugin_rt->logger = logger;
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
|
2023-04-24 19:18:12 +08:00
|
|
|
fqdn_plugin_rt->scan_cpu_time = alignment_int64_array_alloc(max_thread_num);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
return fqdn_plugin_rt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void fqdn_plugin_runtime_free(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
if (fqdn_plugin_rt->engine != NULL) {
|
|
|
|
|
FQDN_engine_free(fqdn_plugin_rt->engine);
|
|
|
|
|
fqdn_plugin_rt->engine = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fqdn_plugin_rt->ex_data_rt != NULL) {
|
|
|
|
|
ex_data_runtime_free(fqdn_plugin_rt->ex_data_rt);
|
|
|
|
|
fqdn_plugin_rt->ex_data_rt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 15:34:56 +08:00
|
|
|
if (fqdn_plugin_rt->scan_cnt != NULL) {
|
|
|
|
|
alignment_int64_array_free(fqdn_plugin_rt->scan_cnt);
|
|
|
|
|
fqdn_plugin_rt->scan_cnt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fqdn_plugin_rt->scan_cpu_time != NULL) {
|
|
|
|
|
alignment_int64_array_free(fqdn_plugin_rt->scan_cpu_time);
|
|
|
|
|
fqdn_plugin_rt->scan_cpu_time = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
FREE(fqdn_plugin_rt);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 03:23:39 +00:00
|
|
|
int fqdn_plugin_accept_tag_match(struct fqdn_plugin_schema *schema, const char *table_name,
|
|
|
|
|
const char *line, struct log_handle *logger)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
|
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
|
|
|
|
size_t n_tag = table_manager_accept_tags_count(schema->ref_tbl_mgr);
|
|
|
|
|
|
|
|
|
|
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_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> has no rule_tag in line:%s",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
return TAG_MATCH_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (column_len > 2) {
|
|
|
|
|
char *tag_str = ALLOC(char, column_len + 1);
|
|
|
|
|
memcpy(tag_str, (line + column_offset), column_len);
|
|
|
|
|
ret = table_manager_accept_tags_match(schema->ref_tbl_mgr, tag_str);
|
|
|
|
|
FREE(tag_str);
|
|
|
|
|
if (TAG_MATCH_ERR == ret) {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> has invalid tag format in line:%s",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
return TAG_MATCH_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TAG_MATCH_UNMATCHED == ret) {
|
2023-05-23 03:23:39 +00:00
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
|
|
|
|
"[%s:%d] table: <%s> has unmatched tag in line:%s",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
return TAG_MATCH_UNMATCHED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TAG_MATCH_MATCHED;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
struct FQDN_rule *
|
|
|
|
|
fqdn_plugin_rule_new(const char *line, struct fqdn_plugin_schema *schema,
|
2023-03-29 22:25:14 +08:00
|
|
|
const char *table_name, struct log_handle *logger)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
2023-05-23 03:23:39 +00:00
|
|
|
int ret = fqdn_plugin_accept_tag_match(schema, table_name, line, logger);
|
2023-02-09 22:13:15 +08:00
|
|
|
if (ret == TAG_MATCH_UNMATCHED) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
2023-02-23 19:08:26 +08:00
|
|
|
const char *fqdn = NULL;
|
|
|
|
|
size_t fqdn_len = 0;
|
|
|
|
|
struct FQDN_rule *fqdn_plugin_rule = ALLOC(struct FQDN_rule, 1);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> has no item_id in line:%s",
|
2023-03-29 22:25:14 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-23 19:08:26 +08:00
|
|
|
fqdn_plugin_rule->id = atoi(line + column_offset);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->suffix_flag_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> has no suffix_match_method in line:%s",
|
2023-03-29 22:25:14 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-23 19:08:26 +08:00
|
|
|
fqdn_plugin_rule->is_suffix_match = atoi(line + column_offset);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->fqdn_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FQDN_PLUGIN,
|
2023-05-23 03:23:39 +00:00
|
|
|
"[%s:%d] table: <%s> has no fqdn in line:%s",
|
2023-03-29 22:25:14 +08:00
|
|
|
__FUNCTION__, __LINE__, table_name, line);
|
2023-02-09 22:13:15 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
fqdn = line + column_offset;
|
|
|
|
|
fqdn_len = column_len;
|
|
|
|
|
|
|
|
|
|
if (fqdn[0] == '.') {
|
|
|
|
|
fqdn++;
|
|
|
|
|
fqdn_len--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fqdn[fqdn_len] == '/') {
|
|
|
|
|
fqdn_len--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fqdn_plugin_rule->FQDN = ALLOC(char, fqdn_len + 1);
|
|
|
|
|
memcpy(fqdn_plugin_rule->FQDN, fqdn, fqdn_len);
|
|
|
|
|
fqdn_plugin_rule->len = fqdn_len;
|
|
|
|
|
|
|
|
|
|
return fqdn_plugin_rule;
|
2023-02-09 22:13:15 +08:00
|
|
|
error:
|
2023-02-23 19:08:26 +08:00
|
|
|
FREE(fqdn_plugin_rule);
|
2023-02-09 22:13:15 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
void fqdn_plugin_rule_free(struct FQDN_rule *rule)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
2023-05-23 03:23:39 +00:00
|
|
|
if (NULL == rule) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
if (rule->FQDN != NULL) {
|
|
|
|
|
FREE(rule->FQDN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(rule);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt,
|
2023-04-05 21:09:19 +08:00
|
|
|
const char *table_name, const char *row,
|
|
|
|
|
const char *key, size_t key_len,
|
|
|
|
|
struct FQDN_rule *fqdn_plugin_rule,
|
|
|
|
|
int is_valid)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
2023-02-23 19:08:26 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt;
|
|
|
|
|
|
|
|
|
|
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
|
2023-04-05 21:09:19 +08:00
|
|
|
void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, table_name, row, key, key_len);
|
2023-03-15 11:36:54 +08:00
|
|
|
struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule);
|
2023-02-23 19:08:26 +08:00
|
|
|
ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_schema,
|
2023-03-29 22:25:14 +08:00
|
|
|
const char *table_name, const char *line, int valid_column)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime || NULL == fqdn_plugin_schema ||
|
|
|
|
|
NULL == line) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 19:08:26 +08:00
|
|
|
struct FQDN_rule *fqdn_plugin_rule = NULL;
|
2023-02-09 22:13:15 +08:00
|
|
|
struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema;
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
2023-03-16 16:03:33 +08:00
|
|
|
size_t item_id_offset = 0, item_id_len = 0;
|
2023-02-22 15:08:52 +08:00
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
int is_valid = get_column_value(line, valid_column);
|
|
|
|
|
if (is_valid < 0) {
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->update_err_cnt++;
|
2023-02-09 22:13:15 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 16:03:33 +08:00
|
|
|
int ret = get_column_pos(line, schema->item_id_column, &item_id_offset, &item_id_len);
|
|
|
|
|
if (ret < 0) {
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->update_err_cnt++;
|
2023-03-16 16:03:33 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
if (1 == schema->container_schema.set_flag) {
|
2023-02-09 22:13:15 +08:00
|
|
|
if (1 == is_valid) {
|
|
|
|
|
// add
|
2023-05-23 03:23:39 +00:00
|
|
|
fqdn_plugin_rule = fqdn_plugin_rule_new(line, schema, table_name,
|
|
|
|
|
fqdn_plugin_rt->logger);
|
2023-02-23 19:08:26 +08:00
|
|
|
if (NULL == fqdn_plugin_rule) {
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->update_err_cnt++;
|
2023-02-09 22:13:15 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-16 16:03:33 +08:00
|
|
|
const char *key = line + item_id_offset;
|
|
|
|
|
size_t key_len = item_id_len;
|
2023-04-05 21:09:19 +08:00
|
|
|
ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, table_name, line, key, key_len,
|
2023-05-23 03:23:39 +00:00
|
|
|
fqdn_plugin_rule, is_valid);
|
2023-02-09 22:13:15 +08:00
|
|
|
if (ret < 0) {
|
2023-02-23 19:08:26 +08:00
|
|
|
if (fqdn_plugin_rule != NULL) {
|
|
|
|
|
fqdn_plugin_rule_free(fqdn_plugin_rule);
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
fqdn_plugin_rt->update_err_cnt++;
|
2023-02-09 22:13:15 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//ex_schema not set
|
|
|
|
|
ex_data_runtime_cache_row_put(fqdn_plugin_rt->ex_data_rt, line);
|
|
|
|
|
fqdn_plugin_rt->rule_num = ex_data_runtime_cached_row_count(fqdn_plugin_rt->ex_data_rt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 22:25:14 +08:00
|
|
|
void garbage_fqdn_engine_free(void *fqdn_engine, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct FQDN_engine *engine = (struct FQDN_engine *)fqdn_engine;
|
|
|
|
|
FQDN_engine_free(engine);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 03:23:39 +00:00
|
|
|
int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name,
|
|
|
|
|
long long maat_rt_version)
|
2023-02-09 22:13:15 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
struct ex_data_runtime *ex_data_rt = fqdn_plugin_rt->ex_data_rt;
|
2023-03-15 11:36:54 +08:00
|
|
|
if (NULL == ex_data_rt) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int updating_flag = ex_data_runtime_is_updating(ex_data_rt);
|
|
|
|
|
if (0 == updating_flag) {
|
2023-02-09 22:13:15 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
ex_data_runtime_commit(ex_data_rt);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
struct FQDN_rule *rules = NULL;
|
|
|
|
|
struct ex_container **ex_container = NULL;
|
|
|
|
|
size_t rule_cnt = ex_data_runtime_list_ex_container(ex_data_rt, &ex_container);
|
|
|
|
|
if (rule_cnt > 0) {
|
|
|
|
|
rules = ALLOC(struct FQDN_rule, rule_cnt);
|
|
|
|
|
for (size_t i = 0; i < rule_cnt; i++) {
|
|
|
|
|
rules[i] = *(struct FQDN_rule *)ex_container[i]->custom_data;
|
|
|
|
|
assert(rules[i].user_tag == ex_container[i] || rules[i].user_tag == NULL);
|
|
|
|
|
rules[i].user_tag = ex_container[i];
|
|
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
struct FQDN_engine *new_fqdn_engine = NULL;
|
|
|
|
|
struct FQDN_engine *old_fqdn_engine = NULL;
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
if (rule_cnt > 0) {
|
|
|
|
|
new_fqdn_engine = FQDN_engine_new(rules, rule_cnt);
|
|
|
|
|
if (NULL == new_fqdn_engine) {
|
|
|
|
|
log_error(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
|
|
|
|
|
"[%s:%d] table[%s] rebuild FQDN engine failed when update %zu fqdn_plugin rules",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, rule_cnt);
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
old_fqdn_engine = fqdn_plugin_rt->engine;
|
|
|
|
|
fqdn_plugin_rt->engine = new_fqdn_engine;
|
2023-03-15 11:36:54 +08:00
|
|
|
if (old_fqdn_engine != NULL) {
|
2023-03-29 22:25:14 +08:00
|
|
|
maat_garbage_bagging(fqdn_plugin_rt->ref_garbage_bin, old_fqdn_engine, NULL,
|
|
|
|
|
garbage_fqdn_engine_free);
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
2023-04-13 14:56:35 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
fqdn_plugin_rt->rule_num = rule_cnt;
|
2023-04-13 14:56:35 +08:00
|
|
|
if (maat_rt_version != 0) {
|
|
|
|
|
fqdn_plugin_rt->version = maat_rt_version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info(fqdn_plugin_rt->logger, MODULE_FQDN_PLUGIN,
|
|
|
|
|
"table[%s] commit %zu fqdn_plugin rules and rebuild FQDN engine completed, version:%lld",
|
|
|
|
|
table_name, rule_cnt, fqdn_plugin_rt->version);
|
2023-02-09 22:13:15 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
if (rules != NULL) {
|
|
|
|
|
FREE(rules);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ex_container != NULL) {
|
|
|
|
|
FREE(ex_container);
|
|
|
|
|
}
|
2023-02-09 22:13:15 +08:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 19:20:05 +08:00
|
|
|
long long fqdn_plugin_runtime_rule_count(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
return fqdn_plugin_rt->rule_num;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
|
|
|
|
|
return fqdn_plugin_rt->ex_data_rt;
|
2023-02-20 11:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-05 21:09:19 +08:00
|
|
|
int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query_fqdn,
|
|
|
|
|
void **ex_data_array, size_t n_ex_data)
|
2023-02-20 11:43:43 +08:00
|
|
|
{
|
2023-04-05 21:09:19 +08:00
|
|
|
if (NULL == fqdn_plugin_runtime || NULL == query_fqdn ||
|
|
|
|
|
NULL == ex_data_array || 0 == n_ex_data) {
|
2023-02-20 11:43:43 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
2023-03-06 10:45:36 +08:00
|
|
|
if (0 == fqdn_plugin_rt->rule_num) {
|
|
|
|
|
return 0;
|
2023-02-20 11:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct FQDN_match results[n_ex_data];
|
|
|
|
|
|
2023-03-06 10:45:36 +08:00
|
|
|
assert(fqdn_plugin_rt->engine != NULL);
|
2023-02-20 11:43:43 +08:00
|
|
|
int n_result = FQDN_engine_search(fqdn_plugin_rt->engine, query_fqdn, strlen(query_fqdn), results, n_ex_data);
|
|
|
|
|
for (int i = 0; i < n_result; i++) {
|
2023-04-05 21:09:19 +08:00
|
|
|
ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt,
|
2023-03-15 11:36:54 +08:00
|
|
|
(struct ex_container *)results[i].user_tag);
|
2023-02-20 11:43:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return n_result;
|
2023-02-22 15:22:41 +08:00
|
|
|
}
|
2023-04-20 15:34:56 +08:00
|
|
|
|
|
|
|
|
void fqdn_plugin_runtime_perf_stat(void *fqdn_plugin_runtime, struct timespec *start,
|
|
|
|
|
struct timespec *end, int thread_id)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime || thread_id < 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
alignment_int64_array_add(fqdn_plugin_rt->scan_cnt, thread_id, 1);
|
|
|
|
|
|
|
|
|
|
if (start != NULL && end != NULL) {
|
|
|
|
|
long long consume_time = (end->tv_sec - start->tv_sec) * 1000000000 + (end->tv_nsec - start->tv_nsec);
|
|
|
|
|
alignment_int64_array_add(fqdn_plugin_rt->scan_cpu_time, thread_id, consume_time);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long fqdn_plugin_runtime_scan_count(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
long long sum = alignment_int64_array_sum(fqdn_plugin_rt->scan_cnt,
|
|
|
|
|
fqdn_plugin_rt->n_worker_thread);
|
|
|
|
|
alignment_int64_array_reset(fqdn_plugin_rt->scan_cnt, fqdn_plugin_rt->n_worker_thread);
|
|
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long fqdn_plugin_runtime_scan_cpu_time(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
long long sum = alignment_int64_array_sum(fqdn_plugin_rt->scan_cpu_time,
|
|
|
|
|
fqdn_plugin_rt->n_worker_thread);
|
|
|
|
|
alignment_int64_array_reset(fqdn_plugin_rt->scan_cpu_time, fqdn_plugin_rt->n_worker_thread);
|
|
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long fqdn_plugin_runtime_update_err_count(void *fqdn_plugin_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == fqdn_plugin_runtime) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fqdn_plugin_runtime *fqdn_plugin_rt = (struct fqdn_plugin_runtime *)fqdn_plugin_runtime;
|
|
|
|
|
return fqdn_plugin_rt->update_err_cnt;
|
|
|
|
|
}
|