diff options
-rw-r--r-- | example/ndpiReader.c | 6 | ||||
-rw-r--r-- | example/reader_util.c | 10 | ||||
-rw-r--r-- | src/include/ndpi_api.h.in | 2 | ||||
-rw-r--r-- | src/include/ndpi_typedefs.h | 2 | ||||
-rw-r--r-- | src/lib/ndpi_analyze.c | 26 |
5 files changed, 25 insertions, 21 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index a4fb74246..1e127d1cd 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -3587,7 +3587,7 @@ void analyzeUnitTest() { ndpi_data_min(s), ndpi_data_max(s)); #endif - ndpi_free_data_analysis(s); + ndpi_free_data_analysis(s, 1); #ifdef RUN_DATA_ANALYSIS_THEN_QUIT exit(0); @@ -3675,7 +3675,7 @@ void analysisUnitTest() { printf("Min/Max: %u/%u\n", ndpi_data_min(s), ndpi_data_max(s)); } - ndpi_free_data_analysis(s); + ndpi_free_data_analysis(s, 1); } /* *********************************************** */ @@ -3704,7 +3704,7 @@ void rsiUnitTest() { for(i=0; i<n; i++) { float rsi = ndpi_rsi_add_value(&s, v[i]); -#if 0 +#if DEBUG printf("%2d) RSI = %f\n", i, rsi); #endif } diff --git a/example/reader_util.c b/example/reader_util.c index 98650cf37..86ddf6ad2 100644 --- a/example/reader_util.c +++ b/example/reader_util.c @@ -493,13 +493,13 @@ static void ndpi_free_flow_tls_data(struct ndpi_flow_info *flow) { /* ***************************************************** */ static void ndpi_free_flow_data_analysis(struct ndpi_flow_info *flow) { - if(flow->iat_c_to_s) ndpi_free_data_analysis(flow->iat_c_to_s); - if(flow->iat_s_to_c) ndpi_free_data_analysis(flow->iat_s_to_c); + if(flow->iat_c_to_s) ndpi_free_data_analysis(flow->iat_c_to_s, 1); + if(flow->iat_s_to_c) ndpi_free_data_analysis(flow->iat_s_to_c, 1); - if(flow->pktlen_c_to_s) ndpi_free_data_analysis(flow->pktlen_c_to_s); - if(flow->pktlen_s_to_c) ndpi_free_data_analysis(flow->pktlen_s_to_c); + if(flow->pktlen_c_to_s) ndpi_free_data_analysis(flow->pktlen_c_to_s, 1); + if(flow->pktlen_s_to_c) ndpi_free_data_analysis(flow->pktlen_s_to_c, 1); - if(flow->iat_flow) ndpi_free_data_analysis(flow->iat_flow); + if(flow->iat_flow) ndpi_free_data_analysis(flow->iat_flow, 1); } /* ***************************************************** */ diff --git a/src/include/ndpi_api.h.in b/src/include/ndpi_api.h.in index 0d68237a6..d90bcde19 100644 --- a/src/include/ndpi_api.h.in +++ b/src/include/ndpi_api.h.in @@ -1370,7 +1370,7 @@ extern "C" { /* Data analysis */ struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len); void ndpi_init_data_analysis(struct ndpi_analyze_struct *s, u_int16_t _max_series_len); - void ndpi_free_data_analysis(struct ndpi_analyze_struct *d); + void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer); void ndpi_reset_data_analysis(struct ndpi_analyze_struct *d); void ndpi_data_add_value(struct ndpi_analyze_struct *s, const u_int32_t value); diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index a3910a7df..859a7f0e4 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -1545,7 +1545,7 @@ struct ndpi_analyze_struct { /* **************************************** */ struct ndpi_rsi_struct { - struct ndpi_analyze_struct *gains, *losses; + struct ndpi_analyze_struct gains, losses; u_int32_t last_value; }; diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index 3b1b2557f..32b1a4920 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -68,9 +68,9 @@ struct ndpi_analyze_struct* ndpi_alloc_data_analysis(u_int16_t _max_series_len) /* ********************************************************************************* */ -void ndpi_free_data_analysis(struct ndpi_analyze_struct *d) { +void ndpi_free_data_analysis(struct ndpi_analyze_struct *d, u_int8_t free_pointer) { if(d->values) ndpi_free(d->values); - ndpi_free(d); + if(free_pointer) ndpi_free(d); } /* ********************************************************************************* */ @@ -827,29 +827,33 @@ int ndpi_cluster_bins(struct ndpi_bin *bins, u_int16_t num_bins, */ void ndpi_init_rsi(struct ndpi_rsi_struct *s, u_int16_t num_learning_values) { - s->gains = ndpi_alloc_data_analysis(num_learning_values); - s->losses = ndpi_alloc_data_analysis(num_learning_values); + ndpi_init_data_analysis(&s->gains, num_learning_values); + ndpi_init_data_analysis(&s->losses, num_learning_values); s->last_value = 0; } void ndpi_free_rsi(struct ndpi_rsi_struct *s) { - ndpi_free_data_analysis(s->gains), ndpi_free_data_analysis(s->losses); + ndpi_free_data_analysis(&s->gains, 0), ndpi_free_data_analysis(&s->losses, 0); } +/* + This function adds a new value and returns the computed RSI, or -1 + if there are too many points (< num_learning_values) +*/ float ndpi_rsi_add_value(struct ndpi_rsi_struct *s, const u_int32_t value) { - if(s->gains->num_data_entries == 0) - ndpi_data_add_value(s->gains, 0), ndpi_data_add_value(s->losses, 0); + if(s->gains.num_data_entries == 0) + ndpi_data_add_value(&s->gains, 0), ndpi_data_add_value(&s->losses, 0); else { if(value > s->last_value) - ndpi_data_add_value(s->gains, value - s->last_value), ndpi_data_add_value(s->losses, 0); + ndpi_data_add_value(&s->gains, value - s->last_value), ndpi_data_add_value(&s->losses, 0); else - ndpi_data_add_value(s->losses, s->last_value - value), ndpi_data_add_value(s->gains, 0); + ndpi_data_add_value(&s->losses, s->last_value - value), ndpi_data_add_value(&s->gains, 0); } s->last_value = value; - if(s->gains->num_data_entries >= s->gains->num_values_array_len) { - float relative_strength = ndpi_data_average(s->gains) / ndpi_data_average(s->losses); + if(s->gains.num_data_entries >= s->gains.num_values_array_len) { + float relative_strength = ndpi_data_window_average(&s->gains) / ndpi_data_window_average(&s->losses); /* printf("RSI: %f\n", relative_strength); */ return(100. - (100. / (1. + relative_strength))); |