Simplified nDPI initialization function

This commit is contained in:
Luca Deri
2016-05-08 10:55:46 +02:00
parent 18901ca4d6
commit 22e03ee98a
6 changed files with 76 additions and 89 deletions
+2 -34
View File
@@ -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);
+33 -11
View File
@@ -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) {
/* 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);
/* ***************************************************** */
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();
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) {
+11 -6
View File
@@ -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