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]>
  • Loading branch information
utoni committed May 12, 2023
1 parent 8c224b4 commit 3636d9b
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 24 deletions.
6 changes: 3 additions & 3 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,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 @@ -827,7 +827,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
9 changes: 5 additions & 4 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,21 +706,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
2 changes: 2 additions & 0 deletions src/include/ndpi_patricia_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#ifndef _NDPI_PATRICIA_TYPEDEF_H_
#define _NDPI_PATRICIA_TYPEDEF_H_

#include <netinet/in.h>

#define UV16_MAX_USER_VALUES 2

struct patricia_uv16 {
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
2 changes: 2 additions & 0 deletions src/lib/ndpi_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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
2 changes: 2 additions & 0 deletions src/lib/ndpi_classify.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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
31 changes: 16 additions & 15 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,7 @@ void ndpi_debug_printf(unsigned int proto, struct ndpi_detection_module_struct *
ndpi_vsnprintf(str, sizeof(str) - 1, format, args);
va_end(args);

if(ndpi_str != NULL) {
if(ndpi_str != NULL || (file_name != NULL && func_name != NULL)) {
printf("%s:%s:%-3d - [%u]: %s", file_name, func_name, line_number, proto, str);
} else {
printf("Proto: %u, %s", proto, str);
Expand Down Expand Up @@ -8322,13 +8322,13 @@ int ndpi_get_category_id(struct ndpi_detection_module_struct *ndpi_str, char *ca

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

void ndpi_dump_protocols(struct ndpi_detection_module_struct *ndpi_str) {
void ndpi_dump_protocols(struct ndpi_detection_module_struct *ndpi_str, FILE *dump_out) {
int i;

if(!ndpi_str) return;
if(!ndpi_str || !dump_out) return;

for(i = 0; i < (int) ndpi_str->ndpi_num_supported_protocols; i++)
printf("%3d %-22s %-10s %-8s %-12s %s\n",
fprintf(dump_out, "%3d %-22s %-10s %-8s %-12s %s\n",
i, ndpi_str->proto_defaults[i].protoName,
ndpi_get_l4_proto_name(ndpi_get_l4_proto_info(ndpi_str, i)),
ndpi_str->proto_defaults[i].isAppProtocol ? "" : "X",
Expand All @@ -8340,11 +8340,12 @@ void ndpi_dump_protocols(struct ndpi_detection_module_struct *ndpi_str) {

/* Helper function used to generate Options fields in OPNsense */

void ndpi_generate_options(u_int opt) {
void ndpi_generate_options(u_int opt, FILE *options_out) {
struct ndpi_detection_module_struct *ndpi_str;
NDPI_PROTOCOL_BITMASK all;
u_int i;

if (!options_out) return;
ndpi_str = ndpi_init_detection_module(ndpi_no_prefs);

NDPI_BITMASK_SET_ALL(all);
Expand All @@ -8354,8 +8355,8 @@ void ndpi_generate_options(u_int opt) {
case 0: /* List known protocols */
{
for(i = 1 /* Skip unknown */; i < ndpi_str->ndpi_num_supported_protocols; i++) {
printf(" <Option%d value=\"%u\">%s</Option%d>\n",
i, i, ndpi_str->proto_defaults[i].protoName, i);
fprintf(options_out, " <Option%d value=\"%u\">%s</Option%d>\n",
i, i, ndpi_str->proto_defaults[i].protoName, i);
}
}
break;
Expand All @@ -8366,8 +8367,8 @@ void ndpi_generate_options(u_int opt) {
const char *name = ndpi_category_get_name(ndpi_str, i);

if((name != NULL) && (name[0] != '\0')) {
printf(" <Option%d value=\"%u\">%s</Option%d>\n",
i, i, name, i);
fprintf(options_out, " <Option%d value=\"%u\">%s</Option%d>\n",
i, i, name, i);
}
}
}
Expand All @@ -8376,26 +8377,26 @@ void ndpi_generate_options(u_int opt) {
case 2: /* List known risks */
{
for(i = 1 /* Skip no risk */; i < NDPI_MAX_RISK; i++) {
ndpi_risk_enum r = (ndpi_risk_enum)i;
ndpi_risk_enum r = (ndpi_risk_enum)i;

printf(" <Option%d value=\"%u\">%s</Option%d>\n",
i, i, ndpi_risk2str(r), i);
fprintf(options_out, " <Option%d value=\"%u\">%s</Option%d>\n",
i, i, ndpi_risk2str(r), i);
}
}
break;

default:
printf("WARNING: option -a out of range\n");
fprintf(options_out, "%s\n", "WARNING: option -a out of range");
break;
}
}

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

void ndpi_dump_risks_score() {
void ndpi_dump_risks_score(FILE *risk_out) {
u_int i;

printf("%3s %-48s %-8s %s %-8s %-8s\n",
fprintf(risk_out, "%3s %-48s %-8s %s %-8s %-8s\n",
"Id", "Risk", "Severity", "Score", "CliScore", "SrvScore");

for(i = 1; i < NDPI_MAX_RISK; i++) {
Expand Down
56 changes: 56 additions & 0 deletions src/lib/ndpi_replace_printf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* ndpi_replace_printf.h
*
* Copyright (C) 2023 - ntop.org and contributors
*
* This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/

// This file may be included in every *.c file that uses printf(...) except for ndpi_main.c !

#include "ndpi_config.h"

#ifndef NDPI_CFFI_PREPROCESSING

#undef printf
#undef fprintf

#include "ndpi_typedefs.h"

#ifdef NDPI_ENABLE_DEBUG_MESSAGES

#define printf(...) ndpi_debug_printf(0, NULL, NDPI_LOG_DEBUG_EXTRA, __FILE__, __func__, __LINE__, __VA_ARGS__)

#ifdef NDPI_REPLACE_FPRINTF
#define fprintf(stream, ...) ndpi_debug_printf(0, NULL, NDPI_LOG_ERROR, __FILE__, __func__, __LINE__, __VA_ARGS__)
#endif

#else

#define printf(...)

#ifdef NDPI_REPLACE_FPRINTF
#define fprintf(stream, ...)
#endif

#endif

void ndpi_debug_printf(unsigned int proto, struct ndpi_detection_module_struct *ndpi_str, ndpi_log_level_t log_level,
const char *file_name, const char *func_name, int line_number, const char *format, ...);

#endif
2 changes: 2 additions & 0 deletions src/lib/ndpi_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "third_party/include/uthash.h"
#include "third_party/include/rce_injection.h"

#include "ndpi_replace_printf.h"

#define NDPI_CONST_GENERIC_PROTOCOL_NAME "GenericProtocol"

// #define MATCH_DEBUG 1
Expand Down
2 changes: 2 additions & 0 deletions src/lib/third_party/src/ahocorasick.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ typedef __kernel_size_t size_t;
#include "ndpi_api.h"
#include "ahocorasick.h"

#include "ndpi_replace_printf.h"

/* TODO: For different depth of node, number of outgoing edges differs
considerably, It is efficient to use different chunk size for
different depths */
Expand Down
3 changes: 3 additions & 0 deletions src/lib/third_party/src/roaring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4293,6 +4293,9 @@ int run_run_container_ixor(
#include <stdbool.h>
#include <stdio.h>

#define NDPI_REPLACE_FPRINTF
#include "ndpi_replace_printf.h"


#ifdef __cplusplus
extern "C" { namespace roaring { namespace internal {
Expand Down

0 comments on commit 3636d9b

Please sign in to comment.