]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
2 | * This file is part of PowerDNS or dnsdist. |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
12c86877 BH |
22 | // Utility class specification. |
23 | ||
24 | #ifndef UTILITY_HH | |
25 | #define UTILITY_HH | |
26 | ||
1258abe0 | 27 | #ifdef NEED_POSIX_TYPEDEF |
092f210a BH |
28 | typedef unsigned char uint8_t; |
29 | typedef unsigned short int uint16_t; | |
30 | typedef unsigned int uint32_t; | |
705f31ae | 31 | typedef unsigned long long uint64_t; |
1258abe0 BH |
32 | #endif |
33 | ||
76473b92 KM |
34 | #include <arpa/inet.h> |
35 | #include <netinet/in.h> | |
36 | #include <sys/socket.h> | |
37 | #include <sys/time.h> | |
38 | #include <sys/uio.h> | |
39 | #include <signal.h> | |
40 | #include <pthread.h> | |
41 | #include <semaphore.h> | |
42 | #include <signal.h> | |
43 | #include <errno.h> | |
44 | #include <unistd.h> | |
12c86877 BH |
45 | #include <string> |
46 | ||
10f4eea8 | 47 | #include "namespaces.hh" |
12c86877 | 48 | |
bbce5bf0 BH |
49 | //! A semaphore class. |
50 | class Semaphore | |
51 | { | |
52 | private: | |
bbce5bf0 BH |
53 | typedef int sem_value_t; |
54 | ||
82442a51 | 55 | #if defined(_AIX) || defined(__APPLE__) |
cefd8904 PD |
56 | uint32_t m_magic; |
57 | pthread_mutex_t m_lock; | |
58 | pthread_cond_t m_gtzero; | |
59 | sem_value_t m_count; | |
60 | uint32_t m_nwaiters; | |
2ce1b817 | 61 | #else |
c2826d2e | 62 | std::unique_ptr<sem_t> m_pSemaphore; |
2ce1b817 | 63 | #endif |
bbce5bf0 BH |
64 | |
65 | protected: | |
66 | public: | |
f6c19c4f CHB |
67 | Semaphore(const Semaphore&) = delete; |
68 | void operator=(const Semaphore&) = delete; | |
bbce5bf0 BH |
69 | //! Default constructor. |
70 | Semaphore( unsigned int value = 0 ); | |
71 | ||
72 | //! Destructor. | |
73 | ~Semaphore( void ); | |
74 | ||
75 | //! Posts to a semaphore. | |
76 | int post( void ); | |
77 | ||
78 | //! Waits for a semaphore. | |
79 | int wait( void ); | |
80 | ||
81 | //! Tries to wait for a semaphore. | |
82 | int tryWait( void ); | |
83 | ||
84 | //! Retrieves the semaphore value. | |
85 | int getValue( Semaphore::sem_value_t *sval ); | |
bbce5bf0 | 86 | }; |
12c86877 | 87 | |
7c696097 | 88 | //! This is a utility class used for platform independent abstraction. |
12c86877 BH |
89 | class Utility |
90 | { | |
12c86877 | 91 | public: |
12c86877 | 92 | typedef ::iovec iovec; |
76473b92 KM |
93 | typedef ::pid_t pid_t; |
94 | typedef int sock_t; | |
95 | typedef ::socklen_t socklen_t; | |
12c86877 | 96 | |
c7c7edb5 IM |
97 | //! Connect with timeout |
98 | // Returns: | |
99 | // > 0 on success | |
100 | // -1 on error | |
101 | // 0 on timeout | |
102 | static int timed_connect(sock_t sock, | |
103 | const sockaddr *addr, | |
104 | socklen_t sockaddr_size, | |
105 | int timeout_sec, | |
106 | int timeout_usec); | |
107 | ||
12c86877 BH |
108 | //! Returns the process id of the current process. |
109 | static pid_t getpid( void ); | |
110 | ||
111 | //! Gets the current time. | |
112 | static int gettimeofday( struct timeval *tv, void *tz = NULL ); | |
113 | ||
114 | //! Converts an address from dot and numbers format to binary data. | |
115 | static int inet_aton( const char *cp, struct in_addr *inp ); | |
116 | ||
117 | //! Converts an address from presentation format to network format. | |
118 | static int inet_pton( int af, const char *src, void *dst ); | |
119 | ||
120 | //! 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. | |
121 | static const char *inet_ntop( int af, const char *src, char *dst, size_t size ); | |
122 | ||
12c86877 BH |
123 | //! Writes a vector. |
124 | static int writev( Utility::sock_t socket, const iovec *vector, size_t count ); | |
12c86877 | 125 | |
8342eb4f | 126 | //! Sets the random seed. |
b51ef4f9 OM |
127 | static void srandom(void); |
128 | ||
f1d6a7ce | 129 | //! Drops the program's group privileges. |
76ba685e | 130 | static void dropGroupPrivs( uid_t uid, gid_t gid ); |
f1d6a7ce KM |
131 | |
132 | //! Drops the program's user privileges. | |
76ba685e | 133 | static void dropUserPrivs( uid_t uid ); |
12c86877 | 134 | |
fec7dd5a SS |
135 | //! Sets the socket into Bind-any mode |
136 | static void setBindAny ( int af, Utility::sock_t socket ); | |
42c235e5 | 137 | |
12c86877 BH |
138 | //! Sleeps for a number of seconds. |
139 | static unsigned int sleep( unsigned int seconds ); | |
140 | ||
141 | //! Sleeps for a number of microseconds. | |
142 | static void usleep( unsigned long usec ); | |
49f72da1 BH |
143 | |
144 | static time_t timegm(struct tm *tm); | |
c96765da | 145 | |
12c86877 BH |
146 | }; |
147 | ||
148 | ||
149 | #endif // UTILITY_HH |