[FEATURE]ip_plugin support CIDR addr_format

This commit is contained in:
liuwentan
2024-03-15 14:50:50 +08:00
parent d3427c62f9
commit b1c629811d
4 changed files with 67 additions and 16 deletions

View File

@@ -26,6 +26,7 @@ struct ip_plugin_schema {
int ip_type_column;
int start_ip_column;
int end_ip_column;
int addr_format_column;
int rule_tag_column;
int gc_timeout_s;
int table_id;
@@ -117,6 +118,11 @@ void *ip_plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr,
goto error;
}
custom_item = cJSON_GetObjectItem(item, "addr_format");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
schema->addr_format_column = custom_item->valueint;
}
// rule_tag is optional
custom_item = cJSON_GetObjectItem(item, "rule_tag");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
@@ -202,6 +208,7 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
size_t column_len = 0;
char start_ip_str[40] = {0};
char end_ip_str[40] = {0};
char addr_format[16] = {"range"};
struct ip_rule *ip_plugin_rule = ALLOC(struct ip_rule, 1);
ret = get_column_pos(line, schema->item_id_column,
@@ -232,6 +239,28 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
goto error;
}
if (schema->addr_format_column > 0) {
ret = get_column_pos(line, schema->addr_format_column,
&column_offset, &column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table:<%s> has no addr_format column in line:%s",
__FUNCTION__, __LINE__, table_name, line);
goto error;
}
memset(addr_format, 0, sizeof(addr_format));
memcpy(addr_format, (line + column_offset), column_len);
}
if (IP_FORMAT_UNKNOWN == ip_format_str2int(addr_format)) {
log_fatal(logger, MODULE_IP_PLUGIN,
"[%s:%d] ip_plugin table(table_id:%d) has invalid addr_format,"
" should be range/CIDR, line:%s",
__FUNCTION__, __LINE__, schema->table_id, line);
goto error;
}
ret = get_column_pos(line, schema->start_ip_column,
&column_offset, &column_len);
if (ret < 0) {
@@ -253,7 +282,7 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
strncpy(end_ip_str, line + column_offset, column_len);
if (IPv4 == ip_plugin_rule->type) {
ret = ip_format2range(ip_plugin_rule->type, IP_FORMAT_RANGE,
ret = ip_format2range(ip_plugin_rule->type, ip_format_str2int(addr_format),
start_ip_str, end_ip_str,
&ip_plugin_rule->ipv4_rule.start_ip,
&ip_plugin_rule->ipv4_rule.end_ip);
@@ -266,7 +295,7 @@ ip_plugin_rule_new(struct ip_plugin_schema *schema, const char *table_name,
}
} else {
//ipv6
ret = ip_format2range(ip_plugin_rule->type, IP_FORMAT_RANGE,
ret = ip_format2range(ip_plugin_rule->type, ip_format_str2int(addr_format),
start_ip_str, end_ip_str,
ip_plugin_rule->ipv6_rule.start_ip,
ip_plugin_rule->ipv6_rule.end_ip);