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 <cstring> |
24 | | #include <cstdio> |
25 | | #include <regex.h> |
26 | | #include <tuple> |
27 | | #include <type_traits> |
28 | | |
29 | | #include <boost/algorithm/string.hpp> |
30 | | |
31 | | #include <atomic> |
32 | | #include <sys/time.h> |
33 | | #include <sys/types.h> |
34 | | #include <sys/socket.h> |
35 | | #include <ctime> |
36 | | #include <syslog.h> |
37 | | #include <stdexcept> |
38 | | #include <string> |
39 | | #include <string_view> |
40 | | #include <cctype> |
41 | | #include <utility> |
42 | | #include <vector> |
43 | | |
44 | | #include "dns.hh" // Do not remove, whatever clang-tidy says |
45 | | #include "namespaces.hh" |
46 | | #include "logr.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, int seconds, int mseconds, int* fdOut); |
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 stringtok(Container& container, const string& str, |
123 | | const char* const delimiters = " \t\n") |
124 | 28.4k | { |
125 | 28.4k | const string::size_type len = str.length(); |
126 | 28.4k | string::size_type index = 0; |
127 | | |
128 | 31.3M | while (index < len) { |
129 | | // eat leading whitespace |
130 | 31.3M | index = str.find_first_not_of(delimiters, index); |
131 | 31.3M | if (index == string::npos) { |
132 | 6 | return; // nothing left but white space |
133 | 6 | } |
134 | | // find the end of the token |
135 | 31.3M | string::size_type end = str.find_first_of(delimiters, index); |
136 | | |
137 | | // push token |
138 | 31.3M | if (end == string::npos) { |
139 | 27.9k | container.push_back(str.substr(index)); |
140 | 27.9k | return; |
141 | 27.9k | } |
142 | 31.3M | container.push_back(str.substr(index, end - index)); |
143 | | |
144 | | // set up for next loop |
145 | 31.3M | index = end + 1; |
146 | 31.3M | } |
147 | 28.4k | } |
148 | | |
149 | | template <typename T> |
150 | | 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> |
157 | | bool rfc1982LessThanOrEqual(T lhs, T rhs) |
158 | | { |
159 | | static_assert(std::is_unsigned_v<T>, "rfc1982LessThanOrEqual only works for unsigned types"); |
160 | | return static_cast<std::make_signed_t<T>>(lhs - rhs) <= 0; |
161 | | } |
162 | | |
163 | | // fills container with ranges, so {posbegin,posend} |
164 | | template <typename Container> |
165 | | void vstringtok(Container& container, string const& str, |
166 | | const char* const delimiters = " \t\n") |
167 | 277k | { |
168 | 277k | const string::size_type len = str.length(); |
169 | 277k | string::size_type index = 0; |
170 | | |
171 | 76.6M | while (index < len) { |
172 | | // eat leading whitespace |
173 | 76.6M | index = str.find_first_not_of(delimiters, index); |
174 | 76.6M | if (index == string::npos) { |
175 | 0 | return; // nothing left but white space |
176 | 0 | } |
177 | | // find the end of the token |
178 | 76.6M | string::size_type end = str.find_first_of(delimiters, index); |
179 | | |
180 | | // push token |
181 | 76.6M | if (end == string::npos) { |
182 | 248k | container.emplace_back(index, len); |
183 | 248k | return; |
184 | 248k | } |
185 | 76.4M | container.emplace_back(index, end); |
186 | | |
187 | | // set up for next loop |
188 | 76.4M | index = end + 1; |
189 | 76.4M | } |
190 | 277k | } 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 | 277k | { | 168 | 277k | const string::size_type len = str.length(); | 169 | 277k | string::size_type index = 0; | 170 | | | 171 | 76.6M | while (index < len) { | 172 | | // eat leading whitespace | 173 | 76.6M | index = str.find_first_not_of(delimiters, index); | 174 | 76.6M | if (index == string::npos) { | 175 | 0 | return; // nothing left but white space | 176 | 0 | } | 177 | | // find the end of the token | 178 | 76.6M | string::size_type end = str.find_first_of(delimiters, index); | 179 | | | 180 | | // push token | 181 | 76.6M | if (end == string::npos) { | 182 | 248k | container.emplace_back(index, len); | 183 | 248k | return; | 184 | 248k | } | 185 | 76.4M | container.emplace_back(index, end); | 186 | | | 187 | | // set up for next loop | 188 | 76.4M | index = end + 1; | 189 | 76.4M | } | 190 | 277k | } |
|
191 | | |
192 | | size_t writen2(int fileDesc, const void* buf, size_t count); |
193 | 0 | inline size_t writen2(int fileDesc, const std::string& str) { return writen2(fileDesc, str.data(), str.size()); } |
194 | | size_t readn2(int fileDesc, void* buffer, size_t len); |
195 | | size_t readn2WithTimeout(int fileDesc, void* buffer, size_t len, const struct timeval& idleTimeout, const struct timeval& totalTimeout = {0, 0}, bool allowIncomplete = false); |
196 | | size_t writen2WithTimeout(int fileDesc, const void* buffer, size_t len, const struct timeval& timeout); |
197 | | |
198 | | void toLowerInPlace(string& str); |
199 | | string toLower(const string& upper); |
200 | | string toLowerCanonic(const string& upper); |
201 | | bool IpToU32(const string& str, uint32_t* ptr); |
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 | | [[nodiscard]] uint64_t ndiff() const |
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 | | |
228 | | private: |
229 | | struct timespec d_start; |
230 | | }; |
231 | | #endif |
232 | | |
233 | | /** The DTime class can be used for timing statistics with microsecond resolution. |
234 | | On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */ |
235 | | class DTime |
236 | | { |
237 | | public: |
238 | | //!< Does not set the timer for you! Saves lots of gettimeofday() calls |
239 | | [[nodiscard]] inline time_t time() const; |
240 | | inline void set(); //!< Reset the timer |
241 | | inline long udiff(bool reset = true); //!< Return the number of microseconds since the timer was last set. |
242 | | |
243 | | long udiffNoReset() //!< Return the number of microseconds since the timer was last set. |
244 | 0 | { |
245 | 0 | return udiff(false); |
246 | 0 | } |
247 | | void setTimeval(const struct timeval& timeval) |
248 | 0 | { |
249 | 0 | d_set = timeval; |
250 | 0 | } |
251 | | [[nodiscard]] struct timeval getTimeval() const |
252 | 0 | { |
253 | 0 | return d_set; |
254 | 0 | } |
255 | | |
256 | | private: |
257 | | struct timeval d_set{0, 0}; |
258 | | }; |
259 | | |
260 | | inline time_t DTime::time() const |
261 | 0 | { |
262 | 0 | return d_set.tv_sec; |
263 | 0 | } |
264 | | |
265 | | inline void DTime::set() |
266 | 0 | { |
267 | 0 | gettimeofday(&d_set, nullptr); |
268 | 0 | } |
269 | | |
270 | | inline long DTime::udiff(bool reset) |
271 | 0 | { |
272 | 0 | struct timeval now{}; |
273 | 0 | gettimeofday(&now, nullptr); |
274 | 0 |
|
275 | 0 | long ret = (1000000 * (now.tv_sec - d_set.tv_sec)) + (now.tv_usec - d_set.tv_usec); |
276 | 0 |
|
277 | 0 | if (reset) { |
278 | 0 | d_set = now; |
279 | 0 | } |
280 | 0 |
|
281 | 0 | return ret; |
282 | 0 | } |
283 | | |
284 | | inline void toLowerInPlace(string& str) |
285 | 0 | { |
286 | 0 | const size_t length = str.length(); |
287 | |
|
288 | 0 | for (size_t i = 0; i < length; ++i) { |
289 | 0 | char character = static_cast<char>(dns_tolower(str[i])); |
290 | 0 | if (character != str[i]) { |
291 | 0 | str[i] = character; |
292 | 0 | } |
293 | 0 | } |
294 | 0 | } |
295 | | |
296 | | inline string toLower(const string& upper) |
297 | 0 | { |
298 | 0 | string reply(upper); |
299 | |
|
300 | 0 | toLowerInPlace(reply); |
301 | |
|
302 | 0 | return reply; |
303 | 0 | } |
304 | | |
305 | | inline string toLowerCanonic(const string& upper) |
306 | 0 | { |
307 | 0 | string reply(upper); |
308 | 0 | if (!reply.empty()) { |
309 | 0 | const auto length = reply.length(); |
310 | 0 | if (reply[length - 1] == '.') { |
311 | 0 | reply.resize(length - 1); |
312 | 0 | } |
313 | 0 | toLowerInPlace(reply); |
314 | 0 | } |
315 | 0 | return reply; |
316 | 0 | } |
317 | | |
318 | | // Make s uppercase: |
319 | | inline string toUpper(const string& str) |
320 | 160k | { |
321 | 160k | string ret(str); |
322 | 661M | for (size_t i = 0; i < str.length(); ++i) { |
323 | 661M | ret[i] = static_cast<char>(dns_toupper(ret[i])); |
324 | 661M | } |
325 | 160k | return ret; |
326 | 160k | } |
327 | | |
328 | | inline double getTime() |
329 | 0 | { |
330 | 0 | struct timeval now{}; |
331 | 0 | gettimeofday(&now, nullptr); |
332 | 0 |
|
333 | 0 | return static_cast<double>(now.tv_sec) + (now.tv_usec / 1000000.0); |
334 | 0 | } |
335 | | |
336 | | [[noreturn]] inline void unixDie(const string& why) |
337 | 0 | { |
338 | 0 | throw runtime_error(why + ": " + stringerror(errno)); |
339 | 0 | } |
340 | | |
341 | | string makeHexDump(const string& str, const string& sep = " "); |
342 | | //! Convert the hexstring to a byte string |
343 | | string makeBytesFromHex(const string& str); |
344 | | |
345 | | void normalizeTV(struct timeval& timeval); |
346 | | struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs); |
347 | | struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs); |
348 | | |
349 | | inline float makeFloat(const struct timeval& timeval) |
350 | 0 | { |
351 | 0 | return static_cast<float>(timeval.tv_sec) + (static_cast<float>(timeval.tv_usec) / 1000000.0F); |
352 | 0 | } |
353 | | inline uint64_t uSec(const struct timeval& timeval) |
354 | 0 | { |
355 | 0 | return (timeval.tv_sec * 1000000) + timeval.tv_usec; |
356 | 0 | } |
357 | | |
358 | | inline bool operator<(const struct timeval& lhs, const struct timeval& rhs) |
359 | 0 | { |
360 | 0 | return std::tie(lhs.tv_sec, lhs.tv_usec) < std::tie(rhs.tv_sec, rhs.tv_usec); |
361 | 0 | } |
362 | | inline bool operator<=(const struct timeval& lhs, const struct timeval& rhs) |
363 | 0 | { |
364 | 0 | return std::tie(lhs.tv_sec, lhs.tv_usec) <= std::tie(rhs.tv_sec, rhs.tv_usec); |
365 | 0 | } |
366 | | |
367 | | inline bool operator<(const struct timespec& lhs, const struct timespec& rhs) |
368 | 0 | { |
369 | 0 | return std::tie(lhs.tv_sec, lhs.tv_nsec) < std::tie(rhs.tv_sec, rhs.tv_nsec); |
370 | 0 | } |
371 | | |
372 | | using AtomicCounterInner = unsigned long; |
373 | | using AtomicCounter = std::atomic<AtomicCounterInner>; |
374 | | |
375 | | // FIXME400 this should probably go? |
376 | | struct CIStringCompare |
377 | | { |
378 | | bool operator()(const string& lhs, const string& rhs) const |
379 | 0 | { |
380 | 0 | return pdns_ilexicographical_compare(lhs, rhs); |
381 | 0 | } |
382 | | }; |
383 | | |
384 | | struct CIStringComparePOSIX |
385 | | { |
386 | | bool operator()(const std::string& lhs, const std::string& rhs) const |
387 | 0 | { |
388 | 0 | const std::locale& loc = std::locale("POSIX"); |
389 | 0 | auto lhsIter = lhs.begin(); |
390 | 0 | auto rhsIter = rhs.begin(); |
391 | 0 | while (lhsIter != lhs.end()) { |
392 | 0 | if (rhsIter == rhs.end() || std::tolower(*rhsIter, loc) < std::tolower(*lhsIter, loc)) { |
393 | 0 | return false; |
394 | 0 | } |
395 | 0 | if (std::tolower(*lhsIter, loc) < std::tolower(*rhsIter, loc)) { |
396 | 0 | return true; |
397 | 0 | } |
398 | 0 | ++lhsIter; |
399 | 0 | ++rhsIter; |
400 | 0 | } |
401 | 0 | return rhsIter != rhs.end(); |
402 | 0 | } |
403 | | }; |
404 | | |
405 | | struct CIStringPairCompare |
406 | | { |
407 | | bool operator()(const pair<string, uint16_t>& lhs, const pair<string, uint16_t>& rhs) const |
408 | 0 | { |
409 | 0 | if (pdns_ilexicographical_compare(lhs.first, rhs.first)) { |
410 | 0 | return true; |
411 | 0 | } |
412 | 0 | if (pdns_ilexicographical_compare(rhs.first, lhs.first)) { |
413 | 0 | return false; |
414 | 0 | } |
415 | 0 | return lhs.second < rhs.second; |
416 | 0 | } |
417 | | }; |
418 | | |
419 | | inline size_t pdns_ci_find(const string& haystack, const string& needle) |
420 | 0 | { |
421 | 0 | string::const_iterator iter = std::search(haystack.begin(), haystack.end(), |
422 | 0 | needle.begin(), needle.end(), pdns_iequals_ch); |
423 | 0 | if (iter == haystack.end()) { |
424 | 0 | // not found |
425 | 0 | return string::npos; |
426 | 0 | } |
427 | 0 | return iter - haystack.begin(); |
428 | 0 | } |
429 | | |
430 | | pair<string, string> splitField(const string& inp, char sepa); |
431 | | |
432 | | inline bool isCanonical(std::string_view qname) |
433 | 187k | { |
434 | 187k | if (qname.empty()) { |
435 | 789 | return false; |
436 | 789 | } |
437 | 186k | auto iter = qname.rbegin(); |
438 | 186k | if (*iter == '.') { |
439 | 27.7k | iter++; |
440 | 27.7k | size_t backslashCount = 0; |
441 | 30.4k | while (iter != qname.rend()) { |
442 | 23.3k | if (*iter != '\\') { |
443 | 20.5k | break; |
444 | 20.5k | } |
445 | 2.73k | backslashCount++; |
446 | 2.73k | iter++; |
447 | 2.73k | } |
448 | 27.7k | return (backslashCount % 2) == 0; |
449 | 27.7k | } |
450 | 158k | return false; |
451 | 186k | } |
452 | | |
453 | | inline DNSName toCanonic(const ZoneName& zone, const string& qname) |
454 | 48.7k | { |
455 | 48.7k | if (qname.size() == 1 && qname[0] == '@') { |
456 | 2.38k | return DNSName{zone}; |
457 | 2.38k | } |
458 | 46.3k | if (isCanonical(qname)) { |
459 | 2.66k | return DNSName{qname}; |
460 | 2.66k | } |
461 | 43.6k | return DNSName{qname} += DNSName{zone}; |
462 | 46.3k | } |
463 | | |
464 | | string stripDot(const string& dom); |
465 | | |
466 | | int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret); |
467 | | int makeIPv4sockaddr(const std::string& arg, struct sockaddr_in* ret); |
468 | | int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret); |
469 | | bool stringfgets(FILE* file, std::string& line); |
470 | | |
471 | | template <typename Index> |
472 | | std::pair<typename Index::iterator, bool> |
473 | | replacing_insert(Index& index, const typename Index::value_type& value) |
474 | | { |
475 | | std::pair<typename Index::iterator, bool> res = index.insert(value); |
476 | | if (!res.second) { |
477 | | res.second = index.replace(res.first, value); |
478 | | } |
479 | | return res; |
480 | | } |
481 | | |
482 | | /** very small regex wrapper */ |
483 | | class Regex |
484 | | { |
485 | | public: |
486 | | /** constructor that accepts the expression to regex */ |
487 | | Regex(const string& expr); |
488 | | Regex(const Regex&) = delete; |
489 | | Regex& operator=(const Regex&) = delete; |
490 | | Regex(Regex&& rhs) = default; |
491 | | Regex& operator=(Regex&& rhs) = default; |
492 | 0 | ~Regex() = default; |
493 | | /** call this to find out if 'line' matches your expression */ |
494 | | [[nodiscard]] bool match(const string& line) const; |
495 | | [[nodiscard]] bool match(const DNSName& name) const; |
496 | | |
497 | | private: |
498 | | struct Deleter |
499 | | { |
500 | | void operator()(regex_t* ptr) const noexcept |
501 | 0 | { |
502 | 0 | regfree(ptr); |
503 | 0 | delete ptr; // NOLINT(cppcoreguidelines-owning-memory) |
504 | 0 | } |
505 | | }; |
506 | | |
507 | | std::unique_ptr<regex_t, Deleter> d_preg; |
508 | | }; |
509 | | |
510 | | class SimpleMatch |
511 | | { |
512 | | public: |
513 | | SimpleMatch(string mask, bool caseFold = false) : |
514 | | d_mask(std::move(mask)), d_fold(caseFold) |
515 | 0 | { |
516 | 0 | } |
517 | | |
518 | | [[nodiscard]] bool match(string::const_iterator iter, string::const_iterator mend, string::const_iterator vbegin, string::const_iterator vend) const |
519 | 0 | { |
520 | 0 | for (;; ++iter) { |
521 | 0 | if (iter == mend) { |
522 | 0 | return vbegin == vend; |
523 | 0 | } |
524 | 0 | if (*iter == '?') { |
525 | 0 | if (vbegin == vend) { |
526 | 0 | return false; |
527 | 0 | } |
528 | 0 | ++vbegin; |
529 | 0 | } |
530 | 0 | else if (*iter == '*') { |
531 | 0 | while (iter != mend && *iter == '*') { |
532 | 0 | ++iter; |
533 | 0 | } |
534 | 0 | if (iter == mend) { |
535 | 0 | return true; |
536 | 0 | } |
537 | 0 | while (vbegin != vend) { |
538 | 0 | if (match(iter, mend, vbegin, vend)) { |
539 | 0 | return true; |
540 | 0 | } |
541 | 0 | ++vbegin; |
542 | 0 | } |
543 | 0 | return false; |
544 | 0 | } |
545 | 0 | else { |
546 | 0 | if ((iter == mend && vbegin != vend) || (iter != mend && vbegin == vend)) { |
547 | 0 | return false; |
548 | 0 | } |
549 | 0 | if (d_fold) { |
550 | 0 | if (dns_tolower(*iter) != dns_tolower(*vbegin)) { |
551 | 0 | return false; |
552 | 0 | } |
553 | 0 | } |
554 | 0 | else { |
555 | 0 | if (*iter != *vbegin) { |
556 | 0 | return false; |
557 | 0 | } |
558 | 0 | } |
559 | 0 | ++vbegin; |
560 | 0 | } |
561 | 0 | } |
562 | 0 | } |
563 | | |
564 | | [[nodiscard]] bool match(const string& value) const |
565 | 0 | { |
566 | 0 | return match(d_mask.begin(), d_mask.end(), value.begin(), value.end()); |
567 | 0 | } |
568 | | |
569 | | [[nodiscard]] bool match(const DNSName& name) const |
570 | 0 | { |
571 | 0 | return match(name.toStringNoDot()); |
572 | 0 | } |
573 | | |
574 | | #if defined(PDNS_AUTH) // [ |
575 | | [[nodiscard]] bool match(const ZoneName& name) const |
576 | 0 | { |
577 | 0 | return match(name.toStringNoDot()); |
578 | 0 | } |
579 | | #endif // ] |
580 | | |
581 | | private: |
582 | | const string d_mask; |
583 | | const bool d_fold; |
584 | | }; |
585 | | |
586 | | union ComboAddress; |
587 | | |
588 | | // An aligned type to hold cmsgbufs. See https://man.openbsd.org/CMSG_DATA |
589 | | union cmsgbuf_aligned |
590 | | { |
591 | | struct cmsghdr hdr; |
592 | | std::array<char, 256> buf; |
593 | | }; |
594 | | |
595 | | /* itfIndex is an interface index, as returned by if_nametoindex(). 0 means default. */ |
596 | | void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cmsgbuf, const ComboAddress* source, int itfIndex); |
597 | | |
598 | | unsigned int getFilenumLimit(bool hardOrSoft = false); |
599 | | void setFilenumLimit(unsigned int lim); |
600 | | bool readFileIfThere(const char* fname, std::string* line); |
601 | | bool setSocketTimestamps(int fileDesc); |
602 | | |
603 | | //! Sets the socket into blocking mode. |
604 | | bool setBlocking(int sock); |
605 | | |
606 | | void setDscp(int sock, unsigned short family, uint8_t dscp); |
607 | | |
608 | | //! Sets the socket into non-blocking mode. |
609 | | bool setNonBlocking(int sock); |
610 | | bool setTCPNoDelay(int sock); |
611 | | bool setReuseAddr(int sock); |
612 | | bool isNonBlocking(int sock); |
613 | | bool setReceiveSocketErrors(int sock, int family); |
614 | | int closesocket(int socket); |
615 | | bool setCloseOnExec(int sock); |
616 | | |
617 | | size_t getPipeBufferSize(int fileDesc); |
618 | | bool setPipeBufferSize(int fileDesc, size_t size); |
619 | | |
620 | | uint64_t udpErrorStats(const std::string& str); |
621 | | uint64_t udp6ErrorStats(const std::string& str); |
622 | | uint64_t tcpErrorStats(const std::string& label); |
623 | | uint64_t getRealMemoryUsage(const std::string&); |
624 | | uint64_t getSpecialMemoryUsage(const std::string&); |
625 | | uint64_t getOpenFileDescriptors(const std::string&); |
626 | | uint64_t getCPUTimeUser(const std::string&); |
627 | | uint64_t getCPUTimeSystem(const std::string&); |
628 | | uint64_t getCPUIOWait(const std::string&); |
629 | | uint64_t getCPUSteal(const std::string&); |
630 | | std::string getMACAddress(const ComboAddress& address); |
631 | | int getMACAddress(const ComboAddress& address, char* dest, size_t len); |
632 | | |
633 | | template <typename T> |
634 | | const T& defTer(const T& lhs, const T& rhs) |
635 | | { |
636 | | return lhs ? lhs : rhs; |
637 | | } |
638 | | |
639 | | template <typename P, typename T> |
640 | | T valueOrEmpty(const P val) |
641 | | { |
642 | | if (!val) { |
643 | | return T{}; |
644 | | } |
645 | | return T(val); |
646 | | } |
647 | | |
648 | | // I'm not very OCD, but I appreciate loglines like "processing 1 delta", "processing 2 deltas" :-) |
649 | | template <typename Integer, |
650 | | typename std::enable_if_t<std::is_integral_v<Integer>, bool> = true> |
651 | | const char* addS(Integer siz, const char* singular = "", const char* plural = "s") |
652 | | { |
653 | | if (siz == 1) { |
654 | | return singular; |
655 | | } |
656 | | return plural; |
657 | | } |
658 | | |
659 | | template <typename C, |
660 | | typename std::enable_if_t<std::is_class_v<C>, bool> = true> |
661 | | const char* addS(const C& container, const char* singular = "", const char* plural = "s") |
662 | | { |
663 | | return addS(container.size(), singular, plural); |
664 | | } |
665 | | |
666 | | template <typename C> |
667 | | const typename C::value_type::second_type* rplookup(const C& container, const typename C::value_type::first_type& key) |
668 | | { |
669 | | auto fnd = container.find(key); |
670 | | if (fnd == container.end()) { |
671 | | return 0; |
672 | | } |
673 | | return &fnd->second; |
674 | | } |
675 | | |
676 | | double DiffTime(const struct timespec& first, const struct timespec& second); |
677 | | double DiffTime(const struct timeval& first, const struct timeval& second); |
678 | | uid_t strToUID(const string& str); |
679 | | gid_t strToGID(const string& str); |
680 | | |
681 | | namespace pdns |
682 | | { |
683 | | /** |
684 | | * \brief Does a checked conversion from one integer type to another. |
685 | | * |
686 | | * \warning The source type `F` and target type `T` must have the same |
687 | | * signedness, otherwise a compilation error is thrown. |
688 | | * |
689 | | * \exception std::out_of_range Thrown if the source value does not fit |
690 | | * in the target type. |
691 | | * |
692 | | * \param[in] from The source value of type `F`. |
693 | | * |
694 | | * \return The target value of type `T`. |
695 | | */ |
696 | | template <typename T, typename F> |
697 | | auto checked_conv(F from) -> T |
698 | 422k | { |
699 | 422k | static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer"); |
700 | 422k | static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer"); |
701 | 422k | 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), |
702 | 422k | "checked_conv: The `T` and `F` types must either both be signed or unsigned"); |
703 | | |
704 | 422k | constexpr auto tMin = std::numeric_limits<T>::min(); |
705 | | if constexpr (std::numeric_limits<F>::min() != tMin) { |
706 | | if (from < tMin) { |
707 | | string str = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin); |
708 | | throw std::out_of_range(str); |
709 | | } |
710 | | } |
711 | | |
712 | 422k | constexpr auto tMax = std::numeric_limits<T>::max(); |
713 | 422k | if constexpr (std::numeric_limits<F>::max() != tMax) { |
714 | 422k | if (from > tMax) { |
715 | 461 | string str = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax); |
716 | 461 | throw std::out_of_range(str); |
717 | 461 | } |
718 | 422k | } |
719 | | |
720 | 421k | return static_cast<T>(from); |
721 | 422k | } unsigned char pdns::checked_conv<unsigned char, unsigned long long>(unsigned long long) Line | Count | Source | 698 | 5.22k | { | 699 | 5.22k | static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer"); | 700 | 5.22k | static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer"); | 701 | 5.22k | 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), | 702 | 5.22k | "checked_conv: The `T` and `F` types must either both be signed or unsigned"); | 703 | | | 704 | 5.22k | constexpr auto tMin = std::numeric_limits<T>::min(); | 705 | | if constexpr (std::numeric_limits<F>::min() != tMin) { | 706 | | if (from < tMin) { | 707 | | string str = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin); | 708 | | throw std::out_of_range(str); | 709 | | } | 710 | | } | 711 | | | 712 | 5.22k | constexpr auto tMax = std::numeric_limits<T>::max(); | 713 | 5.22k | if constexpr (std::numeric_limits<F>::max() != tMax) { | 714 | 5.22k | if (from > tMax) { | 715 | 105 | string str = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax); | 716 | 105 | throw std::out_of_range(str); | 717 | 105 | } | 718 | 5.22k | } | 719 | | | 720 | 5.12k | return static_cast<T>(from); | 721 | 5.22k | } |
unsigned long pdns::checked_conv<unsigned long, unsigned long long>(unsigned long long) Line | Count | Source | 698 | 570 | { | 699 | 570 | static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer"); | 700 | 570 | static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer"); | 701 | 570 | 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), | 702 | 570 | "checked_conv: The `T` and `F` types must either both be signed or unsigned"); | 703 | | | 704 | 570 | constexpr auto tMin = std::numeric_limits<T>::min(); | 705 | | if constexpr (std::numeric_limits<F>::min() != tMin) { | 706 | | if (from < tMin) { | 707 | | string str = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin); | 708 | | throw std::out_of_range(str); | 709 | | } | 710 | | } | 711 | | | 712 | 570 | constexpr auto tMax = std::numeric_limits<T>::max(); | 713 | | if constexpr (std::numeric_limits<F>::max() != tMax) { | 714 | | if (from > tMax) { | 715 | | string str = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax); | 716 | | throw std::out_of_range(str); | 717 | | } | 718 | | } | 719 | | | 720 | 570 | return static_cast<T>(from); | 721 | 570 | } |
unsigned short pdns::checked_conv<unsigned short, unsigned long long>(unsigned long long) Line | Count | Source | 698 | 402k | { | 699 | 402k | static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer"); | 700 | 402k | static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer"); | 701 | 402k | 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), | 702 | 402k | "checked_conv: The `T` and `F` types must either both be signed or unsigned"); | 703 | | | 704 | 402k | constexpr auto tMin = std::numeric_limits<T>::min(); | 705 | | if constexpr (std::numeric_limits<F>::min() != tMin) { | 706 | | if (from < tMin) { | 707 | | string str = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin); | 708 | | throw std::out_of_range(str); | 709 | | } | 710 | | } | 711 | | | 712 | 402k | constexpr auto tMax = std::numeric_limits<T>::max(); | 713 | 402k | if constexpr (std::numeric_limits<F>::max() != tMax) { | 714 | 402k | if (from > tMax) { | 715 | 214 | string str = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax); | 716 | 214 | throw std::out_of_range(str); | 717 | 214 | } | 718 | 402k | } | 719 | | | 720 | 402k | return static_cast<T>(from); | 721 | 402k | } |
unsigned int pdns::checked_conv<unsigned int, unsigned long long>(unsigned long long) Line | Count | Source | 698 | 14.3k | { | 699 | 14.3k | static_assert(std::numeric_limits<F>::is_integer, "checked_conv: The `F` type must be an integer"); | 700 | 14.3k | static_assert(std::numeric_limits<T>::is_integer, "checked_conv: The `T` type must be an integer"); | 701 | 14.3k | 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), | 702 | 14.3k | "checked_conv: The `T` and `F` types must either both be signed or unsigned"); | 703 | | | 704 | 14.3k | constexpr auto tMin = std::numeric_limits<T>::min(); | 705 | | if constexpr (std::numeric_limits<F>::min() != tMin) { | 706 | | if (from < tMin) { | 707 | | string str = "checked_conv: source value " + std::to_string(from) + " is smaller than target's minimum possible value " + std::to_string(tMin); | 708 | | throw std::out_of_range(str); | 709 | | } | 710 | | } | 711 | | | 712 | 14.3k | constexpr auto tMax = std::numeric_limits<T>::max(); | 713 | 14.3k | if constexpr (std::numeric_limits<F>::max() != tMax) { | 714 | 14.3k | if (from > tMax) { | 715 | 142 | string str = "checked_conv: source value " + std::to_string(from) + " is larger than target's maximum possible value " + std::to_string(tMax); | 716 | 142 | throw std::out_of_range(str); | 717 | 142 | } | 718 | 14.3k | } | 719 | | | 720 | 14.2k | return static_cast<T>(from); | 721 | 14.3k | } |
|
722 | | |
723 | | /** |
724 | | * \brief Performs a conversion from `std::string&` to integer. |
725 | | * |
726 | | * This function internally calls `std::stoll` and `std::stoull` to do |
727 | | * the conversion from `std::string&` and calls `pdns::checked_conv` to |
728 | | * do the checked conversion from `long long`/`unsigned long long` to |
729 | | * `T`. |
730 | | * |
731 | | * \warning The target type `T` must be an integer, otherwise a |
732 | | * compilation error is thrown. |
733 | | * |
734 | | * \exception std:stoll Throws what std::stoll throws. |
735 | | * |
736 | | * \exception std::stoull Throws what std::stoull throws. |
737 | | * |
738 | | * \exception pdns::checked_conv Throws what pdns::checked_conv throws. |
739 | | * |
740 | | * \param[in] str The input string to be converted. |
741 | | * |
742 | | * \param[in] idx Location to store the index at which processing |
743 | | * stopped. If the input `str` is empty, `*idx` shall be set to 0. |
744 | | * |
745 | | * \param[in] base The numerical base for conversion. |
746 | | * |
747 | | * \return `str` converted to integer `T`, or 0 if `str` is empty. |
748 | | */ |
749 | | template <typename T> |
750 | | auto checked_stoi(const std::string& str, size_t* idx = nullptr, int base = 10) -> T |
751 | 425k | { |
752 | 425k | static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer"); |
753 | | |
754 | 425k | if (str.empty()) { |
755 | 2.03k | if (idx != nullptr) { |
756 | 0 | *idx = 0; |
757 | 0 | } |
758 | | |
759 | 2.03k | return 0; // compatibility |
760 | 2.03k | } |
761 | | |
762 | 423k | if constexpr (std::is_unsigned_v<T>) { |
763 | 423k | return pdns::checked_conv<T>(std::stoull(str, idx, base)); |
764 | | } |
765 | | else { |
766 | | return pdns::checked_conv<T>(std::stoll(str, idx, base)); |
767 | | } |
768 | 423k | } 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) Line | Count | Source | 751 | 5.23k | { | 752 | 5.23k | static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer"); | 753 | | | 754 | 5.23k | if (str.empty()) { | 755 | 0 | if (idx != nullptr) { | 756 | 0 | *idx = 0; | 757 | 0 | } | 758 | |
| 759 | 0 | return 0; // compatibility | 760 | 0 | } | 761 | | | 762 | 5.23k | if constexpr (std::is_unsigned_v<T>) { | 763 | 5.23k | return pdns::checked_conv<T>(std::stoull(str, idx, base)); | 764 | | } | 765 | | else { | 766 | | return pdns::checked_conv<T>(std::stoll(str, idx, base)); | 767 | | } | 768 | 5.23k | } |
unsigned long pdns::checked_stoi<unsigned long>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long*, int) Line | Count | Source | 751 | 584 | { | 752 | 584 | static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer"); | 753 | | | 754 | 584 | if (str.empty()) { | 755 | 0 | if (idx != nullptr) { | 756 | 0 | *idx = 0; | 757 | 0 | } | 758 | |
| 759 | 0 | return 0; // compatibility | 760 | 0 | } | 761 | | | 762 | 584 | if constexpr (std::is_unsigned_v<T>) { | 763 | 584 | return pdns::checked_conv<T>(std::stoull(str, idx, base)); | 764 | | } | 765 | | else { | 766 | | return pdns::checked_conv<T>(std::stoll(str, idx, base)); | 767 | | } | 768 | 584 | } |
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 | 751 | 404k | { | 752 | 404k | static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer"); | 753 | | | 754 | 404k | if (str.empty()) { | 755 | 2.03k | if (idx != nullptr) { | 756 | 0 | *idx = 0; | 757 | 0 | } | 758 | | | 759 | 2.03k | return 0; // compatibility | 760 | 2.03k | } | 761 | | | 762 | 402k | if constexpr (std::is_unsigned_v<T>) { | 763 | 402k | return pdns::checked_conv<T>(std::stoull(str, idx, base)); | 764 | | } | 765 | | else { | 766 | | return pdns::checked_conv<T>(std::stoll(str, idx, base)); | 767 | | } | 768 | 402k | } |
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 | 751 | 14.4k | { | 752 | 14.4k | static_assert(std::numeric_limits<T>::is_integer, "checked_stoi: The `T` type must be an integer"); | 753 | | | 754 | 14.4k | if (str.empty()) { | 755 | 0 | if (idx != nullptr) { | 756 | 0 | *idx = 0; | 757 | 0 | } | 758 | |
| 759 | 0 | return 0; // compatibility | 760 | 0 | } | 761 | | | 762 | 14.4k | if constexpr (std::is_unsigned_v<T>) { | 763 | 14.4k | return pdns::checked_conv<T>(std::stoull(str, idx, base)); | 764 | | } | 765 | | else { | 766 | | return pdns::checked_conv<T>(std::stoll(str, idx, base)); | 767 | | } | 768 | 14.4k | } |
|
769 | | |
770 | | /** |
771 | | * \brief Performs a conversion from `std::string&` to non-zero integer. |
772 | | * |
773 | | * This function internally calls `checked_stoi` * |
774 | | * |
775 | | * \warning The target type `T` must be an integer, otherwise a |
776 | | * compilation error is thrown. |
777 | | * |
778 | | * \exception pdns::checked_stoi Throws what pdns::checked_stoi throws. |
779 | | * |
780 | | * \param[in] str The input string to be converted. |
781 | | * |
782 | | * \param[in] idx Location to store the index at which processing |
783 | | * stopped. If the input `str` is empty, `*idx` shall be set to 0. |
784 | | * |
785 | | * \param[in] base The numerical base for conversion. |
786 | | * |
787 | | * \return `str` converted to non-zero integer `T`. |
788 | | */ |
789 | | template <typename T> |
790 | | auto checked_stoi_nonzero(const std::string& str, size_t* idx = nullptr, int base = 10) -> T |
791 | | { |
792 | | T ret = checked_stoi<T>(str, idx, base); |
793 | | if (ret == 0) { |
794 | | throw std::out_of_range("checked_stoi_nonzero: value cannot be 0"); |
795 | | } |
796 | | return ret; |
797 | | } |
798 | | |
799 | | /** |
800 | | * \brief Performs a conversion from `std::string&` to integer. |
801 | | * |
802 | | * This function internally calls `pdns::checked_stoi` and stores its |
803 | | * result in `out`. |
804 | | * |
805 | | * \exception pdns::checked_stoi Throws what pdns::checked_stoi throws. |
806 | | * |
807 | | * \param[out] out `str` converted to integer `T`, or 0 if `str` is |
808 | | * empty. |
809 | | * |
810 | | * \param[in] str The input string to be converted. |
811 | | * |
812 | | * \param[in] idx Location to store the index at which processing |
813 | | * stopped. If the input `str` is empty, `*idx` shall be set to 0. |
814 | | * |
815 | | * \param[in] base The numerical base for conversion. |
816 | | * |
817 | | * \return `str` converted to integer `T`, or 0 if `str` is empty. |
818 | | */ |
819 | | template <typename T> |
820 | | auto checked_stoi_into(T& out, const std::string& str, size_t* idx = nullptr, int base = 10) |
821 | 392k | { |
822 | 392k | out = checked_stoi<T>(str, idx, base); |
823 | 392k | } 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 | 821 | 9.41k | { | 822 | 9.41k | out = checked_stoi<T>(str, idx, base); | 823 | 9.41k | } |
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) Line | Count | Source | 821 | 383k | { | 822 | 383k | out = checked_stoi<T>(str, idx, base); | 823 | 383k | } |
|
824 | | |
825 | | /** |
826 | | * \brief execute a lambda on scope exit |
827 | | * |
828 | | * This struct allows the execution of a lambda when the instantiation goes out of scope. |
829 | | * |
830 | | * Usage: |
831 | | * void bla() { |
832 | | * // Create the defer object |
833 | | * auto run_on_exit = pdns::defer([] { |
834 | | * // do this on scope exit |
835 | | * printf("goodbye"); |
836 | | * }); |
837 | | * // do things |
838 | | * printf("hello"); |
839 | | * |
840 | | * // Now the deferred lambda will be run and print "goodbye" |
841 | | * } |
842 | | */ |
843 | | template <class Func> |
844 | | struct defer |
845 | | { |
846 | | defer(Func&& func) : |
847 | | d_func(std::forward<Func>(std::move(func))) {}; |
848 | | ~defer() noexcept { std::invoke(d_func); } |
849 | | |
850 | | defer(const defer&) = delete; |
851 | | defer& operator=(const defer&) = delete; |
852 | | defer(defer&&) = delete; |
853 | | defer& operator=(defer&&) = delete; |
854 | | |
855 | | private: |
856 | | Func d_func; |
857 | | }; |
858 | | |
859 | | /** |
860 | | * \brief execute a function with arguments on scope exit |
861 | | * |
862 | | * This struct allows the execution of a function when the instantiation goes out of scope. |
863 | | * |
864 | | * Usage: |
865 | | * void bla() { |
866 | | * // Create the defer object |
867 | | * auto run_on_exit = pdns::deferFunc(printf, "goodbye"); |
868 | | * |
869 | | * // do things |
870 | | * printf("hello"); |
871 | | * |
872 | | * // Now the deferred function will be run and print "goodbye" |
873 | | * } |
874 | | */ |
875 | | template <class Func, typename... Args> |
876 | | struct deferFunc |
877 | | { |
878 | | deferFunc(Func&& func, Args&&... args) : |
879 | | d_func(std::forward<Func>(std::move(func))), d_args(std::forward<Args>(args)...) {}; |
880 | | ~deferFunc() noexcept { std::invoke(d_func, d_args); } |
881 | | |
882 | | deferFunc(const deferFunc&) = delete; |
883 | | deferFunc& operator=(const deferFunc&) = delete; |
884 | | deferFunc(deferFunc&&) = delete; |
885 | | deferFunc& operator=(deferFunc&&) = delete; |
886 | | |
887 | | private: |
888 | | Func d_func; |
889 | | std::tuple<Args...> d_args; |
890 | | }; |
891 | | |
892 | | long sysconf(int key, long default_value); |
893 | | } |
894 | | |
895 | | bool isSettingThreadCPUAffinitySupported(); |
896 | | int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus); |
897 | | |
898 | | std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath); |
899 | | |
900 | | DNSName reverseNameFromIP(const ComboAddress& address); |
901 | | |
902 | | // The following three routines are generated from Ragel code. |
903 | | // Note that parseRFC1035CharString will return zero if the first character |
904 | | // being processed is < 0x20, >= 07f, or equal to 0x28, 0x29 or 0x3b. |
905 | | // parseRFC1035CharStringRelaxed will too, except within a quoted section. |
906 | | size_t parseRFC1035CharString(std::string_view str, std::string& val); |
907 | | size_t parseRFC1035CharStringRelaxed(std::string_view str, std::string& val); |
908 | | size_t parseSVCBValueListFromParsedRFC1035CharString(const std::string& str, vector<std::string>& val); |
909 | | size_t parseSVCBValueList(const std::string& str, vector<std::string>& val); |
910 | | |
911 | | std::string makeLuaString(const std::string& input); |
912 | | |
913 | | bool constantTimeStringEquals(const std::string& lhs, const std::string& rhs); |
914 | | |
915 | | // Used in NID and L64 records |
916 | | struct NodeOrLocatorID |
917 | | { |
918 | | std::array<uint8_t, 8> content; |
919 | | }; |
920 | | |
921 | | struct FDWrapper |
922 | | { |
923 | | FDWrapper() = default; |
924 | | FDWrapper(int desc) : |
925 | 0 | d_fd(desc) {} |
926 | | FDWrapper(const FDWrapper&) = delete; |
927 | | FDWrapper& operator=(const FDWrapper& rhs) = delete; |
928 | | |
929 | | ~FDWrapper() |
930 | 0 | { |
931 | 0 | reset(); |
932 | 0 | } |
933 | | |
934 | | FDWrapper(FDWrapper&& rhs) noexcept : |
935 | | d_fd(rhs.d_fd) |
936 | 0 | { |
937 | 0 | rhs.d_fd = -1; |
938 | 0 | } |
939 | | |
940 | | FDWrapper& operator=(FDWrapper&& rhs) noexcept |
941 | 0 | { |
942 | 0 | if (d_fd >= 0) { |
943 | 0 | close(d_fd); |
944 | 0 | } |
945 | 0 | d_fd = rhs.d_fd; |
946 | 0 | rhs.d_fd = -1; |
947 | 0 | return *this; |
948 | 0 | } |
949 | | |
950 | | [[nodiscard]] int getHandle() const |
951 | 0 | { |
952 | 0 | return d_fd; |
953 | 0 | } |
954 | | |
955 | | operator int() const |
956 | 0 | { |
957 | 0 | return d_fd; |
958 | 0 | } |
959 | | |
960 | | int reset() |
961 | 0 | { |
962 | 0 | int ret = 0; |
963 | 0 | if (d_fd >= 0) { // NOLINT(clang-analyzer-core.UndefinedBinaryOperatorResult): obvious false positive |
964 | 0 | ret = close(d_fd); |
965 | 0 | } |
966 | 0 | d_fd = -1; |
967 | 0 | return ret; |
968 | 0 | } |
969 | | |
970 | | int release() |
971 | 0 | { |
972 | 0 | auto ret = d_fd; |
973 | 0 | d_fd = -1; |
974 | 0 | return ret; |
975 | 0 | } |
976 | | |
977 | | private: |
978 | | int d_fd{-1}; |
979 | | }; |
980 | | |
981 | | namespace pdns |
982 | | { |
983 | | [[nodiscard]] std::optional<std::string> visit_directory(const std::string& directory, const std::function<bool(ino_t inodeNumber, const std::string_view& name)>& visitor); |
984 | | |
985 | | #ifndef DNSDIST |
986 | | std::vector<std::string> list_directory(const std::string& directory, const std::string& suffix, Logr::log_t d_log); |
987 | | #endif |
988 | | |
989 | | struct FilePtrDeleter |
990 | | { |
991 | | /* using a deleter instead of decltype(&fclose) has two big advantages: |
992 | | - the deleter is included in the type and does not have to be passed |
993 | | when creating a new object (easier to use, less memory usage, in theory |
994 | | better inlining) |
995 | | - we avoid the annoying "ignoring attributes on template argument ‘int (*)(FILE*)’" |
996 | | warning from the compiler, which is there because fclose is tagged as __nonnull((1)) |
997 | | */ |
998 | | void operator()(FILE* filePtr) const noexcept |
999 | 0 | { |
1000 | 0 | fclose(filePtr); // NOLINT(cppcoreguidelines-owning-memory) |
1001 | 0 | } |
1002 | | }; |
1003 | | |
1004 | | using UniqueFilePtr = std::unique_ptr<FILE, FilePtrDeleter>; |
1005 | | |
1006 | | UniqueFilePtr openFileForWriting(const std::string& filePath, mode_t permissions, bool mustNotExist = true, bool appendIfExists = false); |
1007 | | } |
1008 | | |
1009 | | using timebuf_t = std::array<char, 64>; |
1010 | | const char* timestamp(time_t arg, timebuf_t& buf); |