2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
28 #include <type_traits>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/multi_index_container.hpp>
31 #include <boost/multi_index/ordered_index.hpp>
32 #include <boost/tuple/tuple_comparison.hpp>
33 #include <boost/multi_index/key_extractors.hpp>
34 #include <boost/multi_index/sequenced_index.hpp>
36 using namespace ::boost::multi_index;
41 #include <sys/types.h>
42 #include <sys/socket.h>
51 #include "namespaces.hh"
54 typedef enum { TSIG_MD5, TSIG_SHA1, TSIG_SHA224, TSIG_SHA256, TSIG_SHA384, TSIG_SHA512, TSIG_GSS } TSIGHashEnum;
57 const string unquotify(const string &item);
58 string humanDuration(time_t passed);
59 bool stripDomainSuffix(string *qname, const string &domain);
60 void stripLine(string &line);
62 string urlEncode(const string &text);
63 int waitForData(int fd, int seconds, int useconds=0);
64 int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fd);
65 int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fd);
66 int waitForRWData(int fd, bool waitForRead, int seconds, int useconds, bool* error=nullptr, bool* disconnected=nullptr);
67 uint16_t getShort(const unsigned char *p);
68 uint16_t getShort(const char *p);
69 uint32_t getLong(const unsigned char *p);
70 uint32_t getLong(const char *p);
71 bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum);
72 DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum);
74 int logFacilityToLOG(unsigned int facility);
81 void parseService(const string &descr, ServiceTuple &st);
83 template <typename Container>
85 stringtok (Container &container, string const &in,
86 const char * const delimiters = " \t\n")
88 const string::size_type len = in.length();
89 string::size_type i = 0;
92 // eat leading whitespace
93 i = in.find_first_not_of (delimiters, i);
94 if (i == string::npos)
95 return; // nothing left but white space
97 // find the end of the token
98 string::size_type j = in.find_first_of (delimiters, i);
101 if (j == string::npos) {
102 container.push_back (in.substr(i));
105 container.push_back (in.substr(i, j-i));
107 // set up for next loop
112 template<typename T> bool rfc1982LessThan(T a, T b)
114 static_assert(std::is_unsigned<T>::value, "rfc1982LessThan only works for unsigned types");
115 typedef typename std::make_signed<T>::type signed_t;
116 return static_cast<signed_t>(a - b) < 0;
119 // fills container with ranges, so {posbegin,posend}
120 template <typename Container>
122 vstringtok (Container &container, string const &in,
123 const char * const delimiters = " \t\n")
125 const string::size_type len = in.length();
126 string::size_type i = 0;
129 // eat leading whitespace
130 i = in.find_first_not_of (delimiters, i);
131 if (i == string::npos)
132 return; // nothing left but white space
134 // find the end of the token
135 string::size_type j = in.find_first_of (delimiters, i);
138 if (j == string::npos) {
139 container.push_back (make_pair(i, len));
142 container.push_back (make_pair(i, j));
144 // set up for next loop
149 size_t writen2(int fd, const void *buf, size_t count);
150 inline size_t writen2(int fd, const std::string &s) { return writen2(fd, s.data(), s.size()); }
151 size_t readn2(int fd, void* buffer, size_t len);
152 size_t readn2WithTimeout(int fd, void* buffer, size_t len, int idleTimeout, int totalTimeout=0);
153 size_t writen2WithTimeout(int fd, const void * buffer, size_t len, int timeout);
155 const string toLower(const string &upper);
156 const string toLowerCanonic(const string &upper);
157 bool IpToU32(const string &str, uint32_t *ip);
158 string U32ToIP(uint32_t);
159 string stringerror(int);
160 string stringerror();
162 string uitoa(unsigned int i);
163 string bitFlip(const string &str);
165 void dropPrivs(int uid, int gid);
166 void cleanSlashes(string &str);
168 #if defined(_POSIX_THREAD_CPUTIME) && defined(CLOCK_THREAD_CPUTIME_ID)
169 /** CPUTime measurements */
175 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &d_start);
180 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
181 return 1000000000ULL*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec);
184 struct timespec d_start;
188 /** The DTime class can be used for timing statistics with microsecond resolution.
189 On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */
193 DTime(); //!< Does not set the timer for you! Saves lots of gettimeofday() calls
194 DTime(const DTime &dt) = default;
195 DTime & operator=(const DTime &dt) = default;
197 inline void set(); //!< Reset the timer
198 inline int udiff(); //!< Return the number of microseconds since the timer was last set.
199 inline int udiffNoReset(); //!< Return the number of microseconds since the timer was last set.
200 void setTimeval(const struct timeval& tv)
204 struct timeval getTimeval()
209 struct timeval d_set;
212 inline void DTime::set()
214 gettimeofday(&d_set,0);
217 inline int DTime::udiff()
219 int res=udiffNoReset();
220 gettimeofday(&d_set,0);
224 inline int DTime::udiffNoReset()
228 gettimeofday(&now,0);
229 int ret=1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec);
233 inline const string toLower(const string &upper)
236 const size_t length = reply.length();
238 for(unsigned int i = 0; i < length; ++i) {
239 c = dns_tolower(upper[i]);
246 inline const string toLowerCanonic(const string &upper)
250 unsigned int i, limit= ( unsigned int ) reply.length();
252 for(i = 0; i < limit ; i++) {
253 c = dns_tolower(upper[i]);
267 inline string toUpper( const string& s )
270 for( unsigned int i = 0; i < s.length(); i++ ) {
271 r[i] = dns_toupper(r[i]);
276 inline double getTime()
279 gettimeofday(&now,0);
281 return now.tv_sec+now.tv_usec/1000000.0;
284 inline void unixDie(const string &why)
286 throw runtime_error(why+": "+stringerror());
289 string makeHexDump(const string& str);
291 void normalizeTV(struct timeval& tv);
292 const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs);
293 const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs);
294 inline float makeFloat(const struct timeval& tv)
296 return tv.tv_sec + tv.tv_usec/1000000.0f;
299 inline bool operator<(const struct timeval& lhs, const struct timeval& rhs)
301 return make_pair(lhs.tv_sec, lhs.tv_usec) < make_pair(rhs.tv_sec, rhs.tv_usec);
304 inline bool operator<(const struct timespec& lhs, const struct timespec& rhs)
306 return tie(lhs.tv_sec, lhs.tv_nsec) < tie(rhs.tv_sec, rhs.tv_nsec);
310 inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b) __attribute__((pure));
311 inline bool pdns_ilexicographical_compare(const std::string& a, const std::string& b)
313 const unsigned char *aPtr = (const unsigned char*)a.c_str(), *bPtr = (const unsigned char*)b.c_str();
314 const unsigned char *aEptr = aPtr + a.length(), *bEptr = bPtr + b.length();
315 while(aPtr != aEptr && bPtr != bEptr) {
316 if ((*aPtr != *bPtr) && (dns_tolower(*aPtr) - dns_tolower(*bPtr)))
317 return (dns_tolower(*aPtr) - dns_tolower(*bPtr)) < 0;
321 if(aPtr == aEptr && bPtr == bEptr) // strings are equal (in length)
323 return aPtr == aEptr; // true if first string was shorter
326 inline bool pdns_iequals(const std::string& a, const std::string& b) __attribute__((pure));
327 inline bool pdns_iequals(const std::string& a, const std::string& b)
329 if (a.length() != b.length())
332 const char *aPtr = a.c_str(), *bPtr = b.c_str();
333 const char *aEptr = aPtr + a.length();
334 while(aPtr != aEptr) {
335 if((*aPtr != *bPtr) && (dns_tolower(*aPtr) != dns_tolower(*bPtr)))
343 inline bool pdns_iequals_ch(const char a, const char b) __attribute__((pure));
344 inline bool pdns_iequals_ch(const char a, const char b)
346 if ((a != b) && (dns_tolower(a) != dns_tolower(b)))
353 typedef unsigned long AtomicCounterInner;
354 typedef std::atomic<AtomicCounterInner> AtomicCounter ;
356 // FIXME400 this should probably go?
357 struct CIStringCompare: public std::binary_function<string, string, bool>
359 bool operator()(const string& a, const string& b) const
361 return pdns_ilexicographical_compare(a, b);
365 struct CIStringComparePOSIX
367 bool operator() (const std::string& lhs, const std::string& rhs)
369 std::string::const_iterator a,b;
370 const std::locale &loc = std::locale("POSIX");
371 a=lhs.begin();b=rhs.begin();
372 while(a!=lhs.end()) {
373 if (b==rhs.end() || std::tolower(*b,loc)<std::tolower(*a,loc)) return false;
374 else if (std::tolower(*a,loc)<std::tolower(*b,loc)) return true;
377 return (b!=rhs.end());
381 struct CIStringPairCompare: public std::binary_function<pair<string, uint16_t>, pair<string,uint16_t>, bool>
383 bool operator()(const pair<string, uint16_t>& a, const pair<string, uint16_t>& b) const
385 if(pdns_ilexicographical_compare(a.first, b.first))
387 if(pdns_ilexicographical_compare(b.first, a.first))
389 return a.second < b.second;
393 inline size_t pdns_ci_find(const string& haystack, const string& needle)
395 string::const_iterator it = std::search(haystack.begin(), haystack.end(),
396 needle.begin(), needle.end(), pdns_iequals_ch);
397 if (it == haystack.end()) {
401 return it - haystack.begin();
405 pair<string, string> splitField(const string& inp, char sepa);
407 inline bool isCanonical(const string& qname)
411 return qname[qname.size()-1]=='.';
414 inline DNSName toCanonic(const DNSName& zone, const string& qname)
416 if(qname.size()==1 && qname[0]=='@')
418 if(isCanonical(qname))
419 return DNSName(qname);
420 return DNSName(qname) += zone;
423 string stripDot(const string& dom);
425 int makeIPv6sockaddr(const std::string& addr, struct sockaddr_in6* ret);
426 int makeIPv4sockaddr(const std::string& str, struct sockaddr_in* ret);
427 int makeUNsockaddr(const std::string& path, struct sockaddr_un* ret);
428 bool stringfgets(FILE* fp, std::string& line);
430 template<typename Index>
431 std::pair<typename Index::iterator,bool>
432 replacing_insert(Index& i,const typename Index::value_type& x)
434 std::pair<typename Index::iterator,bool> res=i.insert(x);
435 if(!res.second)res.second=i.replace(res.first,x);
439 /** very small regex wrapper */
443 /** constructor that accepts the expression to regex */
444 Regex(const string &expr);
450 /** call this to find out if 'line' matches your expression */
451 bool match(const string &line) const
453 return regexec(&d_preg,line.c_str(),0,0,0)==0;
455 bool match(const DNSName& name) const
457 return match(name.toStringNoDot());
467 SimpleMatch(const string &mask, bool caseFold = false): d_mask(mask), d_fold(caseFold)
471 bool match(string::const_iterator mi, string::const_iterator mend, string::const_iterator vi, string::const_iterator vend)
476 } else if (*mi == '?') {
477 if (vi == vend) return false;
479 } else if (*mi == '*') {
480 while(*mi == '*') ++mi;
481 if (mi == d_mask.end()) return true;
483 if (match(mi,mend,vi,vend)) return true;
488 if ((mi == mend && vi != vend)||
489 (mi != mend && vi == vend)) return false;
491 if (dns_tolower(*mi) != dns_tolower(*vi)) return false;
493 if (*mi != *vi) return false;
500 bool match(const string& value) {
501 return match(d_mask.begin(), d_mask.end(), value.begin(), value.end());
504 bool match(const DNSName& name) {
505 return match(name.toStringNoDot());
515 // An aligned type to hold cmsgbufs. See https://man.openbsd.org/CMSG_DATA
516 typedef union { struct cmsghdr hdr; char buf[256]; } cmsgbuf_aligned;
518 /* itfIndex is an interface index, as returned by if_nametoindex(). 0 means default. */
519 void addCMsgSrcAddr(struct msghdr* msgh, cmsgbuf_aligned* cbuf, const ComboAddress* source, int itfIndex);
521 unsigned int getFilenumLimit(bool hardOrSoft=0);
522 void setFilenumLimit(unsigned int lim);
523 bool readFileIfThere(const char* fname, std::string* line);
524 uint32_t burtle(const unsigned char* k, uint32_t length, uint32_t init);
525 bool setSocketTimestamps(int fd);
527 //! Sets the socket into blocking mode.
528 bool setBlocking( int sock );
530 //! Sets the socket into non-blocking mode.
531 bool setNonBlocking( int sock );
532 bool setTCPNoDelay(int sock);
533 bool setReuseAddr(int sock);
534 bool isNonBlocking(int sock);
535 bool setReceiveSocketErrors(int sock, int af);
536 int closesocket(int fd);
537 bool setCloseOnExec(int sock);
539 size_t getPipeBufferSize(int fd);
540 bool setPipeBufferSize(int fd, size_t size);
542 uint64_t udpErrorStats(const std::string& str);
543 uint64_t getRealMemoryUsage(const std::string&);
544 uint64_t getSpecialMemoryUsage(const std::string&);
545 uint64_t getOpenFileDescriptors(const std::string&);
546 uint64_t getCPUTimeUser(const std::string&);
547 uint64_t getCPUTimeSystem(const std::string&);
548 uint64_t getCPUIOWait(const std::string&);
549 uint64_t getCPUSteal(const std::string&);
550 std::string getMACAddress(const ComboAddress& ca);
551 template<typename T, typename... Args>
552 std::unique_ptr<T> make_unique(Args&&... args)
554 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
559 const T& defTer(const T& a, const T& b)
564 template<typename P, typename T>
565 T valueOrEmpty(const P val) {
566 if (!val) return T{};
571 // I'm not very OCD, but I appreciate loglines like "processing 1 delta", "processing 2 deltas" :-)
572 template <typename Integer>
573 const char* addS(Integer siz, typename std::enable_if<std::is_integral<Integer>::value>::type*P=0)
581 const char* addS(const C& c, typename std::enable_if<std::is_class<C>::value>::type*P=0)
583 return addS(c.size());
587 const typename C::value_type::second_type* rplookup(const C& c, const typename C::value_type::first_type& key)
589 auto fnd = c.find(key);
595 double DiffTime(const struct timespec& first, const struct timespec& second);
596 double DiffTime(const struct timeval& first, const struct timeval& second);
597 uid_t strToUID(const string &str);
598 gid_t strToGID(const string &str);
600 unsigned int pdns_stou(const std::string& str, size_t * idx = 0, int base = 10);
602 bool isSettingThreadCPUAffinitySupported();
603 int mapThreadToCPUList(pthread_t tid, const std::set<int>& cpus);
605 std::vector<ComboAddress> getResolvers(const std::string& resolvConfPath);
607 DNSName reverseNameFromIP(const ComboAddress& ip);