[PATCH] maat_plugin_table_get_ex_data add key_len (23.06)

This commit is contained in:
刘文坛
2023-06-08 07:32:41 +00:00
parent 75e23c7d27
commit c5accc49f2
13 changed files with 463 additions and 143 deletions

View File

@@ -43,14 +43,16 @@ struct plugin_runtime {
enum plugin_key_type {
PLUGIN_KEY_TYPE_INVALID = 0,
PLUGIN_KEY_TYPE_POINTER,
PLUGIN_KEY_TYPE_INTEGER
PLUGIN_KEY_TYPE_INTEGER,
PLUGIN_KEY_TYPE_IP_ADDR
};
#define MAX_PLUGIN_PER_TABLE 32
struct plugin_schema {
enum plugin_key_type key_type;
int addr_type_column;
int key_len;
int key_column;
int addr_type_column;
int rule_tag_column;
int n_foreign;
int foreign_columns[MAX_FOREIGN_CLMN_NUM];
@@ -120,12 +122,31 @@ void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
schema->key_type = PLUGIN_KEY_TYPE_POINTER;
} else if (strcmp(custom_item->valuestring, "integer") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_INTEGER;
custom_item = cJSON_GetObjectItem(item, "key_len");
if (NULL == custom_item || custom_item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d]plugin table:<%s> schema integer key must"
" have key_len column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
schema->key_len = custom_item->valueint;
} else if (strcmp(custom_item->valuestring, "ip_addr") == 0) {
schema->key_type = PLUGIN_KEY_TYPE_IP_ADDR;
custom_item = cJSON_GetObjectItem(item, "addr_type");
if (NULL == custom_item || custom_item->type != cJSON_Number) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d]plugin table:<%s> schema ip_addr key must"
" have addr_type column", __FUNCTION__, __LINE__,
table_name);
goto error;
}
schema->addr_type_column = custom_item->valueint;
} else {
log_error(logger, MODULE_PLUGIN,
"[%s:%d]plugin table:<%s> schema key_type:%s is illegal, "
"just allow {pointer}, {integer}",
__FUNCTION__, __LINE__, table_name,
custom_item->valuestring);
"just allow {pointer}, {integer}, {ip_addr}",
__FUNCTION__, __LINE__, table_name, custom_item->valuestring);
goto error;
}
@@ -142,8 +163,7 @@ void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
MAX_FOREIGN_CLMN_NUM);
} else if (custom_item->type == cJSON_Array) {
schema->n_foreign = cJSON_GetArraySize(custom_item);
for (int i = 0; i < schema->n_foreign; i++)
{
for (int i = 0; i < schema->n_foreign; i++) {
cJSON *foreign_item = cJSON_GetArrayItem(custom_item, i);
assert(foreign_item->type == cJSON_Number);
schema->foreign_columns[i] = foreign_item->valueint;
@@ -197,7 +217,8 @@ int plugin_table_add_callback(void *plugin_schema, int table_id,
return 0;
}
void plugin_table_all_callback_start(struct plugin_schema *plugin_schema, int update_type)
void plugin_table_all_callback_start(struct plugin_schema *plugin_schema,
int update_type)
{
for (size_t i = 0; i < plugin_schema->cb_cnt; i++) {
if (plugin_schema->cb[i].start != NULL) {
@@ -241,8 +262,9 @@ int plugin_table_set_ex_container_schema(void *plugin_schema, int table_id,
if (1 == schema->container_schema.set_flag) {
log_error(schema->logger, MODULE_PLUGIN,
"[%s:%d] plugin table(table_id:%d) ex_container_schema has been set, can't set again",
__FUNCTION__, __LINE__, table_id);
"[%s:%d] plugin table(table_id:%d) ex_container_schema"
" has been set, can't set again", __FUNCTION__, __LINE__,
table_id);
return -1;
}
@@ -258,7 +280,8 @@ int plugin_table_set_ex_container_schema(void *plugin_schema, int table_id,
return 0;
}
struct ex_container_schema *plugin_table_get_ex_container_schema(void *plugin_schema)
struct ex_container_schema *
plugin_table_get_ex_container_schema(void *plugin_schema)
{
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
@@ -337,7 +360,8 @@ int plugin_runtime_update_row(struct plugin_runtime *plugin_rt,
size_t cb_count = plugin_schema->cb_cnt;
if (cb_count > 0) {
for (size_t i = 0; i < cb_count; i++) {
plugin_schema->cb[i].update(plugin_schema->table_id, row, plugin_schema->cb[i].u_para);
plugin_schema->cb[i].update(plugin_schema->table_id, row,
plugin_schema->cb[i].u_para);
}
}
@@ -356,11 +380,13 @@ int plugin_accept_tag_match(struct plugin_schema *schema, const char *table_name
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);
int ret = get_column_pos(line, schema->rule_tag_column, &column_offset,
&column_len);
if (ret < 0) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] table: <%s> has no rule_tag(column_seq:%d) in table_line:%s",
__FUNCTION__, __LINE__, table_name, schema->rule_tag_column, line);
"[%s:%d] table: <%s> has no rule_tag(column_seq:%d) "
"in table_line:%s", __FUNCTION__, __LINE__, table_name,
schema->rule_tag_column, line);
return TAG_MATCH_ERR;
}
@@ -388,6 +414,97 @@ int plugin_accept_tag_match(struct plugin_schema *schema, const char *table_name
return TAG_MATCH_MATCHED;
}
int plugin_table_line_get_key(struct plugin_schema *schema, const char *table_name,
const char *line, char *dst_key, size_t *dst_key_len,
struct log_handle *logger)
{
size_t key_offset = 0, key_len = 0;
int ret = get_column_pos(line, schema->key_column, &key_offset, &key_len);
if (ret < 0) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> has no key(column seq:%d)"
" in table_line:%s", __FUNCTION__, __LINE__, table_name,
schema->key_column, line);
return -1;
}
const char *common_key = line + key_offset;
if (schema->key_type == PLUGIN_KEY_TYPE_POINTER) {
memcpy(dst_key, common_key, key_len);
*dst_key_len = key_len;
} else if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) {
if (schema->key_len == sizeof(long long)) {
long long key_ll = atoll(common_key);
memcpy(dst_key, (char *)&key_ll, schema->key_len);
} else {
int key_int = atoi(common_key);
memcpy(dst_key, (char *)&key_int, schema->key_len);
}
*dst_key_len = schema->key_len;
} else if (schema->key_type == PLUGIN_KEY_TYPE_IP_ADDR) {
if (key_len >= INET6_ADDRSTRLEN) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> ip_key too long(illegal) in "
"table_line:%s", __FUNCTION__, __LINE__, table_name, line);
return -1;
}
size_t addr_type_offset = 0, addr_type_len = 0;
ret = get_column_pos(line, schema->addr_type_column, &addr_type_offset,
&addr_type_len);
if (ret < 0) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> has no addr_type(column seq:%d)"
" in table_line:%s", __FUNCTION__, __LINE__, table_name,
schema->addr_type_column, line);
return -1;
}
char ip_key[INET6_ADDRSTRLEN] = {0};
//snprintf() write at most (key_len+1) bytes (including the terminating null{'\0}) to ip_key.
snprintf(ip_key, key_len + 1, "%s", common_key);
int addr_type = atoi(line + addr_type_offset);
if (IPV4 == addr_type) {
uint32_t ipv4_addr;
ret = inet_pton(AF_INET, ip_key, &ipv4_addr);
if (ret <= 0) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> ipv4 key(column seq:%d)"
" illegal in table_line:%s", __FUNCTION__, __LINE__,
table_name, schema->key_column, line);
return -1;
}
memcpy(dst_key, (char *)&ipv4_addr, sizeof(ipv4_addr));
*dst_key_len = sizeof(ipv4_addr);
} else if (IPV6 == addr_type) {
uint8_t ipv6_addr[16];
ret = inet_pton(AF_INET6, ip_key, ipv6_addr);
if (ret <= 0) {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> ipv6 key(column seq:%d)"
" illegal in table_line:%s", __FUNCTION__, __LINE__,
table_name, schema->key_column, line);
return -1;
}
memcpy(dst_key, (char *)&ipv6_addr, sizeof(ipv6_addr));
*dst_key_len = sizeof(ipv6_addr);
} else {
log_error(logger, MODULE_PLUGIN,
"[%s:%d] plugin table:<%s> addr_type:%d illegal, just"
" allow{4, 6}, table_line:%s", __FUNCTION__, __LINE__,
table_name, addr_type, line);
return -1;
}
}
return 0;
}
int plugin_runtime_update(void *plugin_runtime, void *plugin_schema,
const char *table_name, const char *line,
int valid_column)
@@ -415,21 +532,15 @@ int plugin_runtime_update(void *plugin_runtime, void *plugin_schema,
return -1;
}
size_t key_offset = 0, key_len = 0;
ret = get_column_pos(line, schema->key_column, &key_offset, &key_len);
char key[MAX_KEYWORDS_STR] = {0};
size_t key_len = 0;
ret = plugin_table_line_get_key(schema, table_name, line, key, &key_len,
plugin_rt->logger);
if (ret < 0) {
plugin_rt->update_err_cnt++;
return -1;
}
long long key_int = 0;
const char *key = line + key_offset;
if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) {
key_int = atoll(key);
key = (char *)&key_int;
key_len = sizeof(long long);
}
ret = plugin_runtime_update_row(plugin_rt, schema, table_name, line,
key, key_len, is_valid);
if (ret < 0) {
@@ -539,10 +650,10 @@ void *plugin_runtime_get_ex_data(void *plugin_runtime, void *plugin_schema,
struct plugin_runtime *plugin_rt = (struct plugin_runtime *)plugin_runtime;
struct plugin_schema *schema = (struct plugin_schema *)plugin_schema;
if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) {
key_len = sizeof(long long);
if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER &&
schema->key_len != key_len) {
return NULL;
}
return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, key_len);
}