• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 18942682241

30 Oct 2025 01:42PM UTC coverage: 73.013% (+0.01%) from 73.003%
18942682241

Pull #16398

github

web-flow
Merge 4f6f00c0f into 21d53b639
Pull Request #16398: auth sdist: copy files as files, not as new dirs

38268 of 63120 branches covered (60.63%)

Branch coverage included in aggregate %.

127457 of 163861 relevant lines covered (77.78%)

6616039.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.16
/pdns/iputils.hh
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

56
#if defined(CONNECT_DATA_IDEMPOTENT) && defined(CONNECT_RESUME_ON_READ_WRITE)
57
#define CONNECTX_FASTOPEN 1
58
#endif
59

60
#endif
61

62
#ifdef __sun
63

64
#define htobe16(x) BE_16(x)
65
#define htole16(x) LE_16(x)
66
#define be16toh(x) BE_IN16(&(x))
67
#define le16toh(x) LE_IN16(&(x))
68

69
#define htobe32(x) BE_32(x)
70
#define htole32(x) LE_32(x)
71
#define be32toh(x) BE_IN32(&(x))
72
#define le32toh(x) LE_IN32(&(x))
73

74
#define htobe64(x) BE_64(x)
75
#define htole64(x) LE_64(x)
76
#define be64toh(x) BE_IN64(&(x))
77
#define le64toh(x) LE_IN64(&(x))
78

79
#endif
80

81
#ifdef __FreeBSD__
82
#include <sys/endian.h>
83
#endif
84

85
#if defined(__NetBSD__) && defined(IP_PKTINFO) && !defined(IP_SENDSRCADDR)
86
// The IP_PKTINFO option in NetBSD was incompatible with Linux until a
87
// change that also introduced IP_SENDSRCADDR for FreeBSD compatibility.
88
#undef IP_PKTINFO
89
#endif
90

91
union ComboAddress
92
{
93
  sockaddr_in sin4{};
94
  sockaddr_in6 sin6;
95

96
  bool operator==(const ComboAddress& rhs) const
97
  {
38,306,249✔
98
    if (std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) {
38,306,249✔
99
      return false;
15,532,042✔
100
    }
15,532,042✔
101
    if (sin4.sin_family == AF_INET) {
22,774,208✔
102
      return sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr;
11,642,715✔
103
    }
11,642,715✔
104
    return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) == 0;
2,158,615,139✔
105
  }
22,774,207✔
106

107
  bool operator!=(const ComboAddress& rhs) const
108
  {
6,013,798✔
109
    return (!operator==(rhs));
6,013,798✔
110
  }
6,013,798✔
111

112
  bool operator<(const ComboAddress& rhs) const
113
  {
2,089,098✔
114
    if (sin4.sin_family == 0) {
2,089,098✔
115
      return false;
70,978✔
116
    }
70,978✔
117
    if (std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) {
2,018,120✔
118
      return true;
23,887✔
119
    }
23,887✔
120
    if (std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) {
1,994,233✔
121
      return false;
11,031✔
122
    }
11,031✔
123
    if (sin4.sin_family == AF_INET) {
1,983,222✔
124
      return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr;
1,394,955✔
125
    }
1,394,955✔
126
    return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0;
2,148,071,881✔
127
  }
1,983,202✔
128

129
  bool operator>(const ComboAddress& rhs) const
130
  {
5✔
131
    return rhs.operator<(*this);
5✔
132
  }
5✔
133

134
  struct addressPortOnlyHash
135
  {
136
    uint32_t operator()(const ComboAddress& address) const
137
    {
×
138
      // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
139
      if (address.sin4.sin_family == AF_INET) {
×
140
        const auto* start = reinterpret_cast<const unsigned char*>(&address.sin4.sin_addr.s_addr);
×
141
        auto tmp = burtle(start, 4, 0);
×
142
        return burtle(reinterpret_cast<const uint8_t*>(&address.sin4.sin_port), 2, tmp);
×
143
      }
×
144
      const auto* start = reinterpret_cast<const unsigned char*>(&address.sin6.sin6_addr.s6_addr);
×
145
      auto tmp = burtle(start, 16, 0);
×
146
      return burtle(reinterpret_cast<const unsigned char*>(&address.sin6.sin6_port), 2, tmp);
×
147
      // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
148
    }
×
149
  };
150

151
  struct addressOnlyHash
152
  {
153
    uint32_t operator()(const ComboAddress& address) const
154
    {
619,483✔
155
      const unsigned char* start = nullptr;
619,483✔
156
      uint32_t len = 0;
619,483✔
157
      // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
158
      if (address.sin4.sin_family == AF_INET) {
619,483✔
159
        start = reinterpret_cast<const unsigned char*>(&address.sin4.sin_addr.s_addr);
618,532✔
160
        len = 4;
618,532✔
161
      }
618,532✔
162
      else {
951✔
163
        start = reinterpret_cast<const unsigned char*>(&address.sin6.sin6_addr.s6_addr);
951✔
164
        len = 16;
951✔
165
      }
951✔
166
      // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
167
      return burtle(start, len, 0);
619,483✔
168
    }
619,483✔
169
  };
170

171
  struct addressOnlyLessThan
172
  {
173
    bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const
174
    {
2,348✔
175
      if (lhs.sin4.sin_family < rhs.sin4.sin_family) {
2,348✔
176
        return true;
37✔
177
      }
37✔
178
      if (lhs.sin4.sin_family > rhs.sin4.sin_family) {
2,311✔
179
        return false;
6✔
180
      }
6✔
181
      if (lhs.sin4.sin_family == AF_INET) {
2,305✔
182
        return lhs.sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr;
2,249✔
183
      }
2,249✔
184
      return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) < 0;
56✔
185
    }
2,305✔
186
  };
187

188
  struct addressOnlyEqual
189
  {
190
    bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const
191
    {
4,080✔
192
      if (lhs.sin4.sin_family != rhs.sin4.sin_family) {
4,080✔
193
        return false;
1,701✔
194
      }
1,701✔
195
      if (lhs.sin4.sin_family == AF_INET) {
2,379✔
196
        return lhs.sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr;
2,359✔
197
      }
2,359✔
198
      return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) == 0;
20✔
199
    }
2,379✔
200
  };
201

202
  [[nodiscard]] socklen_t getSocklen() const
203
  {
1,778,860✔
204
    if (sin4.sin_family == AF_INET) {
1,778,860✔
205
      return sizeof(sin4);
431,151✔
206
    }
431,151✔
207
    return sizeof(sin6);
1,347,709✔
208
  }
1,778,860✔
209

210
  ComboAddress()
211
  {
119,328,452✔
212
    sin4.sin_family = AF_INET;
119,328,452✔
213
    sin4.sin_addr.s_addr = 0;
119,328,452✔
214
    sin4.sin_port = 0;
119,328,452✔
215
    sin6.sin6_scope_id = 0;
119,328,452✔
216
    sin6.sin6_flowinfo = 0;
119,328,452✔
217
  }
119,328,452✔
218

219
  ComboAddress(const struct sockaddr* socketAddress, socklen_t salen)
220
  {
110✔
221
    setSockaddr(socketAddress, salen);
110✔
222
  };
110✔
223

224
  ComboAddress(const struct sockaddr_in6* socketAddress)
225
  {
4✔
226
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
227
    setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in6));
4✔
228
  };
4✔
229

230
  ComboAddress(const struct sockaddr_in* socketAddress)
231
  {
6✔
232
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
233
    setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in));
6✔
234
  };
6✔
235

236
  void setSockaddr(const struct sockaddr* socketAddress, socklen_t salen)
237
  {
392✔
238
    if (salen > sizeof(struct sockaddr_in6)) {
392!
239
      throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6");
×
240
    }
×
241
    memcpy(this, socketAddress, salen);
392✔
242
  }
392✔
243

244
  // 'port' sets a default value in case 'str' does not set a port
245
  explicit ComboAddress(const string& str, uint16_t port = 0)
246
  {
2,002,526✔
247
    memset(&sin6, 0, sizeof(sin6));
2,002,526✔
248
    sin4.sin_family = AF_INET;
2,002,526✔
249
    sin4.sin_port = 0;
2,002,526✔
250
    if (makeIPv4sockaddr(str, &sin4) != 0) {
2,002,526✔
251
      sin6.sin6_family = AF_INET6;
933,405✔
252
      if (makeIPv6sockaddr(str, &sin6) < 0) {
933,405✔
253
        throw PDNSException("Unable to convert presentation address '" + str + "'");
109✔
254
      }
109✔
255
    }
933,405✔
256
    if (sin4.sin_port == 0) { // 'str' overrides port!
2,002,417✔
257
      sin4.sin_port = htons(port);
1,727,851✔
258
    }
1,727,851✔
259
  }
2,002,417✔
260

261
  [[nodiscard]] bool isIPv6() const
262
  {
161,392,914✔
263
    return sin4.sin_family == AF_INET6;
161,392,914✔
264
  }
161,392,914✔
265
  [[nodiscard]] bool isIPv4() const
266
  {
295,964,085✔
267
    return sin4.sin_family == AF_INET;
295,964,085✔
268
  }
295,964,085✔
269

270
  [[nodiscard]] bool isMappedIPv4() const
271
  {
×
272
    if (sin4.sin_family != AF_INET6) {
×
273
      return false;
×
274
    }
×
275

276
    int iter = 0;
×
277
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
278
    const auto* ptr = reinterpret_cast<const unsigned char*>(&sin6.sin6_addr.s6_addr);
×
279
    for (iter = 0; iter < 10; ++iter) {
×
280
      if (ptr[iter] != 0) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
×
281
        return false;
×
282
      }
×
283
    }
×
284
    for (; iter < 12; ++iter) {
×
285
      if (ptr[iter] != 0xff) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
×
286
        return false;
×
287
      }
×
288
    }
×
289
    return true;
×
290
  }
×
291

292
  [[nodiscard]] bool isUnspecified() const
293
  {
1,732✔
294
    static const ComboAddress unspecifiedV4("0.0.0.0:0");
1,732✔
295
    static const ComboAddress unspecifiedV6("[::]:0");
1,732✔
296
    const auto compare = ComboAddress::addressOnlyEqual();
1,732✔
297
    return compare(*this, unspecifiedV4) || compare(*this, unspecifiedV6);
1,732✔
298
  }
1,732✔
299

300
  [[nodiscard]] ComboAddress mapToIPv4() const
