aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/ndpiReader.c36
-rw-r--r--example/ndpi_util.c42
-rw-r--r--example/ndpi_util.h17
-rw-r--r--libndpi.sym1
-rw-r--r--src/include/ndpi_api.h14
-rw-r--r--src/lib/ndpi_main.c53
6 files changed, 75 insertions, 88 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 618f10524..f06cc6beb 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -49,13 +49,6 @@
#include <json.h>
#endif
-#define MAX_NUM_READER_THREADS 16
-#define IDLE_SCAN_PERIOD 10 /* msec (use detection_tick_resolution = 1000) */
-#define MAX_IDLE_TIME 30000
-#define IDLE_SCAN_BUDGET 1024
-#define NUM_ROOTS 512
-#define MAX_NDPI_FLOWS 200000000
-
#include "ndpi_util.h"
/** Client parameters **/
@@ -84,7 +77,6 @@ static int core_affinity[MAX_NUM_READER_THREADS];
#endif
static struct timeval pcap_start, pcap_end;
/** Detection parameters **/
-static u_int32_t detection_tick_resolution = 1000;
static time_t capture_for = 0;
static time_t capture_until = 0;
static u_int32_t num_flows;
@@ -109,7 +101,7 @@ typedef struct ndpi_id {
} ndpi_id_t;
// used memory counters
-static u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0;
+u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0;
/********************** FUNCTIONS ********************* */
@@ -353,28 +345,6 @@ char* intoaV4(unsigned int addr, char* buf, u_short bufLen) {
return(retStr);
}
-
-/**
- * @brief malloc wrapper function
- */
-static void *malloc_wrapper(size_t size) {
- current_ndpi_memory += size;
-
- if(current_ndpi_memory > max_ndpi_memory)
- max_ndpi_memory = current_ndpi_memory;
-
- return malloc(size);
-}
-
-
-/**
- * @brief free wrapper function
- */
-static void free_wrapper(void *freeable) {
- free(freeable);
-}
-
-
/**
* @brief Print the flow
*/
@@ -656,10 +626,9 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
prefs.num_roots = NUM_ROOTS;
prefs.max_ndpi_flows = MAX_NDPI_FLOWS;
prefs.quiet_mode = quiet_mode;
- prefs.detection_tick_resolution = detection_tick_resolution;
memset(&ndpi_thread_info[thread_id], 0, sizeof(ndpi_thread_info[thread_id]));
- ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle, malloc_wrapper, free_wrapper, debug_printf);
+ ndpi_thread_info[thread_id].workflow = ndpi_workflow_init(&prefs, pcap_handle);
/* ndpi_thread_info[thread_id].workflow->ndpi_struct->http_dont_dissect_response = 1; */
ndpi_workflow_set_flow_detected_callback(ndpi_thread_info[thread_id].workflow, on_protocol_discovered, (void *)(uintptr_t)thread_id);
@@ -1306,7 +1275,6 @@ 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);
diff --git a/example/ndpi_util.c b/example/ndpi_util.c
index e1135f5d5..ff3132fad 100644
--- a/example/ndpi_util.c
+++ b/example/ndpi_util.c
@@ -85,16 +85,39 @@ static const u_int8_t nDPI_traceLevel = 0;
/* TODO remove in future... */
static void (*removeme_free_wrapper)(void*);
-struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs,
- pcap_t * pcap_handle,
- void * (*malloc_wrapper)(size_t),
- void (*free_wrapper)(void*),
- ndpi_debug_function_ptr ndpi_debug_printf) {
+/* ***************************************************** */
+
+extern u_int32_t current_ndpi_memory, max_ndpi_memory;
+
+/**
+ * @brief malloc wrapper function
+ */
+static void *malloc_wrapper(size_t size) {
+ current_ndpi_memory += size;
+
+ if(current_ndpi_memory > max_ndpi_memory)
+ max_ndpi_memory = current_ndpi_memory;
+
+ return malloc(size);
+}
+
+/* ***************************************************** */
+
+/**
+ * @brief free wrapper function
+ */
+static void free_wrapper(void *freeable) {
+ free(freeable);
+}
+
+/* ***************************************************** */
+
+struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle) {
+ set_ndpi_malloc(malloc_wrapper), set_ndpi_free(free_wrapper);
/* TODO: just needed here to init ndpi malloc wrapper */
- struct ndpi_detection_module_struct * module = ndpi_init_detection_module(prefs->detection_tick_resolution,
- malloc_wrapper, free_wrapper, ndpi_debug_printf);
-
+ struct ndpi_detection_module_struct * module = ndpi_init_detection_module();
+
struct ndpi_workflow * workflow = ndpi_calloc(1, sizeof(struct ndpi_workflow));
removeme_free_wrapper = free_wrapper;
@@ -553,8 +576,7 @@ void ndpi_workflow_process_packet (struct ndpi_workflow * workflow,
workflow->stats.raw_packet_count++;
/* setting time */
- time = ((uint64_t) header->ts.tv_sec) * workflow->prefs.detection_tick_resolution +
- header->ts.tv_usec / (1000000 / workflow->prefs.detection_tick_resolution);
+ time = ((uint64_t) header->ts.tv_sec) * TICK_RESOLUTION + header->ts.tv_usec / (1000000 / TICK_RESOLUTION);
/* safety check */
if(workflow->last_time > time) {
diff --git a/example/ndpi_util.h b/example/ndpi_util.h
index 13bc02cbc..7b1450774 100644
--- a/example/ndpi_util.h
+++ b/example/ndpi_util.h
@@ -32,6 +32,15 @@
#include <pcap.h>
+#define MAX_NUM_READER_THREADS 16
+#define IDLE_SCAN_PERIOD 10 /* msec (use TICK_RESOLUTION = 1000) */
+#define MAX_IDLE_TIME 30000
+#define IDLE_SCAN_BUDGET 1024
+#define NUM_ROOTS 512
+#define MAX_NDPI_FLOWS 200000000
+#define TICK_RESOLUTION 1000
+
+
// flow tracking
typedef struct ndpi_flow_info {
u_int32_t lower_ip;
@@ -80,7 +89,6 @@ typedef struct ndpi_workflow_prefs {
u_int8_t quiet_mode;
u_int32_t num_roots;
u_int32_t max_ndpi_flows;
- u_int32_t detection_tick_resolution;
} ndpi_workflow_prefs_t;
struct ndpi_workflow;
@@ -107,11 +115,7 @@ typedef struct ndpi_workflow {
} ndpi_workflow_t;
/* TODO: remove wrappers parameters and use ndpi global, when their initialization will be fixed... */
-struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs,
- pcap_t * pcap_handle,
- void * (*malloc_wrapper)(size_t),
- void (*free_wrapper)(void*),
- ndpi_debug_function_ptr ndpi_debug_printf);
+struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle);
void ndpi_workflow_free(struct ndpi_workflow * workflow);
@@ -143,4 +147,5 @@ static inline void ndpi_workflow_set_flow_giveup_callback(struct ndpi_workflow *
int ndpi_workflow_node_cmp(const void *a, const void *b);
+
#endif
diff --git a/libndpi.sym b/libndpi.sym
index 2302bfa57..933c7b0fd 100644
--- a/libndpi.sym
+++ b/libndpi.sym
@@ -43,3 +43,4 @@ ndpi_finalize_automa
ndpi_match_string
set_ndpi_malloc
set_ndpi_free
+set_ndpi_debug_function \ No newline at end of file
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h
index 113aa0ea2..1bdd207a1 100644
--- a/src/include/ndpi_api.h
+++ b/src/include/ndpi_api.h
@@ -115,22 +115,14 @@ extern "C" {
*
*/
void ndpi_init_protocol_match(struct ndpi_detection_module_struct *ndpi_mod, ndpi_protocol_match *match);
-
/**
* Returns a new initialized detection module
*
- * @par ticks_per_second = the timestamp resolution per second (like 1000 for millisecond resolution)
- * @par __ndpi_malloc = function pointer to a nDPI memory allocator
- * @par ndpi_debug_printf = function pointer to a nDPI debug output function (use NULL in productive envionments)
* @return the initialized detection module
*
*/
- struct ndpi_detection_module_struct *ndpi_init_detection_module(u_int32_t ticks_per_second,
- void* (*__ndpi_malloc)(size_t size),
- void (*__ndpi_free)(void *ptr),
- ndpi_debug_function_ptr ndpi_debug_printf);
-
+ struct ndpi_detection_module_struct *ndpi_init_detection_module();
/**
* Frees the memory allocated in the specified flow
@@ -595,10 +587,10 @@ extern "C" {
int ndpi_match_string(void *_automa, char *string_to_match);
-
- /* Utility functions to set ndpi malloc/free wrappers */
+ /* Utility functions to set ndpi malloc/free/print wrappers */
void set_ndpi_malloc(void* (*__ndpi_malloc)(size_t size));
void set_ndpi_free(void (*__ndpi_free)(void *ptr));
+ void set_ndpi_debug_function(ndpi_debug_function_ptr ndpi_debug_printf);
#ifdef __cplusplus
}
diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c
index dcc5d8705..7276f3ae0 100644
--- a/src/lib/ndpi_main.c
+++ b/src/lib/ndpi_main.c
@@ -308,7 +308,7 @@ static int removeDefaultPort(ndpi_port_range *range,
/* ****************************************** */
-void* ndpi_malloc(size_t size) { return(_ndpi_malloc(size)); }
+void* ndpi_malloc(size_t size) { return(_ndpi_malloc ? _ndpi_malloc(size) : malloc(size)); }
/* ****************************************** */
@@ -324,7 +324,7 @@ void* ndpi_calloc(unsigned long count, size_t size) {
/* ****************************************** */
-void ndpi_free(void *ptr) { _ndpi_free(ptr); }
+void ndpi_free(void *ptr) { if(_ndpi_free) _ndpi_free(ptr); else free(ptr); }
/* ****************************************** */
@@ -1682,24 +1682,24 @@ 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; }
-/* ******************************************************************** */
+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,
- void* (*__ndpi_malloc)(size_t size),
- void (*__ndpi_free)(void *ptr),
- ndpi_debug_function_ptr ndpi_debug_printf)
-{
- struct ndpi_detection_module_struct *ndpi_str;
+void set_ndpi_debug_function(ndpi_debug_function_ptr ndpi_debug_printf) {
+#ifdef NDPI_ENABLE_DEBUG_MESSAGES
+ ndpi_str->ndpi_debug_printf = ndpi_debug_printf;
+#endif
+}
- /* TODO global malloc wrappers should not be set here: ndpi_init_detection_module can be called many times */
- set_ndpi_malloc(__ndpi_malloc), set_ndpi_free(__ndpi_free);
+/* ******************************************************************** */
- ndpi_str = ndpi_malloc(sizeof(struct ndpi_detection_module_struct));
+struct ndpi_detection_module_struct *ndpi_init_detection_module() {
+ struct ndpi_detection_module_struct *ndpi_str = ndpi_malloc(sizeof(struct ndpi_detection_module_struct));
if(ndpi_str == NULL) {
+#ifdef NDPI_ENABLE_DEBUG_MESSAGES
ndpi_debug_printf(0, NULL, NDPI_LOG_DEBUG, "ndpi_init_detection_module initial malloc failed\n");
+#endif
return NULL;
}
memset(ndpi_str, 0, sizeof(struct ndpi_detection_module_struct));
@@ -1709,30 +1709,29 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(u_int32_t ticks_
NDPI_BITMASK_RESET(ndpi_str->detection_bitmask);
#ifdef NDPI_ENABLE_DEBUG_MESSAGES
- ndpi_str->ndpi_debug_printf = ndpi_debug_printf;
ndpi_str->user_data = NULL;
#endif
- ndpi_str->ticks_per_second = ticks_per_second;
+ ndpi_str->ticks_per_second = 1000; /* ndpi_str->ticks_per_second */
ndpi_str->tcp_max_retransmission_window_size = NDPI_DEFAULT_MAX_TCP_RETRANSMISSION_WINDOW_SIZE;
ndpi_str->directconnect_connection_ip_tick_timeout =
- NDPI_DIRECTCONNECT_CONNECTION_IP_TICK_TIMEOUT * ticks_per_second;
+ NDPI_DIRECTCONNECT_CONNECTION_IP_TICK_TIMEOUT * ndpi_str->ticks_per_second;
- ndpi_str->rtsp_connection_timeout = NDPI_RTSP_CONNECTION_TIMEOUT * ticks_per_second;
- ndpi_str->tvants_connection_timeout = NDPI_TVANTS_CONNECTION_TIMEOUT * ticks_per_second;
- ndpi_str->irc_timeout = NDPI_IRC_CONNECTION_TIMEOUT * ticks_per_second;
- ndpi_str->gnutella_timeout = NDPI_GNUTELLA_CONNECTION_TIMEOUT * ticks_per_second;
+ ndpi_str->rtsp_connection_timeout = NDPI_RTSP_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->tvants_connection_timeout = NDPI_TVANTS_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->irc_timeout = NDPI_IRC_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->gnutella_timeout = NDPI_GNUTELLA_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
- ndpi_str->battlefield_timeout = NDPI_BATTLEFIELD_CONNECTION_TIMEOUT * ticks_per_second;
+ ndpi_str->battlefield_timeout = NDPI_BATTLEFIELD_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
- ndpi_str->thunder_timeout = NDPI_THUNDER_CONNECTION_TIMEOUT * ticks_per_second;
+ ndpi_str->thunder_timeout = NDPI_THUNDER_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
ndpi_str->yahoo_detect_http_connections = NDPI_YAHOO_DETECT_HTTP_CONNECTIONS;
- ndpi_str->yahoo_lan_video_timeout = NDPI_YAHOO_LAN_VIDEO_TIMEOUT * ticks_per_second;
- ndpi_str->zattoo_connection_timeout = NDPI_ZATTOO_CONNECTION_TIMEOUT * ticks_per_second;
- ndpi_str->jabber_stun_timeout = NDPI_JABBER_STUN_TIMEOUT * ticks_per_second;
- ndpi_str->jabber_file_transfer_timeout = NDPI_JABBER_FT_TIMEOUT * ticks_per_second;
- ndpi_str->soulseek_connection_ip_tick_timeout = NDPI_SOULSEEK_CONNECTION_IP_TICK_TIMEOUT * ticks_per_second;
+ ndpi_str->yahoo_lan_video_timeout = NDPI_YAHOO_LAN_VIDEO_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->zattoo_connection_timeout = NDPI_ZATTOO_CONNECTION_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->jabber_stun_timeout = NDPI_JABBER_STUN_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->jabber_file_transfer_timeout = NDPI_JABBER_FT_TIMEOUT * ndpi_str->ticks_per_second;
+ ndpi_str->soulseek_connection_ip_tick_timeout = NDPI_SOULSEEK_CONNECTION_IP_TICK_TIMEOUT * ndpi_str->ticks_per_second;
ndpi_str->ndpi_num_supported_protocols = NDPI_MAX_SUPPORTED_PROTOCOLS;
ndpi_str->ndpi_num_custom_protocols = 0;