diff options
author | Ivan Nardi <12729895+IvanNardi@users.noreply.github.com> | 2024-01-22 18:12:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-22 18:12:06 +0100 |
commit | 9b26e74bb7e105f5f79fd4bbfb4cdfe0b6e94f04 (patch) | |
tree | 4ebd986030b440dd45e99f3b0a95b3219a57f3c0 /example | |
parent | 82e8bf91ddb5bf38974d4372fb0ec4849b964ec8 (diff) |
example: rework code between `ndpiReader.c` and `reader_util.c` (#2273)
Diffstat (limited to 'example')
-rw-r--r-- | example/ndpiReader.c | 61 | ||||
-rw-r--r-- | example/reader_util.c | 57 |
2 files changed, 47 insertions, 71 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 5685cda9a..c516d9b13 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -135,9 +135,9 @@ extern u_int32_t max_num_packets_per_flow, max_packet_payload_dissection, max_nu extern u_int16_t min_pattern_len, max_pattern_len; u_int8_t dump_internal_stats; -struct ndpi_bin malloc_bins; -int enable_malloc_bins = 0; -int max_malloc_bins = 14; +static struct ndpi_bin malloc_bins; +static int enable_malloc_bins = 0; +static int max_malloc_bins = 14; int malloc_size_stats = 0; struct flow_info { @@ -252,7 +252,7 @@ typedef struct ndpi_id { } ndpi_id_t; // used memory counters -u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0; +static u_int32_t current_ndpi_memory = 0, max_ndpi_memory = 0; #ifdef USE_DPDK static int dpdk_port_id = 0, dpdk_run_capture = 1; #endif @@ -270,7 +270,45 @@ extern int parse_proto_name_list(char *str, NDPI_PROTOCOL_BITMASK *bitmask, int FILE *trace = NULL; #endif -/* ********************************** */ +/* ***************************************************** */ + +static u_int32_t reader_slot_malloc_bins(u_int64_t v) +{ + int i; + + /* 0-2,3-4,5-8,9-16,17-32,33-64,65-128,129-256,257-512,513-1024,1025-2048,2049-4096,4097-8192,8193- */ + for(i=0; i < max_malloc_bins - 1; i++) + if((1ULL << (i + 1)) >= v) + return i; + return i; +} + +/** + * @brief ndpi_malloc wrapper function + */ +static void *ndpi_malloc_wrapper(size_t size) { + current_ndpi_memory += size; + + if(current_ndpi_memory > max_ndpi_memory) + max_ndpi_memory = current_ndpi_memory; + + if(enable_malloc_bins && malloc_size_stats) + ndpi_inc_bin(&malloc_bins, reader_slot_malloc_bins(size), 1); + + return(malloc(size)); /* Don't change to ndpi_malloc !!!!! */ +} + +/* ***************************************************** */ + +/** + * @brief free wrapper function + */ +static void free_wrapper(void *freeable) { + free(freeable); /* Don't change to ndpi_free !!!!! */ +} + +/* ***************************************************** */ + #define NUM_DOH_BINS 2 @@ -2820,16 +2858,6 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) { /* Make sure to load lists before finalizing the initialization */ ndpi_set_protocol_detection_bitmask2(ndpi_thread_info[thread_id].workflow->ndpi_struct, &enabled_bitmask); - // clear memory for results - memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter, 0, - sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter)); - memset(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes, 0, - sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_counter_bytes)); - memset(ndpi_thread_info[thread_id].workflow->stats.protocol_flows, 0, - sizeof(ndpi_thread_info[thread_id].workflow->stats.protocol_flows)); - memset(ndpi_thread_info[thread_id].workflow->stats.flow_confidence, 0, - sizeof(ndpi_thread_info[thread_id].workflow->stats.flow_confidence)); - if(_protoFilePath != NULL) ndpi_load_protocols_file(ndpi_thread_info[thread_id].workflow->ndpi_struct, _protoFilePath); @@ -4631,6 +4659,9 @@ void test_lib() { long thread_id; #endif + set_ndpi_malloc(ndpi_malloc_wrapper), set_ndpi_free(free_wrapper); + set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL); + #ifdef DEBUG_TRACE if(trace) fprintf(trace, "Num threads: %d\n", num_threads); #endif diff --git a/example/reader_util.c b/example/reader_util.c index 0fe629ac6..e666e4690 100644 --- a/example/reader_util.c +++ b/example/reader_util.c @@ -81,9 +81,6 @@ static u_int32_t flow_id = 0; u_int8_t enable_doh_dot_detection = 0; extern int malloc_size_stats; -extern struct ndpi_bin malloc_bins; -extern int max_malloc_bins; -extern int enable_malloc_bins; /* ****************************************************** */ @@ -335,45 +332,6 @@ void ndpi_free_flow_info_half(struct ndpi_flow_info *flow) { /* ***************************************************** */ -extern u_int32_t current_ndpi_memory, max_ndpi_memory; - -static u_int32_t __slot_malloc_bins(u_int64_t v) -{ - int i; - - /* 0-2,3-4,5-8,9-16,17-32,33-64,65-128,129-256,257-512,513-1024,1025-2048,2049-4096,4097-8192,8193- */ - for(i=0; i < max_malloc_bins - 1; i++) - if((1ULL << (i + 1)) >= v) - return i; - return i; -} - -/** - * @brief ndpi_malloc wrapper function - */ -static void *ndpi_malloc_wrapper(size_t size) { - current_ndpi_memory += size; - - if(current_ndpi_memory > max_ndpi_memory) - max_ndpi_memory = current_ndpi_memory; - - if(enable_malloc_bins && malloc_size_stats) - ndpi_inc_bin(&malloc_bins, __slot_malloc_bins(size), 1); - - return(malloc(size)); /* Don't change to ndpi_malloc !!!!! */ -} - -/* ***************************************************** */ - -/** - * @brief free wrapper function - */ -static void free_wrapper(void *freeable) { - free(freeable); /* Don't change to ndpi_free !!!!! */ -} - -/* ***************************************************** */ - static uint16_t ndpi_get_proto_id(struct ndpi_detection_module_struct *ndpi_mod, const char *name) { uint16_t proto_id; char *e; @@ -454,13 +412,6 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref struct ndpi_detection_module_struct * module; struct ndpi_workflow * workflow; - /* On some fuzzers we don't want to use these memory allocators, but some custom ones */ -#ifndef DISABLE_CUSTOM_ALLOCATOR_ON_READERUTILS - set_ndpi_malloc(ndpi_malloc_wrapper), set_ndpi_free(free_wrapper); - set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL); -#endif - - /* TODO: just needed here to init ndpi ndpi_malloc wrapper */ module = ndpi_init_detection_module(); if(module == NULL) { @@ -1714,17 +1665,11 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow, if(enough_packets || (flow->detected_protocol.app_protocol != NDPI_PROTOCOL_UNKNOWN)) { if((!enough_packets) && ndpi_extra_dissection_possible(workflow->ndpi_struct, ndpi_flow)) - ; /* Wait for certificate fingerprint */ + ; /* Wait for further metadata */ else { /* New protocol detected or give up */ flow->detection_completed = 1; -#if 0 - /* Check if we should keep checking extra packets */ - if(ndpi_flow && ndpi_flow->check_extra_packets) - flow->check_extra_packets = 1; -#endif - if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UNKNOWN) { u_int8_t proto_guessed; |