301
  {
×
302
    if (!isMappedIPv4()) {
×
303
      throw PDNSException("ComboAddress can't map non-mapped IPv6 address back to IPv4");
×
304
    }
×
305
    ComboAddress ret;
×
306
    ret.sin4.sin_family = AF_INET;
×
307
    ret.sin4.sin_port = sin4.sin_port;
×
308

309
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
310
    const auto* ptr = reinterpret_cast<const unsigned char*>(&sin6.sin6_addr.s6_addr);
×
311
    ptr += (sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
×
312
    memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr));
×
313
    return ret;
×
314
  }
×
315

316
  [[nodiscard]] string toString() const
317
  {
1,395,715✔
318
    std::array<char, 1024> host{};
1,395,715✔
319
    if (sin4.sin_family != 0) {
1,395,717✔
320
      // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
321
      int retval = getnameinfo(reinterpret_cast<const struct sockaddr*>(this), getSocklen(), host.data(), host.size(), nullptr, 0, NI_NUMERICHOST);
1,395,709✔
322
      if (retval == 0) {
1,395,715✔
323
        return host.data();
1,395,709✔
324
      }
1,395,709✔
325
      return "invalid " + string(gai_strerror(retval));
8,589,934,594✔
326
    }
1,395,709✔
327
    return "invalid";
2,147,483,655✔
328
  }
1,395,715✔
329

330
  //! Ignores any interface specifiers possibly available in the sockaddr data.
331
  [[nodiscard]] string toStringNoInterface() const
332
  {
5,206✔
333
    std::array<char, 1024> host{};
5,206✔
334
    if (sin4.sin_family == AF_INET) {
5,206✔
335
      const auto* ret = inet_ntop(sin4.sin_family, &sin4.sin_addr, host.data(), host.size());
4,432✔
336
      if (ret != nullptr) {
4,432!
337
        return host.data();
4,432✔
338
      }
4,432✔
339
    }
4,432✔
340
    else if (sin4.sin_family == AF_INET6) {
774!
341
      const auto* ret = inet_ntop(sin4.sin_family, &sin6.sin6_addr, host.data(), host.size());
774✔
342
      if (ret != nullptr) {
774!
343
        return host.data();
774✔
344
      }
774✔
345
    }
774✔
346
    else {
×
347
      return "invalid";
×
348
    }
×
349
    return "invalid " + stringerror();
×
350
  }
5,206✔
351

352
  [[nodiscard]] string toStringReversed() const
353
  {
70✔
354
    if (isIPv4()) {
70✔
355
      const auto address = ntohl(sin4.sin_addr.s_addr);
34✔
356
      auto aaa = (address >> 0) & 0xFF;
34✔
357
      auto bbb = (address >> 8) & 0xFF;
34✔
358
      auto ccc = (address >> 16) & 0xFF;
34✔
359
      auto ddd = (address >> 24) & 0xFF;
34✔
360
      return std::to_string(aaa) + "." + std::to_string(bbb) + "." + std::to_string(ccc) + "." + std::to_string(ddd);
34✔
361
    }
34✔
362
    const auto* addr = &sin6.sin6_addr;
36✔
363
    std::stringstream res{};
36✔
364
    res << std::hex;
36✔
365
    for (int i = 15; i >= 0; i--) {
612✔
366
      auto byte = addr->s6_addr[i]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
576✔
367
      res << ((byte >> 0) & 0xF) << ".";
576✔
368
      res << ((byte >> 4) & 0xF);
576✔
369
      if (i != 0) {
576✔
370
        res << ".";
540✔
371
      }
540✔
372
    }
576✔
373
    return res.str();
36✔
374
  }
70✔
375

376
  [[nodiscard]] string toStringWithPort() const
377
  {
19,777✔
378
    if (sin4.sin_family == AF_INET) {
19,777✔
379
      return toString() + ":" + std::to_string(ntohs(sin4.sin_port));
19,444✔
380
    }
19,444✔
381
    return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port));
333✔
382
  }
19,777✔
383

384
  [[nodiscard]] string toStringWithPortExcept(int port) const
385
  {
15,969✔
386
    if (ntohs(sin4.sin_port) == port) {
15,969✔
387
      return toString();
13,378✔
388
    }
13,378✔
389
    if (sin4.sin_family == AF_INET) {
2,591✔
390
      return toString() + ":" + std::to_string(ntohs(sin4.sin_port));
2,554✔
391
    }
2,554✔
392
    return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port));
37✔
393
  }
2,591✔
394

395
  [[nodiscard]] string toLogString() const
396
  {
1,790✔
397
    return toStringWithPortExcept(53);
1,790✔
398
  }
1,790✔
399

400
  [[nodiscard]] string toStructuredLogString() const
401
  {
490✔
402
    return toStringWithPort();
490✔
403
  }
490✔
404

405
  [[nodiscard]] string toByteString() const
406
  {
344✔
407
    // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
408
    if (isIPv4()) {
344✔
409
      return {reinterpret_cast<const char*>(&sin4.sin_addr.s_addr), sizeof(sin4.sin_addr.s_addr)};
326✔
410
    }
326✔
411
    return {reinterpret_cast<const char*>(&sin6.sin6_addr.s6_addr), sizeof(sin6.sin6_addr.s6_addr)};
18✔
412
    // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
413
  }
344✔
414

415
  void truncate(unsigned int bits) noexcept;
416

417
  [[nodiscard]] uint16_t getNetworkOrderPort() const noexcept
418
  {
1,030,992✔
419
    return sin4.sin_port;
1,030,992✔
420
  }
1,030,992✔
421
  [[nodiscard]] uint16_t getPort() const noexcept
422
  {
1,030,993✔
423
    return ntohs(getNetworkOrderPort());
1,030,993✔
424
  }
1,030,993✔
425
  void setPort(uint16_t port)
426
  {
292,135✔
427
    sin4.sin_port = htons(port);
292,135✔
428
  }
292,135✔
429

430
  void reset()
431
  {
772,251✔
432
    memset(&sin6, 0, sizeof(sin6));
772,251✔
433
  }
772,251✔
434

435
  //! Get the total number of address bits (either 32 or 128 depending on IP version)
436
  [[nodiscard]] uint8_t getBits() const
437
  {
95,039,473✔
438
    if (isIPv4()) {
95,039,473✔
439
      return 32;
42,988,870✔
440
    }
42,988,870✔
441
    if (isIPv6()) {
52,050,611✔
442
      return 128;
52,050,605✔
443
    }
52,050,605✔
444
    return 0;
6,442,450,943✔
445
  }
52,050,603✔
446
  /** Get the value of the bit at the provided bit index. When the index >= 0,
447
      the index is relative to the LSB starting at index zero. When the index < 0,
448
      the index is relative to the MSB starting at index -1 and counting down.
449
   */
450
  [[nodiscard]] bool getBit(int index) const
451
  {
162,156,500✔
452
    if (isIPv4()) {
162,156,511✔
453
      if (index >= 32) {
52,880,400!
454
        return false;
×
455
      }
×
456
      if (index < 0) {
52,880,411✔
457
        if (index < -32) {
40,179,331!
458
          return false;
×
459
        }
×
460
        index = 32 + index;
40,179,331✔
461
      }
40,179,331✔
462

463
      uint32_t ls_addr = ntohl(sin4.sin_addr.s_addr);
52,880,400✔
464

465
      return ((ls_addr & (1U << index)) != 0x00000000);
52,880,400✔
466
    }
52,880,400✔
467
    if (isIPv6()) {
2,256,759,761✔
468
      if (index >= 128) {
109,276,106!
469
        return false;
×
470
      }
×
471
      if (index < 0) {
109,276,106✔
472
        if (index < -128) {
92,497,739!
473
          return false;
×
474
        }
×
475
        index = 128 + index;
92,497,739✔
476
      }
92,497,739✔
477

478
      const auto* ls_addr = reinterpret_cast<const uint8_t*>(sin6.sin6_addr.s6_addr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
109,276,106✔
479
      uint8_t byte_idx = index / 8;
109,276,106✔
480
      uint8_t bit_idx = index % 8;
109,276,106✔
481

482
      return ((ls_addr[15 - byte_idx] & (1U << bit_idx)) != 0x00); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
109,276,106✔
483
    }
109,276,106✔
484
    return false;
4,294,967,295✔
485
  }
2,256,759,758✔
486

487
  /*! Returns a comma-separated string of IP addresses
488
   *
489
   * \param c  An stl container with ComboAddresses
490
   * \param withPort  Also print the port (default true)
491
   * \param portExcept  Print the port, except when this is the port (default 53)
492
   */
493
  template <template <class...> class Container, class... Args>
494
  static string caContainerToString(const Container<ComboAddress, Args...>& container, const bool withPort = true, const uint16_t portExcept = 53)
495
  {
110✔
496
    vector<string> strs;
110✔
497
    for (const auto& address : container) {
247✔
498
      if (withPort) {
247✔
499
        strs.push_back(address.toStringWithPortExcept(portExcept));
80✔
500
        continue;
80✔
501
      }
80✔
502
      strs.push_back(address.toString());
167✔
503
    }
167✔
504
    return boost::join(strs, ",");
110✔
505
  };
110✔
506
};
507

508
union SockaddrWrapper
509
{
510
  sockaddr_in sin4{};
511
  sockaddr_in6 sin6;
512
  sockaddr_un sinun;
513

514
  [[nodiscard]] socklen_t getSocklen() const
515
  {
30✔
516
    if (sin4.sin_family == AF_INET) {
30!
517
      return sizeof(sin4);
30✔
518
    }
30✔
519
    if (sin6.sin6_family == AF_INET6) {
×
520
      return sizeof(sin6);
×
521
    }
×
522
    if (sinun.sun_family == AF_UNIX) {
×
523
      return sizeof(sinun);
×
524
    }
×
525
    return 0;
×
526
  }
×
527

528
  SockaddrWrapper()
529
  {
×
530
    sin4.sin_family = AF_INET;
×
531
    sin4.sin_addr.s_addr = 0;
×
532
    sin4.sin_port = 0;
×
533
  }
×
534

535
  SockaddrWrapper(const struct sockaddr* socketAddress, socklen_t salen)
536
  {
×
537
    setSockaddr(socketAddress, salen);
×
538
  };
×
539

540
  SockaddrWrapper(const struct sockaddr_in6* socketAddress)
541
  {
×
542
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
×
543
    setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in6));
×
544
  };
×
545

546
  SockaddrWrapper(const struct sockaddr_in* socketAddress)
547
  {
×
548
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
×
549
    setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_in));
×
550
  };
×
551

552
  SockaddrWrapper(const struct sockaddr_un* socketAddress)
553
  {
×
554
    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
×
555
    setSockaddr(reinterpret_cast<const struct sockaddr*>(socketAddress), sizeof(struct sockaddr_un));
×
556
  };
