From 56d87186f7e6d7dba042961be86d1b4b5ae9f2e3 Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Sun, 14 Jun 2020 14:42:39 +0200 Subject: Fix compilation with --enable-debug-messages flag NDPI_LOG* macros dereference ndpi_detection_module_struct object which is private to ndpi library (via NDPI_LIB_COMPILATION define). So we can't use them outside the library itself, i.e. in ndpiReader code Therefore, in files in example/, convert all (rare) uses of NDPI_LOG* macros to a new very simple macro, private to ndpiReader program. If necessary, such macro may be improved. According to a comment in ndpi_define.h, each dissector must define its own NDPI_CURRENT_PROTO macro before including ndpi_api.h file --- example/reader_util.c | 27 +++++++++++---------------- example/reader_util.h | 11 +++++++++++ 2 files changed, 22 insertions(+), 16 deletions(-) (limited to 'example') diff --git a/example/reader_util.c b/example/reader_util.c index 753c78cf1..fa90b7000 100644 --- a/example/reader_util.c +++ b/example/reader_util.c @@ -419,13 +419,13 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref module = ndpi_init_detection_module(ndpi_no_prefs); if(module == NULL) { - NDPI_LOG(0, NULL, NDPI_LOG_ERROR, "global structure initialization failed\n"); + LOG(NDPI_LOG_ERROR, "global structure initialization failed\n"); exit(-1); } workflow = ndpi_calloc(1, sizeof(struct ndpi_workflow)); if(workflow == NULL) { - NDPI_LOG(0, NULL, NDPI_LOG_ERROR, "global structure initialization failed\n"); + LOG(NDPI_LOG_ERROR, "global structure initialization failed\n"); ndpi_free(module); exit(-1); } @@ -441,13 +441,8 @@ struct ndpi_workflow* ndpi_workflow_init(const struct ndpi_workflow_prefs * pref exit(-1); _debug_protocols_ok = 1; } - -#ifdef NDPI_ENABLE_DEBUG_MESSAGES - NDPI_BITMASK_RESET(module->debug_bitmask); - if(_debug_protocols_ok) - module->debug_bitmask = debug_bitmask; -#endif + ndpi_set_debug_bitmask(module, debug_bitmask); workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *)); @@ -811,7 +806,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow if(ret == NULL) { if(workflow->stats.ndpi_flow_count == workflow->prefs.max_ndpi_flows) { - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_ERROR, + LOG(NDPI_LOG_ERROR, "maximum flow count (%u) has been exceeded\n", workflow->prefs.max_ndpi_flows); exit(-1); @@ -819,7 +814,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow struct ndpi_flow_info *newflow = (struct ndpi_flow_info*)malloc(sizeof(struct ndpi_flow_info)); if(newflow == NULL) { - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_ERROR, "[NDPI] %s(1): not enough memory\n", __FUNCTION__); + LOG(NDPI_LOG_ERROR, "[NDPI] %s(1): not enough memory\n", __FUNCTION__); return(NULL); } else workflow->num_allocated_flows++; @@ -856,7 +851,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow } if((newflow->ndpi_flow = ndpi_flow_malloc(SIZEOF_FLOW_STRUCT)) == NULL) { - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_ERROR, "[NDPI] %s(2): not enough memory\n", __FUNCTION__); + LOG(NDPI_LOG_ERROR, "[NDPI] %s(2): not enough memory\n", __FUNCTION__); #ifdef DIRECTION_BINS ndpi_free_bin(&newflow->payload_len_bin_src2dst), ndpi_free_bin(&newflow->payload_len_bin_dst2src); #else @@ -868,7 +863,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow memset(newflow->ndpi_flow, 0, SIZEOF_FLOW_STRUCT); if((newflow->src_id = ndpi_malloc(SIZEOF_ID_STRUCT)) == NULL) { - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_ERROR, "[NDPI] %s(3): not enough memory\n", __FUNCTION__); + LOG(NDPI_LOG_ERROR, "[NDPI] %s(3): not enough memory\n", __FUNCTION__); #ifdef DIRECTION_BINS ndpi_free_bin(&newflow->payload_len_bin_src2dst), ndpi_free_bin(&newflow->payload_len_bin_dst2src); #else @@ -880,7 +875,7 @@ static struct ndpi_flow_info *get_ndpi_flow_info(struct ndpi_workflow * workflow memset(newflow->src_id, 0, SIZEOF_ID_STRUCT); if((newflow->dst_id = ndpi_malloc(SIZEOF_ID_STRUCT)) == NULL) { - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_ERROR, "[NDPI] %s(4): not enough memory\n", __FUNCTION__); + LOG(NDPI_LOG_ERROR, "[NDPI] %s(4): not enough memory\n", __FUNCTION__); #ifdef DIRECTION_BINS ndpi_free_bin(&newflow->payload_len_bin_src2dst), ndpi_free_bin(&newflow->payload_len_bin_dst2src); #else @@ -1754,7 +1749,7 @@ struct ndpi_proto ndpi_workflow_process_packet(struct ndpi_workflow * workflow, if(cap_warning_used == 0) { if(!workflow->prefs.quiet_mode) - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_DEBUG, + LOG(NDPI_LOG_DEBUG, "\n\nWARNING: packet capture size is smaller than packet size, DETECTION MIGHT NOT WORK CORRECTLY\n\n"); cap_warning_used = 1; } @@ -1777,7 +1772,7 @@ struct ndpi_proto ndpi_workflow_process_packet(struct ndpi_workflow * workflow, if(ipv4_frags_warning_used == 0) { if(!workflow->prefs.quiet_mode) - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_DEBUG, "\n\nWARNING: IPv4 fragments are not handled by this demo (nDPI supports them)\n"); + LOG(NDPI_LOG_DEBUG, "\n\nWARNING: IPv4 fragments are not handled by this demo (nDPI supports them)\n"); ipv4_frags_warning_used = 1; } @@ -1811,7 +1806,7 @@ struct ndpi_proto ndpi_workflow_process_packet(struct ndpi_workflow * workflow, v4_warning: if(ipv4_warning_used == 0) { if(!workflow->prefs.quiet_mode) - NDPI_LOG(0, workflow->ndpi_struct, NDPI_LOG_DEBUG, + LOG(NDPI_LOG_DEBUG, "\n\nWARNING: only IPv4/IPv6 packets are supported in this demo (nDPI supports both IPv4 and IPv6), all other packets will be discarded\n\n"); ipv4_warning_used = 1; } diff --git a/example/reader_util.h b/example/reader_util.h index 8298e2ef8..facce0114 100644 --- a/example/reader_util.h +++ b/example/reader_util.h @@ -336,4 +336,15 @@ float ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], unsigned extern int nDPI_LogLevel; +/* TODO: conditional compilation? */ +#if 1 + #define LOG(log_level, args...) \ + { \ + if(log_level <= nDPI_LogLevel) \ + printf(args); \ + } +#else + #define LOG(...) {} +#endif + #endif -- cgit v1.2.3 From c08693fda59d9d3d6e9e55afeaf3cf739fe192a5 Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Wed, 1 Jul 2020 20:16:16 +0200 Subject: Incorporated some feedback --- example/reader_util.h | 3 +-- src/lib/ndpi_main.c | 12 +++++++----- src/lib/protocols/http.c | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) (limited to 'example') diff --git a/example/reader_util.h b/example/reader_util.h index facce0114..8e99bdbbb 100644 --- a/example/reader_util.h +++ b/example/reader_util.h @@ -336,8 +336,7 @@ float ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], unsigned extern int nDPI_LogLevel; -/* TODO: conditional compilation? */ -#if 1 +#ifdef NDPI_ENABLE_DEBUG_MESSAGES #define LOG(log_level, args...) \ { \ if(log_level <= nDPI_LogLevel) \ diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 7e7f11bf0..ec0ade60b 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -1953,10 +1953,10 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs int i; if(ndpi_str == NULL) { -#ifdef NDPI_ENABLE_DEBUG_MESSAGES - /* We can't use NDPI_LOG* functions yet! */ - fprintf(stderr, "ndpi_init_detection_module initial malloc failed for ndpi_str\n"); -#endif /* NDPI_ENABLE_DEBUG_MESSAGES */ + /* Logging this error is a bit tricky. At this point, we can't use NDPI_LOG* + functions yet, we don't have a custom log function and, as a library, + we shouldn't use stdout/stderr. Since this error is quite unlikely, + simply avoid any logs at all */ return(NULL); } @@ -2010,8 +2010,10 @@ struct ndpi_detection_module_struct *ndpi_init_detection_module(ndpi_init_prefs ndpi_str->custom_categories.ipAddresses = ndpi_New_Patricia(32 /* IPv4 */); ndpi_str->custom_categories.ipAddresses_shadow = ndpi_New_Patricia(32 /* IPv4 */); - if((ndpi_str->custom_categories.ipAddresses == NULL) || (ndpi_str->custom_categories.ipAddresses_shadow == NULL)) + if((ndpi_str->custom_categories.ipAddresses == NULL) || (ndpi_str->custom_categories.ipAddresses_shadow == NULL)) { + NDPI_LOG_ERR(ndpi_str, "[NDPI] Error allocating Patricia trees\n"); return(NULL); + } ndpi_init_protocol_defaults(ndpi_str); diff --git a/src/lib/protocols/http.c b/src/lib/protocols/http.c index 1032760d8..1d5e88365 100644 --- a/src/lib/protocols/http.c +++ b/src/lib/protocols/http.c @@ -558,6 +558,14 @@ static void check_http_payload(struct ndpi_detection_module_struct *ndpi_struct, /* ************************************************************* */ +#ifdef NDPI_ENABLE_DEBUG_MESSAGES +static uint8_t non_ctrl(uint8_t c) { + return c < 32 ? '.':c; +} +#endif + +/* ************************************************************* */ + /** * Functions to check whether the packet begins with a valid http request * @param ndpi_struct @@ -589,10 +597,10 @@ static u_int16_t http_request_url_offset(struct ndpi_detection_module_struct *nd int i; NDPI_LOG_DBG2(ndpi_struct, "====>>>> HTTP: %c%c%c%c [len: %u]\n", - packet->payload_packet_len > 0 ? packet->payload[0] : '.', - packet->payload_packet_len > 1 ? packet->payload[1] : '.', - packet->payload_packet_len > 2 ? packet->payload[2] : '.', - packet->payload_packet_len > 3 ? packet->payload[3] : '.', + packet->payload_packet_len > 0 ? non_ctrl(packet->payload[0]) : '.', + packet->payload_packet_len > 1 ? non_ctrl(packet->payload[1]) : '.', + packet->payload_packet_len > 2 ? non_ctrl(packet->payload[2]) : '.', + packet->payload_packet_len > 3 ? non_ctrl(packet->payload[3]) : '.', packet->payload_packet_len); /* Check first char */ -- cgit v1.2.3 From 974c1cc681af5a8faa6e7186a947a76a045b266f Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Thu, 2 Jul 2020 09:42:53 +0200 Subject: Improve help message of --dbg-proto option Make it clear that such option is general, not about extcap functionality --- example/ndpiReader.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'example') diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 57f8048df..1b20ac2f9 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -383,7 +383,8 @@ static void help(u_int long_help) { " | 3 = port stats\n" " -V <1-4> | nDPI logging level\n" " | 1 - trace, 2 - debug, 3 - full debug\n" - " | >3 - full debug + dbg_proto = all\n" + " | >3 - full debug + log enabled for all protocols (i.e. '-u all')\n" + " -u proto|num[,...] | Enable logging only for such protocol(s)\n" " -T | Max number of TCP processed packets before giving up [default: %u]\n" " -U | Max number of UDP processed packets before giving up [default: %u]\n" , @@ -402,7 +403,6 @@ static void help(u_int long_help) { " --extcap-capture-filter\n" " --fifo \n" " --debug\n" - " --dbg-proto proto|num[,...]\n" ); #endif @@ -434,7 +434,6 @@ static struct option longopts[] = { { "extcap-capture-filter", required_argument, NULL, '6'}, { "fifo", required_argument, NULL, '7'}, { "debug", no_argument, NULL, '8'}, - { "dbg-proto", required_argument, NULL, 257}, { "ndpi-proto-filter", required_argument, NULL, '9'}, /* ndpiReader options */ @@ -453,6 +452,7 @@ static struct option longopts[] = { { "revision", no_argument, NULL, 'r'}, { "verbose", no_argument, NULL, 'v'}, { "version", no_argument, NULL, 'V'}, + { "dbg-proto", required_argument, NULL, 'u'}, { "help", no_argument, NULL, 'h'}, { "joy", required_argument, NULL, 'J'}, { "payload-analysis", required_argument, NULL, 'P'}, @@ -660,7 +660,7 @@ static void parseOptions(int argc, char **argv) { } #endif - while((opt = getopt_long(argc, argv, "e:c:C:df:g:i:hp:P:l:s:tv:V:n:Jrp:w:q0123:456:7:89:m:T:U:", + while((opt = getopt_long(argc, argv, "e:c:C:df:g:i:hp:P:l:s:tv:V:u:n:Jrp:w:q0123:456:7:89:m:T:U:", longopts, &option_idx)) != EOF) { #ifdef DEBUG_TRACE if(trace) fprintf(trace, " #### -%c [%s] #### \n", opt, optarg ? optarg : ""); @@ -740,6 +740,10 @@ static void parseOptions(int argc, char **argv) { } break; + case 'u': + _debug_protocols = strdup(optarg); + break; + case 'h': help(1); break; @@ -823,10 +827,6 @@ static void parseOptions(int argc, char **argv) { if(extcap_packet_filter == NDPI_PROTOCOL_UNKNOWN) extcap_packet_filter = atoi(optarg); break; - case 257: - _debug_protocols = strdup(optarg); - break; - case 'T': max_num_tcp_dissected_pkts = atoi(optarg); if(max_num_tcp_dissected_pkts < 3) max_num_tcp_dissected_pkts = 3; -- cgit v1.2.3 From 030f3f3d48184133a6647108c156787fb3f39b58 Mon Sep 17 00:00:00 2001 From: Nardi Ivan Date: Tue, 7 Jul 2020 10:26:11 +0200 Subject: Fix a memory leak --- example/ndpiReader.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'example') diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 1b20ac2f9..4be142d28 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -384,7 +384,9 @@ static void help(u_int long_help) { " -V <1-4> | nDPI logging level\n" " | 1 - trace, 2 - debug, 3 - full debug\n" " | >3 - full debug + log enabled for all protocols (i.e. '-u all')\n" - " -u proto|num[,...] | Enable logging only for such protocol(s)\n" + " -u all|proto|num[,...] | Enable logging only for such protocol(s)\n" + " | If this flag is present multiple times (directly, or via '-V'),\n" + " | only the last instance will be considered\n" " -T | Max number of TCP processed packets before giving up [default: %u]\n" " -U | Max number of UDP processed packets before giving up [default: %u]\n" , @@ -736,11 +738,13 @@ static void parseOptions(int argc, char **argv) { if(nDPI_LogLevel < 0) nDPI_LogLevel = 0; if(nDPI_LogLevel > 3) { nDPI_LogLevel = 3; + free(_debug_protocols); _debug_protocols = strdup("all"); } break; case 'u': + free(_debug_protocols); _debug_protocols = strdup(optarg); break; @@ -819,6 +823,7 @@ static void parseOptions(int argc, char **argv) { case '8': nDPI_LogLevel = NDPI_LOG_DEBUG_EXTRA; + free(_debug_protocols); _debug_protocols = strdup("all"); break; @@ -3525,6 +3530,7 @@ int orginal_main(int argc, char **argv) { if(extcap_fifo_h) pcap_close(extcap_fifo_h); if(ndpi_info_mod) ndpi_exit_detection_module(ndpi_info_mod); if(csv_fp) fclose(csv_fp); + free(_debug_protocols); return 0; } -- cgit v1.2.3