Coverage Report

Created: 2025-05-16 06:24

/src/ntopng/include/nDPIStats.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * (C) 2013-25 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _NDPI_STATS_H_
23
#define _NDPI_STATS_H_
24
25
#include "ntop_includes.h"
26
27
class NetworkInterface;
28
class ThroughputStats;
29
30
/* *************************************** */
31
32
class nDPIStats {
33
 private:
34
  bool enable_throughput_stats, enable_behavior_stats;
35
  time_t nextMinPeriodicUpdate;
36
  std::unordered_map<u_int16_t, ProtoCounter *> counters;
37
  std::unordered_map<u_int16_t, CategoryCounter> cat_counters;
38
39
 public:
40
  nDPIStats(bool enable_throughput_stats = false,
41
            bool enable_behavior_stats = false);
42
  nDPIStats(nDPIStats &stats);
43
  ~nDPIStats();
44
45
  void updateStats(const struct timeval *tv);
46
47
  void incStats(u_int32_t when, u_int16_t proto_id, u_int64_t sent_packets,
48
                u_int64_t sent_bytes, u_int64_t rcvd_packets,
49
                u_int64_t rcvd_bytes);
50
51
  void incCategoryStats(u_int32_t when, ndpi_protocol_category_t category_id,
52
                        u_int64_t sent_bytes, u_int64_t rcvd_bytes);
53
54
  void incFlowsStats(u_int16_t proto_id);
55
  void lua(NetworkInterface *iface, lua_State *vm, bool with_categories = false,
56
           bool tsLua = false, bool diff = false);
57
  json_object *getJSONObject(NetworkInterface *iface);
58
  void sum(nDPIStats *s);
59
#ifdef NTOPNG_PRO
60
  bool getEnableThptStats() { return enable_throughput_stats; }
61
#endif
62
0
  bool getEnableBehaviorStats() { return enable_behavior_stats; }
63
0
  inline u_int64_t getProtoBytes(u_int16_t proto_id) {
64
0
    std::unordered_map<u_int16_t, ProtoCounter *>::iterator pi =
65
0
        counters.find(proto_id);
66
67
0
    if (pi != counters.end()) {
68
0
      TrafficCounter tc = pi->second->get_bytes();
69
70
0
      return (tc.getTotal());
71
0
    } else
72
0
      return (0);
73
0
  }
74
75
0
  inline u_int32_t getProtoDuration(u_int16_t proto_id) {
76
0
    std::unordered_map<u_int16_t, ProtoCounter *>::iterator pi =
77
0
        counters.find(proto_id);
78
79
0
    if (pi != counters.end())
80
0
      return (pi->second->get_duration());
81
0
    else
82
0
      return (0);
83
0
  }
84
85
0
  inline u_int64_t getCategoryBytes(ndpi_protocol_category_t category_id) {
86
0
    std::unordered_map<u_int16_t, CategoryCounter>::iterator cc =
87
0
        cat_counters.find(category_id);
88
89
0
    if (cc != cat_counters.end())
90
0
      return (cc->second.getTotalBytes());
91
0
    else
92
0
      return (0);
93
0
  }
94
95
0
  inline u_int32_t getCategoryDuration(ndpi_protocol_category_t category_id) {
96
0
    std::unordered_map<u_int16_t, CategoryCounter>::iterator cc =
97
0
        cat_counters.find(category_id);
98
0
99
0
    if (cc != cat_counters.end())
100
0
      return (cc->second.getDuration());
101
0
    else
102
0
      return (0);
103
0
  }
104
105
  void resetStats();
106
  char *serialize(NetworkInterface *iface);
107
  void deserialize(NetworkInterface *iface, json_object *o);
108
};
109
110
#endif /* _NDPI_STATS_H_ */