| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002-2006 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License version 2 |
|---|
| 7 | as published by the Free Software Foundation |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | #ifndef MISC_HH |
|---|
| 20 | #define MISC_HH |
|---|
| 21 | |
|---|
| 22 | #if 1 |
|---|
| 23 | #define RDTSC(qp) \ |
|---|
| 24 | do { \ |
|---|
| 25 | unsigned long lowPart, highPart; \ |
|---|
| 26 | __asm__ __volatile__("rdtsc" : "=a" (lowPart), "=d" (highPart)); \ |
|---|
| 27 | qp = (((unsigned long long) highPart) << 32) | lowPart; \ |
|---|
| 28 | } while (0) |
|---|
| 29 | |
|---|
| 30 | #include <iostream> |
|---|
| 31 | using std::cout; |
|---|
| 32 | using std::endl; |
|---|
| 33 | |
|---|
| 34 | struct TSCTimer |
|---|
| 35 | { |
|---|
| 36 | TSCTimer() |
|---|
| 37 | { |
|---|
| 38 | RDTSC(d_tsc1); |
|---|
| 39 | } |
|---|
| 40 | ~TSCTimer() |
|---|
| 41 | { |
|---|
| 42 | uint64_t tsc2; |
|---|
| 43 | RDTSC(tsc2); |
|---|
| 44 | cout<<"Timer: "<< (tsc2 - d_tsc1)/3000.0 << endl; |
|---|
| 45 | } |
|---|
| 46 | uint64_t d_tsc1; |
|---|
| 47 | }; |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | #include "utility.hh" |
|---|
| 51 | #include "dns.hh" |
|---|
| 52 | #ifndef WIN32 |
|---|
| 53 | # include <sys/time.h> |
|---|
| 54 | # include <sys/types.h> |
|---|
| 55 | # include <sys/socket.h> |
|---|
| 56 | # include <time.h> |
|---|
| 57 | # include <syslog.h> |
|---|
| 58 | #else |
|---|
| 59 | # define WINDOWS_LEAN_AND_MEAN |
|---|
| 60 | # include <windows.h> |
|---|
| 61 | # include "utility.hh" |
|---|
| 62 | #endif // WIN32 |
|---|
| 63 | #include <deque> |
|---|
| 64 | #include <stdexcept> |
|---|
| 65 | #include <string> |
|---|
| 66 | #include <ctype.h> |
|---|
| 67 | #include <vector> |
|---|
| 68 | #include <boost/optional.hpp> |
|---|
| 69 | |
|---|
| 70 | using namespace std; |
|---|
| 71 | bool chopOff(string &domain); |
|---|
| 72 | bool chopOffDotted(string &domain); |
|---|
| 73 | |
|---|
| 74 | bool endsOn(const string &domain, const string &suffix); |
|---|
| 75 | bool dottedEndsOn(const string &domain, const string &suffix); |
|---|
| 76 | string nowTime(); |
|---|
| 77 | const string unquotify(const string &item); |
|---|
| 78 | string humanDuration(time_t passed); |
|---|
| 79 | void chomp(string &line, const string &delim); |
|---|
| 80 | bool stripDomainSuffix(string *qname, const string &domain); |
|---|
| 81 | void stripLine(string &line); |
|---|
| 82 | string getHostname(); |
|---|
| 83 | string urlEncode(const string &text); |
|---|
| 84 | int waitForData(int fd, int seconds, int useconds=0); |
|---|
| 85 | uint16_t getShort(const unsigned char *p); |
|---|
| 86 | uint16_t getShort(const char *p); |
|---|
| 87 | uint32_t getLong(const unsigned char *p); |
|---|
| 88 | uint32_t getLong(const char *p); |
|---|
| 89 | boost::optional<int> logFacilityToLOG(unsigned int facility); |
|---|
| 90 | |
|---|
| 91 | inline void putLong(unsigned char* p, uint32_t val) |
|---|
| 92 | { |
|---|
| 93 | *p++=(val>>24)&0xff; |
|---|
| 94 | *p++=(val>>16)&0xff; |
|---|
| 95 | *p++=(val>>8)&0xff; |
|---|
| 96 | *p++=(val )&0xff; |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | inline void putLong(char* p, uint32_t val) |
|---|
| 100 | { |
|---|
| 101 | putLong((unsigned char *)p,val); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | inline uint32_t getLong(unsigned char *p) |
|---|
| 106 | { |
|---|
| 107 | return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | struct ServiceTuple |
|---|
| 112 | { |
|---|
| 113 | string host; |
|---|
| 114 | uint16_t port; |
|---|
| 115 | }; |
|---|
| 116 | void parseService(const string &descr, ServiceTuple &st); |
|---|
| 117 | |
|---|
| 118 | template <typename Container> |
|---|
| 119 | void |
|---|
| 120 | stringtok (Container &container, string const &in, |
|---|
| 121 | const char * const delimiters = " \t\n") |
|---|
| 122 | { |
|---|
| 123 | const string::size_type len = in.length(); |
|---|
| 124 | string::size_type i = 0; |
|---|
| 125 | |
|---|
| 126 | while (i<len) { |
|---|
| 127 | // eat leading whitespace |
|---|
| 128 | i = in.find_first_not_of (delimiters, i); |
|---|
| 129 | if (i == string::npos) |
|---|
| 130 | return; // nothing left but white space |
|---|
| 131 | |
|---|
| 132 | // find the end of the token |
|---|
| 133 | string::size_type j = in.find_first_of (delimiters, i); |
|---|
| 134 | |
|---|
| 135 | // push token |
|---|
| 136 | if (j == string::npos) { |
|---|
| 137 | container.push_back (in.substr(i)); |
|---|
| 138 | return; |
|---|
| 139 | } else |
|---|
| 140 | container.push_back (in.substr(i, j-i)); |
|---|
| 141 | |
|---|
| 142 | // set up for next loop |
|---|
| 143 | i = j + 1; |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | template <typename Container> |
|---|
| 148 | void |
|---|
| 149 | vstringtok (Container &container, string const &in, |
|---|
| 150 | const char * const delimiters = " \t\n") |
|---|
| 151 | { |
|---|
| 152 | const string::size_type len = in.length(); |
|---|
| 153 | string::size_type i = 0; |
|---|
| 154 | |
|---|
| 155 | while (i<len) { |
|---|
| 156 | // eat leading whitespace |
|---|
| 157 | i = in.find_first_not_of (delimiters, i); |
|---|
| 158 | if (i == string::npos) |
|---|
| 159 | return; // nothing left but white space |
|---|
| 160 | |
|---|
| 161 | // find the end of the token |
|---|
| 162 | string::size_type j = in.find_first_of (delimiters, i); |
|---|
| 163 | |
|---|
| 164 | // push token |
|---|
| 165 | if (j == string::npos) { |
|---|
| 166 | container.push_back (make_pair(i, len)); |
|---|
| 167 | return; |
|---|
| 168 | } else |
|---|
| 169 | container.push_back (make_pair(i, j)); |
|---|
| 170 | |
|---|
| 171 | // set up for next loop |
|---|
| 172 | i = j + 1; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | const string toLower(const string &upper); |
|---|
| 177 | const string toLowerCanonic(const string &upper); |
|---|
| 178 | bool IpToU32(const string &str, uint32_t *ip); |
|---|
| 179 | string U32ToIP(uint32_t); |
|---|
| 180 | string stringerror(); |
|---|
| 181 | string netstringerror(); |
|---|
| 182 | string itoa(int i); |
|---|
| 183 | string uitoa(unsigned int i); |
|---|
| 184 | |
|---|
| 185 | void dropPrivs(int uid, int gid); |
|---|
| 186 | int makeGidNumeric(const string &group); |
|---|
| 187 | int makeUidNumeric(const string &user); |
|---|
| 188 | void cleanSlashes(string &str); |
|---|
| 189 | |
|---|
| 190 | /** The DTime class can be used for timing statistics with microsecond resolution. |
|---|
| 191 | On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */ |
|---|
| 192 | class DTime |
|---|
| 193 | { |
|---|
| 194 | public: |
|---|
| 195 | DTime(); //!< Does not set the timer for you! Saves lots of gettimeofday() calls |
|---|
| 196 | DTime(const DTime &dt); |
|---|
| 197 | time_t time(); |
|---|
| 198 | inline void set(); //!< Reset the timer |
|---|
| 199 | inline int udiff(); //!< Return the number of microseconds since the timer was last set. |
|---|
| 200 | void setTimeval(const struct timeval& tv) |
|---|
| 201 | { |
|---|
| 202 | d_set=tv; |
|---|
| 203 | } |
|---|
| 204 | struct timeval getTimeval() |
|---|
| 205 | { |
|---|
| 206 | return d_set; |
|---|
| 207 | } |
|---|
| 208 | private: |
|---|
| 209 | struct timeval d_set; |
|---|
| 210 | }; |
|---|
| 211 | const string sockAddrToString(struct sockaddr_in *remote); |
|---|
| 212 | int sendData(const char *buffer, int replen, int outsock); |
|---|
| 213 | |
|---|
| 214 | inline void DTime::set() |
|---|
| 215 | { |
|---|
| 216 | Utility::gettimeofday(&d_set,0); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | inline int DTime::udiff() |
|---|
| 220 | { |
|---|
| 221 | struct timeval now; |
|---|
| 222 | |
|---|
| 223 | Utility::gettimeofday(&now,0); |
|---|
| 224 | int ret=1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec); |
|---|
| 225 | d_set=now; |
|---|
| 226 | return ret; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | inline bool dns_isspace(char c) |
|---|
| 230 | { |
|---|
| 231 | return c==' ' || c=='\t' || c=='\r' || c=='\n'; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | inline const char dns_tolower(char c) |
|---|
| 235 | { |
|---|
| 236 | if(c>='A' && c<='Z') |
|---|
| 237 | c+='a'-'A'; |
|---|
| 238 | return c; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | inline const string toLower(const string &upper) |
|---|
| 242 | { |
|---|
| 243 | string reply(upper); |
|---|
| 244 | char c; |
|---|
| 245 | for(unsigned int i = 0; i < reply.length(); i++) { |
|---|
| 246 | c = dns_tolower(reply[i]); |
|---|
| 247 | if( c != reply[i]) |
|---|
| 248 | reply[i] = c; |
|---|
| 249 | } |
|---|
| 250 | return reply; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | inline const string toLowerCanonic(const string &upper) |
|---|
| 254 | { |
|---|
| 255 | string reply(upper); |
|---|
| 256 | if(!upper.empty()) { |
|---|
| 257 | unsigned int i, limit= ( unsigned int ) reply.length(); |
|---|
| 258 | char c; |
|---|
| 259 | for(i = 0; i < limit ; i++) { |
|---|
| 260 | c = dns_tolower(reply[i]); |
|---|
| 261 | if(c != reply[i]) |
|---|
| 262 | reply[i] = c; |
|---|
| 263 | } |
|---|
| 264 | if(reply[i-1]=='.') |
|---|
| 265 | reply.resize(i-1); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | return reply; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | // Make s uppercase: |
|---|
| 274 | inline string toUpper( const string& s ) |
|---|
| 275 | { |
|---|
| 276 | string r(s); |
|---|
| 277 | for( unsigned int i = 0; i < s.length(); i++ ) { |
|---|
| 278 | r[i] = toupper( r[i] ); |
|---|
| 279 | } |
|---|
| 280 | return r; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | inline double getTime() |
|---|
| 284 | { |
|---|
| 285 | struct timeval now; |
|---|
| 286 | Utility::gettimeofday(&now,0); |
|---|
| 287 | |
|---|
| 288 | return now.tv_sec+now.tv_usec/1000000.0; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | inline void chomp( string& line, const string& delim ) |
|---|
| 293 | { |
|---|
| 294 | string::size_type pos; |
|---|
| 295 | |
|---|
| 296 | if( ( pos = line.find_last_not_of( delim ) ) != string::npos ) |
|---|
| 297 | { |
|---|
| 298 | line.resize( pos + 1 ); |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | inline void unixDie(const string &why) |
|---|
| 303 | { |
|---|
| 304 | throw runtime_error(why+": "+strerror(errno)); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | string makeHexDump(const string& str); |
|---|
| 308 | void shuffle(vector<DNSResourceRecord>& rrs); |
|---|
| 309 | |
|---|
| 310 | void normalizeTV(struct timeval& tv); |
|---|
| 311 | const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs); |
|---|
| 312 | const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs); |
|---|
| 313 | inline float makeFloat(const struct timeval& tv) |
|---|
| 314 | { |
|---|
| 315 | return tv.tv_sec + tv.tv_usec/1000000.0f; |
|---|
| 316 | } |
|---|
| 317 | struct CIStringCompare: public binary_function<string, string, bool> |
|---|
| 318 | { |
|---|
| 319 | bool operator()(const string& a, const string& b) const |
|---|
| 320 | { |
|---|
| 321 | const unsigned char *p1 = (const unsigned char *) a.c_str(); |
|---|
| 322 | const unsigned char *p2 = (const unsigned char *) b.c_str(); |
|---|
| 323 | int result; |
|---|
| 324 | |
|---|
| 325 | if (p1 == p2) |
|---|
| 326 | return 0; |
|---|
| 327 | |
|---|
| 328 | while ((result = dns_tolower (*p1) - dns_tolower (*p2++)) == 0) |
|---|
| 329 | if (*p1++ == '\0') |
|---|
| 330 | break; |
|---|
| 331 | |
|---|
| 332 | return result < 0; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | bool operator()(const string& a, const char* b) const |
|---|
| 336 | { |
|---|
| 337 | const unsigned char *p1 = (const unsigned char *) a.c_str(); |
|---|
| 338 | const unsigned char *p2 = (const unsigned char *) b; |
|---|
| 339 | int result; |
|---|
| 340 | |
|---|
| 341 | if (p1 == p2) |
|---|
| 342 | return 0; |
|---|
| 343 | |
|---|
| 344 | while ((result = dns_tolower (*p1) - dns_tolower (*p2++)) == 0) |
|---|
| 345 | if (*p1++ == '\0') |
|---|
| 346 | break; |
|---|
| 347 | |
|---|
| 348 | return result < 0; |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | bool operator()(const char* a, const string& b) const |
|---|
| 352 | { |
|---|
| 353 | const unsigned char *p1 = (const unsigned char *) a; |
|---|
| 354 | const unsigned char *p2 = (const unsigned char *) b.c_str(); |
|---|
| 355 | int result; |
|---|
| 356 | |
|---|
| 357 | if (p1 == p2) |
|---|
| 358 | return 0; |
|---|
| 359 | |
|---|
| 360 | while ((result = dns_tolower (*p1) - dns_tolower (*p2++)) == 0) |
|---|
| 361 | if (*p1++ == '\0') |
|---|
| 362 | break; |
|---|
| 363 | |
|---|
| 364 | return result < 0; |
|---|
| 365 | } |
|---|
| 366 | }; |
|---|
| 367 | |
|---|
| 368 | pair<string, string> splitField(const string& inp, char sepa); |
|---|
| 369 | |
|---|
| 370 | inline bool isCanonical(const string& dom) |
|---|
| 371 | { |
|---|
| 372 | if(dom.empty()) |
|---|
| 373 | return false; |
|---|
| 374 | return dom[dom.size()-1]=='.'; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | inline string toCanonic(const string& zone, const string& domain) |
|---|
| 378 | { |
|---|
| 379 | if(isCanonical(domain)) |
|---|
| 380 | return domain; |
|---|
| 381 | string ret=domain; |
|---|
| 382 | ret.append(1,'.'); |
|---|
| 383 | ret.append(zone); |
|---|
| 384 | return ret; |
|---|
| 385 | } |
|---|
| 386 | #endif |
|---|