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