From b6629ba2befccec6356802fabf2d7a076133d900 Mon Sep 17 00:00:00 2001 From: Toni Date: Fri, 21 Apr 2023 12:40:26 +0200 Subject: [PATCH] Improved debug output. (#1951) * try to get rid of some `printf(..)`s as they do not belong to a shared library * replaced all `exit(..)`s with `abort()`s to indicate an abnormal process termination Signed-off-by: Toni Uhlig --- example/ndpiReader.c | 5 +-- fuzz/fuzz_config.cpp | 4 +- src/include/ndpi_api.h | 1 + src/lib/ndpi_main.c | 63 ++++++++++++++-------------- src/lib/third_party/include/uthash.h | 4 +- 5 files changed, 37 insertions(+), 40 deletions(-) diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 05f362ce909..72408409eaf 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -126,7 +126,6 @@ static struct ndpi_detection_module_struct *ndpi_info_mod = NULL; extern u_int8_t enable_doh_dot_detection; extern u_int32_t max_num_packets_per_flow, max_packet_payload_dissection, max_num_reported_top_payloads; extern u_int16_t min_pattern_len, max_pattern_len; -extern void ndpi_self_check_host_match(); /* Self check function */ u_int8_t dump_internal_stats; struct ndpi_bin malloc_bins; @@ -827,7 +826,7 @@ static void parseOptions(int argc, char **argv) { switch (opt) { case 'a': ndpi_generate_options(atoi(optarg)); - break; + exit(0); case 'A': dump_internal_stats = 1; @@ -5198,7 +5197,7 @@ int main(int argc, char **argv) { bitmapUnitTest(); automataUnitTest(); analyzeUnitTest(); - ndpi_self_check_host_match(); + ndpi_self_check_host_match(stderr); analysisUnitTest(); compressedBitmapUnitTest(); #endif diff --git a/fuzz/fuzz_config.cpp b/fuzz/fuzz_config.cpp index 177266ba782..8f98c592983 100644 --- a/fuzz/fuzz_config.cpp +++ b/fuzz/fuzz_config.cpp @@ -6,8 +6,6 @@ #include #include "fuzzer/FuzzedDataProvider.h" -extern "C" void ndpi_self_check_host_match(); /* Self check function */ - extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FuzzedDataProvider fuzzed_data(data, size); struct ndpi_detection_module_struct *ndpi_info_mod; @@ -136,7 +134,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { ndpi_get_num_supported_protocols(ndpi_info_mod); ndpi_get_ndpi_num_custom_protocols(ndpi_info_mod); - ndpi_self_check_host_match(); + ndpi_self_check_host_match(stderr); /* Basic code to try testing this "config" */ bool_value = fuzzed_data.ConsumeBool(); diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 16dc2014df1..70d9021d299 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -974,6 +974,7 @@ extern "C" { u_int16_t user_proto_id); u_int16_t ndpi_map_ndpi_id_to_user_proto_id(struct ndpi_detection_module_struct *ndpi_str, u_int16_t ndpi_proto_id); + void ndpi_self_check_host_match(FILE *error_out); /* Tells to called on what l4 protocol given application protocol can be found */ ndpi_l4_proto_info ndpi_get_l4_proto_info(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t ndpi_proto_id); diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index 8b3a09cdf05..8dd2944733a 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -611,20 +611,6 @@ static int ndpi_default_ports_tree_node_t_cmp(const void *a, const void *b) { /* ******************************************************************** */ -void ndpi_default_ports_tree_node_t_walker(const void *node, const ndpi_VISIT which, const int depth) { - ndpi_default_ports_tree_node_t *f = *(ndpi_default_ports_tree_node_t **) node; - - printf("<%d>Walk on node %s (%u)\n", depth, - which == ndpi_preorder ? - "ndpi_preorder" : - which == ndpi_postorder ? - "ndpi_postorder" : - which == ndpi_endorder ? "ndpi_endorder" : which == ndpi_leaf ? "ndpi_leaf" : "unknown", - f->default_port); -} - -/* ******************************************************************** */ - static int addDefaultPort(struct ndpi_detection_module_struct *ndpi_str, ndpi_port_range *range, ndpi_proto_defaults_t *def, @@ -875,16 +861,19 @@ void ndpi_init_protocol_match(struct ndpi_detection_module_struct *ndpi_str, /* ******************************************************************** */ /* Self check function to be called only for testing purposes */ -void ndpi_self_check_host_match() { +void ndpi_self_check_host_match(FILE *error_out) { u_int32_t i, j; for(i = 0; host_match[i].string_to_match != NULL; i++) { for(j = 0; host_match[j].string_to_match != NULL; j++) { if((i != j) && (strcmp(host_match[i].string_to_match, host_match[j].string_to_match) == 0)) { - printf("[INTERNAL ERROR]: Duplicate string detected '%s' [id: %u, id %u]\n", - host_match[i].string_to_match, i, j); - printf("\nPlease fix host_match[] in ndpi_content_match.c.inc\n"); - exit(0); + if (error_out != NULL) { + fprintf(error_out, + "[NDPI] INTERNAL ERROR duplicate string detected '%s' [id: %u, id %u]\n", + host_match[i].string_to_match, i, j); + fprintf(error_out, "\nPlease fix host_match[] in ndpi_content_match.c.inc\n"); + } + abort(); } } } @@ -895,20 +884,34 @@ void ndpi_self_check_host_match() { #define XGRAMS_C 26 static int ndpi_xgrams_inited = 0; static unsigned int bigrams_bitmap[(XGRAMS_C*XGRAMS_C+31)/32]; -static unsigned int imposible_bigrams_bitmap[(XGRAMS_C*XGRAMS_C+31)/32]; +static unsigned int impossible_bigrams_bitmap[(XGRAMS_C*XGRAMS_C+31)/32]; static unsigned int trigrams_bitmap[(XGRAMS_C*XGRAMS_C*XGRAMS_C+31)/32]; -static void ndpi_xgrams_init(unsigned int *dst,size_t dn, const char **src,size_t sn, unsigned int l) { +static void ndpi_xgrams_init(struct ndpi_detection_module_struct *ndpi_str, + unsigned int *dst, size_t dn, + const char **src, size_t sn, + unsigned int l) +{ unsigned int i,j,c; for(i=0;i < sn && src[i]; i++) { for(j=0,c=0; j < l; j++) { unsigned char a = (unsigned char)src[i][j]; - if(a < 'a' || a > 'z') { printf("%u: c%u %c\n",i,j,a); abort(); } + if(a < 'a' || a > 'z') { + NDPI_LOG_ERR(ndpi_str, + "[NDPI] INTERNAL ERROR ndpi_xgrams_init %u: c%u %c\n", + i,j,a); + abort(); + } c *= XGRAMS_C; c += a - 'a'; } - if(src[i][l]) { printf("%u: c[%d] != 0\n",i,l); abort(); } + if(src[i][l]) { + NDPI_LOG_ERR(ndpi_str, + "[NDPI] INTERNAL ERROR ndpi_xgrams_init %u: c[%d] != 0\n", + i,l); + abort(); + } if((c >> 3) >= dn) abort(); dst[c >> 5] |= 1u << (c & 0x1f); } @@ -950,12 +953,12 @@ static void init_string_based_protocols(struct ndpi_detection_module_struct *ndp if(!ndpi_xgrams_inited) { ndpi_xgrams_inited = 1; - ndpi_xgrams_init(bigrams_bitmap,sizeof(bigrams_bitmap), + ndpi_xgrams_init(ndpi_str,bigrams_bitmap,sizeof(bigrams_bitmap), ndpi_en_bigrams,sizeof(ndpi_en_bigrams)/sizeof(ndpi_en_bigrams[0]), 2); - ndpi_xgrams_init(imposible_bigrams_bitmap,sizeof(imposible_bigrams_bitmap), + ndpi_xgrams_init(ndpi_str,impossible_bigrams_bitmap,sizeof(impossible_bigrams_bitmap), ndpi_en_impossible_bigrams,sizeof(ndpi_en_impossible_bigrams)/sizeof(ndpi_en_impossible_bigrams[0]), 2); - ndpi_xgrams_init(trigrams_bitmap,sizeof(trigrams_bitmap), + ndpi_xgrams_init(ndpi_str,trigrams_bitmap,sizeof(trigrams_bitmap), ndpi_en_trigrams,sizeof(ndpi_en_trigrams)/sizeof(ndpi_en_trigrams[0]), 3); } } @@ -6184,9 +6187,7 @@ static void ndpi_add_connection_as_zoom(struct ndpi_detection_module_struct *ndp */ static void ndpi_check_tcp_flags(struct ndpi_detection_module_struct *ndpi_str, struct ndpi_flow_struct *flow) { -#if 0 - printf("[TOTAL] %u / %u [tot: %u]\n", flow->packet_direction_complete_counter[0], flow->packet_direction_complete_counter[1], flow->all_packets_counter); -#endif + // printf("[TOTAL] %u / %u [tot: %u]\n", flow->packet_direction_complete_counter[0], flow->packet_direction_complete_counter[1], flow->all_packets_counter); if((flow->l4.tcp.cli2srv_tcp_flags & TH_SYN) && (flow->l4.tcp.srv2cli_tcp_flags & TH_RST) @@ -8271,8 +8272,6 @@ void ndpi_generate_options(u_int opt) { printf("WARNING: option -a out of range\n"); break; } - - exit(0); } /* ****************************************************** */ @@ -8577,7 +8576,7 @@ int ndpi_match_bigram(const char *str) { } int ndpi_match_impossible_bigram(const char *str) { - return ndpi_match_xgram(imposible_bigrams_bitmap, 2, str); + return ndpi_match_xgram(impossible_bigrams_bitmap, 2, str); } /* ****************************************************** */ diff --git a/src/lib/third_party/include/uthash.h b/src/lib/third_party/include/uthash.h index 49c69df0301..b7dfe4d3b3c 100644 --- a/src/lib/third_party/include/uthash.h +++ b/src/lib/third_party/include/uthash.h @@ -118,7 +118,7 @@ do { /* malloc failures result in lost memory, hash tables are unusable */ #ifndef uthash_fatal -#define uthash_fatal(msg) exit(-1) /* fatal OOM error */ +#define uthash_fatal(msg) abort() /* fatal OOM error */ #endif #define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory") @@ -513,7 +513,7 @@ do { */ #ifdef HASH_DEBUG #include /* fprintf, stderr */ -#define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0) +#define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); abort(); } while (0) #define HASH_FSCK(hh,head,where) \ do { \ struct UT_hash_handle *_thh; \