×
557

558
  void setSockaddr(const struct sockaddr* socketAddress, socklen_t salen)
559
  {
×
560
    if (salen > sizeof(struct sockaddr_un)) {
×
561
      throw PDNSException("ComboAddress can't handle other than sockaddr_in, sockaddr_in6 or sockaddr_un");
×
562
    }
×
563
    memcpy(this, socketAddress, salen);
×
564
  }
×
565

566
  explicit SockaddrWrapper(const string& str, uint16_t port = 0)
567
  {
90✔
568
    memset(&sinun, 0, sizeof(sinun));
90✔
569
    sin4.sin_family = AF_INET;
90✔
570
    sin4.sin_port = 0;
90✔
571
    if (str == "\"\"" || str == "''") {
90✔
572
      throw PDNSException("Stray quotation marks in address.");
10✔
573
    }
10✔
574
    if (makeIPv4sockaddr(str, &sin4) != 0) {
80✔
575
      sin6.sin6_family = AF_INET6;
45✔
576
      if (makeIPv6sockaddr(str, &sin6) < 0) {
45✔
577
        sinun.sun_family = AF_UNIX;
25✔
578
        // only attempt Unix socket address if address candidate does not contain a port
579
        if (str.find(':') != string::npos || makeUNsockaddr(str, &sinun) < 0) {
25!
580
          throw PDNSException("Unable to convert presentation address '" + str + "'");
20✔
581
        }
20✔
582
      }
25✔
583
    }
45✔
584
    if (sinun.sun_family != AF_UNIX && sin4.sin_port == 0) { // 'str' overrides port!
60✔
585
      sin4.sin_port = htons(port);
35✔
586
    }
35✔
587
  }
60✔
588

589
  [[nodiscard]] bool isIPv6() const
590
  {
×
591
    return sin4.sin_family == AF_INET6;
×
592
  }
×
593
  [[nodiscard]] bool isIPv4() const
594
  {
×
595
    return sin4.sin_family == AF_INET;
×
596
  }
×
597
  [[nodiscard]] bool isUnixSocket() const
598
  {
1,591✔
599
    return sin4.sin_family == AF_UNIX;
1,591✔
600
  }
1,591✔
601

602
  [[nodiscard]] string toString() const
603
  {
15✔
604
    if (sinun.sun_family == AF_UNIX) {
15!
605
      return sinun.sun_path;
×
606
    }
×
607
    std::array<char, 1024> host{};
15✔
608
    if (sin4.sin_family != 0) {
15!
609
      // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
610
      int retval = getnameinfo(reinterpret_cast<const struct sockaddr*>(this), getSocklen(), host.data(), host.size(), nullptr, 0, NI_NUMERICHOST);
15✔
611
      if (retval == 0) {
15!
612
        return host.data();
15✔
613
      }
15✔
614
      return "invalid " + string(gai_strerror(retval));
×
615
    }
15✔
616
    return "invalid";
×
617
  }
15✔
618

619
  [[nodiscard]] string toStringWithPort() const
620
  {
15✔
621
    if (sinun.sun_family == AF_UNIX) {
15!
622
      return toString();
×
623
    }
×
624
    if (sin4.sin_family == AF_INET) {
15!
625
      return toString() + ":" + std::to_string(ntohs(sin4.sin_port));
15✔
626
    }
15✔
627
    return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port));
×
628
  }
15✔
629

630
  void reset()
631
  {
×
632
    memset(&sinun, 0, sizeof(sinun));
×
633
  }
×
634
};
635

636
/** This exception is thrown by the Netmask class and by extension by the NetmaskGroup class */
637
class NetmaskException : public PDNSException
638
{
639
public:
640
  NetmaskException(const string& arg) :
641
    PDNSException(arg) {}
355✔
642
};
643

644
inline ComboAddress makeComboAddress(const string& str)
645
{
708,982✔
646
  ComboAddress address;
708,982✔
647
  address.sin4.sin_family = AF_INET;
708,982✔
648
  if (inet_pton(AF_INET, str.c_str(), &address.sin4.sin_addr) <= 0) {
708,982✔
649
    address.sin4.sin_family = AF_INET6;
342,577✔
650
    if (makeIPv6sockaddr(str, &address.sin6) < 0) {
342,577✔
651
      throw NetmaskException("Unable to convert '" + str + "' to a netmask");
355✔
652
    }
355✔
653
  }
342,577✔
654
  return address;
708,627✔
655
}
708,982✔
656

657
inline ComboAddress makeComboAddressFromRaw(uint8_t version, const char* raw, size_t len)
658
{
307✔
659
  ComboAddress address;
307✔
660

661
  if (version == 4) {
307✔
662
    address.sin4.sin_family = AF_INET;
188✔
663
    if (len != sizeof(address.sin4.sin_addr)) {
188!
664
      throw NetmaskException("invalid raw address length");
×
665
    }
×
666
    memcpy(&address.sin4.sin_addr, raw, sizeof(address.sin4.sin_addr));
188✔
667
  }
188✔
668
  else if (version == 6) {
119!
669
    address.sin6.sin6_family = AF_INET6;
119✔
670
    if (len != sizeof(address.sin6.sin6_addr)) {
119!
671
      throw NetmaskException("invalid raw address length");
×
672
    }
×
673
    memcpy(&address.sin6.sin6_addr, raw, sizeof(address.sin6.sin6_addr));
119✔
674
  }
119✔
675
  else {
×
676
    throw NetmaskException("invalid address family");
×
677
  }
×
678

679
  return address;
307✔
680
}
307✔
681

682
inline ComboAddress makeComboAddressFromRaw(uint8_t version, const string& str)
683
{
119✔
684
  return makeComboAddressFromRaw(version, str.c_str(), str.size());
119✔
685
}
119✔
686

687
/** This class represents a netmask and can be queried to see if a certain
688
    IP address is matched by this mask */
