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