Coverage Report

Created: 2026-05-14 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntopng/include/IpAddress.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2013-26 - 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 _IP_ADDRESS_H_
23
#define _IP_ADDRESS_H_
24
25
#include "ntop_includes.h"
26
27
struct ipAddress {
28
  u_int8_t ipVersion : 3 /* Either 4 or 6 */, loopbackIP : 1, privateIP : 1,
29
      multicastIP : 1, broadcastIP : 1, blacklistedIP : 1, localIP : 1;
30
31
  u_int8_t gateway : 1, unused : 7;
32
  u_int16_t services_bitmap;
33
34
  union {
35
    struct ndpi_in6_addr ipv6;
36
    u_int32_t ipv4; /* Host byte code */
37
  } ipType;
38
};
39
40
/* **************************************** */
41
42
class IpAddress {
43
 private:
44
  struct ipAddress addr;
45
  u_int32_t ip_key;
46
47
  char* intoa(char* buf, u_short bufLen, u_int8_t bitmask) const;
48
  void compute_key();
49
50
 public:
51
  IpAddress();
52
  IpAddress(const IpAddress& ipa);
53
  IpAddress(const struct ipAddress ipa);
54
55
  void checkIP();
56
  bool isEmpty() const;
57
0
  inline void reset() { memset(&addr, 0, sizeof(addr)); }
58
183k
  inline bool isIPv4() const { return ((addr.ipVersion == 4) ? true : false); }
59
5.09k
  inline bool isIPv6() const { return ((addr.ipVersion == 6) ? true : false); }
60
  char* get_ip_hex(char* buf, u_int buf_len);
61
202k
  inline u_int32_t get_ipv4() const {
62
202k
    return ((addr.ipVersion == 4) ? addr.ipType.ipv4 : 0);
63
202k
  }
64
13.8k
  inline const struct ndpi_in6_addr* get_ipv6() const {
65
13.8k
    return ((addr.ipVersion == 6) ? &addr.ipType.ipv6 : NULL);
66
13.8k
  }
67
68
  void fillIP(ndpi_ip_addr_t* ip_addr);
69
10.0k
  inline const struct ipAddress* getIP() const { return (&addr); };
70
98.5k
  inline u_int8_t getVersion() { return (addr.ipVersion); }
71
0
  inline bool equal(u_int32_t ipv4_addr) const {
72
0
    if ((addr.ipVersion == 4) && (addr.ipType.ipv4 == ipv4_addr))
73
0
      return (true);
74
0
    else
75
0
      return (false);
76
0
  };
77
0
  inline bool equal(struct ndpi_in6_addr* ip6_addr) const {
78
0
    if ((addr.ipVersion == 6) && (memcmp(&addr.ipType.ipv6, ip6_addr,
79
0
                                         sizeof(struct ndpi_in6_addr)) == 0))
80
0
      return (true);
81
0
    else
82
0
      return (false);
83
0
  };
84
45.7k
  inline bool equal(const IpAddress* const _ip) const {
85
45.7k
    return ((this->compare(_ip) == 0) ? true : false);
86
45.7k
  };
87
  int compare(const IpAddress* const ip) const;
88
326k
  inline u_int32_t key() const { return (ip_key); };
89
132k
  inline void set(u_int32_t _ipv4) {
90
132k
    addr.ipVersion = 4, addr.ipType.ipv4 = _ipv4;
91
132k
    compute_key();
92
132k
  }
93
13.8k
  inline void set(struct ndpi_in6_addr* _ipv6) {
94
13.8k
    addr.ipVersion = 6,
95
13.8k
    memcpy(&addr.ipType.ipv6, _ipv6, sizeof(struct ndpi_in6_addr));
96
13.8k
    addr.privateIP = false;
97
13.8k
    compute_key();
98
13.8k
  }
99
0
  inline void set(struct in6_addr* _ipv6) {
100
0
    addr.ipVersion = 6,
101
0
    memcpy(&addr.ipType.ipv6.u6_addr, _ipv6->s6_addr, sizeof(_ipv6->s6_addr));
102
0
    addr.privateIP = false;
103
0
    compute_key();
104
0
  }
105
39.6k
  inline void set(const IpAddress* const ip) {
106
39.6k
    memcpy(&addr, &ip->addr, sizeof(struct ipAddress));
107
39.6k
    ip_key = ip->ip_key;
108
39.6k
  };
109
0
  inline void set(const struct ipAddress* const ip) {
110
0
    memcpy(&addr, ip, sizeof(struct ipAddress));
111
0
    compute_key();
112
0
  };
113
  void set(union usa* ip);
114
  void set(const char* ip);
115
  void reloadBlacklist(ndpi_detection_module_struct* ndpi_struct);
116
0
  inline bool isLoopbackAddress() const { return (addr.loopbackIP); };
117
0
  inline bool isPrivateAddress() const { return (addr.privateIP); };
118
350k
  inline bool isMulticastAddress() const { return (addr.multicastIP); };
119
379k
  inline bool isBroadcastAddress() const { return (addr.broadcastIP); };
120
53.6k
  inline bool isBlacklistedAddress() const { return (addr.blacklistedIP); };
121
0
  inline bool isBroadMulticastAddress() const {
122
0
    return (addr.broadcastIP || addr.multicastIP);
123
0
  };
124
82.7k
  inline bool isNonEmptyUnicastAddress() const {
125
82.7k
    return (!isMulticastAddress() && !isBroadcastAddress() && !isEmpty());
126
82.7k
  };
127
0
  inline bool isPublicAddress() const {
128
0
    return ((!isPrivateAddress()) && isNonEmptyUnicastAddress());
129
0
  }
130
0
  inline u_int8_t getVersion() const { return (addr.ipVersion); };
131
0
  inline void setVersion(u_int8_t version) { addr.ipVersion = version; };
132
133
643k
  inline bool providesService(int service_enum) const {
134
643k
    return (addr.services_bitmap & (1 << service_enum));
135
643k
  }
136
0
  inline void setService(int service_enum) {
137
0
    if (!(addr.services_bitmap & (1 << service_enum)))
138
0
      addr.services_bitmap |= 1 << service_enum;
139
0
  }
140
0
  inline u_int16_t getServicesMap() { return addr.services_bitmap; }
141
142
0
  inline bool isGateway() const { return (addr.gateway); }
143
15.6k
  inline void setGateway(bool is_gateway) { addr.gateway = is_gateway; }
144
  /* NOTE: update Host::updateView() when adding new services */
145
146
  char* get_country(char* buf, u_int buf_len);
147
  char* print(char* str, u_int str_len, u_int8_t bitmask = 0xFF) const;
148
  char* printMask(char* str, u_int str_len, bool isLocalIP);
149
  bool isLocalHost() const;
150
  bool isLocalHost(int32_t* network_id) const;
151
  bool isLocalInterfaceAddress();
152
  bool get_sockaddr(struct sockaddr** const sa, ssize_t* const sa_len) const;
153
154
  json_object* getJSONObject();
155
  bool match(const AddressTree* const tree) const;
156
  void* findAddress(AddressTree* ptree);
157
  void dump();
158
  void incCardinality(Cardinality* c);
159
};
160
161
#endif /* _IP_ADDRESS_H_ */