/src/ntopng/include/HostPools.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * (C) 2015-24 - 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 _HOST_POOLS_H_ |
23 | | #define _HOST_POOLS_H_ |
24 | | |
25 | | #include "ntop_includes.h" |
26 | | |
27 | | class NetworkInterface; |
28 | | class Host; |
29 | | class Mac; |
30 | | |
31 | | class HostPools { |
32 | | private: |
33 | | VLANAddressTree *tree, *tree_shadow; |
34 | | NetworkInterface *iface; |
35 | | u_int16_t max_num_pools; |
36 | | int32_t *num_active_hosts_inline, *num_active_hosts_offline; |
37 | | int32_t *num_active_l2_devices_inline, *num_active_l2_devices_offline; |
38 | | HostPoolStats **stats, **stats_shadow; |
39 | | |
40 | | #ifdef NTOPNG_PRO |
41 | | bool *children_safe; |
42 | | bool *forge_global_dns; |
43 | | u_int8_t *routing_policy_id; |
44 | | u_int16_t *pool_shaper; |
45 | | u_int32_t *schedule_bitmap; |
46 | | bool *enforce_quotas_per_pool_member; /* quotas can be pool-wide or per pool |
47 | | member */ |
48 | | bool *enforce_shapers_per_pool_member; |
49 | | #endif |
50 | 15.6k | inline HostPoolStats *getPoolStats(u_int16_t host_pool_id) { |
51 | 15.6k | if ((host_pool_id >= max_num_pools) || (!stats)) return NULL; |
52 | 0 | return stats[host_pool_id]; |
53 | 15.6k | } |
54 | | void reloadPoolStats(); |
55 | | static void deleteStats(HostPoolStats ***hps); |
56 | | |
57 | | void swap(VLANAddressTree *new_trees, HostPoolStats **new_stats); |
58 | | |
59 | 36.9k | inline void incNumMembers(u_int16_t pool_id, int32_t *ctr) const { |
60 | 36.9k | if (ctr && pool_id < max_num_pools) ctr[pool_id]++; |
61 | 36.9k | }; |
62 | 21.2k | inline void decNumMembers(u_int16_t pool_id, int32_t *ctr) const { |
63 | 21.2k | if (ctr && pool_id < max_num_pools) ctr[pool_id]--; |
64 | 21.2k | }; |
65 | | |
66 | | public: |
67 | | HostPools(NetworkInterface *_iface); |
68 | | virtual ~HostPools(); |
69 | | |
70 | | void reloadPools(); |
71 | | void reloadPool(u_int16_t _pool_id, VLANAddressTree *new_tree, HostPoolStats **new_stats); |
72 | | u_int16_t getPool(Host *h); |
73 | | u_int16_t getPool(Mac *m); |
74 | | u_int16_t getPoolByName(const char *pool_name); |
75 | | |
76 | | bool findIpPool(IpAddress *ip, u_int16_t vlan_id, u_int16_t *found_pool, |
77 | | ndpi_patricia_node_t **found_node); |
78 | | bool findMacPool(const u_int8_t *const mac, u_int16_t *found_pool); |
79 | | bool findMacPool(Mac *mac, u_int16_t *found_pool); |
80 | | void lua(lua_State *vm); |
81 | | |
82 | 0 | inline int32_t getNumPoolHosts(u_int16_t pool_id) { |
83 | 0 | if (pool_id >= max_num_pools) return NO_HOST_POOL_ID; |
84 | 0 | return num_active_hosts_inline[pool_id] + num_active_hosts_offline[pool_id]; |
85 | 0 | } |
86 | | |
87 | 0 | inline int32_t getNumPoolL2Devices(u_int16_t pool_id) { |
88 | 0 | if (pool_id >= max_num_pools) return NO_HOST_POOL_ID; |
89 | | |
90 | 0 | return num_active_l2_devices_inline[pool_id] + |
91 | 0 | num_active_l2_devices_offline[pool_id]; |
92 | 0 | } |
93 | | |
94 | 20.3k | inline void incNumHosts(u_int16_t pool_id, bool isInlineCall) { |
95 | 20.3k | incNumMembers(pool_id, isInlineCall ? num_active_hosts_inline |
96 | 20.3k | : num_active_hosts_offline); |
97 | 20.3k | }; |
98 | 20.3k | inline void decNumHosts(u_int16_t pool_id, bool isInlineCall) { |
99 | 20.3k | decNumMembers(pool_id, isInlineCall ? num_active_hosts_inline |
100 | 20.3k | : num_active_hosts_offline); |
101 | 20.3k | }; |
102 | 16.6k | inline void incNumL2Devices(u_int16_t pool_id, bool isInlineCall) { |
103 | 16.6k | incNumMembers(pool_id, isInlineCall ? num_active_l2_devices_inline |
104 | 16.6k | : num_active_l2_devices_offline); |
105 | 16.6k | }; |
106 | 955 | inline void decNumL2Devices(u_int16_t pool_id, bool isInlineCall) { |
107 | 955 | decNumMembers(pool_id, isInlineCall ? num_active_l2_devices_inline |
108 | 955 | : num_active_l2_devices_offline); |
109 | 955 | }; |
110 | | |
111 | | void incPoolNumDroppedFlows(u_int16_t pool_id); |
112 | | void incPoolStats(u_int32_t when, u_int16_t host_pool_id, |
113 | | u_int16_t ndpi_proto, ndpi_protocol_category_t category_id, |
114 | | u_int64_t sent_packets, u_int64_t sent_bytes, |
115 | | u_int64_t rcvd_packets, u_int64_t rcvd_bytes); |
116 | | void updateStats(const struct timeval *tv); |
117 | | void luaStats(lua_State *vm); |
118 | | void luaStats(lua_State *mv, u_int16_t pool_id); |
119 | | |
120 | | /* To be called on the same thread as incPoolStats */ |
121 | | void checkPoolsStatsReset(); |
122 | | |
123 | | inline bool getProtoStats(u_int16_t host_pool_id, u_int16_t ndpi_proto, |
124 | 0 | u_int64_t *bytes, u_int32_t *duration) { |
125 | 0 | HostPoolStats *hps; |
126 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
127 | 0 |
|
128 | 0 | hps->getProtoStats(ndpi_proto, bytes, duration); |
129 | 0 | return true; |
130 | 0 | } |
131 | | |
132 | | inline bool getCategoryStats(u_int16_t host_pool_id, |
133 | | ndpi_protocol_category_t category_id, |
134 | 0 | u_int64_t *bytes, u_int32_t *duration) { |
135 | 0 | HostPoolStats *hps; |
136 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
137 | 0 |
|
138 | 0 | hps->getCategoryStats(category_id, bytes, duration); |
139 | 0 | return true; |
140 | 0 | } |
141 | | |
142 | | inline bool getStats(u_int16_t host_pool_id, u_int64_t *bytes, |
143 | 0 | u_int32_t *duration) { |
144 | 0 | HostPoolStats *hps; |
145 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
146 | 0 |
|
147 | 0 | hps->getStats(bytes, duration); |
148 | 0 | return true; |
149 | 0 | } |
150 | | |
151 | | void resetPoolsStats(u_int16_t pool_filter); |
152 | | |
153 | | #ifdef NTOPNG_PRO |
154 | | inline bool enforceQuotasPerPoolMember(u_int16_t pool_id) { |
155 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
156 | | ? enforce_quotas_per_pool_member[pool_id] |
157 | | : false); |
158 | | } |
159 | | inline bool enforceShapersPerPoolMember(u_int16_t pool_id) { |
160 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
161 | | ? enforce_shapers_per_pool_member[pool_id] |
162 | | : false); |
163 | | } |
164 | | inline u_int16_t getPoolShaper(u_int16_t pool_id) { |
165 | | return ((pool_id < max_num_pools) ? pool_shaper[pool_id] |
166 | | : DEFAULT_SHAPER_ID); |
167 | | } |
168 | | inline u_int32_t getPoolSchedule(u_int16_t pool_id) { |
169 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
170 | | ? schedule_bitmap[pool_id] |
171 | | : DEFAULT_TIME_SCHEDULE); |
172 | | } |
173 | | |
174 | | inline bool isChildrenSafePool(u_int16_t pool_id) { |
175 | | return ((pool_id < max_num_pools) ? children_safe[pool_id] : false); |
176 | | } |
177 | | |
178 | | inline bool forgeGlobalDns(u_int16_t pool_id) { |
179 | | return ((pool_id < max_num_pools) ? forge_global_dns[pool_id] : false); |
180 | | } |
181 | | |
182 | | inline u_int8_t getRoutingPolicy(u_int16_t pool_id) { |
183 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
184 | | ? routing_policy_id[pool_id] |
185 | | : DEFAULT_ROUTING_TABLE_ID); |
186 | | } |
187 | | #endif |
188 | | }; |
189 | | |
190 | | #endif /* _HOST_POOLS_H_ */ |