]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/misc.cc
dnsdist: Fix DNS over plain HTTP broken by `reloadAllCertificates()`
[thirdparty/pdns.git] / pdns / misc.cc
CommitLineData
12c86877 1/*
12471842
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
1f63a8c3 22
870a0fe4
AT
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
1f63a8c3 26
705f31ae 27#include <sys/param.h>
22cf1fda 28#include <sys/socket.h>
3897b9e1 29#include <fcntl.h>
705f31ae
BH
30#include <netdb.h>
31#include <sys/time.h>
1f63a8c3 32#include <ctime>
3a8a4d68 33#include <sys/resource.h>
705f31ae 34#include <netinet/in.h>
76cb4593 35#include <sys/un.h>
705f31ae 36#include <unistd.h>
8290ad9f 37#include <fstream>
12c86877
BH
38#include "misc.hh"
39#include <vector>
66910c5d 40#include <string>
12c86877 41#include <sstream>
da78b86e 42#include <cerrno>
1258abe0 43#include <cstring>
c4bee46c 44#include <iostream>
a9b6db56 45#include <sys/types.h>
46#include <dirent.h>
705f31ae 47#include <algorithm>
fab091b9 48#include <poll.h>
12c86877 49#include <iomanip>
149d0144 50#include <netinet/tcp.h>
e24c61ed 51#include <optional>
1f63a8c3
FM
52#include <cstdlib>
53#include <cstdio>
5c409fa2 54#include "pdnsexception.hh"
040712e0 55#include <boost/algorithm/string.hpp>
a48e03da 56#include <boost/format.hpp>
65d8e171 57#include "iputils.hh"
e325f20c 58#include "dnsparser.hh"
3dc4644f 59#include "dns_random.hh"
ffb07158 60#include <pwd.h>
61#include <grp.h>
1f63a8c3 62#include <climits>
6c78a20e
R
63#ifdef __FreeBSD__
64# include <pthread_np.h>
65#endif
4d39d7f3
TIH
66#ifdef __NetBSD__
67# include <pthread.h>
68# include <sched.h>
69#endif
a61e8e59 70
66910c5d
FM
71#if defined(HAVE_LIBCRYPTO)
72#include <openssl/err.h>
73#endif // HAVE_LIBCRYPTO
74
d2adfa5c 75size_t writen2(int fileDesc, const void *buf, size_t count)
040712e0 76{
d2adfa5c 77 const char *ptr = static_cast<const char*>(buf);
040712e0 78 const char *eptr = ptr + count;
3ddb9247 79
d2adfa5c
OM
80 while (ptr != eptr) {
81 auto res = ::write(fileDesc, ptr, eptr - ptr);
82 if (res < 0) {
83 if (errno == EAGAIN) {
4957a608 84 throw std::runtime_error("used writen2 on non-blocking socket, got EAGAIN");
d2adfa5c
OM
85 }
86 unixDie("failed in writen2");
040712e0 87 }
d2adfa5c 88 else if (res == 0) {
040712e0 89 throw std::runtime_error("could not write all bytes, got eof in writen2");
d2adfa5c 90 }
3ddb9247 91
d2adfa5c 92 ptr += res;
040712e0 93 }
3ddb9247 94
040712e0
BH
95 return count;
96}
97
a683e8bd 98size_t readn2(int fd, void* buffer, size_t len)
36fbf590 99{
a683e8bd
RG
100 size_t pos=0;
101 ssize_t res;
36fbf590 102 for(;;) {
103 res = read(fd, (char*)buffer + pos, len - pos);
3ddb9247 104 if(res == 0)
3f6d07a4 105 throw runtime_error("EOF while reading message");
36fbf590 106 if(res < 0) {
107 if (errno == EAGAIN)
3f6d07a4 108 throw std::runtime_error("used readn2 on non-blocking socket, got EAGAIN");
36fbf590 109 else
3f6d07a4 110 unixDie("failed in readn2");
3ddb9247
PD
111 }
112
a683e8bd 113 pos+=(size_t)res;
36fbf590 114 if(pos == len)
115 break;
116 }
117 return len;
118}
119
1342b949 120size_t readn2WithTimeout(int fd, void* buffer, size_t len, const struct timeval& idleTimeout, const struct timeval& totalTimeout, bool allowIncomplete)
3f6d07a4
RG
121{
122 size_t pos = 0;
50111728
O
123 struct timeval start{0,0};
124 struct timeval remainingTime = totalTimeout;
125 if (totalTimeout.tv_sec != 0 || totalTimeout.tv_usec != 0) {
126 gettimeofday(&start, nullptr);
9396d955
RG
127 }
128
3f6d07a4
RG
129 do {
130 ssize_t got = read(fd, (char *)buffer + pos, len - pos);
131 if (got > 0) {
132 pos += (size_t) got;
1342b949
RG
133 if (allowIncomplete) {
134 break;
135 }
3f6d07a4
RG
136 }
137 else if (got == 0) {
138 throw runtime_error("EOF while reading message");
139 }
140 else {
141 if (errno == EAGAIN) {
50111728
O
142 struct timeval w = ((totalTimeout.tv_sec == 0 && totalTimeout.tv_usec == 0) || idleTimeout <= remainingTime) ? idleTimeout : remainingTime;
143 int res = waitForData(fd, w.tv_sec, w.tv_usec);
3f6d07a4
RG
144 if (res > 0) {
145 /* there is data available */
146 }
147 else if (res == 0) {
148 throw runtime_error("Timeout while waiting for data to read");
149 } else {
150 throw runtime_error("Error while waiting for data to read");
151 }
152 }
153 else {
154 unixDie("failed in readn2WithTimeout");
155 }
156 }
9396d955 157
50111728
O
158 if (totalTimeout.tv_sec != 0 || totalTimeout.tv_usec != 0) {
159 struct timeval now;
160 gettimeofday(&now, nullptr);
161 struct timeval elapsed = now - start;
162 if (remainingTime < elapsed) {
9396d955
RG
163 throw runtime_error("Timeout while reading data");
164 }
165 start = now;
50111728 166 remainingTime = remainingTime - elapsed;
9396d955 167 }
3f6d07a4
RG
168 }
169 while (pos < len);
170
171 return len;
172}
173
50111728 174size_t writen2WithTimeout(int fd, const void * buffer, size_t len, const struct timeval& timeout)
3f6d07a4
RG
175{
176 size_t pos = 0;
177 do {
f1b2ae9b 178 ssize_t written = write(fd, reinterpret_cast<const char *>(buffer) + pos, len - pos);
3f6d07a4
RG
179
180 if (written > 0) {
181 pos += (size_t) written;
182 }
183 else if (written == 0)
184 throw runtime_error("EOF while writing message");
185 else {
186 if (errno == EAGAIN) {
50111728 187 int res = waitForRWData(fd, false, timeout.tv_sec, timeout.tv_usec);
3f6d07a4
RG
188 if (res > 0) {
189 /* there is room available */
190 }
191 else if (res == 0) {
192 throw runtime_error("Timeout while waiting to write data");
193 } else {
194 throw runtime_error("Error while waiting for room to write data");
195 }
196 }
197 else {
198 unixDie("failed in write2WithTimeout");
199 }
200 }
201 }
202 while (pos < len);
203
204 return len;
205}
12c86877 206
da78b86e
FM
207auto pdns::getMessageFromErrno(const int errnum) -> std::string
208{
209 const size_t errLen = 2048;
210 std::string errMsgData{};
211 errMsgData.resize(errLen);
212
213 const char* errMsg = nullptr;
60c8f3f5 214#ifdef STRERROR_R_CHAR_P
da78b86e
FM
215 errMsg = strerror_r(errnum, errMsgData.data(), errMsgData.length());
216#else
217 // This can fail, and when it does, it sets errno. We ignore that and
218 // set our own error message instead.
219 int res = strerror_r(errnum, errMsgData.data(), errMsgData.length());
220 errMsg = errMsgData.c_str();
221 if (res != 0) {
222 errMsg = "Unknown (the exact error could not be retrieved)";
223 }
224#endif
225
226 // We make a copy here because `strerror_r()` might return a static
227 // immutable buffer for an error message. The copy shouldn't be
228 // critical though, we're on the bailout/error-handling path anyways.
229 std::string message{errMsg};
230 return message;
231}
232
66910c5d
FM
233#if defined(HAVE_LIBCRYPTO)
234auto pdns::OpenSSL::error(const std::string& errorMessage) -> std::runtime_error
235{
236 unsigned long errorCode = 0;
237 auto fullErrorMessage{errorMessage};
238#if OPENSSL_VERSION_MAJOR >= 3
239 const char* filename = nullptr;
240 const char* functionName = nullptr;
241 int lineNumber = 0;
242 while ((errorCode = ERR_get_error_all(&filename, &lineNumber, &functionName, nullptr, nullptr)) != 0) {
243 fullErrorMessage += std::string(": ") + std::to_string(errorCode);
244
245 const auto* lib = ERR_lib_error_string(errorCode);
246 if (lib != nullptr) {
247 fullErrorMessage += std::string(":") + lib;
248 }
249
250 const auto* reason = ERR_reason_error_string(errorCode);
251 if (reason != nullptr) {
252 fullErrorMessage += std::string("::") + reason;
253 }
254
255 if (filename != nullptr) {
256 fullErrorMessage += std::string(" - ") + filename;
257 }
258 if (lineNumber != 0) {
259 fullErrorMessage += std::string(":") + std::to_string(lineNumber);
260 }
261 if (functionName != nullptr) {
262 fullErrorMessage += std::string(" - ") + functionName;
263 }
264 }
265#else
266 while ((errorCode = ERR_get_error()) != 0) {
267 fullErrorMessage += std::string(": ") + std::to_string(errorCode);
268
269 const auto* lib = ERR_lib_error_string(errorCode);
270 if (lib != nullptr) {
271 fullErrorMessage += std::string(":") + lib;
272 }
273
274 const auto* func = ERR_func_error_string(errorCode);
275 if (func != nullptr) {
276 fullErrorMessage += std::string(":") + func;
277 }
278
279 const auto* reason = ERR_reason_error_string(errorCode);
280 if (reason != nullptr) {
281 fullErrorMessage += std::string("::") + reason;
282 }
283 }
284#endif
285 return std::runtime_error(fullErrorMessage);
286}
287
288auto pdns::OpenSSL::error(const std::string& componentName, const std::string& errorMessage) -> std::runtime_error
289{
290 return pdns::OpenSSL::error(componentName + ": " + errorMessage);
291}
292#endif // HAVE_LIBCRYPTO
293
cc3afe25
BH
294string nowTime()
295{
74ab661c 296 time_t now = time(nullptr);
d2adfa5c
OM
297 struct tm theTime{};
298 localtime_r(&now, &theTime);
299 std::array<char, 30> buffer{};
74ab661c 300 // YYYY-mm-dd HH:MM:SS TZOFF
d2adfa5c 301 size_t ret = strftime(buffer.data(), buffer.size(), "%F %T %z", &theTime);
d291045e
OM
302 if (ret == 0) {
303 buffer[0] = '\0';
304 }
d2adfa5c 305 return {buffer.data()};
b636533b
BH
306}
307
d2adfa5c 308static bool ciEqual(const string& lhs, const string& rhs)
49bd5a20 309{
d2adfa5c 310 if (lhs.size() != rhs.size()) {
49bd5a20 311 return false;
d2adfa5c 312 }
49bd5a20 313
d2adfa5c
OM
314 string::size_type pos = 0;
315 const string::size_type epos = lhs.size();
316 for (; pos < epos; ++pos) {
317 if (dns_tolower(lhs[pos]) != dns_tolower(rhs[pos])) {
49bd5a20 318 return false;
d2adfa5c
OM
319 }
320 }
49bd5a20
BH
321 return true;
322}
323
728485ca 324/** does domain end on suffix? Is smart about "wwwds9a.nl" "ds9a.nl" not matching */
a683e8bd 325static bool endsOn(const string &domain, const string &suffix)
728485ca 326{
d2adfa5c 327 if( suffix.empty() || ciEqual(domain, suffix) ) {
728485ca 328 return true;
d2adfa5c 329 }
7738a23f 330
d2adfa5c 331 if(domain.size() <= suffix.size()) {
728485ca 332 return false;
d2adfa5c 333 }
3ddb9247 334
d2adfa5c
OM
335 string::size_type dpos = domain.size() - suffix.size() - 1;
336 string::size_type spos = 0;
7738a23f 337
d2adfa5c 338 if (domain[dpos++] != '.') {
49bd5a20 339 return false;
d2adfa5c 340 }
728485ca 341
d2adfa5c
OM
342 for(; dpos < domain.size(); ++dpos, ++spos) {
343 if (dns_tolower(domain[dpos]) != dns_tolower(suffix[spos])) {
49bd5a20 344 return false;
d2adfa5c
OM
345 }
346 }
49bd5a20
BH
347
348 return true;
349}
f2c11a48 350
a683e8bd
RG
351/** strips a domain suffix from a domain, returns true if it stripped */
352bool stripDomainSuffix(string *qname, const string &domain)
353{
d2adfa5c 354 if (!endsOn(*qname, domain)) {
a683e8bd 355 return false;
d2adfa5c 356 }
a683e8bd 357
d2adfa5c 358 if (toLower(*qname) == toLower(domain)) {
a683e8bd 359 *qname="@";
d2adfa5c 360 }
a683e8bd 361 else {
d2adfa5c 362 if ((*qname)[qname->size() - domain.size() - 1] != '.') {
a683e8bd 363 return false;
d2adfa5c 364 }
a683e8bd 365
d2adfa5c 366 qname->resize(qname->size() - domain.size()-1);
a683e8bd
RG
367 }
368 return true;
369}
370
1fb3f0fc 371// returns -1 in case if error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
d2adfa5c 372int waitForData(int fileDesc, int seconds, int useconds)
649a88df 373{
d2adfa5c 374 return waitForRWData(fileDesc, true, seconds, useconds);
649a88df
BH
375}
376
d2adfa5c 377int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error, bool* disconnected)
12c86877 378{
d2adfa5c 379 struct pollfd pfd{};
fab091b9 380 memset(&pfd, 0, sizeof(pfd));
d2adfa5c 381 pfd.fd = fileDesc;
3ddb9247 382
d2adfa5c
OM
383 if (waitForRead) {
384 pfd.events = POLLIN;
385 }
386 else {
387 pfd.events = POLLOUT;
388 }
1258abe0 389
d2adfa5c 390 int ret = poll(&pfd, 1, seconds * 1000 + useconds/1000);
e07c3801 391 if (ret > 0) {
d2adfa5c 392 if ((error != nullptr) && (pfd.revents & POLLERR) != 0) {
51959320
RG
393 *error = true;
394 }
d2adfa5c 395 if ((disconnected != nullptr) && (pfd.revents & POLLHUP) != 0) {
51959320
RG
396 *disconnected = true;
397 }
398 }
1258abe0 399
12c86877
BH
400 return ret;
401}
402
a71bee29 403// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
dc058f65 404int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fdOut) {
d529011f
PL
405 set<int> realFDs;
406 for (const auto& fd : fds) {
407 if (fd >= 0 && realFDs.count(fd) == 0) {
408 realFDs.insert(fd);
409 }
410 }
411
dcc0e65d 412 std::vector<struct pollfd> pfds(realFDs.size());
c2a16005 413 memset(pfds.data(), 0, realFDs.size()*sizeof(struct pollfd));
d529011f
PL
414 int ctr = 0;
415 for (const auto& fd : realFDs) {
416 pfds[ctr].fd = fd;
417 pfds[ctr].events = POLLIN;
418 ctr++;
419 }
420
421 int ret;
422 if(seconds >= 0)
c2a16005 423 ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000);
d529011f 424 else
c2a16005 425 ret = poll(pfds.data(), realFDs.size(), -1);
d529011f
PL
426 if(ret <= 0)
427 return ret;
428
429 set<int> pollinFDs;
aca9c605
PL
430 for (const auto& pfd : pfds) {
431 if (pfd.revents & POLLIN) {
432 pollinFDs.insert(pfd.fd);
d529011f 433 }
d529011f
PL
434 }
435 set<int>::const_iterator it(pollinFDs.begin());
3dc4644f 436 advance(it, dns_random(pollinFDs.size()));
dc058f65 437 *fdOut = *it;
d529011f
PL
438 return 1;
439}
440
a71bee29 441// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
f668f576 442int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr)
f4ff5929 443{
f668f576
OM
444 std::array<pollfd,2> pfds{};
445 memset(pfds.data(), 0, pfds.size() * sizeof(struct pollfd));
f4ff5929
BH
446 pfds[0].fd = fd1;
447 pfds[1].fd = fd2;
3ddb9247 448
f4ff5929
BH
449 pfds[0].events= pfds[1].events = POLLIN;
450
f668f576 451 int nsocks = 1 + static_cast<int>(fd2 >= 0); // fd2 can optionally be -1
f4ff5929 452
f668f576
OM
453 int ret{};
454 if (seconds >= 0) {
455 ret = poll(pfds.data(), nsocks, seconds * 1000 + useconds / 1000);
456 }
457 else {
458 ret = poll(pfds.data(), nsocks, -1);
459 }
460 if (ret <= 0) {
f4ff5929 461 return ret;
f668f576 462 }
3ddb9247 463
f668f576
OM
464 if ((pfds[0].revents & POLLIN) != 0 && (pfds[1].revents & POLLIN) == 0) {
465 *fdPtr = pfds[0].fd;
466 }
467 else if ((pfds[1].revents & POLLIN) != 0 && (pfds[0].revents & POLLIN) == 0) {
468 *fdPtr = pfds[1].fd;
469 }
f4ff5929 470 else if(ret == 2) {
f668f576
OM
471 *fdPtr = pfds.at(dns_random_uint32() % 2).fd;
472 }
473 else {
474 *fdPtr = -1; // should never happen
f4ff5929 475 }
3ddb9247 476
f4ff5929
BH
477 return 1;
478}
479
12c86877
BH
480
481string humanDuration(time_t passed)
482{
483 ostringstream ret;
484 if(passed<60)
485 ret<<passed<<" seconds";
486 else if(passed<3600)
9e04108d 487 ret<<std::setprecision(2)<<passed/60.0<<" minutes";
12c86877 488 else if(passed<86400)
9e04108d 489 ret<<std::setprecision(3)<<passed/3600.0<<" hours";
12c86877 490 else if(passed<(86400*30.41))
9e04108d 491 ret<<std::setprecision(3)<<passed/86400.0<<" days";
12c86877 492 else
9e04108d 493 ret<<std::setprecision(3)<<passed/(86400*30.41)<<" months";
12c86877
BH
494
495 return ret.str();
496}
497
d2adfa5c 498string unquotify(const string &item)
1d329048
BH
499{
500 if(item.size()<2)
501 return item;
502
503 string::size_type bpos=0, epos=item.size();
12c86877 504
3ddb9247 505 if(item[0]=='"')
1d329048 506 bpos=1;
c4bee46c 507
1d329048
BH
508 if(item[epos-1]=='"')
509 epos-=1;
510
c4bee46c 511 return item.substr(bpos,epos-bpos);
1d329048 512}
12c86877
BH
513
514void stripLine(string &line)
515{
bdf40704 516 string::size_type pos=line.find_first_of("\r\n");
12c86877
BH
517 if(pos!=string::npos) {
518 line.resize(pos);
519 }
520}
521
522string urlEncode(const string &text)
523{
524 string ret;
d7f67000
RP
525 for(char i : text)
526 if(i==' ')ret.append("%20");
527 else ret.append(1,i);
12c86877
BH
528 return ret;
529}
530
ff32c991 531static size_t getMaxHostNameSize()
12c86877 532{
ff32c991
FM
533#if defined(HOST_NAME_MAX)
534 return HOST_NAME_MAX;
0db70140 535#endif
ac2bb9e7 536
ff32c991
FM
537#if defined(_SC_HOST_NAME_MAX)
538 auto tmp = sysconf(_SC_HOST_NAME_MAX);
539 if (tmp != -1) {
540 return tmp;
541 }
542#endif
12c86877 543
ff32c991
FM
544 const size_t maxHostNameSize = 255;
545 return maxHostNameSize;
546}
547
548std::optional<string> getHostname()
549{
550 const size_t maxHostNameBufSize = getMaxHostNameSize() + 1;
551 std::string hostname;
552 hostname.resize(maxHostNameBufSize, 0);
553
554 if (gethostname(hostname.data(), maxHostNameBufSize) == -1) {
555 return std::nullopt;
556 }
557
558 hostname.resize(strlen(hostname.c_str()));
559 return std::make_optional(hostname);
560}
561
562std::string getCarbonHostName()
563{
564 auto hostname = getHostname();
565 if (!hostname.has_value()) {
566 throw std::runtime_error(stringerror());
567 }
568
569 boost::replace_all(*hostname, ".", "_");
570 return *hostname;
12c86877
BH
571}
572
d88babea
KM
573string bitFlip(const string &str)
574{
575 string::size_type pos=0, epos=str.size();
576 string ret;
577 ret.reserve(epos);
578 for(;pos < epos; ++pos)
579 ret.append(1, ~str[pos]);
580 return ret;
581}
22c9c86a 582
12c86877
BH
583void cleanSlashes(string &str)
584{
12c86877 585 string out;
b289a3bb
RG
586 bool keepNextSlash = true;
587 for (const auto& value : str) {
588 if (value == '/') {
589 if (keepNextSlash) {
590 keepNextSlash = false;
591 }
592 else {
593 continue;
594 }
595 }
596 else {
597 keepNextSlash = true;
598 }
599 out.append(1, value);
12c86877 600 }
b289a3bb 601 str = std::move(out);
12c86877
BH
602}
603
092f210a 604bool IpToU32(const string &str, uint32_t *ip)
525b8a7c 605{
4cf26303
BH
606 if(str.empty()) {
607 *ip=0;
608 return true;
609 }
3ddb9247 610
092c9cc4 611 struct in_addr inp;
3897b9e1 612 if(inet_aton(str.c_str(), &inp)) {
092c9cc4
BH
613 *ip=inp.s_addr;
614 return true;
615 }
616 return false;
525b8a7c
BH
617}
618
5730f30c
BH
619string U32ToIP(uint32_t val)
620{
621 char tmp[17];
9b2244e1 622 snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
4957a608
BH
623 (val >> 24)&0xff,
624 (val >> 16)&0xff,
625 (val >> 8)&0xff,
626 (val )&0xff);
bfbf6814 627 return string(tmp);
5730f30c
BH
628}
629
37d3f960 630
2db9c30e
BH
631string makeHexDump(const string& str)
632{
8c7a1b8a 633 std::array<char, 5> tmp;
2db9c30e 634 string ret;
8c7a1b8a 635 ret.reserve(static_cast<size_t>(str.size()*2.2));
2db9c30e 636
8c7a1b8a
RG
637 for (char n : str) {
638 snprintf(tmp.data(), tmp.size(), "%02x ", static_cast<unsigned char>(n));
639 ret += tmp.data();
2db9c30e
BH
640 }
641 return ret;
642}
643
a02d0fa6
PL
644string makeBytesFromHex(const string &in) {
645 if (in.size() % 2 != 0) {
646 throw std::range_error("odd number of bytes in hex string");
647 }
648 string ret;
50953de8
RG
649 ret.reserve(in.size() / 2);
650
50953de8
RG
651 for (size_t i = 0; i < in.size(); i += 2) {
652 const auto numStr = in.substr(i, 2);
7f73a566 653 unsigned int num = 0;
50953de8
RG
654 if (sscanf(numStr.c_str(), "%02x", &num) != 1) {
655 throw std::range_error("Invalid value while parsing the hex string '" + in + "'");
656 }
657 ret.push_back(static_cast<uint8_t>(num));
a02d0fa6 658 }
50953de8 659
a02d0fa6
PL
660 return ret;
661}
662
88358c9b
BH
663void normalizeTV(struct timeval& tv)
664{
665 if(tv.tv_usec > 1000000) {
666 ++tv.tv_sec;
667 tv.tv_usec-=1000000;
668 }
669 else if(tv.tv_usec < 0) {
670 --tv.tv_sec;
671 tv.tv_usec+=1000000;
672 }
673}
674
d2adfa5c 675struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs)
88358c9b
BH
676{
677 struct timeval ret;
678 ret.tv_sec=lhs.tv_sec + rhs.tv_sec;
679 ret.tv_usec=lhs.tv_usec + rhs.tv_usec;
680 normalizeTV(ret);
681 return ret;
682}
683
d2adfa5c 684struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs)
88358c9b
BH
685{
686 struct timeval ret;
687 ret.tv_sec=lhs.tv_sec - rhs.tv_sec;
688 ret.tv_usec=lhs.tv_usec - rhs.tv_usec;
689 normalizeTV(ret);
690 return ret;
691}
50c79a76
BH
692
693pair<string, string> splitField(const string& inp, char sepa)
694{
695 pair<string, string> ret;
696 string::size_type cpos=inp.find(sepa);
697 if(cpos==string::npos)
698 ret.first=inp;
699 else {
700 ret.first=inp.substr(0, cpos);
701 ret.second=inp.substr(cpos+1);
702 }
703 return ret;
704}
6e2514e4 705
f8499e52 706int logFacilityToLOG(unsigned int facility)
6e2514e4 707{
6e2514e4
BH
708 switch(facility) {
709 case 0:
710 return LOG_LOCAL0;
711 case 1:
712 return(LOG_LOCAL1);
713 case 2:
714 return(LOG_LOCAL2);
715 case 3:
716 return(LOG_LOCAL3);
717 case 4:
718 return(LOG_LOCAL4);
719 case 5:
720 return(LOG_LOCAL5);
721 case 6:
722 return(LOG_LOCAL6);
723 case 7:
724 return(LOG_LOCAL7);
725 default:
f8499e52 726 return -1;
6e2514e4
BH
727 }
728}
da042e6e
BH
729
730string stripDot(const string& dom)
731{
732 if(dom.empty())
733 return dom;
734
735 if(dom[dom.size()-1]!='.')
736 return dom;
737
738 return dom.substr(0,dom.size()-1);
739}
4dadd22f 740
f71bc087
BH
741int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
742{
1a736a1a 743 if (addr.empty()) {
85db02c5 744 return -1;
1a736a1a
FM
745 }
746
85db02c5 747 string ourAddr(addr);
1a736a1a
FM
748 std::optional<uint16_t> port = std::nullopt;
749
3a9b5bc5 750 if (addr[0] == '[') { // [::]:53 style address
85db02c5 751 string::size_type pos = addr.find(']');
1a736a1a 752 if (pos == string::npos) {
85db02c5 753 return -1;
1a736a1a
FM
754 }
755
3a9b5bc5 756 ourAddr.assign(addr.c_str() + 1, pos - 1);
ed2ff96e 757 if (pos + 1 != addr.size()) { // complete after ], no port specified
1a736a1a 758 if (pos + 2 > addr.size() || addr[pos + 1] != ':') {
0047d734 759 return -1;
1a736a1a
FM
760 }
761
0047d734 762 try {
1a736a1a
FM
763 auto tmpPort = pdns::checked_stoi<uint16_t>(addr.substr(pos + 2));
764 port = std::make_optional(tmpPort);
0047d734 765 }
3a9b5bc5 766 catch (const std::out_of_range&) {
0047d734
CH
767 return -1;
768 }
5b4ed369 769 }
85db02c5 770 }
1a736a1a 771
3a9b5bc5
FM
772 ret->sin6_scope_id = 0;
773 ret->sin6_family = AF_INET6;
39d2d9b2 774
3a9b5bc5 775 if (inet_pton(AF_INET6, ourAddr.c_str(), (void*)&ret->sin6_addr) != 1) {
1a736a1a
FM
776 struct addrinfo hints{};
777 std::memset(&hints, 0, sizeof(struct addrinfo));
0c9de4fc 778 hints.ai_flags = AI_NUMERICHOST;
1a736a1a 779 hints.ai_family = AF_INET6;
3ddb9247 780
1a736a1a 781 struct addrinfo* res = nullptr;
1746e9bb 782 // getaddrinfo has anomalous return codes, anything nonzero is an error, positive or negative
4646277d 783 if (getaddrinfo(ourAddr.c_str(), nullptr, &hints, &res) != 0) {
0c9de4fc 784 return -1;
785 }
3ddb9247 786
0c9de4fc 787 memcpy(ret, res->ai_addr, res->ai_addrlen);
788 freeaddrinfo(res);
f71bc087 789 }
0c9de4fc 790
1a736a1a
FM
791 if (port.has_value()) {
792 ret->sin6_port = htons(*port);
6cbfa73b 793 }
0c9de4fc 794
f71bc087
BH
795 return 0;
796}
834942f1 797
76cb4593 798int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret)
85db02c5
BH
799{
800 if(str.empty()) {
801 return -1;
802 }
803 struct in_addr inp;
3ddb9247 804
85db02c5
BH
805 string::size_type pos = str.find(':');
806 if(pos == string::npos) { // no port specified, not touching the port
3897b9e1 807 if(inet_aton(str.c_str(), &inp)) {
85db02c5
BH
808 ret->sin_addr.s_addr=inp.s_addr;
809 return 0;
810 }
811 return -1;
812 }
813 if(!*(str.c_str() + pos + 1)) // trailing :
3ddb9247
PD
814 return -1;
815
f1b2ae9b 816 char *eptr = const_cast<char*>(str.c_str()) + str.size();
85db02c5 817 int port = strtol(str.c_str() + pos + 1, &eptr, 10);
5b4ed369
PL
818 if (port < 0 || port > 65535)
819 return -1;
820
85db02c5
BH
821 if(*eptr)
822 return -1;
3ddb9247 823
85db02c5 824 ret->sin_port = htons(port);
3897b9e1 825 if(inet_aton(str.substr(0, pos).c_str(), &inp)) {
85db02c5
BH
826 ret->sin_addr.s_addr=inp.s_addr;
827 return 0;
828 }
829 return -1;
830}
831
76cb4593
CH
832int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret)
833{
834 if (path.empty())
835 return -1;
836
837 memset(ret, 0, sizeof(struct sockaddr_un));
838 ret->sun_family = AF_UNIX;
839 if (path.length() >= sizeof(ret->sun_path))
840 return -1;
841
842 path.copy(ret->sun_path, sizeof(ret->sun_path), 0);
843 return 0;
844}
85db02c5 845
834942f1
BH
846//! read a line of text from a FILE* to a std::string, returns false on 'no data'
847bool stringfgets(FILE* fp, std::string& line)
848{
849 char buffer[1024];
850 line.clear();
3ddb9247 851
834942f1
BH
852 do {
853 if(!fgets(buffer, sizeof(buffer), fp))
854 return !line.empty();
3ddb9247
PD
855
856 line.append(buffer);
834942f1
BH
857 } while(!strchr(buffer, '\n'));
858 return true;
859}
e611a06c 860
4e9a20e6 861bool readFileIfThere(const char* fname, std::string* line)
862{
863 line->clear();
114b8796
RG
864 auto filePtr = pdns::UniqueFilePtr(fopen(fname, "r"));
865 if (!filePtr) {
4e9a20e6 866 return false;
11917b03 867 }
114b8796 868 return stringfgets(filePtr.get(), *line);
4e9a20e6 869}
870
3ee84c3f
BH
871Regex::Regex(const string &expr)
872{
873 if(regcomp(&d_preg, expr.c_str(), REG_ICASE|REG_NOSUB|REG_EXTENDED))
3f81d239 874 throw PDNSException("Regular expression did not compile");
3ee84c3f 875}
65d8e171 876
25ba7344
PD
877// if you end up here because valgrind told you were are doing something wrong
878// with msgh->msg_controllen, please refer to https://github.com/PowerDNS/pdns/pull/3962
879// first.
7bec330a 880// Note that cmsgbuf should be aligned the same as a struct cmsghdr
7bec330a 881void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cmsgbuf, const ComboAddress* source, int itfIndex)
65d8e171 882{
4646277d 883 struct cmsghdr *cmsg = nullptr;
65d8e171
KN
884
885 if(source->sin4.sin_family == AF_INET6) {
886 struct in6_pktinfo *pkt;
887
888 msgh->msg_control = cmsgbuf;
aeb011b7
RG
889#if !defined( __APPLE__ )
890 /* CMSG_SPACE is not a constexpr on macOS */
7f818542 891 static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in6_pktinfo");
aeb011b7
RG
892#else /* __APPLE__ */
893 if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
894 throw std::runtime_error("Buffer is too small for in6_pktinfo");
895 }
896#endif /* __APPLE__ */
65d8e171
KN
897 msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
898
899 cmsg = CMSG_FIRSTHDR(msgh);
900 cmsg->cmsg_level = IPPROTO_IPV6;
901 cmsg->cmsg_type = IPV6_PKTINFO;
902 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
903
904 pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
20b22895
OM
905 // Include the padding to stop valgrind complaining about passing uninitialized data
906 memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
65d8e171 907 pkt->ipi6_addr = source->sin6.sin6_addr;
fbe2a2e0 908 pkt->ipi6_ifindex = itfIndex;
65d8e171
KN
909 }
910 else {
4d39d7f3 911#if defined(IP_PKTINFO)
65d8e171
KN
912 struct in_pktinfo *pkt;
913
914 msgh->msg_control = cmsgbuf;
aeb011b7
RG
915#if !defined( __APPLE__ )
916 /* CMSG_SPACE is not a constexpr on macOS */
7f818542 917 static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in_pktinfo");
aeb011b7
RG
918#else /* __APPLE__ */
919 if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
920 throw std::runtime_error("Buffer is too small for in_pktinfo");
921 }
922#endif /* __APPLE__ */
65d8e171
KN
923 msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
924
925 cmsg = CMSG_FIRSTHDR(msgh);
926 cmsg->cmsg_level = IPPROTO_IP;
927 cmsg->cmsg_type = IP_PKTINFO;
928 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
929
930 pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
20b22895
OM
931 // Include the padding to stop valgrind complaining about passing uninitialized data
932 memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
65d8e171 933 pkt->ipi_spec_dst = source->sin4.sin_addr;
fbe2a2e0 934 pkt->ipi_ifindex = itfIndex;
4d39d7f3 935#elif defined(IP_SENDSRCADDR)
65d8e171
KN
936 struct in_addr *in;
937
938 msgh->msg_control = cmsgbuf;
aeb011b7 939#if !defined( __APPLE__ )
7f818542 940 static_assert(CMSG_SPACE(sizeof(*in)) <= sizeof(*cmsgbuf), "Buffer is too small for in_addr");
aeb011b7
RG
941#else /* __APPLE__ */
942 if (CMSG_SPACE(sizeof(*in)) > sizeof(*cmsgbuf)) {
943 throw std::runtime_error("Buffer is too small for in_addr");
944 }
945#endif /* __APPLE__ */
65d8e171
KN
946 msgh->msg_controllen = CMSG_SPACE(sizeof(*in));
947
948 cmsg = CMSG_FIRSTHDR(msgh);
949 cmsg->cmsg_level = IPPROTO_IP;
950 cmsg->cmsg_type = IP_SENDSRCADDR;
951 cmsg->cmsg_len = CMSG_LEN(sizeof(*in));
952
20b22895 953 // Include the padding to stop valgrind complaining about passing uninitialized data
65d8e171 954 in = (struct in_addr *) CMSG_DATA(cmsg);
20b22895 955 memset(in, 0, CMSG_SPACE(sizeof(*in)));
65d8e171 956 *in = source->sin4.sin_addr;
ecc8571f 957#endif
65d8e171
KN
958 }
959}
3a8a4d68 960
961unsigned int getFilenumLimit(bool hardOrSoft)
962{
963 struct rlimit rlim;
964 if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
965 unixDie("Requesting number of available file descriptors");
966 return hardOrSoft ? rlim.rlim_max : rlim.rlim_cur;
967}
968
969void setFilenumLimit(unsigned int lim)
970{
971 struct rlimit rlim;
972
973 if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
974 unixDie("Requesting number of available file descriptors");
975 rlim.rlim_cur=lim;
976 if(setrlimit(RLIMIT_NOFILE, &rlim) < 0)
977 unixDie("Setting number of available file descriptors");
978}
06ea9015 979
915b0c39 980bool setSocketTimestamps(int fd)
9ae194e2 981{
eb4c1792 982#ifdef SO_TIMESTAMP
9ae194e2 983 int on=1;
915b0c39 984 return setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, (char*)&on, sizeof(on)) == 0;
99be0a43 985#else
915b0c39 986 return true; // we pretend this happened.
99be0a43 987#endif
9ae194e2 988}
58044407 989
883a30a7 990bool setTCPNoDelay(int sock)
149d0144 991{
992 int flag = 1;
883a30a7
RG
993 return setsockopt(sock, /* socket affected */
994 IPPROTO_TCP, /* set option at TCP level */
995 TCP_NODELAY, /* name of option */
996 (char *) &flag, /* the cast is historical cruft */
997 sizeof(flag)) == 0; /* length of option value */
149d0144 998}
999
1000
3897b9e1 1001bool setNonBlocking(int sock)
1002{
3ddb9247 1003 int flags=fcntl(sock,F_GETFL,0);
3897b9e1 1004 if(flags<0 || fcntl(sock, F_SETFL,flags|O_NONBLOCK) <0)
1005 return false;
1006 return true;
1007}
1008
1009bool setBlocking(int sock)
1010{
3ddb9247 1011 int flags=fcntl(sock,F_GETFL,0);
3897b9e1 1012 if(flags<0 || fcntl(sock, F_SETFL,flags&(~O_NONBLOCK)) <0)
1013 return false;
1014 return true;
1015}
1016
bf676f2f
PL
1017bool setReuseAddr(int sock)
1018{
1019 int tmp = 1;
1020 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&tmp, static_cast<unsigned>(sizeof tmp))<0)
04360367 1021 throw PDNSException(string("Setsockopt failed: ")+stringerror());
bf676f2f
PL
1022 return true;
1023}
1024
18861f97
RG
1025bool isNonBlocking(int sock)
1026{
1027 int flags=fcntl(sock,F_GETFL,0);
1028 return flags & O_NONBLOCK;
1029}
1030
3bb98d97 1031bool setReceiveSocketErrors([[maybe_unused]] int sock, [[maybe_unused]] int af)
29bb743c
OM
1032{
1033#ifdef __linux__
1034 int tmp = 1, ret;
1035 if (af == AF_INET) {
1036 ret = setsockopt(sock, IPPROTO_IP, IP_RECVERR, &tmp, sizeof(tmp));
1037 } else {
1038 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &tmp, sizeof(tmp));
1039 }
1040 if (ret < 0) {
04360367 1041 throw PDNSException(string("Setsockopt failed: ") + stringerror());
29bb743c
OM
1042 }
1043#endif
1044 return true;
1045}
1046
3897b9e1 1047// Closes a socket.
18e9f940 1048int closesocket(int socket)
3897b9e1 1049{
18e9f940
OM
1050 int ret = ::close(socket);
1051 if(ret < 0 && errno == ECONNRESET) { // see ticket 192, odd BSD behaviour
3897b9e1 1052 return 0;
18e9f940
OM
1053 }
1054 if (ret < 0) {
1055 int err = errno;
1056 throw PDNSException("Error closing socket: " + stringerror(err));
1057 }
3897b9e1 1058 return ret;
1059}
1060
1061bool setCloseOnExec(int sock)
1062{
3ddb9247 1063 int flags=fcntl(sock,F_GETFD,0);
3897b9e1 1064 if(flags<0 || fcntl(sock, F_SETFD,flags|FD_CLOEXEC) <0)
1065 return false;
1066 return true;
1067}
1068
6907f014 1069#ifdef __linux__
ab7cf5f0
RG
1070#include <linux/rtnetlink.h>
1071
1072int getMACAddress(const ComboAddress& ca, char* dest, size_t destLen)
1073{
1074 struct {
1075 struct nlmsghdr headermsg;
1076 struct ndmsg neighbormsg;
1077 } request;
1078
1079 std::array<char, 8192> buffer;
1080
1081 auto sock = FDWrapper(socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE));
1082 if (sock.getHandle() == -1) {
1083 return errno;
6f238a33 1084 }
ab7cf5f0
RG
1085
1086 memset(&request, 0, sizeof(request));
1087 request.headermsg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
1088 request.headermsg.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1089 request.headermsg.nlmsg_type = RTM_GETNEIGH;
1090 request.neighbormsg.ndm_family = ca.sin4.sin_family;
1091
1092 while (true) {
1093 ssize_t sent = send(sock.getHandle(), &request, sizeof(request), 0);
1094 if (sent == -1) {
1095 if (errno == EINTR) {
1096 continue;
1097 }
1098 return errno;
1099 }
1100 else if (static_cast<size_t>(sent) != sizeof(request)) {
1101 return EIO;
1102 }
1103 break;
6f238a33 1104 }
ab7cf5f0
RG
1105
1106 bool done = false;
1107 bool foundIP = false;
1108 bool foundMAC = false;
1109 do {
1110 ssize_t got = recv(sock.getHandle(), buffer.data(), buffer.size(), 0);
1111
1112 if (got < 0) {
1113 if (errno == EINTR) {
1114 continue;
1115 }
1116 return errno;
1117 }
1118
1119 size_t remaining = static_cast<size_t>(got);
1120 for (struct nlmsghdr* nlmsgheader = reinterpret_cast<struct nlmsghdr*>(buffer.data());
1121 done == false && NLMSG_OK (nlmsgheader, remaining);
1122 nlmsgheader = reinterpret_cast<struct nlmsghdr*>(NLMSG_NEXT(nlmsgheader, remaining))) {
1123
1124 if (nlmsgheader->nlmsg_type == NLMSG_DONE) {
1125 done = true;
1126 break;
1127 }
1128
1129 auto nd = reinterpret_cast<struct ndmsg*>(NLMSG_DATA(nlmsgheader));
f161f5ff 1130 auto rtatp = reinterpret_cast<struct rtattr*>(reinterpret_cast<char*>(nd) + NLMSG_ALIGN(sizeof(struct ndmsg)));
ab7cf5f0
RG
1131 int rtattrlen = nlmsgheader->nlmsg_len - NLMSG_LENGTH(sizeof(struct ndmsg));
1132
1133 if (nd->ndm_family != ca.sin4.sin_family) {
1134 continue;
1135 }
1136
6cb04f61
RG
1137 if (ca.sin4.sin_family == AF_INET6 && ca.sin6.sin6_scope_id != 0 && static_cast<int32_t>(ca.sin6.sin6_scope_id) != nd->ndm_ifindex) {
1138 continue;
1139 }
1140
ab7cf5f0
RG
1141 for (; done == false && RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp, rtattrlen)) {
1142 if (rtatp->rta_type == NDA_DST){
1143 if (nd->ndm_family == AF_INET) {
1144 auto inp = reinterpret_cast<struct in_addr*>(RTA_DATA(rtatp));
1145 if (inp->s_addr == ca.sin4.sin_addr.s_addr) {
1146 foundIP = true;
1147 }
1148 }
1149 else if (nd->ndm_family == AF_INET6) {
1150 auto inp = reinterpret_cast<struct in6_addr *>(RTA_DATA(rtatp));
1151 if (memcmp(inp->s6_addr, ca.sin6.sin6_addr.s6_addr, sizeof(ca.sin6.sin6_addr.s6_addr)) == 0) {
1152 foundIP = true;
1153 }
1154 }
1155 }
1156 else if (rtatp->rta_type == NDA_LLADDR) {
1157 if (foundIP) {
7c591a96
RG
1158 size_t addrLen = rtatp->rta_len - sizeof(struct rtattr);
1159 if (addrLen > destLen) {
1160 return ENOBUFS;
ab7cf5f0 1161 }
7c591a96 1162 memcpy(dest, reinterpret_cast<const char*>(rtatp) + sizeof(struct rtattr), addrLen);
ab7cf5f0
RG
1163 foundMAC = true;
1164 done = true;
1165 break;
1166 }
1167 }
1817fef6 1168 }
6907f014 1169 }
1170 }
ab7cf5f0
RG
1171 while (done == false);
1172
1173 return foundMAC ? 0 : ENOENT;
1174}
1175#else
3bb98d97 1176int getMACAddress(const ComboAddress& /* ca */, char* /* dest */, size_t /* len */)
ab7cf5f0 1177{
6f238a33
CHB
1178 return ENOENT;
1179}
ab7cf5f0
RG
1180#endif /* __linux__ */
1181
6f238a33
CHB
1182string getMACAddress(const ComboAddress& ca)
1183{
1184 string ret;
1185 char tmp[6];
1186 if (getMACAddress(ca, tmp, sizeof(tmp)) == 0) {
1187 ret.append(tmp, sizeof(tmp));
1188 }
6907f014 1189 return ret;
1190}
1191
3bb98d97 1192uint64_t udpErrorStats([[maybe_unused]] const std::string& str)
8290ad9f 1193{
1194#ifdef __linux__
1195 ifstream ifs("/proc/net/snmp");
68c3e465 1196 if (!ifs) {
8290ad9f 1197 return 0;
68c3e465
RG
1198 }
1199
8290ad9f 1200 string line;
68c3e465
RG
1201 while (getline(ifs, line)) {
1202 if (boost::starts_with(line, "Udp: ") && isdigit(line.at(5))) {
84d1d33a 1203 vector<string> parts;
8290ad9f 1204 stringtok(parts, line, " \n\t\r");
68c3e465
RG
1205
1206 if (parts.size() < 7) {
f9562824 1207 break;
68c3e465
RG
1208 }
1209
1210 if (str == "udp-rcvbuf-errors") {
f9562824 1211 return std::stoull(parts.at(5));
68c3e465
RG
1212 }
1213 else if (str == "udp-sndbuf-errors") {
f9562824 1214 return std::stoull(parts.at(6));
68c3e465
RG
1215 }
1216 else if (str == "udp-noport-errors") {
f9562824 1217 return std::stoull(parts.at(2));
68c3e465
RG
1218 }
1219 else if (str == "udp-in-errors") {
f9562824 1220 return std::stoull(parts.at(3));
68c3e465
RG
1221 }
1222 else if (parts.size() >= 8 && str == "udp-in-csum-errors") {
1223 return std::stoull(parts.at(7));
1224 }
1225 else {
f9562824 1226 return 0;
68c3e465 1227 }
8290ad9f 1228 }
1229 }
1230#endif
1231 return 0;
1232}
da15912b 1233
3bb98d97 1234uint64_t udp6ErrorStats([[maybe_unused]] const std::string& str)
84d1d33a
RG
1235{
1236#ifdef __linux__
1237 const std::map<std::string, std::string> keys = {
1238 { "udp6-in-errors", "Udp6InErrors" },
1239 { "udp6-recvbuf-errors", "Udp6RcvbufErrors" },
1240 { "udp6-sndbuf-errors", "Udp6SndbufErrors" },
1241 { "udp6-noport-errors", "Udp6NoPorts" },
1242 { "udp6-in-csum-errors", "Udp6InCsumErrors" }
1243 };
1244
1245 auto key = keys.find(str);
1246 if (key == keys.end()) {
1247 return 0;
1248 }
1249
1250 ifstream ifs("/proc/net/snmp6");
1251 if (!ifs) {
1252 return 0;
1253 }
1254
1255 std::string line;
1256 while (getline(ifs, line)) {
1257 if (!boost::starts_with(line, key->second)) {
1258 continue;
1259 }
1260
1261 std::vector<std::string> parts;
84d1d33a
RG
1262 stringtok(parts, line, " \n\t\r");
1263
1264 if (parts.size() != 2) {
1265 return 0;
1266 }
1267
1268 return std::stoull(parts.at(1));
1269 }
1270#endif
1271 return 0;
1272}
1273
d73de874 1274uint64_t tcpErrorStats(const std::string& /* str */)
e150c22c
RG
1275{
1276#ifdef __linux__
1277 ifstream ifs("/proc/net/netstat");
1278 if (!ifs) {
1279 return 0;
1280 }
1281
1282 string line;
1283 vector<string> parts;
1284 while (getline(ifs,line)) {
1285 if (line.size() > 9 && boost::starts_with(line, "TcpExt: ") && isdigit(line.at(8))) {
1286 stringtok(parts, line, " \n\t\r");
1287
1288 if (parts.size() < 21) {
1289 break;
1290 }
1291
1292 return std::stoull(parts.at(20));
1293 }
1294 }
1295#endif
1296 return 0;
1297}
1298
d73de874 1299uint64_t getCPUIOWait(const std::string& /* str */)
591d1bd3
RG
1300{
1301#ifdef __linux__
1302 ifstream ifs("/proc/stat");
1303 if (!ifs) {
1304 return 0;
1305 }
1306
1307 string line;
1308 vector<string> parts;
1309 while (getline(ifs, line)) {
1310 if (boost::starts_with(line, "cpu ")) {
1311 stringtok(parts, line, " \n\t\r");
1312
1313 if (parts.size() < 6) {
1314 break;
1315 }
1316
1317 return std::stoull(parts[5]);
1318 }
1319 }
1320#endif
1321 return 0;
1322}
1323
d73de874 1324uint64_t getCPUSteal(const std::string& /* str */)
591d1bd3
RG
1325{
1326#ifdef __linux__
1327 ifstream ifs("/proc/stat");
1328 if (!ifs) {
1329 return 0;
1330 }
1331
1332 string line;
1333 vector<string> parts;
1334 while (getline(ifs, line)) {
1335 if (boost::starts_with(line, "cpu ")) {
1336 stringtok(parts, line, " \n\t\r");
1337
1338 if (parts.size() < 9) {
1339 break;
1340 }
1341
1342 return std::stoull(parts[8]);
1343 }
1344 }
1345#endif
1346 return 0;
1347}
1348
21a3792f 1349bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum)
da15912b 1350{
2330c421 1351 if (algoName == DNSName("hmac-md5.sig-alg.reg.int") || algoName == DNSName("hmac-md5"))
da15912b 1352 algoEnum = TSIG_MD5;
2330c421 1353 else if (algoName == DNSName("hmac-sha1"))
da15912b 1354 algoEnum = TSIG_SHA1;
2330c421 1355 else if (algoName == DNSName("hmac-sha224"))
da15912b 1356 algoEnum = TSIG_SHA224;
2330c421 1357 else if (algoName == DNSName("hmac-sha256"))
da15912b 1358 algoEnum = TSIG_SHA256;
2330c421 1359 else if (algoName == DNSName("hmac-sha384"))
da15912b 1360 algoEnum = TSIG_SHA384;
2330c421 1361 else if (algoName == DNSName("hmac-sha512"))
da15912b 1362 algoEnum = TSIG_SHA512;
2330c421 1363 else if (algoName == DNSName("gss-tsig"))
bcb5b94e 1364 algoEnum = TSIG_GSS;
da15912b
AT
1365 else {
1366 return false;
1367 }
1368 return true;
1369}
bfd4efae 1370
21a3792f 1371DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum)
bfd4efae
AT
1372{
1373 switch(algoEnum) {
8171ab83 1374 case TSIG_MD5: return DNSName("hmac-md5.sig-alg.reg.int.");
1375 case TSIG_SHA1: return DNSName("hmac-sha1.");
1376 case TSIG_SHA224: return DNSName("hmac-sha224.");
1377 case TSIG_SHA256: return DNSName("hmac-sha256.");
1378 case TSIG_SHA384: return DNSName("hmac-sha384.");
1379 case TSIG_SHA512: return DNSName("hmac-sha512.");
1380 case TSIG_GSS: return DNSName("gss-tsig.");
bfd4efae
AT
1381 }
1382 throw PDNSException("getTSIGAlgoName does not understand given algorithm, please fix!");
1383}
a1a787dc 1384
a9b6db56 1385uint64_t getOpenFileDescriptors(const std::string&)
1386{
1387#ifdef __linux__
d3190b7d
RG
1388 uint64_t nbFileDescriptors = 0;
1389 const auto dirName = "/proc/" + std::to_string(getpid()) + "/fd/";
1390 auto directoryError = pdns::visit_directory(dirName, [&nbFileDescriptors]([[maybe_unused]] ino_t inodeNumber, const std::string_view& name) {
335da0ba
AT
1391 uint32_t num;
1392 try {
d3190b7d
RG
1393 pdns::checked_stoi_into(num, std::string(name));
1394 if (std::to_string(num) == name) {
1395 nbFileDescriptors++;
1396 }
335da0ba 1397 } catch (...) {
d3190b7d 1398 // was not a number.
84a873c7 1399 }
d3190b7d
RG
1400 return true;
1401 });
1402 if (directoryError) {
1403 return 0U;
a9b6db56 1404 }
d3190b7d 1405 return nbFileDescriptors;
a68eedb2
O
1406#elif defined(__OpenBSD__)
1407 // FreeBSD also has this in libopenbsd, but I don't know if that's available always
1408 return getdtablecount();
a9b6db56 1409#else
d3190b7d 1410 return 0U;
a9b6db56 1411#endif
1412}
1413
a1a787dc 1414uint64_t getRealMemoryUsage(const std::string&)
1415{
330dcb5c 1416#ifdef __linux__
16101f83 1417 ifstream ifs("/proc/self/statm");
330dcb5c
OM
1418 if(!ifs)
1419 return 0;
1420
1421 uint64_t size, resident, shared, text, lib, data;
1422 ifs >> size >> resident >> shared >> text >> lib >> data;
1423
ff1f5c0f 1424 // We used to use "data" here, but it proves unreliable and even is marked "broken"
1e05b07c 1425 // in https://www.kernel.org/doc/html/latest/filesystems/proc.html
ff1f5c0f 1426 return resident * getpagesize();
330dcb5c
OM
1427#else
1428 struct rusage ru;
1429 if (getrusage(RUSAGE_SELF, &ru) != 0)
1430 return 0;
1431 return ru.ru_maxrss * 1024;
1432#endif
1433}
1434
1435
1436uint64_t getSpecialMemoryUsage(const std::string&)
1437{
a1a787dc 1438#ifdef __linux__
16101f83 1439 ifstream ifs("/proc/self/smaps");
a1a787dc 1440 if(!ifs)
1441 return 0;
1442 string line;
1443 uint64_t bytes=0;
1444 string header("Private_Dirty:");
1445 while(getline(ifs, line)) {
1446 if(boost::starts_with(line, header)) {
335da0ba 1447 bytes += std::stoull(line.substr(header.length() + 1))*1024;
a1a787dc 1448 }
1449 }
1450 return bytes;
1451#else
1452 return 0;
1453#endif
1454}
4f99f3d3
RG
1455
1456uint64_t getCPUTimeUser(const std::string&)
1457{
1458 struct rusage ru;
1459 getrusage(RUSAGE_SELF, &ru);
1460 return (ru.ru_utime.tv_sec*1000ULL + ru.ru_utime.tv_usec/1000);
1461}
1462
1463uint64_t getCPUTimeSystem(const std::string&)
1464{
1465 struct rusage ru;
1466 getrusage(RUSAGE_SELF, &ru);
1467 return (ru.ru_stime.tv_sec*1000ULL + ru.ru_stime.tv_usec/1000);
1468}
3fcaeeac 1469
1470double DiffTime(const struct timespec& first, const struct timespec& second)
1471{
d2adfa5c
OM
1472 auto seconds = second.tv_sec - first.tv_sec;
1473 auto nseconds = second.tv_nsec - first.tv_nsec;
1e05b07c 1474
d2adfa5c
OM
1475 if (nseconds < 0) {
1476 seconds -= 1;
1477 nseconds += 1000000000;
3fcaeeac 1478 }
d2adfa5c 1479 return static_cast<double>(seconds) + static_cast<double>(nseconds) / 1000000000.0;
3fcaeeac 1480}
1481
1482double DiffTime(const struct timeval& first, const struct timeval& second)
1483{
1484 int seconds=second.tv_sec - first.tv_sec;
1485 int useconds=second.tv_usec - first.tv_usec;
1e05b07c 1486
3fcaeeac 1487 if(useconds < 0) {
1488 seconds-=1;
1489 useconds+=1000000;
1490 }
1491 return seconds + useconds/1000000.0;
1492}
ffb07158 1493
ffb07158 1494uid_t strToUID(const string &str)
1495{
1496 uid_t result = 0;
1497 const char * cstr = str.c_str();
1498 struct passwd * pwd = getpwnam(cstr);
1499
4646277d 1500 if (pwd == nullptr) {
e805cba5 1501 long long val;
ffb07158 1502
e805cba5
RG
1503 try {
1504 val = stoll(str);
ffb07158 1505 }
e805cba5
RG
1506 catch(std::exception& e) {
1507 throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
1508 }
1509
1510 if (val < std::numeric_limits<uid_t>::min() || val > std::numeric_limits<uid_t>::max()) {
1511 throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
ffb07158 1512 }
e805cba5
RG
1513
1514 result = static_cast<uid_t>(val);
ffb07158 1515 }
1516 else {
1517 result = pwd->pw_uid;
1518 }
1519
1520 return result;
1521}
1522
1523gid_t strToGID(const string &str)
1524{
1525 gid_t result = 0;
1526 const char * cstr = str.c_str();
1527 struct group * grp = getgrnam(cstr);
1528
4646277d 1529 if (grp == nullptr) {
e805cba5 1530 long long val;
ffb07158 1531
e805cba5
RG
1532 try {
1533 val = stoll(str);
ffb07158 1534 }
e805cba5
RG
1535 catch(std::exception& e) {
1536 throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
1537 }
1538
1539 if (val < std::numeric_limits<gid_t>::min() || val > std::numeric_limits<gid_t>::max()) {
1540 throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
ffb07158 1541 }
e805cba5
RG
1542
1543 result = static_cast<gid_t>(val);
ffb07158 1544 }
1545 else {
1546 result = grp->gr_gid;
1547 }
1548
1549 return result;
1550}
1551
8fd25133
RG
1552bool isSettingThreadCPUAffinitySupported()
1553{
1554#ifdef HAVE_PTHREAD_SETAFFINITY_NP
1555 return true;
1556#else
1557 return false;
1558#endif
1559}
1560
3bb98d97 1561int mapThreadToCPUList([[maybe_unused]] pthread_t tid, [[maybe_unused]] const std::set<int>& cpus)
8fd25133
RG
1562{
1563#ifdef HAVE_PTHREAD_SETAFFINITY_NP
4d39d7f3
TIH
1564# ifdef __NetBSD__
1565 cpuset_t *cpuset;
1566 cpuset = cpuset_create();
1567 for (const auto cpuID : cpus) {
1568 cpuset_set(cpuID, cpuset);
1569 }
1570
1571 return pthread_setaffinity_np(tid,
1572 cpuset_size(cpuset),
1573 cpuset);
1574# else
1575# ifdef __FreeBSD__
1576# define cpu_set_t cpuset_t
1577# endif
8fd25133
RG
1578 cpu_set_t cpuset;
1579 CPU_ZERO(&cpuset);
1580 for (const auto cpuID : cpus) {
1581 CPU_SET(cpuID, &cpuset);
1582 }
1583
1584 return pthread_setaffinity_np(tid,
1585 sizeof(cpuset),
1586 &cpuset);
4d39d7f3 1587# endif
99be0a43 1588#else
8fd25133 1589 return ENOSYS;
99be0a43 1590#endif /* HAVE_PTHREAD_SETAFFINITY_NP */
8fd25133 1591}
5d4e1ef8
RG
1592
1593std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath)
1594{
1595 std::vector<ComboAddress> results;
1596
1597 ifstream ifs(resolvConfPath);
1598 if (!ifs) {
1599 return results;
1600 }
1601
1602 string line;
1603 while(std::getline(ifs, line)) {
dc593046 1604 boost::trim_right_if(line, boost::is_any_of(" \r\n\x1a"));
5d4e1ef8
RG
1605 boost::trim_left(line); // leading spaces, let's be nice
1606
1607 string::size_type tpos = line.find_first_of(";#");
1608 if (tpos != string::npos) {
1609 line.resize(tpos);
1610 }
1611
1612 if (boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
1613 vector<string> parts;
1614 stringtok(parts, line, " \t,"); // be REALLY nice
d2adfa5c 1615 for (auto iter = parts.begin() + 1; iter != parts.end(); ++iter) {
5d4e1ef8
RG
1616 try {
1617 results.emplace_back(*iter, 53);
1618 }
1619 catch(...)
1620 {
1621 }
1622 }
1623 }
1624 }
1625
1626 return results;
1627}
ee271fc4 1628
3bb98d97 1629size_t getPipeBufferSize([[maybe_unused]] int fd)
ee271fc4
RG
1630{
1631#ifdef F_GETPIPE_SZ
1632 int res = fcntl(fd, F_GETPIPE_SZ);
1633 if (res == -1) {
1634 return 0;
1635 }
1636 return res;
1637#else
1638 errno = ENOSYS;
1639 return 0;
1640#endif /* F_GETPIPE_SZ */
1641}
1642
3bb98d97 1643bool setPipeBufferSize([[maybe_unused]] int fd, [[maybe_unused]] size_t size)
ee271fc4
RG
1644{
1645#ifdef F_SETPIPE_SZ
60a133cc 1646 if (size > static_cast<size_t>(std::numeric_limits<int>::max())) {
ee271fc4
RG
1647 errno = EINVAL;
1648 return false;
1649 }
1650 int newSize = static_cast<int>(size);
1651 int res = fcntl(fd, F_SETPIPE_SZ, newSize);
1652 if (res == -1) {
1653 return false;
1654 }
1655 return true;
1656#else
1657 errno = ENOSYS;
1658 return false;
1659#endif /* F_SETPIPE_SZ */
1660}
ef3ee606
RG
1661
1662DNSName reverseNameFromIP(const ComboAddress& ip)
1663{
1664 if (ip.isIPv4()) {
1665 std::string result("in-addr.arpa.");
1666 auto ptr = reinterpret_cast<const uint8_t*>(&ip.sin4.sin_addr.s_addr);
1667 for (size_t idx = 0; idx < sizeof(ip.sin4.sin_addr.s_addr); idx++) {
1668 result = std::to_string(ptr[idx]) + "." + result;
1669 }
1670 return DNSName(result);
1671 }
1672 else if (ip.isIPv6()) {
1673 std::string result("ip6.arpa.");
1674 auto ptr = reinterpret_cast<const uint8_t*>(&ip.sin6.sin6_addr.s6_addr[0]);
1675 for (size_t idx = 0; idx < sizeof(ip.sin6.sin6_addr.s6_addr); idx++) {
1676 std::stringstream stream;
1677 stream << std::hex << (ptr[idx] & 0x0F);
1678 stream << '.';
1679 stream << std::hex << (((ptr[idx]) >> 4) & 0x0F);
1680 stream << '.';
1681 result = stream.str() + result;
1682 }
1683 return DNSName(result);
1684 }
1685
1686 throw std::runtime_error("Calling reverseNameFromIP() for an address which is neither an IPv4 nor an IPv6");
1687}
64d38231 1688
032382bc
PD
1689std::string makeLuaString(const std::string& in)
1690{
1691 ostringstream str;
1692
1693 str<<'"';
1694
1695 char item[5];
1696 for (unsigned char n : in) {
1697 if (islower(n) || isupper(n)) {
1698 item[0] = n;
1699 item[1] = 0;
1700 }
1701 else {
1702 snprintf(item, sizeof(item), "\\%03d", n);
1703 }
1704 str << item;
1705 }
1706
1707 str<<'"';
1708
1709 return str.str();
1710}
e701f9d4
PL
1711
1712size_t parseSVCBValueList(const std::string &in, vector<std::string> &val) {
1713 std::string parsed;
1714 auto ret = parseRFC1035CharString(in, parsed);
1715 parseSVCBValueListFromParsedRFC1035CharString(parsed, val);
1716 return ret;
1717};
b2504b29
RG
1718
1719#ifdef HAVE_CRYPTO_MEMCMP
1720#include <openssl/crypto.h>
dde80251
RG
1721#else /* HAVE_CRYPTO_MEMCMP */
1722#ifdef HAVE_SODIUM_MEMCMP
1723#include <sodium.h>
1724#endif /* HAVE_SODIUM_MEMCMP */
1725#endif /* HAVE_CRYPTO_MEMCMP */
b2504b29
RG
1726
1727bool constantTimeStringEquals(const std::string& a, const std::string& b)
1728{
1729 if (a.size() != b.size()) {
1730 return false;
1731 }
1732 const size_t size = a.size();
1733#ifdef HAVE_CRYPTO_MEMCMP
1734 return CRYPTO_memcmp(a.c_str(), b.c_str(), size) == 0;
dde80251
RG
1735#else /* HAVE_CRYPTO_MEMCMP */
1736#ifdef HAVE_SODIUM_MEMCMP
1737 return sodium_memcmp(a.c_str(), b.c_str(), size) == 0;
1738#else /* HAVE_SODIUM_MEMCMP */
b2504b29
RG
1739 const volatile unsigned char *_a = (const volatile unsigned char *) a.c_str();
1740 const volatile unsigned char *_b = (const volatile unsigned char *) b.c_str();
1741 unsigned char res = 0;
1742
1743 for (size_t idx = 0; idx < size; idx++) {
1744 res |= _a[idx] ^ _b[idx];
1745 }
1746
1747 return res == 0;
dde80251
RG
1748#endif /* !HAVE_SODIUM_MEMCMP */
1749#endif /* !HAVE_CRYPTO_MEMCMP */
b2504b29 1750}
d3190b7d
RG
1751
1752namespace pdns
1753{
20b2f204
RG
1754struct CloseDirDeleter
1755{
07d4785d 1756 void operator()(DIR* dir) const noexcept {
20b2f204
RG
1757 closedir(dir);
1758 }
1759};
1760
d3190b7d
RG
1761std::optional<std::string> visit_directory(const std::string& directory, const std::function<bool(ino_t inodeNumber, const std::string_view& name)>& visitor)
1762{
20b2f204 1763 auto dirHandle = std::unique_ptr<DIR, CloseDirDeleter>(opendir(directory.c_str()));
d3190b7d
RG
1764 if (!dirHandle) {
1765 auto err = errno;
1766 return std::string("Error opening directory '" + directory + "': " + stringerror(err));
1767 }
1768
1769 bool keepGoing = true;
1770 struct dirent* ent = nullptr;
1771 // NOLINTNEXTLINE(concurrency-mt-unsafe): readdir is thread-safe nowadays and readdir_r is deprecated
1772 while (keepGoing && (ent = readdir(dirHandle.get())) != nullptr) {
1773 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay: dirent API
1774 auto name = std::string_view(ent->d_name, strlen(ent->d_name));
1775 keepGoing = visitor(ent->d_ino, name);
1776 }
1777
1778 return std::nullopt;
1779}
b1564d45
RG
1780
1781UniqueFilePtr openFileForWriting(const std::string& filePath, mode_t permissions, bool mustNotExist, bool appendIfExists)
1782{
1783 int flags = O_WRONLY | O_CREAT;
1784 if (mustNotExist) {
1785 flags |= O_EXCL;
1786 }
1787 else if (appendIfExists) {
1788 flags |= O_APPEND;
1789 }
1790 int fileDesc = open(filePath.c_str(), flags, permissions);
1791 if (fileDesc == -1) {
6e58535e 1792 return {};
b1564d45
RG
1793 }
1794 auto filePtr = pdns::UniqueFilePtr(fdopen(fileDesc, appendIfExists ? "a" : "w"));
1795 if (!filePtr) {
1796 auto error = errno;
1797 close(fileDesc);
1798 errno = error;
6e58535e 1799 return {};
b1564d45
RG
1800 }
1801 return filePtr;
1802}
1803
d3190b7d 1804}