/src/pdns/pdns/dnsdistdist/iputils.hh
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * This file is part of PowerDNS or dnsdist. |
3 | | * Copyright -- PowerDNS.COM B.V. and its contributors |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of version 2 of the GNU General Public License as |
7 | | * published by the Free Software Foundation. |
8 | | * |
9 | | * In addition, for the avoidance of any doubt, permission is granted to |
10 | | * link this program with OpenSSL and to (re)distribute the binaries |
11 | | * produced as the result of such linking. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
21 | | */ |
22 | | #pragma once |
23 | | #include <string> |
24 | | #include <sys/socket.h> |
25 | | #include <netinet/in.h> |
26 | | #include <arpa/inet.h> |
27 | | #include <iostream> |
28 | | #include <cstdio> |
29 | | #include <functional> |
30 | | #include "pdnsexception.hh" |
31 | | #include "misc.hh" |
32 | | #include <netdb.h> |
33 | | #include <sstream> |
34 | | #include <sys/un.h> |
35 | | |
36 | | #include "namespaces.hh" |
37 | | |
38 | | #ifdef __APPLE__ |
39 | | #include <libkern/OSByteOrder.h> |
40 | | |
41 | | #define htobe16(x) OSSwapHostToBigInt16(x) |
42 | | #define htole16(x) OSSwapHostToLittleInt16(x) |
43 | | #define be16toh(x) OSSwapBigToHostInt16(x) |
44 | | #define le16toh(x) OSSwapLittleToHostInt16(x) |
45 | | |
46 | | #define htobe32(x) OSSwapHostToBigInt32(x) |
47 | | #define htole32(x) OSSwapHostToLittleInt32(x) |
48 | | #define be32toh(x) OSSwapBigToHostInt32(x) |
49 | | #define le32toh(x) OSSwapLittleToHostInt32(x) |
50 | | |
51 | | #define htobe64(x) OSSwapHostToBigInt64(x) |
52 | | #define htole64(x) OSSwapHostToLittleInt64(x) |
53 | | #define be64toh(x) OSSwapBigToHostInt64(x) |
54 | | #define le64toh(x) OSSwapLittleToHostInt64(x) |
55 | | #endif |
56 | | |
57 | | #ifdef __sun |
58 | | |
59 | | #define htobe16(x) BE_16(x) |
60 | | #define htole16(x) LE_16(x) |
61 | | #define be16toh(x) BE_IN16(&(x)) |
62 | | #define le16toh(x) LE_IN16(&(x)) |
63 | | |
64 | | #define htobe32(x) BE_32(x) |
65 | | #define htole32(x) LE_32(x) |
66 | | #define be32toh(x) BE_IN32(&(x)) |
67 | | #define le32toh(x) LE_IN32(&(x)) |
68 | | |
69 | | #define htobe64(x) BE_64(x) |
70 | | #define htole64(x) LE_64(x) |
71 | | #define be64toh(x) BE_IN64(&(x)) |
72 | | #define le64toh(x) LE_IN64(&(x)) |
73 | | |
74 | | #endif |
75 | | |
76 | | #ifdef __FreeBSD__ |
77 | | #include <sys/endian.h> |
78 | | #endif |
79 | | |
80 | | #if defined(__NetBSD__) && defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR) |
81 | | // The IP_PKTINFO option in NetBSD was incompatible with Linux until a |
82 | | // change that also introduced IP_SENDSRCADDR for FreeBSD compatibility. |
83 | | #undef IP_PKTINFO |
84 | | #endif |
85 | | |
86 | | union ComboAddress |
87 | | { |
88 | | sockaddr_in sin4{}; |
89 | | sockaddr_in6 sin6; |
90 | | |
91 | | bool operator==(const ComboAddress& rhs) const |
92 | 0 | { |
93 | 0 | if (std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { |
94 | 0 | return false; |
95 | 0 | } |
96 | 0 | if (sin4.sin_family == AF_INET) { |
97 | 0 | return sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr; |
98 | 0 | } |
99 | 0 | return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) == 0; |
100 | 0 | } |
101 | | |
102 | | bool operator!=(const ComboAddress& rhs) const |
103 | 0 | { |
104 | 0 | return (!operator==(rhs)); |
105 | 0 | } |
106 | | |
107 | | bool operator<(const ComboAddress& rhs) const |
108 | 0 | { |
109 | 0 | if (sin4.sin_family == 0) { |
110 | 0 | return false; |
111 | 0 | } |
112 | 0 | if (std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { |
113 | 0 | return true; |
114 | 0 | } |
115 | 0 | if (std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { |
116 | 0 | return false; |
117 | 0 | } |
118 | 0 | if (sin4.sin_family == AF_INET) { |
119 | 0 | return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr; |
120 | 0 | } |
121 | 0 | return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0; |
122 | 0 | } |
123 | | |
124 | | bool operator>(const ComboAddress& rhs) const |
125 | 0 | { |
126 | 0 | return rhs.operator<(*this); |
127 | 0 | } |
128 | | |
129 | | struct addressPortOnlyHash |
130 | | { |
131 | | uint32_t operator()(const ComboAddress& address) const |
132 | 0 | { |
133 | 0 | // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) |
134 | 0 | if (address.sin4.sin_family == AF_INET) { |
135 | 0 | const auto* start = reinterpret_cast<const unsigned char*>(&address.sin4.sin_addr.s_addr); |
136 | 0 | auto tmp = burtle(start, 4, 0); |
137 | 0 | return burtle(reinterpret_cast<const uint8_t*>(&address.sin4.sin_port), 2, tmp); |
138 | 0 | } |
139 | 0 | const auto* start = reinterpret_cast<const unsigned char*>(&address.sin6.sin6_addr.s6_addr); |
140 | 0 | auto tmp = burtle(start, 16, 0); |
141 | 0 | return burtle(reinterpret_cast<const unsigned char*>(&address.sin6.sin6_port), 2, tmp); |
142 | 0 | // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) |
143 | 0 | } |
144 | | }; |
145 | | |
146 | | struct addressOnlyHash |
147 | | { |
148 | | uint32_t operator()(const ComboAddress& address) const |
149 | 0 | { |
150 | 0 | const unsigned char* start = nullptr; |
151 | 0 | uint32_t len = 0; |
152 | 0 | // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) |
153 | 0 | if (address.sin4.sin_family == AF_INET) { |
154 | 0 | start = reinterpret_cast<const unsigned char*>(&address.sin4.sin_addr.s_addr); |
155 | 0 | len = 4; |
156 | 0 | } |
157 | 0 | else { |
158 | 0 | start = reinterpret_cast<const unsigned char*>(&address.sin6.sin6_addr.s6_addr); |
159 | 0 | len = 16; |
160 | 0 | } |
161 | 0 | // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) |
162 | 0 | return burtle(start, len, 0); |
163 | 0 | } |
164 | | }; |
165 | | |
166 | | struct addressOnlyLessThan |
167 | | { |
168 | | bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const |
169 | 0 | { |
170 | 0 | if (lhs.sin4.sin_family < rhs.sin4.sin_family) { |
171 | 0 | return true; |
172 | 0 | } |
173 | 0 | if (lhs.sin4.sin_family > rhs.sin4.sin_family) { |
174 | 0 | return false; |
175 | 0 | } |
176 | 0 | if (lhs.sin4.sin_family == AF_INET) { |
177 | 0 | return lhs.sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr; |
178 | 0 | } |
179 | 0 | return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) < 0; |
180 | 0 | } |
181 | | }; |
182 | | |
183 | | struct addressOnlyEqual |
184 | | { |
185 | | bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const |
186 | 0 | { |
187 | 0 | if (lhs.sin4.sin_family != rhs.sin4.sin_family) { |
188 | 0 | return false; |
189 | 0 | } |
190 | 0 | if (lhs.sin4.sin_family == AF_INET) { |
191 | 0 | return lhs.sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr; |
192 | 0 | } |
193 | 0 | return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) == 0; |
194 | 0 | } |
195 | | }; |
196 | | |
197 | | [[nodiscard]] socklen_t getSocklen() const |
198 | 0 | { |
199 | 0 | if (sin4.sin_family == AF_INET) { |
200 | 0 | return sizeof(sin4); |
201 | 0 | } |
202 | 0 | return sizeof(sin6); |
203 | 0 | } |
204 | | |
205 | | ComboAddress() |
206 | 436 | { |
207 | 436 | sin4.sin_family = AF_INET; |
208 | 436 | sin4.sin_addr.s_addr = 0; |
209 | 436 | sin4.sin_port = 0; |
210 | 436 | sin6.sin6_scope_id = 0; |
211 | 436 | sin6.sin6_flowinfo = 0; |
212 | 436 | } |
213 | | |
214 | | ComboAddress(const struct sockaddr* socketAddress, socklen_t salen) |
215 | 0 | { |
216 | 0 | setSockaddr(socketAddress, salen); |
217 | 0 | }; |
218 | | |
219 | | ComboAddress(const struct sockaddr_in6* socketAddress) |
220 | 0 | { |
221 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
222 | 0 | setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in6)); |
223 | 0 | }; |
224 | | |
225 | | ComboAddress(const struct sockaddr_in* socketAddress) |
226 | 0 | { |
227 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
228 | 0 | setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in)); |
229 | 0 | }; |
230 | | |
231 | | void setSockaddr(const struct sockaddr* socketAddress, socklen_t salen) |
232 | 0 | { |
233 | 0 | if (salen > sizeof(struct sockaddr_in6)) { |
234 | 0 | throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6"); |
235 | 0 | } |
236 | 0 | memcpy(this, socketAddress, salen); |
237 | 0 | } |
238 | | |
239 | | // 'port' sets a default value in case 'str' does not set a port |
240 | | explicit ComboAddress(const string& str, uint16_t port = 0) |
241 | 2 | { |
242 | 2 | memset(&sin6, 0, sizeof(sin6)); |
243 | 2 | sin4.sin_family = AF_INET; |
244 | 2 | sin4.sin_port = 0; |
245 | 2 | if (makeIPv4sockaddr(str, &sin4) != 0) { |
246 | 0 | sin6.sin6_family = AF_INET6; |
247 | 0 | if (makeIPv6sockaddr(str, &sin6) < 0) { |
248 | 0 | throw PDNSException("Unable to convert presentation address '" + str + "'"); |
249 | 0 | } |
250 | 0 | } |
251 | 2 | if (sin4.sin_port == 0) { // 'str' overrides port! |
252 | 0 | sin4.sin_port = htons(port); |
253 | 0 | } |
254 | 2 | } |
255 | | |
256 | | [[nodiscard]] bool isIPv6() const |
257 | 0 | { |
258 | 0 | return sin4.sin_family == AF_INET6; |
259 | 0 | } |
260 | | [[nodiscard]] bool isIPv4() const |
261 | 60 | { |
262 | 60 | return sin4.sin_family == AF_INET; |
263 | 60 | } |
264 | | |
265 | | [[nodiscard]] bool isMappedIPv4() const |
266 | 0 | { |
267 | 0 | if (sin4.sin_family != AF_INET6) { |
268 | 0 | return false; |
269 | 0 | } |
270 | 0 |
|
271 | 0 | int iter = 0; |
272 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
273 | 0 | const auto* ptr = reinterpret_cast<const unsigned char*>(&sin6.sin6_addr.s6_addr); |
274 | 0 | for (iter = 0; iter < 10; ++iter) { |
275 | 0 | if (ptr[iter] != 0) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
276 | 0 | return false; |
277 | 0 | } |
278 | 0 | } |
279 | 0 | for (; iter < 12; ++iter) { |
280 | 0 | if (ptr[iter] != 0xff) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
281 | 0 | return false; |
282 | 0 | } |
283 | 0 | } |
284 | 0 | return true; |
285 | 0 | } |
286 | | |
287 | | [[nodiscard]] bool isUnspecified() const |
288 | 0 | { |
289 | 0 | const ComboAddress unspecifiedV4("0.0.0.0:0"); |
290 | 0 | const ComboAddress unspecifiedV6("[::]:0"); |
291 | 0 | return *this == unspecifiedV4 || *this == unspecifiedV6; |
292 | 0 | } |
293 | | |
294 | | [[nodiscard]] ComboAddress mapToIPv4() const |
295 | 0 | { |
296 | 0 | if (!isMappedIPv4()) { |
297 | 0 | throw PDNSException("ComboAddress can't map non-mapped IPv6 address back to IPv4"); |
298 | 0 | } |
299 | 0 | ComboAddress ret; |
300 | 0 | ret.sin4.sin_family = AF_INET; |
301 | 0 | ret.sin4.sin_port = sin4.sin_port; |
302 | 0 |
|
303 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
304 | 0 | const auto* ptr = reinterpret_cast<const unsigned char*>(&sin6.sin6_addr.s6_addr); |
305 | 0 | ptr += (sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
306 | 0 | memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr)); |
307 | 0 | return ret; |
308 | 0 | } |
309 | | |
310 | | [[nodiscard]] string toString() const |
311 | 0 | { |
312 | 0 | std::array<char, 1024> host{}; |
313 | 0 | if (sin4.sin_family != 0) { |
314 | | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
315 | 0 | int retval = getnameinfo(reinterpret_cast<const struct sockaddr*>(this), getSocklen(), host.data(), host.size(), nullptr, 0, NI_NUMERICHOST); |
316 | 0 | if (retval == 0) { |
317 | 0 | return host.data(); |
318 | 0 | } |
319 | 0 | return "invalid " + string(gai_strerror(retval)); |
320 | 0 | } |
321 | 0 | return "invalid"; |
322 | 0 | } |
323 | | |
324 | | //! Ignores any interface specifiers possibly available in the sockaddr data. |
325 | | [[nodiscard]] string toStringNoInterface() const |
326 | 0 | { |
327 | 0 | std::array<char, 1024> host{}; |
328 | 0 | if (sin4.sin_family == AF_INET) { |
329 | 0 | const auto* ret = inet_ntop(sin4.sin_family, &sin4.sin_addr, host.data(), host.size()); |
330 | 0 | if (ret != nullptr) { |
331 | 0 | return host.data(); |
332 | 0 | } |
333 | 0 | } |
334 | 0 | else if (sin4.sin_family == AF_INET6) { |
335 | 0 | const auto* ret = inet_ntop(sin4.sin_family, &sin6.sin6_addr, host.data(), host.size()); |
336 | 0 | if (ret != nullptr) { |
337 | 0 | return host.data(); |
338 | 0 | } |
339 | 0 | } |
340 | 0 | else { |
341 | 0 | return "invalid"; |
342 | 0 | } |
343 | 0 | return "invalid " + stringerror(); |
344 | 0 | } |
345 | | |
346 | | [[nodiscard]] string toStringReversed() const |
347 | 0 | { |
348 | 0 | if (isIPv4()) { |
349 | 0 | const auto address = ntohl(sin4.sin_addr.s_addr); |
350 | 0 | auto aaa = (address >> 0) & 0xFF; |
351 | 0 | auto bbb = (address >> 8) & 0xFF; |
352 | 0 | auto ccc = (address >> 16) & 0xFF; |
353 | 0 | auto ddd = (address >> 24) & 0xFF; |
354 | 0 | return std::to_string(aaa) + "." + std::to_string(bbb) + "." + std::to_string(ccc) + "." + std::to_string(ddd); |
355 | 0 | } |
356 | 0 | const auto* addr = &sin6.sin6_addr; |
357 | 0 | std::stringstream res{}; |
358 | 0 | res << std::hex; |
359 | 0 | for (int i = 15; i >= 0; i--) { |
360 | 0 | auto byte = addr->s6_addr[i]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) |
361 | 0 | res << ((byte >> 0) & 0xF) << "."; |
362 | 0 | res << ((byte >> 4) & 0xF); |
363 | 0 | if (i != 0) { |
364 | 0 | res << "."; |
365 | 0 | } |
366 | 0 | } |
367 | 0 | return res.str(); |
368 | 0 | } |
369 | | |
370 | | [[nodiscard]] string toStringWithPort() const |
371 | 0 | { |
372 | 0 | if (sin4.sin_family == AF_INET) { |
373 | 0 | return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); |
374 | 0 | } |
375 | 0 | return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); |
376 | 0 | } |
377 | | |
378 | | [[nodiscard]] string toStringWithPortExcept(int port) const |
379 | 0 | { |
380 | 0 | if (ntohs(sin4.sin_port) == port) { |
381 | 0 | return toString(); |
382 | 0 | } |
383 | 0 | if (sin4.sin_family == AF_INET) { |
384 | 0 | return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); |
385 | 0 | } |
386 | 0 | return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); |
387 | 0 | } |
388 | | |
389 | | [[nodiscard]] string toLogString() const |
390 | 0 | { |
391 | 0 | return toStringWithPortExcept(53); |
392 | 0 | } |
393 | | |
394 | | [[nodiscard]] string toStructuredLogString() const |
395 | 0 | { |
396 | 0 | return toStringWithPort(); |
397 | 0 | } |
398 | | |
399 | | [[nodiscard]] string toByteString() const |
400 | 0 | { |
401 | 0 | // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) |
402 | 0 | if (isIPv4()) { |
403 | 0 | return {reinterpret_cast<const char*>(&sin4.sin_addr.s_addr), sizeof(sin4.sin_addr.s_addr)}; |
404 | 0 | } |
405 | 0 | return {reinterpret_cast<const char*>(&sin6.sin6_addr.s6_addr), sizeof(sin6.sin6_addr.s6_addr)}; |
406 | 0 | // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) |
407 | 0 | } |
408 | | |
409 | | void truncate(unsigned int bits) noexcept; |
410 | | |
411 | | [[nodiscard]] uint16_t getNetworkOrderPort() const noexcept |
412 | 0 | { |
413 | 0 | return sin4.sin_port; |
414 | 0 | } |
415 | | [[nodiscard]] uint16_t getPort() const noexcept |
416 | 0 | { |
417 | 0 | return ntohs(getNetworkOrderPort()); |
418 | 0 | } |
419 | | void setPort(uint16_t port) |
420 | 0 | { |
421 | 0 | sin4.sin_port = htons(port); |
422 | 0 | } |
423 | | |
424 | | void reset() |
425 | 60 | { |
426 | 60 | memset(&sin6, 0, sizeof(sin6)); |
427 | 60 | } |
428 | | |
429 | | //! Get the total number of address bits (either 32 or 128 depending on IP version) |
430 | | [[nodiscard]] uint8_t getBits() const |
431 | 0 | { |
432 | 0 | if (isIPv4()) { |
433 | 0 | return 32; |
434 | 0 | } |
435 | 0 | if (isIPv6()) { |
436 | 0 | return 128; |
437 | 0 | } |
438 | 0 | return 0; |
439 | 0 | } |
440 | | /** Get the value of the bit at the provided bit index. When the index >= 0, |
441 | | the index is relative to the LSB starting at index zero. When the index < 0, |
442 | | the index is relative to the MSB starting at index -1 and counting down. |
443 | | */ |
444 | | [[nodiscard]] bool getBit(int index) const |
445 | 0 | { |
446 | 0 | if (isIPv4()) { |
447 | 0 | if (index >= 32) { |
448 | 0 | return false; |
449 | 0 | } |
450 | 0 | if (index < 0) { |
451 | 0 | if (index < -32) { |
452 | 0 | return false; |
453 | 0 | } |
454 | 0 | index = 32 + index; |
455 | 0 | } |
456 | | |
457 | 0 | uint32_t ls_addr = ntohl(sin4.sin_addr.s_addr); |
458 | |
|
459 | 0 | return ((ls_addr & (1U << index)) != 0x00000000); |
460 | 0 | } |
461 | 0 | if (isIPv6()) { |
462 | 0 | if (index >= 128) { |
463 | 0 | return false; |
464 | 0 | } |
465 | 0 | if (index < 0) { |
466 | 0 | if (index < -128) { |
467 | 0 | return false; |
468 | 0 | } |
469 | 0 | index = 128 + index; |
470 | 0 | } |
471 | | |
472 | 0 | const auto* ls_addr = reinterpret_cast<const uint8_t*>(sin6.sin6_addr.s6_addr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) |
473 | 0 | uint8_t byte_idx = index / 8; |
474 | 0 | uint8_t bit_idx = index % 8; |
475 | |
|
476 | 0 | return ((ls_addr[15 - byte_idx] & (1U << bit_idx)) != 0x00); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
477 | 0 | } |
478 | 0 | return false; |
479 | 0 | } |
480 | | |
481 | | /*! Returns a comma-separated string of IP addresses |
482 | | * |
483 | | * \param c An stl container with ComboAddresses |
484 | | * \param withPort Also print the port (default true) |
485 | | * \param portExcept Print the port, except when this is the port (default 53) |
486 | | */ |
487 | | template <template <class...> class Container, class... Args> |
488 | | static string caContainerToString(const Container<ComboAddress, Args...>& container, const bool withPort = true, const uint16_t portExcept = 53) |
489 | | { |
490 | | vector<string> strs; |
491 | | for (const auto& address : container) { |
492 | | if (withPort) { |
493 | | strs.push_back(address.toStringWithPortExcept(portExcept)); |
494 | | continue; |
495 | | } |
496 | | strs.push_back(address.toString()); |
497 | | } |
498 | | return boost::join(strs, ","); |
499 | | }; |
500 | | }; |
501 | | |
502 | | union SockaddrWrapper |
503 | | { |
504 | | sockaddr_in sin4{}; |
505 | | sockaddr_in6 sin6; |
506 | | sockaddr_un sinun; |
507 | | |
508 | | [[nodiscard]] socklen_t getSocklen() const |
509 | 0 | { |
510 | 0 | if (sin4.sin_family == AF_INET) { |
511 | 0 | return sizeof(sin4); |
512 | 0 | } |
513 | 0 | if (sin6.sin6_family == AF_INET6) { |
514 | 0 | return sizeof(sin6); |
515 | 0 | } |
516 | 0 | if (sinun.sun_family == AF_UNIX) { |
517 | 0 | return sizeof(sinun); |
518 | 0 | } |
519 | 0 | return 0; |
520 | 0 | } |
521 | | |
522 | | SockaddrWrapper() |
523 | 0 | { |
524 | 0 | sin4.sin_family = AF_INET; |
525 | 0 | sin4.sin_addr.s_addr = 0; |
526 | 0 | sin4.sin_port = 0; |
527 | 0 | } |
528 | | |
529 | | SockaddrWrapper(const struct sockaddr* socketAddress, socklen_t salen) |
530 | 0 | { |
531 | 0 | setSockaddr(socketAddress, salen); |
532 | 0 | }; |
533 | | |
534 | | SockaddrWrapper(const struct sockaddr_in6* socketAddress) |
535 | 0 | { |
536 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
537 | 0 | setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in6)); |
538 | 0 | }; |
539 | | |
540 | | SockaddrWrapper(const struct sockaddr_in* socketAddress) |
541 | 0 | { |
542 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
543 | 0 | setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in)); |
544 | 0 | }; |
545 | | |
546 | | SockaddrWrapper(const struct sockaddr_un* socketAddress) |
547 | 0 | { |
548 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
549 | 0 | setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_un)); |
550 | 0 | }; |
551 | | |
552 | | void setSockaddr(const struct sockaddr* socketAddress, socklen_t salen) |
553 | 0 | { |
554 | 0 | if (salen > sizeof(struct sockaddr_un)) { |
555 | 0 | throw PDNSException("ComboAddress can't handle other than sockaddr_in, sockaddr_in6 or sockaddr_un"); |
556 | 0 | } |
557 | 0 | memcpy(this, socketAddress, salen); |
558 | 0 | } |
559 | | |
560 | | explicit SockaddrWrapper(const string& str, uint16_t port = 0) |
561 | 0 | { |
562 | 0 | memset(&sinun, 0, sizeof(sinun)); |
563 | 0 | sin4.sin_family = AF_INET; |
564 | 0 | sin4.sin_port = 0; |
565 | 0 | if (str == "\"\"" || str == "''") { |
566 | 0 | throw PDNSException("Stray quotation marks in address."); |
567 | 0 | } |
568 | 0 | if (makeIPv4sockaddr(str, &sin4) != 0) { |
569 | 0 | sin6.sin6_family = AF_INET6; |
570 | 0 | if (makeIPv6sockaddr(str, &sin6) < 0) { |
571 | 0 | sinun.sun_family = AF_UNIX; |
572 | 0 | // only attempt Unix socket address if address candidate does not contain a port |
573 | 0 | if (str.find(':') != string::npos || makeUNsockaddr(str, &sinun) < 0) { |
574 | 0 | throw PDNSException("Unable to convert presentation address '" + str + "'"); |
575 | 0 | } |
576 | 0 | } |
577 | 0 | } |
578 | 0 | if (sinun.sun_family != AF_UNIX && sin4.sin_port == 0) { // 'str' overrides port! |
579 | 0 | sin4.sin_port = htons(port); |
580 | 0 | } |
581 | 0 | } |
582 | | |
583 | | [[nodiscard]] bool isIPv6() const |
584 | 0 | { |
585 | 0 | return sin4.sin_family == AF_INET6; |
586 | 0 | } |
587 | | [[nodiscard]] bool isIPv4() const |
588 | 0 | { |
589 | 0 | return sin4.sin_family == AF_INET; |
590 | 0 | } |
591 | | [[nodiscard]] bool isUnixSocket() const |
592 | 0 | { |
593 | 0 | return sin4.sin_family == AF_UNIX; |
594 | 0 | } |
595 | | |
596 | | [[nodiscard]] string toString() const |
597 | 0 | { |
598 | 0 | if (sinun.sun_family == AF_UNIX) { |
599 | 0 | return sinun.sun_path; |
600 | 0 | } |
601 | 0 | std::array<char, 1024> host{}; |
602 | 0 | if (sin4.sin_family != 0) { |
603 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
604 | 0 | int retval = getnameinfo(reinterpret_cast<const struct sockaddr*>(this), getSocklen(), host.data(), host.size(), nullptr, 0, NI_NUMERICHOST); |
605 | 0 | if (retval == 0) { |
606 | 0 | return host.data(); |
607 | 0 | } |
608 | 0 | return "invalid " + string(gai_strerror(retval)); |
609 | 0 | } |
610 | 0 | return "invalid"; |
611 | 0 | } |
612 | | |
613 | | [[nodiscard]] string toStringWithPort() const |
614 | 0 | { |
615 | 0 | if (sinun.sun_family == AF_UNIX) { |
616 | 0 | return toString(); |
617 | 0 | } |
618 | 0 | if (sin4.sin_family == AF_INET) { |
619 | 0 | return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); |
620 | 0 | } |
621 | 0 | return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); |
622 | 0 | } |
623 | | |
624 | | void reset() |
625 | 0 | { |
626 | 0 | memset(&sinun, 0, sizeof(sinun)); |
627 | 0 | } |
628 | | }; |
629 | | |
630 | | /** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */ |
631 | | class NetmaskException : public PDNSException |
632 | | { |
633 | | public: |
634 | | NetmaskException(const string& arg) : |
635 | 0 | PDNSException(arg) {} |
636 | | }; |
637 | | |
638 | | inline ComboAddress makeComboAddress(const string& str) |
639 | 0 | { |
640 | 0 | ComboAddress address; |
641 | 0 | address.sin4.sin_family = AF_INET; |
642 | 0 | if (inet_pton(AF_INET, str.c_str(), &address.sin4.sin_addr) <= 0) { |
643 | 0 | address.sin4.sin_family = AF_INET6; |
644 | 0 | if (makeIPv6sockaddr(str, &address.sin6) < 0) { |
645 | 0 | throw NetmaskException("Unable to convert '" + str + "' to a netmask"); |
646 | 0 | } |
647 | 0 | } |
648 | 0 | return address; |
649 | 0 | } |
650 | | |
651 | | inline ComboAddress makeComboAddressFromRaw(uint8_t version, const char* raw, size_t len) |
652 | 0 | { |
653 | 0 | ComboAddress address; |
654 | |
|
655 | 0 | if (version == 4) { |
656 | 0 | address.sin4.sin_family = AF_INET; |
657 | 0 | if (len != sizeof(address.sin4.sin_addr)) { |
658 | 0 | throw NetmaskException("invalid raw address length"); |
659 | 0 | } |
660 | 0 | memcpy(&address.sin4.sin_addr, raw, sizeof(address.sin4.sin_addr)); |
661 | 0 | } |
662 | 0 | else if (version == 6) { |
663 | 0 | address.sin6.sin6_family = AF_INET6; |
664 | 0 | if (len != sizeof(address.sin6.sin6_addr)) { |
665 | 0 | throw NetmaskException("invalid raw address length"); |
666 | 0 | } |
667 | 0 | memcpy(&address.sin6.sin6_addr, raw, sizeof(address.sin6.sin6_addr)); |
668 | 0 | } |
669 | 0 | else { |
670 | 0 | throw NetmaskException("invalid address family"); |
671 | 0 | } |
672 | | |
673 | 0 | return address; |
674 | 0 | } |
675 | | |
676 | | inline ComboAddress makeComboAddressFromRaw(uint8_t version, const string& str) |
677 | 0 | { |
678 | 0 | return makeComboAddressFromRaw(version, str.c_str(), str.size()); |
679 | 0 | } |
680 | | |
681 | | /** This class represents a netmask and can be queried to see if a certain |
682 | | IP address is matched by this mask */ |
683 | | class Netmask |
684 | | { |
685 | | public: |
686 | | Netmask() |
687 | 230 | { |
688 | 230 | d_network.sin4.sin_family = 0; // disable this doing anything useful |
689 | 230 | d_network.sin4.sin_port = 0; // this guarantees d_network compares identical |
690 | 230 | } |
691 | | |
692 | | Netmask(const ComboAddress& network, uint8_t bits = 0xff) : |
693 | 60 | d_network(network) |
694 | 60 | { |
695 | 60 | d_network.sin4.sin_port = 0; |
696 | 60 | setBits(bits); |
697 | 60 | } |
698 | | |
699 | | Netmask(const sockaddr_in* network, uint8_t bits = 0xff) : |
700 | | d_network(network) |
701 | 0 | { |
702 | 0 | d_network.sin4.sin_port = 0; |
703 | 0 | setBits(bits); |
704 | 0 | } |
705 | | Netmask(const sockaddr_in6* network, uint8_t bits = 0xff) : |
706 | | d_network(network) |
707 | 0 | { |
708 | 0 | d_network.sin4.sin_port = 0; |
709 | 0 | setBits(bits); |
710 | 0 | } |
711 | | void setBits(uint8_t value) |
712 | 60 | { |
713 | 60 | d_bits = d_network.isIPv4() ? std::min(value, static_cast<uint8_t>(32U)) : std::min(value, static_cast<uint8_t>(128U)); |
714 | | |
715 | 60 | if (d_bits < 32) { |
716 | 42 | d_mask = ~(0xFFFFFFFF >> d_bits); |
717 | 42 | } |
718 | 18 | else { |
719 | | // note that d_mask is unused for IPv6 |
720 | 18 | d_mask = 0xFFFFFFFF; |
721 | 18 | } |
722 | | |
723 | 60 | if (isIPv4()) { |
724 | 24 | d_network.sin4.sin_addr.s_addr = htonl(ntohl(d_network.sin4.sin_addr.s_addr) & d_mask); |
725 | 24 | } |
726 | 36 | else if (isIPv6()) { |
727 | 36 | uint8_t bytes = d_bits / 8; |
728 | 36 | auto* address = reinterpret_cast<uint8_t*>(&d_network.sin6.sin6_addr.s6_addr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) |
729 | 36 | uint8_t bits = d_bits % 8; |
730 | 36 | auto mask = static_cast<uint8_t>(~(0xFF >> bits)); |
731 | | |
732 | 36 | if (bytes < sizeof(d_network.sin6.sin6_addr.s6_addr)) { |
733 | 35 | address[bytes] &= mask; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
734 | 35 | } |
735 | | |
736 | 373 | for (size_t idx = bytes + 1; idx < sizeof(d_network.sin6.sin6_addr.s6_addr); ++idx) { |
737 | 337 | address[idx] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
738 | 337 | } |
739 | 36 | } |
740 | 60 | } |
741 | | |
742 | | enum stringType |
743 | | { |
744 | | humanString, |
745 | | byteString, |
746 | | }; |
747 | | //! Constructor supplies the mask, which cannot be changed |
748 | | Netmask(const string& mask, stringType type = humanString) |
749 | 0 | { |
750 | 0 | if (type == byteString) { |
751 | 0 | uint8_t afi = mask.at(0); |
752 | 0 | size_t len = afi == 4 ? 4 : 16; |
753 | 0 | uint8_t bits = mask.at(len + 1); |
754 | |
|
755 | 0 | d_network = makeComboAddressFromRaw(afi, mask.substr(1, len)); |
756 | |
|
757 | 0 | setBits(bits); |
758 | 0 | } |
759 | 0 | else { |
760 | 0 | pair<string, string> split = splitField(mask, '/'); |
761 | 0 | d_network = makeComboAddress(split.first); |
762 | |
|
763 | 0 | if (!split.second.empty()) { |
764 | 0 | setBits(pdns::checked_stoi<uint8_t>(split.second)); |
765 | 0 | } |
766 | 0 | else if (d_network.sin4.sin_family == AF_INET) { |
767 | 0 | setBits(32); |
768 | 0 | } |
769 | 0 | else { |
770 | 0 | setBits(128); |
771 | 0 | } |
772 | 0 | } |
773 | 0 | } |
774 | | |
775 | | [[nodiscard]] bool match(const ComboAddress& address) const |
776 | 0 | { |
777 | 0 | return match(&address); |
778 | 0 | } |
779 | | |
780 | | //! If this IP address in socket address matches |
781 | | bool match(const ComboAddress* address) const |
782 | 0 | { |
783 | 0 | if (d_network.sin4.sin_family != address->sin4.sin_family) { |
784 | 0 | return false; |
785 | 0 | } |
786 | 0 | if (d_network.sin4.sin_family == AF_INET) { |
787 | 0 | return match4(htonl((unsigned int)address->sin4.sin_addr.s_addr)); |
788 | 0 | } |
789 | 0 | if (d_network.sin6.sin6_family == AF_INET6) { |
790 | 0 | uint8_t bytes = d_bits / 8; |
791 | 0 | uint8_t index = 0; |
792 | 0 | // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) |
793 | 0 | const auto* lhs = reinterpret_cast<const uint8_t*>(&d_network.sin6.sin6_addr.s6_addr); |
794 | 0 | const auto* rhs = reinterpret_cast<const uint8_t*>(&address->sin6.sin6_addr.s6_addr); |
795 | 0 | // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) |
796 | 0 |
|
797 | 0 | // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
798 | 0 | for (index = 0; index < bytes; ++index) { |
799 | 0 | if (lhs[index] != rhs[index]) { |
800 | 0 | return false; |
801 | 0 | } |
802 | 0 | } |
803 | 0 | // still here, now match remaining bits |
804 | 0 | uint8_t bits = d_bits % 8; |
805 | 0 | auto mask = static_cast<uint8_t>(~(0xFF >> bits)); |
806 | 0 |
|
807 | 0 | return ((lhs[index]) == (rhs[index] & mask)); |
808 | 0 | // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
809 | 0 | } |
810 | 0 | return false; |
811 | 0 | } |
812 | | |
813 | | //! If this ASCII IP address matches |
814 | | [[nodiscard]] bool match(const string& arg) const |
815 | 0 | { |
816 | 0 | ComboAddress address = makeComboAddress(arg); |
817 | 0 | return match(&address); |
818 | 0 | } |
819 | | |
820 | | //! If this IP address in native format matches |
821 | | [[nodiscard]] bool match4(uint32_t arg) const |
822 | 0 | { |
823 | 0 | return (arg & d_mask) == (ntohl(d_network.sin4.sin_addr.s_addr)); |
824 | 0 | } |
825 | | |
826 | | [[nodiscard]] string toString() const |
827 | 0 | { |
828 | 0 | return d_network.toStringNoInterface() + "/" + std::to_string((unsigned int)d_bits); |
829 | 0 | } |
830 | | |
831 | | [[nodiscard]] string toStringNoMask() const |
832 | 0 | { |
833 | 0 | return d_network.toStringNoInterface(); |
834 | 0 | } |
835 | | |
836 | | [[nodiscard]] string toByteString() const |
837 | 0 | { |
838 | 0 | ostringstream tmp; |
839 | 0 |
|
840 | 0 | tmp << (d_network.isIPv4() ? "\x04" : "\x06") |
841 | 0 | << d_network.toByteString() |
842 | 0 | << getBits(); |
843 | 0 |
|
844 | 0 | return tmp.str(); |
845 | 0 | } |
846 | | |
847 | | [[nodiscard]] const ComboAddress& getNetwork() const |
848 | 0 | { |
849 | 0 | return d_network; |
850 | 0 | } |
851 | | |
852 | | [[nodiscard]] const ComboAddress& getMaskedNetwork() const |
853 | 0 | { |
854 | 0 | return getNetwork(); |
855 | 0 | } |
856 | | |
857 | | [[nodiscard]] uint8_t getBits() const |
858 | 0 | { |
859 | 0 | return d_bits; |
860 | 0 | } |
861 | | |
862 | | [[nodiscard]] bool isIPv6() const |
863 | 36 | { |
864 | 36 | return d_network.sin6.sin6_family == AF_INET6; |
865 | 36 | } |
866 | | |
867 | | [[nodiscard]] bool isIPv4() const |
868 | 60 | { |
869 | 60 | return d_network.sin4.sin_family == AF_INET; |
870 | 60 | } |
871 | | |
872 | | bool operator<(const Netmask& rhs) const |
873 | 0 | { |
874 | 0 | if (empty() && !rhs.empty()) { |
875 | 0 | return false; |
876 | 0 | } |
877 | 0 | if (!empty() && rhs.empty()) { |
878 | 0 | return true; |
879 | 0 | } |
880 | 0 | if (d_bits > rhs.d_bits) { |
881 | 0 | return true; |
882 | 0 | } |
883 | 0 | if (d_bits < rhs.d_bits) { |
884 | 0 | return false; |
885 | 0 | } |
886 | 0 |
|
887 | 0 | return d_network < rhs.d_network; |
888 | 0 | } |
889 | | |
890 | | bool operator>(const Netmask& rhs) const |
891 | 0 | { |
892 | 0 | return rhs.operator<(*this); |
893 | 0 | } |
894 | | |
895 | | bool operator==(const Netmask& rhs) const |
896 | 0 | { |
897 | 0 | return std::tie(d_network, d_bits) == std::tie(rhs.d_network, rhs.d_bits); |
898 | 0 | } |
899 | | bool operator!=(const Netmask& rhs) const |
900 | 0 | { |
901 | 0 | return !operator==(rhs); |
902 | 0 | } |
903 | | |
904 | | [[nodiscard]] bool empty() const |
905 | 0 | { |
906 | 0 | return d_network.sin4.sin_family == 0; |
907 | 0 | } |
908 | | |
909 | | //! Get normalized version of the netmask. This means that all address bits below the network bits are zero. |
910 | | [[nodiscard]] Netmask getNormalized() const |
911 | 0 | { |
912 | 0 | return {getMaskedNetwork(), d_bits}; |
913 | 0 | } |
914 | | //! Get Netmask for super network of this one (i.e. with fewer network bits) |
915 | | [[nodiscard]] Netmask getSuper(uint8_t bits) const |
916 | 0 | { |
917 | 0 | return {d_network, std::min(d_bits, bits)}; |
918 | 0 | } |
919 | | |
920 | | //! Get the total number of address bits for this netmask (either 32 or 128 depending on IP version) |
921 | | [[nodiscard]] uint8_t getFullBits() const |
922 | 0 | { |
923 | 0 | return d_network.getBits(); |
924 | 0 | } |
925 | | |
926 | | /** Get the value of the bit at the provided bit index. When the index >= 0, |
927 | | the index is relative to the LSB starting at index zero. When the index < 0, |
928 | | the index is relative to the MSB starting at index -1 and counting down. |
929 | | When the index points outside the network bits, it always yields zero. |
930 | | */ |
931 | | [[nodiscard]] bool getBit(int bit) const |
932 | 0 | { |
933 | 0 | if (bit < -d_bits) { |
934 | 0 | return false; |
935 | 0 | } |
936 | 0 | if (bit >= 0) { |
937 | 0 | if (isIPv4()) { |
938 | 0 | if (bit >= 32 || bit < (32 - d_bits)) { |
939 | 0 | return false; |
940 | 0 | } |
941 | 0 | } |
942 | 0 | if (isIPv6()) { |
943 | 0 | if (bit >= 128 || bit < (128 - d_bits)) { |
944 | 0 | return false; |
945 | 0 | } |
946 | 0 | } |
947 | 0 | } |
948 | 0 | return d_network.getBit(bit); |
949 | 0 | } |
950 | | |
951 | | struct Hash |
952 | | { |
953 | | size_t operator()(const Netmask& netmask) const |
954 | 0 | { |
955 | 0 | return burtle(&netmask.d_bits, 1, ComboAddress::addressOnlyHash()(netmask.d_network)); |
956 | 0 | } |
957 | | }; |
958 | | |
959 | | private: |
960 | | ComboAddress d_network; |
961 | | uint32_t d_mask{0}; |
962 | | uint8_t d_bits{0}; |
963 | | }; |
964 | | |
965 | | namespace std |
966 | | { |
967 | | template <> |
968 | | struct hash<Netmask> |
969 | | { |
970 | | auto operator()(const Netmask& netmask) const |
971 | 0 | { |
972 | 0 | return Netmask::Hash{}(netmask); |
973 | 0 | } |
974 | | }; |
975 | | } |
976 | | |
977 | | /** Binary tree map implementation with <Netmask,T> pair. |
978 | | * |
979 | | * This is an binary tree implementation for storing attributes for IPv4 and IPv6 prefixes. |
980 | | * The most simple use case is simple NetmaskTree<bool> used by NetmaskGroup, which only |
981 | | * wants to know if given IP address is matched in the prefixes stored. |
982 | | * |
983 | | * This element is useful for anything that needs to *STORE* prefixes, and *MATCH* IP addresses |
984 | | * to a *LIST* of *PREFIXES*. Not the other way round. |
985 | | * |
986 | | * You can store IPv4 and IPv6 addresses to same tree, separate payload storage is kept per AFI. |
987 | | * Network prefixes (Netmasks) are always recorded in normalized fashion, meaning that only |
988 | | * the network bits are set. This is what is returned in the insert() and lookup() return |
989 | | * values. |
990 | | * |
991 | | * Use swap if you need to move the tree to another NetmaskTree instance, it is WAY faster |
992 | | * than using copy ctor or assignment operator, since it moves the nodes and tree root to |
993 | | * new home instead of actually recreating the tree. |
994 | | * |
995 | | * Please see NetmaskGroup for example of simple use case. Other usecases can be found |
996 | | * from GeoIPBackend and Sortlist, and from dnsdist. |
997 | | */ |
998 | | template <typename T, class K = Netmask> |
999 | | class NetmaskTree |
1000 | | { |
1001 | | public: |
1002 | | class Iterator; |
1003 | | |
1004 | | using key_type = K; |
1005 | | using value_type = T; |
1006 | | using node_type = std::pair<const key_type, value_type>; |
1007 | | using size_type = size_t; |
1008 | | using iterator = class Iterator; |
1009 | | |
1010 | | private: |
1011 | | /** Single node in tree, internal use only. |
1012 | | */ |
1013 | | class TreeNode : boost::noncopyable |
1014 | | { |
1015 | | public: |
1016 | | explicit TreeNode() noexcept : |
1017 | 8 | parent(nullptr), node(), assigned(false), d_bits(0) |
1018 | 8 | { |
1019 | 8 | } |
1020 | | explicit TreeNode(const key_type& key) : |
1021 | 0 | parent(nullptr), node({key.getNormalized(), value_type()}), assigned(false), d_bits(key.getFullBits()) |
1022 | 0 | { |
1023 | 0 | } |
1024 | | |
1025 | | //<! Makes a left leaf node with specified key. |
1026 | | TreeNode* make_left(const key_type& key) |
1027 | 0 | { |
1028 | 0 | d_bits = node.first.getBits(); |
1029 | 0 | left = make_unique<TreeNode>(key); |
1030 | 0 | left->parent = this; |
1031 | 0 | return left.get(); |
1032 | 0 | } |
1033 | | |
1034 | | //<! Makes a right leaf node with specified key. |
1035 | | TreeNode* make_right(const key_type& key) |
1036 | 0 | { |
1037 | 0 | d_bits = node.first.getBits(); |
1038 | 0 | right = make_unique<TreeNode>(key); |
1039 | 0 | right->parent = this; |
1040 | 0 | return right.get(); |
1041 | 0 | } |
1042 | | |
1043 | | //<! Splits branch at indicated bit position by inserting key |
1044 | | TreeNode* split(const key_type& key, int bits) |
1045 | 0 | { |
1046 | 0 | if (parent == nullptr) { |
1047 | | // not to be called on the root node |
1048 | 0 | throw std::logic_error( |
1049 | 0 | "NetmaskTree::TreeNode::split(): must not be called on root node"); |
1050 | 0 | } |
1051 | | |
1052 | | // determine reference from parent |
1053 | 0 | unique_ptr<TreeNode>& parent_ref = (parent->left.get() == this ? parent->left : parent->right); |
1054 | 0 | if (parent_ref.get() != this) { |
1055 | 0 | throw std::logic_error( |
1056 | 0 | "NetmaskTree::TreeNode::split(): parent node reference is invalid"); |
1057 | 0 | } |
1058 | | |
1059 | | // create new tree node for the new key and |
1060 | | // attach the new node under our former parent |
1061 | 0 | auto new_intermediate_node = make_unique<TreeNode>(key); |
1062 | 0 | new_intermediate_node->d_bits = bits; |
1063 | 0 | new_intermediate_node->parent = parent; |
1064 | 0 | auto* new_intermediate_node_raw = new_intermediate_node.get(); |
1065 | | |
1066 | | // hereafter new_intermediate points to "this" |
1067 | | // ie the child of the new intermediate node |
1068 | 0 | std::swap(parent_ref, new_intermediate_node); |
1069 | | // and we now assign this to current_node so |
1070 | | // it's clear it no longer refers to the new |
1071 | | // intermediate node |
1072 | 0 | std::unique_ptr<TreeNode> current_node = std::move(new_intermediate_node); |
1073 | | |
1074 | | // attach "this" node below the new node |
1075 | | // (left or right depending on bit) |
1076 | | // technically the raw pointer escapes the duration of the |
1077 | | // unique pointer, but just below we store the unique pointer |
1078 | | // in the parent, so it lives as long as necessary |
1079 | | // coverity[escape] |
1080 | 0 | current_node->parent = new_intermediate_node_raw; |
1081 | 0 | if (current_node->node.first.getBit(-1 - bits)) { |
1082 | 0 | new_intermediate_node_raw->right = std::move(current_node); |
1083 | 0 | } |
1084 | 0 | else { |
1085 | 0 | new_intermediate_node_raw->left = std::move(current_node); |
1086 | 0 | } |
1087 | |
|
1088 | 0 | return new_intermediate_node_raw; |
1089 | 0 | } |
1090 | | |
1091 | | //<! Forks branch for new key at indicated bit position |
1092 | | TreeNode* fork(const key_type& key, int bits) |
1093 | 0 | { |
1094 | 0 | if (parent == nullptr) { |
1095 | | // not to be called on the root node |
1096 | 0 | throw std::logic_error( |
1097 | 0 | "NetmaskTree::TreeNode::fork(): must not be called on root node"); |
1098 | 0 | } |
1099 | | |
1100 | | // determine reference from parent |
1101 | 0 | unique_ptr<TreeNode>& parent_ref = (parent->left.get() == this ? parent->left : parent->right); |
1102 | 0 | if (parent_ref.get() != this) { |
1103 | 0 | throw std::logic_error( |
1104 | 0 | "NetmaskTree::TreeNode::fork(): parent node reference is invalid"); |
1105 | 0 | } |
1106 | | |
1107 | | // create new tree node for the branch point |
1108 | | |
1109 | | // the current node will now be a child of the new branch node |
1110 | | // (hereafter new_child1 points to "this") |
1111 | 0 | unique_ptr<TreeNode> new_child1 = std::move(parent_ref); |
1112 | | // attach the branch node under our former parent |
1113 | 0 | parent_ref = make_unique<TreeNode>(node.first.getSuper(bits)); |
1114 | 0 | auto* branch_node = parent_ref.get(); |
1115 | 0 | branch_node->d_bits = bits; |
1116 | 0 | branch_node->parent = parent; |
1117 | | |
1118 | | // create second new leaf node for the new key |
1119 | 0 | unique_ptr<TreeNode> new_child2 = make_unique<TreeNode>(key); |
1120 | 0 | TreeNode* new_node = new_child2.get(); |
1121 | | |
1122 | | // attach the new child nodes below the branch node |
1123 | | // (left or right depending on bit) |
1124 | 0 | new_child1->parent = branch_node; |
1125 | 0 | new_child2->parent = branch_node; |
1126 | 0 | if (new_child1->node.first.getBit(-1 - bits)) { |
1127 | 0 | branch_node->right = std::move(new_child1); |
1128 | 0 | branch_node->left = std::move(new_child2); |
1129 | 0 | } |
1130 | 0 | else { |
1131 | 0 | branch_node->right = std::move(new_child2); |
1132 | 0 | branch_node->left = std::move(new_child1); |
1133 | 0 | } |
1134 | | // now we have attached the new unique pointers to the tree: |
1135 | | // - branch_node is below its parent |
1136 | | // - new_child1 (ourselves) is below branch_node |
1137 | | // - new_child2, the new leaf node, is below branch_node as well |
1138 | |
|
1139 | 0 | return new_node; |
1140 | 0 | } |
1141 | | |
1142 | | //<! Traverse left branch depth-first |
1143 | | TreeNode* traverse_l() |
1144 | 0 | { |
1145 | 0 | TreeNode* tnode = this; |
1146 | |
|
1147 | 0 | while (tnode->left) { |
1148 | 0 | tnode = tnode->left.get(); |
1149 | 0 | } |
1150 | 0 | return tnode; |
1151 | 0 | } |
1152 | | |
1153 | | //<! Traverse tree depth-first and in-order (L-N-R) |
1154 | | TreeNode* traverse_lnr() |
1155 | 0 | { |
1156 | 0 | TreeNode* tnode = this; |
1157 | | |
1158 | | // precondition: descended left as deep as possible |
1159 | 0 | if (tnode->right) { |
1160 | | // descend right |
1161 | 0 | tnode = tnode->right.get(); |
1162 | | // descend left as deep as possible and return next node |
1163 | 0 | return tnode->traverse_l(); |
1164 | 0 | } |
1165 | | |
1166 | | // ascend to parent |
1167 | 0 | while (tnode->parent != nullptr) { |
1168 | 0 | TreeNode* prev_child = tnode; |
1169 | 0 | tnode = tnode->parent; |
1170 | | |
1171 | | // return this node, but only when we come from the left child branch |
1172 | 0 | if (tnode->left && tnode->left.get() == prev_child) { |
1173 | 0 | return tnode; |
1174 | 0 | } |
1175 | 0 | } |
1176 | 0 | return nullptr; |
1177 | 0 | } |
1178 | | |
1179 | | //<! Traverse only assigned nodes |
1180 | | TreeNode* traverse_lnr_assigned() |
1181 | 0 | { |
1182 | 0 | TreeNode* tnode = traverse_lnr(); |
1183 | |
|
1184 | 0 | while (tnode != nullptr && !tnode->assigned) { |
1185 | 0 | tnode = tnode->traverse_lnr(); |
1186 | 0 | } |
1187 | 0 | return tnode; |
1188 | 0 | } |
1189 | | |
1190 | | unique_ptr<TreeNode> left; |
1191 | | unique_ptr<TreeNode> right; |
1192 | | TreeNode* parent; |
1193 | | |
1194 | | node_type node; |
1195 | | bool assigned; //<! Whether this node is assigned-to by the application |
1196 | | |
1197 | | int d_bits; //<! How many bits have been used so far |
1198 | | }; |
1199 | | |
1200 | | void cleanup_tree(TreeNode* node) |
1201 | 0 | { |
1202 | | // only cleanup this node if it has no children and node not assigned |
1203 | 0 | if (!(node->left || node->right || node->assigned)) { |
1204 | | // get parent node ptr |
1205 | 0 | TreeNode* pparent = node->parent; |
1206 | | // delete this node |
1207 | 0 | if (pparent) { |
1208 | 0 | if (pparent->left.get() == node) { |
1209 | 0 | pparent->left.reset(); |
1210 | 0 | } |
1211 | 0 | else { |
1212 | 0 | pparent->right.reset(); |
1213 | 0 | } |
1214 | | // now recurse up to the parent |
1215 | 0 | cleanup_tree(pparent); |
1216 | 0 | } |
1217 | 0 | } |
1218 | 0 | } |
1219 | | |
1220 | | void copyTree(const NetmaskTree& rhs) |
1221 | 0 | { |
1222 | 0 | try { |
1223 | 0 | TreeNode* node = rhs.d_root.get(); |
1224 | 0 | if (node != nullptr) { |
1225 | 0 | node = node->traverse_l(); |
1226 | 0 | } |
1227 | 0 | while (node != nullptr) { |
1228 | 0 | if (node->assigned) { |
1229 | 0 | insert(node->node.first).second = node->node.second; |
1230 | 0 | } |
1231 | 0 | node = node->traverse_lnr(); |
1232 | 0 | } |
1233 | 0 | } |
1234 | 0 | catch (const NetmaskException&) { |
1235 | 0 | abort(); |
1236 | 0 | } |
1237 | 0 | catch (const std::logic_error&) { |
1238 | 0 | abort(); |
1239 | 0 | } |
1240 | 0 | } |
1241 | | |
1242 | | public: |
1243 | | class Iterator |
1244 | | { |
1245 | | public: |
1246 | | using value_type = node_type; |
1247 | | using reference = node_type&; |
1248 | | using pointer = node_type*; |
1249 | | using iterator_category = std::forward_iterator_tag; |
1250 | | using difference_type = size_type; |
1251 | | |
1252 | | private: |
1253 | | friend class NetmaskTree; |
1254 | | |
1255 | | const NetmaskTree* d_tree; |
1256 | | TreeNode* d_node; |
1257 | | |
1258 | | Iterator(const NetmaskTree* tree, TreeNode* node) : |
1259 | 0 | d_tree(tree), d_node(node) |
1260 | 0 | { |
1261 | 0 | } |
1262 | | |
1263 | | public: |
1264 | | Iterator() : |
1265 | 0 | d_tree(nullptr), d_node(nullptr) {} |
1266 | | |
1267 | | Iterator& operator++() // prefix |
1268 | 0 | { |
1269 | 0 | if (d_node == nullptr) { |
1270 | 0 | throw std::logic_error( |
1271 | 0 | "NetmaskTree::Iterator::operator++: iterator is invalid"); |
1272 | 0 | } |
1273 | 0 | d_node = d_node->traverse_lnr_assigned(); |
1274 | 0 | return *this; |
1275 | 0 | } |
1276 | | Iterator operator++(int) // postfix |
1277 | 0 | { |
1278 | 0 | Iterator tmp(*this); |
1279 | 0 | operator++(); |
1280 | 0 | return tmp; |
1281 | 0 | } |
1282 | | |
1283 | | reference operator*() |
1284 | 0 | { |
1285 | 0 | if (d_node == nullptr) { |
1286 | 0 | throw std::logic_error( |
1287 | 0 | "NetmaskTree::Iterator::operator*: iterator is invalid"); |
1288 | 0 | } |
1289 | 0 | return d_node->node; |
1290 | 0 | } |
1291 | | |
1292 | | pointer operator->() |
1293 | 0 | { |
1294 | 0 | if (d_node == nullptr) { |
1295 | 0 | throw std::logic_error( |
1296 | 0 | "NetmaskTree::Iterator::operator->: iterator is invalid"); |
1297 | 0 | } |
1298 | 0 | return &d_node->node; |
1299 | 0 | } |
1300 | | |
1301 | | bool operator==(const Iterator& rhs) |
1302 | 0 | { |
1303 | 0 | return (d_tree == rhs.d_tree && d_node == rhs.d_node); |
1304 | 0 | } |
1305 | | bool operator!=(const Iterator& rhs) |
1306 | 0 | { |
1307 | 0 | return !(*this == rhs); |
1308 | 0 | } |
1309 | | }; |
1310 | | |
1311 | | NetmaskTree() noexcept : |
1312 | 8 | d_root(new TreeNode()), d_left(nullptr) |
1313 | 8 | { |
1314 | 8 | } |
1315 | | |
1316 | | NetmaskTree(const NetmaskTree& rhs) : |
1317 | 0 | d_root(new TreeNode()), d_left(nullptr) |
1318 | 0 | { |
1319 | 0 | copyTree(rhs); |
1320 | 0 | } |
1321 | | |
1322 | 0 | ~NetmaskTree() = default; |
1323 | | |
1324 | | NetmaskTree& operator=(const NetmaskTree& rhs) |
1325 | 0 | { |
1326 | 0 | if (this != &rhs) { |
1327 | 0 | clear(); |
1328 | 0 | copyTree(rhs); |
1329 | 0 | } |
1330 | 0 | return *this; |
1331 | 0 | } |
1332 | | |
1333 | 0 | NetmaskTree(NetmaskTree&&) noexcept = default; |
1334 | | NetmaskTree& operator=(NetmaskTree&&) noexcept = default; |
1335 | | |
1336 | | [[nodiscard]] iterator begin() const |
1337 | 0 | { |
1338 | 0 | return Iterator(this, d_left); |
1339 | 0 | } |
1340 | | [[nodiscard]] iterator end() const |
1341 | 0 | { |
1342 | 0 | return Iterator(this, nullptr); |
1343 | 0 | } |
1344 | | iterator begin() |
1345 | 0 | { |
1346 | 0 | return Iterator(this, d_left); |
1347 | 0 | } |
1348 | | iterator end() |
1349 | 0 | { |
1350 | 0 | return Iterator(this, nullptr); |
1351 | 0 | } |
1352 | | |
1353 | | node_type& insert(const string& mask) |
1354 | 0 | { |
1355 | 0 | return insert(key_type(mask)); |
1356 | 0 | } |
1357 | | |
1358 | | //<! Creates new value-pair in tree and returns it. |
1359 | | node_type& insert(const key_type& key) |
1360 | 0 | { |
1361 | 0 | TreeNode* node{}; |
1362 | 0 | bool is_left = true; |
1363 | | |
1364 | | // we turn left on IPv4 and right on IPv6 |
1365 | 0 | if (key.isIPv4()) { |
1366 | 0 | node = d_root->left.get(); |
1367 | 0 | if (node == nullptr) { |
1368 | |
|
1369 | 0 | d_root->left = make_unique<TreeNode>(key); |
1370 | 0 | node = d_root->left.get(); |
1371 | 0 | node->assigned = true; |
1372 | 0 | node->parent = d_root.get(); |
1373 | 0 | d_size++; |
1374 | 0 | d_left = node; |
1375 | 0 | return node->node; |
1376 | 0 | } |
1377 | 0 | } |
1378 | 0 | else if (key.isIPv6()) { |
1379 | 0 | node = d_root->right.get(); |
1380 | 0 | if (node == nullptr) { |
1381 | |
|
1382 | 0 | d_root->right = make_unique<TreeNode>(key); |
1383 | 0 | node = d_root->right.get(); |
1384 | 0 | node->assigned = true; |
1385 | 0 | node->parent = d_root.get(); |
1386 | 0 | d_size++; |
1387 | 0 | if (!d_root->left) { |
1388 | 0 | d_left = node; |
1389 | 0 | } |
1390 | 0 | return node->node; |
1391 | 0 | } |
1392 | 0 | if (d_root->left) { |
1393 | 0 | is_left = false; |
1394 | 0 | } |
1395 | 0 | } |
1396 | 0 | else { |
1397 | 0 | throw NetmaskException("invalid address family"); |
1398 | 0 | } |
1399 | | |
1400 | | // we turn left on 0 and right on 1 |
1401 | 0 | int bits = 0; |
1402 | 0 | for (; bits < key.getBits(); bits++) { |
1403 | 0 | bool vall = key.getBit(-1 - bits); |
1404 | |
|
1405 | 0 | if (bits >= node->d_bits) { |
1406 | | // the end of the current node is reached; continue with the next |
1407 | 0 | if (vall) { |
1408 | 0 | if (node->left || node->assigned) { |
1409 | 0 | is_left = false; |
1410 | 0 | } |
1411 | 0 | if (!node->right) { |
1412 | | // the right branch doesn't exist yet; attach our key here |
1413 | 0 | node = node->make_right(key); |
1414 | 0 | break; |
1415 | 0 | } |
1416 | 0 | node = node->right.get(); |
1417 | 0 | } |
1418 | 0 | else { |
1419 | 0 | if (!node->left) { |
1420 | | // the left branch doesn't exist yet; attach our key here |
1421 | 0 | node = node->make_left(key); |
1422 | 0 | break; |
1423 | 0 | } |
1424 | 0 | node = node->left.get(); |
1425 | 0 | } |
1426 | 0 | continue; |
1427 | 0 | } |
1428 | 0 | if (bits >= node->node.first.getBits()) { |
1429 | | // the matching branch ends here, yet the key netmask has more bits; add a |
1430 | | // child node below the existing branch leaf. |
1431 | 0 | if (vall) { |
1432 | 0 | if (node->assigned) { |
1433 | 0 | is_left = false; |
1434 | 0 | } |
1435 | 0 | node = node->make_right(key); |
1436 | 0 | } |
1437 | 0 | else { |
1438 | 0 | node = node->make_left(key); |
1439 | 0 | } |
1440 | 0 | break; |
1441 | 0 | } |
1442 | 0 | bool valr = node->node.first.getBit(-1 - bits); |
1443 | 0 | if (vall != valr) { |
1444 | 0 | if (vall) { |
1445 | 0 | is_left = false; |
1446 | 0 | } |
1447 | | // the branch matches just upto this point, yet continues in a different |
1448 | | // direction; fork the branch. |
1449 | 0 | node = node->fork(key, bits); |
1450 | 0 | break; |
1451 | 0 | } |
1452 | 0 | } |
1453 | |
|
1454 | 0 | if (node->node.first.getBits() > key.getBits()) { |
1455 | | // key is a super-network of the matching node; split the branch and |
1456 | | // insert a node for the key above the matching node. |
1457 | 0 | node = node->split(key, key.getBits()); |
1458 | 0 | } |
1459 | |
|
1460 | 0 | if (node->left) { |
1461 | 0 | is_left = false; |
1462 | 0 | } |
1463 | |
|
1464 | 0 | node_type& value = node->node; |
1465 | |
|
1466 | 0 | if (!node->assigned) { |
1467 | | // only increment size if not assigned before |
1468 | 0 | d_size++; |
1469 | | // update the pointer to the left-most tree node |
1470 | 0 | if (is_left) { |
1471 | 0 | d_left = node; |
1472 | 0 | } |
1473 | 0 | node->assigned = true; |
1474 | 0 | } |
1475 | 0 | else { |
1476 | | // tree node exists for this value |
1477 | 0 | if (is_left && d_left != node) { |
1478 | 0 | throw std::logic_error( |
1479 | 0 | "NetmaskTree::insert(): lost track of left-most node in tree"); |
1480 | 0 | } |
1481 | 0 | } |
1482 | | |
1483 | 0 | return value; |
1484 | 0 | } |
1485 | | |
1486 | | //<! Creates or updates value |
1487 | | void insert_or_assign(const key_type& mask, const value_type& value) |
1488 | 0 | { |
1489 | 0 | insert(mask).second = value; |
1490 | 0 | } |
1491 | | |
1492 | | void insert_or_assign(const string& mask, const value_type& value) |
1493 | 0 | { |
1494 | 0 | insert(key_type(mask)).second = value; |
1495 | 0 | } |
1496 | | |
1497 | | //<! check if given key is present in TreeMap |
1498 | | [[nodiscard]] bool has_key(const key_type& key) const |
1499 | 0 | { |
1500 | 0 | const node_type* ptr = lookup(key); |
1501 | 0 | return ptr && ptr->first == key; |
1502 | 0 | } |
1503 | | |
1504 | | //<! Returns "best match" for key_type, which might not be value |
1505 | | [[nodiscard]] node_type* lookup(const key_type& value) const |
1506 | 0 | { |
1507 | 0 | uint8_t max_bits = value.getBits(); |
1508 | 0 | return lookupImpl(value, max_bits); |
1509 | 0 | } |
1510 | | |
1511 | | //<! Perform best match lookup for value, using at most max_bits |
1512 | | [[nodiscard]] node_type* lookup(const ComboAddress& value, int max_bits = 128) const |
1513 | 0 | { |
1514 | 0 | uint8_t addr_bits = value.getBits(); |
1515 | 0 | if (max_bits < 0 || max_bits > addr_bits) { |
1516 | 0 | max_bits = addr_bits; |
1517 | 0 | } |
1518 | |
|
1519 | 0 | return lookupImpl(key_type(value, max_bits), max_bits); |
1520 | 0 | } |
1521 | | |
1522 | | //<! Removes key from TreeMap. |
1523 | | void erase(const key_type& key) |
1524 | 0 | { |
1525 | 0 | TreeNode* node = nullptr; |
1526 | |
|
1527 | 0 | if (key.isIPv4()) { |
1528 | 0 | node = d_root->left.get(); |
1529 | 0 | } |
1530 | 0 | else if (key.isIPv6()) { |
1531 | 0 | node = d_root->right.get(); |
1532 | 0 | } |
1533 | 0 | else { |
1534 | 0 | throw NetmaskException("invalid address family"); |
1535 | 0 | } |
1536 | | // no tree, no value |
1537 | 0 | if (node == nullptr) { |
1538 | 0 | return; |
1539 | 0 | } |
1540 | 0 | int bits = 0; |
1541 | 0 | for (; node && bits < key.getBits(); bits++) { |
1542 | 0 | bool vall = key.getBit(-1 - bits); |
1543 | 0 | if (bits >= node->d_bits) { |
1544 | | // the end of the current node is reached; continue with the next |
1545 | 0 | if (vall) { |
1546 | 0 | node = node->right.get(); |
1547 | 0 | } |
1548 | 0 | else { |
1549 | 0 | node = node->left.get(); |
1550 | 0 | } |
1551 | 0 | continue; |
1552 | 0 | } |
1553 | 0 | if (bits >= node->node.first.getBits()) { |
1554 | | // the matching branch ends here |
1555 | 0 | if (key.getBits() != node->node.first.getBits()) { |
1556 | 0 | node = nullptr; |
1557 | 0 | } |
1558 | 0 | break; |
1559 | 0 | } |
1560 | 0 | bool valr = node->node.first.getBit(-1 - bits); |
1561 | 0 | if (vall != valr) { |
1562 | | // the branch matches just upto this point, yet continues in a different |
1563 | | // direction |
1564 | 0 | node = nullptr; |
1565 | 0 | break; |
1566 | 0 | } |
1567 | 0 | } |
1568 | 0 | if (node) { |
1569 | 0 | if (d_size == 0) { |
1570 | 0 | throw std::logic_error( |
1571 | 0 | "NetmaskTree::erase(): size of tree is zero before erase"); |
1572 | 0 | } |
1573 | 0 | d_size--; |
1574 | 0 | node->assigned = false; |
1575 | 0 | node->node.second = value_type(); |
1576 | |
|
1577 | 0 | if (node == d_left) { |
1578 | 0 | d_left = d_left->traverse_lnr_assigned(); |
1579 | 0 | } |
1580 | 0 | cleanup_tree(node); |
1581 | 0 | } |
1582 | 0 | } |
1583 | | |
1584 | | void erase(const string& key) |
1585 | 0 | { |
1586 | 0 | erase(key_type(key)); |
1587 | 0 | } |
1588 | | |
1589 | | //<! checks whether the container is empty. |
1590 | | [[nodiscard]] bool empty() const |
1591 | 0 | { |
1592 | 0 | return (d_size == 0); |
1593 | 0 | } |
1594 | | |
1595 | | //<! returns the number of elements |
1596 | | [[nodiscard]] size_type size() const |
1597 | 0 | { |
1598 | 0 | return d_size; |
1599 | 0 | } |
1600 | | |
1601 | | //<! See if given ComboAddress matches any prefix |
1602 | | [[nodiscard]] bool match(const ComboAddress& value) const |
1603 | 0 | { |
1604 | 0 | return (lookup(value) != nullptr); |
1605 | 0 | } |
1606 | | |
1607 | | [[nodiscard]] bool match(const std::string& value) const |
1608 | 0 | { |
1609 | 0 | return match(ComboAddress(value)); |
1610 | 0 | } |
1611 | | |
1612 | | //<! Clean out the tree |
1613 | | void clear() |
1614 | 0 | { |
1615 | 0 | d_root = make_unique<TreeNode>(); |
1616 | 0 | d_left = nullptr; |
1617 | 0 | d_size = 0; |
1618 | 0 | } |
1619 | | |
1620 | | //<! swaps the contents with another NetmaskTree |
1621 | | void swap(NetmaskTree& rhs) noexcept |
1622 | 0 | { |
1623 | 0 | std::swap(d_root, rhs.d_root); |
1624 | 0 | std::swap(d_left, rhs.d_left); |
1625 | 0 | std::swap(d_size, rhs.d_size); |
1626 | 0 | } |
1627 | | |
1628 | | private: |
1629 | | [[nodiscard]] node_type* lookupImpl(const key_type& value, uint8_t max_bits) const |
1630 | 0 | { |
1631 | 0 | TreeNode* node = nullptr; |
1632 | |
|
1633 | 0 | if (value.isIPv4()) { |
1634 | 0 | node = d_root->left.get(); |
1635 | 0 | } |
1636 | 0 | else if (value.isIPv6()) { |
1637 | 0 | node = d_root->right.get(); |
1638 | 0 | } |
1639 | 0 | else { |
1640 | 0 | throw NetmaskException("invalid address family"); |
1641 | 0 | } |
1642 | 0 | if (node == nullptr) { |
1643 | 0 | return nullptr; |
1644 | 0 | } |
1645 | | |
1646 | 0 | node_type* ret = nullptr; |
1647 | |
|
1648 | 0 | int bits = 0; |
1649 | 0 | for (; bits < max_bits; bits++) { |
1650 | 0 | bool vall = value.getBit(-1 - bits); |
1651 | 0 | if (bits >= node->d_bits) { |
1652 | | // the end of the current node is reached; continue with the next |
1653 | | // (we keep track of last assigned node) |
1654 | 0 | if (node->assigned && bits == node->node.first.getBits()) { |
1655 | 0 | ret = &node->node; |
1656 | 0 | } |
1657 | 0 | if (vall) { |
1658 | 0 | if (!node->right) { |
1659 | 0 | break; |
1660 | 0 | } |
1661 | 0 | node = node->right.get(); |
1662 | 0 | } |
1663 | 0 | else { |
1664 | 0 | if (!node->left) { |
1665 | 0 | break; |
1666 | 0 | } |
1667 | 0 | node = node->left.get(); |
1668 | 0 | } |
1669 | 0 | continue; |
1670 | 0 | } |
1671 | 0 | if (bits >= node->node.first.getBits()) { |
1672 | | // the matching branch ends here |
1673 | 0 | break; |
1674 | 0 | } |
1675 | 0 | bool valr = node->node.first.getBit(-1 - bits); |
1676 | 0 | if (vall != valr) { |
1677 | | // the branch matches just upto this point, yet continues in a different |
1678 | | // direction |
1679 | 0 | break; |
1680 | 0 | } |
1681 | 0 | } |
1682 | | // needed if we did not find one in loop |
1683 | 0 | if (node->assigned && bits == node->node.first.getBits()) { |
1684 | 0 | ret = &node->node; |
1685 | 0 | } |
1686 | | // this can be nullptr. |
1687 | 0 | return ret; |
1688 | 0 | } |
1689 | | |
1690 | | unique_ptr<TreeNode> d_root; //<! Root of our tree |
1691 | | TreeNode* d_left; |
1692 | | size_type d_size{0}; |
1693 | | }; |
1694 | | |
1695 | | /** This class represents a group of supplemental Netmask classes. An IP address matches |
1696 | | if it is matched by one or more of the Netmask objects within. |
1697 | | */ |
1698 | | class NetmaskGroup |
1699 | | { |
1700 | | public: |
1701 | 8 | NetmaskGroup() noexcept = default; |
1702 | | |
1703 | | //! If this IP address is matched by any of the classes within |
1704 | | |
1705 | | bool match(const ComboAddress* address) const |
1706 | 0 | { |
1707 | 0 | const auto& ret = tree.lookup(*address); |
1708 | 0 | if (ret != nullptr) { |
1709 | 0 | return ret->second; |
1710 | 0 | } |
1711 | 0 | return false; |
1712 | 0 | } |
1713 | | |
1714 | | [[nodiscard]] bool match(const ComboAddress& address) const |
1715 | 0 | { |
1716 | 0 | return match(&address); |
1717 | 0 | } |
1718 | | |
1719 | | bool lookup(const ComboAddress* address, Netmask* nmp) const |
1720 | 0 | { |
1721 | 0 | const auto& ret = tree.lookup(*address); |
1722 | 0 | if (ret != nullptr) { |
1723 | 0 | if (nmp != nullptr) { |
1724 | 0 | *nmp = ret->first; |
1725 | 0 | } |
1726 | 0 | return ret->second; |
1727 | 0 | } |
1728 | 0 | return false; |
1729 | 0 | } |
1730 | | |
1731 | | bool lookup(const ComboAddress& address, Netmask* nmp) const |
1732 | 0 | { |
1733 | 0 | return lookup(&address, nmp); |
1734 | 0 | } |
1735 | | |
1736 | | //! Add this string to the list of possible matches |
1737 | | void addMask(const string& address, bool positive = true) |
1738 | 0 | { |
1739 | 0 | if (!address.empty() && address[0] == '!') { |
1740 | 0 | addMask(Netmask(address.substr(1)), false); |
1741 | 0 | } |
1742 | 0 | else { |
1743 | 0 | addMask(Netmask(address), positive); |
1744 | 0 | } |
1745 | 0 | } |
1746 | | |
1747 | | //! Add this Netmask to the list of possible matches |
1748 | | void addMask(const Netmask& netmask, bool positive = true) |
1749 | 0 | { |
1750 | 0 | tree.insert(netmask).second = positive; |
1751 | 0 | } |
1752 | | |
1753 | | void addMasks(const NetmaskGroup& group, boost::optional<bool> positive) |
1754 | 0 | { |
1755 | 0 | for (const auto& entry : group.tree) { |
1756 | 0 | addMask(entry.first, positive ? *positive : entry.second); |
1757 | 0 | } |
1758 | 0 | } |
1759 | | |
1760 | | //! Delete this Netmask from the list of possible matches |
1761 | | void deleteMask(const Netmask& netmask) |
1762 | 0 | { |
1763 | 0 | tree.erase(netmask); |
1764 | 0 | } |
1765 | | |
1766 | | void deleteMasks(const NetmaskGroup& group) |
1767 | 0 | { |
1768 | 0 | for (const auto& entry : group.tree) { |
1769 | 0 | deleteMask(entry.first); |
1770 | 0 | } |
1771 | 0 | } |
1772 | | |
1773 | | void deleteMask(const std::string& address) |
1774 | 0 | { |
1775 | 0 | if (!address.empty()) { |
1776 | 0 | deleteMask(Netmask(address)); |
1777 | 0 | } |
1778 | 0 | } |
1779 | | |
1780 | | void clear() |
1781 | 0 | { |
1782 | 0 | tree.clear(); |
1783 | 0 | } |
1784 | | |
1785 | | [[nodiscard]] bool empty() const |
1786 | 0 | { |
1787 | 0 | return tree.empty(); |
1788 | 0 | } |
1789 | | |
1790 | | [[nodiscard]] size_t size() const |
1791 | 0 | { |
1792 | 0 | return tree.size(); |
1793 | 0 | } |
1794 | | |
1795 | | [[nodiscard]] string toString() const |
1796 | 0 | { |
1797 | 0 | ostringstream str; |
1798 | 0 | for (auto iter = tree.begin(); iter != tree.end(); ++iter) { |
1799 | 0 | if (iter != tree.begin()) { |
1800 | 0 | str << ", "; |
1801 | 0 | } |
1802 | 0 | if (!(iter->second)) { |
1803 | 0 | str << "!"; |
1804 | 0 | } |
1805 | 0 | str << iter->first.toString(); |
1806 | 0 | } |
1807 | 0 | return str.str(); |
1808 | 0 | } |
1809 | | |
1810 | | [[nodiscard]] std::vector<std::string> toStringVector() const |
1811 | 0 | { |
1812 | 0 | std::vector<std::string> out; |
1813 | 0 | out.reserve(tree.size()); |
1814 | 0 | for (const auto& entry : tree) { |
1815 | 0 | out.push_back((entry.second ? "" : "!") + entry.first.toString()); |
1816 | 0 | } |
1817 | 0 | return out; |
1818 | 0 | } |
1819 | | |
1820 | | void toMasks(const string& ips) |
1821 | 0 | { |
1822 | 0 | vector<string> parts; |
1823 | 0 | stringtok(parts, ips, ", \t"); |
1824 | 0 |
|
1825 | 0 | for (const auto& part : parts) { |
1826 | 0 | addMask(part); |
1827 | 0 | } |
1828 | 0 | } |
1829 | | |
1830 | | private: |
1831 | | NetmaskTree<bool> tree; |
1832 | | }; |
1833 | | |
1834 | | struct SComboAddress |
1835 | | { |
1836 | | SComboAddress(const ComboAddress& orig) : |
1837 | 0 | ca(orig) {} |
1838 | | ComboAddress ca; |
1839 | | bool operator<(const SComboAddress& rhs) const |
1840 | 0 | { |
1841 | 0 | return ComboAddress::addressOnlyLessThan()(ca, rhs.ca); |
1842 | 0 | } |
1843 | | operator const ComboAddress&() const |
1844 | 0 | { |
1845 | 0 | return ca; |
1846 | 0 | } |
1847 | | }; |
1848 | | |
1849 | | class NetworkError : public runtime_error |
1850 | | { |
1851 | | public: |
1852 | | NetworkError(const string& why = "Network Error") : |
1853 | 0 | runtime_error(why.c_str()) |
1854 | 0 | {} |
1855 | | NetworkError(const char* why = "Network Error") : |
1856 | | runtime_error(why) |
1857 | 0 | {} |
1858 | | }; |
1859 | | |
1860 | | class AddressAndPortRange |
1861 | | { |
1862 | | public: |
1863 | | AddressAndPortRange() : |
1864 | | d_addrMask(0), d_portMask(0) |
1865 | 0 | { |
1866 | 0 | d_addr.sin4.sin_family = 0; // disable this doing anything useful |
1867 | 0 | d_addr.sin4.sin_port = 0; // this guarantees d_network compares identical |
1868 | 0 | } |
1869 | | |
1870 | | AddressAndPortRange(ComboAddress address, uint8_t addrMask, uint8_t portMask = 0) : |
1871 | | d_addr(address), d_addrMask(addrMask), d_portMask(portMask) |
1872 | 0 | { |
1873 | 0 | if (!d_addr.isIPv4()) { |
1874 | 0 | d_portMask = 0; |
1875 | 0 | } |
1876 | 0 |
|
1877 | 0 | uint16_t port = d_addr.getPort(); |
1878 | 0 | if (d_portMask < 16) { |
1879 | 0 | auto mask = static_cast<uint16_t>(~(0xFFFF >> d_portMask)); |
1880 | 0 | port = port & mask; |
1881 | 0 | } |
1882 | 0 |
|
1883 | 0 | if (d_addrMask < d_addr.getBits()) { |
1884 | 0 | if (d_portMask > 0) { |
1885 | 0 | throw std::runtime_error("Trying to create a AddressAndPortRange with a reduced address mask (" + std::to_string(d_addrMask) + ") and a port range (" + std::to_string(d_portMask) + ")"); |
1886 | 0 | } |
1887 | 0 | d_addr = Netmask(d_addr, d_addrMask).getMaskedNetwork(); |
1888 | 0 | } |
1889 | 0 | d_addr.setPort(port); |
1890 | 0 | } |
1891 | | |
1892 | | [[nodiscard]] uint8_t getFullBits() const |
1893 | 0 | { |
1894 | 0 | return d_addr.getBits() + 16; |
1895 | 0 | } |
1896 | | |
1897 | | [[nodiscard]] uint8_t getBits() const |
1898 | 0 | { |
1899 | 0 | if (d_addrMask < d_addr.getBits()) { |
1900 | 0 | return d_addrMask; |
1901 | 0 | } |
1902 | 0 |
|
1903 | 0 | return d_addr.getBits() + d_portMask; |
1904 | 0 | } |
1905 | | |
1906 | | /** Get the value of the bit at the provided bit index. When the index >= 0, |
1907 | | the index is relative to the LSB starting at index zero. When the index < 0, |
1908 | | the index is relative to the MSB starting at index -1 and counting down. |
1909 | | */ |
1910 | | [[nodiscard]] bool getBit(int index) const |
1911 | 0 | { |
1912 | 0 | if (index >= getFullBits()) { |
1913 | 0 | return false; |
1914 | 0 | } |
1915 | 0 | if (index < 0) { |
1916 | 0 | index = getFullBits() + index; |
1917 | 0 | } |
1918 | 0 |
|
1919 | 0 | if (index < 16) { |
1920 | 0 | /* we are into the port bits */ |
1921 | 0 | uint16_t port = d_addr.getPort(); |
1922 | 0 | return ((port & (1U << index)) != 0x0000); |
1923 | 0 | } |
1924 | 0 |
|
1925 | 0 | index -= 16; |
1926 | 0 |
|
1927 | 0 | return d_addr.getBit(index); |
1928 | 0 | } |
1929 | | |
1930 | | [[nodiscard]] bool isIPv4() const |
1931 | 0 | { |
1932 | 0 | return d_addr.isIPv4(); |
1933 | 0 | } |
1934 | | |
1935 | | [[nodiscard]] bool isIPv6() const |
1936 | 0 | { |
1937 | 0 | return d_addr.isIPv6(); |
1938 | 0 | } |
1939 | | |
1940 | | [[nodiscard]] AddressAndPortRange getNormalized() const |
1941 | 0 | { |
1942 | 0 | return {d_addr, d_addrMask, d_portMask}; |
1943 | 0 | } |
1944 | | |
1945 | | [[nodiscard]] AddressAndPortRange getSuper(uint8_t bits) const |
1946 | 0 | { |
1947 | 0 | if (bits <= d_addrMask) { |
1948 | 0 | return {d_addr, bits, 0}; |
1949 | 0 | } |
1950 | 0 | if (bits <= d_addrMask + d_portMask) { |
1951 | 0 | return {d_addr, d_addrMask, static_cast<uint8_t>(d_portMask - (bits - d_addrMask))}; |
1952 | 0 | } |
1953 | 0 |
|
1954 | 0 | return {d_addr, d_addrMask, d_portMask}; |
1955 | 0 | } |
1956 | | |
1957 | | [[nodiscard]] const ComboAddress& getNetwork() const |
1958 | 0 | { |
1959 | 0 | return d_addr; |
1960 | 0 | } |
1961 | | |
1962 | | [[nodiscard]] string toString() const |
1963 | 0 | { |
1964 | 0 | if (d_addrMask < d_addr.getBits() || d_portMask == 0) { |
1965 | 0 | return d_addr.toStringNoInterface() + "/" + std::to_string(d_addrMask); |
1966 | 0 | } |
1967 | 0 | return d_addr.toStringNoInterface() + ":" + std::to_string(d_addr.getPort()) + "/" + std::to_string(d_portMask); |
1968 | 0 | } |
1969 | | |
1970 | | [[nodiscard]] bool empty() const |
1971 | 0 | { |
1972 | 0 | return d_addr.sin4.sin_family == 0; |
1973 | 0 | } |
1974 | | |
1975 | | bool operator==(const AddressAndPortRange& rhs) const |
1976 | 0 | { |
1977 | 0 | return std::tie(d_addr, d_addrMask, d_portMask) == std::tie(rhs.d_addr, rhs.d_addrMask, rhs.d_portMask); |
1978 | 0 | } |
1979 | | |
1980 | | bool operator<(const AddressAndPortRange& rhs) const |
1981 | 0 | { |
1982 | 0 | if (empty() && !rhs.empty()) { |
1983 | 0 | return false; |
1984 | 0 | } |
1985 | 0 |
|
1986 | 0 | if (!empty() && rhs.empty()) { |
1987 | 0 | return true; |
1988 | 0 | } |
1989 | 0 |
|
1990 | 0 | if (d_addrMask > rhs.d_addrMask) { |
1991 | 0 | return true; |
1992 | 0 | } |
1993 | 0 |
|
1994 | 0 | if (d_addrMask < rhs.d_addrMask) { |
1995 | 0 | return false; |
1996 | 0 | } |
1997 | 0 |
|
1998 | 0 | if (d_addr < rhs.d_addr) { |
1999 | 0 | return true; |
2000 | 0 | } |
2001 | 0 |
|
2002 | 0 | if (d_addr > rhs.d_addr) { |
2003 | 0 | return false; |
2004 | 0 | } |
2005 | 0 |
|
2006 | 0 | if (d_portMask > rhs.d_portMask) { |
2007 | 0 | return true; |
2008 | 0 | } |
2009 | 0 |
|
2010 | 0 | if (d_portMask < rhs.d_portMask) { |
2011 | 0 | return false; |
2012 | 0 | } |
2013 | 0 |
|
2014 | 0 | return d_addr.getPort() < rhs.d_addr.getPort(); |
2015 | 0 | } |
2016 | | |
2017 | | bool operator>(const AddressAndPortRange& rhs) const |
2018 | 0 | { |
2019 | 0 | return rhs.operator<(*this); |
2020 | 0 | } |
2021 | | |
2022 | | struct hash |
2023 | | { |
2024 | | uint32_t operator()(const AddressAndPortRange& apr) const |
2025 | 0 | { |
2026 | 0 | ComboAddress::addressOnlyHash hashOp; |
2027 | 0 | uint16_t port = apr.d_addr.getPort(); |
2028 | 0 | /* it's fine to hash the whole address and port because the non-relevant parts have |
2029 | 0 | been masked to 0 */ |
2030 | 0 | return burtle(reinterpret_cast<const unsigned char*>(&port), sizeof(port), hashOp(apr.d_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) |
2031 | 0 | } |
2032 | | }; |
2033 | | |
2034 | | private: |
2035 | | ComboAddress d_addr; |
2036 | | uint8_t d_addrMask; |
2037 | | /* only used for v4 addresses */ |
2038 | | uint8_t d_portMask; |
2039 | | }; |
2040 | | |
2041 | | int SSocket(int family, int type, int flags); |
2042 | | int SConnect(int sockfd, const ComboAddress& remote); |
2043 | | /* tries to connect to remote for a maximum of timeout seconds. |
2044 | | sockfd should be set to non-blocking beforehand. |
2045 | | returns 0 on success (the socket is writable), throw a |
2046 | | runtime_error otherwise */ |
2047 | | int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct timeval& timeout); |
2048 | | int SBind(int sockfd, const ComboAddress& local); |
2049 | | int SAccept(int sockfd, ComboAddress& remote); |
2050 | | int SListen(int sockfd, int limit); |
2051 | | int SSetsockopt(int sockfd, int level, int opname, int value); |
2052 | | void setSocketIgnorePMTU(int sockfd, int family); |
2053 | | void setSocketForcePMTU(int sockfd, int family); |
2054 | | bool setReusePort(int sockfd); |
2055 | | |
2056 | | #if defined(IP_PKTINFO) |
2057 | | #define GEN_IP_PKTINFO IP_PKTINFO |
2058 | | #elif defined(IP_RECVDSTADDR) |
2059 | | #define GEN_IP_PKTINFO IP_RECVDSTADDR |
2060 | | #endif |
2061 | | |
2062 | | bool IsAnyAddress(const ComboAddress& addr); |
2063 | | bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destination); |
2064 | | bool HarvestTimestamp(struct msghdr* msgh, struct timeval* timeval); |
2065 | | void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr); |
2066 | | int sendOnNBSocket(int fileDesc, const struct msghdr* msgh); |
2067 | | size_t sendMsgWithOptions(int socketDesc, const void* buffer, size_t len, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int flags); |
2068 | | |
2069 | | /* requires a non-blocking, connected TCP socket */ |
2070 | | bool isTCPSocketUsable(int sock); |
2071 | | |
2072 | | extern template class NetmaskTree<bool>; |
2073 | | ComboAddress parseIPAndPort(const std::string& input, uint16_t port); |
2074 | | |
2075 | | std::set<std::string> getListOfNetworkInterfaces(); |
2076 | | std::vector<ComboAddress> getListOfAddressesOfNetworkInterface(const std::string& itf); |
2077 | | std::vector<Netmask> getListOfRangesOfNetworkInterface(const std::string& itf); |
2078 | | |
2079 | | /* These functions throw if the value was already set to a higher value, |
2080 | | or on error */ |
2081 | | void setSocketBuffer(int fileDesc, int optname, uint32_t size); |
2082 | | void setSocketReceiveBuffer(int fileDesc, uint32_t size); |
2083 | | void setSocketSendBuffer(int fileDesc, uint32_t size); |
2084 | | uint32_t raiseSocketReceiveBufferToMax(int socket); |
2085 | | uint32_t raiseSocketSendBufferToMax(int socket); |