aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorVitaly Lavrov <vel21ripn@gmail.com>2017-10-14 14:38:48 +0300
committerVitaly Lavrov <vel21ripn@gmail.com>2017-10-26 20:41:22 +0300
commit2787c2390cdd7129c2dcf50b0d4990d3f7d1bccc (patch)
treedb110f640b9c1de43a0a64a43974ea90776c6588 /example
parent4f72b954da705f8d54a9dd61eae46b2b36b24dc0 (diff)
Refactoring the debugging output.
levels of debug output: 0 - ERROR: Only for errors. 1 - TRACE: Start of each packets and if found protocol. 2 - DEBUG: Start of searching each protocol and excluding protocols. 3 - DEBUG_EXTRA: For all other messages. Added field ndpi_struct->debug_logging for enable debug output of each protocols. Simple macros for debugging output are added: NDPI_LOG_ERR(), NDPI_LOG_INFO(), NDPI_LOG_DBG(), NDPI_LOG_DBG2(), NDPI_EXCLUDE_PROTO()
Diffstat (limited to 'example')
-rw-r--r--example/ndpiReader.c37
-rw-r--r--example/ndpi_util.c66
-rw-r--r--example/ndpi_util.h3
3 files changed, 96 insertions, 10 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 8d8ee47b0..7a60fddb7 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -77,7 +77,9 @@ static json_object *jArray_topStats;
static u_int8_t live_capture = 0;
static u_int8_t undetected_flows_deleted = 0;
/** User preferences **/
-static u_int8_t enable_protocol_guess = 1, verbose = 0, nDPI_traceLevel = 0, json_flag = 0;
+static u_int8_t enable_protocol_guess = 1, verbose = 0, json_flag = 0;
+int nDPI_LogLevel = 0;
+char *_debug_protocols = NULL;
static u_int8_t stats_flag = 0, bpf_filter_flag = 0;
#ifdef HAVE_JSON_C
static u_int8_t file_first_time = 1;
@@ -250,9 +252,12 @@ static void help(u_int long_help) {
" | 1 = verbose\n"
" | 2 = very verbose\n"
" | 3 = port stats\n"
- " -b <file.json> | Specify a file to write port based diagnose statistics\n"
- " -x <file.json> | Produce bpf filters for specified diagnose file. Use\n"
- " | this option only for .json files generated with -b flag.\n");
+ " -V <1-4> | nDPI logging level\n"
+ " | 1 - trace, 2 - debug, 3 - full debug\n"
+ " | >3 - full debug + dbg_proto = all\n"
+ " -b <file.json> | Specify a file to write port based diagnose statistics\n"
+ " -x <file.json> | Produce bpf filters for specified diagnose file. Use\n"
+ " | this option only for .json files generated with -b flag.\n");
#ifndef WIN32
@@ -266,6 +271,7 @@ static void help(u_int long_help) {
" --extcap-capture-filter\n"
" --fifo <path to file or pipe>\n"
" --debug\n"
+ " --dbg-proto proto|num[,...]\n"
);
#endif
@@ -289,7 +295,8 @@ static struct option longopts[] = {
{ "capture", no_argument, NULL, '5'},
{ "extcap-capture-filter", required_argument, NULL, '6'},
{ "fifo", required_argument, NULL, '7'},
- { "debug", optional_argument, NULL, '8'},
+ { "debug", no_argument, NULL, '8'},
+ { "dbg-proto", required_argument, NULL, 257},
{ "ndpi-proto-filter", required_argument, NULL, '9'},
/* ndpiReader options */
@@ -519,8 +526,12 @@ static void parseOptions(int argc, char **argv) {
break;
case 'V':
- printf("%d\n",atoi(optarg) );
- nDPI_traceLevel = atoi(optarg);
+ nDPI_LogLevel = atoi(optarg);
+ if(nDPI_LogLevel < 0) nDPI_LogLevel = 0;
+ if(nDPI_LogLevel > 3) {
+ nDPI_LogLevel = 3;
+ _debug_protocols = strdup("all");
+ }
break;
case 'h':
@@ -546,6 +557,7 @@ static void parseOptions(int argc, char **argv) {
case 'q':
quiet_mode = 1;
+ nDPI_LogLevel = 0;
break;
/* Extcap */
@@ -574,12 +586,17 @@ static void parseOptions(int argc, char **argv) {
break;
case '8':
- nDPI_traceLevel = 9;
+ nDPI_LogLevel = NDPI_LOG_DEBUG_EXTRA;
+ _debug_protocols = strdup("all");
break;
case '9':
extcap_packet_filter = atoi(optarg);
break;
+
+ case 257:
+ _debug_protocols = strdup(optarg);
+ break;
default:
help(0);
@@ -985,7 +1002,6 @@ void updateTopIpAddress(u_int32_t addr, u_int8_t version, const char *proto,
int min = count;
int update = 0;
int min_i = 0;
- int r;
int i;
if(count == 0) return;
@@ -1117,6 +1133,7 @@ static struct receiver *cutBackTo(struct receiver **receivers, u_int32_t size, u
HASH_DEL(*receivers, r);
free(r);
}
+ return NULL;
}
/* *********************************************** */
@@ -1381,7 +1398,7 @@ static void debug_printf(u_int32_t protocol, void *id_struct,
struct tm result;
#endif
- if(log_level <= nDPI_traceLevel) {
+ if(log_level <= nDPI_LogLevel) {
char buf[8192], out_buf[8192];
char theDate[32];
const char *extra_msg = "";
diff --git a/example/ndpi_util.c b/example/ndpi_util.c
index d57e9e90e..11f66049c 100644
--- a/example/ndpi_util.c
+++ b/example/ndpi_util.c
@@ -105,6 +105,61 @@ static void free_wrapper(void *freeable) {
/* ***************************************************** */
+static uint16_t ndpi_get_proto_id(struct ndpi_detection_module_struct *ndpi_mod, const char *name) {
+ uint16_t proto_id;
+ char *e;
+ unsigned long p = strtol(name,&e,0);
+ if(e && !*e) {
+ if(p < NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS &&
+ ndpi_mod->proto_defaults[p].protoName) return (uint16_t)p;
+ return NDPI_PROTOCOL_UNKNOWN;
+ }
+ for(proto_id=NDPI_PROTOCOL_UNKNOWN; proto_id < NDPI_MAX_SUPPORTED_PROTOCOLS+NDPI_MAX_NUM_CUSTOM_PROTOCOLS; proto_id++) {
+ if(ndpi_mod->proto_defaults[proto_id].protoName &&
+ !strcasecmp(ndpi_mod->proto_defaults[proto_id].protoName,name))
+ return proto_id;
+ }
+ return NDPI_PROTOCOL_UNKNOWN;
+}
+static NDPI_PROTOCOL_BITMASK debug_bitmask;
+static char _proto_delim[] = " \t,:;";
+static int parse_debug_proto(struct ndpi_detection_module_struct *ndpi_mod, char *str) {
+char *n;
+uint16_t proto;
+char op=1;
+for(n = strtok(str,_proto_delim); n && *n; n = strtok(NULL,_proto_delim)) {
+ if(*n == '-') {
+ op = 0;
+ n++;
+ } else if(*n == '+') {
+ op = 1;
+ n++;
+ }
+ if(!strcmp(n,"all")) {
+ if(op)
+ NDPI_BITMASK_SET_ALL(debug_bitmask);
+ else
+ NDPI_BITMASK_RESET(debug_bitmask);
+ continue;
+ }
+ proto = ndpi_get_proto_id(ndpi_mod, n);
+ if(proto == NDPI_PROTOCOL_UNKNOWN && strcmp(n,"unknown") && strcmp(n,"0")) {
+ fprintf(stderr,"Invalid protocol %s\n",n);
+ return 1;
+ }
+ if(op)
+ NDPI_BITMASK_ADD(debug_bitmask,proto);
+ else
+ NDPI_BITMASK_DEL(debug_bitmask,proto);
+}
+return 0;
+}
+
+/* ***************************************************** */
+
+extern char *_debug_protocols;
+static int _debug_protocols_ok = 0;
+
struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * prefs, pcap_t * pcap_handle) {
set_ndpi_malloc(malloc_wrapper), set_ndpi_free(free_wrapper);
set_ndpi_flow_malloc(NULL), set_ndpi_flow_free(NULL);
@@ -121,7 +176,18 @@ struct ndpi_workflow * ndpi_workflow_init(const struct ndpi_workflow_prefs * pre
NDPI_LOG(0, NULL, NDPI_LOG_ERROR, "global structure initialization failed\n");
exit(-1);
}
+ module->ndpi_log_level = nDPI_LogLevel;
+ if(_debug_protocols != NULL && ! _debug_protocols_ok) {
+ if(parse_debug_proto(module,_debug_protocols))
+ exit(-1);
+ _debug_protocols_ok = 1;
+ }
+#ifdef NDPI_ENABLE_DEBUG_MESSAGES
+ NDPI_BITMASK_RESET(module->debug_bitmask);
+ if(_debug_protocols_ok)
+ module->debug_bitmask = debug_bitmask;
+#endif
workflow->ndpi_flows_root = ndpi_calloc(workflow->prefs.num_roots, sizeof(void *));
return workflow;
}
diff --git a/example/ndpi_util.h b/example/ndpi_util.h
index 51bc09ddb..7abebe4f5 100644
--- a/example/ndpi_util.h
+++ b/example/ndpi_util.h
@@ -179,4 +179,7 @@ int ndpi_workflow_node_cmp(const void *a, const void *b);
void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_flow_info *flow);
u_int32_t ethernet_crc32(const void* data, size_t n_bytes);
void ndpi_flow_info_freer(void *node);
+
+extern int nDPI_LogLevel;
+
#endif