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 21, 2023
1 parent 5e8f93c commit 9552a47
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 27 deletions.
7 changes: 4 additions & 3 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -547,10 +548,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 +828,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
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 @@ -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
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
9 changes: 9 additions & 0 deletions src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@
#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"

/* Used by both nDPI core and patricia code under third-party */
#include "ndpi_patricia_typedefs.h"

#ifdef __APPLE__
typedef unsigned char u_char;
typedef unsigned short u_short;
typedef unsigned int u_int;
#endif

/* NDPI_LOG_LEVEL */
typedef enum {
NDPI_LOG_ERROR,
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 @@ -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 Expand Up @@ -727,6 +729,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 @@ -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
41 changes: 25 additions & 16 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
#include <errno.h>
#include <sys/types.h>

#ifdef __APPLE__
#include <netinet/ip.h>
#endif

#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_UNKNOWN

#include "ndpi_config.h"
Expand All @@ -35,7 +39,7 @@
#ifdef USE_HOST_LIBGCRYPT
#include <gcrypt.h>
#else
#include <gcrypt_light.h>
#include "gcrypt_light.h"
#endif

#include <time.h>
Expand Down Expand Up @@ -2102,7 +2106,11 @@ static void ndpi_init_protocol_defaults(struct ndpi_detection_module_struct *ndp

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

#ifdef NDPI_ENABLE_DEBUG_MESSAGES
#define MATCH_DEBUG_INFO(fmt, ...) if(txt->option & AC_FEATURE_DEBUG) printf(fmt, ##__VA_ARGS__)
#else
#define MATCH_DEBUG_INFO(fmt, ...)
#endif

/* No static because it is used by fuzzer, too */
int ac_domain_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
Expand Down Expand Up @@ -2588,7 +2596,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 @@ -8309,13 +8317,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 @@ -8327,11 +8335,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 @@ -8341,8 +8350,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 @@ -8353,8 +8362,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 @@ -8363,26 +8372,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(...) do {} while(0);

#ifdef NDPI_REPLACE_FPRINTF
#define fprintf(stream, ...) do {} while(0);
#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
5 changes: 5 additions & 0 deletions src/lib/third_party/include/roaring.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,13 @@ static inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t* r,

roaring_bitmap_t *roaring_bitmap_add_offset(const roaring_bitmap_t *bm,
int64_t offset);

#ifdef NDPI_ENABLE_DEBUG_MESSAGES
/**
* Describe the inner structure of the bitmap.
*/
void roaring_bitmap_printf_describe(const roaring_bitmap_t *r);
#endif

/**
* Creates a new bitmap from a list of uint32_t integers
Expand All @@ -286,10 +289,12 @@ roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r);
bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
const roaring_bitmap_t *src);

#ifdef NDPI_ENABLE_DEBUG_MESSAGES
/**
* Print the content of the bitmap.
*/
void roaring_bitmap_printf(const roaring_bitmap_t *r);
#endif

/**
* Computes the intersection between two bitmaps and returns new bitmap. The
Expand Down
Loading

0 comments on commit 9552a47

Please sign in to comment.