/src/ntopng/include/HostPools.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * (C) 2015-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 _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 | 22.4k | inline HostPoolStats *getPoolStats(u_int16_t host_pool_id) { |
51 | 22.4k | if ((host_pool_id >= max_num_pools) || (!stats)) return NULL; |
52 | 0 | return stats[host_pool_id]; |
53 | 22.4k | } |
54 | | void reloadPoolStats(); |
55 | | static void deleteStats(HostPoolStats ***hps); |
56 | | |
57 | | void swap(VLANAddressTree *new_trees, HostPoolStats **new_stats); |
58 | | |
59 | 57.7k | inline void incNumMembers(u_int16_t pool_id, int32_t *ctr) const { |
60 | 57.7k | if (ctr && pool_id < max_num_pools) ctr[pool_id]++; |
61 | 57.7k | }; |
62 | 37.7k | inline void decNumMembers(u_int16_t pool_id, int32_t *ctr) const { |
63 | 37.7k | if (ctr && pool_id < max_num_pools) ctr[pool_id]--; |
64 | 37.7k | }; |
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, bool *mac_match = NULL); |
73 | | u_int16_t getPool(Mac *m); |
74 | | u_int16_t getPoolByName(const char *pool_name); |
75 | | u_int16_t getCurrentHostPoolsNumber(); |
76 | | u_int32_t getCurrentMaxHostPoolsMembers(); |
77 | | |
78 | | bool findIpPool(IpAddress *ip, u_int16_t vlan_id, u_int16_t *found_pool, |
79 | | ndpi_patricia_node_t **found_node); |
80 | | bool findMacPool(const u_int8_t *const mac, u_int16_t *found_pool); |
81 | | bool findMacPool(Mac *mac, u_int16_t *found_pool); |
82 | | void lua(lua_State *vm); |
83 | | |
84 | 0 | inline int32_t getNumPoolHosts(u_int16_t pool_id) { |
85 | 0 | if (pool_id >= max_num_pools) return NO_HOST_POOL_ID; |
86 | 0 | return num_active_hosts_inline[pool_id] + num_active_hosts_offline[pool_id]; |
87 | 0 | } |
88 | | |
89 | 0 | inline int32_t getNumPoolL2Devices(u_int16_t pool_id) { |
90 | 0 | if (pool_id >= max_num_pools) return NO_HOST_POOL_ID; |
91 | | |
92 | 0 | return num_active_l2_devices_inline[pool_id] + |
93 | 0 | num_active_l2_devices_offline[pool_id]; |
94 | 0 | } |
95 | | |
96 | 34.1k | inline void incNumHosts(u_int16_t pool_id, bool isInlineCall) { |
97 | 34.1k | incNumMembers(pool_id, isInlineCall ? num_active_hosts_inline |
98 | 34.1k | : num_active_hosts_offline); |
99 | 34.1k | }; |
100 | 34.1k | inline void decNumHosts(u_int16_t pool_id, bool isInlineCall) { |
101 | 34.1k | decNumMembers(pool_id, isInlineCall ? num_active_hosts_inline |
102 | 34.1k | : num_active_hosts_offline); |
103 | 34.1k | }; |
104 | 23.5k | inline void incNumL2Devices(u_int16_t pool_id, bool isInlineCall) { |
105 | 23.5k | incNumMembers(pool_id, isInlineCall ? num_active_l2_devices_inline |
106 | 23.5k | : num_active_l2_devices_offline); |
107 | 23.5k | }; |
108 | 3.56k | inline void decNumL2Devices(u_int16_t pool_id, bool isInlineCall) { |
109 | 3.56k | decNumMembers(pool_id, isInlineCall ? num_active_l2_devices_inline |
110 | 3.56k | : num_active_l2_devices_offline); |
111 | 3.56k | }; |
112 | | |
113 | | void incPoolNumDroppedFlows(u_int16_t pool_id); |
114 | | void incPoolStats(u_int32_t when, u_int16_t host_pool_id, |
115 | | u_int16_t ndpi_proto, ndpi_protocol_category_t category_id, |
116 | | u_int64_t sent_packets, u_int64_t sent_bytes, |
117 | | u_int64_t rcvd_packets, u_int64_t rcvd_bytes); |
118 | | void updateStats(const struct timeval *tv); |
119 | | void luaStats(lua_State *vm); |
120 | | void luaStats(lua_State *mv, u_int16_t pool_id); |
121 | | |
122 | | /* To be called on the same thread as incPoolStats */ |
123 | | void checkPoolsStatsReset(); |
124 | | |
125 | | inline bool getProtoStats(u_int16_t host_pool_id, u_int16_t ndpi_proto, |
126 | 0 | u_int64_t *bytes, u_int32_t *duration) { |
127 | 0 | HostPoolStats *hps; |
128 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
129 | 0 |
|
130 | 0 | hps->getProtoStats(ndpi_proto, bytes, duration); |
131 | 0 | return true; |
132 | 0 | } |
133 | | |
134 | | inline bool getCategoryStats(u_int16_t host_pool_id, |
135 | | ndpi_protocol_category_t category_id, |
136 | 0 | u_int64_t *bytes, u_int32_t *duration) { |
137 | 0 | HostPoolStats *hps; |
138 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
139 | 0 |
|
140 | 0 | hps->getCategoryStats(category_id, bytes, duration); |
141 | 0 | return true; |
142 | 0 | } |
143 | | |
144 | | inline bool getStats(u_int16_t host_pool_id, u_int64_t *bytes, |
145 | 0 | u_int32_t *duration) { |
146 | 0 | HostPoolStats *hps; |
147 | 0 | if (!(hps = getPoolStats(host_pool_id))) return false; |
148 | 0 |
|
149 | 0 | hps->getStats(bytes, duration); |
150 | 0 | return true; |
151 | 0 | } |
152 | | |
153 | | void resetPoolsStats(u_int16_t pool_filter); |
154 | | |
155 | | #ifdef NTOPNG_PRO |
156 | | inline bool enforceQuotasPerPoolMember(u_int16_t pool_id) { |
157 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
158 | | ? enforce_quotas_per_pool_member[pool_id] |
159 | | : false); |
160 | | } |
161 | | inline bool enforceShapersPerPoolMember(u_int16_t pool_id) { |
162 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
163 | | ? enforce_shapers_per_pool_member[pool_id] |
164 | | : false); |
165 | | } |
166 | | inline u_int16_t getPoolShaper(u_int16_t pool_id) { |
167 | | return ((pool_id < max_num_pools) ? pool_shaper[pool_id] |
168 | | : DEFAULT_SHAPER_ID); |
169 | | } |
170 | | inline u_int32_t getPoolSchedule(u_int16_t pool_id) { |
171 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
172 | | ? schedule_bitmap[pool_id] |
173 | | : DEFAULT_TIME_SCHEDULE); |
174 | | } |
175 | | |
176 | | inline bool isChildrenSafePool(u_int16_t pool_id) { |
177 | | return ((pool_id < max_num_pools) ? children_safe[pool_id] : false); |
178 | | } |
179 | | |
180 | | inline bool forgeGlobalDns(u_int16_t pool_id) { |
181 | | return ((pool_id < max_num_pools) ? forge_global_dns[pool_id] : false); |
182 | | } |
183 | | |
184 | | inline u_int8_t getRoutingPolicy(u_int16_t pool_id) { |
185 | | return (((pool_id != NO_HOST_POOL_ID) && (pool_id < max_num_pools)) |
186 | | ? routing_policy_id[pool_id] |
187 | | : DEFAULT_ROUTING_TABLE_ID); |
188 | | } |
189 | | |
190 | | #endif |
191 | | }; |
192 | | |
193 | | #endif /* _HOST_POOLS_H_ */ |