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