689
class Netmask
690
{
691
public:
692
  Netmask()
693
  {
5,120,631✔
694
    d_network.sin4.sin_family = 0; // disable this doing anything useful
5,120,631✔
695
    d_network.sin4.sin_port = 0; // this guarantees d_network compares identical
5,120,631✔
696
  }
5,120,631✔
697

698
  Netmask(const ComboAddress& network, uint8_t bits = 0xff) :
699
    d_network(network)
14,332,084✔
700
  {
37,460,804✔
701
    d_network.sin4.sin_port = 0;
37,460,804✔
702
    setBits(bits);
37,460,804✔
703
  }
37,460,804✔
704

705
  Netmask(const sockaddr_in* network, uint8_t bits = 0xff) :
706
    d_network(network)
707
  {
×
708
    d_network.sin4.sin_port = 0;
×
709
    setBits(bits);
×
710
  }
×
711
  Netmask(const sockaddr_in6* network, uint8_t bits = 0xff) :
712
    d_network(network)
713
  {
×
714
    d_network.sin4.sin_port = 0;
×
715
    setBits(bits);
×
716
  }
×
717
  void setBits(uint8_t value)
718
  {
38,169,434✔
719
    d_bits = d_network.isIPv4() ? std::min(value, static_cast<uint8_t>(32U)) : std::min(value, static_cast<uint8_t>(128U));
38,169,434✔
720

721
    if (d_bits < 32) {
38,169,434✔
722
      d_mask = ~(0xFFFFFFFF >> d_bits);
8,047,369✔
723
    }
8,047,369✔
724
    else {
30,122,065✔
725
      // note that d_mask is unused for IPv6
726
      d_mask = 0xFFFFFFFF;
30,122,065✔
727
    }
30,122,065✔
728

729
    if (isIPv4()) {
38,169,434✔
730
      d_network.sin4.sin_addr.s_addr = htonl(ntohl(d_network.sin4.sin_addr.s_addr) & d_mask);
15,881,866✔
731
    }
15,881,866✔
732
    else if (isIPv6()) {
22,287,580✔
733
      uint8_t bytes = d_bits / 8;
21,038,691✔
734
      auto* address = reinterpret_cast<uint8_t*>(&d_network.sin6.sin6_addr.s6_addr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
21,038,691✔
735
      uint8_t bits = d_bits % 8;
21,038,691✔
736
      auto mask = static_cast<uint8_t>(~(0xFF >> bits));
21,038,691✔
737

738
      if (bytes < sizeof(d_network.sin6.sin6_addr.s6_addr)) {
21,038,691✔
739
        address[bytes] &= mask; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
17,074,926✔
740
      }
17,074,926✔
741

742
      for (size_t idx = bytes + 1; idx < sizeof(d_network.sin6.sin6_addr.s6_addr); ++idx) {
156,887,327✔
743
        address[idx] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
135,848,636✔
744
      }
135,848,636✔
745
    }
21,038,691✔
746
  }
38,169,434✔
747

748
  enum stringType
749
  {
750
    humanString,
751
    byteString,
752
  };
753
  //! Constructor supplies the mask, which cannot be changed
754
  Netmask(const string& mask, stringType type = humanString)
755
  {
709,000✔
756
    if (type == byteString) {
709,000✔
757
      uint8_t afi = mask.at(0);
56✔
758
      size_t len = afi == 4 ? 4 : 16;
56!
759
      uint8_t bits = mask.at(len + 1);
56✔
760

761
      d_network = makeComboAddressFromRaw(afi, mask.substr(1, len));
56✔
762

763
      setBits(bits);
56✔
764
    }
56✔
765
    else {
708,944✔
766
      pair<string, string> split = splitField(mask, '/');
708,944✔
767
      d_network = makeComboAddress(split.first);
708,944✔
768

769
      if (!split.second.empty()) {
708,944✔
770
        setBits(pdns::checked_stoi<uint8_t>(split.second));
371,173✔
771
      }
371,173✔
772
      else if (d_network.sin4.sin_family == AF_INET) {
337,771✔
773
        setBits(32);
332,592✔
774
      }
332,592✔
775
      else {
5,179✔
776
        setBits(128);
5,179✔
777
      }
5,179✔
778
    }
708,944✔
779
  }
709,000✔
780

781
  [[nodiscard]] bool match(const ComboAddress& address) const
782
  {
79✔
783
    return match(&address);
79✔
784
  }
79✔
785

786
  //! If this IP address in socket address matches
787
  bool match(const ComboAddress* address) const
788
  {
114✔
789
    if (d_network.sin4.sin_family != address->sin4.sin_family) {
114!
790
      return false;
×
791
    }
×
792
    if (d_network.sin4.sin_family == AF_INET) {
114✔
793
      return match4(htonl((unsigned int)address->sin4.sin_addr.s_addr));
84✔
794
    }
84✔
795
    if (d_network.sin6.sin6_family == AF_INET6) {
30!
796
      uint8_t bytes = d_bits / 8;
30✔
797
      uint8_t index = 0;
30✔
798
      // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
799
      const auto* lhs = reinterpret_cast<const uint8_t*>(&d_network.sin6.sin6_addr.s6_addr);
30✔
800
      const auto* rhs = reinterpret_cast<const uint8_t*>(&address->sin6.sin6_addr.s6_addr);
30✔
801
      // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
802

803
      // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
804
      for (index = 0; index < bytes; ++index) {
160✔
805
        if (lhs[index] != rhs[index]) {
140✔
806
          return false;
10✔
807
        }
10✔
808
      }
140✔
809
      // still here, now match remaining bits
810
      uint8_t bits = d_bits % 8;
20✔
811
      auto mask = static_cast<uint8_t>(~(0xFF >> bits));
20✔
812

813
      return ((lhs[index]) == (rhs[index] & mask));
20✔
814
      // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
815
    }
30✔
816
    return false;
×
817
  }
30✔
818

819
  //! If this ASCII IP address matches
820
  [[nodiscard]] bool match(const string& arg) const
821
  {
35✔
822
    ComboAddress address = makeComboAddress(arg);
35✔
823
    return match(&address);
35✔
824
  }
35✔
825

826
  //! If this IP address in native format matches
827
  [[nodiscard]] bool match4(uint32_t arg) const
828
  {
84✔
829
    return (arg & d_mask) == (ntohl(d_network.sin4.sin_addr.s_addr));
84✔
830
  }
84✔
831

832
  [[nodiscard]] string toString() const
833
  {
3,102✔
834
    return d_network.toStringNoInterface() + "/" + std::to_string((unsigned int)d_bits);
3,102✔
835
  }
3,102✔
836

837
  [[nodiscard]] string toStringNoMask() const
838
  {
1,501✔
839
    return d_network.toStringNoInterface();
1,501✔
840
  }
1,501✔
841

842
  [[nodiscard]] string toByteString() const
843
  {
88✔
844
    ostringstream tmp;
88✔
845

846
    tmp << (d_network.isIPv4() ? "\x04" : "\x06")
88✔
847
        << d_network.toByteString()
88✔
848
        << getBits();
88✔
849

850
    return tmp.str();
88✔
851
  }
88✔
852

853
  [[nodiscard]] const ComboAddress& getNetwork() const
854
  {
35,065,409✔
855
    return d_network;
35,065,409✔
856
  }
35,065,409✔
857

858
  [[nodiscard]] const ComboAddress& getMaskedNetwork() const
859
  {
34,960,620✔
860
    return getNetwork();
34,960,620✔
861
  }
34,960,620✔
862

863
  [[nodiscard]] uint8_t getBits() const
864
  {
93,852,486✔
865
    return d_bits;
93,852,486✔
866
  }
93,852,486✔
867

868
  [[nodiscard]] bool isIPv6() const
869
  {
23,364,284✔
870
    return d_network.sin6.sin6_family == AF_INET6;
23,364,284✔
871
  }
23,364,284✔
872

873
  [[nodiscard]] bool isIPv4() const
874
  {
40,686,614✔
875
    return d_network.sin4.sin_family == AF_INET;
40,686,614✔
876
  }
40,686,614✔
877

878
  bool operator<(const Netmask& rhs) const
879
  {
1,607,079✔
880
    if (empty() && !rhs.empty()) {
1,607,079✔
881
      return false;
6✔
882
    }
6✔
883
    if (!empty() && rhs.empty()) {
1,607,073✔
884
      return true;
3,180✔
885
    }
3,180✔
886
    if (d_bits > rhs.d_bits) {
1,603,893✔
887
      return true;
297,748✔
888
    }
297,748✔
889
    if (d_bits < rhs.d_bits) {
1,306,145✔
890
      return false;
154,892✔
891
    }
154,892✔
892

893
    return d_network < rhs.d_network;
1,151,253✔
894
  }
1,306,145✔
895

896
  bool operator>(const Netmask& rhs) const
897
  {
30✔
898
    return rhs.operator<(*this);
30✔
899
  }
30✔
900

901
  bool operator==(const Netmask& rhs) const
902
  {
32,081,429✔
903
    return std::tie(d_network, d_bits) == std::tie(rhs.d_network, rhs.d_bits);
32,081,429✔
904
  }
32,081,429✔
905

906
  bool operator!=(const Netmask& rhs) const
907
  {
56✔
908
    return !operator==(rhs);
56✔
909
  }
56✔
910

911
  [[nodiscard]] bool empty() const
912
  {
6,376,336✔
913
    return d_network.sin4.sin_family == 0;
6,376,336✔
914
  }
6,376,336✔
915

916
  //! Get normalized version of the netmask. This means that all address bits below the network bits are zero.
917
  [[nodiscard]] Netmask getNormalized() const
918
  {
34,913,031✔
919
    return {getMaskedNetwork(), d_bits};
34,913,031✔
920
  }
34,913,031✔
921
  //! Get Netmask for super network of this one (i.e. with fewer network bits)
922
  [[nodiscard]] Netmask getSuper(uint8_t bits) const
923
  {
701,498✔
924
    return {d_network, std::min(d_bits, bits)};
701,498✔
925
  }
701,498✔
926

927
  //! Get the total number of address bits for this netmask (either 32 or 128 depending on IP version)
928
  [[nodiscard]] uint8_t getFullBits() const
929
  {
1,480,515✔
930
    return d_network.getBits();
1,480,515✔
931
  }
1,480,515✔
932

933
  /** Get the value of the bit at the provided bit index. When the index >= 0,
934
      the index is relative to the LSB starting at index zero. When the index < 0,
935
      the index is relative to the MSB starting at index -1 and counting down.
936
      When the index points outside the network bits, it always yields zero.
937
   */
938
  [[nodiscard]] bool getBit(int bit) const
939
  {
132,677,055✔
940
    if (bit < -d_bits) {
132,677,055!
941
      return false;
×
942
    }
×
943
    if (bit >= 0) {
132,677,055!
944
      if (isIPv4()) {
×
945
        if (bit >= 32 || bit < (32 - d_bits)) {
×
946
          return false;
×
947
        }
×
948
      }
×
949
      if (isIPv6()) {
×
950
        if (bit >= 128 || bit < (128 - d_bits)) {
×
951
          return false;
×
952
        }
×
953
      }
×
954
    }
×
955
    return d_network.getBit(bit);
132,677,055✔
956
  }
132,677,055✔
957

958
  struct Hash
959
  {
960
    size_t operator()(const Netmask& netmask) const
961
    {
×
962
      return burtle(&netmask.d_bits, 1, ComboAddress::addressOnlyHash()(netmask.d_network));
×
963
    }
×
964
  };
965

966
private:
967
  ComboAddress d_network;
968
  uint32_t d_mask{0};
969
  uint8_t d_bits{0};
970
};
971

972
namespace std
973
{
974
template <>
975
struct hash<Netmask>
976
{
977
  auto operator()(const Netmask& netmask) const
978
  {
×
979
    return Netmask::Hash{}(netmask);
×
980
  }
×
981
};
982
}
983

984
/** Binary tree map implementation with <Netmask,T> pair.
985
 *
986
 * This is an binary tree implementation for storing attributes for IPv4 and IPv6 prefixes.
987
 * The most simple use case is simple NetmaskTree<bool> used by NetmaskGroup, which only
988
 * wants to know if given IP address is matched in the prefixes stored.
989
 *
990
 * This element is useful for anything that needs to *STORE* prefixes, and *MATCH* IP addresses
991
 * to a *LIST* of *PREFIXES*. Not the other way round.
992
 *
993
 * You can store IPv4 and IPv6 addresses to same tree, separate payload storage is kept per AFI.
994
 * Network prefixes (Netmasks) are always recorded in normalized fashion, meaning that only
995
 * the network bits are set. This is what is returned in the insert() and lookup() return
996
 * values.
997
 *
998
 * Use swap if you need to move the tree to another NetmaskTree instance, it is WAY faster
999
 * than using copy ctor or assignment operator, since it moves the nodes and tree root to
1000
 * new home instead of actually recreating the tree.
1001
 *
1002
 * Please see NetmaskGroup for example of simple use case. Other usecases can be found
1003
 * from GeoIPBackend and Sortlist, and from dnsdist.
1004
 */
1005
template <typename T, class K = Netmask>
1006
class NetmaskTree
1007
{
1008
public:
1009
  class Iterator;
1010

1011
  using key_type = K;
1012
  using value_type = T;
1013
  using node_type = std::pair<const key_type, value_type>;
1014
  using size_type = size_t;
1015
  using iterator = class Iterator;
1016

1017
private:
1018
  /** Single node in tree, internal use only.
1019
   */
1020
  class TreeNode : boost::noncopyable
1021
  {
1022
  public:
1023
    explicit TreeNode() noexcept :
1024
      parent(nullptr), node(), assigned(false), d_bits(0)
42,423✔
1025
    {
65,132✔
1026
    }
65,132✔
1027
    explicit TreeNode(const key_type& key) :
1028
      parent(nullptr), node({key.getNormalized(), value_type()}), assigned(false), d_bits(key.getFullBits())
613,253✔
1029
    {
1,481,633✔
1030
    }
1,481,633✔
1031

1032
    //<! Makes a left leaf node with specified key.
1033
    TreeNode* make_left(const key_type& key)
1034
    {
2,594✔
1035
      d_bits = node.first.getBits();
2,594✔
1036
      left = make_unique<TreeNode>(key);
2,594✔
1037
      left->parent = this;
2,594✔
1038
      return left.get();
2,594✔
1039
    }
2,594✔
1040

1041
    //<! Makes a right leaf node with specified key.
1042
    TreeNode* make_right(const key_type& key)
1043
    {
24,996✔
1044
      d_bits = node.first.getBits();
24,996✔
1045
      right = make_unique<TreeNode>(key);
24,996✔
1046
      right->parent = this;
24,996✔
1047
      return right.get();
24,996✔
1048
    }
24,996✔
1049

1050
    //<! Splits branch at indicated bit position by inserting key
1051
    TreeNode* split(const key_type& key, int bits)
1052
    {
5,302✔
1053
      if (parent == nullptr) {
5,302!
1054
        // not to be called on the root node
1055
        throw std::logic_error(
×
1056
          "NetmaskTree::TreeNode::split(): must not be called on root node");
×
1057
      }
×
1058

1059
      // determine reference from parent
1060
      unique_ptr<TreeNode>& parent_ref = (parent->left.get() == this ? parent->left : parent->right);
5,302!
1061
      if (parent_ref.get() != this) {
5,302!
1062
        throw std::logic_error(
×
1063
          "NetmaskTree::TreeNode::split(): parent node reference is invalid");
×
1064
      }
×
1065

1066
      // create new tree node for the new key and
1067
      // attach the new node under our former parent
1068
      auto new_intermediate_node = make_unique<TreeNode>(key);
5,302✔
1069
      new_intermediate_node->d_bits = bits;
5,302✔
1070
      new_intermediate_node->parent = parent;
5,302✔
1071
      auto* new_intermediate_node_raw = new_intermediate_node.get();
5,302✔
1072

1073
      // hereafter new_intermediate points to "this"
1074
      // ie the child of the new intermediate node
1075
      std::swap(parent_ref, new_intermediate_node);
5,302✔
1076
      // and we now assign this to current_node so
1077
      // it's clear it no longer refers to the new
1078
      // intermediate node
1079
      std::unique_ptr<TreeNode> current_node = std::move(new_intermediate_node);
5,302✔
1080

1081
      // attach "this" node below the new node
1082
      // (left or right depending on bit)
1083
      // technically the raw pointer escapes the duration of the
1084
      // unique pointer, but just below we store the unique pointer
1085
      // in the parent, so it lives as long as necessary
1086
      // coverity[escape]
1087
      current_node->parent = new_intermediate_node_raw;
5,302✔
1088
      if (current_node->node.first.getBit(-1 - bits)) {
5,302!
1089
        new_intermediate_node_raw->right = std::move(current_node);
188✔
1090
      }
188✔
1091
      else {
5,114✔
1092
        new_intermediate_node_raw->left = std::move(current_node);
5,114✔
1093
      }
5,114✔
1094

1095
      return new_intermediate_node_raw;
5,302✔
1096
    }
5,302✔
1097

1098
    //<! Forks branch for new key at indicated bit position
1099
    TreeNode* fork(const key_type& key, int bits)
1100
    {
702,008✔
1101
      if (parent == nullptr) {
702,008!
1102
        // not to be called on the root node
1103
        throw std::logic_error(
×
1104
          "NetmaskTree::TreeNode::fork(): must not be called on root node");
×
1105
      }
×
1106

1107
      // determine reference from parent
1108
      unique_ptr<TreeNode>& parent_ref = (parent->left.get() == this ? parent->left : parent->right);
702,008!
1109
      if (parent_ref.get() != this) {
702,008!
1110
        throw std::logic_error(
×
1111
          "NetmaskTree::TreeNode::fork(): parent node reference is invalid");
×
1112
      }
×
1113

1114
      // create new tree node for the branch point
1115

1116
      // the current node will now be a child of the new branch node
1117
      // (hereafter new_child1 points to "this")
1118
      unique_ptr<TreeNode> new_child1 = std::move(parent_ref);
702,008✔
1119
      // attach the branch node under our former parent
1120
      parent_ref = make_unique<TreeNode>(node.first.getSuper(bits));
702,008✔
1121
      auto* branch_node = parent_ref.get();
702,008✔
1122
      branch_node->d_bits = bits;
702,008✔
1123
      branch_node->parent = parent;
702,008✔
1124

1125
      // create second new leaf node for the new key
1126
      unique_ptr<TreeNode> new_child2 = make_unique<TreeNode>(key);
702,008✔
1127
      TreeNode* new_node = new_child2.get();
702,008✔
1128

1129
      // attach the new child nodes below the branch node
1130
      // (left or right depending on bit)
1131
      new_child1->parent = branch_node;
702,008✔
1132
      new_child2->parent = branch_node;
702,008✔
1133
      if (new_child1->node.first.getBit(-1 - bits)) {
702,008✔
1134
        branch_node->right = std::move(new_child1);
4,308✔
1135
        branch_node->left = std::move(new_child2);
4,308✔
1136
      }
4,308✔
1137
      else {
697,700✔
1138
        branch_node->right = std::move(new_child2);
697,700✔
1139
        branch_node->left = std::move(new_child1);
697,700✔
1140
      }
697,700✔
1141
      // now we have attached the new unique pointers to the tree:
1142
      // - branch_node is below its parent
1143
      // - new_child1 (ourselves) is below branch_node
1144
      // - new_child2, the new leaf node, is below branch_node as well
1145

1146
      return new_node;
702,008✔
1147
    }
702,008✔
1148

1149
    //<! Traverse left branch depth-first
1150
    TreeNode* traverse_l()
1151
    {
211,167✔
1152
      TreeNode* tnode = this;
211,167✔
1153

1154
      while (tnode->left) {
393,936✔
1155
        tnode = tnode->left.get();
182,769✔
1156
      }
182,769✔
1157
      return tnode;
211,167✔
1158
    }
211,167✔
1159

1160
    //<! Traverse tree depth-first and in-order (L-N-R)
1161
    TreeNode* traverse_lnr()
1162
    {
397,237✔
1163
      TreeNode* tnode = this;
397,237✔
1164

1165
      // precondition: descended left as deep as possible
1166
      if (tnode->right) {
397,237✔
1167
        // descend right
1168
        tnode = tnode->right.get();
174,175✔
1169
        // descend left as deep as possible and return next node
1170
        return tnode->traverse_l();
174,175✔
1171
      }
174,175✔
1172

1173
      // ascend to parent
1174
      while (tnode->parent != nullptr) {
397,237✔
1175
        TreeNode* prev_child = tnode;
359,043✔
1176
        tnode = tnode->parent;
359,043✔
1177

1178
        // return this node, but only when we come from the left child branch
1179
        if (tnode->left && tnode->left.get() == prev_child) {
359,043!
1180
          return tnode;
184,868✔
1181
        }
184,868✔
1182
      }
359,043✔
1183
      return nullptr;
38,194✔
1184
    }
223,062✔
1185

1186
    //<! Traverse only assigned nodes
1187
    TreeNode* traverse_lnr_assigned()
1188
    {
145,072✔
1189
      TreeNode* tnode = traverse_lnr();
145,072✔
1190

1191
      while (tnode != nullptr && !tnode->assigned) {
254,849✔
1192
        tnode = tnode->traverse_lnr();
109,777✔
1193
      }
109,777✔
1194
      return tnode;
145,072✔
1195
    }
145,072✔
1196

1197
    unique_ptr<TreeNode> left;
1198
    unique_ptr<TreeNode> right;
1199
    TreeNode* parent;
1200

1201
    node_type node;
1202
    bool assigned; //<! Whether this node is assigned-to by the application
1203

1204
    int d_bits; //<! How many bits have been used so far
1205
  };
1206

1207
  void cleanup_tree(TreeNode* node)
1208
  {
47,278✔
1209
    // only cleanup this node if it has no children and node not assigned
1210
    if (!(node->left || node->right || node->assigned)) {
47,278!
1211
      // get parent node ptr
1212
      TreeNode* pparent = node->parent;
24,681✔
1213
      // delete this node
1214
      if (pparent) {
24,681✔
1215
        if (pparent->left.get() == node) {
24,654✔
1216
          pparent->left.reset();
3,389✔
1217
        }
3,389✔
1218
        else {
21,265✔
1219
          pparent->right.reset();
21,265✔
1220
        }
21,265✔
1221
        // now recurse up to the parent
1222
        cleanup_tree(pparent);
24,654✔
1223
      }
24,654✔
1224
    }
24,681✔
1225
  }
47,278✔
1226

1227
  void copyTree(const NetmaskTree& rhs)
1228
  {
36,992✔
1229
    try {
36,992✔
1230
      TreeNode* node = rhs.d_root.get();
36,992✔
1231
      if (node != nullptr) {
36,992!
1232
        node = node->traverse_l();
36,992✔
1233
      }
36,992✔
1234
      while (node != nullptr) {
179,380✔
1235
        if (node->assigned) {
142,388✔
1236
          insert(node->node.first).second = node->node.second;
73,455✔
1237
        }
73,455✔
1238
        node = node->traverse_lnr();
142,388✔
1239
      }
142,388✔
1240
    }
36,992✔
1241
    catch (const NetmaskException&) {
36,992✔
1242
      abort();
×
1243
    }
×
1244
    catch (const std::logic_error&) {
36,992✔
1245
      abort();
×
1246
    }
×
1247
  }
36,992✔
1248

1249
public:
1250
  class Iterator
1251
  {
1252
  public:
1253
    using value_type = node_type;
1254
    using reference = node_type&;
1255
    using pointer = node_type*;
1256
    using iterator_category = std::forward_iterator_tag;
1257
    using difference_type = size_type;
1258

1259
  private:
1260
    friend class NetmaskTree;
1261

1262
    const NetmaskTree* d_tree;
1263
    TreeNode* d_node;
1264

1265
    Iterator(const NetmaskTree* tree, TreeNode* node) :
1266
      d_tree(tree), d_node(node)
2,434✔
1267
    {
2,794✔
1268
    }
2,794✔
1269

1270
  public:
1271
    Iterator() :
1272
      d_tree(nullptr), d_node(nullptr) {}
×
1273

1274
    Iterator& operator++() // prefix
1275
    {
141,723✔
1276
      if (d_node == nullptr) {
141,723!
1277
        throw std::logic_error(
×
1278
          "NetmaskTree::Iterator::operator++: iterator is invalid");
×
1279
      }
×
1280
      d_node = d_node->traverse_lnr_assigned();
141,723✔
1281
      return *this;
141,723✔
1282
    }
141,723✔
1283
    Iterator operator++(int) // postfix
1284
    {
×
1285
      Iterator tmp(*this);
×
1286
      operator++();
×
1287
      return tmp;
×
1288
    }
×
1289

1290
    reference operator*()
1291
    {
78,297✔
1292
      if (d_node == nullptr) {
78,297!
1293
        throw std::logic_error(
×
1294
          "NetmaskTree::Iterator::operator*: iterator is invalid");
×
1295
      }
×
1296
      return d_node->node;
78,297✔
1297
    }
78,297✔
1298

1299
    pointer operator->()
1300
    {
332✔
1301
      if (d_node == nullptr) {
332!
1302
        throw std::logic_error(
×
1303
          "NetmaskTree::Iterator::operator->: iterator is invalid");
×
1304
      }
×
1305
      return &d_node->node;
332✔
1306
    }
332✔
1307

1308
    bool operator==(const Iterator& rhs)
1309
    {
143,120✔
1310
      return (d_tree == rhs.d_tree && d_node == rhs.d_node);
143,120!
1311
    }
143,120✔
1312
    bool operator!=(const Iterator& rhs)
1313
    {
143,120✔
1314
      return !(*this == rhs);
143,120✔
1315
    }
143,120✔
1316
  };
1317

1318
  NetmaskTree() noexcept :
1319
    d_root(new TreeNode()), d_left(nullptr)
7,716✔
1320
  {
25,224✔
1321
  }
25,224✔
1322

1323
  NetmaskTree(const NetmaskTree& rhs) :
1324
    d_root(new TreeNode()), d_left(nullptr)
34,500✔
1325
  {
36,973✔
1326
    copyTree(rhs);
36,973✔
1327
  }
36,973✔
1328

1329
  ~NetmaskTree() = default;
80,407✔
1330

1331
  NetmaskTree& operator=(const NetmaskTree& rhs)
1332
  {
19✔
1333
    if (this != &rhs) {
19!
1334
      clear();
19✔
1335
      copyTree(rhs);
19✔
1336
    }
19✔
1337
    return *this;
19✔
1338
  }
19✔
1339

1340
  NetmaskTree(NetmaskTree&&) noexcept = default;
35,073✔
1341
  NetmaskTree& operator=(NetmaskTree&&) noexcept = default;
3,573✔
1342

1343
  [[nodiscard]] iterator begin() const
1344
  {
1,342✔
1345
    return Iterator(this, d_left);
1,342✔
1346
  }
1,342✔
1347
  [[nodiscard]] iterator end() const
1348
  {
1,342✔
1349
    return Iterator(this, nullptr);
1,342✔
1350
  }
1,342✔
1351
  iterator begin()
1352
  {
55✔
1353
    return Iterator(this, d_left);
55✔
1354
  }
55✔
1355
  iterator end()
1356
  {
55✔
1357
    return Iterator(this, nullptr);
55✔
1358
  }
55✔
1359

1360
  node_type& insert(const string& mask)
1361
  {
22✔
1362
    return insert(key_type(mask));
22✔
1363
  }
22✔
1364

1365
  //<! Creates new value-pair in tree and returns it.
1366
  node_type& insert(const key_type& key)
1367
  {
780,462✔
1368
    TreeNode* node{};
780,462✔
1369
    bool is_left = true;
780,462✔
1370

1371
    // we turn left on IPv4 and right on IPv6
1372
    if (key.isIPv4()) {
780,462✔
1373
      node = d_root->left.get();
402,777✔
1374
      if (node == nullptr) {
402,777✔
1375

1376
        d_root->left = make_unique<TreeNode>(key);
26,650✔
1377
        node = d_root->left.get();
26,650✔
1378
        node->assigned = true;
26,650✔
1379
        node->parent = d_root.get();
26,650✔
1380
        d_size++;
26,650✔
1381
        d_left = node;
26,650✔
1382
        return node->node;
26,650✔
1383
      }
26,650✔
1384
    }
402,777✔
1385
    else if (key.isIPv6()) {
377,685!
1386
      node = d_root->right.get();
377,685✔
1387
      if (node == nullptr) {
377,685!
1388

1389
        d_root->right = make_unique<TreeNode>(key);
18,075✔
1390
        node = d_root->right.get();
18,075✔
1391
        node->assigned = true;
18,075✔
1392
        node->parent = d_root.get();
18,075✔
1393
        d_size++;
18,075✔
1394
        if (!d_root->left) {
18,075!
1395
          d_left = node;
25✔
1396
        }
25✔
1397
        return node->node;
18,075✔
1398
      }
18,075✔
1399
      if (d_root->left) {
359,610!
1400
        is_left = false;
359,598✔
1401
      }
359,598✔
1402
    }
359,610✔
1403
    else {
×
1404
      throw NetmaskException("invalid address family");
×
1405
    }
×
1406

1407
    // we turn left on 0 and right on 1
1408
    int bits = 0;
735,737✔
1409
    for (; bits < key.getBits(); bits++) {
32,652,633!
1410
      bool vall = key.getBit(-1 - bits);
32,646,494✔
1411

1412
      if (bits >= node->d_bits) {
32,646,494!
1413
        // the end of the current node is reached; continue with the next
1414
        if (vall) {
5,001,706!
1415
          if (node->left || node->assigned) {
4,847,019!
1416
            is_left = false;
4,847,019✔
1417
          }
4,847,019✔
1418
          if (!node->right) {
4,847,019!
1419
            // the right branch doesn't exist yet; attach our key here
1420
            node = node->make_right(key);
1,879✔
1421
            break;
1,879✔
1422
          }
1,879✔
1423
          node = node->right.get();
4,845,140✔
1424
        }
4,845,140✔
1425
        else {
154,687✔
1426
          if (!node->left) {
154,687!
1427
            // the left branch doesn't exist yet; attach our key here
1428
            node = node->make_left(key);
56✔
1429
            break;
56✔
1430
          }
56✔
1431
          node = node->left.get();
154,631✔
1432
        }
154,631✔
1433
        continue;
4,999,771✔
1434
      }
5,001,706✔
1435
      if (bits >= node->node.first.getBits()) {
27,644,788!
1436
        // the matching branch ends here, yet the key netmask has more bits; add a
1437
        // child node below the existing branch leaf.
1438
        if (vall) {
25,655!
1439
          if (node->assigned) {
23,117!
1440
            is_left = false;
23,117✔
1441
          }
23,117✔
1442
          node = node->make_right(key);
23,117✔
1443
        }
23,117✔
1444
        else {
2,538✔
1445
          node = node->make_left(key);
2,538✔
1446
        }
2,538✔
1447
        break;
25,655✔
1448
      }
25,655✔
1449
      bool valr = node->node.first.getBit(-1 - bits);
27,619,133✔
1450
      if (vall != valr) {
27,619,133!
1451
        if (vall) {
702,008✔
1452
          is_left = false;
697,700✔
1453
        }
697,700✔
1454
        // the branch matches just upto this point, yet continues in a different
1455
        // direction; fork the branch.
1456
        node = node->fork(key, bits);
702,008✔
1457
        break;
702,008✔
1458
      }
702,008✔
1459
    }
27,619,133✔
1460

1461
    if (node->node.first.getBits() > key.getBits()) {
735,737!
1462
      // key is a super-network of the matching node; split the branch and
1463
      // insert a node for the key above the matching node.
1464
      node = node->split(key, key.getBits());
5,302✔
1465
    }
5,302✔
1466

1467
    if (node->left) {
735,737!
1468
      is_left = false;
5,864✔
1469
    }
5,864✔
1470

1471
    node_type& value = node->node;
735,737✔
1472

1473
    if (!node->assigned) {
735,737!
1474
      // only increment size if not assigned before
1475
      d_size++;
735,185✔
1476
      // update the pointer to the left-most tree node
1477
      if (is_left) {
735,185!
1478
        d_left = node;
829✔
1479
      }
829✔
1480
      node->assigned = true;
735,185✔
1481
    }
735,185✔
1482
    else {
552✔
1483
      // tree node exists for this value
1484
      if (is_left && d_left != node) {
552!
1485
        throw std::logic_error(
×
1486
          "NetmaskTree::insert(): lost track of left-most node in tree");
×
1487
      }
×
1488
    }
552✔
1489

1490
    return value;
735,737✔
1491
  }
735,737✔
1492

1493
  //<! Creates or updates value
1494
  void insert_or_assign(const key_type& mask, const value_type& value)
1495
  {
69✔
1496
    insert(mask).second = value;
69✔
1497
  }
69✔
1498

1499
  void insert_or_assign(const string& mask, const value_type& value)
1500
  {
×
1501
    insert(key_type(mask)).second = value;
×
1502
  }
×
1503

1504
  //<! check if given key is present in TreeMap
1505
  [[nodiscard]] bool has_key(const key_type& key) const
1506
  {
80✔
1507
    const node_type* ptr = lookup(key);
80✔
1508
    return ptr && ptr->first == key;
80!
1509
  }
80✔
1510

1511
  //<! Returns "best match" for key_type, which might not be value
1512
  [[nodiscard]] node_type* lookup(const key_type& value) const
1513
  {
332,573✔
1514
    uint8_t max_bits = value.getBits();
332,573✔
1515
    return lookupImpl(value, max_bits);
332,573✔
1516
  }
332,573✔
1517

1518
  //<! Perform best match lookup for value, using at most max_bits
1519
  [[nodiscard]] node_type* lookup(const ComboAddress& value, int max_bits = 128) const
1520
  {
1,650,407✔
1521
    uint8_t addr_bits = value.getBits();
1,650,407✔
1522
    if (max_bits < 0 || max_bits > addr_bits) {
1,650,416!
1523
      max_bits = addr_bits;
993,147✔
1524
    }
993,147✔
1525

1526
    return lookupImpl(key_type(value, max_bits), max_bits);
1,650,407✔
1527
  }
1,650,407✔
1528

1529
  //<! Removes key from TreeMap.
1530
  void erase(const key_type& key)
1531
  {
22,631✔
1532
    TreeNode* node = nullptr;
22,631✔
1533

1534
    if (key.isIPv4()) {
22,631✔
1535
      node = d_root->left.get();
22,596✔
1536
    }
22,596✔
1537
    else if (key.isIPv6()) {
35!
1538
      node = d_root->right.get();
35✔
1539
    }
35✔
1540
    else {
×
1541
      throw NetmaskException("invalid address family");
×
1542
    }
×
1543
    // no tree, no value
1544
    if (node == nullptr) {
22,631!
1545
      return;
×
1546
    }
×
1547
    int bits = 0;
22,631✔
1548
    for (; node && bits < key.getBits(); bits++) {
574,292!
1549
      bool vall = key.getBit(-1 - bits);
551,666✔
1550
      if (bits >= node->d_bits) {
551,666!
1551
        // the end of the current node is reached; continue with the next
1552
        if (vall) {
356,718✔
1553
          node = node->right.get();
244,732✔
1554
        }
244,732✔
1555
        else {
111,986✔
1556
          node = node->left.get();
111,986✔
1557
        }
111,986✔
1558
        continue;
356,718✔
1559
      }
356,718✔
1560
      if (bits >= node->node.first.getBits()) {
194,948!
1561
        // the matching branch ends here
1562
        if (key.getBits() != node->node.first.getBits()) {
×
1563
          node = nullptr;
×
1564
        }
×
1565
        break;
×
1566
      }
×
1567
      bool valr = node->node.first.getBit(-1 - bits);
194,948✔
1568
      if (vall != valr) {
194,948✔
1569
        // the branch matches just upto this point, yet continues in a different
1570
        // direction
1571
        node = nullptr;
5✔
1572
        break;
5✔
1573
      }
5✔
1574
    }
194,948✔
1575
    if (node) {
22,631✔
1576
      if (d_size == 0) {
22,624!
1577
        throw std::logic_error(
×
1578
          "NetmaskTree::erase(): size of tree is zero before erase");
×
1579
      }
×
1580
      d_size--;
22,624✔
1581
      node->assigned = false;
22,624✔
1582
      node->node.second = value_type();
22,624✔
1583

1584
      if (node == d_left) {
22,624!
1585
        d_left = d_left->traverse_lnr_assigned();
3,349✔
1586
      }
3,349✔
1587
      cleanup_tree(node);
22,624✔
1588
    }
22,624✔
1589
  }
22,631✔
1590

1591
  void erase(const string& key)
1592
  {
15✔
1593
    erase(key_type(key));
15✔
1594
  }
15✔
1595

1596
  //<! checks whether the container is empty.
1597
  [[nodiscard]] bool empty() const
1598
  {
130,279✔
1599
    return (d_size == 0);
130,279✔
1600
  }
130,279✔
1601

1602
  //<! returns the number of elements
1603
  [[nodiscard]] size_type size() const
1604
  {
700,889✔
1605
    return d_size;
700,889✔
1606
  }
700,889✔
1607

1608
  //<! See if given ComboAddress matches any prefix
1609
  [[nodiscard]] bool match(const ComboAddress& value) const
1610
  {
3,098✔
1611
    return (lookup(value) != nullptr);
3,098✔
1612
  }
3,098✔
1613

1614
  [[nodiscard]] bool match(const std::string& value) const
1615
  {
×
1616
    return match(ComboAddress(value));
×
1617
  }
×
1618

1619
  //<! Clean out the tree
1620
  void clear()
1621
  {
2,935✔
1622
    d_root = make_unique<TreeNode>();
2,935✔
1623
    d_left = nullptr;
2,935✔
1624
    d_size = 0;
2,935✔
1625
  }
2,935✔
1626

1627
  //<! swaps the contents with another NetmaskTree
1628
  void swap(NetmaskTree& rhs) noexcept
1629
  {
206✔
1630
    std::swap(d_root, rhs.d_root);
206✔
1631
    std::swap(d_left, rhs.d_left);
206✔
1632
    std::swap(d_size, rhs.d_size);
206✔
1633
  }
206✔
1634

1635
private:
1636
  [[nodiscard]] node_type* lookupImpl(const key_type& value, uint8_t max_bits) const
1637
  {
1,982,970✔
1638
    TreeNode* node = nullptr;
1,982,970✔
1639

1640
    if (value.isIPv4()) {
1,982,988!
1641
      node = d_root->left.get();
1,222,891✔
1642
    }
1,222,891✔
1643
    else if (value.isIPv6()) {
6,443,211,032!
1644
      node = d_root->right.get();
760,091✔
1645
    }
760,091✔
1646
    else {
6,442,450,941✔
1647
      throw NetmaskException("invalid address family");
6,442,450,941✔
1648
    }
6,442,450,941✔
1649
    if (node == nullptr) {
1,982,982✔
1650
      return nullptr;
252,236✔
1651
    }
252,236✔
1652

1653
    node_type* ret = nullptr;
1,730,746✔
1654

1655
    int bits = 0;
1,730,746✔
1656
    for (; bits < max_bits; bits++) {
57,594,064!
1657
      bool vall = value.getBit(-1 - bits);
57,127,557✔
1658
      if (bits >= node->d_bits) {
57,127,557✔
1659
        // the end of the current node is reached; continue with the next
1660
        // (we keep track of last assigned node)
1661
        if (node->assigned && bits == node->node.first.getBits()) {
12,753,696!
1662
          ret = &node->node;
1,134,139✔
1663
        }
1,134,139✔
1664
        if (vall) {
12,753,696✔
1665
          if (!node->right) {
5,716,545!
1666
            break;
19,267✔
1667
          }
19,267✔
1668
          node = node->right.get();
5,697,278✔
1669
        }
5,697,278✔
1670
        else {
7,037,151✔
1671
          if (!node->left) {
7,037,151!
1672
            break;
19,471✔
1673
          }
19,471✔
1674
          node = node->left.get();
7,017,680✔
1675
        }
7,017,680✔
1676
        continue;
12,714,958✔
1677
      }
12,753,696✔
1678
      if (bits >= node->node.first.getBits()) {
44,373,861!
1679
        // the matching branch ends here
1680
        break;
375,726✔
1681
      }
375,726✔
1682
      bool valr = node->node.first.getBit(-1 - bits);
43,998,135✔
1683
      if (vall != valr) {
43,998,135!
1684
        // the branch matches just upto this point, yet continues in a different
1685
        // direction
1686
        break;
849,780✔
1687
      }
849,780✔
1688
    }
43,998,135✔
1689
    // needed if we did not find one in loop
1690
    if (node->assigned && bits == node->node.first.getBits()) {
1,730,746!
1691
      ret = &node->node;
879,423✔
1692
    }
879,423✔
1693
    // this can be nullptr.
1694
    return ret;
1,730,746✔
1695
  }
1,982,982✔
1696

1697
  unique_ptr<TreeNode> d_root; //<! Root of our tree
1698
  TreeNode* d_left;
1699
  size_type d_size{0};
1700
};
1701

1702
/** This class represents a group of supplemental Netmask classes. An IP address matches
1703
    if it is matched by one or more of the Netmask objects within.
1704
*/
1705
class NetmaskGroup
1706
{
1707
public:
1708
  NetmaskGroup() noexcept = default;
12,377✔
1709

1710
  //! If this IP address is matched by any of the classes within
1711

1712
  bool match(const ComboAddress* address) const
1713
  {
212,715✔
1714
    const auto& ret = tree.lookup(*address);
212,715✔
1715
    if (ret != nullptr) {
212,715✔
1716
      return ret->second;
21,966✔
1717
    }
21,966✔
1718
    return false;
190,749✔
1719
  }
212,715✔
1720

1721
  [[nodiscard]] bool match(const ComboAddress& address) const
1722
  {
199,169✔
1723
    return match(&address);
199,169✔
1724
  }
199,169✔
1725

1726
  bool lookup(const ComboAddress* address, Netmask* nmp) const
1727
  {
×
1728
    const auto& ret = tree.lookup(*address);
×
1729
    if (ret != nullptr) {
×
1730
      if (nmp != nullptr) {
×
1731
        *nmp = ret->first;
×
1732
      }
×
1733
      return ret->second;
×
1734
    }
×
1735
    return false;
×
1736
  }
×
1737

1738
  bool lookup(const ComboAddress& address, Netmask* nmp) const
1739
  {
×
1740
    return lookup(&address, nmp);
×
1741
  }
×
1742

1743
  //! Add this string to the list of possible matches
1744
  void addMask(const string& address, bool positive = true)
1745
  {
15,689✔
1746
    if (!address.empty() && address[0] == '!') {
15,689!
1747
      addMask(Netmask(address.substr(1)), false);
1,389✔
1748
    }
1,389✔
1749
    else {
14,300✔
1750
      addMask(Netmask(address), positive);
14,300✔
1751
    }
14,300✔
1752
  }
15,689✔
1753

1754
  //! Add this Netmask to the list of possible matches
1755
  void addMask(const Netmask& netmask, bool positive = true)
1756
  {
15,444✔
1757
    tree.insert(netmask).second = positive;
15,444✔
1758
  }
15,444✔
1759

1760
  void addMasks(const NetmaskGroup& group, boost::optional<bool> positive)
1761
  {
2✔
1762
    for (const auto& entry : group.tree) {
2✔
1763
      addMask(entry.first, positive ? *positive : entry.second);
2!
1764
    }
2✔
1765
  }
2✔
1766

1767
  //! Delete this Netmask from the list of possible matches
1768
  void deleteMask(const Netmask& netmask)
1769
  {
×
1770
    tree.erase(netmask);
×
1771
  }
×
1772

1773
  void deleteMasks(const NetmaskGroup& group)
1774
  {
×
1775
    for (const auto& entry : group.tree) {
×
1776
      deleteMask(entry.first);
×
1777
    }
×
1778
  }
×
1779

1780
  void deleteMask(const std::string& address)
1781
  {
×
1782
    if (!address.empty()) {
×
1783
      deleteMask(Netmask(address));
×
1784
    }
×
1785
  }
×
1786

1787
  void clear()
1788
  {
1,614✔
1789
    tree.clear();
1,614✔
1790
  }
1,614✔
1791

1792
  [[nodiscard]] bool empty() const
1793
  {
128,796✔
1794
    return tree.empty();
128,796✔
1795
  }
128,796✔
1796

1797
  [[nodiscard]] size_t size() const
1798
  {
326✔
1799
    return tree.size();
326✔
1800
  }
326✔
1801

1802
  [[nodiscard]] string toString() const
1803
  {
33✔
1804
    ostringstream str;
33✔
1805
    for (auto iter = tree.begin(); iter != tree.end(); ++iter) {
199✔
1806
      if (iter != tree.begin()) {
166✔
1807
        str << ", ";
133✔
1808
      }
133✔
1809
      if (!(iter->second)) {
166✔
1810
        str << "!";
30✔
1811
      }
30✔
1812
      str << iter->first.toString();
166✔
1813
    }
166✔
1814
    return str.str();
33✔
1815
  }
33✔
1816

1817
  [[nodiscard]] std::vector<std::string> toStringVector() const
1818
  {
1,029✔
1819
    std::vector<std::string> out;
1,029✔
1820
    out.reserve(tree.size());
1,029✔
1821
    for (const auto& entry : tree) {
1,802✔
1822
      out.push_back((entry.second ? "" : "!") + entry.first.toString());
1,802!
1823
    }
1,802✔
1824
    return out;
1,029✔
1825
  }
1,029✔
1826

1827
  void toMasks(const string& ips)
1828
  {
1,798✔
1829
    vector<string> parts;
1,798✔
1830
    stringtok(parts, ips, ", \t");
1,798✔
1831

1832
    for (const auto& part : parts) {
2,705✔
1833
      addMask(part);
2,361✔
1834
    }
2,361✔
1835
  }
1,798✔
1836

1837
private:
1838
  NetmaskTree<bool> tree;
1839
};
1840

1841
struct SComboAddress
1842
{
1843
  SComboAddress(const ComboAddress& orig) :
1844
    ca(orig) {}
4,841✔
1845
  ComboAddress ca;
1846
  bool operator<(const SComboAddress& rhs) const
1847
  {
×
1848
    return ComboAddress::addressOnlyLessThan()(ca, rhs.ca);
×
1849
  }
×
1850
  operator const ComboAddress&() const
1851
  {
×
1852
    return ca;
×
1853
  }
×
1854
};
1855

1856
class NetworkError : public runtime_error
1857
{
1858
public:
1859
  NetworkError(const string& why = "Network Error") :
1860
    runtime_error(why.c_str())
8✔
1861
  {}
213✔
1862
  NetworkError(const char* why = "Network Error") :
1863
    runtime_error(why)
28✔
1864
  {}
28✔
1865
};
1866

1867
class AddressAndPortRange
1868
{
1869
public:
1870
  AddressAndPortRange() :
1871
    d_addrMask(0), d_portMask(0)
1,348✔
1872
  {
1,348✔
1873
    d_addr.sin4.sin_family = 0; // disable this doing anything useful
1,348✔
1874
    d_addr.sin4.sin_port = 0; // this guarantees d_network compares identical
1,348✔
1875
  }
1,348✔
1876

1877
  AddressAndPortRange(ComboAddress address, uint8_t addrMask, uint8_t portMask = 0) :
1878
    d_addr(address), d_addrMask(addrMask), d_portMask(portMask)
291,646✔
1879
  {
291,646✔
1880
    if (!d_addr.isIPv4()) {
291,646✔
1881
      d_portMask = 0;
66,523✔
1882
    }
66,523✔
1883

1884
    uint16_t port = d_addr.getPort();
291,646✔
1885
    if (d_portMask < 16) {
291,646✔
1886
      auto mask = static_cast<uint16_t>(~(0xFFFF >> d_portMask));
88,860✔
1887
      port = port & mask;
88,860✔
1888
    }
88,860✔
1889

1890
    if (d_addrMask < d_addr.getBits()) {
291,646✔
1891
      if (d_portMask > 0) {
2,170!
1892
        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) + ")");
×
1893
      }
×
1894
      d_addr = Netmask(d_addr, d_addrMask).getMaskedNetwork();
2,170✔
1895
    }
2,170✔
1896
    d_addr.setPort(port);
291,646✔
1897
  }
