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 <matzeton@googlemail.com>
This commit is contained in:
Toni
2023-04-21 12:40:26 +02:00
committed by GitHub
parent 85751ba9d7
commit b6629ba2be
5 changed files with 37 additions and 40 deletions
+2 -3
View File
@@ -126,7 +126,6 @@ static struct ndpi_detection_module_struct *ndpi_info_mod = NULL;
extern u_int8_t enable_doh_dot_detection; 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_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 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; u_int8_t dump_internal_stats;
struct ndpi_bin malloc_bins; struct ndpi_bin malloc_bins;
@@ -827,7 +826,7 @@ static void parseOptions(int argc, char **argv) {
switch (opt) { switch (opt) {
case 'a': case 'a':
ndpi_generate_options(atoi(optarg)); ndpi_generate_options(atoi(optarg));
break; exit(0);
case 'A': case 'A':
dump_internal_stats = 1; dump_internal_stats = 1;
@@ -5198,7 +5197,7 @@ int main(int argc, char **argv) {
bitmapUnitTest(); bitmapUnitTest();
automataUnitTest(); automataUnitTest();
analyzeUnitTest(); analyzeUnitTest();
ndpi_self_check_host_match(); ndpi_self_check_host_match(stderr);
analysisUnitTest(); analysisUnitTest();
compressedBitmapUnitTest(); compressedBitmapUnitTest();
#endif #endif
+1 -3
View File
@@ -6,8 +6,6 @@
#include <assert.h> #include <assert.h>
#include "fuzzer/FuzzedDataProvider.h" #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) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fuzzed_data(data, size); FuzzedDataProvider fuzzed_data(data, size);
struct ndpi_detection_module_struct *ndpi_info_mod; 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_num_supported_protocols(ndpi_info_mod);
ndpi_get_ndpi_num_custom_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" */ /* Basic code to try testing this "config" */
bool_value = fuzzed_data.ConsumeBool(); bool_value = fuzzed_data.ConsumeBool();
+1
View File
@@ -974,6 +974,7 @@ extern "C" {
u_int16_t user_proto_id); 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_map_ndpi_id_to_user_proto_id(struct ndpi_detection_module_struct *ndpi_str,
u_int16_t ndpi_proto_id); 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 */ /* 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); ndpi_l4_proto_info ndpi_get_l4_proto_info(struct ndpi_detection_module_struct *ndpi_struct, u_int16_t ndpi_proto_id);
+31 -32
View File
@@ -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, static int addDefaultPort(struct ndpi_detection_module_struct *ndpi_str,
ndpi_port_range *range, ndpi_port_range *range,
ndpi_proto_defaults_t *def, 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 */ /* 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; u_int32_t i, j;
for(i = 0; host_match[i].string_to_match != NULL; i++) { for(i = 0; host_match[i].string_to_match != NULL; i++) {
for(j = 0; host_match[j].string_to_match != NULL; j++) { 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)) { 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", if (error_out != NULL) {
host_match[i].string_to_match, i, j); fprintf(error_out,
printf("\nPlease fix host_match[] in ndpi_content_match.c.inc\n"); "[NDPI] INTERNAL ERROR duplicate string detected '%s' [id: %u, id %u]\n",
exit(0); 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 #define XGRAMS_C 26
static int ndpi_xgrams_inited = 0; static int ndpi_xgrams_inited = 0;
static unsigned int bigrams_bitmap[(XGRAMS_C*XGRAMS_C+31)/32]; 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 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; unsigned int i,j,c;
for(i=0;i < sn && src[i]; i++) { for(i=0;i < sn && src[i]; i++) {
for(j=0,c=0; j < l; j++) { for(j=0,c=0; j < l; j++) {
unsigned char a = (unsigned char)src[i][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 *= XGRAMS_C;
c += a - 'a'; 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(); if((c >> 3) >= dn) abort();
dst[c >> 5] |= 1u << (c & 0x1f); 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) { if(!ndpi_xgrams_inited) {
ndpi_xgrams_inited = 1; 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_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_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); 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, static void ndpi_check_tcp_flags(struct ndpi_detection_module_struct *ndpi_str,
struct ndpi_flow_struct *flow) { 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);
printf("[TOTAL] %u / %u [tot: %u]\n", flow->packet_direction_complete_counter[0], flow->packet_direction_complete_counter[1], flow->all_packets_counter);
#endif
if((flow->l4.tcp.cli2srv_tcp_flags & TH_SYN) if((flow->l4.tcp.cli2srv_tcp_flags & TH_SYN)
&& (flow->l4.tcp.srv2cli_tcp_flags & TH_RST) && (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"); printf("WARNING: option -a out of range\n");
break; break;
} }
exit(0);
} }
/* ****************************************************** */ /* ****************************************************** */
@@ -8577,7 +8576,7 @@ int ndpi_match_bigram(const char *str) {
} }
int ndpi_match_impossible_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);
} }
/* ****************************************************** */ /* ****************************************************** */
+2 -2
View File
@@ -118,7 +118,7 @@ do {
/* malloc failures result in lost memory, hash tables are unusable */ /* malloc failures result in lost memory, hash tables are unusable */
#ifndef uthash_fatal #ifndef uthash_fatal
#define uthash_fatal(msg) exit(-1) /* fatal OOM error */ #define uthash_fatal(msg) abort() /* fatal OOM error */
#endif #endif
#define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory") #define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory")
@@ -513,7 +513,7 @@ do {
*/ */
#ifdef HASH_DEBUG #ifdef HASH_DEBUG
#include <stdio.h> /* fprintf, stderr */ #include <stdio.h> /* 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) \ #define HASH_FSCK(hh,head,where) \
do { \ do { \
struct UT_hash_handle *_thh; \ struct UT_hash_handle *_thh; \