aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Deri <deri@ntop.org>2020-06-29 22:36:46 +0200
committerLuca Deri <deri@ntop.org>2020-06-29 22:36:46 +0200
commit392ce445737b0cb7469775458596b52a52198b7e (patch)
treee9824e4a4ed41cef80a072aadcdd614494988e84
parent0d2d44f1b6c89a851a6a9634d66cb42cc81b3244 (diff)
Added ndpi_print_bin() API call
-rw-r--r--example/ndpiReader.c13
-rw-r--r--src/include/ndpi_api.h.in3
-rw-r--r--src/lib/ndpi_analyze.c43
3 files changed, 55 insertions, 4 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c
index 6c851179c..8f0219caf 100644
--- a/example/ndpiReader.c
+++ b/example/ndpiReader.c
@@ -3114,7 +3114,8 @@ static void binUnitTest() {
struct ndpi_bin b1, b2;
u_int8_t num_bins = 32;
u_int32_t i;
-
+ char out_buf[128];
+
srand(time(NULL));
ndpi_init_bin(&b1, ndpi_bin_family8, num_bins), ndpi_init_bin(&b2, ndpi_bin_family8, num_bins);
@@ -3122,9 +3123,13 @@ static void binUnitTest() {
for(i=0; i<32; i++)
ndpi_inc_bin(&b1, rand() % num_bins), ndpi_inc_bin(&b2, rand() % num_bins);
- ndpi_bin_similarity(&b1, &b1, 0);
- ndpi_bin_similarity(&b1, &b2, 0);
-
+#if 0
+ printf("1) %s\n", ndpi_print_bin(&b1, 0, out_buf, sizeof(out_buf)));
+ printf("2) %s\n", ndpi_print_bin(&b2, 0, out_buf, sizeof(out_buf)));
+
+ printf("Similarity: %f\n\n", ndpi_bin_similarity(&b1, &b2, 1));
+#endif
+
ndpi_free_bin(&b1), ndpi_free_bin(&b2);
}
diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in
index 26d7d2f66..ed94c5bf3 100644
--- a/src/include/ndpi_api.h.in
+++ b/src/include/ndpi_api.h.in
@@ -1071,7 +1071,10 @@ extern "C" {
void ndpi_free_bin(struct ndpi_bin *b);
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id);
void ndpi_normalize_bin(struct ndpi_bin *b);
+ char* ndpi_print_bin(struct ndpi_bin *b, u_int8_t normalize_first, char *out_buf, u_int out_buf_len);
float ndpi_bin_similarity(struct ndpi_bin *b1, struct ndpi_bin *b2, u_int8_t normalize_first);
+
+
#ifdef __cplusplus
}
#endif
diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c
index 5c3460eaf..eaa0d30c2 100644
--- a/src/lib/ndpi_analyze.c
+++ b/src/lib/ndpi_analyze.c
@@ -324,6 +324,49 @@ void ndpi_normalize_bin(struct ndpi_bin *b) {
/* ********************************************************************************* */
+char* ndpi_print_bin(struct ndpi_bin *b, u_int8_t normalize_first, char *out_buf, u_int out_buf_len) {
+ u_int8_t i;
+ u_int len = 0;
+
+ if(!out_buf) return(out_buf); else out_buf[0] = '\0';
+
+ if(normalize_first)
+ ndpi_normalize_bin(b);
+
+ switch(b->family) {
+ case ndpi_bin_family8:
+ for(i=0; i<b->num_bins; i++) {
+ int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins8[i]);
+
+ if(rc < 0) break;
+ len += rc;
+ }
+ break;
+
+ case ndpi_bin_family16:
+ for(i=0; i<b->num_bins; i++) {
+ int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins16[i]);
+
+ if(rc < 0) break;
+ len += rc;
+ }
+ break;
+
+ case ndpi_bin_family32:
+ for(i=0; i<b->num_bins; i++) {
+ int rc = snprintf(&out_buf[len], out_buf_len-len, "%s%u", (i > 0) ? "," : "", b->u.bins32[i]);
+
+ if(rc < 0) break;
+ len += rc;
+ }
+ break;
+ }
+
+ return(out_buf);
+}
+
+/* ********************************************************************************* */
+
/*
Determines how similar are two bins