1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #ifndef _EVENT_UTIL_INTERNAL_H
27 #define _EVENT_UTIL_INTERNAL_H
28
29 #include "event2/event-config.h"
30 #include <errno.h>
31
32
33 #include "log-internal.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #ifdef _EVENT_HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include "event2/util.h"
40
41 #include "ipv6-internal.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 #ifdef _EVENT_inline
49 #define inline _EVENT_inline
50 #endif
51 #ifdef _EVENT___func__
52 #define __func__ _EVENT___func__
53 #endif
54
55
56 #define _EVUTIL_NIL_STMT ((void)0)
57
58
59
60
61
62 #define _EVUTIL_NIL_CONDITION(condition) do { \
63 (void)sizeof(!(condition)); \
64 } while(0)
65
66
67
68
69
70
71
72
73
74 #ifndef WIN32
75
76
77 #define EVUTIL_ERR_RW_RETRIABLE(e) \
78 ((e) == EINTR || (e) == EAGAIN)
79
80 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
81 ((e) == EINTR || (e) == EINPROGRESS)
82
83 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
84 ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED)
85
86
87 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
88 ((e) == ECONNREFUSED)
89
90 #else
91
92 #define EVUTIL_ERR_RW_RETRIABLE(e) \
93 ((e) == WSAEWOULDBLOCK || \
94 (e) == WSAEINTR)
95
96 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
97 ((e) == WSAEWOULDBLOCK || \
98 (e) == WSAEINTR || \
99 (e) == WSAEINPROGRESS || \
100 (e) == WSAEINVAL)
101
102 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
103 EVUTIL_ERR_RW_RETRIABLE(e)
104
105 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
106 ((e) == WSAECONNREFUSED)
107
108 #endif
109
110 #ifdef _EVENT_socklen_t
111 #define socklen_t _EVENT_socklen_t
112 #endif
113
114
115 #ifdef SHUT_RD
116 #define EVUTIL_SHUT_RD SHUT_RD
117 #else
118 #define EVUTIL_SHUT_RD 0
119 #endif
120 #ifdef SHUT_WR
121 #define EVUTIL_SHUT_WR SHUT_WR
122 #else
123 #define EVUTIL_SHUT_WR 1
124 #endif
125 #ifdef SHUT_BOTH
126 #define EVUTIL_SHUT_BOTH SHUT_BOTH
127 #else
128 #define EVUTIL_SHUT_BOTH 2
129 #endif
130
131
132
133
134
135 int EVUTIL_ISALPHA(char c);
136 int EVUTIL_ISALNUM(char c);
137 int EVUTIL_ISSPACE(char c);
138 int EVUTIL_ISDIGIT(char c);
139 int EVUTIL_ISXDIGIT(char c);
140 int EVUTIL_ISPRINT(char c);
141 int EVUTIL_ISLOWER(char c);
142 int EVUTIL_ISUPPER(char c);
143 char EVUTIL_TOUPPER(char c);
144 char EVUTIL_TOLOWER(char c);
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 #define EVUTIL_UPCAST(ptr, type, field) \
162 ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
163
164
165
166
167 int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode);
168
169 int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
170 int is_binary);
171
172 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
173
174 int evutil_socket_finished_connecting(evutil_socket_t fd);
175
176 int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
177
178 int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
179 ev_socklen_t *socklen, int port);
180
181 const char *evutil_getenv(const char *name);
182
183 long _evutil_weakrand(void);
184
185
186
187 #if defined(__GNUC__) && __GNUC__ >= 3
188 #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
189 #else
190 #define EVUTIL_UNLIKELY(p) (p)
191 #endif
192
193
194 #ifdef NDEBUG
195 #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
196 #define EVUTIL_FAILURE_CHECK(cond) 0
197 #else
198 #define EVUTIL_ASSERT(cond) \
199 do { \
200 if (EVUTIL_UNLIKELY(!(cond))) { \
201 event_errx(_EVENT_ERR_ABORT, \
202 "%s:%d: Assertion %s failed in %s", \
203 __FILE__,__LINE__,#cond,__func__); \
204 \
205 \
206 (void)fprintf(stderr, \
207 "%s:%d: Assertion %s failed in %s", \
208 __FILE__,__LINE__,#cond,__func__); \
209 abort(); \
210 } \
211 } while (0)
212 #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
213 #endif
214
215 #ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE
216
217
218
219 struct sockaddr_storage {
220 union {
221 struct sockaddr ss_sa;
222 struct sockaddr_in ss_sin;
223 struct sockaddr_in6 ss_sin6;
224 char ss_padding[128];
225 } ss_union;
226 };
227 #define ss_family ss_union.ss_sa.sa_family
228 #endif
229
230
231
232
233 #define EVUTIL_EAI_NEED_RESOLVE -90002
234
235 struct evdns_base;
236 struct evdns_getaddrinfo_request;
237 typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
238 struct evdns_base *base,
239 const char *nodename, const char *servname,
240 const struct evutil_addrinfo *hints_in,
241 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
242
243 void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
244
245 struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
246 ev_socklen_t socklen, const struct evutil_addrinfo *hints);
247 struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
248 struct evutil_addrinfo *append);
249 void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
250 int evutil_getaddrinfo_common(const char *nodename, const char *servname,
251 struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
252
253 int evutil_getaddrinfo_async(struct evdns_base *dns_base,
254 const char *nodename, const char *servname,
255 const struct evutil_addrinfo *hints_in,
256 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
257
258
259
260 int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
261
262
263
264
265
266
267
268 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
269
270 long evutil_tv_to_msec(const struct timeval *tv);
271
272 int evutil_hex_char_to_int(char c);
273
274 #ifdef WIN32
275 HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
276 #endif
277
278 #ifndef EV_SIZE_FMT
279 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
280 #define EV_U64_FMT "%I64u"
281 #define EV_I64_FMT "%I64d"
282 #define EV_I64_ARG(x) ((__int64)(x))
283 #define EV_U64_ARG(x) ((unsigned __int64)(x))
284 #else
285 #define EV_U64_FMT "%llu"
286 #define EV_I64_FMT "%lld"
287 #define EV_I64_ARG(x) ((long long)(x))
288 #define EV_U64_ARG(x) ((unsigned long long)(x))
289 #endif
290 #endif
291
292 #ifdef _WIN32
293 #define EV_SOCK_FMT EV_I64_FMT
294 #define EV_SOCK_ARG(x) EV_I64_ARG((x))
295 #else
296 #define EV_SOCK_FMT "%d"
297 #define EV_SOCK_ARG(x) (x)
298 #endif
299
300 #if defined(__STDC__) && defined(__STDC_VERSION__)
301 #if (__STDC_VERSION__ >= 199901L)
302 #define EV_SIZE_FMT "%zu"
303 #define EV_SSIZE_FMT "%zd"
304 #define EV_SIZE_ARG(x) (x)
305 #define EV_SSIZE_ARG(x) (x)
306 #endif
307 #endif
308
309 #ifndef EV_SIZE_FMT
310 #if (_EVENT_SIZEOF_SIZE_T <= _EVENT_SIZEOF_LONG)
311 #define EV_SIZE_FMT "%lu"
312 #define EV_SSIZE_FMT "%ld"
313 #define EV_SIZE_ARG(x) ((unsigned long)(x))
314 #define EV_SSIZE_ARG(x) ((long)(x))
315 #else
316 #define EV_SIZE_FMT EV_U64_FMT
317 #define EV_SSIZE_FMT EV_I64_FMT
318 #define EV_SIZE_ARG(x) EV_U64_ARG(x)
319 #define EV_SSIZE_ARG(x) EV_I64_ARG(x)
320 #endif
321 #endif
322
323 void evutil_memclear_(void *mem, size_t len);
324
325 #ifdef __cplusplus
326 }
327 #endif
328
329 #endif