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.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'example/reader_util.h') 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/reader_util.h') 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