| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002 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 as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | #ifndef MISC_HH |
|---|
| 20 | #define MISC_HH |
|---|
| 21 | /* (C) 2002 POWERDNS.COM BV */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include "utility.hh" |
|---|
| 25 | |
|---|
| 26 | #ifndef WIN32 |
|---|
| 27 | # include <sys/time.h> |
|---|
| 28 | # include <sys/types.h> |
|---|
| 29 | # include <sys/socket.h> |
|---|
| 30 | # include <time.h> |
|---|
| 31 | |
|---|
| 32 | #else |
|---|
| 33 | # pragma warning ( disable: 4786 ) |
|---|
| 34 | # define WINDOWS_LEAN_AND_MEAN |
|---|
| 35 | # include <windows.h> |
|---|
| 36 | # include "utility.hh" |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | #endif // WIN32 |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #include <string> |
|---|
| 43 | #include <ctype.h> |
|---|
| 44 | using namespace std; |
|---|
| 45 | bool chopOff(string &domain); |
|---|
| 46 | bool endsOn(const string &domain, const string &suffix); |
|---|
| 47 | string nowTime(); |
|---|
| 48 | const string unquotify(const string &item); |
|---|
| 49 | int matchNetmask(const char *address, const char *omask); |
|---|
| 50 | string humanDuration(time_t passed); |
|---|
| 51 | void chomp(string &line, const string &delim); |
|---|
| 52 | bool stripDomainSuffix(string *qname, const string &domain); |
|---|
| 53 | void stripLine(string &line); |
|---|
| 54 | string getHostname(); |
|---|
| 55 | string urlEncode(const string &text); |
|---|
| 56 | int waitForData(int fd, int seconds); |
|---|
| 57 | u_int16_t getShort(const unsigned char *p); |
|---|
| 58 | u_int16_t getShort(const char *p); |
|---|
| 59 | inline void putLong(unsigned char* p, u_int32_t val) |
|---|
| 60 | { |
|---|
| 61 | *p++=(val>>24)&0xff; |
|---|
| 62 | *p++=(val>>16)&0xff; |
|---|
| 63 | *p++=(val>>8)&0xff; |
|---|
| 64 | *p++=(val )&0xff; |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | inline void putLong(char* p, u_int32_t val) |
|---|
| 68 | { |
|---|
| 69 | putLong((unsigned char *)p,val); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | inline u_int32_t getLong(unsigned char *p) |
|---|
| 74 | { |
|---|
| 75 | return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void upperCase(string& s); |
|---|
| 79 | |
|---|
| 80 | struct ServiceTuple |
|---|
| 81 | { |
|---|
| 82 | string host; |
|---|
| 83 | u_int16_t port; |
|---|
| 84 | }; |
|---|
| 85 | void parseService(const string &descr, ServiceTuple &st); |
|---|
| 86 | |
|---|
| 87 | template <typename Container> |
|---|
| 88 | void |
|---|
| 89 | stringtok (Container &container, string const &in, |
|---|
| 90 | const char * const delimiters = " \t\n") |
|---|
| 91 | { |
|---|
| 92 | const string::size_type len = in.length(); |
|---|
| 93 | string::size_type i = 0; |
|---|
| 94 | |
|---|
| 95 | while (i<len) { |
|---|
| 96 | // eat leading whitespace |
|---|
| 97 | i = in.find_first_not_of (delimiters, i); |
|---|
| 98 | if (i == string::npos) |
|---|
| 99 | return; // nothing left but white space |
|---|
| 100 | |
|---|
| 101 | // find the end of the token |
|---|
| 102 | string::size_type j = in.find_first_of (delimiters, i); |
|---|
| 103 | |
|---|
| 104 | // push token |
|---|
| 105 | if (j == string::npos) { |
|---|
| 106 | container.push_back (in.substr(i)); |
|---|
| 107 | return; |
|---|
| 108 | } else |
|---|
| 109 | container.push_back (in.substr(i, j-i)); |
|---|
| 110 | |
|---|
| 111 | // set up for next loop |
|---|
| 112 | i = j + 1; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | string toLower(const string &upper); |
|---|
| 116 | |
|---|
| 117 | string stringerror(); |
|---|
| 118 | string itoa(int i); |
|---|
| 119 | |
|---|
| 120 | void dropPrivs(int uid, int gid); |
|---|
| 121 | int makeGidNumeric(const string &group); |
|---|
| 122 | int makeUidNumeric(const string &user); |
|---|
| 123 | void cleanSlashes(string &str); |
|---|
| 124 | |
|---|
| 125 | /** The DTime class can be used for timing statistics with microsecond resolution. |
|---|
| 126 | On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */ |
|---|
| 127 | class DTime |
|---|
| 128 | { |
|---|
| 129 | public: |
|---|
| 130 | DTime(); //!< This constructor calls set() for you so the timer is set on construction |
|---|
| 131 | DTime(const DTime &dt); |
|---|
| 132 | time_t time(); |
|---|
| 133 | inline void set(); //!< Reset the timer |
|---|
| 134 | inline int udiff(); //!< Return the number of microseconds since the timer was last set. |
|---|
| 135 | private: |
|---|
| 136 | struct timeval d_set; |
|---|
| 137 | }; |
|---|
| 138 | const string sockAddrToString(struct sockaddr_in *remote, Utility::socklen_t socklen); |
|---|
| 139 | int sendData(const char *buffer, int replen, int outsock); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | inline void DTime::set() |
|---|
| 143 | { |
|---|
| 144 | Utility::gettimeofday(&d_set,0); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | inline int DTime::udiff() |
|---|
| 148 | { |
|---|
| 149 | struct timeval now; |
|---|
| 150 | Utility::gettimeofday(&now,0); |
|---|
| 151 | |
|---|
| 152 | return 1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | inline string toLower(const string &upper) |
|---|
| 156 | { |
|---|
| 157 | string reply(upper); |
|---|
| 158 | for(unsigned int i = 0; i < reply.length(); i++) |
|---|
| 159 | reply[i] = tolower(reply[i]); |
|---|
| 160 | return reply; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | #endif |
|---|