Coverage Report

Created: 2026-07-25 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntopng/include/AddressTree.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2013-26 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can addresstribute 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 _ADDRESS_TREE_H_
23
#define _ADDRESS_TREE_H_
24
25
#include "ntop_includes.h"
26
27
class IpAddress;
28
29
class AddressTree {
30
 protected:
31
  u_int32_t numAddresses, numAddressesIPv4, numAddressesIPv6;
32
  ndpi_patricia_tree_t* getPatricia(char* what);
33
  ndpi_patricia_tree_t *ptree_v4, *ptree_v6;
34
  std::map<u_int64_t, int64_t> macs;
35
  ndpi_void_fn_t free_func;
36
  RwLock updateLock;
37
  bool lock_enabled;
38
39
  void removePrefix(bool isV4, ndpi_prefix_t* prefix);
40
  static void walk(ndpi_patricia_tree_t* ptree, ndpi_void_fn3_t func,
41
                   void* const user_data);
42
  static bool removePrefix(ndpi_patricia_tree_t* ptree, ndpi_prefix_t* prefix);
43
  void cleanup(ndpi_void_fn_t free_func);
44
45
 public:
46
  AddressTree(bool handleIPv6 = true, ndpi_void_fn_t data_free_func = NULL,
47
              bool use_locking = true);
48
  AddressTree(const AddressTree& at, ndpi_void_fn_t data_free_func = NULL,
49
              bool use_locking = true);
50
  virtual ~AddressTree();
51
52
  void init(bool handleIPv6);
53
  void cleanup();
54
55
8
  inline u_int32_t getNumAddresses() const { return (numAddresses); }
56
0
  inline u_int32_t getNumAddressesIPv4() const { return (numAddressesIPv4); }
57
0
  inline u_int32_t getNumAddressesIPv6() const { return (numAddressesIPv6); }
58
59
0
  inline bool isEmpty() { return (getNumAddresses() == 0); }
60
61
24.5k
  inline ndpi_patricia_tree_t* getTree(bool isV4) const {
62
24.5k
    return (isV4 ? ptree_v4 : ptree_v6);
63
24.5k
  }
64
65
  bool addAddress(const char* _what, const int64_t user_data = -1);
66
  bool addAddressAndData(const char* _what, void* user_data,
67
                         bool fail_if_already_present = false);
68
  ndpi_patricia_node_t* addAddress(const IpAddress* const ipa);
69
  ndpi_patricia_node_t* addAddress(const IpAddress* const ipa, int network_bits,
70
                                   bool compact_after_add);
71
  bool addAddresses(const char* net, const int64_t user_data = -1);
72
73
  void getAddresses(lua_State* vm);
74
75
  int64_t findAddress(int family, void* addr,
76
                      u_int8_t* network_mask_bits = NULL);
77
  int64_t findMac(const u_int8_t addr[]);
78
  int64_t find(const char* addr, u_int8_t* network_mask_bits = NULL);
79
80
  /* Return true on match, false otherwise */
81
  bool match(char* addr);
82
  /* Return user data on success, NULL otherwise */
83
  void* matchAndGetData(const char* addr);
84
  /* Return node on success, NULL otherwise */
85
  ndpi_patricia_node_t* matchAndGetNode(const char* addr);
86
87
  ndpi_patricia_node_t* match(IpAddress* ipa, int network_bits);
88
  void* matchAndGetData(IpAddress* ipa);
89
90
  void dump();
91
  void walk(ndpi_void_fn3_t func, void* const user_data);
92
93
  char* serialize(char* buf, u_int buf_len);
94
  bool deserialize(char* json);
95
};
96
97
#endif /* _ADDRESS_TREE_H_ */