Coverage Report

Created: 2024-04-25 06:25

/src/pdns/pdns/dnsdistdist/misc.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#pragma once
23
#include <cinttypes>
24
#include <cstring>
25
#include <cstdio>
26
#include <regex.h>
27
#include <climits>
28
#include <type_traits>
29
30
#include <boost/algorithm/string.hpp>
31
32
#include "dns.hh"
33
#include <atomic>
34
#include <sys/time.h>
35
#include <sys/types.h>
36
#include <sys/socket.h>
37
#include <ctime>
38
#include <syslog.h>
39
#include <stdexcept>
40
#include <string>
41
#include <cctype>
42
#include <vector>
43
44
#include "namespaces.hh"
45
46
class DNSName;
47
48
// Do not change to "using TSIGHashEnum ..." until you know CodeQL does not choke on it
49
typedef enum { TSIG_MD5, TSIG_SHA1, TSIG_SHA224, TSIG_SHA256, TSIG_SHA384, TSIG_SHA512, TSIG_GSS } TSIGHashEnum;
50
namespace pdns
51
{
52
/**
53
 * \brief Retrieves the errno-based error message in a reentrant way.
54
 *
55
 * This internally handles the portability issues around using
56
 * `strerror_r` and returns a `std::string` that owns the error
57
 * message's contents.
58
 *
59
 * \param[in] errnum The errno value.
60
 *
61
 * \return The `std::string` error message.
62
 */
63
auto getMessageFromErrno(int errnum) -> std::string;
64
65
#if defined(HAVE_LIBCRYPTO)
66
namespace OpenSSL
67
{
68
  /**
69
   * \brief Throws a `std::runtime_error` with the current OpenSSL error.
70
   *
71
   * \param[in] errorMessage The message to attach in addition to the OpenSSL error.
72
   */
73
  [[nodiscard]] auto error(const std::string& errorMessage) -> std::runtime_error;
74
75
  /**
76
   * \brief Throws a `std::runtime_error` with a name and the current OpenSSL error.
77
   *
78
   * \param[in] componentName The name of the component to mark the error message with.
79
   * \param[in] errorMessage The message to attach in addition to the OpenSSL error.
80
   */
81
  [[nodiscard]] auto error(const std::string& componentName, const std::string& errorMessage) -> std::runtime_error;
82
}
83
#endif // HAVE_LIBCRYPTO
84
}
85
86
string nowTime();
87
string unquotify(const string &item);
88
string humanDuration(time_t passed);
89
bool stripDomainSuffix(string *qname, const string &domain);
90
void stripLine(string &line);
91
std::optional<string> getHostname();
92
std::string getCarbonHostName();
93
string urlEncode(const string &text);
94
int waitForData(int fileDesc, int seconds, int useconds = 0);
95
int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fd);
96
int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fd);
97
int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error = nullptr, bool* disconnected = nullptr);
98
bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum);
99
DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum);
100
101
int logFacilityToLOG(unsigned int facility);
102
103
template<typename Container>
104
void
105
stringtok (Container &container, string const &in,
106
           const char * const delimiters = " \t\n")
107
0
{
108
0
  const string::size_type len = in.length();
109
0
  string::size_type i = 0;
110
111
0
  while (i<len) {
112
    // eat leading whitespace
113
0
    i = in.find_first_not_of (delimiters, i);
114
0
    if (i == string::npos)
115
0
      return;   // nothing left but white space
116
117
    // find the end of the token
118
0
    string::size_type j = in.find_first_of (delimiters, i);
119
120
    // push token
121
0
    if (j == string::npos) {
122
0
      container.push_back (in.substr(i));
123
0
      return;
124
0
    } else
125
0
      container.push_back (in.substr(i, j-i));
126
127
    // set up for next loop
128
0
    i = j + 1;
129
0
  }
130
0
}
131
132
template<typename T> bool rfc1982LessThan(T a, T b)
133
{
134
  static_assert(std::is_unsigned_v<T>, "rfc1982LessThan only works for unsigned types");
135
  return std::make_signed_t<T>(a - b) < 0;
136
}
137
138
// fills container with ranges, so {posbegin,posend}
139
template <typename Container>
140
void
141
vstringtok (Container &container, string const &in,
142
           const char * const delimiters = " \t\n")
