/src/ntopng/src/VLANAddressTree.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * (C) 2017-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 | | #include "ntop_includes.h" |
23 | | |
24 | | /* **************************************** */ |
25 | | |
26 | 12 | VLANAddressTree::VLANAddressTree(ndpi_void_fn_t data_free_func) { |
27 | 12 | if(trace_new_delete) ntop->getTrace()->traceEvent(TRACE_NORMAL, "[new] %s", __FILE__); |
28 | 12 | free_func = data_free_func; |
29 | 12 | tree = new (std::nothrow) AddressTree *[MAX_NUM_VLAN]; |
30 | 12 | memset(tree, 0, sizeof(AddressTree *) * MAX_NUM_VLAN); |
31 | 12 | num_addresses = 0; |
32 | 12 | } |
33 | | |
34 | | /* **************************************** */ |
35 | | |
36 | 12 | VLANAddressTree::~VLANAddressTree() { |
37 | 49.1k | for (int i = 0; i < MAX_NUM_VLAN; i++) |
38 | 49.1k | if (tree[i]) delete tree[i]; |
39 | | |
40 | 12 | delete[] tree; |
41 | 12 | } |
42 | | |
43 | | /* **************************************** */ |
44 | | |
45 | | bool VLANAddressTree::addAddress(u_int16_t vlan_id, char *_net, |
46 | 0 | const int16_t user_data) { |
47 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
48 | |
|
49 | 0 | if (tree[vlan_id] |
50 | 0 | || (tree[vlan_id] = new (std::nothrow) AddressTree(true, free_func))) { |
51 | 0 | num_addresses++; |
52 | 0 | return tree[vlan_id]->addAddress(_net, user_data); |
53 | 0 | } |
54 | | |
55 | 0 | return false; |
56 | 0 | } |
57 | | |
58 | | /* **************************************** */ |
59 | | |
60 | | bool VLANAddressTree::addVLANAddressAndData(u_int16_t vlan_id, |
61 | | const char *_what, |
62 | 0 | void *user_data) { |
63 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
64 | |
|
65 | 0 | if (tree[vlan_id] |
66 | 0 | || (tree[vlan_id] = new (std::nothrow) AddressTree(true, free_func))) { |
67 | 0 | num_addresses++; |
68 | 0 | return tree[vlan_id]->addAddressAndData(_what, user_data); |
69 | 0 | } |
70 | | |
71 | 0 | return false; |
72 | 0 | } |
73 | | |
74 | | /* **************************************** */ |
75 | | |
76 | | bool VLANAddressTree::addAddresses(u_int16_t vlan_id, char *net, |
77 | 0 | const int16_t user_data) { |
78 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
79 | |
|
80 | 0 | if (tree[vlan_id] || |
81 | 0 | (tree[vlan_id] = new (std::nothrow) AddressTree(true, free_func))) { |
82 | 0 | num_addresses++; |
83 | 0 | return tree[vlan_id]->addAddresses(net, user_data); |
84 | 0 | } |
85 | | |
86 | 0 | return false; |
87 | 0 | } |
88 | | |
89 | | /* **************************************** */ |
90 | | |
91 | | int16_t VLANAddressTree::findAddress(u_int16_t vlan_id, int family, void *addr, |
92 | 0 | u_int8_t *network_mask_bits) { |
93 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
94 | |
|
95 | 0 | if (!tree[vlan_id]) return -1; |
96 | 0 | return tree[vlan_id]->findAddress(family, addr, network_mask_bits); |
97 | 0 | } |
98 | | |
99 | | /* **************************************** */ |
100 | | |
101 | 0 | int16_t VLANAddressTree::findMac(u_int16_t vlan_id, const u_int8_t addr[]) { |
102 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
103 | |
|
104 | 0 | if (!tree[vlan_id]) return -1; |
105 | 0 | return tree[vlan_id]->findMac(addr); |
106 | 0 | } |
107 | | |
108 | | /* **************************************** */ |
109 | | |
110 | | void *VLANAddressTree::findAndGetData(u_int16_t vlan_id, |
111 | 0 | IpAddress *ipa) const { |
112 | 0 | vlan_id &= 0xFFF; /* Make sure we use 12 bits */ |
113 | |
|
114 | 0 | if (!tree[vlan_id]) return NULL; |
115 | 0 | return tree[vlan_id]->matchAndGetData(ipa); |
116 | 0 | } |
117 | | |
118 | | /* **************************************** */ |