Skip to content

Commit 46ff069

Browse files
authored
ndpiReader: improve printing of payload statistics (#1989)
Add a basic unit test Fix an endianess issue
1 parent 7ce14da commit 46ff069

File tree

7 files changed

+316
-36
lines changed

7 files changed

+316
-36
lines changed

example/ndpiReader.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static int dpdk_port_id = 0, dpdk_run_capture = 1;
252252

253253
void test_lib(); /* Forward */
254254

255-
extern void ndpi_report_payload_stats(int print);
255+
extern void ndpi_report_payload_stats(FILE *out);
256256
extern int parse_proto_name_list(char *str, NDPI_PROTOCOL_BITMASK *bitmask, int inverted_logic);
257257

258258
/* ********************************** */
@@ -420,10 +420,10 @@ flowGetBDMeanandVariance(struct ndpi_flow_info* flow) {
420420
if(csv_fp) {
421421
fprintf(csv_fp, ",%.3f,%.3f,%.3f,%.3f", mean, variance, entropy, entropy * num_bytes);
422422
} else {
423-
fprintf(out, "[byte_dist_mean: %f", mean);
424-
fprintf(out, "][byte_dist_std: %f]", variance);
425-
fprintf(out, "[entropy: %f]", entropy);
426-
fprintf(out, "[total_entropy: %f]", entropy * num_bytes);
423+
fprintf(out, "[byte_dist_mean: %.3f", mean);
424+
fprintf(out, "][byte_dist_std: %.3f]", variance);
425+
fprintf(out, "[entropy: %.3f]", entropy);
426+
fprintf(out, "[total_entropy: %.3f]", entropy * num_bytes);
427427
}
428428
} else {
429429
if(csv_fp)
@@ -2747,7 +2747,7 @@ static void printFlowsStats() {
27472747
FILE *out = results_file ? results_file : stdout;
27482748

27492749
if(enable_payload_analyzer)
2750-
ndpi_report_payload_stats(1);
2750+
ndpi_report_payload_stats(out);
27512751

27522752
for(thread_id = 0; thread_id < num_threads; thread_id++)
27532753
total_flows += ndpi_thread_info[thread_id].workflow->num_allocated_flows;

example/reader_util.c

+28-27
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void ndpi_analyze_payload(struct ndpi_flow_info *flow,
126126
struct packet_id_stats *p;
127127

128128
#ifdef DEBUG_PAYLOAD
129+
u_int16_t i;
129130
for(i=0; i<payload_len; i++)
130131
printf("%c", isprint(payload[i]) ? payload[i] : '.');
131132
printf("\n");
@@ -216,68 +217,68 @@ static int payload_stats_sort_asc(void *_a, void *_b) {
216217

217218
/* ***************************************************** */
218219

219-
void print_payload_stat(struct payload_stats *p) {
220+
static void print_payload_stat(struct payload_stats *p, FILE *out) {
220221
u_int i;
221222
struct flow_id_stats *s, *tmp;
222223
struct packet_id_stats *s1, *tmp1;
223224

224-
printf("\t[");
225+
fprintf(out, "\t[");
225226

226227
for(i=0; i<p->pattern_len; i++) {
227-
printf("%c", isprint(p->pattern[i]) ? p->pattern[i] : '.');
228+
fprintf(out, "%c", isprint(p->pattern[i]) ? p->pattern[i] : '.');
228229
}
229230

230-
printf("]");
231-
for(; i<16; i++) printf(" ");
232-
printf("[");
231+
fprintf(out, "]");
232+
for(; i<16; i++) fprintf(out, " ");
233+
fprintf(out, "[");
233234

234235
for(i=0; i<p->pattern_len; i++) {
235-
printf("%s%02X", (i > 0) ? " " : "", isprint(p->pattern[i]) ? p->pattern[i] : '.');
236+
fprintf(out, "%s%02X", (i > 0) ? " " : "", isprint(p->pattern[i]) ? p->pattern[i] : '.');
236237
}
237238

238-
printf("]");
239+
fprintf(out, "]");
239240

240-
for(; i<16; i++) printf(" ");
241-
for(i=p->pattern_len; i<max_pattern_len; i++) printf(" ");
241+
for(; i<16; i++) fprintf(out, " ");
242+
for(i=p->pattern_len; i<max_pattern_len; i++) fprintf(out, " ");
242243

243-
printf("[len: %u][num_occurrencies: %u][flowId: ",
244-
p->pattern_len, p->num_occurrencies);
244+
fprintf(out, "[len: %u][num_occurrencies: %u][flowId: ",
245+
p->pattern_len, p->num_occurrencies);
245246

246247
i = 0;
247248
HASH_ITER(hh, p->flows, s, tmp) {
248-
printf("%s%u", (i > 0) ? " " : "", s->flow_id);
249+
fprintf(out, "%s%u", (i > 0) ? " " : "", s->flow_id);
249250
i++;
250251
}
251252

252-
printf("][packetIds: ");
253+
fprintf(out, "][packetIds: ");
253254

254255
/* ******************************** */
255256

256257
i = 0;
257258
HASH_ITER(hh, p->packets, s1, tmp1) {
258-
printf("%s%u", (i > 0) ? " " : "", s1->packet_id);
259+
fprintf(out, "%s%u", (i > 0) ? " " : "", s1->packet_id);
259260
i++;
260261
}
261262

262-
printf("]\n");
263+
fprintf(out, "]\n");
263264

264265

265266
}
266267

267268
/* ***************************************************** */
268269

269-
void ndpi_report_payload_stats(int print) {
270+
void ndpi_report_payload_stats(FILE *out) {
270271
struct payload_stats *p, *tmp;
271272
u_int num = 0;
272273

273-
if(print)
274-
printf("\n\nPayload Analysis\n");
274+
if(out)
275+
fprintf(out, "\n\nPayload Analysis\n");
275276

276277
HASH_SORT(pstats, payload_stats_sort_asc);
277278

278279
HASH_ITER(hh, pstats, p, tmp) {
279-
if(print && num <= max_num_reported_top_payloads)
280-
print_payload_stat(p);
280+
if(out && num <= max_num_reported_top_payloads)
281+
print_payload_stat(p, out);
281282

282283
ndpi_free(p->pattern);
283284

@@ -711,20 +712,20 @@ ndpi_flow_update_byte_dist_mean_var(ndpi_flow_info_t *flow, const void *x,
711712

712713
/* ***************************************************** */
713714

714-
float ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256],
715+
double ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256],
715716
unsigned int num_bytes)
716717
{
717718
int i;
718-
float sum = 0.0;
719+
double sum = 0.0;
719720

720721
for(i=0; i<256; i++) {
721-
float tmp = (float) byte_count[i] / (float) num_bytes;
722+
double tmp = (double) byte_count[i] / (double) num_bytes;
722723

723724
if(tmp > FLT_EPSILON) {
724725
sum -= tmp * logf(tmp);
725726
}
726727
}
727-
return(sum / logf(2.0));
728+
return(sum / log(2.0));
728729
}
729730

730731
/* ***************************************************** */
@@ -1584,15 +1585,15 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
15841585
flow->entropy->score = ndpi_classify(flow->entropy->src2dst_pkt_len, flow->entropy->src2dst_pkt_time,
15851586
flow->entropy->dst2src_pkt_len, flow->entropy->dst2src_pkt_time,
15861587
flow->entropy->src2dst_start, flow->entropy->dst2src_start,
1587-
max_num_packets_per_flow, flow->src_port, flow->dst_port,
1588+
max_num_packets_per_flow, ntohs(flow->src_port), ntohs(flow->dst_port),
15881589
flow->src2dst_packets, flow->dst2src_packets,
15891590
flow->entropy->src2dst_opackets, flow->entropy->dst2src_opackets,
15901591
flow->entropy->src2dst_l4_bytes, flow->entropy->dst2src_l4_bytes, 1,
15911592
flow->entropy->src2dst_byte_count, flow->entropy->dst2src_byte_count);
15921593
else
15931594
flow->entropy->score = ndpi_classify(flow->entropy->src2dst_pkt_len, flow->entropy->src2dst_pkt_time,
15941595
NULL, NULL, flow->entropy->src2dst_start, flow->entropy->src2dst_start,
1595-
max_num_packets_per_flow, flow->src_port, flow->dst_port,
1596+
max_num_packets_per_flow, ntohs(flow->src_port), ntohs(flow->dst_port),
15961597
flow->src2dst_packets, 0,
15971598
flow->entropy->src2dst_opackets, 0,
15981599
flow->entropy->src2dst_l4_bytes, 0, 1,

example/reader_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
396396
void ndpi_flow_info_free_data(struct ndpi_flow_info *flow);
397397
void ndpi_flow_info_freer(void *node);
398398
const char* print_cipher_id(u_int32_t cipher);
399-
float ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], unsigned int num_bytes);
399+
double ndpi_flow_get_byte_count_entropy(const uint32_t byte_count[256], unsigned int num_bytes);
400400

401401
extern int nDPI_LogLevel;
402402

fuzz/fuzz_ndpi_reader.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int malloc_size_stats = 0;
2424
int max_malloc_bins = 14;
2525
struct ndpi_bin malloc_bins; /* unused */
2626

27-
extern void ndpi_report_payload_stats(int print);
27+
extern void ndpi_report_payload_stats(FILE *out);
2828

2929
#ifdef CRYPT_FORCE_NO_AESNI
3030
extern int force_no_aesni;
@@ -152,7 +152,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
152152
ndpi_free(workflow->ndpi_flows_root);
153153
/* Free payload analyzer data, without printing */
154154
if(enable_payload_analyzer)
155-
ndpi_report_payload_stats(0);
155+
ndpi_report_payload_stats(NULL);
156156

157157
return 0;
158158
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-P 4:8:10:16:25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../default/pcap/1kxun.pcap

0 commit comments

Comments
 (0)