Skip to content

Commit

Permalink
Improved debug output. (#1951)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
utoni authored Apr 21, 2023
1 parent 85751ba commit b6629ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
5 changes: 2 additions & 3 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions fuzz/fuzz_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <assert.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) {
FuzzedDataProvider fuzzed_data(data, size);
struct ndpi_detection_module_struct *ndpi_info_mod;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 31 additions & 32 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -8271,8 +8272,6 @@ void ndpi_generate_options(u_int opt) {
printf("WARNING: option -a out of range\n");
break;
}

exit(0);
}

/* ****************************************************** */
Expand Down Expand Up @@ -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);
}

/* ****************************************************** */
Expand Down
4 changes: 2 additions & 2 deletions src/lib/third_party/include/uthash.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -513,7 +513,7 @@ do {
*/
#ifdef HASH_DEBUG
#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) \
do { \
struct UT_hash_handle *_thh; \
Expand Down

0 comments on commit b6629ba

Please sign in to comment.