291,646✔
1898

1899
  [[nodiscard]] uint8_t getFullBits() const
1900
  {
60,337,656✔
1901
    return d_addr.getBits() + 16;
60,337,656✔
1902
  }
60,337,656✔
1903

1904
  [[nodiscard]] uint8_t getBits() const
1905
  {
15,656,748✔
1906
    if (d_addrMask < d_addr.getBits()) {
15,656,748✔
1907
      return d_addrMask;
34,300✔
1908
    }
34,300✔
1909

1910
    return d_addr.getBits() + d_portMask;
15,622,448✔
1911
  }
15,656,748✔
1912

1913
  /** Get the value of the bit at the provided bit index. When the index >= 0,
1914
      the index is relative to the LSB starting at index zero. When the index < 0,
1915
      the index is relative to the MSB starting at index -1 and counting down.
1916
  */
1917
  [[nodiscard]] bool getBit(int index) const
1918
  {
30,168,257✔
1919
    if (index >= getFullBits()) {
30,168,257!
1920
      return false;
×
1921
    }
×
1922
    if (index < 0) {
30,168,257!
1923
      index = getFullBits() + index;
30,168,257✔
1924
    }
30,168,257✔
1925

1926
    if (index < 16) {
30,168,257✔
1927
      /* we are into the port bits */
1928
      uint16_t port = d_addr.getPort();
688,812✔
1929
      return ((port & (1U << index)) != 0x0000);
688,812✔
1930
    }
688,812✔
1931

1932
    index -= 16;
29,479,445✔
1933

1934
    return d_addr.getBit(index);
29,479,445✔
1935
  }