143
0
{
144
0
  const string::size_type len = in.length();
145
0
  string::size_type i = 0;
146
147
0
  while (i<len) {
148
    // eat leading whitespace
149
0
    i = in.find_first_not_of (delimiters, i);
150
0
    if (i == string::npos)
151
0
      return;   // nothing left but white space
152
153
    // find the end of the token
154
0
    string::size_type j = in.find_first_of (delimiters, i);
155
156
    // push token
157
0
    if (j == string::npos) {
158
0
      container.emplace_back(i, len);
159
0
      return;
160
0
    } else
161
0
      container.emplace_back(i, j);
162
163
    // set up for next loop
164
0
    i = j + 1;
165
0
  }
166
0
}
167
168
size_t writen2(int fd, const void *buf, size_t count);
169
0
inline size_t writen2(int fd, const std::string &s) { return writen2(fd, s.data(), s.size()); }
170
size_t readn2(int fd, void* buffer, size_t len);
171
size_t readn2WithTimeout(int fd, void* buffer, size_t len, const struct timeval& idleTimeout, const struct timeval& totalTimeout={0,0}, bool allowIncomplete=false);
172
size_t writen2WithTimeout(int fd, const void * buffer, size_t len, const struct timeval& timeout);
173
174
void toLowerInPlace(string& str);
175
const string toLower(const string &upper);
176
const string toLowerCanonic(const string &upper);
177
bool IpToU32(const string &str, uint32_t *ip);
178
string U32ToIP(uint32_t);
179
180
inline string stringerror(int err = errno)
181
0
{
182
0
  return pdns::getMessageFromErrno(err);
183
0
}
184
185
string bitFlip(const string &str);
186
187
void dropPrivs(int uid, int gid);
188
void cleanSlashes(string &str);
189
190
#if defined(_POSIX_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID)
191
/** CPUTime measurements */
192
class CPUTime
193
{
194
public:
195
  void start()
196
0
  {
197
0
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &d_start);
198
0
  }
199
  uint64_t ndiff()
200
0
  {
201
0
    struct timespec now;
202
0
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
203
0
    return 1000000000ULL*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec);
204
0
  }
205
private:
206
  struct timespec d_start;
207
};
208
#endif
209
210
/** The DTime class can be used for timing statistics with microsecond resolution.
211
On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */
212
class DTime
213
{
214
public:
215
  //!< Does not set the timer for you! Saves lots of gettimeofday() calls
216
  DTime() = default;
217
  DTime(const DTime &dt) = default;
218
  DTime & operator=(const DTime &dt) = default;
219
  inline time_t time() const;
220
  inline void set();  //!< Reset the timer
221
  inline int udiff(bool reset = true); //!< Return the number of microseconds since the timer was last set.
222
223
  int udiffNoReset() //!< Return the number of microseconds since the timer was last set.
224
0
  {
225
0
    return udiff(false);
226
0
  }
227
  void setTimeval(const struct timeval& tv)
228
0
  {
229
0
    d_set=tv;
230
0
  }
231
  struct timeval getTimeval() const
232
0
  {
233
0
    return d_set;
234
0
  }
235
private:
236
struct timeval d_set{0, 0};
237
};
238
239
inline time_t DTime::time() const
240
0
{
241
0
  return d_set.tv_sec;
242
0
}
243
244
inline void DTime::set()
245
0
{
246
0
  gettimeofday(&d_set, nullptr);
247
0
}
248
249
inline int DTime::udiff(bool reset)
250
0
{
251
0
  struct timeval now;
252
0
  gettimeofday(&now, nullptr);
253
0
254
0
  int ret=1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec);
255
0
256
0
  if (reset) {
257
0
    d_set = now;
258
0
  }
259
0
260
0
  return ret;
