Integrate root flows free and little changes

This commit is contained in:
emanuele-f
2016-04-20 15:36:44 +02:00
parent 4e1317cd0f
commit 5386aede36
3 changed files with 79 additions and 87 deletions
+3 -33
View File
@@ -125,11 +125,6 @@ typedef struct ndpi_id {
struct ndpi_id_struct *ndpi_id; // nDpi worker structure struct ndpi_id_struct *ndpi_id; // nDpi worker structure
} ndpi_id_t; } ndpi_id_t;
static u_int32_t size_id_struct = 0; // ID tracking structure size
static u_int32_t size_flow_struct = 0;
static void help(u_int long_help) { static void help(u_int long_help) {
printf("ndpiReader -i <file|device> [-f <filter>][-s <duration>]\n" printf("ndpiReader -i <file|device> [-f <filter>][-s <duration>]\n"
" [-p <protos>][-l <loops> [-q][-d][-h][-t][-v <level>]\n" " [-p <protos>][-l <loops> [-q][-d][-h][-t][-v <level>]\n"
@@ -472,24 +467,6 @@ static void printFlow(u_int16_t thread_id, struct ndpi_flow_info *flow) {
/* ***************************************************** */ /* ***************************************************** */
static void free_ndpi_flow_info(struct ndpi_flow_info *flow) {
if(flow->ndpi_flow) { ndpi_free_flow(flow->ndpi_flow); flow->ndpi_flow = NULL; }
if(flow->src_id) { ndpi_free(flow->src_id); flow->src_id = NULL; }
if(flow->dst_id) { ndpi_free(flow->dst_id); flow->dst_id = NULL; }
}
/* ***************************************************** */
static void ndpi_flow_info_freer(void *node) {
struct ndpi_flow_info *flow = (struct ndpi_flow_info*)node;
free_ndpi_flow_info(flow);
ndpi_free(flow);
}
/* ***************************************************** */
static void node_print_unknown_proto_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) { static void node_print_unknown_proto_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) {
struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)node; struct ndpi_flow_info *flow = *(struct ndpi_flow_info**)node;
u_int16_t thread_id = *((u_int16_t*)user_data); u_int16_t thread_id = *((u_int16_t*)user_data);
@@ -569,7 +546,7 @@ static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth,
if((flow->detected_protocol.protocol == NDPI_PROTOCOL_UNKNOWN) && !undetected_flows_deleted) if((flow->detected_protocol.protocol == NDPI_PROTOCOL_UNKNOWN) && !undetected_flows_deleted)
undetected_flows_deleted = 1; undetected_flows_deleted = 1;
free_ndpi_flow_info(flow); ndpi_free_flow_info_half(flow);
ndpi_thread_info[thread_id].workflow->stats.ndpi_flow_count--; ndpi_thread_info[thread_id].workflow->stats.ndpi_flow_count--;
/* adding to a queue (we can't delete it from the tree inline ) */ /* adding to a queue (we can't delete it from the tree inline ) */
@@ -669,13 +646,6 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
/* ***************************************************** */ /* ***************************************************** */
static void terminateDetection(u_int16_t thread_id) { static void terminateDetection(u_int16_t thread_id) {
int i;
for(i=0; i<NUM_ROOTS; i++) {
ndpi_tdestroy(ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i], ndpi_flow_info_freer);
ndpi_thread_info[thread_id].workflow->ndpi_flows_root[i] = NULL;
}
ndpi_workflow_free(ndpi_thread_info[thread_id].workflow); ndpi_workflow_free(ndpi_thread_info[thread_id].workflow);
} }
@@ -810,7 +780,7 @@ static void printResults(u_int64_t tot_usec) {
if(!quiet_mode) { if(!quiet_mode) {
printf("\nnDPI Memory statistics:\n"); printf("\nnDPI Memory statistics:\n");
printf("\tnDPI Memory (once): %-13s\n", formatBytes(sizeof(struct ndpi_detection_module_struct), buf, sizeof(buf))); printf("\tnDPI Memory (once): %-13s\n", formatBytes(sizeof(struct ndpi_detection_module_struct), buf, sizeof(buf)));
printf("\tFlow Memory (per flow): %-13s\n", formatBytes(size_flow_struct, buf, sizeof(buf))); printf("\tFlow Memory (per flow): %-13s\n", formatBytes(sizeof(struct ndpi_flow_struct), buf, sizeof(buf)));
printf("\tActual Memory: %-13s\n", formatBytes(current_ndpi_memory, buf, sizeof(buf))); printf("\tActual Memory: %-13s\n", formatBytes(current_ndpi_memory, buf, sizeof(buf)));
printf("\tPeak Memory: %-13s\n", formatBytes(max_ndpi_memory, buf, sizeof(buf))); printf("\tPeak Memory: %-13s\n", formatBytes(max_ndpi_memory, buf, sizeof(buf)));
@@ -1155,7 +1125,7 @@ static void pcap_packet_callback_checked(u_char *args,
ndpi_workflow_node_cmp); ndpi_workflow_node_cmp);
/* free the memory associated to idle flow in "idle_flows" - (see struct reader thread)*/ /* free the memory associated to idle flow in "idle_flows" - (see struct reader thread)*/
free_ndpi_flow_info(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]); ndpi_free_flow_info_half(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]);
ndpi_free(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]); ndpi_free(ndpi_thread_info[thread_id].idle_flows[ndpi_thread_info[thread_id].num_idle_flows]);
} }
+6
View File
@@ -119,6 +119,12 @@ struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * pre
void ndpi_workflow_free(struct ndpi_workflow * workflow); void ndpi_workflow_free(struct ndpi_workflow * workflow);
/** Free flow_info ndpi support structures but not the flow_info itself
*
* TODO remove! Half freeing things is bad!
*/
void ndpi_free_flow_info_half(struct ndpi_flow_info *flow);
/** Process a @packet and update the @workflow. */ /** Process a @packet and update the @workflow. */
void ndpi_workflow_process_packet (struct ndpi_workflow * workflow, void ndpi_workflow_process_packet (struct ndpi_workflow * workflow,
const struct pcap_pkthdr *header, const struct pcap_pkthdr *header,
+18 -2
View File
@@ -71,7 +71,7 @@
/* ***************************************************** */ /* ***************************************************** */
static void free_ndpi_flow_info(struct ndpi_flow_info *flow) { void ndpi_free_flow_info_half(struct ndpi_flow_info *flow) {
if(flow->ndpi_flow) { ndpi_free_flow(flow->ndpi_flow); flow->ndpi_flow = NULL; } if(flow->ndpi_flow) { ndpi_free_flow(flow->ndpi_flow); flow->ndpi_flow = NULL; }
if(flow->src_id) { ndpi_free(flow->src_id); flow->src_id = NULL; } if(flow->src_id) { ndpi_free(flow->src_id); flow->src_id = NULL; }
if(flow->dst_id) { ndpi_free(flow->dst_id); flow->dst_id = NULL; } if(flow->dst_id) { ndpi_free(flow->dst_id); flow->dst_id = NULL; }
@@ -113,7 +113,23 @@ struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * pre
return workflow; return workflow;
} }
/* ***************************************************** */
static void ndpi_flow_info_freer(void *node) {
struct ndpi_flow_info *flow = (struct ndpi_flow_info*)node;
ndpi_free_flow_info_half(flow);
ndpi_free(flow);
}
/* ***************************************************** */
void ndpi_workflow_free(struct ndpi_workflow * workflow) { void ndpi_workflow_free(struct ndpi_workflow * workflow) {
int i;
for(i=0; i<workflow->prefs.num_roots; i++)
ndpi_tdestroy(workflow->ndpi_flows_root[i], ndpi_flow_info_freer);
ndpi_exit_detection_module(workflow->ndpi_struct, removeme_free_wrapper); ndpi_exit_detection_module(workflow->ndpi_struct, removeme_free_wrapper);
free(workflow->ndpi_flows_root); free(workflow->ndpi_flows_root);
free(workflow); free(workflow);
@@ -474,7 +490,7 @@ static unsigned int packet_processing(struct ndpi_workflow * workflow,
workflow->__flow_detected_callback(workflow, flow, workflow->__flow_detected_udata); workflow->__flow_detected_callback(workflow, flow, workflow->__flow_detected_udata);
} }
free_ndpi_flow_info(flow); ndpi_free_flow_info_half(flow);
} }
return 0; return 0;