30,168,257✔
1936

1937
  [[nodiscard]] bool isIPv4() const
1938
  {
269,906✔
1939
    return d_addr.isIPv4();
269,906✔
1940
  }
269,906✔
1941

1942
  [[nodiscard]] bool isIPv6() const
1943
  {
65,569✔
1944
    return d_addr.isIPv6();
65,569✔
1945
  }
65,569✔
1946

1947
  [[nodiscard]] AddressAndPortRange getNormalized() const
1948
  {
1,142✔
1949
    return {d_addr, d_addrMask, d_portMask};
1,142✔
1950
  }
1,142✔
1951

1952
  [[nodiscard]] AddressAndPortRange getSuper(uint8_t bits) const
1953
  {
510✔
1954
    if (bits <= d_addrMask) {
510!
1955
      return {d_addr, bits, 0};
510✔
1956
    }
510✔
1957
    if (bits <= d_addrMask + d_portMask) {
×
1958
      return {d_addr, d_addrMask, static_cast<uint8_t>(d_portMask - (bits - d_addrMask))};
×
1959
    }
×
1960

1961
    return {d_addr, d_addrMask, d_portMask};
×
1962
  }
×
1963

1964
  [[nodiscard]] const ComboAddress& getNetwork() const
1965
  {
348✔
1966
    return d_addr;
348✔
1967
  }
