]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/misc.hh
split out base64 en/decoding
[thirdparty/pdns.git] / pdns / misc.hh
CommitLineData
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"
25
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>
44using namespace std;
728485ca
BH
45bool chopOff(string &domain);
46bool endsOn(const string &domain, const string &suffix);
cc3afe25 47string nowTime();
1d329048 48const string unquotify(const string &item);
12c86877
BH
49string humanDuration(time_t passed);
50void chomp(string &line, const string &delim);
f2c11a48 51bool stripDomainSuffix(string *qname, const string &domain);
12c86877
BH
52void stripLine(string &line);
53string getHostname();
54string urlEncode(const string &text);
a7d50153 55int waitForData(int fd, int seconds, int useconds=0);
092f210a
BH
56uint16_t getShort(const unsigned char *p);
57uint16_t getShort(const char *p);
58uint32_t getLong(const unsigned char *p);
59uint32_t getLong(const char *p);
b636533b 60
092f210a 61inline void putLong(unsigned char* p, uint32_t val)
7bf26383
BH
62{
63 *p++=(val>>24)&0xff;
64 *p++=(val>>16)&0xff;
65 *p++=(val>>8)&0xff;
66 *p++=(val )&0xff;
67
68}
092f210a 69inline void putLong(char* p, uint32_t val)
7bf26383
BH
70{
71 putLong((unsigned char *)p,val);
72}
12c86877 73
7b35aa49 74
092f210a 75inline uint32_t getLong(unsigned char *p)
7b35aa49
BH
76{
77 return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
78}
79
12c86877
BH
80
81struct ServiceTuple
82{
83 string host;
092f210a 84 uint16_t port;
12c86877
BH
85};
86void parseService(const string &descr, ServiceTuple &st);
87
88template <typename Container>
89void
90stringtok (Container &container, string const &in,
91 const char * const delimiters = " \t\n")
92{
93 const string::size_type len = in.length();
94 string::size_type i = 0;
95
96 while (i<len) {
97 // eat leading whitespace
98 i = in.find_first_not_of (delimiters, i);
99 if (i == string::npos)
100 return; // nothing left but white space
101
102 // find the end of the token
103 string::size_type j = in.find_first_of (delimiters, i);
104
105 // push token
106 if (j == string::npos) {
107 container.push_back (in.substr(i));
108 return;
109 } else
110 container.push_back (in.substr(i, j-i));
111
112 // set up for next loop
113 i = j + 1;
114 }
115}
c3d9d009 116const string toLower(const string &upper);
092f210a 117bool IpToU32(const string &str, uint32_t *ip);
12c86877
BH
118string stringerror();
119string itoa(int i);
22c9c86a 120string uitoa(unsigned int i);
12c86877
BH
121
122void dropPrivs(int uid, int gid);
123int makeGidNumeric(const string &group);
124int makeUidNumeric(const string &user);
125void cleanSlashes(string &str);
126
127/** The DTime class can be used for timing statistics with microsecond resolution.
128On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */
129class DTime
130{
131public:
eefd15f9 132 DTime(); //!< Does not set the timer for you! Saves lots of gettimeofday() calls
12c86877
BH
133 DTime(const DTime &dt);
134 time_t time();
135 inline void set(); //!< Reset the timer
136 inline int udiff(); //!< Return the number of microseconds since the timer was last set.
137private:
138 struct timeval d_set;
139};
ac2bb9e7 140const string sockAddrToString(struct sockaddr_in *remote, Utility::socklen_t socklen);
12c86877
BH
141int sendData(const char *buffer, int replen, int outsock);
142
12c86877
BH
143inline void DTime::set()
144{
1ab67463
BH
145 // Utility::
146 gettimeofday(&d_set,0);
12c86877
BH
147}
148
149inline int DTime::udiff()
150{
151 struct timeval now;
1ab67463
BH
152 // Utility::
153 gettimeofday(&now,0);
12c86877
BH
154
155 return 1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec);
156}
157
c3d9d009 158inline const string toLower(const string &upper)
12c86877
BH
159{
160 string reply(upper);
161 for(unsigned int i = 0; i < reply.length(); i++)
162 reply[i] = tolower(reply[i]);
163 return reply;
164}
165
166
52936200
BH
167// Make s uppercase:
168inline string toUpper( const string& s )
169{
170 string r(s);
171 for( unsigned int i = 0; i < s.length(); i++ ) {
172 r[i] = toupper( r[i] );
173 }
174 return r;
175}
176
a6d7640a
BH
177inline double getTime()
178{
179 struct timeval now;
180 Utility::gettimeofday(&now,0);
181
182 return now.tv_sec+now.tv_usec/1000000.0;
183}
184
52936200 185
eefd15f9
BH
186inline void chomp( string& line, const string& delim )
187{
188 string::size_type pos;
189
190 if( ( pos = line.find_last_not_of( delim ) ) != string::npos )
191 {
192 line.resize( pos + 1 );
193 }
194}
195
a640a9d4
BH
196inline void unixDie(const string &why)
197{
198 throw runtime_error(why+": "+strerror(errno));
199}
200
eefd15f9 201
12c86877 202#endif