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