aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2016-05-08 10:55:46 +0200
committerLuca Deri <deri@ntop.org>2016-05-08 10:55:46 +0200
commit22e03ee98a07cce4a58b38b97860f56541ab82d9 (patch)
tree65511035e08e4b010bc5719f3a45ccf5fa4d0b34 /example
parent18901ca4d68d0c6ab4e56d3eaebf7ed5fb05264b (diff)
Simplified nDPI initialization function
Diffstat (limited to 'example')
-rw-r--r--example/ndpiReader.c36
-rw-r--r--example/ndpi_util.c42
-rw-r--r--example/ndpi_util.h17
3 files changed, 45 insertions, 50 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