348✔
1968

1969
  [[nodiscard]] string toString() const
1970
  {
55✔
1971
    if (d_addrMask < d_addr.getBits() || d_portMask == 0) {
55✔
1972
      return d_addr.toStringNoInterface() + "/" + std::to_string(d_addrMask);
54✔
1973
    }
54✔
1974
    return d_addr.toStringNoInterface() + ":" + std::to_string(d_addr.getPort()) + "/" + std::to_string(d_portMask);
1✔
1975
  }
55✔
1976

1977
  [[nodiscard]] bool empty() const
1978
  {
×
1979
    return d_addr.sin4.sin_family == 0;
×
1980
  }
×
1981

1982
  bool operator==(const AddressAndPortRange& rhs) const
1983
  {
20,585✔
1984
    return std::tie(d_addr, d_addrMask, d_portMask) == std::tie(rhs.d_addr, rhs.d_addrMask, rhs.d_portMask);
20,585✔
1985
  }
20,585✔
1986

1987
  bool operator<(const AddressAndPortRange& rhs) const
1988
  {
×
1989
    if (empty() && !rhs.empty()) {
×
1990
      return false;
×
1991
    }
×
1992

1993
    if (!empty() && rhs.empty()) {
×
1994
      return true;
×
1995
    }
×
1996

1997
    if (d_addrMask > rhs.d_addrMask) {
×
1998
      return true;
×
1999
    }
×
2000

2001
    if (d_addrMask < rhs.d_addrMask) {
×
2002
      return false;
×
2003
    }
×
2004

2005
    if (d_addr < rhs.d_addr) {
×
2006
      return true;
×
2007
    }
×
2008

2009
    if (d_addr > rhs.d_addr) {
×
2010
      return false;
×
2011
    }
×
2012

2013
    if (d_portMask > rhs.d_portMask) {
×
2014
      return true;
×
2015
    }
×
2016

2017
    if (d_portMask < rhs.d_portMask) {
×
2018
      return false;
×
2019
    }
×
2020

2021
    return d_addr.getPort() < rhs.d_addr.getPort();
×
2022
  }
