diff options
-rw-r--r-- | src/lib/ndpi_main.c | 42 | ||||
-rw-r--r-- | src/lib/third_party/include/actypes.h | 1 | ||||
-rw-r--r-- | src/lib/third_party/include/ahocorasick.h | 2 | ||||
-rw-r--r-- | src/lib/third_party/include/node.h | 4 | ||||
-rw-r--r-- | src/lib/third_party/src/ahocorasick.c | 9 | ||||
-rw-r--r-- | src/lib/third_party/src/node.c | 12 |
6 files changed, 37 insertions, 33 deletions
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index a951c225d..7dc90b460 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -447,42 +447,36 @@ static int ndpi_string_to_automa(struct ndpi_detection_module_struct *ndpi_str, else ac_pattern.length = strlen(ac_pattern.astring); - ac_automata_add(((AC_AUTOMATA_t*)automa->ac_automa), &ac_pattern); + if(ac_automata_add(((AC_AUTOMATA_t*)automa->ac_automa), &ac_pattern) != ACERR_SUCCESS) + return(-2); return(0); } /* ****************************************************** */ -/* - TODO - This function should free the memory during program termination - - */ -static void ndpi_free_memory_at_termination(struct ndpi_detection_module_struct *ndpi_str, char *buf) { - - -} - -/* ****************************************************** */ - static int ndpi_add_host_url_subprotocol(struct ndpi_detection_module_struct *ndpi_str, char *_value, int protocol_id, ndpi_protocol_category_t category, ndpi_protocol_breed_t breed) { + int rv; char *value = ndpi_strdup(_value); - if(!value) return(-1); else ndpi_free_memory_at_termination(ndpi_str, value); + if(!value) return(-1); #ifdef DEBUG NDPI_LOG_DEBUG2(ndpi_str, "[NDPI] Adding [%s][%d]\n", value, protocol_id); #endif - return(ndpi_string_to_automa(ndpi_str, + rv = ndpi_string_to_automa(ndpi_str, &ndpi_str->host_automa, value, protocol_id, - category, breed)); + category, breed); + + if(rv != 0) ndpi_free(value); + + return(rv); } /* ****************************************************** */ @@ -2207,7 +2201,7 @@ int ndpi_add_string_to_automa(void *_automa, char *str) { return(ndpi_add_string_value_to_automa(_automa, str, 1)); } -void ndpi_free_automa(void *_automa) { ac_automata_release((AC_AUTOMATA_t*)_automa); } +void ndpi_free_automa(void *_automa) { ac_automata_release((AC_AUTOMATA_t*)_automa, 0); } void ndpi_finalize_automa(void *_automa) { ac_automata_finalize((AC_AUTOMATA_t*)_automa); } /* ****************************************************** */ @@ -2385,16 +2379,16 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) { ndpi_tdestroy(ndpi_str->tcpRoot, ndpi_free); if(ndpi_str->host_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->host_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->host_automa.ac_automa, 1 /* free patterns strings memory */); if(ndpi_str->content_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->content_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->content_automa.ac_automa, 0); if(ndpi_str->bigrams_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->bigrams_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->bigrams_automa.ac_automa, 0); if(ndpi_str->impossible_bigrams_automa.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->impossible_bigrams_automa.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->impossible_bigrams_automa.ac_automa, 0); #ifdef HAVE_HYPERSCAN destroy_hyperscan(ndpi_str); @@ -2410,10 +2404,10 @@ void ndpi_exit_detection_module(struct ndpi_detection_module_struct *ndpi_str) { free_hyperscan_memory(ndpi_str->custom_categories.hostnames); #else if(ndpi_str->custom_categories.hostnames.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 0); if(ndpi_str->custom_categories.hostnames_shadow.ac_automa != NULL) - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa, 0); #endif if(ndpi_str->custom_categories.ipAddresses != NULL) @@ -4382,7 +4376,7 @@ int ndpi_enable_loaded_categories(struct ndpi_detection_module_struct *ndpi_str) } #else /* Free */ - ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa); + ac_automata_release((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames.ac_automa, 0); /* Finalize */ ac_automata_finalize((AC_AUTOMATA_t*)ndpi_str->custom_categories.hostnames_shadow.ac_automa); diff --git a/src/lib/third_party/include/actypes.h b/src/lib/third_party/include/actypes.h index 5a94b63a1..2308cd686 100644 --- a/src/lib/third_party/include/actypes.h +++ b/src/lib/third_party/include/actypes.h @@ -63,6 +63,7 @@ typedef struct { AC_ALPHABET_t * astring; /* String of alphabets */ unsigned int length; /* Length of pattern */ + u_int8_t is_existing; /* 1 if the node is already part of another AC_PATTERN_t */ AC_REP_t rep; /* Representative string (optional) */ } AC_PATTERN_t; diff --git a/src/lib/third_party/include/ahocorasick.h b/src/lib/third_party/include/ahocorasick.h index 192a0e728..74812bef1 100644 --- a/src/lib/third_party/include/ahocorasick.h +++ b/src/lib/third_party/include/ahocorasick.h @@ -63,7 +63,7 @@ AC_ERROR_t ac_automata_add (AC_AUTOMATA_t * thiz, AC_PATTERN_t * str); void ac_automata_finalize (AC_AUTOMATA_t * thiz); int ac_automata_search (AC_AUTOMATA_t * thiz, AC_TEXT_t * str, AC_REP_t * param); void ac_automata_reset (AC_AUTOMATA_t * thiz); -void ac_automata_release (AC_AUTOMATA_t * thiz); +void ac_automata_release (AC_AUTOMATA_t * thiz, u_int8_t free_pattern); void ac_automata_display (AC_AUTOMATA_t * thiz, char repcast); #endif diff --git a/src/lib/third_party/include/node.h b/src/lib/third_party/include/node.h index 13cf2ff11..c172112aa 100644 --- a/src/lib/third_party/include/node.h +++ b/src/lib/third_party/include/node.h @@ -55,11 +55,11 @@ struct edge AC_NODE_t * node_create (void); AC_NODE_t * node_create_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha); -void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str); +void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str, u_int8_t is_existing); void node_register_outgoing (AC_NODE_t * thiz, AC_NODE_t * next, AC_ALPHABET_t alpha); AC_NODE_t * node_find_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha); AC_NODE_t * node_findbs_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha); -void node_release (AC_NODE_t * thiz); +void node_release (AC_NODE_t * thiz, u_int8_t free_pattern); void node_assign_id (AC_NODE_t * thiz); void node_sort_edges (AC_NODE_t * thiz); diff --git a/src/lib/third_party/src/ahocorasick.c b/src/lib/third_party/src/ahocorasick.c index 371dc06f8..ffa8bac77 100644 --- a/src/lib/third_party/src/ahocorasick.c +++ b/src/lib/third_party/src/ahocorasick.c @@ -108,7 +108,7 @@ AC_ERROR_t ac_automata_add (AC_AUTOMATA_t * thiz, AC_PATTERN_t * patt) return ACERR_DUPLICATE_PATTERN; n->final = 1; - node_register_matchstr(n, patt); + node_register_matchstr(n, patt, 0); thiz->total_patterns++; return ACERR_SUCCESS; @@ -227,8 +227,9 @@ void ac_automata_reset (AC_AUTOMATA_t * thiz) * Release all allocated memories to the automata * PARAMS: * AC_AUTOMATA_t * thiz: the pointer to the automata + * uint8_t free_pattern: if true, deallocate the patterns strings ******************************************************************************/ -void ac_automata_release (AC_AUTOMATA_t * thiz) +void ac_automata_release (AC_AUTOMATA_t * thiz, u_int8_t free_pattern) { unsigned int i; AC_NODE_t * n; @@ -236,7 +237,7 @@ void ac_automata_release (AC_AUTOMATA_t * thiz) for (i=0; i < thiz->all_nodes_num; i++) { n = thiz->all_nodes[i]; - node_release(n); + node_release(n, free_pattern); } ndpi_free(thiz->all_nodes); ndpi_free(thiz); @@ -326,7 +327,7 @@ static void ac_automata_union_matchstrs (AC_NODE_t * node) while ((m = m->failure_node)) { for (i=0; i < m->matched_patterns_num; i++) - node_register_matchstr(node, &(m->matched_patterns[i])); + node_register_matchstr(node, &(m->matched_patterns[i]), 1 /* this is an existing node */); if (m->final) node->final = 1; diff --git a/src/lib/third_party/src/node.c b/src/lib/third_party/src/node.c index 4da04de9b..96e25e8af 100644 --- a/src/lib/third_party/src/node.c +++ b/src/lib/third_party/src/node.c @@ -73,8 +73,15 @@ void node_init(AC_NODE_t * thiz) * FUNCTION: node_release * Release node ******************************************************************************/ -void node_release(AC_NODE_t * thiz) +void node_release(AC_NODE_t * thiz, u_int8_t free_pattern) { + if(free_pattern) { + for(int i=0; i<thiz->matched_patterns_num; i++) { + if(!thiz->matched_patterns[i].is_existing) + ndpi_free(thiz->matched_patterns[i].astring); + } + } + ndpi_free(thiz->matched_patterns); ndpi_free(thiz->outgoing); ndpi_free(thiz); @@ -174,7 +181,7 @@ AC_NODE_t * node_create_next (AC_NODE_t * thiz, AC_ALPHABET_t alpha) * FUNCTION: node_register_matchstr * Adds the pattern to the list of accepted pattern. ******************************************************************************/ -void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str) +void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str, u_int8_t is_existing) { /* Check if the new pattern already exists in the node list */ if (node_has_matchstr(thiz, str)) @@ -192,6 +199,7 @@ void node_register_matchstr (AC_NODE_t * thiz, AC_PATTERN_t * str) thiz->matched_patterns[thiz->matched_patterns_num].astring = str->astring; thiz->matched_patterns[thiz->matched_patterns_num].length = str->length; + thiz->matched_patterns[thiz->matched_patterns_num].is_existing = is_existing; memcpy(&thiz->matched_patterns[thiz->matched_patterns_num].rep, &str->rep, sizeof(AC_REP_t)); thiz->matched_patterns_num++; } |