Skip to content

Commit

Permalink
Manual merge of pull #769
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaderi committed Oct 2, 2019
1 parent 3dbf7bb commit 4fd7e57
Show file tree
Hide file tree
Showing 76 changed files with 1,003 additions and 977 deletions.
42 changes: 20 additions & 22 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ flowGetBDMeanandVariance(struct ndpi_flow_info* flow) {
uint32_t tmp[256], i;
unsigned int num_bytes;
double mean = 0.0, variance = 0.0;
struct ndpi_entropy last_entropy = flow->last_entropy;

fflush(out);

Expand All @@ -270,38 +271,38 @@ flowGetBDMeanandVariance(struct ndpi_flow_info* flow) {
* if this flow is bidirectional
*/
if (!flow->bidirectional) {
array = flow->src2dst_byte_count;
num_bytes = flow->src2dst_l4_bytes;
array = last_entropy.src2dst_byte_count;
num_bytes = last_entropy.src2dst_l4_bytes;
for (i=0; i<256; i++) {
tmp[i] = flow->src2dst_byte_count[i];
tmp[i] = last_entropy.src2dst_byte_count[i];
}

if (flow->src2dst_num_bytes != 0) {
mean = flow->src2dst_bd_mean;
variance = flow->src2dst_bd_variance/(flow->src2dst_num_bytes - 1);
if (last_entropy.src2dst_num_bytes != 0) {
mean = last_entropy.src2dst_bd_mean;
variance = last_entropy.src2dst_bd_variance/(last_entropy.src2dst_num_bytes - 1);
variance = sqrt(variance);

if (flow->src2dst_num_bytes == 1) {
if (last_entropy.src2dst_num_bytes == 1) {
variance = 0.0;
}
}
} else {
for (i=0; i<256; i++) {
tmp[i] = flow->src2dst_byte_count[i] + flow->dst2src_byte_count[i];
tmp[i] = last_entropy.src2dst_byte_count[i] + last_entropy.dst2src_byte_count[i];
}
array = tmp;
num_bytes = flow->src2dst_l4_bytes + flow->dst2src_l4_bytes;
num_bytes = last_entropy.src2dst_l4_bytes + last_entropy.dst2src_l4_bytes;

if (flow->src2dst_num_bytes + flow->dst2src_num_bytes != 0) {
mean = ((double)flow->src2dst_num_bytes)/((double)(flow->src2dst_num_bytes+flow->dst2src_num_bytes))*flow->src2dst_bd_mean +
((double)flow->dst2src_num_bytes)/((double)(flow->dst2src_num_bytes+flow->src2dst_num_bytes))*flow->dst2src_bd_mean;
if (last_entropy.src2dst_num_bytes + last_entropy.dst2src_num_bytes != 0) {
mean = ((double)last_entropy.src2dst_num_bytes)/((double)(last_entropy.src2dst_num_bytes+last_entropy.dst2src_num_bytes))*last_entropy.src2dst_bd_mean +
((double)last_entropy.dst2src_num_bytes)/((double)(last_entropy.dst2src_num_bytes+last_entropy.src2dst_num_bytes))*last_entropy.dst2src_bd_mean;

variance = ((double)flow->src2dst_num_bytes)/((double)(flow->src2dst_num_bytes+flow->dst2src_num_bytes))*flow->src2dst_bd_variance +
((double)flow->dst2src_num_bytes)/((double)(flow->dst2src_num_bytes+flow->src2dst_num_bytes))*flow->dst2src_bd_variance;
variance = ((double)last_entropy.src2dst_num_bytes)/((double)(last_entropy.src2dst_num_bytes+last_entropy.dst2src_num_bytes))*last_entropy.src2dst_bd_variance +
((double)last_entropy.dst2src_num_bytes)/((double)(last_entropy.dst2src_num_bytes+last_entropy.src2dst_num_bytes))*last_entropy.dst2src_bd_variance;

variance = variance/((double)(flow->src2dst_num_bytes + flow->dst2src_num_bytes - 1));
variance = variance/((double)(last_entropy.src2dst_num_bytes + last_entropy.dst2src_num_bytes - 1));
variance = sqrt(variance);
if (flow->src2dst_num_bytes + flow->dst2src_num_bytes == 1) {
if (last_entropy.src2dst_num_bytes + last_entropy.dst2src_num_bytes == 1) {
variance = 0.0;
}
}
Expand All @@ -320,14 +321,11 @@ flowGetBDMeanandVariance(struct ndpi_flow_info* flow) {
}

/* Output the mean */
if(num_bytes != 0) {
fprintf(out, "][byte_dist_mean: %f", mean);
fprintf(out, "][byte_dist_std: %f]", variance);
}

if(num_bytes != 0) {
double entropy = ndpi_flow_get_byte_count_entropy(array, num_bytes);

fprintf(out, "][byte_dist_mean: %f", mean);
fprintf(out, "][byte_dist_std: %f]", variance);
fprintf(out, "[entropy: %f]", entropy);
fprintf(out, "[total_entropy: %f]", entropy * num_bytes);
}
Expand Down Expand Up @@ -1086,7 +1084,7 @@ static void printFlow(u_int16_t id, struct ndpi_flow_info *flow, u_int16_t threa
/* Print entropy values for monitored flows. */
flowGetBDMeanandVariance(flow);
fflush(out);
fprintf(out, "[score: %.4f]", flow->score);
fprintf(out, "[score: %.4f]", flow->entropy.score);
}

if(flow->detected_protocol.master_protocol) {
Expand Down
Loading

0 comments on commit 4fd7e57

Please sign in to comment.