root/trunk/pdns/pdns/misc.hh @ 137

Revision 137, 4.1 KB (checked in by ahu, 10 years ago)

recursor work, alignment issues

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
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>
44using namespace std;
45bool chopOff(string &domain);
46bool endsOn(const string &domain, const string &suffix);
47string nowTime();
48const string unquotify(const string &item);
49int matchNetmask(const char *address, const char *omask);
50string humanDuration(time_t passed);
51void chomp(string &line, const string &delim);
52bool stripDomainSuffix(string *qname, const string &domain);
53void stripLine(string &line);
54string getHostname();
55string urlEncode(const string &text);
56int waitForData(int fd, int seconds);
57u_int16_t getShort(const unsigned char *p);
58u_int16_t getShort(const char *p);
59inline 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}
67inline void putLong(char* p, u_int32_t val)
68{
69  putLong((unsigned char *)p,val);
70}
71
72
73inline u_int32_t getLong(unsigned char *p)
74{
75  return (p[0]<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
76}
77
78void upperCase(string& s);
79
80struct ServiceTuple
81{
82  string host;
83  u_int16_t port;
84};
85void parseService(const string &descr, ServiceTuple &st);
86
87template <typename Container>
88void
89stringtok (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}
115string toLower(const string &upper);
116
117string stringerror();
118string itoa(int i);
119
120void dropPrivs(int uid, int gid);
121int makeGidNumeric(const string &group);
122int makeUidNumeric(const string &user);
123void cleanSlashes(string &str);
124
125/** The DTime class can be used for timing statistics with microsecond resolution.
126On 32 bits systems this means that 2147 seconds is the longest time that can be measured. */
127class DTime 
128{
129public:
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.
135private:
136  struct timeval d_set;
137};
138const string sockAddrToString(struct sockaddr_in *remote, Utility::socklen_t socklen);
139int sendData(const char *buffer, int replen, int outsock);
140
141
142inline void DTime::set()
143{
144  Utility::gettimeofday(&d_set,0);
145}
146
147inline 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
155inline 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
Note: See TracBrowser for help on using the browser.