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()
This commit is contained in:
+16
-1
@@ -2,6 +2,7 @@
|
|||||||
* ndpiReader.c
|
* ndpiReader.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011-16 - ntop.org
|
* Copyright (C) 2011-16 - ntop.org
|
||||||
|
*
|
||||||
* nDPI is free software: you can redistribute it and/or modify
|
* nDPI is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Lesser General Public License as published by
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
@@ -40,7 +41,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <assert.h>
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
#include "ndpi_api.h"
|
#include "ndpi_api.h"
|
||||||
|
|
||||||
@@ -1302,6 +1303,18 @@ void test_lib() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void automataUnitTest() {
|
||||||
|
void *automa;
|
||||||
|
|
||||||
|
set_ndpi_malloc(malloc_wrapper), set_ndpi_free(free_wrapper);
|
||||||
|
assert(automa = ndpi_init_automa());
|
||||||
|
assert(ndpi_add_string_to_automa(automa, "hello") == 0);
|
||||||
|
assert(ndpi_add_string_to_automa(automa, "world") == 0);
|
||||||
|
ndpi_finalize_automa(automa);
|
||||||
|
assert(ndpi_match_string(automa, "This is the wonderful world of nDPI") == 0);
|
||||||
|
|
||||||
|
ndpi_free_automa(automa);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief MAIN FUNCTION
|
@brief MAIN FUNCTION
|
||||||
@@ -1310,6 +1323,8 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
automataUnitTest();
|
||||||
|
|
||||||
memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info));
|
memset(ndpi_thread_info, 0, sizeof(ndpi_thread_info));
|
||||||
memset(&pcap_start, 0, sizeof(pcap_start));
|
memset(&pcap_start, 0, sizeof(pcap_start));
|
||||||
memset(&pcap_end, 0, sizeof(pcap_end));
|
memset(&pcap_end, 0, sizeof(pcap_end));
|
||||||
|
|||||||
+2
-2
@@ -92,8 +92,8 @@ struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * pre
|
|||||||
ndpi_debug_function_ptr ndpi_debug_printf) {
|
ndpi_debug_function_ptr ndpi_debug_printf) {
|
||||||
|
|
||||||
/* TODO: just needed here to init ndpi malloc wrapper */
|
/* TODO: just needed here to init ndpi malloc wrapper */
|
||||||
struct ndpi_detection_module_struct * module = ndpi_init_detection_module(
|
struct ndpi_detection_module_struct * module = ndpi_init_detection_module(prefs->detection_tick_resolution,
|
||||||
prefs->detection_tick_resolution, malloc_wrapper, free_wrapper, ndpi_debug_printf);
|
malloc_wrapper, free_wrapper, ndpi_debug_printf);
|
||||||
|
|
||||||
struct ndpi_workflow * workflow = ndpi_calloc(1, sizeof(struct ndpi_workflow));
|
struct ndpi_workflow * workflow = ndpi_calloc(1, sizeof(struct ndpi_workflow));
|
||||||
|
|
||||||
|
|||||||
@@ -36,3 +36,10 @@ ndpi_malloc
|
|||||||
ndpi_calloc
|
ndpi_calloc
|
||||||
ndpi_set_detected_protocol
|
ndpi_set_detected_protocol
|
||||||
ndpi_match_string_subprotocol
|
ndpi_match_string_subprotocol
|
||||||
|
ndpi_init_automa
|
||||||
|
ndpi_free_automa
|
||||||
|
ndpi_add_string_to_automa
|
||||||
|
ndpi_finalize_automa
|
||||||
|
ndpi_match_string
|
||||||
|
set_ndpi_malloc
|
||||||
|
set_ndpi_free
|
||||||
|
|||||||
@@ -544,6 +544,62 @@ extern "C" {
|
|||||||
struct ndpi_flow_struct *flow, char *certificate);
|
struct ndpi_flow_struct *flow, char *certificate);
|
||||||
#endif
|
#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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -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",
|
"yj", "yq", "yv", "yz", "yw", "zb", "zc", "zg", "zh", "zj", "zn", "zq", "zr", "zs", "zx", "wh", "wk",
|
||||||
"wb", "zk", "kp", "zk", "xy",
|
"wb", "zk", "kp", "zk", "xy",
|
||||||
NULL };
|
NULL };
|
||||||
|
|
||||||
|
|||||||
+45
-2
@@ -1681,6 +1681,9 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp
|
|||||||
|
|
||||||
#endif
|
#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,
|
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;
|
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 */
|
/* TODO global malloc wrappers should not be set here: ndpi_init_detection_module can be called many times */
|
||||||
_ndpi_malloc = __ndpi_malloc;
|
set_ndpi_malloc(__ndpi_malloc), set_ndpi_free(__ndpi_free);
|
||||||
_ndpi_free = __ndpi_free;
|
|
||||||
|
|
||||||
ndpi_str = ndpi_malloc(sizeof(struct ndpi_detection_module_struct));
|
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) { ; }
|
static void free_ptree_data(void *data) { ; }
|
||||||
|
|
||||||
/* ****************************************************** */
|
/* ****************************************************** */
|
||||||
|
|||||||
@@ -117,8 +117,7 @@ void ndpi_search_dns(struct ndpi_detection_module_struct *ndpi_struct, struct nd
|
|||||||
if(off < flow->packet.payload_packet_len)
|
if(off < flow->packet.payload_packet_len)
|
||||||
{
|
{
|
||||||
flow->host_server_name[j] = flow->packet.payload[off];
|
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] < ' ')
|
if(flow->host_server_name[j] < ' ')
|
||||||
flow->host_server_name[j] = '.';
|
flow->host_server_name[j] = '.';
|
||||||
j++;
|
j++;
|
||||||
|
|||||||
Reference in New Issue
Block a user