]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/misc.cc
Merge pull request #8096 from mind04/pdns-notify-db-queries
[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 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
705f31ae 25#include <sys/param.h>
22cf1fda 26#include <sys/socket.h>
3897b9e1 27#include <fcntl.h>
705f31ae
BH
28#include <netdb.h>
29#include <sys/time.h>
30#include <time.h>
3a8a4d68 31#include <sys/resource.h>
705f31ae 32#include <netinet/in.h>
76cb4593 33#include <sys/un.h>
705f31ae 34#include <unistd.h>
8290ad9f 35#include <fstream>
12c86877
BH
36#include "misc.hh"
37#include <vector>
38#include <sstream>
39#include <errno.h>
1258abe0 40#include <cstring>
c4bee46c 41#include <iostream>
a9b6db56 42#include <sys/types.h>
43#include <dirent.h>
705f31ae 44#include <algorithm>
6e2514e4 45#include <boost/optional.hpp>
fab091b9 46#include <poll.h>
12c86877 47#include <iomanip>
149d0144 48#include <netinet/tcp.h>
12c86877
BH
49#include <string.h>
50#include <stdlib.h>
51#include <stdio.h>
5c409fa2 52#include "pdnsexception.hh"
c6566265 53#include <sys/types.h>
040712e0 54#include <boost/algorithm/string.hpp>
65d8e171 55#include "iputils.hh"
e325f20c 56#include "dnsparser.hh"
ffb07158 57#include <sys/types.h>
58#include <pwd.h>
59#include <grp.h>
6c78a20e
R
60#ifdef __FreeBSD__
61# include <pthread_np.h>
62#endif
4d39d7f3
TIH
63#ifdef __NetBSD__
64# include <pthread.h>
65# include <sched.h>
66#endif
a61e8e59 67
1e08edde
BH
68bool g_singleThreaded;
69
a683e8bd 70size_t writen2(int fd, const void *buf, size_t count)
040712e0
BH
71{
72 const char *ptr = (char*)buf;
73 const char *eptr = ptr + count;
3ddb9247 74
a683e8bd 75 ssize_t res;
040712e0
BH
76 while(ptr != eptr) {
77 res = ::write(fd, ptr, eptr - ptr);
78 if(res < 0) {
79 if (errno == EAGAIN)
4957a608 80 throw std::runtime_error("used writen2 on non-blocking socket, got EAGAIN");
040712e0 81 else
4957a608 82 unixDie("failed in writen2");
040712e0
BH
83 }
84 else if (res == 0)
85 throw std::runtime_error("could not write all bytes, got eof in writen2");
3ddb9247 86
a683e8bd 87 ptr += (size_t) res;
040712e0 88 }
3ddb9247 89
040712e0
BH
90 return count;
91}
92
a683e8bd 93size_t readn2(int fd, void* buffer, size_t len)
36fbf590 94{
a683e8bd
RG
95 size_t pos=0;
96 ssize_t res;
36fbf590 97 for(;;) {
98 res = read(fd, (char*)buffer + pos, len - pos);
3ddb9247 99 if(res == 0)
3f6d07a4 100 throw runtime_error("EOF while reading message");
36fbf590 101 if(res < 0) {
102 if (errno == EAGAIN)
3f6d07a4 103 throw std::runtime_error("used readn2 on non-blocking socket, got EAGAIN");
36fbf590 104 else
3f6d07a4 105 unixDie("failed in readn2");
3ddb9247
PD
106 }
107
a683e8bd 108 pos+=(size_t)res;
36fbf590 109 if(pos == len)
110 break;
111 }
112 return len;
113}
114
9396d955 115size_t readn2WithTimeout(int fd, void* buffer, size_t len, int idleTimeout, int totalTimeout)
3f6d07a4
RG
116{
117 size_t pos = 0;
9396d955
RG
118 time_t start = 0;
119 int remainingTime = totalTimeout;
120 if (totalTimeout) {
121 start = time(NULL);
122 }
123
3f6d07a4
RG
124 do {
125 ssize_t got = read(fd, (char *)buffer + pos, len - pos);
126 if (got > 0) {
127 pos += (size_t) got;
128 }
129 else if (got == 0) {
130 throw runtime_error("EOF while reading message");
131 }
132 else {
133 if (errno == EAGAIN) {
9396d955 134 int res = waitForData(fd, (totalTimeout == 0 || idleTimeout <= remainingTime) ? idleTimeout : remainingTime);
3f6d07a4
RG
135 if (res > 0) {
136 /* there is data available */
137 }
138 else if (res == 0) {
139 throw runtime_error("Timeout while waiting for data to read");
140 } else {
141 throw runtime_error("Error while waiting for data to read");
142 }
143 }
144 else {
145 unixDie("failed in readn2WithTimeout");
146 }
147 }
9396d955
RG
148
149 if (totalTimeout) {
150 time_t now = time(NULL);
151 int elapsed = now - start;
152 if (elapsed >= remainingTime) {
153 throw runtime_error("Timeout while reading data");
154 }
155 start = now;
156 remainingTime -= elapsed;
157 }
3f6d07a4
RG
158 }
159 while (pos < len);
160
161 return len;
162}
163
a683e8bd 164size_t writen2WithTimeout(int fd, const void * buffer, size_t len, int timeout)
3f6d07a4
RG
165{
166 size_t pos = 0;
167 do {
168 ssize_t written = write(fd, (char *)buffer + pos, len - pos);
169
170 if (written > 0) {
171 pos += (size_t) written;
172 }
173 else if (written == 0)
174 throw runtime_error("EOF while writing message");
175 else {
176 if (errno == EAGAIN) {
177 int res = waitForRWData(fd, false, timeout, 0);
178 if (res > 0) {
179 /* there is room available */
180 }
181 else if (res == 0) {
182 throw runtime_error("Timeout while waiting to write data");
183 } else {
184 throw runtime_error("Error while waiting for room to write data");
185 }
186 }
187 else {
188 unixDie("failed in write2WithTimeout");
189 }
190 }
191 }
192 while (pos < len);
193
194 return len;
195}
12c86877 196
cc3afe25
BH
197string nowTime()
198{
74ab661c 199 time_t now = time(nullptr);
f0dbdd62
OM
200 struct tm tm;
201 localtime_r(&now, &tm);
74ab661c
CH
202 char buffer[30];
203 // YYYY-mm-dd HH:MM:SS TZOFF
f0dbdd62 204 strftime(buffer, sizeof(buffer), "%F %T %z", &tm);
74ab661c 205 buffer[sizeof(buffer)-1] = '\0';
bfbf6814 206 return string(buffer);
cc3afe25 207}
12c86877 208
092f210a 209uint16_t getShort(const unsigned char *p)
0d8612f2
BH
210{
211 return p[0] * 256 + p[1];
212}
213
214
092f210a 215uint16_t getShort(const char *p)
0d8612f2
BH
216{
217 return getShort((const unsigned char *)p);
218}
219
092f210a 220uint32_t getLong(const unsigned char* p)
b636533b
BH
221{
222 return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3];
223}
224
092f210a 225uint32_t getLong(const char* p)
b636533b
BH
226{
227 return getLong((unsigned char *)p);
228}
229
a683e8bd 230static bool ciEqual(const string& a, const string& b)
49bd5a20
BH
231{
232 if(a.size()!=b.size())
233 return false;
234
235 string::size_type pos=0, epos=a.size();
236 for(;pos < epos; ++pos)
237 if(dns_tolower(a[pos])!=dns_tolower(b[pos]))
238 return false;
239 return true;
240}
241
728485ca 242/** does domain end on suffix? Is smart about "wwwds9a.nl" "ds9a.nl" not matching */
a683e8bd 243static bool endsOn(const string &domain, const string &suffix)
728485ca 244{
49bd5a20 245 if( suffix.empty() || ciEqual(domain, suffix) )
728485ca 246 return true;
7738a23f 247
728485ca
BH
248 if(domain.size()<=suffix.size())
249 return false;
3ddb9247 250
49bd5a20 251 string::size_type dpos=domain.size()-suffix.size()-1, spos=0;
7738a23f 252
49bd5a20
BH
253 if(domain[dpos++]!='.')
254 return false;
728485ca 255
49bd5a20
BH
256 for(; dpos < domain.size(); ++dpos, ++spos)
257 if(dns_tolower(domain[dpos]) != dns_tolower(suffix[spos]))
258 return false;
259
260 return true;
261}
f2c11a48 262
a683e8bd
RG
263/** strips a domain suffix from a domain, returns true if it stripped */
264bool stripDomainSuffix(string *qname, const string &domain)
265{
266 if(!endsOn(*qname, domain))
267 return false;
268
269 if(toLower(*qname)==toLower(domain))
270 *qname="@";
271 else {
272 if((*qname)[qname->size()-domain.size()-1]!='.')
273 return false;
274
275 qname->resize(qname->size()-domain.size()-1);
276 }
277 return true;
278}
279
32252594 280static void parseService4(const string &descr, ServiceTuple &st)
12c86877
BH
281{
282 vector<string>parts;
283 stringtok(parts,descr,":");
52936200 284 if(parts.empty())
3f81d239 285 throw PDNSException("Unable to parse '"+descr+"' as a service");
12c86877
BH
286 st.host=parts[0];
287 if(parts.size()>1)
335da0ba 288 st.port=pdns_stou(parts[1]);
12c86877
BH
289}
290
32252594
BH
291static void parseService6(const string &descr, ServiceTuple &st)
292{
293 string::size_type pos=descr.find(']');
294 if(pos == string::npos)
3f81d239 295 throw PDNSException("Unable to parse '"+descr+"' as an IPv6 service");
32252594
BH
296
297 st.host=descr.substr(1, pos-1);
298 if(pos + 2 < descr.length())
335da0ba 299 st.port=pdns_stou(descr.substr(pos+2));
32252594
BH
300}
301
302
303void parseService(const string &descr, ServiceTuple &st)
304{
305 if(descr.empty())
3f81d239 306 throw PDNSException("Unable to parse '"+descr+"' as a service");
32252594
BH
307
308 vector<string> parts;
309 stringtok(parts, descr, ":");
310
311 if(descr[0]=='[') {
312 parseService6(descr, st);
313 }
314 else if(descr[0]==':' || parts.size() > 2 || descr.find("::") != string::npos) {
315 st.host=descr;
316 }
317 else {
318 parseService4(descr, st);
319 }
320}
12c86877 321
1fb3f0fc 322// returns -1 in case if error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
a7d50153 323int waitForData(int fd, int seconds, int useconds)
649a88df
BH
324{
325 return waitForRWData(fd, true, seconds, useconds);
326}
327
51959320 328int waitForRWData(int fd, bool waitForRead, int seconds, int useconds, bool* error, bool* disconnected)
12c86877 329{
1258abe0
BH
330 int ret;
331
fab091b9
BH
332 struct pollfd pfd;
333 memset(&pfd, 0, sizeof(pfd));
334 pfd.fd = fd;
3ddb9247 335
649a88df 336 if(waitForRead)
fab091b9 337 pfd.events=POLLIN;
649a88df 338 else
fab091b9 339 pfd.events=POLLOUT;
1258abe0 340
6b3d4330 341 ret = poll(&pfd, 1, seconds * 1000 + useconds/1000);
e07c3801 342 if (ret > 0) {
51959320
RG
343 if (error && (pfd.revents & POLLERR)) {
344 *error = true;
345 }
346 if (disconnected && (pfd.revents & POLLHUP)) {
347 *disconnected = true;
348 }
349 }
1258abe0 350
12c86877
BH
351 return ret;
352}
353
a71bee29 354// 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 355int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fdOut) {
d529011f
PL
356 set<int> realFDs;
357 for (const auto& fd : fds) {
358 if (fd >= 0 && realFDs.count(fd) == 0) {
359 realFDs.insert(fd);
360 }
361 }
362
dcc0e65d 363 std::vector<struct pollfd> pfds(realFDs.size());
c2a16005 364 memset(pfds.data(), 0, realFDs.size()*sizeof(struct pollfd));
d529011f
PL
365 int ctr = 0;
366 for (const auto& fd : realFDs) {
367 pfds[ctr].fd = fd;
368 pfds[ctr].events = POLLIN;
369 ctr++;
370 }
371
372 int ret;
373 if(seconds >= 0)
c2a16005 374 ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000);
d529011f 375 else
c2a16005 376 ret = poll(pfds.data(), realFDs.size(), -1);
d529011f
PL
377 if(ret <= 0)
378 return ret;
379
380 set<int> pollinFDs;
aca9c605
PL
381 for (const auto& pfd : pfds) {
382 if (pfd.revents & POLLIN) {
383 pollinFDs.insert(pfd.fd);
d529011f 384 }
d529011f
PL
385 }
386 set<int>::const_iterator it(pollinFDs.begin());
387 advance(it, random() % pollinFDs.size());
dc058f65 388 *fdOut = *it;
d529011f
PL
389 return 1;
390}
391
a71bee29 392// returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
f4ff5929
BH
393int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int*fd)
394{
395 int ret;
396
397 struct pollfd pfds[2];
398 memset(&pfds[0], 0, 2*sizeof(struct pollfd));
399 pfds[0].fd = fd1;
400 pfds[1].fd = fd2;
3ddb9247 401
f4ff5929
BH
402 pfds[0].events= pfds[1].events = POLLIN;
403
404 int nsocks = 1 + (fd2 >= 0); // fd2 can optionally be -1
405
406 if(seconds >= 0)
407 ret = poll(pfds, nsocks, seconds * 1000 + useconds/1000);
408 else
409 ret = poll(pfds, nsocks, -1);
410 if(!ret || ret < 0)
411 return ret;
3ddb9247 412
f4ff5929
BH
413 if((pfds[0].revents & POLLIN) && !(pfds[1].revents & POLLIN))
414 *fd = pfds[0].fd;
415 else if((pfds[1].revents & POLLIN) && !(pfds[0].revents & POLLIN))
416 *fd = pfds[1].fd;
417 else if(ret == 2) {
418 *fd = pfds[random()%2].fd;
419 }
420 else
421 *fd = -1; // should never happen
3ddb9247 422
f4ff5929
BH
423 return 1;
424}
425
12c86877
BH
426
427string humanDuration(time_t passed)
428{
429 ostringstream ret;
430 if(passed<60)
431 ret<<passed<<" seconds";
432 else if(passed<3600)
9e04108d 433 ret<<std::setprecision(2)<<passed/60.0<<" minutes";
12c86877 434 else if(passed<86400)
9e04108d 435 ret<<std::setprecision(3)<<passed/3600.0<<" hours";
12c86877 436 else if(passed<(86400*30.41))
9e04108d 437 ret<<std::setprecision(3)<<passed/86400.0<<" days";
12c86877 438 else
9e04108d 439 ret<<std::setprecision(3)<<passed/(86400*30.41)<<" months";
12c86877
BH
440
441 return ret.str();
442}
443
444DTime::DTime()
445{
eefd15f9 446// set(); // saves lots of gettimeofday calls
55b6512d 447 d_set.tv_sec=d_set.tv_usec=0;
12c86877
BH
448}
449
12c86877
BH
450time_t DTime::time()
451{
452 return d_set.tv_sec;
453}
454
1d329048
BH
455const string unquotify(const string &item)
456{
457 if(item.size()<2)
458 return item;
459
460 string::size_type bpos=0, epos=item.size();
12c86877 461
3ddb9247 462 if(item[0]=='"')
1d329048 463 bpos=1;
c4bee46c 464
1d329048
BH
465 if(item[epos-1]=='"')
466 epos-=1;
467
c4bee46c 468 return item.substr(bpos,epos-bpos);
1d329048 469}
12c86877
BH
470
471void stripLine(string &line)
472{
bdf40704 473 string::size_type pos=line.find_first_of("\r\n");
12c86877
BH
474 if(pos!=string::npos) {
475 line.resize(pos);
476 }
477}
478
479string urlEncode(const string &text)
480{
481 string ret;
482 for(string::const_iterator i=text.begin();i!=text.end();++i)
483 if(*i==' ')ret.append("%20");
484 else ret.append(1,*i);
485 return ret;
486}
487
488string getHostname()
489{
0db70140 490#ifndef MAXHOSTNAMELEN
76473b92 491#define MAXHOSTNAMELEN 255
0db70140 492#endif
ac2bb9e7 493
12c86877
BH
494 char tmp[MAXHOSTNAMELEN];
495 if(gethostname(tmp, MAXHOSTNAMELEN))
496 return "UNKNOWN";
497
bfbf6814 498 return string(tmp);
12c86877
BH
499}
500
501string itoa(int i)
502{
503 ostringstream o;
504 o<<i;
505 return o.str();
506}
507
22c9c86a
BH
508string uitoa(unsigned int i) // MSVC 6 doesn't grok overloading (un)signed
509{
510 ostringstream o;
511 o<<i;
512 return o.str();
513}
514
d88babea
KM
515string bitFlip(const string &str)
516{
517 string::size_type pos=0, epos=str.size();
518 string ret;
519 ret.reserve(epos);
520 for(;pos < epos; ++pos)
521 ret.append(1, ~str[pos]);
522 return ret;
523}
22c9c86a 524
a2a81d42 525string stringerror(int err)
12c86877 526{
a2a81d42 527 return strerror(err);
12c86877
BH
528}
529
a2a81d42 530string stringerror()
d3b4137e 531{
a2a81d42 532 return strerror(errno);
d3b4137e 533}
d3b4137e 534
12c86877
BH
535void cleanSlashes(string &str)
536{
537 string::const_iterator i;
538 string out;
539 for(i=str.begin();i!=str.end();++i) {
540 if(*i=='/' && i!=str.begin() && *(i-1)=='/')
541 continue;
542 out.append(1,*i);
543 }
544 str=out;
545}
546
7b35aa49 547
092f210a 548bool IpToU32(const string &str, uint32_t *ip)
525b8a7c 549{
4cf26303
BH
550 if(str.empty()) {
551 *ip=0;
552 return true;
553 }
3ddb9247 554
092c9cc4 555 struct in_addr inp;
3897b9e1 556 if(inet_aton(str.c_str(), &inp)) {
092c9cc4
BH
557 *ip=inp.s_addr;
558 return true;
559 }
560 return false;
525b8a7c
BH
561}
562
5730f30c
BH
563string U32ToIP(uint32_t val)
564{
565 char tmp[17];
9b2244e1 566 snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
4957a608
BH
567 (val >> 24)&0xff,
568 (val >> 16)&0xff,
569 (val >> 8)&0xff,
570 (val )&0xff);
bfbf6814 571 return string(tmp);
5730f30c
BH
572}
573
37d3f960 574
2db9c30e
BH
575string makeHexDump(const string& str)
576{
577 char tmp[5];
578 string ret;
ea634573 579 ret.reserve((int)(str.size()*2.2));
2db9c30e
BH
580
581 for(string::size_type n=0;n<str.size();++n) {
385b86f6 582 snprintf(tmp, sizeof(tmp), "%02x ", (unsigned char)str[n]);
2db9c30e
BH
583 ret+=tmp;
584 }
585 return ret;
586}
587
e67e250f 588// shuffle, maintaining some semblance of order
d190894c 589void shuffle(vector<DNSZoneRecord>& rrs)
e67e250f 590{
d190894c 591 vector<DNSZoneRecord>::iterator first, second;
3ddb9247 592 for(first=rrs.begin();first!=rrs.end();++first)
d190894c 593 if(first->dr.d_place==DNSResourceRecord::ANSWER && first->dr.d_type != QType::CNAME) // CNAME must come first
e67e250f
BH
594 break;
595 for(second=first;second!=rrs.end();++second)
d190894c 596 if(second->dr.d_place!=DNSResourceRecord::ANSWER)
e67e250f 597 break;
3ddb9247 598
d190894c 599 if(second-first > 1)
e67e250f 600 random_shuffle(first,second);
3ddb9247 601
e67e250f 602 // now shuffle the additional records
3ddb9247 603 for(first=second;first!=rrs.end();++first)
d190894c 604 if(first->dr.d_place==DNSResourceRecord::ADDITIONAL && first->dr.d_type != QType::CNAME) // CNAME must come first
e67e250f
BH
605 break;
606 for(second=first;second!=rrs.end();++second)
d190894c 607 if(second->dr.d_place!=DNSResourceRecord::ADDITIONAL)
e67e250f 608 break;
3ddb9247 609
e67e250f
BH
610 if(second-first>1)
611 random_shuffle(first,second);
612
613 // we don't shuffle the rest
614}
88358c9b 615
e325f20c 616
617// shuffle, maintaining some semblance of order
618void shuffle(vector<DNSRecord>& rrs)
92476c8b 619{
e325f20c 620 vector<DNSRecord>::iterator first, second;
621 for(first=rrs.begin();first!=rrs.end();++first)
e693ff5a 622 if(first->d_place==DNSResourceRecord::ANSWER && first->d_type != QType::CNAME) // CNAME must come first
e325f20c 623 break;
624 for(second=first;second!=rrs.end();++second)
2cccc377 625 if(second->d_place!=DNSResourceRecord::ANSWER || second->d_type == QType::RRSIG) // leave RRSIGs at the end
e325f20c 626 break;
627
628 if(second-first>1)
629 random_shuffle(first,second);
630
631 // now shuffle the additional records
632 for(first=second;first!=rrs.end();++first)
e693ff5a 633 if(first->d_place==DNSResourceRecord::ADDITIONAL && first->d_type != QType::CNAME) // CNAME must come first
e325f20c 634 break;
635 for(second=first; second!=rrs.end(); ++second)
e693ff5a 636 if(second->d_place!=DNSResourceRecord::ADDITIONAL)
e325f20c 637 break;
638
639 if(second-first>1)
640 random_shuffle(first,second);
641
642 // we don't shuffle the rest
92476c8b
PD
643}
644
2cccc377 645static uint16_t mapTypesToOrder(uint16_t type)
27b85f24 646{
647 if(type == QType::CNAME)
648 return 0;
2cccc377 649 if(type == QType::RRSIG)
650 return 65535;
27b85f24 651 else
652 return 1;
653}
654
92476c8b
PD
655// make sure rrs is sorted in d_place order to avoid surprises later
656// then shuffle the parts that desire shuffling
e325f20c 657void orderAndShuffle(vector<DNSRecord>& rrs)
92476c8b 658{
e325f20c 659 std::stable_sort(rrs.begin(), rrs.end(), [](const DNSRecord&a, const DNSRecord& b) {
2cccc377 660 return std::make_tuple(a.d_place, mapTypesToOrder(a.d_type)) < std::make_tuple(b.d_place, mapTypesToOrder(b.d_type));
e325f20c 661 });
92476c8b
PD
662 shuffle(rrs);
663}
88358c9b
BH
664
665void normalizeTV(struct timeval& tv)
666{
667 if(tv.tv_usec > 1000000) {
668 ++tv.tv_sec;
669 tv.tv_usec-=1000000;
670 }
671 else if(tv.tv_usec < 0) {
672 --tv.tv_sec;
673 tv.tv_usec+=1000000;
674 }
675}
676
677const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs)
678{
679 struct timeval ret;
680 ret.tv_sec=lhs.tv_sec + rhs.tv_sec;
681 ret.tv_usec=lhs.tv_usec + rhs.tv_usec;
682 normalizeTV(ret);
683 return ret;
684}
685
686const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs)
687{
688 struct timeval ret;
689 ret.tv_sec=lhs.tv_sec - rhs.tv_sec;
690 ret.tv_usec=lhs.tv_usec - rhs.tv_usec;
691 normalizeTV(ret);
692 return ret;
693}
50c79a76
BH
694
695pair<string, string> splitField(const string& inp, char sepa)
696{
697 pair<string, string> ret;
698 string::size_type cpos=inp.find(sepa);
699 if(cpos==string::npos)
700 ret.first=inp;
701 else {
702 ret.first=inp.substr(0, cpos);
703 ret.second=inp.substr(cpos+1);
704 }
705 return ret;
706}
6e2514e4 707
f8499e52 708int logFacilityToLOG(unsigned int facility)
6e2514e4 709{
6e2514e4
BH
710 switch(facility) {
711 case 0:
712 return LOG_LOCAL0;
713 case 1:
714 return(LOG_LOCAL1);
715 case 2:
716 return(LOG_LOCAL2);
717 case 3:
718 return(LOG_LOCAL3);
719 case 4:
720 return(LOG_LOCAL4);
721 case 5:
722 return(LOG_LOCAL5);
723 case 6:
724 return(LOG_LOCAL6);
725 case 7:
726 return(LOG_LOCAL7);
727 default:
f8499e52 728 return -1;
6e2514e4
BH
729 }
730}
da042e6e
BH
731
732string stripDot(const string& dom)
733{
734 if(dom.empty())
735 return dom;
736
737 if(dom[dom.size()-1]!='.')
738 return dom;
739
740 return dom.substr(0,dom.size()-1);
741}
4dadd22f
BH
742
743
f71bc087
BH
744
745int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret)
746{
85db02c5
BH
747 if(addr.empty())
748 return -1;
749 string ourAddr(addr);
6cbfa73b
RG
750 bool portSet = false;
751 unsigned int port;
85db02c5
BH
752 if(addr[0]=='[') { // [::]:53 style address
753 string::size_type pos = addr.find(']');
0047d734 754 if(pos == string::npos)
85db02c5
BH
755 return -1;
756 ourAddr.assign(addr.c_str() + 1, pos-1);
ed2ff96e 757 if (pos + 1 != addr.size()) { // complete after ], no port specified
0047d734
CH
758 if (pos + 2 > addr.size() || addr[pos+1]!=':')
759 return -1;
760 try {
761 port = pdns_stou(addr.substr(pos+2));
762 portSet = true;
763 }
d38e2ba9 764 catch(const std::out_of_range&) {
0047d734
CH
765 return -1;
766 }
5b4ed369 767 }
85db02c5 768 }
39d2d9b2 769 ret->sin6_scope_id=0;
770 ret->sin6_family=AF_INET6;
771
0c9de4fc 772 if(inet_pton(AF_INET6, ourAddr.c_str(), (void*)&ret->sin6_addr) != 1) {
773 struct addrinfo* res;
774 struct addrinfo hints;
775 memset(&hints, 0, sizeof(hints));
3ddb9247 776
0c9de4fc 777 hints.ai_family = AF_INET6;
778 hints.ai_flags = AI_NUMERICHOST;
3ddb9247 779
0c9de4fc 780 int error;
1746e9bb 781 // getaddrinfo has anomalous return codes, anything nonzero is an error, positive or negative
782 if((error=getaddrinfo(ourAddr.c_str(), 0, &hints, &res))) {
0c9de4fc 783 return -1;
784 }
3ddb9247 785
0c9de4fc 786 memcpy(ret, res->ai_addr, res->ai_addrlen);
787 freeaddrinfo(res);
f71bc087 788 }
0c9de4fc 789
6cbfa73b
RG
790 if(portSet) {
791 if(port > 65535)
792 return -1;
5b4ed369 793
85db02c5 794 ret->sin6_port = htons(port);
6cbfa73b 795 }
0c9de4fc 796
f71bc087
BH
797 return 0;
798}
834942f1 799
76cb4593 800int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret)
85db02c5
BH
801{
802 if(str.empty()) {
803 return -1;
804 }
805 struct in_addr inp;
3ddb9247 806
85db02c5
BH
807 string::size_type pos = str.find(':');
808 if(pos == string::npos) { // no port specified, not touching the port
3897b9e1 809 if(inet_aton(str.c_str(), &inp)) {
85db02c5
BH
810 ret->sin_addr.s_addr=inp.s_addr;
811 return 0;
812 }
813 return -1;
814 }
815 if(!*(str.c_str() + pos + 1)) // trailing :
3ddb9247
PD
816 return -1;
817
85db02c5
BH
818 char *eptr = (char*)str.c_str() + str.size();
819 int port = strtol(str.c_str() + pos + 1, &eptr, 10);
5b4ed369
PL
820 if (port < 0 || port > 65535)
821 return -1;
822
85db02c5
BH
823 if(*eptr)
824 return -1;
3ddb9247 825
85db02c5 826 ret->sin_port = htons(port);
3897b9e1 827 if(inet_aton(str.substr(0, pos).c_str(), &inp)) {
85db02c5
BH
828 ret->sin_addr.s_addr=inp.s_addr;
829 return 0;
830 }
831 return -1;
832}
833
76cb4593
CH
834int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret)
835{
836 if (path.empty())
837 return -1;
838
839 memset(ret, 0, sizeof(struct sockaddr_un));
840 ret->sun_family = AF_UNIX;
841 if (path.length() >= sizeof(ret->sun_path))
842 return -1;
843
844 path.copy(ret->sun_path, sizeof(ret->sun_path), 0);
845 return 0;
846}
85db02c5 847
834942f1
BH
848//! read a line of text from a FILE* to a std::string, returns false on 'no data'
849bool stringfgets(FILE* fp, std::string& line)
850{
851 char buffer[1024];
852 line.clear();
3ddb9247 853
834942f1
BH
854 do {
855 if(!fgets(buffer, sizeof(buffer), fp))
856 return !line.empty();
3ddb9247
PD
857
858 line.append(buffer);
834942f1
BH
859 } while(!strchr(buffer, '\n'));
860 return true;
861}
e611a06c 862
4e9a20e6 863bool readFileIfThere(const char* fname, std::string* line)
864{
865 line->clear();
5e1f23ca 866 auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname, "r"), fclose);
4e9a20e6 867 if(!fp)
868 return false;
5e1f23ca
RG
869 stringfgets(fp.get(), *line);
870 fp.reset();
871
4e9a20e6 872 return true;
873}
874
3ee84c3f
BH
875Regex::Regex(const string &expr)
876{
877 if(regcomp(&d_preg, expr.c_str(), REG_ICASE|REG_NOSUB|REG_EXTENDED))
3f81d239 878 throw PDNSException("Regular expression did not compile");
3ee84c3f 879}
65d8e171 880
25ba7344
PD
881// if you end up here because valgrind told you were are doing something wrong
882// with msgh->msg_controllen, please refer to https://github.com/PowerDNS/pdns/pull/3962
883// first.
7bec330a 884// Note that cmsgbuf should be aligned the same as a struct cmsghdr
7bec330a 885void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cmsgbuf, const ComboAddress* source, int itfIndex)
65d8e171 886{
ecc8571f 887 struct cmsghdr *cmsg = NULL;
65d8e171
KN
888
889 if(source->sin4.sin_family == AF_INET6) {
890 struct in6_pktinfo *pkt;
891
892 msgh->msg_control = cmsgbuf;
aeb011b7
RG
893#if !defined( __APPLE__ )
894 /* CMSG_SPACE is not a constexpr on macOS */
7f818542 895 static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in6_pktinfo");
aeb011b7
RG
896#else /* __APPLE__ */
897 if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
898 throw std::runtime_error("Buffer is too small for in6_pktinfo");
899 }
900#endif /* __APPLE__ */
65d8e171
KN
901 msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
902
903 cmsg = CMSG_FIRSTHDR(msgh);
904 cmsg->cmsg_level = IPPROTO_IPV6;
905 cmsg->cmsg_type = IPV6_PKTINFO;
906 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
907
908 pkt = (struct in6_pktinfo *) CMSG_DATA(cmsg);
20b22895
OM
909 // Include the padding to stop valgrind complaining about passing uninitialized data
910 memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
65d8e171 911 pkt->ipi6_addr = source->sin6.sin6_addr;
fbe2a2e0 912 pkt->ipi6_ifindex = itfIndex;
65d8e171
KN
913 }
914 else {
4d39d7f3 915#if defined(IP_PKTINFO)
65d8e171
KN
916 struct in_pktinfo *pkt;
917
918 msgh->msg_control = cmsgbuf;
aeb011b7
RG
919#if !defined( __APPLE__ )
920 /* CMSG_SPACE is not a constexpr on macOS */
7f818542 921 static_assert(CMSG_SPACE(sizeof(*pkt)) <= sizeof(*cmsgbuf), "Buffer is too small for in_pktinfo");
aeb011b7
RG
922#else /* __APPLE__ */
923 if (CMSG_SPACE(sizeof(*pkt)) > sizeof(*cmsgbuf)) {
924 throw std::runtime_error("Buffer is too small for in_pktinfo");
925 }
926#endif /* __APPLE__ */
65d8e171
KN
927 msgh->msg_controllen = CMSG_SPACE(sizeof(*pkt));
928
929 cmsg = CMSG_FIRSTHDR(msgh);
930 cmsg->cmsg_level = IPPROTO_IP;
931 cmsg->cmsg_type = IP_PKTINFO;
932 cmsg->cmsg_len = CMSG_LEN(sizeof(*pkt));
933
934 pkt = (struct in_pktinfo *) CMSG_DATA(cmsg);
20b22895
OM
935 // Include the padding to stop valgrind complaining about passing uninitialized data
936 memset(pkt, 0, CMSG_SPACE(sizeof(*pkt)));
65d8e171 937 pkt->ipi_spec_dst = source->sin4.sin_addr;
fbe2a2e0 938 pkt->ipi_ifindex = itfIndex;
4d39d7f3 939#elif defined(IP_SENDSRCADDR)
65d8e171
KN
940 struct in_addr *in;
941
942 msgh->msg_control = cmsgbuf;
aeb011b7 943#if !defined( __APPLE__ )
7f818542 944 static_assert(CMSG_SPACE(sizeof(*in)) <= sizeof(*cmsgbuf), "Buffer is too small for in_addr");
aeb011b7
RG
945#else /* __APPLE__ */
946 if (CMSG_SPACE(sizeof(*in)) > sizeof(*cmsgbuf)) {
947 throw std::runtime_error("Buffer is too small for in_addr");
948 }
949#endif /* __APPLE__ */
65d8e171
KN
950 msgh->msg_controllen = CMSG_SPACE(sizeof(*in));
951
952 cmsg = CMSG_FIRSTHDR(msgh);
953 cmsg->cmsg_level = IPPROTO_IP;
954 cmsg->cmsg_type = IP_SENDSRCADDR;
955 cmsg->cmsg_len = CMSG_LEN(sizeof(*in));
956
20b22895 957 // Include the padding to stop valgrind complaining about passing uninitialized data
65d8e171 958 in = (struct in_addr *) CMSG_DATA(cmsg);
20b22895 959 memset(in, 0, CMSG_SPACE(sizeof(*in)));
65d8e171 960 *in = source->sin4.sin_addr;
ecc8571f 961#endif
65d8e171
KN
962 }
963}
3a8a4d68 964
965unsigned int getFilenumLimit(bool hardOrSoft)
966{
967 struct rlimit rlim;
968 if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
969 unixDie("Requesting number of available file descriptors");
970 return hardOrSoft ? rlim.rlim_max : rlim.rlim_cur;
971}
972
973void setFilenumLimit(unsigned int lim)
974{
975 struct rlimit rlim;
976
977 if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
978 unixDie("Requesting number of available file descriptors");
979 rlim.rlim_cur=lim;
980 if(setrlimit(RLIMIT_NOFILE, &rlim) < 0)
981 unixDie("Setting number of available file descriptors");
982}
06ea9015 983
984#define burtlemix(a,b,c) \
985{ \
986 a -= b; a -= c; a ^= (c>>13); \
987 b -= c; b -= a; b ^= (a<<8); \
988 c -= a; c -= b; c ^= (b>>13); \
989 a -= b; a -= c; a ^= (c>>12); \
990 b -= c; b -= a; b ^= (a<<16); \
991 c -= a; c -= b; c ^= (b>>5); \
992 a -= b; a -= c; a ^= (c>>3); \
993 b -= c; b -= a; b ^= (a<<10); \
994 c -= a; c -= b; c ^= (b>>15); \
995}
996
997uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t initval)
998{
999 uint32_t a,b,c,len;
1000
1001 /* Set up the internal state */
1002 len = length;
1003 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
1004 c = initval; /* the previous hash value */
1005
1006 /*---------------------------------------- handle most of the key */
1007 while (len >= 12) {
1008 a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24));
1009 b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24));
1010 c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24));
1011 burtlemix(a,b,c);
1012 k += 12; len -= 12;
1013 }
1014
1015 /*------------------------------------- handle the last 11 bytes */
1016 c += length;
1017 switch(len) { /* all the case statements fall through */
1018 case 11: c+=((uint32_t)k[10]<<24);
d2fe6b5e 1019 /* fall-through */
06ea9015 1020 case 10: c+=((uint32_t)k[9]<<16);
d2fe6b5e 1021 /* fall-through */
06ea9015 1022 case 9 : c+=((uint32_t)k[8]<<8);
1023 /* the first byte of c is reserved for the length */
d2fe6b5e 1024 /* fall-through */
06ea9015 1025 case 8 : b+=((uint32_t)k[7]<<24);
d2fe6b5e 1026 /* fall-through */
06ea9015 1027 case 7 : b+=((uint32_t)k[6]<<16);
d2fe6b5e 1028 /* fall-through */
06ea9015 1029 case 6 : b+=((uint32_t)k[5]<<8);
d2fe6b5e 1030 /* fall-through */
06ea9015 1031 case 5 : b+=k[4];
d2fe6b5e 1032 /* fall-through */
06ea9015 1033 case 4 : a+=((uint32_t)k[3]<<24);
d2fe6b5e 1034 /* fall-through */
06ea9015 1035 case 3 : a+=((uint32_t)k[2]<<16);
d2fe6b5e 1036 /* fall-through */
06ea9015 1037 case 2 : a+=((uint32_t)k[1]<<8);
d2fe6b5e 1038 /* fall-through */
06ea9015 1039 case 1 : a+=k[0];
1040 /* case 0: nothing left to add */
1041 }
1042 burtlemix(a,b,c);
1043 /*-------------------------------------------- report the result */
1044 return c;
1045}
9ae194e2 1046
6200227b 1047uint32_t burtleCI(const unsigned char* k, uint32_t length, uint32_t initval)
1048{
1049 uint32_t a,b,c,len;
1050
1051 /* Set up the internal state */
1052 len = length;
1053 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
1054 c = initval; /* the previous hash value */
1055
1056 /*---------------------------------------- handle most of the key */
1057 while (len >= 12) {
af891b3d 1058 a += (dns_tolower(k[0]) +((uint32_t)dns_tolower(k[1])<<8) +((uint32_t)dns_tolower(k[2])<<16) +((uint32_t)dns_tolower(k[3])<<24));
1059 b += (dns_tolower(k[4]) +((uint32_t)dns_tolower(k[5])<<8) +((uint32_t)dns_tolower(k[6])<<16) +((uint32_t)dns_tolower(k[7])<<24));
1060 c += (dns_tolower(k[8]) +((uint32_t)dns_tolower(k[9])<<8) +((uint32_t)dns_tolower(k[10])<<16)+((uint32_t)dns_tolower(k[11])<<24));
6200227b 1061 burtlemix(a,b,c);
1062 k += 12; len -= 12;
1063 }
1064
1065 /*------------------------------------- handle the last 11 bytes */
1066 c += length;
1067 switch(len) { /* all the case statements fall through */
af891b3d 1068 case 11: c+=((uint32_t)dns_tolower(k[10])<<24);
d2fe6b5e 1069 /* fall-through */
af891b3d 1070 case 10: c+=((uint32_t)dns_tolower(k[9])<<16);
d2fe6b5e 1071 /* fall-through */
af891b3d 1072 case 9 : c+=((uint32_t)dns_tolower(k[8])<<8);
6200227b 1073 /* the first byte of c is reserved for the length */
d2fe6b5e 1074 /* fall-through */
af891b3d 1075 case 8 : b+=((uint32_t)dns_tolower(k[7])<<24);
d2fe6b5e 1076 /* fall-through */
af891b3d 1077 case 7 : b+=((uint32_t)dns_tolower(k[6])<<16);
d2fe6b5e 1078 /* fall-through */
af891b3d 1079 case 6 : b+=((uint32_t)dns_tolower(k[5])<<8);
d2fe6b5e 1080 /* fall-through */
af891b3d 1081 case 5 : b+=dns_tolower(k[4]);
d2fe6b5e 1082 /* fall-through */
af891b3d 1083 case 4 : a+=((uint32_t)dns_tolower(k[3])<<24);
d2fe6b5e 1084 /* fall-through */
af891b3d 1085 case 3 : a+=((uint32_t)dns_tolower(k[2])<<16);
d2fe6b5e 1086 /* fall-through */
af891b3d 1087 case 2 : a+=((uint32_t)dns_tolower(k[1])<<8);
d2fe6b5e 1088 /* fall-through */
af891b3d 1089 case 1 : a+=dns_tolower(k[0]);
6200227b 1090 /* case 0: nothing left to add */
1091 }
1092 burtlemix(a,b,c);
1093 /*-------------------------------------------- report the result */
1094 return c;
1095}
1096
1097
915b0c39 1098bool setSocketTimestamps(int fd)
9ae194e2 1099{
eb4c1792 1100#ifdef SO_TIMESTAMP
9ae194e2 1101 int on=1;
915b0c39 1102 return setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, (char*)&on, sizeof(on)) == 0;
99be0a43 1103#else
915b0c39 1104 return true; // we pretend this happened.
99be0a43 1105#endif
9ae194e2 1106}
58044407 1107
883a30a7 1108bool setTCPNoDelay(int sock)
149d0144 1109{
1110 int flag = 1;
883a30a7
RG
1111 return setsockopt(sock, /* socket affected */
1112 IPPROTO_TCP, /* set option at TCP level */
1113 TCP_NODELAY, /* name of option */
1114 (char *) &flag, /* the cast is historical cruft */
1115 sizeof(flag)) == 0; /* length of option value */
149d0144 1116}
1117
1118
3897b9e1 1119bool setNonBlocking(int sock)
1120{
3ddb9247 1121 int flags=fcntl(sock,F_GETFL,0);
3897b9e1 1122 if(flags<0 || fcntl(sock, F_SETFL,flags|O_NONBLOCK) <0)
1123 return false;
1124 return true;
1125}
1126
1127bool setBlocking(int sock)
1128{
3ddb9247 1129 int flags=fcntl(sock,F_GETFL,0);
3897b9e1 1130 if(flags<0 || fcntl(sock, F_SETFL,flags&(~O_NONBLOCK)) <0)
1131 return false;
1132 return true;
1133}
1134
bf676f2f
PL
1135bool setReuseAddr(int sock)
1136{
1137 int tmp = 1;
1138 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&tmp, static_cast<unsigned>(sizeof tmp))<0)
04360367 1139 throw PDNSException(string("Setsockopt failed: ")+stringerror());
bf676f2f
PL
1140 return true;
1141}
1142
18861f97
RG
1143bool isNonBlocking(int sock)
1144{
1145 int flags=fcntl(sock,F_GETFL,0);
1146 return flags & O_NONBLOCK;
1147}
1148
29bb743c
OM
1149bool setReceiveSocketErrors(int sock, int af)
1150{
1151#ifdef __linux__
1152 int tmp = 1, ret;
1153 if (af == AF_INET) {
1154 ret = setsockopt(sock, IPPROTO_IP, IP_RECVERR, &tmp, sizeof(tmp));
1155 } else {
1156 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVERR, &tmp, sizeof(tmp));
1157 }
1158 if (ret < 0) {
04360367 1159 throw PDNSException(string("Setsockopt failed: ") + stringerror());
29bb743c
OM
1160 }
1161#endif
1162 return true;
1163}
1164
3897b9e1 1165// Closes a socket.
1166int closesocket( int socket )
1167{
1168 int ret=::close(socket);
1169 if(ret < 0 && errno == ECONNRESET) // see ticket 192, odd BSD behaviour
1170 return 0;
3ddb9247 1171 if(ret < 0)
3897b9e1 1172 throw PDNSException("Error closing socket: "+stringerror());
1173 return ret;
1174}
1175
1176bool setCloseOnExec(int sock)
1177{
3ddb9247 1178 int flags=fcntl(sock,F_GETFD,0);
3897b9e1 1179 if(flags<0 || fcntl(sock, F_SETFD,flags|FD_CLOEXEC) <0)
1180 return false;
1181 return true;
1182}
1183
6907f014 1184string getMACAddress(const ComboAddress& ca)
1185{
1186 string ret;
1187#ifdef __linux__
1188 ifstream ifs("/proc/net/arp");
1189 if(!ifs)
1190 return ret;
1191 string line;
1192 string match=ca.toString()+' ';
1193 while(getline(ifs, line)) {
1194 if(boost::starts_with(line, match)) {
1195 vector<string> parts;
1196 stringtok(parts, line, " \n\t\r");
1197 if(parts.size() < 4)
1198 return ret;
1199 unsigned int tmp[6];
1200 sscanf(parts[3].c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5);
1201 for(int i = 0 ; i< 6 ; ++i)
1202 ret.append(1, (char)tmp[i]);
1203 return ret;
1204 }
1205 }
1206#endif
1207 return ret;
1208}
1209
8290ad9f 1210uint64_t udpErrorStats(const std::string& str)
1211{
1212#ifdef __linux__
1213 ifstream ifs("/proc/net/snmp");
1214 if(!ifs)
1215 return 0;
1216 string line;
1217 vector<string> parts;
1218 while(getline(ifs,line)) {
1219 if(boost::starts_with(line, "Udp: ") && isdigit(line[5])) {
1220 stringtok(parts, line, " \n\t\r");
1221 if(parts.size() < 7)
1222 break;
1223 if(str=="udp-rcvbuf-errors")
335da0ba 1224 return std::stoull(parts[5]);
8290ad9f 1225 else if(str=="udp-sndbuf-errors")
335da0ba 1226 return std::stoull(parts[6]);
8290ad9f 1227 else if(str=="udp-noport-errors")
335da0ba 1228 return std::stoull(parts[2]);
8290ad9f 1229 else if(str=="udp-in-errors")
335da0ba 1230 return std::stoull(parts[3]);
8290ad9f 1231 else
1232 return 0;
1233 }
1234 }
1235#endif
1236 return 0;
1237}
da15912b 1238
591d1bd3
RG
1239uint64_t getCPUIOWait(const std::string& str)
1240{
1241#ifdef __linux__
1242 ifstream ifs("/proc/stat");
1243 if (!ifs) {
1244 return 0;
1245 }
1246
1247 string line;
1248 vector<string> parts;
1249 while (getline(ifs, line)) {
1250 if (boost::starts_with(line, "cpu ")) {
1251 stringtok(parts, line, " \n\t\r");
1252
1253 if (parts.size() < 6) {
1254 break;
1255 }
1256
1257 return std::stoull(parts[5]);
1258 }
1259 }
1260#endif
1261 return 0;
1262}
1263
1264uint64_t getCPUSteal(const std::string& str)
1265{
1266#ifdef __linux__
1267 ifstream ifs("/proc/stat");
1268 if (!ifs) {
1269 return 0;
1270 }
1271
1272 string line;
1273 vector<string> parts;
1274 while (getline(ifs, line)) {
1275 if (boost::starts_with(line, "cpu ")) {
1276 stringtok(parts, line, " \n\t\r");
1277
1278 if (parts.size() < 9) {
1279 break;
1280 }
1281
1282 return std::stoull(parts[8]);
1283 }
1284 }
1285#endif
1286 return 0;
1287}
1288
21a3792f 1289bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum)
da15912b 1290{
2330c421 1291 if (algoName == DNSName("hmac-md5.sig-alg.reg.int") || algoName == DNSName("hmac-md5"))
da15912b 1292 algoEnum = TSIG_MD5;
2330c421 1293 else if (algoName == DNSName("hmac-sha1"))
da15912b 1294 algoEnum = TSIG_SHA1;
2330c421 1295 else if (algoName == DNSName("hmac-sha224"))
da15912b 1296 algoEnum = TSIG_SHA224;
2330c421 1297 else if (algoName == DNSName("hmac-sha256"))
da15912b 1298 algoEnum = TSIG_SHA256;
2330c421 1299 else if (algoName == DNSName("hmac-sha384"))
da15912b 1300 algoEnum = TSIG_SHA384;
2330c421 1301 else if (algoName == DNSName("hmac-sha512"))
da15912b 1302 algoEnum = TSIG_SHA512;
2330c421 1303 else if (algoName == DNSName("gss-tsig"))
bcb5b94e 1304 algoEnum = TSIG_GSS;
da15912b
AT
1305 else {
1306 return false;
1307 }
1308 return true;
1309}
bfd4efae 1310
21a3792f 1311DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum)
bfd4efae
AT
1312{
1313 switch(algoEnum) {
8171ab83 1314 case TSIG_MD5: return DNSName("hmac-md5.sig-alg.reg.int.");
1315 case TSIG_SHA1: return DNSName("hmac-sha1.");
1316 case TSIG_SHA224: return DNSName("hmac-sha224.");
1317 case TSIG_SHA256: return DNSName("hmac-sha256.");
1318 case TSIG_SHA384: return DNSName("hmac-sha384.");
1319 case TSIG_SHA512: return DNSName("hmac-sha512.");
1320 case TSIG_GSS: return DNSName("gss-tsig.");
bfd4efae
AT
1321 }
1322 throw PDNSException("getTSIGAlgoName does not understand given algorithm, please fix!");
1323}
a1a787dc 1324
a9b6db56 1325uint64_t getOpenFileDescriptors(const std::string&)
1326{
1327#ifdef __linux__
1328 DIR* dirhdl=opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str());
1329 if(!dirhdl)
1330 return 0;
1331
1332 struct dirent *entry;
1333 int ret=0;
1334 while((entry = readdir(dirhdl))) {
335da0ba
AT
1335 uint32_t num;
1336 try {
1337 num = pdns_stou(entry->d_name);
1338 } catch (...) {
1339 continue; // was not a number.
1340 }
a9b6db56 1341 if(std::to_string(num) == entry->d_name)
1342 ret++;
1343 }
1344 closedir(dirhdl);
1345 return ret;
1346
1347#else
1348 return 0;
1349#endif
1350}
1351
a1a787dc 1352uint64_t getRealMemoryUsage(const std::string&)
1353{
330dcb5c 1354#ifdef __linux__
16101f83 1355 ifstream ifs("/proc/self/statm");
330dcb5c
OM
1356 if(!ifs)
1357 return 0;
1358
1359 uint64_t size, resident, shared, text, lib, data;
1360 ifs >> size >> resident >> shared >> text >> lib >> data;
1361
1362 return data * getpagesize();
1363#else
1364 struct rusage ru;
1365 if (getrusage(RUSAGE_SELF, &ru) != 0)
1366 return 0;
1367 return ru.ru_maxrss * 1024;
1368#endif
1369}
1370
1371
1372uint64_t getSpecialMemoryUsage(const std::string&)
1373{
a1a787dc 1374#ifdef __linux__
16101f83 1375 ifstream ifs("/proc/self/smaps");
a1a787dc 1376 if(!ifs)
1377 return 0;
1378 string line;
1379 uint64_t bytes=0;
1380 string header("Private_Dirty:");
1381 while(getline(ifs, line)) {
1382 if(boost::starts_with(line, header)) {
335da0ba 1383 bytes += std::stoull(line.substr(header.length() + 1))*1024;
a1a787dc 1384 }
1385 }
1386 return bytes;
1387#else
1388 return 0;
1389#endif
1390}
4f99f3d3
RG
1391
1392uint64_t getCPUTimeUser(const std::string&)
1393{
1394 struct rusage ru;
1395 getrusage(RUSAGE_SELF, &ru);
1396 return (ru.ru_utime.tv_sec*1000ULL + ru.ru_utime.tv_usec/1000);
1397}
1398
1399uint64_t getCPUTimeSystem(const std::string&)
1400{
1401 struct rusage ru;
1402 getrusage(RUSAGE_SELF, &ru);
1403 return (ru.ru_stime.tv_sec*1000ULL + ru.ru_stime.tv_usec/1000);
1404}
3fcaeeac 1405
1406double DiffTime(const struct timespec& first, const struct timespec& second)
1407{
1408 int seconds=second.tv_sec - first.tv_sec;
1409 int nseconds=second.tv_nsec - first.tv_nsec;
1410
1411 if(nseconds < 0) {
1412 seconds-=1;
1413 nseconds+=1000000000;
1414 }
1415 return seconds + nseconds/1000000000.0;
1416}
1417
1418double DiffTime(const struct timeval& first, const struct timeval& second)
1419{
1420 int seconds=second.tv_sec - first.tv_sec;
1421 int useconds=second.tv_usec - first.tv_usec;
1422
1423 if(useconds < 0) {
1424 seconds-=1;
1425 useconds+=1000000;
1426 }
1427 return seconds + useconds/1000000.0;
1428}
ffb07158 1429
ffb07158 1430uid_t strToUID(const string &str)
1431{
1432 uid_t result = 0;
1433 const char * cstr = str.c_str();
1434 struct passwd * pwd = getpwnam(cstr);
1435
1436 if (pwd == NULL) {
e805cba5 1437 long long val;
ffb07158 1438
e805cba5
RG
1439 try {
1440 val = stoll(str);
ffb07158 1441 }
e805cba5
RG
1442 catch(std::exception& e) {
1443 throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
1444 }
1445
1446 if (val < std::numeric_limits<uid_t>::min() || val > std::numeric_limits<uid_t>::max()) {
1447 throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() );
ffb07158 1448 }
e805cba5
RG
1449
1450 result = static_cast<uid_t>(val);
ffb07158 1451 }
1452 else {
1453 result = pwd->pw_uid;
1454 }
1455
1456 return result;
1457}
1458
1459gid_t strToGID(const string &str)
1460{
1461 gid_t result = 0;
1462 const char * cstr = str.c_str();
1463 struct group * grp = getgrnam(cstr);
1464
1465 if (grp == NULL) {
e805cba5 1466 long long val;
ffb07158 1467
e805cba5
RG
1468 try {
1469 val = stoll(str);
ffb07158 1470 }
e805cba5
RG
1471 catch(std::exception& e) {
1472 throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
1473 }
1474
1475 if (val < std::numeric_limits<gid_t>::min() || val > std::numeric_limits<gid_t>::max()) {
1476 throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() );
ffb07158 1477 }
e805cba5
RG
1478
1479 result = static_cast<gid_t>(val);
ffb07158 1480 }
1481 else {
1482 result = grp->gr_gid;
1483 }
1484
1485 return result;
1486}
1487
335da0ba 1488unsigned int pdns_stou(const std::string& str, size_t * idx, int base)
a5c4e4a6 1489{
00f45062 1490 if (str.empty()) return 0; // compatibility
2f975181
PD
1491 unsigned long result;
1492 try {
1493 result = std::stoul(str, idx, base);
1494 }
1495 catch(std::invalid_argument& e) {
1496 throw std::invalid_argument(string(e.what()) + "; (invalid argument during std::stoul); data was \""+str+"\"");
1497 }
cccfa59b
PD
1498 catch(std::out_of_range& e) {
1499 throw std::out_of_range(string(e.what()) + "; (out of range during std::stoul); data was \""+str+"\"");
1500 }
a5c4e4a6 1501 if (result > std::numeric_limits<unsigned int>::max()) {
cccfa59b 1502 throw std::out_of_range("stoul returned result out of unsigned int range; data was \""+str+"\"");
a5c4e4a6
AT
1503 }
1504 return static_cast<unsigned int>(result);
1505}
1506
8fd25133
RG
1507bool isSettingThreadCPUAffinitySupported()
1508{
1509#ifdef HAVE_PTHREAD_SETAFFINITY_NP
1510 return true;
1511#else
1512 return false;
1513#endif
1514}
1515
1516int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus)
1517{
1518#ifdef HAVE_PTHREAD_SETAFFINITY_NP
4d39d7f3
TIH
1519# ifdef __NetBSD__
1520 cpuset_t *cpuset;
1521 cpuset = cpuset_create();
1522 for (const auto cpuID : cpus) {
1523 cpuset_set(cpuID, cpuset);
1524 }
1525
1526 return pthread_setaffinity_np(tid,
1527 cpuset_size(cpuset),
1528 cpuset);
1529# else
1530# ifdef __FreeBSD__
1531# define cpu_set_t cpuset_t
1532# endif
8fd25133
RG
1533 cpu_set_t cpuset;
1534 CPU_ZERO(&cpuset);
1535 for (const auto cpuID : cpus) {
1536 CPU_SET(cpuID, &cpuset);
1537 }
1538
1539 return pthread_setaffinity_np(tid,
1540 sizeof(cpuset),
1541 &cpuset);
4d39d7f3 1542# endif
99be0a43 1543#else
8fd25133 1544 return ENOSYS;
99be0a43 1545#endif /* HAVE_PTHREAD_SETAFFINITY_NP */
8fd25133 1546}
5d4e1ef8
RG
1547
1548std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath)
1549{
1550 std::vector<ComboAddress> results;
1551
1552 ifstream ifs(resolvConfPath);
1553 if (!ifs) {
1554 return results;
1555 }
1556
1557 string line;
1558 while(std::getline(ifs, line)) {
1559 boost::trim_right_if(line, is_any_of(" \r\n\x1a"));
1560 boost::trim_left(line); // leading spaces, let's be nice
1561
1562 string::size_type tpos = line.find_first_of(";#");
1563 if (tpos != string::npos) {
1564 line.resize(tpos);
1565 }
1566
1567 if (boost::starts_with(line, "nameserver ") || boost::starts_with(line, "nameserver\t")) {
1568 vector<string> parts;
1569 stringtok(parts, line, " \t,"); // be REALLY nice
1570 for(vector<string>::const_iterator iter = parts.begin() + 1; iter != parts.end(); ++iter) {
1571 try {
1572 results.emplace_back(*iter, 53);
1573 }
1574 catch(...)
1575 {
1576 }
1577 }
1578 }
1579 }
1580
1581 return results;
1582}
ee271fc4
RG
1583
1584size_t getPipeBufferSize(int fd)
1585{
1586#ifdef F_GETPIPE_SZ
1587 int res = fcntl(fd, F_GETPIPE_SZ);
1588 if (res == -1) {
1589 return 0;
1590 }
1591 return res;
1592#else
1593 errno = ENOSYS;
1594 return 0;
1595#endif /* F_GETPIPE_SZ */
1596}
1597
1598bool setPipeBufferSize(int fd, size_t size)
1599{
1600#ifdef F_SETPIPE_SZ
1601 if (size > std::numeric_limits<int>::max()) {
1602 errno = EINVAL;
1603 return false;
1604 }
1605 int newSize = static_cast<int>(size);
1606 int res = fcntl(fd, F_SETPIPE_SZ, newSize);
1607 if (res == -1) {
1608 return false;
1609 }
1610 return true;
1611#else
1612 errno = ENOSYS;
1613 return false;
1614#endif /* F_SETPIPE_SZ */
1615}