]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
a640a9d4 | 3 | Copyright (C) 2002-2005 PowerDNS.COM BV |
12c86877 BH |
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 */ | |
cc3afe25 | 22 | |
12c86877 BH |
23 | |
24 | #include "utility.hh" | |
e67e250f | 25 | #include "dns.hh" |
12c86877 BH |
26 | #ifndef WIN32 |
27 | # include <sys/time.h> | |
28 | # include <sys/types.h> | |
29 | # include <sys/socket.h> | |
cc3afe25 | 30 | # include <time.h> |
cc3afe25 BH |
31 | #else |
32 | # pragma warning ( disable: 4786 ) | |
33 | # define WINDOWS_LEAN_AND_MEAN | |
34 | # include <windows.h> | |
35 | # include "utility.hh" | |
36 | ||
37 | ||
38 | #endif // WIN32 | |
39 | ||
3de83124 | 40 | #include <deque> |
a640a9d4 | 41 | #include <stdexcept> |
12c86877 BH |
42 | #include <string> |
43 | #include <ctype.h> | |
e67e250f BH |
44 | #include <vector> |
45 | ||
12c86877 | 46 | using namespace std; |
728485ca BH |
47 | bool chopOff(string &domain); |
48 | bool endsOn(const string &domain, const string &suffix); | |
cc3afe25 | 49 | string nowTime(); |
1d329048 | 50 | const string unquotify(const string &item); |
12c86877 BH |
51 | string humanDuration(time_t passed); |
52 | void chomp(string &line, const string &delim); | |
f2c11a48 | 53 | bool stripDomainSuffix(string *qname, const string &domain); |
12c86877 BH |
54 | void stripLine(string &line); |
55 | string getHostname(); | |
56 | string urlEncode(const string &text); | |
a7d50153 | 57 | int waitForData(int fd, int seconds, int useconds=0); |
092f210a BH |
58 | uint16_t getShort(const unsigned char *p); |
59 | uint16_t getShort(const char *p); | |
60 | uint32_t getLong(const unsigned char *p); | |
61 | uint32_t getLong(const char *p); | |
b636533b | 62 | |
092f210a | 63 | inline void putLong(unsigned char* p, uint32_t val) |
7bf26383 BH |
64 | { |
65 | *p++=(val>>24)&0xff; | |
66 | *p++=(val>>16)&0xff; | |
67 | *p++=(val>>8)&0xff; | |
68 | *p++=(val )&0xff; | |
69 | ||
70 | } | |
092f210a | 71 | inline void putLong(char* p, uint32_t val) |
7bf26383 BH |
72 | { |
73 | putLong((unsigned char *)p,val); | |
74 | } | |
12c86877 | 75 | |
7b35aa49 | 76 | |
092f210a | 77 | inline uint32_t getLong(unsigned char *p) |
7b35aa49 BH |
78 | { |
79 | return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3]; | |
80 | } | |
81 | ||
12c86877 BH |
82 | |
83 | struct ServiceTuple | |
84 | { | |
85 | string host; | |
092f210a | 86 | uint16_t port; |
12c86877 BH |
87 | }; |
88 | void parseService(const string &descr, ServiceTuple &st); | |
89 | ||
90 | template <typename Container> | |
91 | void | |
92 | stringtok (Container &container, string const &in, | |
93 | const char * const delimiters = " \t\n") | |
94 | { | |
95 | const string::size_type len = in.length(); | |
96 | string::size_type i = 0; | |
97 | ||
98 | while (i<len) { | |
99 | // eat leading whitespace | |
100 | i = in.find_first_not_of (delimiters, i); | |
101 | if (i == string::npos) | |
102 | return; // nothing left but white space | |
103 | ||
104 | // find the end of the token | |
105 | string::size_type j = in.find_first_of (delimiters, i); | |
106 | ||
107 | // push token | |
108 | if (j == string::npos) { | |
109 | container.push_back (in.substr(i)); | |
110 | return; | |
111 | } else | |
112 | container.push_back (in.substr(i, j-i)); | |
113 | ||
114 | // set up for next loop | |
115 | i = j + 1; | |
116 | } | |
117 | } | |
c3d9d009 | 118 | const string toLower(const string &upper); |
b0d4fb45 | 119 | const string toLowerCanonic(const string &upper); |
092f210a | 120 | bool IpToU32(const string &str, uint32_t *ip); |
12c86877 BH |
121 | string stringerror(); |
122 | string itoa(int i); | |
22c9c86a | 123 | string uitoa(unsigned int i); |
12c86877 BH |
124 | |
125 | void dropPrivs(int uid, int gid); | |
126 | int makeGidNumeric(const string &group); | |
127 | int makeUidNumeric(const string &user); | |
128 | void cleanSlashes(string &str); | |
129 | ||
130 | /** The DTime class can be used for timing statistics with microsecond resolution. | |
131 | On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */ | |
132 | class DTime | |
133 | { | |
134 | public: | |
eefd15f9 | 135 | DTime(); //!< Does not set the timer for you! Saves lots of gettimeofday() calls |
12c86877 BH |
136 | DTime(const DTime &dt); |
137 | time_t time(); | |
138 | inline void set(); //!< Reset the timer | |
139 | inline int udiff(); //!< Return the number of microseconds since the timer was last set. | |
88358c9b BH |
140 | void setTimeval(const struct timeval& tv) |
141 | { | |
142 | d_set=tv; | |
143 | } | |
144 | struct timeval getTimeval() | |
145 | { | |
146 | return d_set; | |
147 | } | |
12c86877 BH |
148 | private: |
149 | struct timeval d_set; | |
150 | }; | |
ac2bb9e7 | 151 | const string sockAddrToString(struct sockaddr_in *remote, Utility::socklen_t socklen); |
12c86877 BH |
152 | int sendData(const char *buffer, int replen, int outsock); |
153 | ||
12c86877 BH |
154 | inline void DTime::set() |
155 | { | |
1ab67463 BH |
156 | // Utility:: |
157 | gettimeofday(&d_set,0); | |
12c86877 BH |
158 | } |
159 | ||
160 | inline int DTime::udiff() | |
161 | { | |
162 | struct timeval now; | |
12c86877 | 163 | |
fe213470 BH |
164 | gettimeofday(&now,0); |
165 | int ret=1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec); | |
166 | d_set=now; | |
167 | return ret; | |
12c86877 BH |
168 | } |
169 | ||
f5be3a1e BH |
170 | inline bool dns_isspace(char c) |
171 | { | |
172 | return c==' ' || c=='\t' || c=='\r' || c=='\n'; | |
173 | } | |
174 | ||
175 | inline const char dns_tolower(char c) | |
176 | { | |
177 | if(c>='A' && c<='Z') | |
178 | c+='a'-'A'; | |
179 | return c; | |
180 | } | |
181 | ||
c3d9d009 | 182 | inline const string toLower(const string &upper) |
12c86877 BH |
183 | { |
184 | string reply(upper); | |
185 | for(unsigned int i = 0; i < reply.length(); i++) | |
f5be3a1e | 186 | reply[i] = dns_tolower(reply[i]); |
12c86877 BH |
187 | return reply; |
188 | } | |
189 | ||
b0d4fb45 BH |
190 | inline const string toLowerCanonic(const string &upper) |
191 | { | |
192 | string reply(upper); | |
193 | if(!upper.empty()) { | |
194 | unsigned int i; | |
195 | for(i = 0; i < reply.length() ; i++) | |
f5be3a1e | 196 | reply[i] = dns_tolower(reply[i]); |
b0d4fb45 BH |
197 | |
198 | if(reply[i-1]=='.') | |
199 | reply.resize(i-1); | |
200 | } | |
201 | ||
202 | return reply; | |
203 | } | |
204 | ||
205 | ||
12c86877 | 206 | |
52936200 BH |
207 | // Make s uppercase: |
208 | inline string toUpper( const string& s ) | |
209 | { | |
210 | string r(s); | |
211 | for( unsigned int i = 0; i < s.length(); i++ ) { | |
212 | r[i] = toupper( r[i] ); | |
213 | } | |
214 | return r; | |
215 | } | |
216 | ||
a6d7640a BH |
217 | inline double getTime() |
218 | { | |
219 | struct timeval now; | |
220 | Utility::gettimeofday(&now,0); | |
221 | ||
222 | return now.tv_sec+now.tv_usec/1000000.0; | |
223 | } | |
224 | ||
52936200 | 225 | |
eefd15f9 BH |
226 | inline void chomp( string& line, const string& delim ) |
227 | { | |
228 | string::size_type pos; | |
229 | ||
230 | if( ( pos = line.find_last_not_of( delim ) ) != string::npos ) | |
231 | { | |
232 | line.resize( pos + 1 ); | |
233 | } | |
234 | } | |
235 | ||
a640a9d4 BH |
236 | inline void unixDie(const string &why) |
237 | { | |
238 | throw runtime_error(why+": "+strerror(errno)); | |
239 | } | |
240 | ||
2db9c30e | 241 | string makeHexDump(const string& str); |
e67e250f | 242 | void shuffle(vector<DNSResourceRecord>& rrs); |
88358c9b BH |
243 | |
244 | void normalizeTV(struct timeval& tv); | |
245 | const struct timeval operator+(const struct timeval& lhs, const struct timeval& rhs); | |
246 | const struct timeval operator-(const struct timeval& lhs, const struct timeval& rhs); | |
247 | inline float makeFloat(const struct timeval& tv) | |
248 | { | |
249 | return tv.tv_sec + tv.tv_usec/1000000.0; | |
250 | } | |
12c86877 | 251 | #endif |