diff options
author | Luca Deri <deri@ntop.org> | 2016-05-07 18:45:18 +0200 |
---|---|---|
committer | Luca Deri <deri@ntop.org> | 2016-05-07 18:45:18 +0200 |
commit | 18901ca4d68d0c6ab4e56d3eaebf7ed5fb05264b (patch) | |
tree | 45b2dc2edd12fd90d586bc0dce768d379633f205 /src | |
parent | d5f77e92f7887ffc192c7df0ebb5a7bf93f37e41 (diff) |
Added wrappers for substring-search
ndpi_init_automa()
ndpi_free_automa()
ndpi_add_string_to_automa()
ndpi_finalize_automa()
ndpi_match_string()
set_ndpi_malloc()
set_ndpi_free()
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_api.h | 56 | ||||
-rw-r--r-- | src/lib/ndpi_content_match.c.inc | 1 | ||||
-rw-r--r-- | src/lib/ndpi_main.c | 47 | ||||
-rw-r--r-- | src/lib/protocols/dns.c | 3 |
4 files changed, 103 insertions, 4 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index a07c96e63..113aa0ea2 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -544,6 +544,62 @@ extern "C" { struct ndpi_flow_struct *flow, char *certificate); #endif + /* Wrappers functions */ + /** + * Init Aho-Corasick automata + * + * @return The requested automata, or NULL if an error occurred + * + */ + void* ndpi_init_automa(); + + + /** + * Free Aho-Corasick automata allocated with ndpi_init_automa(); + * + * @par The automata initialized with ndpi_init_automa(); + * @return The requested automata, or NULL if an error occurred + * + */ + void ndpi_free_automa(void *_automa); + + + /** + * Add a string to match to an automata + * + * @par The automata initialized with ndpi_init_automa(); + * @par The (sub)string to search + * @return 0 in case of no error, or -1 if an error occurred. + * + */ + int ndpi_add_string_to_automa(void *_automa, char *str); + + + /** + * Finalize the automa (necessary before start searching) + * + * @par The automata initialized with ndpi_init_automa(); + * + */ + void ndpi_finalize_automa(void *_automa); + + + /** + * Add a string to match to an automata + * + * @par The automata initialized with ndpi_init_automa(); + * @par The (sub)string to search + * @return 0 in case of match, or -1 if no match, or -2 if an error occurred. + * + */ + int ndpi_match_string(void *_automa, char *string_to_match); + + + + /* Utility functions to set ndpi malloc/free wrappers */ + void set_ndpi_malloc(void* (*__ndpi_malloc)(size_t size)); + void set_ndpi_free(void (*__ndpi_free)(void *ptr)); + #ifdef __cplusplus } #endif diff --git a/src/lib/ndpi_content_match.c.inc b/src/lib/ndpi_content_match.c.inc index 458ab1a7a..af6215842 100644 --- a/src/lib/ndpi_content_match.c.inc +++ b/src/lib/ndpi_content_match.c.inc @@ -7533,3 +7533,4 @@ static const char *ndpi_en_impossible_bigrams[] = { "yj", "yq", "yv", "yz", "yw", "zb", "zc", "zg", "zh", "zj", "zn", "zq", "zr", "zs", "zx", "wh", "wk", "wb", "zk", "kp", "zk", "xy", NULL }; + diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 9e8c75c8f..dcc5d8705 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1681,6 +1681,9 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp #endif +void set_ndpi_malloc(void* (*__ndpi_malloc)(size_t size)) { _ndpi_malloc = __ndpi_malloc; } +void set_ndpi_free(void (*__ndpi_free)(void *ptr)) { _ndpi_free = __ndpi_free; } + /* ******************************************************************** */ struct ndpi_detection_module_struct *ndpi_init_detection_module(u_int32_t ticks_per_second, @@ -1691,8 +1694,7 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(u_int32_t ticks_ struct ndpi_detection_module_struct *ndpi_str; /* TODO global malloc wrappers should not be set here: ndpi_init_detection_module can be called many times */ - _ndpi_malloc = __ndpi_malloc; - _ndpi_free = __ndpi_free; + set_ndpi_malloc(__ndpi_malloc), set_ndpi_free(__ndpi_free); ndpi_str = ndpi_malloc(sizeof(struct ndpi_detection_module_struct)); @@ -1746,6 +1748,47 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(u_int32_t ticks_ /* *********************************************** */ +/* Wrappers */ +void* ndpi_init_automa() { + return(ac_automata_init(ac_match_handler)); +} + +int ndpi_add_string_to_automa(void *_automa, char *str) { + AC_PATTERN_t ac_pattern; + AC_AUTOMATA_t *automa = (AC_AUTOMATA_t*)_automa; + + if(automa == NULL) return(-1); + + ac_pattern.astring = str; + ac_pattern.rep.number = 1; /* Dummy */ + ac_pattern.length = strlen(ac_pattern.astring); + return(ac_automata_add(automa, &ac_pattern) == ACERR_SUCCESS ? 0 : -1); +} + +void ndpi_free_automa(void *_automa) { ac_automata_release((AC_AUTOMATA_t*)_automa); } +void ndpi_finalize_automa(void *_automa) { ac_automata_finalize((AC_AUTOMATA_t*)_automa); } + +/* ****************************************************** */ + +int ndpi_match_string(void *_automa, char *string_to_match) { + int matching_protocol_id = NDPI_PROTOCOL_UNKNOWN; + AC_TEXT_t ac_input_text; + AC_AUTOMATA_t *automa = (AC_AUTOMATA_t*)_automa; + + if((automa == NULL) + || (string_to_match == NULL) + || (string_to_match[0] == '\0')) + return(-2); + + ac_input_text.astring = string_to_match, ac_input_text.length = strlen(string_to_match); + ac_automata_search(automa, &ac_input_text, (void*)&matching_protocol_id); + ac_automata_reset(automa); + + return(matching_protocol_id > 0 ? 0 : -1); +} + +/* *********************************************** */ + static void free_ptree_data(void *data) { ; } /* ****************************************************** */ diff --git a/src/lib/protocols/dns.c b/src/lib/protocols/dns.c index 503761137..c54c62f18 100644 --- a/src/lib/protocols/dns.c +++ b/src/lib/protocols/dns.c @@ -117,8 +117,7 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd if(off < flow->packet.payload_packet_len) { flow->host_server_name[j] = flow->packet.payload[off]; - if(j < strlen(flow->host_server_name)) - { + if(j < strlen((char*)flow->host_server_name)) { if(flow->host_server_name[j] < ' ') flow->host_server_name[j] = '.'; j++; |