add basic code without test case, just compile success

This commit is contained in:
root
2024-09-12 09:31:27 +00:00
parent 537c75887d
commit feb1576545
54 changed files with 1618 additions and 4796 deletions

View File

@@ -24,11 +24,7 @@
#define MODULE_IP module_name_str("maat.ip")
struct ip_schema {
int item_id_column;
int object_id_column;
int ip_column;
int table_id;
int port_column;
struct table_manager *ref_tbl_mgr;
};
@@ -78,7 +74,6 @@ void *ip_schema_new(cJSON *json, struct table_manager *tbl_mgr,
{
struct ip_schema *ip_schema = ALLOC(struct ip_schema, 1);
cJSON *custom_item = NULL;
cJSON *item = cJSON_GetObjectItem(json, "table_id");
if (item != NULL && item->type == cJSON_Number) {
ip_schema->table_id = item->valueint;
@@ -89,50 +84,6 @@ void *ip_schema_new(cJSON *json, struct table_manager *tbl_mgr,
goto error;
}
item = cJSON_GetObjectItem(json, "custom");
if (NULL == item || item->type != cJSON_Object) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> schema has no custom column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "item_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_schema->item_id_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> schema has no item_id column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "object_id");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_schema->object_id_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> schema has no object_id column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "ip");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_schema->ip_column = custom_item->valueint;
} else {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> schema has no ip column",
__FUNCTION__, __LINE__, table_name);
goto error;
}
custom_item = cJSON_GetObjectItem(item, "port");
if (custom_item != NULL && custom_item->type == cJSON_Number) {
ip_schema->port_column = custom_item->valueint;
} else {
ip_schema->port_column = 0;
}
ip_schema->ref_tbl_mgr = tbl_mgr;
return ip_schema;
error:
@@ -147,42 +98,32 @@ void ip_schema_free(void *ip_schema)
static struct ip_item *
ip_item_new(struct ip_schema *ip_schema, const char *table_name,
const char *line, struct log_handle *logger)
const cJSON *json, struct log_handle *logger, long long item_id)
{
size_t column_offset = 0;
size_t column_len = 0;
char ip_str[128] = {0};
struct ip_item *ip_item = ALLOC(struct ip_item, 1);
cJSON *tmp_obj = NULL;
int ret = 0;
int ret = get_column_pos(line, ip_schema->item_id_column, &column_offset,
&column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no item_id in line:%s",
__FUNCTION__, __LINE__, table_name, line);
goto error;
}
ip_item->item_id = atoll(line + column_offset);
ip_item->item_id = item_id;
ret = get_column_pos(line, ip_schema->object_id_column, &column_offset,
&column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP,
tmp_obj = cJSON_GetObjectItem(json, "object_id");
if (NULL == tmp_obj || tmp_obj->type != cJSON_String) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no object_id in line:%s",
__FUNCTION__, __LINE__, table_name, line);
__FUNCTION__, __LINE__, table_name, cJSON_Print(json));
goto error;
}
ip_item->object_id = atoll(line + column_offset);
ip_item->object_id = atoll(tmp_obj->valuestring);
ret = get_column_pos(line, ip_schema->ip_column, &column_offset,
&column_len);
if (ret < 0) {
tmp_obj = cJSON_GetObjectItem(json, "ip");
if (NULL == tmp_obj || tmp_obj->type != cJSON_String) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no ip1 in line:%s",
__FUNCTION__, __LINE__, table_name, line);
"[%s:%d] ip table:<%s> has no ip in line:%s",
__FUNCTION__, __LINE__, table_name, cJSON_Print(json));
goto error;
}
memcpy(ip_str, (line + column_offset), column_len);
memcpy(ip_str, tmp_obj->valuestring, strlen(tmp_obj->valuestring));
if (strchr(ip_str, ':') != NULL) {
ip_item->addr_type = IPV6;
@@ -195,7 +136,7 @@ ip_item_new(struct ip_schema *ip_schema, const char *table_name,
if (ret < 0) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> ip_format2range(ip4) failed in line:%s",
__FUNCTION__, __LINE__, table_name, line);
__FUNCTION__, __LINE__, table_name, cJSON_Print(json));
goto error;
}
} else {
@@ -204,23 +145,16 @@ ip_item_new(struct ip_schema *ip_schema, const char *table_name,
if (ret < 0) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> ip_format2range(ip6) failed in line:%s",
__FUNCTION__, __LINE__, table_name, line);
__FUNCTION__, __LINE__, table_name, cJSON_Print(json));
goto error;
}
}
if(ip_schema->port_column>0)
tmp_obj = cJSON_GetObjectItem(json, "port");
if(tmp_obj && tmp_obj->type == cJSON_String)
{
ret = get_column_pos(line, ip_schema->port_column, &column_offset,
&column_len);
if (ret < 0) {
log_fatal(logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no port in line:%s",
__FUNCTION__, __LINE__, table_name, line);
goto error;
}
char port_range[20] = {0};
memcpy(port_range, (line + column_offset), column_len);
memcpy(port_range, tmp_obj->valuestring, strlen(tmp_obj->valuestring));
//port range is port or port_start-port_end
if(strchr(port_range,'-')!=NULL){
@@ -338,11 +272,11 @@ static void ip_item_to_ip_rule(struct ip_item *item, struct ip_rule *rule)
}
static int ip_runtime_update_row(struct ip_runtime *ip_rt, char *key, size_t key_len,
struct ip_item *item, int is_valid)
struct ip_item *item, enum maat_operation op)
{
int ret = -1;
if (0 == is_valid) {
if (MAAT_OP_DEL == op) {
// delete
rcu_hash_del(ip_rt->item_hash, key, key_len);
} else {
@@ -361,7 +295,7 @@ static int ip_runtime_update_row(struct ip_runtime *ip_rt, char *key, size_t key
int ip_runtime_update(void *ip_runtime, void *ip_schema,
const char *table_name, const char *line,
int valid_column)
enum maat_operation op)
{
if (NULL == ip_runtime || NULL == ip_schema || NULL == line) {
return -1;
@@ -369,39 +303,40 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema,
struct ip_schema *schema = (struct ip_schema *)ip_schema;
struct ip_runtime *ip_rt = (struct ip_runtime *)ip_runtime;
long long item_id = get_column_value(line, schema->item_id_column);
if (item_id < 0) {
cJSON *json = NULL;
cJSON *tmp_obj = NULL;
json = cJSON_Parse(line);
if (NULL == json) {
log_fatal(ip_rt->logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no item_id(column seq:%d)"
" in table_line:%s", __FUNCTION__, __LINE__, table_name,
schema->item_id_column, line);
"[%s:%d] ip table:<%s> line is not a valid json string",
__FUNCTION__, __LINE__, table_name);
ip_rt->update_err_cnt++;
return -1;
goto ERROR;
}
int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) {
tmp_obj = cJSON_GetObjectItem(json, "item_id");
if (NULL == tmp_obj || tmp_obj->type != cJSON_String) {
log_fatal(ip_rt->logger, MODULE_IP,
"[%s:%d] ip table:<%s> has no is_valid(column seq:%d)"
" in table_line:%s", __FUNCTION__, __LINE__, table_name,
valid_column, line);
"[%s:%d] ip table:<%s> has no item_id in line:%s",
__FUNCTION__, __LINE__, table_name, cJSON_Print(json));
ip_rt->update_err_cnt++;
return -1;
goto ERROR;
}
long long item_id = atoll(tmp_obj->valuestring);
struct ip_item *ip_item = NULL;
if (1 == is_valid) {
if (MAAT_OP_ADD == op) {
//add
ip_item = ip_item_new(schema, table_name, line, ip_rt->logger);
ip_item = ip_item_new(schema, table_name, json, ip_rt->logger, item_id);
if (NULL == ip_item) {
ip_rt->update_err_cnt++;
return -1;
goto ERROR;
}
}
int ret = ip_runtime_update_row(ip_rt, (char *)&item_id, sizeof(long long),
ip_item, is_valid);
ip_item, op);
if (ret < 0) {
if (ip_item != NULL) {
ip_item_free(ip_item);
@@ -409,7 +344,13 @@ int ip_runtime_update(void *ip_runtime, void *ip_schema,
//don't return failed, ignore the case of adding duplicate keys
}
cJSON_Delete(json);
return 0;
ERROR:
if (json != NULL) {
cJSON_Delete(json);
}
return -1;
}
void garbage_ip_matcher_free(void *ip_matcher, void *arg)