Added ndpi_cluster_bins() for clustering bins and ancillary functions for bins manipulation
This commit is contained in:
+50
-23
@@ -67,9 +67,9 @@ static char *results_path = NULL;
|
|||||||
static char * bpfFilter = NULL; /**< bpf filter */
|
static char * bpfFilter = NULL; /**< bpf filter */
|
||||||
static char *_protoFilePath = NULL; /**< Protocol file path */
|
static char *_protoFilePath = NULL; /**< Protocol file path */
|
||||||
static char *_customCategoryFilePath= NULL; /**< Custom categories file path */
|
static char *_customCategoryFilePath= NULL; /**< Custom categories file path */
|
||||||
static FILE *csv_fp = NULL; /**< for CSV export */
|
|
||||||
static u_int8_t live_capture = 0;
|
static u_int8_t live_capture = 0;
|
||||||
static u_int8_t undetected_flows_deleted = 0;
|
static u_int8_t undetected_flows_deleted = 0;
|
||||||
|
FILE *csv_fp = NULL; /**< for CSV export */
|
||||||
/** User preferences **/
|
/** User preferences **/
|
||||||
u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0;
|
u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0;
|
||||||
u_int8_t verbose = 0, enable_joy_stats = 0;
|
u_int8_t verbose = 0, enable_joy_stats = 0;
|
||||||
@@ -621,8 +621,10 @@ void printCSVHeader() {
|
|||||||
fprintf(csv_fp, "tls_version,ja3c,tls_client_unsafe,");
|
fprintf(csv_fp, "tls_version,ja3c,tls_client_unsafe,");
|
||||||
fprintf(csv_fp, "ja3s,tls_server_unsafe,");
|
fprintf(csv_fp, "ja3s,tls_server_unsafe,");
|
||||||
fprintf(csv_fp, "tls_alpn,tls_supported_versions,");
|
fprintf(csv_fp, "tls_alpn,tls_supported_versions,");
|
||||||
|
#if 0
|
||||||
fprintf(csv_fp, "tls_issuerDN,tls_subjectDN,");
|
fprintf(csv_fp, "tls_issuerDN,tls_subjectDN,");
|
||||||
fprintf(csv_fp, "ssh_client_hassh,ssh_server_hassh,flow_info");
|
#endif
|
||||||
|
fprintf(csv_fp, "ssh_client_hassh,ssh_server_hassh,flow_info,plen_bins");
|
||||||
|
|
||||||
/* Joy */
|
/* Joy */
|
||||||
if(enable_joy_stats) {
|
if(enable_joy_stats) {
|
||||||
@@ -996,32 +998,33 @@ static char* is_unsafe_cipher(ndpi_cipher_weakness c) {
|
|||||||
|
|
||||||
/* ********************************** */
|
/* ********************************** */
|
||||||
|
|
||||||
void print_bin(const char *label, struct ndpi_bin *b) {
|
void print_bin(FILE *fout, const char *label, struct ndpi_bin *b) {
|
||||||
if(b->num_incs == 0)
|
if(b->num_incs == 0)
|
||||||
return;
|
return;
|
||||||
else {
|
else {
|
||||||
u_int8_t i;
|
u_int8_t i;
|
||||||
FILE *out = results_file ? results_file : stdout;
|
FILE *out = results_file ? results_file : stdout;
|
||||||
|
const char *sep = label ? "," : ";";
|
||||||
|
|
||||||
ndpi_normalize_bin(b);
|
ndpi_normalize_bin(b);
|
||||||
|
|
||||||
fprintf(out, "[%s: ", label);
|
if(label) fprintf(fout, "[%s: ", label);
|
||||||
|
|
||||||
for(i=0; i<b->num_bins; i++) {
|
for(i=0; i<b->num_bins; i++) {
|
||||||
switch(b->family) {
|
switch(b->family) {
|
||||||
case ndpi_bin_family8:
|
case ndpi_bin_family8:
|
||||||
fprintf(out, "%s%u", (i > 0) ? "," : "", b->u.bins8[i]);
|
fprintf(fout, "%s%u", (i > 0) ? sep : "", b->u.bins8[i]);
|
||||||
break;
|
break;
|
||||||
case ndpi_bin_family16:
|
case ndpi_bin_family16:
|
||||||
fprintf(out, "%s%u", (i > 0) ? "," : "", b->u.bins16[i]);
|
fprintf(fout, "%s%u", (i > 0) ? sep : "", b->u.bins16[i]);
|
||||||
break;
|
break;
|
||||||
case ndpi_bin_family32:
|
case ndpi_bin_family32:
|
||||||
fprintf(out, "%s%u", (i > 0) ? "," : "", b->u.bins32[i]);
|
fprintf(fout, "%s%u", (i > 0) ? sep : "", b->u.bins32[i]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(out, "]");
|
if(label) fprintf(fout, "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1139,16 +1142,22 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
|
|||||||
flow->ssh_tls.tls_alpn ? flow->ssh_tls.tls_alpn : "",
|
flow->ssh_tls.tls_alpn ? flow->ssh_tls.tls_alpn : "",
|
||||||
flow->ssh_tls.tls_supported_versions ? flow->ssh_tls.tls_supported_versions : ""
|
flow->ssh_tls.tls_supported_versions ? flow->ssh_tls.tls_supported_versions : ""
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#if 0
|
||||||
fprintf(csv_fp, "%s,%s,",
|
fprintf(csv_fp, "%s,%s,",
|
||||||
flow->ssh_tls.tls_issuerDN ? flow->ssh_tls.tls_issuerDN : "",
|
flow->ssh_tls.tls_issuerDN ? flow->ssh_tls.tls_issuerDN : "",
|
||||||
flow->ssh_tls.tls_subjectDN ? flow->ssh_tls.tls_subjectDN : ""
|
flow->ssh_tls.tls_subjectDN ? flow->ssh_tls.tls_subjectDN : ""
|
||||||
);
|
);
|
||||||
|
#endif
|
||||||
|
|
||||||
fprintf(csv_fp, "%s,%s",
|
fprintf(csv_fp, "%s,%s",
|
||||||
(flow->ssh_tls.client_hassh[0] != '\0') ? flow->ssh_tls.client_hassh : "",
|
(flow->ssh_tls.client_hassh[0] != '\0') ? flow->ssh_tls.client_hassh : "",
|
||||||
(flow->ssh_tls.server_hassh[0] != '\0') ? flow->ssh_tls.server_hassh : ""
|
(flow->ssh_tls.server_hassh[0] != '\0') ? flow->ssh_tls.server_hassh : ""
|
||||||
);
|
);
|
||||||
|
|
||||||
fprintf(csv_fp, ",%s", flow->info);
|
fprintf(csv_fp, ",%s,", flow->info);
|
||||||
|
|
||||||
|
print_bin(csv_fp, NULL, &flow->payload_len_bin);
|
||||||
}
|
}
|
||||||
|
|
||||||
if((verbose != 1) && (verbose != 2)) {
|
if((verbose != 1) && (verbose != 2)) {
|
||||||
@@ -1335,10 +1344,10 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
|
|||||||
flow->human_readeable_string_buffer);
|
flow->human_readeable_string_buffer);
|
||||||
|
|
||||||
#ifdef DIRECTION_BINS
|
#ifdef DIRECTION_BINS
|
||||||
print_bin("Plen c2s", &flow->payload_len_bin_src2dst);
|
print_bin(out, "Plen c2s", &flow->payload_len_bin_src2dst);
|
||||||
print_bin("Plen s2c", &flow->payload_len_bin_dst2src);
|
print_bin(out, "Plen s2c", &flow->payload_len_bin_dst2src);
|
||||||
#else
|
#else
|
||||||
print_bin("Plen Bins", &flow->payload_len_bin);
|
print_bin(out, "Plen Bins", &flow->payload_len_bin);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fprintf(out, "\n");
|
fprintf(out, "\n");
|
||||||
@@ -3116,26 +3125,44 @@ void test_lib() {
|
|||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|
||||||
static void binUnitTest() {
|
static void binUnitTest() {
|
||||||
struct ndpi_bin b1, b2;
|
struct ndpi_bin *bins;
|
||||||
|
u_int8_t versbose = 0;
|
||||||
u_int8_t num_bins = 32;
|
u_int8_t num_bins = 32;
|
||||||
u_int32_t i;
|
u_int8_t num_points = 24;
|
||||||
|
u_int32_t i, j;
|
||||||
|
u_int8_t num_clusters = 3;
|
||||||
|
u_int16_t cluster_ids[256];
|
||||||
char out_buf[128];
|
char out_buf[128];
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
ndpi_init_bin(&b1, ndpi_bin_family8, num_bins), ndpi_init_bin(&b2, ndpi_bin_family8, num_bins);
|
assert((bins = (struct ndpi_bin*)malloc(sizeof(struct ndpi_bin)*num_bins)) != NULL);
|
||||||
|
|
||||||
for(i=0; i<32; i++)
|
for(i=0; i<num_bins; i++) {
|
||||||
ndpi_inc_bin(&b1, rand() % num_bins), ndpi_inc_bin(&b2, rand() % num_bins);
|
ndpi_init_bin(&bins[i], ndpi_bin_family8, num_points);
|
||||||
|
|
||||||
#if 0
|
for(j=0; j<num_points; j++)
|
||||||
printf("1) %s\n", ndpi_print_bin(&b1, 0, out_buf, sizeof(out_buf)));
|
ndpi_set_bin(&bins[i], j, rand() % 0xFF);
|
||||||
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));
|
ndpi_normalize_bin(&bins[i]);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
ndpi_free_bin(&b1), ndpi_free_bin(&b2);
|
|
||||||
|
ndpi_cluster_bins(bins, num_bins, num_clusters, cluster_ids);
|
||||||
|
|
||||||
|
for(j=0; j<num_clusters; j++) {
|
||||||
|
if(verbose) printf("\n");
|
||||||
|
|
||||||
|
for(i=0; i<num_bins; i++) {
|
||||||
|
if(cluster_ids[i] == j) {
|
||||||
|
if(verbose) printf("[%u] %s\n", cluster_ids[i], ndpi_print_bin(&bins[i], 0, out_buf, sizeof(out_buf)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// printf("Similarity: %f\n\n", ndpi_bin_similarity(&b1, &b2, 1));
|
||||||
|
|
||||||
|
for(i=0; i<num_bins; i++)
|
||||||
|
ndpi_free_bin(&bins[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *********************************************** */
|
/* *********************************************** */
|
||||||
|
|||||||
+23
-8
@@ -89,7 +89,7 @@
|
|||||||
extern u_int8_t enable_protocol_guess, enable_joy_stats, enable_payload_analyzer;
|
extern u_int8_t enable_protocol_guess, enable_joy_stats, enable_payload_analyzer;
|
||||||
extern u_int8_t verbose, human_readeable_string_len;
|
extern u_int8_t verbose, human_readeable_string_len;
|
||||||
extern u_int8_t max_num_udp_dissected_pkts /* 8 */, max_num_tcp_dissected_pkts /* 10 */;
|
extern u_int8_t max_num_udp_dissected_pkts /* 8 */, max_num_tcp_dissected_pkts /* 10 */;
|
||||||
|
extern FILE *csv_fp;
|
||||||
static u_int32_t flow_id = 0;
|
static u_int32_t flow_id = 0;
|
||||||
|
|
||||||
/* ****************************************************** */
|
/* ****************************************************** */
|
||||||
@@ -1176,14 +1176,24 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
|
|||||||
&& flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions) {
|
&& flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions) {
|
||||||
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
||||||
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions);
|
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions);
|
||||||
snprintf(flow->info, sizeof(flow->info), "ALPN: %s][TLS Supported Versions: %s",
|
|
||||||
flow->ndpi_flow->protos.stun_ssl.ssl.alpn,
|
if(csv_fp)
|
||||||
flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions);
|
snprintf(flow->info, sizeof(flow->info), "%s",
|
||||||
|
flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
||||||
|
else
|
||||||
|
snprintf(flow->info, sizeof(flow->info), "ALPN: %s][TLS Supported Versions: %s",
|
||||||
|
flow->ndpi_flow->protos.stun_ssl.ssl.alpn,
|
||||||
|
flow->ndpi_flow->protos.stun_ssl.ssl.tls_supported_versions);
|
||||||
}
|
}
|
||||||
else if(flow->ndpi_flow->protos.stun_ssl.ssl.alpn) {
|
else if(flow->ndpi_flow->protos.stun_ssl.ssl.alpn) {
|
||||||
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
correct_csv_data_field(flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
||||||
snprintf(flow->info, sizeof(flow->info), "ALPN: %s",
|
|
||||||
flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
if(csv_fp)
|
||||||
|
snprintf(flow->info, sizeof(flow->info), "%s,",
|
||||||
|
flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
||||||
|
else
|
||||||
|
snprintf(flow->info, sizeof(flow->info), "ALPN: %s",
|
||||||
|
flow->ndpi_flow->protos.stun_ssl.ssl.alpn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1385,8 +1395,13 @@ static struct ndpi_proto packet_processing(struct ndpi_workflow * workflow,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef DIRECTION_BINS
|
#ifndef DIRECTION_BINS
|
||||||
if(payload_len && ((flow->src2dst_packets+flow->dst2src_packets) < MAX_NUM_BIN_PKTS))
|
if(payload_len && ((flow->src2dst_packets+flow->dst2src_packets) < MAX_NUM_BIN_PKTS)) {
|
||||||
ndpi_inc_bin(&flow->payload_len_bin, plen2slot(payload_len));
|
#if 0
|
||||||
|
/* Discard packets until the protocol is detected */
|
||||||
|
if(flow->detected_protocol.app_protocol != NDPI_PROTOCOL_UNKNOWN)
|
||||||
|
#endif
|
||||||
|
ndpi_inc_bin(&flow->payload_len_bin, plen2slot(payload_len), 1);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(enable_payload_analyzer && (payload_len > 0))
|
if(enable_payload_analyzer && (payload_len > 0))
|
||||||
|
|||||||
@@ -1070,11 +1070,15 @@ extern "C" {
|
|||||||
|
|
||||||
int ndpi_init_bin(struct ndpi_bin *b, enum ndpi_bin_family f, u_int8_t num_bins);
|
int ndpi_init_bin(struct ndpi_bin *b, enum ndpi_bin_family f, u_int8_t num_bins);
|
||||||
void ndpi_free_bin(struct ndpi_bin *b);
|
void ndpi_free_bin(struct ndpi_bin *b);
|
||||||
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id);
|
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id, u_int32_t val);
|
||||||
|
void ndpi_set_bin(struct ndpi_bin *b, u_int8_t slot_id, u_int32_t value);
|
||||||
|
u_int32_t ndpi_get_bin_value(struct ndpi_bin *b, u_int8_t slot_id);
|
||||||
|
void ndpi_reset_bin(struct ndpi_bin *b);
|
||||||
void ndpi_normalize_bin(struct ndpi_bin *b);
|
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);
|
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);
|
float ndpi_bin_similarity(struct ndpi_bin *b1, struct ndpi_bin *b2, u_int8_t normalize_first);
|
||||||
|
int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
|
||||||
|
u_int8_t num_clusters, u_int16_t *cluster_ids);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
+169
-6
@@ -284,26 +284,81 @@ void ndpi_free_bin(struct ndpi_bin *b) {
|
|||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id) {
|
void ndpi_set_bin(struct ndpi_bin *b, u_int8_t slot_id, u_int32_t val) {
|
||||||
if(slot_id >= b->num_bins) slot_id = 0;
|
if(slot_id >= b->num_bins) slot_id = 0;
|
||||||
|
|
||||||
b->num_incs += 1;
|
b->num_incs += val;
|
||||||
|
|
||||||
switch(b->family) {
|
switch(b->family) {
|
||||||
case ndpi_bin_family8:
|
case ndpi_bin_family8:
|
||||||
b->u.bins8[slot_id]++;
|
b->u.bins8[slot_id] = (u_int8_t)val;
|
||||||
break;
|
break;
|
||||||
case ndpi_bin_family16:
|
case ndpi_bin_family16:
|
||||||
b->u.bins16[slot_id]++;
|
b->u.bins16[slot_id] = (u_int16_t)val;
|
||||||
break;
|
break;
|
||||||
case ndpi_bin_family32:
|
case ndpi_bin_family32:
|
||||||
b->u.bins32[slot_id]++;
|
b->u.bins32[slot_id] = (u_int32_t)val;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
void ndpi_inc_bin(struct ndpi_bin *b, u_int8_t slot_id, u_int32_t val) {
|
||||||
|
if(slot_id >= b->num_bins) slot_id = 0;
|
||||||
|
|
||||||
|
b->num_incs += val;
|
||||||
|
|
||||||
|
switch(b->family) {
|
||||||
|
case ndpi_bin_family8:
|
||||||
|
b->u.bins8[slot_id] += (u_int8_t)val;
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family16:
|
||||||
|
b->u.bins16[slot_id] += (u_int16_t)val;
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family32:
|
||||||
|
b->u.bins32[slot_id] += (u_int32_t)val;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
u_int32_t ndpi_get_bin_value(struct ndpi_bin *b, u_int8_t slot_id) {
|
||||||
|
if(slot_id >= b->num_bins) slot_id = 0;
|
||||||
|
|
||||||
|
switch(b->family) {
|
||||||
|
case ndpi_bin_family8:
|
||||||
|
return(b->u.bins8[slot_id]);
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family16:
|
||||||
|
return(b->u.bins16[slot_id]);
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family32:
|
||||||
|
return(b->u.bins32[slot_id]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
void ndpi_reset_bin(struct ndpi_bin *b) {
|
||||||
|
b->num_incs = 0;
|
||||||
|
|
||||||
|
switch(b->family) {
|
||||||
|
case ndpi_bin_family8:
|
||||||
|
memset(b->u.bins8, 0, sizeof(u_int8_t)*b->num_bins);
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family16:
|
||||||
|
memset(b->u.bins16, 0, sizeof(u_int16_t)*b->num_bins);
|
||||||
|
break;
|
||||||
|
case ndpi_bin_family32:
|
||||||
|
memset(b->u.bins32, 0, sizeof(u_int32_t)*b->num_bins);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Each bin slot is transformed in a % with respect to the value total
|
Each bin slot is transformed in a % with respect to the value total
|
||||||
*/
|
*/
|
||||||
@@ -387,7 +442,8 @@ float ndpi_bin_similarity(struct ndpi_bin *b1, struct ndpi_bin *b2, u_int8_t nor
|
|||||||
u_int32_t sumxx = 0, sumxy = 0, sumyy = 0;
|
u_int32_t sumxx = 0, sumxy = 0, sumyy = 0;
|
||||||
|
|
||||||
if((b1->num_incs == 0) || (b2->num_incs == 0)
|
if((b1->num_incs == 0) || (b2->num_incs == 0)
|
||||||
|| (b1->family != b2->family) || (b1->num_bins != b2->num_bins))
|
// || (b1->family != b2->family)
|
||||||
|
|| (b1->num_bins != b2->num_bins))
|
||||||
return(0);
|
return(0);
|
||||||
|
|
||||||
if(normalize_first)
|
if(normalize_first)
|
||||||
@@ -412,3 +468,110 @@ float ndpi_bin_similarity(struct ndpi_bin *b1, struct ndpi_bin *b2, u_int8_t nor
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ********************************************************************************* */
|
/* ********************************************************************************* */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Clusters bins into 'num_clusters'
|
||||||
|
- (in) bins: a vection 'num_bins' long of bins to cluster
|
||||||
|
- (in) 'num_clusters': number of desired clusters 0...(num_clusters-1)
|
||||||
|
- (out) 'cluster_ids': a vector 'num_bins' long containing the id's of each clustered bin
|
||||||
|
|
||||||
|
See
|
||||||
|
- https://en.wikipedia.org/wiki/K-means_clustering
|
||||||
|
*/
|
||||||
|
int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins,
|
||||||
|
u_int8_t num_clusters, u_int16_t *cluster_ids) {
|
||||||
|
u_int16_t i, j, max_iterations = 100, num_iterations = 0, num_moves;
|
||||||
|
struct ndpi_bin *centroids;
|
||||||
|
u_int8_t verbose = 0;
|
||||||
|
|
||||||
|
if(num_clusters > num_bins) return(-1);
|
||||||
|
|
||||||
|
if((centroids = (struct ndpi_bin*)malloc(sizeof(struct ndpi_bin)*num_clusters)) == NULL)
|
||||||
|
return(-2);
|
||||||
|
else {
|
||||||
|
for(i=0; i<num_clusters; i++)
|
||||||
|
ndpi_init_bin(¢roids[i], ndpi_bin_family32 /* Use 32 bit to avoid overlaps */, bins[0].num_bins);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Reset the id's */
|
||||||
|
memset(cluster_ids, 0, sizeof(u_int16_t) * num_bins);
|
||||||
|
|
||||||
|
/* Randomly pick a cluster id */
|
||||||
|
for(i=0; i<num_clusters; i++) cluster_ids[i] = i;
|
||||||
|
|
||||||
|
/* Assign the remaining bins to the nearest cluster */
|
||||||
|
for(i=num_clusters; i<num_bins; i++) {
|
||||||
|
u_int16_t j;
|
||||||
|
float top_similarity = -1;
|
||||||
|
u_int8_t cluster_id;
|
||||||
|
|
||||||
|
for(j=0; j<num_clusters; j++) {
|
||||||
|
float similarity = ndpi_bin_similarity(&bins[i], &bins[j], 0);
|
||||||
|
|
||||||
|
if(similarity > top_similarity)
|
||||||
|
cluster_id = j, top_similarity = similarity;
|
||||||
|
}
|
||||||
|
|
||||||
|
cluster_ids[i] = cluster_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now let's try to find a better arrangement */
|
||||||
|
while(num_iterations++ < max_iterations) {
|
||||||
|
/* Find the center of each cluster */
|
||||||
|
|
||||||
|
if(verbose) printf("Iteration %u\n", num_iterations);
|
||||||
|
|
||||||
|
for(i=0; i<num_clusters; i++)
|
||||||
|
ndpi_reset_bin(¢roids[i]);
|
||||||
|
|
||||||
|
for(i=0; i<num_bins; i++) {
|
||||||
|
for(j=0; j<bins[i].num_bins; j++) {
|
||||||
|
ndpi_inc_bin(¢roids[cluster_ids[i]], j, ndpi_get_bin_value(&bins[i], j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0; i<num_clusters; i++) {
|
||||||
|
char out_buf[256];
|
||||||
|
|
||||||
|
ndpi_normalize_bin(¢roids[i]);
|
||||||
|
if(verbose)
|
||||||
|
printf("Centroid [%u] %s\n", cluster_ids[i],
|
||||||
|
ndpi_print_bin(¢roids[i], 0, out_buf, sizeof(out_buf)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now let's check if there are bins to move across clusters */
|
||||||
|
num_moves = 0;
|
||||||
|
|
||||||
|
for(i=0; i<num_bins; i++) {
|
||||||
|
u_int16_t j;
|
||||||
|
float top_similarity = -1;
|
||||||
|
u_int8_t cluster_id;
|
||||||
|
|
||||||
|
for(j=0; j<num_clusters; j++) {
|
||||||
|
float similarity = ndpi_bin_similarity(&bins[i], ¢roids[j], 0);
|
||||||
|
|
||||||
|
if(similarity > top_similarity)
|
||||||
|
cluster_id = j, top_similarity = similarity;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((top_similarity > 0) && (cluster_ids[i] != cluster_id)) {
|
||||||
|
if(verbose)
|
||||||
|
printf("Moved bin %u from cluster %u -> %u [similarity: %f]\n",
|
||||||
|
i, cluster_ids[i], cluster_id, top_similarity);
|
||||||
|
|
||||||
|
cluster_ids[i] = cluster_id;
|
||||||
|
num_moves++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(num_moves == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0; i<num_clusters; i++)
|
||||||
|
ndpi_free_bin(¢roids[i]);
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ********************************************************************************* */
|
||||||
|
|||||||
Reference in New Issue
Block a user