aboutsummaryrefslogtreecommitdiff
path: root/src/lib/ndpi_analyze.c
blob: ce316816569e52f5f8dddf268f600039d250beeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * ndpi_analyze.c
 *
 * Copyright (C) 2019 - ntop.org
 *
 * This file is part of nDPI, an open source deep packet inspection
 * library.
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include "ndpi_config.h"
#endif

#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>
#include <math.h>
#include <float.h> /* FLT_EPSILON */
#include "ndpi_api.h"
#include "ndpi_config.h"

/* ********************************************************************************* */

struct ndpi_analyze_struct* ndpi_init_data_analysis(u_int16_t _max_series_len) {
  struct ndpi_analyze_struct *ret = ndpi_malloc(sizeof(struct ndpi_analyze_struct));
  u_int32_t len;

  if(ret == NULL)
    return(ret);
  else
    memset(ret, 0, sizeof(struct ndpi_analyze_struct));
  
  if(_max_series_len > MAX_SERIES_LEN) _max_series_len = MAX_SERIES_LEN;
  if(_max_series_len == 0)             _max_series_len = 1; /* At least 1 element */
  ret->num_values_array_len = _max_series_len;

  len = sizeof(u_int32_t)*ret->num_values_array_len;
  if((ret->values = ndpi_malloc(len)) == NULL) {
    ndpi_free(ret);
    ret = NULL;
  } else
    memset(ret->values, 0, len);
  
  return(ret);
}

/* ********************************************************************************* */

void ndpi_free_data_analysis(struct ndpi_analyze_struct *d) {
  ndpi_free(d->values);
  ndpi_free(d);
}

/* ********************************************************************************* */

/*
  Add a new point to analyze
 */
void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value) {
  float tmp_mu;

  s->sum_total += value, s->num_data_entries++, s->values[s->next_value_insert_index] = value;

  if(++s->next_value_insert_index == s->num_values_array_len)
    s->next_value_insert_index = 0;

  /* Update stddev */
  tmp_mu = s->stddev.mu;
  s->stddev.mu = ((s->stddev.mu * (s->num_data_entries - 1)) + value) / s->num_data_entries;
  s->stddev.q = s->stddev.q + (value - tmp_mu)*(value - s->stddev.mu);    
}

/* ********************************************************************************* */

/* Compute the average on all values */
float ndpi_data_average(struct ndpi_analyze_struct *s) {
  return((float)s->sum_total / (float)s->num_data_entries);
}

/* ********************************************************************************* */

/* Compute the variance on all values */
float ndpi_data_variance(struct ndpi_analyze_struct *s) {
  return(s->num_data_entries ? (s->stddev.q / s->num_data_entries) : 0);
}

/* ********************************************************************************* */

/* Compute the standard deviation on all values */
float ndpi_data_stddev(struct ndpi_analyze_struct *s) {
  return(sqrt(ndpi_data_variance(s)));
}

/* ********************************************************************************* */

/* Compute the average only on the sliding window */
float ndpi_data_window_average(struct ndpi_analyze_struct *s) {
  float   sum = 0.0;
  u_int16_t i, n = ndpi_min(s->num_data_entries, s->num_values_array_len);
    
  for(i=0; i<n; i++)
    sum += s->values[i];

  return((float)sum / (float)n);
}

/* ********************************************************************************* */

/*
  Compute entropy on the last sliding window values
*/
float ndpi_data_entropy(struct ndpi_analyze_struct *s) {
  int i;
  float sum = 0.0, total = 0.0;

  for(i=0; i<s->num_values_array_len; i++)
    total += s->values[i];
    
  for (i=0; i<s->num_values_array_len; i++) {
    float tmp = (float)s->values[i] / (float)total;
    
    if(tmp > FLT_EPSILON)
      sum -= tmp * logf(tmp);    
  }
  
  return(sum / logf(2.0));
}

/* ********************************************************************************* */

void ndpi_data_print_window_values(struct ndpi_analyze_struct *s) {
  u_int16_t i, n = ndpi_min(s->num_data_entries, s->num_values_array_len);
  
  for(i=0; i<n; i++)
    printf("[%u: %u]", i, s->values[i]);

  printf("\n");
}

/* ********************************************************************************* */

float ndpi_data_ratio(u_int32_t sent, u_int32_t rcvd) {
  int64_t s = (int64_t)sent +  (int64_t)rcvd;
  int64_t d =  (int64_t)sent -  (int64_t)rcvd;

  return((s == 0) ? 0 : ((float)d)/((float)s));
}