ntopng/include/AddressTree.h
2026-03-15 10:50:50 +01:00

94 lines
3.3 KiB
C++

/*
*
* (C) 2013-26 - ntop.org
*
*
* This program is free software; you can addresstribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#ifndef _ADDRESS_TREE_H_
#define _ADDRESS_TREE_H_
#include "ntop_includes.h"
class IpAddress;
class AddressTree {
protected:
u_int32_t numAddresses, numAddressesIPv4, numAddressesIPv6;
ndpi_patricia_tree_t* getPatricia(char* what);
ndpi_patricia_tree_t *ptree_v4, *ptree_v6;
std::map<u_int64_t, int64_t> macs;
ndpi_void_fn_t free_func;
RwLock updateLock;
void removePrefix(bool isV4, ndpi_prefix_t* prefix);
static void walk(ndpi_patricia_tree_t* ptree, ndpi_void_fn3_t func,
void* const user_data);
static bool removePrefix(ndpi_patricia_tree_t* ptree, ndpi_prefix_t* prefix);
void cleanup(ndpi_void_fn_t free_func);
public:
AddressTree(bool handleIPv6 = true, ndpi_void_fn_t data_free_func = NULL);
AddressTree(const AddressTree& at, ndpi_void_fn_t data_free_func = NULL);
virtual ~AddressTree();
void init(bool handleIPv6);
void cleanup();
inline u_int32_t getNumAddresses() const { return (numAddresses); }
inline u_int32_t getNumAddressesIPv4() const { return (numAddressesIPv4); }
inline u_int32_t getNumAddressesIPv6() const { return (numAddressesIPv6); }
inline bool isEmpty() { return (getNumAddresses() == 0); }
inline ndpi_patricia_tree_t* getTree(bool isV4) const {
return (isV4 ? ptree_v4 : ptree_v6);
}
bool addAddress(const char* _what, const int64_t user_data = -1);
bool addAddressAndData(const char* _what, void* user_data,
bool fail_if_already_present = false);
ndpi_patricia_node_t* addAddress(const IpAddress* const ipa);
ndpi_patricia_node_t* addAddress(const IpAddress* const ipa, int network_bits,
bool compact_after_add);
bool addAddresses(const char* net, const int64_t user_data = -1);
void getAddresses(lua_State* vm);
int64_t findAddress(int family, void* addr,
u_int8_t* network_mask_bits = NULL);
int64_t findMac(const u_int8_t addr[]);
int64_t find(const char* addr, u_int8_t* network_mask_bits = NULL);
/* Return true on match, false otherwise */
bool match(char* addr);
/* Return user data on success, NULL otherwise */
void* matchAndGetData(const char* addr);
/* Return node on success, NULL otherwise */
ndpi_patricia_node_t* matchAndGetNode(const char* addr);
ndpi_patricia_node_t* match(IpAddress* ipa, int network_bits);
void* matchAndGetData(IpAddress* ipa);
void dump();
void walk(ndpi_void_fn3_t func, void* const user_data);
char* serialize(char* buf, u_int buf_len);
bool deserialize(char* json);
};
#endif /* _ADDRESS_TREE_H_ */