diff options
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r-- | example/ndpiReader.c | 50 |
1 files changed, 12 insertions, 38 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 85ebac478..4357606fb 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -132,6 +132,7 @@ u_int8_t max_num_udp_dissected_pkts = 24 /* 8 is enough for most protocols, Sign static u_int32_t pcap_analysis_duration = (u_int32_t)-1; static u_int32_t risk_stats[NDPI_MAX_RISK] = { 0 }, risks_found = 0, flows_with_risks = 0; static struct ndpi_stats cumulative_stats; +static int cumulative_stats_initialized = 0; static u_int16_t decode_tunnels = 0; static u_int16_t num_loops = 1; static u_int8_t shutdown_app = 0, quiet_mode = 0; @@ -3133,18 +3134,7 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle, } unsigned int num_protocols = ndpi_get_num_protocols(ndpi_thread_info[thread_id].workflow->ndpi_struct); - ndpi_thread_info[thread_id].workflow->stats.protocol_counter = ndpi_calloc(sizeof(u_int64_t), num_protocols); - ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes = ndpi_calloc(sizeof(u_int64_t), num_protocols); - ndpi_thread_info[thread_id].workflow->stats.protocol_flows = ndpi_calloc(sizeof(u_int64_t), num_protocols); - ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_counter = ndpi_calloc(sizeof(u_int64_t), num_protocols); - ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_counter_bytes = ndpi_calloc(sizeof(u_int64_t), num_protocols); - ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_flows = ndpi_calloc(sizeof(u_int64_t), num_protocols); - if(!ndpi_thread_info[thread_id].workflow->stats.protocol_counter || - !ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes || - !ndpi_thread_info[thread_id].workflow->stats.protocol_flows || - !ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_counter || - !ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_counter_bytes || - !ndpi_thread_info[thread_id].workflow->stats.fpc_protocol_flows) { + if (!ndpi_stats_init(&ndpi_thread_info[thread_id].workflow->stats, num_protocols)) { exit(-1); } @@ -4055,25 +4045,14 @@ static void printResults(u_int64_t processing_time_usec, u_int64_t setup_time_us long long unsigned int breed_stats_pkts[NUM_BREEDS] = { 0 }; long long unsigned int breed_stats_bytes[NUM_BREEDS] = { 0 }; long long unsigned int breed_stats_flows[NUM_BREEDS] = { 0 }; - unsigned int num_protocols; - - memset(&cumulative_stats, 0, sizeof(cumulative_stats)); /* In ndpiReader all the contexts have the same configuration */ - num_protocols = ndpi_get_num_protocols(ndpi_thread_info[0].workflow->ndpi_struct); - cumulative_stats.protocol_counter = ndpi_calloc(sizeof(u_int64_t), num_protocols); - cumulative_stats.protocol_counter_bytes = ndpi_calloc(sizeof(u_int64_t), num_protocols); - cumulative_stats.protocol_flows = ndpi_calloc(sizeof(u_int64_t), num_protocols); - cumulative_stats.fpc_protocol_counter = ndpi_calloc(sizeof(u_int64_t), num_protocols); - cumulative_stats.fpc_protocol_counter_bytes = ndpi_calloc(sizeof(u_int64_t), num_protocols); - cumulative_stats.fpc_protocol_flows = ndpi_calloc(sizeof(u_int64_t), num_protocols); - if(!cumulative_stats.protocol_counter || - !cumulative_stats.protocol_counter_bytes || - !cumulative_stats.protocol_flows || - !cumulative_stats.fpc_protocol_counter || - !cumulative_stats.fpc_protocol_counter_bytes || - !cumulative_stats.fpc_protocol_flows) { - goto free_stats; + if (!cumulative_stats_initialized) { + unsigned int num_protocols = ndpi_get_num_protocols(ndpi_thread_info[0].workflow->ndpi_struct); + if (!ndpi_stats_init(&cumulative_stats, num_protocols)) { + return; + } + cumulative_stats_initialized = 1; } for(thread_id = 0; thread_id < num_threads; thread_id++) { @@ -4096,7 +4075,7 @@ static void printResults(u_int64_t processing_time_usec, u_int64_t setup_time_us cumulative_stats.total_ip_bytes += ndpi_thread_info[thread_id].workflow->stats.total_ip_bytes; cumulative_stats.total_discarded_bytes += ndpi_thread_info[thread_id].workflow->stats.total_discarded_bytes; - for(i = 0; i < ndpi_get_num_protocols(ndpi_thread_info[0].workflow->ndpi_struct); i++) { + for (i = 0; i < cumulative_stats.num_protocols; i++) { cumulative_stats.protocol_counter[i] += ndpi_thread_info[thread_id].workflow->stats.protocol_counter[i]; cumulative_stats.protocol_counter_bytes[i] += ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes[i]; cumulative_stats.protocol_flows[i] += ndpi_thread_info[thread_id].workflow->stats.protocol_flows[i]; @@ -4479,7 +4458,7 @@ static void printResults(u_int64_t processing_time_usec, u_int64_t setup_time_us } if(!quiet_mode) printf("\n\nDetected protocols:\n"); - for(i = 0; i < ndpi_get_num_protocols(ndpi_thread_info[0].workflow->ndpi_struct); i++) { + for(i = 0; i < cumulative_stats.num_protocols; i++) { ndpi_protocol_breed_t breed = ndpi_get_proto_breed(ndpi_thread_info[0].workflow->ndpi_struct, ndpi_map_ndpi_id_to_user_proto_id(ndpi_thread_info[0].workflow->ndpi_struct, i)); @@ -4628,12 +4607,7 @@ static void printResults(u_int64_t processing_time_usec, u_int64_t setup_time_us dstStats = NULL; } - ndpi_free(cumulative_stats.protocol_counter); - ndpi_free(cumulative_stats.protocol_counter_bytes); - ndpi_free(cumulative_stats.protocol_flows); - ndpi_free(cumulative_stats.fpc_protocol_counter); - ndpi_free(cumulative_stats.fpc_protocol_counter_bytes); - ndpi_free(cumulative_stats.fpc_protocol_flows); + ndpi_stats_reset(&cumulative_stats); } /** @@ -4962,7 +4936,7 @@ static void ndpi_process_packet(u_char *args, 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; - memset(&ndpi_thread_info[thread_id].workflow->stats, 0, sizeof(struct ndpi_stats)); + ndpi_stats_reset(&ndpi_thread_info[thread_id].workflow->stats); } if(!quiet_mode) |