diff --git a/scanner/adapter_hs/adapter_hs.cpp b/scanner/adapter_hs/adapter_hs.cpp index 4ad7b90..6920159 100644 --- a/scanner/adapter_hs/adapter_hs.cpp +++ b/scanner/adapter_hs/adapter_hs.cpp @@ -646,22 +646,25 @@ struct adapter_hs_stream *adapter_hs_stream_open(struct adapter_hs *hs_instance, utarray_new(hs_stream->pattern_id_set, &ut_pattern_id_icd); utarray_reserve(hs_stream->pattern_id_set, hs_stream->n_patterns); + int err_count = 0; if (hs_instance->hs_rt->literal_db != NULL) { err = hs_open_stream(hs_instance->hs_rt->literal_db, 0, &hs_stream->literal_stream); if (err != HS_SUCCESS) { - // log_error - return NULL; + err_count++; } } if (hs_instance->hs_rt->regex_db != NULL) { err = hs_open_stream(hs_instance->hs_rt->regex_db, 0, &hs_stream->regex_stream); if (err != HS_SUCCESS) { - // log_error - return NULL; + err_count++; } } + if (2 == err_count) { + return NULL; + } + return hs_stream; } @@ -675,13 +678,14 @@ int adapter_hs_scan_stream(struct adapter_hs_stream *hs_stream, const char *data return -1; } + int err_count = 0; int thread_id = hs_stream->thread_id; if (hs_stream->literal_stream != NULL) { err = hs_scan_stream(hs_stream->literal_stream, data, data_len, 0, hs_stream->hs_rt->scratchs[thread_id], matched_event_cb, hs_stream->pattern_id_set); if (err != HS_SUCCESS) { - return -1; + err_count++; } } @@ -690,10 +694,14 @@ int adapter_hs_scan_stream(struct adapter_hs_stream *hs_stream, const char *data 0, hs_stream->hs_rt->scratchs[thread_id], matched_event_cb, hs_stream->pattern_id_set); if (err != HS_SUCCESS) { - return -1; + err_count++; } } + if (2 == err_count) { + return -1; + } + size_t pattern_set_size = utarray_len(hs_stream->pattern_id_set); unsigned long long items[pattern_set_size]; memset(items, 0, sizeof(unsigned long long) * pattern_set_size); @@ -736,10 +744,19 @@ void adapter_hs_stream_close(struct adapter_hs_stream *hs_stream) int thread_id = hs_stream->thread_id; if (hs_stream->hs_rt != NULL) { - hs_close_stream(hs_stream->literal_stream, - hs_stream->hs_rt->scratchs[thread_id], NULL, NULL); - hs_close_stream(hs_stream->regex_stream, - hs_stream->hs_rt->scratchs[thread_id], NULL, NULL); + if (hs_stream->literal_stream != NULL) { + hs_close_stream(hs_stream->literal_stream, + hs_stream->hs_rt->scratchs[thread_id], + NULL, NULL); + hs_stream->literal_stream = NULL; + } + + if (hs_stream->regex_stream != NULL) { + hs_close_stream(hs_stream->regex_stream, + hs_stream->hs_rt->scratchs[thread_id], + NULL, NULL); + hs_stream->regex_stream = NULL; + } } utarray_free(hs_stream->pattern_id_set); diff --git a/src/inc_internal/maat_bool_plugin.h b/src/inc_internal/maat_bool_plugin.h index 9c8a6bb..dad2ce4 100644 --- a/src/inc_internal/maat_bool_plugin.h +++ b/src/inc_internal/maat_bool_plugin.h @@ -49,8 +49,9 @@ int bool_plugin_runtime_commit(void *bool_plugin_runtime, const char *table_name struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_runtime); -int bool_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, unsigned long long *item_ids, - size_t n_item, void **ex_data_array, size_t n_ex_data); +int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, void *bool_plugin_schema, + unsigned long long *item_ids, size_t n_item, + void **ex_data_array, size_t n_ex_data); #ifdef __cplusplus } diff --git a/src/inc_internal/maat_ex_data.h b/src/inc_internal/maat_ex_data.h index b64a0d2..22ac88a 100644 --- a/src/inc_internal/maat_ex_data.h +++ b/src/inc_internal/maat_ex_data.h @@ -64,9 +64,6 @@ struct ex_data_schema *ex_data_schema_new(maat_ex_new_func_t *new_func, long argl, void *argp); void ex_data_schema_free(struct ex_data_schema *ex_schema); -void ex_data_runtime_set_schema(struct ex_data_runtime *ex_data_rt, - struct ex_data_schema *schema); - /* set user_ctx API */ void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, struct ex_container_schema *container_schema); @@ -78,7 +75,8 @@ struct ex_container *ex_container_new(void *ex_data, void *custom_data); void ex_container_free(void *ctx, void *data); /* ex_data_runtime ex data API */ -void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, +void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, const char *row, const char *key, size_t key_len); @@ -96,10 +94,12 @@ size_t ex_data_runtime_ex_container_count(struct ex_data_runtime *ex_data_rt); int ex_data_runtime_is_updating(struct ex_data_runtime *ex_data_rt); -void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, +void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, const char *key, size_t key_len); void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, struct ex_container *ex_container); void *ex_data_runtime_get_custom_data(struct ex_data_runtime *ex_data_rt, diff --git a/src/inc_internal/maat_fqdn_plugin.h b/src/inc_internal/maat_fqdn_plugin.h index 1633bf4..637d7ae 100644 --- a/src/inc_internal/maat_fqdn_plugin.h +++ b/src/inc_internal/maat_fqdn_plugin.h @@ -49,8 +49,8 @@ int fqdn_plugin_runtime_commit(void *fqdn_plugin_runtime, const char *table_name struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_runtime); -int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *fqdn, - void **ex_data_array, size_t n_ex_data); +int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, void *fqdn_plugin_schema, + const char *fqdn, void **ex_data_array, size_t n_ex_data); #ifdef __cplusplus } #endif diff --git a/src/inc_internal/maat_ip_plugin.h b/src/inc_internal/maat_ip_plugin.h index a174a4d..2f49217 100644 --- a/src/inc_internal/maat_ip_plugin.h +++ b/src/inc_internal/maat_ip_plugin.h @@ -49,8 +49,9 @@ int ip_plugin_runtime_commit(void *ip_plugin_runtime, const char *table_name); struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime); -int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr, - void **ex_data_array, size_t n_ex_data_array); +int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schema, + const struct ip_addr *ip_addr, void **ex_data_array, + size_t n_ex_data_array); #ifdef __cplusplus } diff --git a/src/maat_api.c b/src/maat_api.c index 064babd..ecd7b34 100644 --- a/src/maat_api.c +++ b/src/maat_api.c @@ -601,34 +601,33 @@ int generic_plugin_table_set_ex_schema(struct table_manager *tbl_mgr, int table_ void generic_plugin_runtime_commit_ex_schema(void *runtime, void *schema, int table_id, enum table_type table_type, int valid_column) { - struct ex_data_schema *ex_data_schema = NULL; + struct ex_data_schema *ex_schema = NULL; struct ex_data_runtime *ex_data_rt = NULL; switch (table_type) { case TABLE_TYPE_PLUGIN: - ex_data_schema = plugin_table_get_ex_data_schema(schema); + ex_schema = plugin_table_get_ex_data_schema(schema); ex_data_rt = plugin_runtime_get_ex_data_rt(runtime); break; case TABLE_TYPE_IP_PLUGIN: - ex_data_schema = ip_plugin_table_get_ex_data_schema(schema); + ex_schema = ip_plugin_table_get_ex_data_schema(schema); ex_data_rt = ip_plugin_runtime_get_ex_data_rt(runtime); break; case TABLE_TYPE_FQDN_PLUGIN: - ex_data_schema = fqdn_plugin_table_get_ex_data_schema(schema); + ex_schema = fqdn_plugin_table_get_ex_data_schema(schema); ex_data_rt = fqdn_plugin_runtime_get_ex_data_rt(runtime); break; case TABLE_TYPE_BOOL_PLUGIN: - ex_data_schema = bool_plugin_table_get_ex_data_schema(schema); + ex_schema = bool_plugin_table_get_ex_data_schema(schema); ex_data_rt = bool_plugin_runtime_get_ex_data_rt(runtime); break; default: break; } - ex_data_runtime_set_schema(ex_data_rt, ex_data_schema); struct ex_container_schema *container_schema = ALLOC(struct ex_container_schema, 1); container_schema->table_id = table_id; - container_schema->ex_schema = ex_data_schema; + container_schema->ex_schema = ex_schema; ex_data_runtime_set_ex_container_schema(ex_data_rt, container_schema); size_t n_cached_row = ex_data_runtime_cached_row_count(ex_data_rt); @@ -809,7 +808,16 @@ int maat_ip_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, return -1; } - int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_addr, ex_data_array, n_ex_data); + void *ip_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); + if (NULL == ip_plugin_schema) { + log_error(maat_instance->logger, MODULE_MAAT_API, + "[%s:%d] table(table_id:%d) schema is NULL", + __FUNCTION__, __LINE__, table_id); + return -1; + } + + int n_hit_ex_data = ip_plugin_runtime_get_ex_data(ip_plugin_rt, ip_plugin_schema, ip_addr, + ex_data_array, n_ex_data); if (n_hit_ex_data < 0) { log_error(maat_instance->logger, MODULE_MAAT_API, "[%s:%d] ip_plugin table(table_id:%d) get ex_data error.", @@ -848,7 +856,16 @@ int maat_fqdn_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, return -1; } - int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn, ex_data_array, n_ex_data); + void *fqdn_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); + if (NULL == fqdn_plugin_schema) { + log_error(maat_instance->logger, MODULE_MAAT_API, + "[%s:%d] table(table_id:%d) schema is NULL", + __FUNCTION__, __LINE__, table_id); + return -1; + } + + int n_hit_ex_data = fqdn_plugin_runtime_get_ex_data(fqdn_plugin_rt, fqdn_plugin_schema, + fqdn, ex_data_array, n_ex_data); if (n_hit_ex_data < 0) { log_error(maat_instance->logger, MODULE_MAAT_API, "[%s:%d] fqdn_plugin table(table_id:%d) get ex_data error.", @@ -887,7 +904,17 @@ int maat_bool_plugin_table_get_ex_data(struct maat *maat_instance, int table_id, return -1; } - int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, item_ids, n_item, ex_data_array, n_ex_data); + void *bool_plugin_schema = table_manager_get_schema(maat_rt->ref_tbl_mgr, table_id); + if (NULL == bool_plugin_schema) { + log_error(maat_instance->logger, MODULE_MAAT_API, + "[%s:%d] table(table_id:%d) schema is NULL", + __FUNCTION__, __LINE__, table_id); + return -1; + } + + int n_hit_ex_data = bool_plugin_runtime_get_ex_data(bool_plugin_rt, bool_plugin_schema, + item_ids, n_item, ex_data_array, + n_ex_data); if (n_hit_ex_data < 0) { log_error(maat_instance->logger, MODULE_MAAT_API, "[%s:%d] bool_plugin table(table_id:%d) get ex_data error.", diff --git a/src/maat_bool_plugin.c b/src/maat_bool_plugin.c index c6ae600..e0b204c 100644 --- a/src/maat_bool_plugin.c +++ b/src/maat_bool_plugin.c @@ -212,6 +212,7 @@ void bool_plugin_runtime_free(void *bool_plugin_runtime) } int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt, + struct ex_data_schema *ex_schema, const char *row, char *key, size_t key_len, struct bool_expr *expr, int is_valid) { @@ -226,7 +227,7 @@ int bool_plugin_runtime_update_row(struct bool_plugin_runtime *bool_plugin_rt, } } else { // add - void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row, key, key_len); + void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row, key, key_len); struct ex_container *ex_container = ex_container_new(ex_data, (void *)expr); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { @@ -369,7 +370,8 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche return -1; } - if (schema->ex_schema != NULL) { + struct ex_data_schema *ex_schema = schema->ex_schema; + if (ex_schema != NULL) { if (1 == is_valid) { // add bool_expr = bool_plugin_expr_new(line, schema, bool_plugin_rt->logger); @@ -379,7 +381,7 @@ int bool_plugin_runtime_update(void *bool_plugin_runtime, void *bool_plugin_sche } char *key = (char *)&item_id; - int ret = bool_plugin_runtime_update_row(bool_plugin_rt, line, key, + int ret = bool_plugin_runtime_update_row(bool_plugin_rt, ex_schema, line, key, sizeof(long long), bool_expr, is_valid); if (ret < 0) { if (bool_expr != NULL) { @@ -475,10 +477,17 @@ struct ex_data_runtime *bool_plugin_runtime_get_ex_data_rt(void *bool_plugin_run return bool_plugin_rt->ex_data_rt; } -int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long long *item_ids, - size_t n_item, void **ex_data_array, size_t n_ex_data) +int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, void *bool_plugin_schema, + unsigned long long *item_ids, size_t n_item, + void **ex_data_array, size_t n_ex_data) { - if (NULL == bool_plugin_runtime) { + if (NULL == bool_plugin_runtime || NULL == bool_plugin_schema) { + return -1; + } + + struct bool_plugin_schema *schema = (struct bool_plugin_schema *)bool_plugin_schema; + struct ex_data_schema *ex_schema = schema->ex_schema; + if (NULL == ex_schema) { return -1; } @@ -494,7 +503,7 @@ int bool_plugin_runtime_get_ex_data(void *bool_plugin_runtime, unsigned long lon assert(bool_plugin_rt->matcher != NULL); int n_result = bool_matcher_match(bool_plugin_rt->matcher, item_ids, n_item, results, n_ex_data); for (int i = 0; i < n_result; i++) { - ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt, + ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(bool_plugin_rt->ex_data_rt, ex_schema, (struct ex_container *)results[i].user_tag); } diff --git a/src/maat_ex_data.c b/src/maat_ex_data.c index 7f9c122..1314080 100644 --- a/src/maat_ex_data.c +++ b/src/maat_ex_data.c @@ -25,7 +25,6 @@ struct ex_data_runtime { size_t cache_size; struct rcu_hash_table *htable; - struct ex_data_schema *ref_ex_schema; struct maat_garbage_bin *ref_garbage_bin; int table_id; @@ -158,16 +157,6 @@ void ex_data_schema_free(struct ex_data_schema *ex_schema) } -void ex_data_runtime_set_schema(struct ex_data_runtime *ex_data_rt, - struct ex_data_schema *schema) -{ - if (NULL == ex_data_rt) { - return; - } - - ex_data_rt->ref_ex_schema = schema; -} - void ex_data_runtime_set_ex_container_schema(struct ex_data_runtime *ex_data_rt, struct ex_container_schema *container_schema) { @@ -188,14 +177,15 @@ ex_data_runtime_get_ex_container_schema(struct ex_data_runtime *ex_data_rt) return (struct ex_container_schema *)rcu_hash_get_user_ctx(ex_data_rt->htable); } -void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, const char *row, - const char *key, size_t key_len) +void *ex_data_runtime_row2ex_data(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, + const char *row, const char *key, + size_t key_len) { void *ex_data = NULL; - struct ex_data_schema *ex_schema = ex_data_rt->ref_ex_schema; + ex_schema->new_func(ex_data_rt->table_id, key, row, &ex_data, ex_schema->argl, ex_schema->argp); - return ex_data; } @@ -252,7 +242,8 @@ int ex_data_runtime_del_ex_container(struct ex_data_runtime *ex_data_rt, return rcu_hash_del(ex_data_rt->htable, key, key_len); } -void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, +void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, const char *key, size_t key_len) { struct ex_container *ex_container = NULL; @@ -264,21 +255,18 @@ void *ex_data_runtime_get_ex_data_by_key(struct ex_data_runtime *ex_data_rt, } void *dup_ex_data = NULL; - ex_data_rt->ref_ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, - &(ex_container->ex_data), - ex_data_rt->ref_ex_schema->argl, - ex_data_rt->ref_ex_schema->argp); + ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data), + ex_schema->argl, ex_schema->argp); return dup_ex_data; } -void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt, +void *ex_data_runtime_get_ex_data_by_container(struct ex_data_runtime *ex_data_rt, + struct ex_data_schema *ex_schema, struct ex_container *ex_container) { void *dup_ex_data = NULL; - ex_data_rt->ref_ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, - &(ex_container->ex_data), - ex_data_rt->ref_ex_schema->argl, - ex_data_rt->ref_ex_schema->argp); + ex_schema->dup_func(ex_data_rt->table_id, &dup_ex_data, &(ex_container->ex_data), + ex_schema->argl, ex_schema->argp); return dup_ex_data; } diff --git a/src/maat_fqdn_plugin.c b/src/maat_fqdn_plugin.c index 1d81b3f..3199277 100644 --- a/src/maat_fqdn_plugin.c +++ b/src/maat_fqdn_plugin.c @@ -304,7 +304,8 @@ void fqdn_plugin_rule_free(struct FQDN_rule *rule) } int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt, - const char *row, const char *key, size_t key_len, + struct ex_data_schema *ex_schema, const char *row, + const char *key, size_t key_len, struct FQDN_rule *fqdn_plugin_rule, int is_valid) { int ret = -1; @@ -318,7 +319,7 @@ int fqdn_plugin_runtime_update_row(struct fqdn_plugin_runtime *fqdn_plugin_rt, } } else { // add - void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row, key, key_len); + void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row, key, key_len); struct ex_container *ex_container = ex_container_new(ex_data, (void *)fqdn_plugin_rule); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { @@ -350,7 +351,8 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche return -1; } - if (schema->ex_schema != NULL) { + struct ex_data_schema *ex_schema = schema->ex_schema; + if (ex_schema != NULL) { if (1 == is_valid) { // add fqdn_plugin_rule = fqdn_plugin_rule_new(line, schema, fqdn_plugin_rt->logger); @@ -360,19 +362,13 @@ int fqdn_plugin_runtime_update(void *fqdn_plugin_runtime, void *fqdn_plugin_sche } char *key = (char *)&item_id; - int ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, line, key, sizeof(long long), + int ret = fqdn_plugin_runtime_update_row(fqdn_plugin_rt, ex_schema, line, key, sizeof(long long), fqdn_plugin_rule, is_valid); if (ret < 0) { if (fqdn_plugin_rule != NULL) { fqdn_plugin_rule_free(fqdn_plugin_rule); } return -1; - } else { - if (0 == is_valid) { - fqdn_plugin_rt->rule_num--; - } else { - fqdn_plugin_rt->rule_num++; - } } } else { //ex_schema not set @@ -462,10 +458,17 @@ struct ex_data_runtime *fqdn_plugin_runtime_get_ex_data_rt(void *fqdn_plugin_run } -int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query_fqdn, - void **ex_data_array, size_t n_ex_data) +int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, void *fqdn_plugin_schema, + const char *query_fqdn, void **ex_data_array, + size_t n_ex_data) { - if (NULL == fqdn_plugin_runtime) { + if (NULL == fqdn_plugin_runtime || NULL == fqdn_plugin_schema) { + return -1; + } + + struct fqdn_plugin_schema *schema = (struct fqdn_plugin_schema *)fqdn_plugin_schema; + struct ex_data_schema *ex_schema = schema->ex_schema; + if (NULL == ex_schema) { return -1; } @@ -480,7 +483,7 @@ int fqdn_plugin_runtime_get_ex_data(void *fqdn_plugin_runtime, const char *query assert(fqdn_plugin_rt->engine != NULL); 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++) { - ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt, + ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(fqdn_plugin_rt->ex_data_rt, ex_schema, (struct ex_container *)results[i].user_tag); } diff --git a/src/maat_ip_plugin.c b/src/maat_ip_plugin.c index 35ef420..795a203 100644 --- a/src/maat_ip_plugin.c +++ b/src/maat_ip_plugin.c @@ -311,8 +311,9 @@ void ip_plugin_table_set_ex_data_schema(void *ip_plugin_schema, } int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt, - const char *row, char *key, size_t key_len, - struct ip_rule *ip_plugin_rule, int is_valid) + struct ex_data_schema *ex_schema, + const char *row, char *key, size_t key_len, + struct ip_rule *ip_plugin_rule, int is_valid) { int ret = -1; struct ex_data_runtime *ex_data_rt = ip_plugin_rt->ex_data_rt; @@ -328,7 +329,7 @@ int ip_plugin_runtime_update_row(struct ip_plugin_runtime *ip_plugin_rt, } } else { // add - void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row, key, key_len); + void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row, key, key_len); struct ex_container *ex_container = ex_container_new(ex_data, (void *)ip_plugin_rule); ret = ex_data_runtime_add_ex_container(ex_data_rt, key, key_len, ex_container); if (ret < 0) { @@ -399,7 +400,8 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema, return -1; } - if (schema->ex_schema != NULL) { + struct ex_data_schema *ex_schema = schema->ex_schema; + if (ex_schema != NULL) { if (1 == is_valid) { // add ip_plugin_rule = ip_plugin_rule_new(line, schema, ip_plugin_rt->logger); @@ -409,8 +411,9 @@ int ip_plugin_runtime_update(void *ip_plugin_runtime, void *ip_plugin_schema, } char *key = (char *)&item_id; - int ret = ip_plugin_runtime_update_row(ip_plugin_rt, line, key, sizeof(long long), - ip_plugin_rule, is_valid); + int ret = ip_plugin_runtime_update_row(ip_plugin_rt, ex_schema, line, key, + sizeof(long long), ip_plugin_rule, + is_valid); if (ret < 0) { if (ip_plugin_rule != NULL) { ip_plugin_rule_free(ip_plugin_rule); @@ -505,10 +508,17 @@ struct ex_data_runtime *ip_plugin_runtime_get_ex_data_rt(void *ip_plugin_runtime return ip_plugin_rt->ex_data_rt; } -int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr *ip_addr, - void **ex_data_array, size_t n_ex_data) +int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, void *ip_plugin_schema, + const struct ip_addr *ip_addr, void **ex_data_array, + size_t n_ex_data) { - if (NULL == ip_plugin_runtime) { + if (NULL == ip_plugin_runtime || NULL == ip_plugin_schema) { + return -1; + } + + struct ip_plugin_schema *schema = (struct ip_plugin_schema *)ip_plugin_schema; + struct ex_data_schema *ex_schema = schema->ex_schema; + if (NULL == ex_schema) { return -1; } @@ -530,7 +540,7 @@ int ip_plugin_runtime_get_ex_data(void *ip_plugin_runtime, const struct ip_addr assert(ip_plugin_rt->ip_matcher != NULL); int n_result = ip_matcher_match(ip_plugin_rt->ip_matcher, &ip_data, results, n_ex_data); for (int i = 0; i < n_result; i++) { - ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt, + ex_data_array[i] = ex_data_runtime_get_ex_data_by_container(ip_plugin_rt->ex_data_rt, ex_schema, (struct ex_container *)results[i].tag); } return n_result; diff --git a/src/maat_plugin.c b/src/maat_plugin.c index 125aa06..ccb4251 100644 --- a/src/maat_plugin.c +++ b/src/maat_plugin.c @@ -9,6 +9,8 @@ */ #include +#include +#include #include "log/log.h" #include "maat_utils.h" @@ -20,6 +22,12 @@ #define MODULE_PLUGIN module_name_str("maat.plugin") +enum plugin_key_type { + PLUGIN_KEY_TYPE_INVALID = 0, + PLUGIN_KEY_TYPE_POINTER, + PLUGIN_KEY_TYPE_INTEGER +}; + struct plugin_callback_schema { maat_start_callback_t *start; maat_update_callback_t *update; @@ -40,6 +48,7 @@ struct plugin_runtime { #define MAX_PLUGIN_PER_TABLE 32 struct plugin_schema { int key_column; + enum plugin_key_type key_type; int rule_tag_column; int n_foreign; int foreign_columns[MAX_FOREIGN_CLMN_NUM]; @@ -102,6 +111,26 @@ void *plugin_schema_new(cJSON *json, struct table_manager *tbl_mgr, } schema->key_column = custom_item->valueint; + custom_item = cJSON_GetObjectItem(item, "key_type"); + if (NULL == custom_item || custom_item->type != cJSON_String) { + log_error(logger, MODULE_PLUGIN, + "[%s:%d] plugin table: %s has no key_type column", + __FUNCTION__, __LINE__, table_name); + goto error; + } + + if (strcmp(custom_item->valuestring, "pointer") == 0) { + schema->key_type = PLUGIN_KEY_TYPE_POINTER; + } else if (strcmp(custom_item->valuestring, "integer") == 0) { + schema->key_type = PLUGIN_KEY_TYPE_INTEGER; + } else { + log_error(logger, MODULE_PLUGIN, + "[%s:%d] plugin table: %s key_type:%s illegal", + __FUNCTION__, __LINE__, table_name, + custom_item->valuestring); + goto error; + } + custom_item = cJSON_GetObjectItem(item, "tag"); if (custom_item != NULL && custom_item->type == cJSON_Number) { schema->rule_tag_column = custom_item->valueint; @@ -281,7 +310,7 @@ int plugin_runtime_update_row(struct plugin_runtime *plugin_rt, } } else { // add - void *ex_data = ex_data_runtime_row2ex_data(plugin_rt->ex_data_rt, row, key, key_len); + void *ex_data = ex_data_runtime_row2ex_data(plugin_rt->ex_data_rt, ex_schema, row, key, key_len); struct ex_container *ex_container = ex_container_new(ex_data, NULL); ret = ex_data_runtime_add_ex_container(plugin_rt->ex_data_rt, key, key_len, ex_container); if (ret < 0) { @@ -452,9 +481,18 @@ 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 (NULL == schema->ex_schema) { + struct ex_data_schema *ex_schema = schema->ex_schema; + if (NULL == ex_schema) { return NULL; } - return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, key, key_len); + if (schema->key_type == PLUGIN_KEY_TYPE_INTEGER) { + char key_str[MAX_KEYWORDS_STR] = {0}; + long long key_int = *(long long *)key; + sprintf(key_str, "%lld", key_int); + key_len = strlen(key_str); + return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, ex_schema, key_str, key_len); + } else { + return ex_data_runtime_get_ex_data_by_key(plugin_rt->ex_data_rt, ex_schema, key, key_len); + } } \ No newline at end of file diff --git a/test/maat_ex_data_gtest.cpp b/test/maat_ex_data_gtest.cpp index f24e28d..33f28e8 100644 --- a/test/maat_ex_data_gtest.cpp +++ b/test/maat_ex_data_gtest.cpp @@ -60,12 +60,11 @@ TEST(EXDataRuntime, Update) { struct ex_data_runtime *ex_data_rt = ex_data_runtime_new(table_id, ex_container_free, garbage_bin, g_logger); struct ex_data_schema *ex_schema = ex_data_schema_new(ex_data_new_cb, ex_data_free_cb, ex_data_dup_cb, 0, &ex_data_counter); - ex_data_runtime_set_schema(ex_data_rt, ex_schema); const char *row1 = "1\t192.168.0.1\tmahuateng\t1\t0"; const char *key1 = "192.168.0.1"; size_t key1_len = strlen(key1); - void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row1, key1, key1_len); + void *ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row1, key1, key1_len); EXPECT_EQ(ex_data_counter, 1); struct ex_container *ex_container = ex_container_new(ex_data, NULL); @@ -75,21 +74,21 @@ TEST(EXDataRuntime, Update) { const char *row2 = "2\t192.168.0.2\tliyanhong\t1\t0"; const char *key2 = "192.168.0.2"; size_t key2_len = strlen(key2); - ex_data = ex_data_runtime_row2ex_data(ex_data_rt, row2, key2, key2_len); + ex_data = ex_data_runtime_row2ex_data(ex_data_rt, ex_schema, row2, key2, key2_len); ex_container = ex_container_new(ex_data, NULL); ret = ex_data_runtime_add_ex_container(ex_data_rt, key2, key2_len, ex_container); EXPECT_EQ(ret, 0); ex_data_runtime_commit(ex_data_rt); - void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.1", 11); + void *res_data1 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.1", 11); EXPECT_TRUE(res_data1 != NULL); struct user_info *info = (struct user_info *)res_data1; EXPECT_EQ(0, strcmp(info->name, "mahuateng")); EXPECT_EQ(info->id, 1); - void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, "192.168.0.2", 11); + void *res_data2 = ex_data_runtime_get_ex_data_by_key(ex_data_rt, ex_schema, "192.168.0.2", 11); EXPECT_TRUE(res_data2 != NULL); info = (struct user_info *)res_data2; diff --git a/test/maat_framework_gtest.cpp b/test/maat_framework_gtest.cpp index d475d66..e379c6b 100644 --- a/test/maat_framework_gtest.cpp +++ b/test/maat_framework_gtest.cpp @@ -1414,6 +1414,7 @@ protected: struct maat_options *opts = maat_options_new(); maat_options_set_redis(opts, redis_ip, redis_port, redis_db); + maat_options_set_deferred_load_on(opts); maat_options_set_logger(opts, g_logger); maat_options_set_accept_tags(opts, accept_tags); @@ -1449,6 +1450,107 @@ TEST_F(PluginTable, Callback) { EXPECT_EQ(ret, 0); } +struct plugin_ud { + char key[256]; + char value[256]; + int id; + int ref_cnt; +}; + +void plugin_EX_new_cb(int table_id, const char *key, const char *table_line, + void **ad, long argl, void *argp) +{ + int *counter = (int *)argp; + int valid = 0, tag = 0; + struct plugin_ud *ud = ALLOC(struct plugin_ud, 1); + + int ret = sscanf(table_line, "%d\t%s\t%s\t%d\t%d", &(ud->id), ud->key, ud->value, &valid, &tag); + EXPECT_EQ(ret, 5); + ud->ref_cnt = 1; + *ad = ud; + (*counter)++; +} + +void plugin_EX_free_cb(int table_id, void **ad, long argl, void *argp) +{ + struct plugin_ud *ud = (struct plugin_ud *)(*ad); + if ((__sync_sub_and_fetch(&ud->ref_cnt, 1) == 0)) { + free(ud); + *ad = NULL; + } +} + +void plugin_EX_dup_cb(int table_id, void **to, void **from, long argl, void *argp) +{ + struct plugin_ud *ud = (struct plugin_ud *)(*from); + __sync_add_and_fetch(&(ud->ref_cnt), 1); + *to = ud; +} + +TEST_F(PluginTable, EX_DATA) { + const char *table_name = "TEST_PLUGIN_EXDATA_TABLE"; + struct maat *maat_instance = PluginTable::_shared_maat_instance; + + int table_id = maat_get_table_id(maat_instance, table_name); + ASSERT_GT(table_id, 0); + + int plugin_ex_data_counter = 0; + int ret = maat_plugin_table_ex_schema_register(maat_instance, table_id, + plugin_EX_new_cb, + plugin_EX_free_cb, + plugin_EX_dup_cb, + 0, &plugin_ex_data_counter); + EXPECT_EQ(ret, 0); + EXPECT_EQ(plugin_ex_data_counter, 4); + + const char *key1 = "HeBei"; + struct plugin_ud *ud = NULL; + ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, key1); + ASSERT_TRUE(ud != NULL); + EXPECT_STREQ(ud->value, "Shijiazhuang"); + EXPECT_EQ(ud->id, 1); + plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL); + + const char *key2 = "ShanDong"; + ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, key2); + ASSERT_TRUE(ud != NULL); + EXPECT_STREQ(ud->value, "Jinan"); + EXPECT_EQ(ud->id, 3); + plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL); +} + +TEST_F(PluginTable, KEY_TYPE) { + const char *table_name = "TEST_PLUGIN_KEY_TYPE_TABLE"; + struct maat *maat_instance = PluginTable::_shared_maat_instance; + + int table_id = maat_get_table_id(maat_instance, table_name); + ASSERT_GT(table_id, 0); + + int plugin_ex_data_counter = 0; + int ret = maat_plugin_table_ex_schema_register(maat_instance, table_id, + plugin_EX_new_cb, + plugin_EX_free_cb, + plugin_EX_dup_cb, + 0, &plugin_ex_data_counter); + EXPECT_EQ(ret, 0); + EXPECT_EQ(plugin_ex_data_counter, 4); + + long long key1 = 11111111; + struct plugin_ud *ud = NULL; + ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, (char *)&key1); + ASSERT_TRUE(ud != NULL); + EXPECT_STREQ(ud->value, "Shijiazhuang"); + EXPECT_EQ(ud->id, 1); + plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL); + + long long key2 = 33333333; + ud = (struct plugin_ud *)maat_plugin_table_get_ex_data(maat_instance, table_id, (char *)&key2); + ASSERT_TRUE(ud != NULL); + EXPECT_STREQ(ud->value, "Jinan"); + EXPECT_EQ(ud->id, 3); + plugin_EX_free_cb(table_id, (void **)&ud, 0, NULL); +} + class IPPluginTable : public testing::Test { protected: @@ -2380,6 +2482,7 @@ protected: g_logger = log_handle_create("./maat_framework_gtest.log", 0); assert(g_logger != NULL); + maat_options_set_deferred_load_on(opts); maat_options_set_logger(opts, g_logger); _shared_maat_instance = maat_new(opts, table_info_path); maat_options_free(opts); diff --git a/test/maat_json.json b/test/maat_json.json index e1cdcd9..4b8af6e 100644 --- a/test/maat_json.json +++ b/test/maat_json.json @@ -2441,6 +2441,24 @@ "3\tcccc\t11\t1" ] }, + { + "table_name": "TEST_PLUGIN_EXDATA_TABLE", + "table_content": [ + "1\tHeBei\tShijiazhuang\t1\t0", + "2\tHeNan\tZhengzhou\t1\t0", + "3\tShanDong\tJinan\t1\t0", + "4\tShanXi\tTaiyuan\t1\t0" + ] + }, + { + "table_name": "TEST_PLUGIN_KEY_TYPE_TABLE", + "table_content": [ + "1\t11111111\tShijiazhuang\t1\t0", + "2\t22222222\tZhengzhou\t1\t0", + "3\t33333333\tJinan\t1\t0", + "4\t44444444\tTaiyuan\t1\t0" + ] + }, { "table_name": "TEST_EFFECTIVE_RANGE_TABLE", "table_content": [ diff --git a/test/table_info.conf b/test/table_info.conf index 7ecbaee..6337cf6 100644 --- a/test/table_info.conf +++ b/test/table_info.conf @@ -148,6 +148,7 @@ "valid_column":4, "custom": { "key":1, + "key_type":"pointer", "tag":3 } }, @@ -191,6 +192,7 @@ "valid_column":4, "custom": { "key":1, + "key_type":"integer", "tag":5 } }, @@ -201,6 +203,7 @@ "valid_column":4, "custom": { "key":2, + "key_type":"pointer", "tag":3, "foreign": [6,8] } @@ -212,8 +215,8 @@ "valid_column":4, "custom": { "key":2, - "tag":5, - "estimate_size": 1024 + "key_type":"pointer", + "tag":5 } }, { @@ -223,6 +226,7 @@ "valid_column":14, "custom": { "key":2, + "key_type":"pointer", "tag":18 } }, @@ -524,5 +528,16 @@ "source_port":"VIRTUAL_PORT_SOURCE", "dest_port":"VIRTUAL_PORT_DESTINATION" } + }, + { + "table_id":45, + "table_name":"TEST_PLUGIN_KEY_TYPE_TABLE", + "table_type":"plugin", + "valid_column":4, + "custom": { + "key":2, + "key_type":"integer", + "tag":5 + } } ] \ No newline at end of file