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