261
0
}
262
263
inline void toLowerInPlace(string& str)
264
0
{
265
0
  const size_t length = str.length();
266
0
  char c;
267
0
  for (size_t i = 0; i < length; ++i) {
268
0
    c = dns_tolower(str[i]);
269
0
    if (c != str[i]) {
270
0
      str[i] = c;
271
0
    }
272
0
  }
273
0
}
274
275
inline const string toLower(const string &upper)
276
0
{
277
0
  string reply(upper);
278
279
0
  toLowerInPlace(reply);
280
281
0
  return reply;
282
0
}
283
284
inline const string toLowerCanonic(const string &upper)
285
0
{
286
0
  string reply(upper);
287
0
  if(!upper.empty()) {
288
0
    unsigned int i, limit= ( unsigned int ) reply.length();
289
0
    unsigned char c;
290
0
    for(i = 0; i < limit ; i++) {
291
0
      c = dns_tolower(upper[i]);
292
0
      if(c != upper[i])
293
0
        reply[i] = c;
294
0
    }
295
0
    if(upper[i-1]=='.')
296
0
      reply.resize(i-1);
297
0
  }
298
0
299
0
  return reply;
300
0
}
301
302
303
304
// Make s uppercase:
305
inline string toUpper( const string& s )
306
0
{
307
0
        string r(s);
308
0
        for( unsigned int i = 0; i < s.length(); i++ ) {
309
0
          r[i] = dns_toupper(r[i]);
310
0
        }
311
0
        return r;
312
0
}
313
314
inline double getTime()
315
0
{
316
0
  struct timeval now;
317
0
  gettimeofday(&now,0);
318
0
319
0
  return now.tv_sec+now.tv_usec/1000000.0;
320
0
}
321
322
inline void unixDie(const string &why)
323
0
{
324
0
  throw runtime_error(why+": "+stringerror());
325
0
}
326
327
string makeHexDump(const string& str);
328
//! Convert the hexstring in to a byte string
329
string makeBytesFromHex(const string &in);
330
331
void normalizeTV(struct timeval& tv);
332
struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs);
333
struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs);
334
335
inline float makeFloat(const struct timeval& tv)
336
0
{
337
0
  return tv.tv_sec + tv.tv_usec/1000000.0f;
338
0
}
339
inline uint64_t uSec(const struct timeval& tv)
340
0
{
341
0
  return tv.tv_sec * 1000000 + tv.tv_usec;
342
0
}
343
344
inline bool operator<(const struct timeval& lhs, const struct timeval& rhs)
345
0
{
346
0
  return std::tie(lhs.tv_sec, lhs.tv_usec) < std::tie(rhs.tv_sec, rhs.tv_usec);
347
0
}
348
inline bool operator<=(const struct timeval& lhs, const struct timeval& rhs)
349
0
{
350
0
  return std::tie(lhs.tv_sec, lhs.tv_usec) <= std::tie(rhs.tv_sec, rhs.tv_usec);
351
0
}
352
353
inline bool operator<(const struct timespec& lhs, const struct timespec& rhs)
354
0
{
355
0
  return std::tie(lhs.tv_sec, lhs.tv_nsec) < std::tie(rhs.tv_sec, rhs.tv_nsec);
356
0
}
357
358
359
inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b)  __attribute__((pure));
360
inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b)
361
0
{
362
0
  const unsigned char *aPtr = (const unsigned char*)a.c_str(), *bPtr = (const unsigned char*)b.c_str();
363
0
  const unsigned char *aEptr = aPtr + a.length(), *bEptr = bPtr + b.length();
364
0
  while(aPtr != aEptr && bPtr != bEptr) {
365
0
    if ((*aPtr != *bPtr) && (dns_tolower(*aPtr) - dns_tolower(*bPtr)))
366
0
      return (dns_tolower(*aPtr) - dns_tolower(*bPtr)) < 0;
367
0
    aPtr++;
368
0
    bPtr++;
369
0
  }
370
0
  if(aPtr == aEptr && bPtr == bEptr) // strings are equal (in length)
371
0
    return false;
372
0
  return aPtr == aEptr; // true if first string was shorter
373
0
}
374
375
inline bool pdns_iequals(const std::string& a, const std::string& b) __attribute__((pure));
376
inline bool pdns_iequals(const std::string& a, const std::string& b)
377
0
{
378
0
  if (a.length() != b.length())
379
0
    return false;
380
381
0
  const char *aPtr = a.c_str(), *bPtr = b.c_str();
382
0
  const char *aEptr = aPtr + a.length();
383
0
  while(aPtr != aEptr) {
384
0
    if((*aPtr != *bPtr) && (dns_tolower(*aPtr) != dns_tolower(*bPtr)))
385
0
      return false;
386
0
    aPtr++;
387
0
    bPtr++;
388
0
  }
389
0
  return true;
390
0
}
391
392
inline bool pdns_iequals_ch(const char a, const char b) __attribute__((pure));
393
inline bool pdns_iequals_ch(const char a, const char b)
394
0
{
395
0
  if ((a != b) && (dns_tolower(a) != dns_tolower(b)))
396
0
    return false;
397
0
398
0
  return true;
399
0
}
400
401
402
typedef unsigned long AtomicCounterInner;
403
typedef std::atomic<AtomicCounterInner> AtomicCounter ;
404
405
// FIXME400 this should probably go?
406
struct CIStringCompare
407
{
408
  bool operator()(const string& a, const string& b) const
409
0
  {
410
0
    return pdns_ilexicographical_compare(a, b);
411
0
  }
412
};
413
414
struct CIStringComparePOSIX
415
{
416
   bool operator() (const std::string& lhs, const std::string& rhs) const
417
0
   {
418
0
      const std::locale &loc = std::locale("POSIX");
419
0
      auto lhsIter = lhs.begin();
420
0
      auto rhsIter = rhs.begin();
421
0
      while (lhsIter != lhs.end()) {
422
0
        if (rhsIter == rhs.end() || std::tolower(*rhsIter,loc) < std::tolower(*lhsIter,loc)) {
423
0
          return false;
424
0
        }
425
0
        if (std::tolower(*lhsIter,loc) < std::tolower(*rhsIter,loc)) {
426
0
          return true;
427
0
        }
428
0
        ++lhsIter;++rhsIter;
429
0
      }
430
0
      return rhsIter != rhs.end();
431
0
   }
432
};
433
434
struct CIStringPairCompare
435
{
436
  bool operator()(const pair<string, uint16_t>& a, const pair<string, uint16_t>& b) const
437
0
  {
438
0
    if(pdns_ilexicographical_compare(a.first, b.first))
439
0
  return true;
440
0
    if(pdns_ilexicographical_compare(b.first, a.first))
441
0
  return false;
442
0
    return a.second < b.second;
443
0
  }
444
};
445
446
inline size_t pdns_ci_find(const string& haystack, const string& needle)
447
0
{
448
0
  string::const_iterator it = std::search(haystack.begin(), haystack.end(),
449
0
    needle.begin(), needle.end(), pdns_iequals_ch);
450
0
  if (it == haystack.end()) {
451
0
    // not found
452
0
    return string::npos;
453
0
  } else {
454
0
    return it - haystack.begin();
455
0
  }
456
0
}
457
458
pair<string, string> splitField(const string& inp, char sepa);
459
460
inline bool isCanonical(const string& qname)
461
0
{
462
0
  if(qname.empty())
463
0
    return false;
464
0
  return qname[qname.size()-1]=='.';
465
0
}
466
467
inline DNSName toCanonic(const DNSName& zone, const string& qname)
468
0
{
469
0
  if(qname.size()==1 && qname[0]=='@')
470
0
    return zone;
471
0
  if(isCanonical(qname))
472
0
    return DNSName(qname);
473
0
  return DNSName(qname) += zone;
474
0
}
475
476
string stripDot(const string& dom);
477
478
int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret);
479
int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret);
480
int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret);
481
bool stringfgets(FILE* fp, std::string& line);
482
483
template<typename Index>
484
std::pair<typename Index::iterator,bool>
485
replacing_insert(Index& i,const typename Index::value_type& x)
486
{
487
  std::pair<typename Index::iterator,bool> res=i.insert(x);
488
  if(!res.second)res.second=i.replace(res.first,x);
489
  return res;
490
}
491
492
/** very small regex wrapper */
493
class Regex
494
{
495
public:
496
  /** constructor that accepts the expression to regex */
497
  Regex(const string &expr);
498
499
  ~Regex()
500
0
  {
501
0
    regfree(&d_preg);
502
0
  }
503
  /** call this to find out if 'line' matches your expression */
504
  bool match(const string &line) const
505
0
  {
506
0
    return regexec(&d_preg,line.c_str(),0,0,0)==0;
507
0
  }
508
  bool match(const DNSName& name) const
509
0
  {
510
0
    return match(name.toStringNoDot());
511
0
  }
512
513
private:
514
  regex_t d_preg;
515
};
516
517
class SimpleMatch
518
{
519
public:
520
  SimpleMatch(const string &mask, bool caseFold = false): d_mask(mask), d_fold(caseFold)
521
0
  {
522
0
  }
523
524
  bool match(string::const_iterator mi, string::const_iterator mend, string::const_iterator vi, string::const_iterator vend) const
525
0
  {
526
0
    for(;;++mi) {
527
0
      if (mi == mend) {
528
0
        return vi == vend;
529
0
      } else if (*mi == '?') {
530
0
        if (vi == vend) return false;
531
0
        ++vi;
532
0
      } else if (*mi == '*') {
533
0
        while(mi != mend && *mi == '*') ++mi;
534
0
        if (mi == mend) return true;
535
0
        while(vi != vend) {
536
0
          if (match(mi,mend,vi,vend)) return true;
537
0
          ++vi;
538
0
        }
539
0
        return false;
540
0
      } else {
541
0
        if ((mi == mend && vi != vend)||
542
0
            (mi != mend && vi == vend)) return false;
543
0
        if (d_fold) {
544
0
          if (dns_tolower(*mi) != dns_tolower(*vi)) return false;
545
0
        } else {
546
0
          if (*mi != *vi) return false;
547
0
        }
548
0
        ++vi;
549
0
      }
550
0
    }
551
0
  }
552
553
0
  bool match(const string& value) const {
554
0
    return match(d_mask.begin(), d_mask.end(), value.begin(), value.end());
555
0
  }
556
557
0
  bool match(const DNSName& name) const {
558
0
    return match(name.toStringNoDot());
559
0
  }
560
561
private:
562
  const string d_mask;
563
  const bool d_fold;
564
};
565
566
union ComboAddress;
567
568
// An aligned type to hold cmsgbufs. See https://man.openbsd.org/CMSG_DATA
569
typedef union { struct cmsghdr hdr; char buf[256]; } cmsgbuf_aligned;
570
571
/* itfIndex is an interface index, as returned by if_nametoindex(). 0 means default. */
572
void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cbuf, const ComboAddress* source, int itfIndex);
573
574
unsigned int getFilenumLimit(bool hardOrSoft=0);
575
void setFilenumLimit(unsigned int lim);
576
bool readFileIfThere(const char* fname, std::string* line);
577
bool setSocketTimestamps(int fd);
578
579
//! Sets the socket into blocking mode.
580
bool setBlocking( int sock );
581
582
//! Sets the socket into non-blocking mode.
583
bool setNonBlocking( int sock );
584
bool setTCPNoDelay(int sock);
585
bool setReuseAddr(int sock);
586
bool isNonBlocking(int sock);
587
bool setReceiveSocketErrors(int sock, int af);
588
int closesocket(int socket);
589
bool setCloseOnExec(int sock);
590
591
size_t getPipeBufferSize(int fd);
592
bool setPipeBufferSize(int fd, size_t size);
593
594
uint64_t udpErrorStats(const std::string& str);
595
uint64_t udp6ErrorStats(const std::string& str);
596
uint64_t tcpErrorStats(const std::string& str);
597
uint64_t getRealMemoryUsage(const std::string&);
598
uint64_t getSpecialMemoryUsage(const std::string&);
599
uint64_t getOpenFileDescriptors(const std::string&);
600
uint64_t getCPUTimeUser(const std::string&);
601
uint64_t getCPUTimeSystem(const std::string&);
602
uint64_t getCPUIOWait(const std::string&);
603
uint64_t getCPUSteal(const std::string&);
604
std::string getMACAddress(const ComboAddress& ca);
605
int getMACAddress(const ComboAddress& ca, char* dest, size_t len);
606
607
template<typename T>
608
const T& defTer(const T& a, const T& b)
609
{
610
  return a ? a : b;
611
}
612
613
template<typename P, typename T>
614
T valueOrEmpty(const P val) {
615
  if (!val) return T{};
616
  return T(val);
617
}
618
619
620
// I'm not very OCD, but I appreciate loglines like "processing 1 delta", "processing 2 deltas" :-)
621
template <typename Integer,
622
typename std::enable_if_t<std::is_integral_v<Integer>, bool> = true>
623
const char* addS(Integer siz, const char* singular = "", const char *plural = "s")
624
{
625
  if (siz == 1) {
626
    return singular;
627
  }
628
  return plural;
629
}
630
631
template <typename C,
632
typename std::enable_if_t<std::is_class_v<C>, bool> = true>
633
const char* addS(const C& c, const char* singular = "", const char *plural = "s")
634
{
635
  return addS(c.size(), singular, plural);
636
}
637
638
template<typename C>
639
const typename C::value_type::second_type* rplookup(const C& c, const typename C::value_type::first_type& key)
640
{
641
  auto fnd = c.find(key);
642
  if(fnd == c.end())
643
    return 0;
644
  return &fnd->second;
645
}
646
647
double DiffTime(const struct timespec& first, const struct timespec& second);
648
double DiffTime(const struct timeval& first, const struct timeval& second);
649
uid_t strToUID(const string &str);
650
gid_t strToGID(const string &str);
651
652
namespace pdns
653
{
654
/**
655
 * \brief Does a checked conversion from one integer type to another.
656
 *
657
 * \warning The source type `F` and target type `T` must have the same
658
 * signedness, otherwise a compilation error is thrown.
659
 *
660
 * \exception std::out_of_range Thrown if the source value does not fit
661
 * in the target type.
662
 *
663
 * \param[in] from The source value of type `F`.
664
 *
665
 * \return The target value of type `T`.
666
 */
667
template <typename T, typename F>
668
auto checked_conv(F from) -> T
669
0
{
670
0
  static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer");
671
0
  static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer");
672
0
  static_assert((std::numeric_limits<F>::is_signed && std::numeric_limits<T>::is_signed) || (!std::numeric_limits<F>::is_signed && !std::numeric_limits<T>::is_signed),
673
0
                "checked_conv: The `T` and `F` types must either both be signed or unsigned");
674
675
0
  constexpr auto tMin = std::numeric_limits<T>::min();
676
0
  if constexpr (std::numeric_limits<F>::min() != tMin) {
677
0
    if (from < tMin) {
678
0
      string s = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin);
679
0
      throw std::out_of_range(s);
680
0
    }
681
0
  }
682
683
0
  constexpr auto tMax = std::numeric_limits<T>::max();
684
0
  if constexpr (std::numeric_limits<F>::max() != tMax) {
685
0
    if (from > tMax) {
686
0
      string s = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax);
687
0
      throw std::out_of_range(s);
688
0
    }
689
0
  }
690
691
0
  return static_cast<T>(from);
692
0
}
Unexecuted instantiation: unsigned char pdns::checked_conv<unsigned char, unsigned long long>(unsigned long long)
Unexecuted instantiation: unsigned short pdns::checked_conv<unsigned short, unsigned long long>(unsigned long long)
Unexecuted instantiation: unsigned int pdns::checked_conv<unsigned int, unsigned long long>(unsigned long long)
693
694
/**
695
 * \brief Performs a conversion from `std::string&` to integer.
696
 *
697
 * This function internally calls `std::stoll` and `std::stoull` to do
698
 * the conversion from `std::string&` and calls `pdns::checked_conv` to
699
 * do the checked conversion from `long long`/`unsigned long long` to
700
 * `T`.
701
 *
702
 * \warning The target type `T` must be an integer, otherwise a
703
 * compilation error is thrown.
704
 *
705
 * \exception std:stoll Throws what std::stoll throws.
706
 *
707
 * \exception std::stoull Throws what std::stoull throws.
708
 *
709
 * \exception pdns::checked_conv Throws what pdns::checked_conv throws.
710
 *
711
 * \param[in] str The input string to be converted.
712
 *
713
 * \param[in] idx Location to store the index at which processing
714
 * stopped. If the input `str` is empty, `*idx` shall be set to 0.
715
 *
716
 * \param[in] base The numerical base for conversion.
717
 *
718
 * \return `str` converted to integer `T`, or 0 if `str` is empty.
719
 */
720
template <typename T>
721
auto checked_stoi(const std::string& str, size_t* idx = nullptr, int base = 10) -> T
722
0
{
723
0
  static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer");
724
725
0
  if (str.empty()) {
726
0
    if (idx != nullptr) {
727
0
      *idx = 0;
728
0
    }
729
730
0
    return 0; // compatibility
731
0
  }
732
733
0
  if constexpr (std::is_unsigned_v<T>) {
734
0
    return pdns::checked_conv<T>(std::stoull(str, idx, base));
735
0
  }
736
0
  else {
737
0
    return pdns::checked_conv<T>(std::stoll(str, idx, base));
738
0
  }
739
0
}
Unexecuted instantiation: unsigned char pdns::checked_stoi<unsigned char>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)
Unexecuted instantiation: unsigned short pdns::checked_stoi<unsigned short>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)
Unexecuted instantiation: unsigned int pdns::checked_stoi<unsigned int>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int)
740
741
/**
742
 * \brief Performs a conversion from `std::string&` to integer.
743
 *
744
 * This function internally calls `pdns::checked_stoi` and stores its
745
 * result in `out`.
746
 *
747
 * \exception pdns::checked_stoi Throws what pdns::checked_stoi throws.
748
 *
749
 * \param[out] out `str` converted to integer `T`, or 0 if `str` is
750
 * empty.
751
 *
752
 * \param[in] str The input string to be converted.
753
 *
754
 * \param[in] idx Location to store the index at which processing
755
 * stopped. If the input `str` is empty, `*idx` shall be set to 0.
756
 *
757
 * \param[in] base The numerical base for conversion.
758
 *
759
 * \return `str` converted to integer `T`, or 0 if `str` is empty.
760
 */
761
template <typename T>
762
auto checked_stoi_into(T& out, const std::string& str, size_t* idx = nullptr, int base = 10)
763
0
{
764
0
  out = checked_stoi<T>(str, idx, base);
765
0
}
766
}
767
768
bool isSettingThreadCPUAffinitySupported();
769
int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
770
771
std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath);
772
773
DNSName reverseNameFromIP(const ComboAddress& ip);
774
775
size_t parseRFC1035CharString(const std::string &in, std::string &val); // from ragel
776
size_t parseSVCBValueListFromParsedRFC1035CharString(const std::string &in, vector<std::string> &val); // from ragel
777
size_t parseSVCBValueList(const std::string &in, vector<std::string> &val);
778
779
std::string makeLuaString(const std::string& in);
780
781
bool constantTimeStringEquals(const std::string& a, const std::string& b);
782
783
// Used in NID and L64 records
784
struct NodeOrLocatorID { uint8_t content[8]; };
785
786
struct FDWrapper
787
{
788
  FDWrapper() = default;
789
0
  FDWrapper(int desc): d_fd(desc) {}
790
  FDWrapper(const FDWrapper&) = delete;
791
  FDWrapper& operator=(const FDWrapper& rhs) = delete;
792
793
794
  ~FDWrapper()
795
0
  {
796
0
    reset();
797
0
  }
798
799
  FDWrapper(FDWrapper&& rhs) noexcept : d_fd(rhs.d_fd)
800
0
  {
801
0
    rhs.d_fd = -1;
802
0
  }
803
804
  FDWrapper& operator=(FDWrapper&& rhs) noexcept
805
0
  {
806
0
    if (d_fd >= 0) {
807
0
      close(d_fd);
808
0
    }
809
0
    d_fd = rhs.d_fd;
810
0
    rhs.d_fd = -1;
811
0
    return *this;
812
0
  }
813
814
  [[nodiscard]] int getHandle() const
815
0
  {
816
0
    return d_fd;
817
0
  }
818
819
  operator int() const
820
0
  {
821
0
    return d_fd;
822
0
  }
823
824
  int reset()
825
0
  {
826
0
    int ret = 0;
827
0
    if (d_fd >= 0) {
828
0
      ret = close(d_fd);
829
0
    }
830
0
    d_fd = -1;
831
0
    return ret;
832
0
  }
833
834
private:
835
  int d_fd{-1};
836
};
837
838
namespace pdns
839
{
840
[[nodiscard]] std::optional<std::string> visit_directory(const std::string& directory, const std::function<bool(ino_t inodeNumber, const std::string_view& name)>& visitor);
841
842
struct FilePtrDeleter
843
{
844
  /* using a deleter instead of decltype(&fclose) has two big advantages:
845
     - the deleter is included in the type and does not have to be passed
846
       when creating a new object (easier to use, less memory usage, in theory
847
       better inlining)
848
     - we avoid the annoying "ignoring attributes on template argument ‘int (*)(FILE*)’"
849
       warning from the compiler, which is there because fclose is tagged as __nonnull((1))
850
  */
851
0
  void operator()(FILE* filePtr) const noexcept {
852
0
    fclose(filePtr);
853
0
  }
854
};
855
856
using UniqueFilePtr = std::unique_ptr<FILE, FilePtrDeleter>;
857
858
UniqueFilePtr openFileForWriting(const std::string& filePath, mode_t permissions, bool mustNotExist = true, bool appendIfExists = false);
859
}