×
2023

2024
  bool operator>(const AddressAndPortRange& rhs) const
2025
  {
×
2026
    return rhs.operator<(*this);
×
2027
  }
×
2028

2029
  struct hash
2030
  {
2031
    uint32_t operator()(const AddressAndPortRange& apr) const
2032
    {
28,874✔
2033
      ComboAddress::addressOnlyHash hashOp;
28,874✔
2034
      uint16_t port = apr.d_addr.getPort();
28,874✔
2035
      /* it's fine to hash the whole address and port because the non-relevant parts have
2036
         been masked to 0 */
2037
      return burtle(reinterpret_cast<const unsigned char*>(&port), sizeof(port), hashOp(apr.d_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
28,874✔
2038
    }
28,874✔
2039
  };
2040

2041
private:
2042
  ComboAddress d_addr;
2043
  uint8_t d_addrMask;
2044
  /* only used for v4 addresses */
2045
  uint8_t d_portMask;
2046
};
2047

2048
int SSocket(int family, int type, int flags);
2049
int SConnect(int sockfd, bool fastopen, const ComboAddress& remote);
2050
/* tries to connect to remote for a maximum of timeout seconds.
2051
   sockfd should be set to non-blocking beforehand.
2052
   returns 0 on success (the socket is writable), throw a
2053
   runtime_error otherwise */
2054
int SConnectWithTimeout(int sockfd, bool fastopen, const ComboAddress& remote, const struct timeval& timeout);
2055
int SBind(int sockfd, const ComboAddress& local);
2056
int SAccept(int sockfd, ComboAddress& remote);
2057
int SListen(int sockfd, int limit);
2058
int SSetsockopt(int sockfd, int level, int opname, int value);
2059
void setSocketIgnorePMTU(int sockfd, int family);
2060
void setSocketForcePMTU(int sockfd, int family);
2061
bool setReusePort(int sockfd);
2062

2063
#if defined(IP_PKTINFO)
2064
#define GEN_IP_PKTINFO IP_PKTINFO
113✔
2065
#elif defined(IP_RECVDSTADDR)
2066
#define GEN_IP_PKTINFO IP_RECVDSTADDR
2067
#endif
2068

2069
bool IsAnyAddress(const ComboAddress& addr);
2070
bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destination);
2071
bool HarvestTimestamp(struct msghdr* msgh, struct timeval* timeval);
2072
void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr);
2073
int sendOnNBSocket(int fileDesc, const struct msghdr* msgh);
2074
size_t sendMsgWithOptions(int socketDesc, const void* buffer, size_t len, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int flags);
2075

2076
/* requires a non-blocking, connected TCP socket */
2077
bool isTCPSocketUsable(int sock);
2078

2079
extern template class NetmaskTree<bool>;
2080
ComboAddress parseIPAndPort(const std::string& input, uint16_t port);
2081

2082
std::set<std::string> getListOfNetworkInterfaces();
2083
std::vector<ComboAddress> getListOfAddressesOfNetworkInterface(const std::string& itf);
2084
std::vector<Netmask> getListOfRangesOfNetworkInterface(const std::string& itf);
2085

2086
/* These functions throw if the value was already set to a higher value,
2087
   or on error */
2088
void setSocketBuffer(int fileDesc, int optname, uint32_t size);
2089
void setSocketReceiveBuffer(int fileDesc, uint32_t size);
2090
void setSocketSendBuffer(int fileDesc, uint32_t size);
2091
uint32_t raiseSocketReceiveBufferToMax(int socket);
2092
uint32_t raiseSocketSendBufferToMax(int socket);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc