| 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 | // Utility class specification. |
|---|
| 20 | |
|---|
| 21 | #ifndef UTILITY_HH |
|---|
| 22 | #define UTILITY_HH |
|---|
| 23 | |
|---|
| 24 | #ifndef WIN32 |
|---|
| 25 | # include "config.h" |
|---|
| 26 | #endif // WIN32 |
|---|
| 27 | |
|---|
| 28 | #ifdef NEED_POSIX_TYPEDEF |
|---|
| 29 | typedef unsigned short int u_int16_t; |
|---|
| 30 | typedef unsigned int u_int32_t; |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | #ifndef WIN32 |
|---|
| 35 | # include <arpa/inet.h> |
|---|
| 36 | # include <netinet/in.h> |
|---|
| 37 | # include <sys/socket.h> |
|---|
| 38 | # include <sys/time.h> |
|---|
| 39 | # include <sys/uio.h> |
|---|
| 40 | # include <signal.h> |
|---|
| 41 | # include <pthread.h> |
|---|
| 42 | # include <semaphore.h> |
|---|
| 43 | # include <signal.h> |
|---|
| 44 | # include <errno.h> |
|---|
| 45 | #else |
|---|
| 46 | // Disable debug info truncation warning. |
|---|
| 47 | # pragma warning ( disable: 4786 ) |
|---|
| 48 | # pragma warning ( disable: 4503 ) |
|---|
| 49 | # pragma warning ( disable: 4101 ) |
|---|
| 50 | |
|---|
| 51 | # define WINDOWS_LEAN_AND_MEAN |
|---|
| 52 | # include <windows.h> |
|---|
| 53 | # include <signal.h> |
|---|
| 54 | # include <map> |
|---|
| 55 | |
|---|
| 56 | // For scope fix. |
|---|
| 57 | # define for if ( false ) {} else for |
|---|
| 58 | |
|---|
| 59 | # define ETIMEDOUT WSAETIMEDOUT |
|---|
| 60 | # define EINPROGRESS WSAEWOULDBLOCK |
|---|
| 61 | |
|---|
| 62 | # define AF_INET6 -1 |
|---|
| 63 | |
|---|
| 64 | # define VERSION "2.9.2-WIN32" |
|---|
| 65 | |
|---|
| 66 | # define snprintf _snprintf |
|---|
| 67 | |
|---|
| 68 | // Custom bittypes. |
|---|
| 69 | typedef unsigned char int8_t; |
|---|
| 70 | typedef unsigned int int16_t; |
|---|
| 71 | typedef unsigned long int32_t; |
|---|
| 72 | typedef unsigned char u_int8_t; |
|---|
| 73 | typedef unsigned int u_int16_t; |
|---|
| 74 | typedef unsigned long u_int32_t; |
|---|
| 75 | |
|---|
| 76 | struct in6_addr { |
|---|
| 77 | unsigned char s6_addr[16]; /* IPv6 address */ |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | struct sockaddr_in6 { |
|---|
| 81 | unsigned short sin6_family; /* AF_INET6 */ |
|---|
| 82 | unsigned short sin6_port; /* transport layer port # */ |
|---|
| 83 | unsigned long sin6_flowinfo; /* IPv6 flow information */ |
|---|
| 84 | struct in6_addr sin6_addr; /* IPv6 address */ |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | #endif // WIN32 |
|---|
| 88 | |
|---|
| 89 | #include <semaphore.h> |
|---|
| 90 | #include <string> |
|---|
| 91 | |
|---|
| 92 | using namespace std; |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | //! A semaphore class. |
|---|
| 96 | class Semaphore |
|---|
| 97 | { |
|---|
| 98 | private: |
|---|
| 99 | sem_t *m_pSemaphore; |
|---|
| 100 | #ifdef WIN32 |
|---|
| 101 | typedef int sem_value_t; |
|---|
| 102 | |
|---|
| 103 | //! The semaphore. |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | //! Semaphore counter. |
|---|
| 108 | long m_counter; |
|---|
| 109 | |
|---|
| 110 | #else |
|---|
| 111 | typedef int sem_value_t; |
|---|
| 112 | |
|---|
| 113 | u_int32_t m_magic; |
|---|
| 114 | pthread_mutex_t m_lock; |
|---|
| 115 | pthread_cond_t m_gtzero; |
|---|
| 116 | sem_value_t m_count; |
|---|
| 117 | u_int32_t m_nwaiters; |
|---|
| 118 | #endif |
|---|
| 119 | |
|---|
| 120 | protected: |
|---|
| 121 | public: |
|---|
| 122 | //! Default constructor. |
|---|
| 123 | Semaphore( unsigned int value = 0 ); |
|---|
| 124 | |
|---|
| 125 | //! Destructor. |
|---|
| 126 | ~Semaphore( void ); |
|---|
| 127 | |
|---|
| 128 | //! Posts to a semaphore. |
|---|
| 129 | int post( void ); |
|---|
| 130 | |
|---|
| 131 | //! Waits for a semaphore. |
|---|
| 132 | int wait( void ); |
|---|
| 133 | |
|---|
| 134 | //! Tries to wait for a semaphore. |
|---|
| 135 | int tryWait( void ); |
|---|
| 136 | |
|---|
| 137 | //! Retrieves the semaphore value. |
|---|
| 138 | int getValue( Semaphore::sem_value_t *sval ); |
|---|
| 139 | |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | //! This is a utility class used for platform independant abstraction. |
|---|
| 144 | class Utility |
|---|
| 145 | { |
|---|
| 146 | #ifdef WIN32 |
|---|
| 147 | private: |
|---|
| 148 | static int inet_pton4( const char *src, void *dst ); |
|---|
| 149 | static int inet_pton6( const char *src, void *dst ); |
|---|
| 150 | |
|---|
| 151 | static const char *inet_ntop4( const char *src, char *dst, size_t size ); |
|---|
| 152 | static const char *inet_ntop6( const char *src, char *dst, size_t size ); |
|---|
| 153 | |
|---|
| 154 | #endif // WIN32 |
|---|
| 155 | |
|---|
| 156 | public: |
|---|
| 157 | #ifdef WIN32 |
|---|
| 158 | |
|---|
| 159 | //! iovec structure for windows. |
|---|
| 160 | typedef struct |
|---|
| 161 | { |
|---|
| 162 | void *iov_base; //!< Base address. |
|---|
| 163 | size_t iov_len; //!< Number of bytes. |
|---|
| 164 | } iovec; |
|---|
| 165 | |
|---|
| 166 | // A few type defines. |
|---|
| 167 | typedef DWORD pid_t; |
|---|
| 168 | typedef SOCKET sock_t; |
|---|
| 169 | typedef int socklen_t; |
|---|
| 170 | |
|---|
| 171 | #else |
|---|
| 172 | typedef ::iovec iovec; |
|---|
| 173 | typedef ::pid_t pid_t; |
|---|
| 174 | typedef int sock_t; |
|---|
| 175 | typedef ::socklen_t socklen_t; |
|---|
| 176 | |
|---|
| 177 | #endif // WIN32 |
|---|
| 178 | |
|---|
| 179 | //! Closes a socket. |
|---|
| 180 | static int closesocket( sock_t socket ); |
|---|
| 181 | |
|---|
| 182 | //! Returns the process id of the current process. |
|---|
| 183 | static pid_t getpid( void ); |
|---|
| 184 | |
|---|
| 185 | //! Gets the current time. |
|---|
| 186 | static int gettimeofday( struct timeval *tv, void *tz = NULL ); |
|---|
| 187 | |
|---|
| 188 | //! Converts an address from dot and numbers format to binary data. |
|---|
| 189 | static int inet_aton( const char *cp, struct in_addr *inp ); |
|---|
| 190 | |
|---|
| 191 | //! Converts an address from presentation format to network format. |
|---|
| 192 | static int inet_pton( int af, const char *src, void *dst ); |
|---|
| 193 | |
|---|
| 194 | //! The inet_ntop() function converts an address from network format (usually a struct in_addr or some other binary form, in network byte order) to presentation format. |
|---|
| 195 | static const char *inet_ntop( int af, const char *src, char *dst, size_t size ); |
|---|
| 196 | |
|---|
| 197 | //! Retrieves a gid using a groupname. |
|---|
| 198 | static int makeGidNumeric( const string & group ); |
|---|
| 199 | |
|---|
| 200 | //! Retrieves an uid using an username. |
|---|
| 201 | static int makeUidNumeric( const string & username ); |
|---|
| 202 | |
|---|
| 203 | //! Writes a vector. |
|---|
| 204 | static int writev( Utility::sock_t socket, const iovec *vector, size_t count ); |
|---|
| 205 | //! Returns a random number. |
|---|
| 206 | static long int random( void ); |
|---|
| 207 | |
|---|
| 208 | //! Sets the random seed. |
|---|
| 209 | static void srandom( unsigned int seed ); |
|---|
| 210 | |
|---|
| 211 | //! Compares two strings and ignores case. |
|---|
| 212 | static int strcasecmp( const char *s1, const char *s2 ); |
|---|
| 213 | |
|---|
| 214 | //! Drops the program's privileges. |
|---|
| 215 | static void dropPrivs( int uid, int gid ); |
|---|
| 216 | |
|---|
| 217 | //! Sets the socket into blocking mode. |
|---|
| 218 | static bool setBlocking( Utility::sock_t socket ); |
|---|
| 219 | |
|---|
| 220 | //! Sets the socket into non-blocking mode. |
|---|
| 221 | static bool setNonBlocking( Utility::sock_t socket ); |
|---|
| 222 | |
|---|
| 223 | //! Sleeps for a number of seconds. |
|---|
| 224 | static unsigned int sleep( unsigned int seconds ); |
|---|
| 225 | |
|---|
| 226 | //! Sleeps for a number of microseconds. |
|---|
| 227 | static void usleep( unsigned long usec ); |
|---|
| 228 | |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | #endif // UTILITY_HH |
|---|