Skip to content

Commit

Permalink
Added printf/fprintf replacement for some internal modules.
Browse files Browse the repository at this point in the history
 * logging is instead redirected to `ndpi_debug_printf`

Signed-off-by: lns <[email protected]>
Signed-off-by: Toni Uhlig <[email protected]>
  • Loading branch information
utoni committed Aug 5, 2023
1 parent 5019022 commit 58150d0
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 141 deletions.
47 changes: 4 additions & 43 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#else
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/mman.h>
#endif
Expand Down Expand Up @@ -610,10 +611,10 @@ static void help(u_int long_help) {
NDPI_BITMASK_SET_ALL(all);
ndpi_set_protocol_detection_bitmask2(ndpi_info_mod, &all);

ndpi_dump_protocols(ndpi_info_mod);
ndpi_dump_protocols(ndpi_info_mod, stdout);

printf("\n\nnDPI supported risks:\n");
ndpi_dump_risks_score();
ndpi_dump_risks_score(stdout);

ndpi_exit_detection_module(ndpi_info_mod);
}
Expand Down Expand Up @@ -973,7 +974,7 @@ static void parseOptions(int argc, char **argv) {

switch (opt) {
case 'a':
ndpi_generate_options(atoi(optarg));
ndpi_generate_options(atoi(optarg), stdout);
exit(0);

case 'A':
Expand Down Expand Up @@ -2580,46 +2581,6 @@ static void node_idle_scan_walker(const void *node, ndpi_VISIT which, int depth,

/* *********************************************** */

#if 0
/**
* @brief Print debug
*/
static void debug_printf(u_int32_t protocol, void *id_struct,
ndpi_log_level_t log_level,
const char *format, ...) {
va_list va_ap;
struct tm result;

if(log_level <= nDPI_LogLevel) {
char buf[8192], out_buf[8192];
char theDate[32];
const char *extra_msg = "";
time_t theTime = time(NULL);

va_start (va_ap, format);

if(log_level == NDPI_LOG_ERROR)
extra_msg = "ERROR: ";
else if(log_level == NDPI_LOG_TRACE)
extra_msg = "TRACE: ";
else
extra_msg = "DEBUG: ";

memset(buf, 0, sizeof(buf));
strftime(theDate, 32, "%d/%b/%Y %H:%M:%S", localtime_r(&theTime,&result));
ndpi_snprintf(buf, sizeof(buf)-1, format, va_ap);

ndpi_snprintf(out_buf, sizeof(out_buf), "%s %s%s", theDate, extra_msg, buf);
printf("%s", out_buf);
fflush(stdout);
}

va_end(va_ap);
}
#endif

/* *********************************************** */

/**
* @brief Setup for detection begin
*/
Expand Down
1 change: 1 addition & 0 deletions example/reader_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#else
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#endif

#include "reader_util.h"
Expand Down
9 changes: 5 additions & 4 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,21 +728,22 @@ extern "C" {
*
* @par ndpi_mod = the detection module
*/
void ndpi_dump_protocols(struct ndpi_detection_module_struct *mod);
void ndpi_dump_protocols(struct ndpi_detection_module_struct *mod, FILE *dump_out);

/**
* Generate Options list used in OPNsense firewall plugin
*
* @par opt = The Option list to generate
* @par dump_out = Output stream for generated options
*/
void ndpi_generate_options(u_int opt);
void ndpi_generate_options(u_int opt, FILE *dump_out);

/**
* Write the list of the scores and their associated risks
*
* @par ndpi_mod = the detection module
* @par dump_out = Output stream for dumped risk scores
*/
void ndpi_dump_risks_score(void);
void ndpi_dump_risks_score(FILE *dump_out);

/**
* Read a file and load the protocols
Expand Down
40 changes: 23 additions & 17 deletions src/include/ndpi_define.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@
#define MAX_DEFAULT_PORTS 5

#ifdef NDPI_ENABLE_DEBUG_MESSAGES
#define NDPI_LOG(proto, m, log_level, args...) \
#define NDPI_LOG(proto, m, log_level, log_facility, args...) \
{ \
struct ndpi_detection_module_struct *mod = (struct ndpi_detection_module_struct*) m; \
if(mod && mod->ndpi_log_level >= log_level) { \
if(mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(proto, mod, log_level, __FILE__, __FUNCTION__, __LINE__, args); \
if(mod != NULL && mod->ndpi_log_level >= log_level) { \
if(mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(proto, mod, log_level, log_facility, __FILE__, __FUNCTION__, __LINE__, args); \
} \
}

Expand All @@ -186,38 +186,43 @@
#endif

#define NDPI_LOG_ERR(mod, args...) \
if(mod && mod->ndpi_log_level >= NDPI_LOG_ERROR) { \
if(mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_ERROR , __FILE__, __FUNCTION__, __LINE__, args); \
if(mod != NULL && mod->ndpi_log_level >= NDPI_LOG_ERROR) { \
if(mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_ERROR , NDPI_FACILITY_CORE , __FILE__, __FUNCTION__, __LINE__, args); \
}

#define NDPI_LOG_INFO(mod, args...) \
if(mod && mod->ndpi_log_level >= NDPI_LOG_TRACE) { \
if(mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_TRACE , __FILE__, __FUNCTION__, __LINE__, args); \
if(mod != NULL && mod->ndpi_log_level >= NDPI_LOG_TRACE) { \
if(mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_TRACE , NDPI_FACILITY_CORE , __FILE__, __FUNCTION__, __LINE__, args); \
}

#define NDPI_LOG_DBG(mod, args...) \
if(mod && mod->ndpi_log_level >= NDPI_LOG_DEBUG) { \
if(mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_DEBUG , __FILE__, __FUNCTION__, __LINE__, args); \
if(mod != NULL && mod->ndpi_log_level >= NDPI_LOG_DEBUG) { \
if(mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_DEBUG , NDPI_FACILITY_CORE , __FILE__, __FUNCTION__, __LINE__, args); \
}

#define NDPI_LOG_DBG2(mod, args...) \
if(mod && mod->ndpi_log_level >= NDPI_LOG_DEBUG_EXTRA) { \
if(mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_DEBUG_EXTRA , __FILE__, __FUNCTION__, __LINE__, args); \
if(mod != NULL && mod->ndpi_log_level >= NDPI_LOG_DEBUG_EXTRA) { \
if(mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_DEBUG_EXTRA , NDPI_FACILITY_CORE , __FILE__, __FUNCTION__, __LINE__, args); \
}

#define NDPI_LOG_DGA(mod, args...) \
if(ndpi_verbose_dga_detection && mod != NULL && mod->ndpi_debug_printf != NULL) \
(*(mod->ndpi_debug_printf))(NDPI_CURRENT_PROTO, mod, NDPI_LOG_DEBUG_EXTRA , NDPI_FACILITY_DGA , __FILE__, __FUNCTION__, __LINE__, args);

#else /* not defined NDPI_ENABLE_DEBUG_MESSAGES */
# ifdef WIN32
# define NDPI_LOG(...) {}
# define NDPI_LOG_ERR(...) {}
# define NDPI_LOG_INFO(...) {}
# define NDPI_LOG_DBG(...) {}
# define NDPI_LOG_DBG2(...) {}
# define NDPI_LOG_DGA(...) {}
# else
# define NDPI_LOG(proto, mod, log_level, args...) { /* printf(args); */ }
# define NDPI_LOG(proto, mod, log_level, log_facility, args...) { /* printf(args); */ }
# ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
# define NDPI_LOG_ERR(mod, args...) { printf(args); }
# else
Expand All @@ -226,6 +231,7 @@
# define NDPI_LOG_INFO(mod, args...) { /* printf(args); */ }
# define NDPI_LOG_DBG(mod, args...) { /* printf(args); */ }
# define NDPI_LOG_DBG2(mod, args...) { /* printf(args); */ }
# define NDPI_LOG_DGA(mod, args...) { /* printf(args); */ }
# endif
#endif /* NDPI_ENABLE_DEBUG_MESSAGES */

Expand Down
3 changes: 3 additions & 0 deletions src/include/ndpi_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@
#include <sys/param.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifndef __APPLE__
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#endif

#if !defined __APPLE__ && !defined __FreeBSD__ && !defined __NetBSD__ && !defined __OpenBSD__
#include <endian.h>
Expand Down
4 changes: 4 additions & 0 deletions src/include/ndpi_patricia_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
#ifndef _NDPI_PATRICIA_TYPEDEF_H_
#define _NDPI_PATRICIA_TYPEDEF_H_

#ifndef NDPI_CFFI_PREPROCESSING
#include "ndpi_includes.h"
#endif

#define UV16_MAX_USER_VALUES 2

struct patricia_uv16 {
Expand Down
11 changes: 10 additions & 1 deletion src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#define __NDPI_TYPEDEFS_H__

#include "ndpi_define.h"
#ifndef NDPI_CFFI_PREPROCESSING
#include "ndpi_includes.h"
#endif
#include "ndpi_protocol_ids.h"
#include "ndpi_utils.h"

Expand Down Expand Up @@ -53,6 +56,11 @@ typedef enum {
NDPI_LOG_DEBUG_EXTRA
} ndpi_log_level_t;

typedef enum {
NDPI_FACILITY_CORE,
NDPI_FACILITY_DGA
} ndpi_log_facility_t;

typedef enum {
ndpi_multimedia_unknown_flow = 0,
ndpi_multimedia_audio_flow,
Expand Down Expand Up @@ -247,7 +255,8 @@ struct ndpi_detection_module_struct;

/* NDPI_DEBUG_FUNCTION_PTR (cast) */
typedef void (*ndpi_debug_function_ptr) (u_int32_t protocol, struct ndpi_detection_module_struct *module_struct,
ndpi_log_level_t log_level, const char *file,
ndpi_log_level_t log_level, ndpi_log_facility_t log_facility,
const char *file,
const char *func, unsigned line,
const char *format, ...);

Expand Down
3 changes: 3 additions & 0 deletions src/include/ndpi_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#define __NDPI_UTILS_H__

#include "ndpi_define.h"
#ifndef NDPI_CFFI_PREPROCESSING
#include "ndpi_includes.h"
#endif

#ifndef NDPI_CFFI_PREPROCESSING
extern u_int8_t ndpi_ends_with(char *str, char *ends);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ includedir = @includedir@/ndpi
ifneq ($(OS),Windows_NT)
CFLAGS += -fPIC -DPIC
endif
CFLAGS += -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ @ADDITIONAL_INCS@
CFLAGS += -I. -I../include -Ithird_party/include -DNDPI_LIB_COMPILATION @NDPI_CFLAGS@ @GPROF_CFLAGS@ @CUSTOM_NDPI@ @ADDITIONAL_INCS@
LDFLAGS += @NDPI_LDFLAGS@
LIBS = @ADDITIONAL_LIBS@ @LIBS@ @GPROF_LIBS@

OBJECTS = $(patsubst protocols/%.c, protocols/%.o, $(wildcard protocols/*.c)) $(patsubst third_party/src/%.c, third_party/src/%.o, $(wildcard third_party/src/*.c)) $(patsubst ./%.c, ./%.o, $(wildcard ./*.c))
HEADERS = $(wildcard ../include/*.h)
HEADERS = $(wildcard *.h) $(wildcard ../include/*.h)
NDPI_VERSION_MAJOR = @NDPI_MAJOR@
NDPI_LIB_STATIC = libndpi.a
NDPI_LIB_SHARED_BASE = libndpi.so
Expand Down
3 changes: 3 additions & 0 deletions src/lib/ndpi_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "ndpi_api.h"
#include "ndpi_config.h"

#include "ndpi_replace_printf.h"

/* ********************************************************************************* */

void ndpi_init_data_analysis(struct ndpi_analyze_struct *ret, u_int16_t _max_series_len) {
Expand Down Expand Up @@ -728,6 +730,7 @@ int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
float *bin_score;
u_int16_t num_cluster_elems[MAX_NUM_CLUSTERS] = { 0 };

(void)out_buf;
srand(time(NULL));

if(!bins || num_bins == 0 || !cluster_ids || num_clusters == 0)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/ndpi_classify.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include "ndpi_classify.h"
#include "ndpi_includes.h"

#include "ndpi_replace_printf.h"

/** finds the minimum value between to inputs */
#ifndef min
#define min(a,b) \
Expand Down
Loading

0 comments on commit 58150d0

Please sign in to comment.