/src/ntopng/include/IpAddress.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * (C) 2013-24 - 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 dnsServer : 1, dhcpServer : 1, smtpServer : 1, ntpServer : 1, |
32 | | imapServer : 1, popServer : 1, unused : 2; |
33 | | union { |
34 | | struct ndpi_in6_addr ipv6; |
35 | | u_int32_t ipv4; /* Host byte code */ |
36 | | } ipType; |
37 | | }; |
38 | | |
39 | | /* **************************************** */ |
40 | | |
41 | | class IpAddress { |
42 | | private: |
43 | | struct ipAddress addr; |
44 | | u_int32_t ip_key; |
45 | | |
46 | | char* intoa(char* buf, u_short bufLen, u_int8_t bitmask) const; |
47 | | void compute_key(); |
48 | | |
49 | | public: |
50 | | IpAddress(); |
51 | | IpAddress(const IpAddress& ipa); |
52 | | |
53 | | void checkIP(); |
54 | | bool isEmpty() const; |
55 | 0 | inline void reset() { memset(&addr, 0, sizeof(addr)); } |
56 | 87.8k | inline bool isIPv4() const { return ((addr.ipVersion == 4) ? true : false); } |
57 | 12.2k | inline bool isIPv6() const { return ((addr.ipVersion == 6) ? true : false); } |
58 | | char* get_ip_hex(char* buf, u_int buf_len); |
59 | 78.9k | inline u_int32_t get_ipv4() const { |
60 | 78.9k | return ((addr.ipVersion == 4) ? addr.ipType.ipv4 : 0); |
61 | 78.9k | } |
62 | 18.2k | inline const struct ndpi_in6_addr* get_ipv6() const { |
63 | 18.2k | return ((addr.ipVersion == 6) ? &addr.ipType.ipv6 : NULL); |
64 | 18.2k | } |
65 | 645 | inline const struct ipAddress* getIP() const { return (&addr); }; |
66 | 0 | inline bool equal(u_int32_t ipv4_addr) const { |
67 | 0 | if ((addr.ipVersion == 4) && (addr.ipType.ipv4 == ipv4_addr)) |
68 | 0 | return (true); |
69 | 0 | else |
70 | 0 | return (false); |
71 | 0 | }; |
72 | 0 | inline bool equal(struct ndpi_in6_addr* ip6_addr) const { |
73 | 0 | if ((addr.ipVersion == 6) && (memcmp(&addr.ipType.ipv6, ip6_addr, |
74 | 0 | sizeof(struct ndpi_in6_addr)) == 0)) |
75 | 0 | return (true); |
76 | 0 | else |
77 | 0 | return (false); |
78 | 0 | }; |
79 | 50.2k | inline bool equal(const IpAddress* const _ip) const { return((this->compare(_ip) == 0) ? true : false); }; |
80 | | int compare(const IpAddress* const ip) const; |
81 | 292k | inline u_int32_t key() const { return (ip_key); }; |
82 | 153k | inline void set(u_int32_t _ipv4) { |
83 | 153k | addr.ipVersion = 4, addr.ipType.ipv4 = _ipv4; |
84 | 153k | compute_key(); |
85 | 153k | } |
86 | 46.5k | inline void set(struct ndpi_in6_addr* _ipv6) { |
87 | 46.5k | addr.ipVersion = 6, |
88 | 46.5k | memcpy(&addr.ipType.ipv6, _ipv6, sizeof(struct ndpi_in6_addr)); |
89 | 46.5k | addr.privateIP = false; |
90 | 46.5k | compute_key(); |
91 | 46.5k | } |
92 | 0 | inline void set(struct in6_addr* _ipv6) { |
93 | 0 | addr.ipVersion = 6, |
94 | 0 | memcpy(&addr.ipType.ipv6.u6_addr, _ipv6->s6_addr, sizeof(_ipv6->s6_addr)); |
95 | 0 | addr.privateIP = false; |
96 | 0 | compute_key(); |
97 | 0 | } |
98 | 20.1k | inline void set(const IpAddress* const ip) { |
99 | 20.1k | memcpy(&addr, &ip->addr, sizeof(struct ipAddress)); |
100 | 20.1k | ip_key = ip->ip_key; |
101 | 20.1k | }; |
102 | 0 | inline void set(const struct ipAddress* const ip) { |
103 | 0 | memcpy(&addr, ip, sizeof(struct ipAddress)); |
104 | 0 | compute_key(); |
105 | 0 | }; |
106 | | void set(union usa* ip); |
107 | | void set(const char* ip); |
108 | | void reloadBlacklist(ndpi_detection_module_struct* ndpi_struct); |
109 | 0 | inline bool isLoopbackAddress() const { return (addr.loopbackIP); }; |
110 | 801 | inline bool isPrivateAddress() const { return (addr.privateIP); }; |
111 | 153k | inline bool isMulticastAddress() const { return (addr.multicastIP); }; |
112 | 176k | inline bool isBroadcastAddress() const { return (addr.broadcastIP); }; |
113 | 29.8k | inline bool isBlacklistedAddress() const { return (addr.blacklistedIP); }; |
114 | 0 | inline bool isBroadMulticastAddress() const { |
115 | 0 | return (addr.broadcastIP || addr.multicastIP); |
116 | 0 | }; |
117 | 70.0k | inline bool isNonEmptyUnicastAddress() const { |
118 | 70.0k | return (!isMulticastAddress() && !isBroadcastAddress() && !isEmpty()); |
119 | 70.0k | }; |
120 | 54.5k | inline u_int8_t getVersion() const { return (addr.ipVersion); }; |
121 | 0 | inline void setVersion(u_int8_t version) { addr.ipVersion = version; }; |
122 | | |
123 | 0 | inline bool isDhcpServer() const { return (addr.dhcpServer); } |
124 | 0 | inline void setDhcpServer() { addr.dhcpServer = true; } |
125 | 0 | inline bool isDnsServer() const { return (addr.dnsServer); } |
126 | 0 | inline void setDnsServer() { addr.dnsServer = true; } |
127 | 0 | inline bool isSmtpServer() const { return (addr.smtpServer); } |
128 | 0 | inline void setSmtpServer() { addr.smtpServer = true; } |
129 | 0 | inline bool isImapServer() const { return (addr.imapServer); } |
130 | 0 | inline void setImapServer() { addr.imapServer = true; } |
131 | 0 | inline bool isPopServer() const { return (addr.popServer); } |
132 | 0 | inline void setPopServer() { addr.popServer = true; } |
133 | 0 | inline bool isNtpServer() const { return (addr.ntpServer); } |
134 | 0 | inline void setNtpServer() { addr.ntpServer = true; } |
135 | | |
136 | | char* print(char* str, u_int str_len, u_int8_t bitmask = 0xFF) const; |
137 | | char* printMask(char* str, u_int str_len, bool isLocalIP); |
138 | | bool isLocalHost() const; |
139 | | bool isLocalHost(int16_t* network_id) const; |
140 | | bool isLocalInterfaceAddress(); |
141 | | bool get_sockaddr(struct sockaddr** const sa, ssize_t* const sa_len) const; |
142 | | |
143 | | json_object* getJSONObject(); |
144 | | bool match(const AddressTree* const tree) const; |
145 | | void* findAddress(AddressTree* ptree); |
146 | | void dump(); |
147 | | void incCardinality(Cardinality* c); |
148 | | }; |
149 | | |
150 | | #endif /* _IP_ADDRESS_H_ */ |