xref: /libevent-2.1.12/include/event2/util.h (revision 946b5841)
1 /*
2  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef EVENT2_UTIL_H_INCLUDED_
27 #define EVENT2_UTIL_H_INCLUDED_
28 
29 /** @file event2/util.h
30 
31   Common convenience functions for cross-platform portability and
32   related socket manipulations.
33 
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include <event2/event-config.h>
41 #ifdef EVENT__HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44 #ifdef EVENT__HAVE_STDINT_H
45 #include <stdint.h>
46 #elif defined(EVENT__HAVE_INTTYPES_H)
47 #include <inttypes.h>
48 #endif
49 #ifdef EVENT__HAVE_SYS_TYPES_H
50 #include <sys/types.h>
51 #endif
52 #ifdef EVENT__HAVE_STDDEF_H
53 #include <stddef.h>
54 #endif
55 #ifdef _MSC_VER
56 #include <BaseTsd.h>
57 #endif
58 #include <stdarg.h>
59 #ifdef EVENT__HAVE_NETDB_H
60 #if !defined(_GNU_SOURCE)
61 #define _GNU_SOURCE
62 #endif
63 #include <netdb.h>
64 #endif
65 
66 #ifdef _WIN32
67 #include <winsock2.h>
68 #else
69 #include <sys/socket.h>
70 #endif
71 
72 /* Some openbsd autoconf versions get the name of this macro wrong. */
73 #if defined(EVENT__SIZEOF_VOID__) && !defined(EVENT__SIZEOF_VOID_P)
74 #define EVENT__SIZEOF_VOID_P EVENT__SIZEOF_VOID__
75 #endif
76 
77 /**
78  * @name Standard integer types.
79  *
80  * Integer type definitions for types that are supposed to be defined in the
81  * C99-specified stdint.h.  Shamefully, some platforms do not include
82  * stdint.h, so we need to replace it.  (If you are on a platform like this,
83  * your C headers are now over 10 years out of date.  You should bug them to
84  * do something about this.)
85  *
86  * We define:
87  *
88  * <dl>
89  *   <dt>ev_uint64_t, ev_uint32_t, ev_uint16_t, ev_uint8_t</dt>
90  *      <dd>unsigned integer types of exactly 64, 32, 16, and 8 bits
91  *          respectively.</dd>
92  *    <dt>ev_int64_t, ev_int32_t, ev_int16_t, ev_int8_t</dt>
93  *      <dd>signed integer types of exactly 64, 32, 16, and 8 bits
94  *          respectively.</dd>
95  *    <dt>ev_uintptr_t, ev_intptr_t</dt>
96  *      <dd>unsigned/signed integers large enough
97  *      to hold a pointer without loss of bits.</dd>
98  *    <dt>ev_ssize_t</dt>
99  *      <dd>A signed type of the same size as size_t</dd>
100  *    <dt>ev_off_t</dt>
101  *      <dd>A signed type typically used to represent offsets within a
102  *      (potentially large) file</dd>
103  *
104  * @{
105  */
106 #ifdef EVENT__HAVE_UINT64_T
107 #define ev_uint64_t uint64_t
108 #define ev_int64_t int64_t
109 #elif defined(_WIN32)
110 #define ev_uint64_t unsigned __int64
111 #define ev_int64_t signed __int64
112 #elif EVENT__SIZEOF_LONG_LONG == 8
113 #define ev_uint64_t unsigned long long
114 #define ev_int64_t long long
115 #elif EVENT__SIZEOF_LONG == 8
116 #define ev_uint64_t unsigned long
117 #define ev_int64_t long
118 #elif defined(_EVENT_IN_DOXYGEN)
119 #define ev_uint64_t ...
120 #define ev_int64_t ...
121 #else
122 #error "No way to define ev_uint64_t"
123 #endif
124 
125 #ifdef EVENT__HAVE_UINT32_T
126 #define ev_uint32_t uint32_t
127 #define ev_int32_t int32_t
128 #elif defined(_WIN32)
129 #define ev_uint32_t unsigned int
130 #define ev_int32_t signed int
131 #elif EVENT__SIZEOF_LONG == 4
132 #define ev_uint32_t unsigned long
133 #define ev_int32_t signed long
134 #elif EVENT__SIZEOF_INT == 4
135 #define ev_uint32_t unsigned int
136 #define ev_int32_t signed int
137 #elif defined(_EVENT_IN_DOXYGEN)
138 #define ev_uint32_t ...
139 #define ev_int32_t ...
140 #else
141 #error "No way to define ev_uint32_t"
142 #endif
143 
144 #ifdef EVENT__HAVE_UINT16_T
145 #define ev_uint16_t uint16_t
146 #define ev_int16_t  int16_t
147 #elif defined(_WIN32)
148 #define ev_uint16_t unsigned short
149 #define ev_int16_t  signed short
150 #elif EVENT__SIZEOF_INT == 2
151 #define ev_uint16_t unsigned int
152 #define ev_int16_t  signed int
153 #elif EVENT__SIZEOF_SHORT == 2
154 #define ev_uint16_t unsigned short
155 #define ev_int16_t  signed short
156 #elif defined(_EVENT_IN_DOXYGEN)
157 #define ev_uint16_t ...
158 #define ev_int16_t ...
159 #else
160 #error "No way to define ev_uint16_t"
161 #endif
162 
163 #ifdef EVENT__HAVE_UINT8_T
164 #define ev_uint8_t uint8_t
165 #define ev_int8_t int8_t
166 #elif defined(_EVENT_IN_DOXYGEN)
167 #define ev_uint8_t ...
168 #define ev_int8_t ...
169 #else
170 #define ev_uint8_t unsigned char
171 #define ev_int8_t signed char
172 #endif
173 
174 #ifdef EVENT__HAVE_UINTPTR_T
175 #define ev_uintptr_t uintptr_t
176 #define ev_intptr_t intptr_t
177 #elif EVENT__SIZEOF_VOID_P <= 4
178 #define ev_uintptr_t ev_uint32_t
179 #define ev_intptr_t ev_int32_t
180 #elif EVENT__SIZEOF_VOID_P <= 8
181 #define ev_uintptr_t ev_uint64_t
182 #define ev_intptr_t ev_int64_t
183 #elif defined(_EVENT_IN_DOXYGEN)
184 #define ev_uintptr_t ...
185 #define ev_intptr_t ...
186 #else
187 #error "No way to define ev_uintptr_t"
188 #endif
189 
190 #ifdef EVENT__ssize_t
191 #define ev_ssize_t EVENT__ssize_t
192 #else
193 #define ev_ssize_t ssize_t
194 #endif
195 
196 /* Note that we define ev_off_t based on the compile-time size of off_t that
197  * we used to build Libevent, and not based on the current size of off_t.
198  * (For example, we don't define ev_off_t to off_t.).  We do this because
199  * some systems let you build your software with different off_t sizes
200  * at runtime, and so putting in any dependency on off_t would risk API
201  * mismatch.
202  */
203 #ifdef _WIN32
204 #define ev_off_t ev_int64_t
205 #elif EVENT__SIZEOF_OFF_T == 8
206 #define ev_off_t ev_int64_t
207 #elif EVENT__SIZEOF_OFF_T == 4
208 #define ev_off_t ev_int32_t
209 #elif defined(_EVENT_IN_DOXYGEN)
210 #define ev_off_t ...
211 #else
212 #define ev_off_t off_t
213 #endif
214 /**@}*/
215 
216 /* Limits for integer types.
217 
218    We're making two assumptions here:
219      - The compiler does constant folding properly.
220      - The platform does signed arithmetic in two's complement.
221 */
222 
223 /**
224    @name Limits for integer types
225 
226    These macros hold the largest or smallest values possible for the
227    ev_[u]int*_t types.
228 
229    @{
230 */
231 #define EV_UINT64_MAX ((((ev_uint64_t)0xffffffffUL) << 32) | 0xffffffffUL)
232 #define EV_INT64_MAX  ((((ev_int64_t) 0x7fffffffL) << 32) | 0xffffffffL)
233 #define EV_INT64_MIN  ((-EV_INT64_MAX) - 1)
234 #define EV_UINT32_MAX ((ev_uint32_t)0xffffffffUL)
235 #define EV_INT32_MAX  ((ev_int32_t) 0x7fffffffL)
236 #define EV_INT32_MIN  ((-EV_INT32_MAX) - 1)
237 #define EV_UINT16_MAX ((ev_uint16_t)0xffffUL)
238 #define EV_INT16_MAX  ((ev_int16_t) 0x7fffL)
239 #define EV_INT16_MIN  ((-EV_INT16_MAX) - 1)
240 #define EV_UINT8_MAX  255
241 #define EV_INT8_MAX   127
242 #define EV_INT8_MIN   ((-EV_INT8_MAX) - 1)
243 /** @} */
244 
245 /**
246    @name Limits for SIZE_T and SSIZE_T
247 
248    @{
249 */
250 #if EVENT__SIZEOF_SIZE_T == 8
251 #define EV_SIZE_MAX EV_UINT64_MAX
252 #define EV_SSIZE_MAX EV_INT64_MAX
253 #elif EVENT__SIZEOF_SIZE_T == 4
254 #define EV_SIZE_MAX EV_UINT32_MAX
255 #define EV_SSIZE_MAX EV_INT32_MAX
256 #elif defined(_EVENT_IN_DOXYGEN)
257 #define EV_SIZE_MAX ...
258 #define EV_SSIZE_MAX ...
259 #else
260 #error "No way to define SIZE_MAX"
261 #endif
262 
263 #define EV_SSIZE_MIN ((-EV_SSIZE_MAX) - 1)
264 /**@}*/
265 
266 #ifdef _WIN32
267 #define ev_socklen_t int
268 #elif defined(EVENT__socklen_t)
269 #define ev_socklen_t EVENT__socklen_t
270 #else
271 #define ev_socklen_t socklen_t
272 #endif
273 
274 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY
275 #if !defined(EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY) \
276  && !defined(ss_family)
277 #define ss_family __ss_family
278 #endif
279 #endif
280 
281 /**
282  * A type wide enough to hold the output of "socket()" or "accept()".  On
283  * Windows, this is an intptr_t; elsewhere, it is an int. */
284 #ifdef _WIN32
285 #define evutil_socket_t intptr_t
286 #else
287 #define evutil_socket_t int
288 #endif
289 
290 /** Create two new sockets that are connected to each other.
291 
292     On Unix, this simply calls socketpair().  On Windows, it uses the
293     loopback network interface on 127.0.0.1, and only
294     AF_INET,SOCK_STREAM are supported.
295 
296     (This may fail on some Windows hosts where firewall software has cleverly
297     decided to keep 127.0.0.1 from talking to itself.)
298 
299     Parameters and return values are as for socketpair()
300 */
301 int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]);
302 /** Do platform-specific operations as needed to make a socket nonblocking.
303 
304     @param sock The socket to make nonblocking
305     @return 0 on success, -1 on failure
306  */
307 int evutil_make_socket_nonblocking(evutil_socket_t sock);
308 
309 /** Do platform-specific operations to make a listener socket reusable.
310 
311     Specifically, we want to make sure that another program will be able
312     to bind this address right after we've closed the listener.
313 
314     This differs from Windows's interpretation of "reusable", which
315     allows multiple listeners to bind the same address at the same time.
316 
317     @param sock The socket to make reusable
318     @return 0 on success, -1 on failure
319  */
320 int evutil_make_listen_socket_reuseable(evutil_socket_t sock);
321 
322 /** Do platform-specific operations as needed to close a socket upon a
323     successful execution of one of the exec*() functions.
324 
325     @param sock The socket to be closed
326     @return 0 on success, -1 on failure
327  */
328 int evutil_make_socket_closeonexec(evutil_socket_t sock);
329 
330 /** Do the platform-specific call needed to close a socket returned from
331     socket() or accept().
332 
333     @param sock The socket to be closed
334     @return 0 on success, -1 on failure
335  */
336 int evutil_closesocket(evutil_socket_t sock);
337 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
338 
339 /** Do platform-specific operations, if possible, to make a tcp listener
340  *  socket defer accept()s until there is data to read.
341  *
342  *  Not all platforms support this.  You don't want to do this for every
343  *  listener socket: only the ones that implement a protocol where the
344  *  client transmits before the server needs to respond.
345  *
346  *  @param sock The listening socket to to make deferred
347  *  @return 0 on success (whether the operation is supported or not),
348  *       -1 on failure
349 */
350 int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
351 
352 #ifdef _WIN32
353 /** Return the most recent socket error.  Not idempotent on all platforms. */
354 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
355 /** Replace the most recent socket error with errcode */
356 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
357 	do { WSASetLastError(errcode); } while (0)
358 /** Return the most recent socket error to occur on sock. */
359 int evutil_socket_geterror(evutil_socket_t sock);
360 /** Convert a socket error to a string. */
361 const char *evutil_socket_error_to_string(int errcode);
362 #elif defined(_EVENT_IN_DOXYGEN)
363 /**
364    @name Socket error functions
365 
366    These functions are needed for making programs compatible between
367    Windows and Unix-like platforms.
368 
369    You see, Winsock handles socket errors differently from the rest of
370    the world.  Elsewhere, a socket error is like any other error and is
371    stored in errno.  But winsock functions require you to retrieve the
372    error with a special function, and don't let you use strerror for
373    the error codes.  And handling EWOULDBLOCK is ... different.
374 
375    @{
376 */
377 /** Return the most recent socket error.  Not idempotent on all platforms. */
378 #define EVUTIL_SOCKET_ERROR() ...
379 /** Replace the most recent socket error with errcode */
380 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
381 /** Return the most recent socket error to occur on sock. */
382 #define evutil_socket_geterror(sock) ...
383 /** Convert a socket error to a string. */
384 #define evutil_socket_error_to_string(errcode) ...
385 /**@}*/
386 #else
387 #define EVUTIL_SOCKET_ERROR() (errno)
388 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
389 		do { errno = (errcode); } while (0)
390 #define evutil_socket_geterror(sock) (errno)
391 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
392 #endif
393 
394 
395 /**
396  * @name Manipulation macros for struct timeval.
397  *
398  * We define replacements
399  * for timeradd, timersub, timerclear, timercmp, and timerisset.
400  *
401  * @{
402  */
403 #ifdef EVENT__HAVE_TIMERADD
404 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
405 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
406 #else
407 #define evutil_timeradd(tvp, uvp, vvp)					\
408 	do {								\
409 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
410 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
411 		if ((vvp)->tv_usec >= 1000000) {			\
412 			(vvp)->tv_sec++;				\
413 			(vvp)->tv_usec -= 1000000;			\
414 		}							\
415 	} while (0)
416 #define	evutil_timersub(tvp, uvp, vvp)					\
417 	do {								\
418 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
419 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
420 		if ((vvp)->tv_usec < 0) {				\
421 			(vvp)->tv_sec--;				\
422 			(vvp)->tv_usec += 1000000;			\
423 		}							\
424 	} while (0)
425 #endif /* !EVENT__HAVE_TIMERADD */
426 
427 #ifdef EVENT__HAVE_TIMERCLEAR
428 #define evutil_timerclear(tvp) timerclear(tvp)
429 #else
430 #define	evutil_timerclear(tvp)	(tvp)->tv_sec = (tvp)->tv_usec = 0
431 #endif
432 /**@}*/
433 
434 /** Return true iff the tvp is related to uvp according to the relational
435  * operator cmp.  Recognized values for cmp are ==, <=, <, >=, and >. */
436 #define	evutil_timercmp(tvp, uvp, cmp)					\
437 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
438 	 ((tvp)->tv_usec cmp (uvp)->tv_usec) :				\
439 	 ((tvp)->tv_sec cmp (uvp)->tv_sec))
440 
441 #ifdef EVENT__HAVE_TIMERISSET
442 #define evutil_timerisset(tvp) timerisset(tvp)
443 #else
444 #define	evutil_timerisset(tvp)	((tvp)->tv_sec || (tvp)->tv_usec)
445 #endif
446 
447 /** Replacement for offsetof on platforms that don't define it. */
448 #ifdef offsetof
449 #define evutil_offsetof(type, field) offsetof(type, field)
450 #else
451 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
452 #endif
453 
454 /* big-int related functions */
455 /** Parse a 64-bit value from a string.  Arguments are as for strtol. */
456 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
457 
458 /** Replacement for gettimeofday on platforms that lack it. */
459 #ifdef EVENT__HAVE_GETTIMEOFDAY
460 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
461 #else
462 struct timezone;
463 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
464 #endif
465 
466 /** Replacement for snprintf to get consistent behavior on platforms for
467     which the return value of snprintf does not conform to C99.
468  */
469 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
470 #ifdef __GNUC__
471 	__attribute__((format(printf, 3, 4)))
472 #endif
473 ;
474 /** Replacement for vsnprintf to get consistent behavior on platforms for
475     which the return value of snprintf does not conform to C99.
476  */
477 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap);
478 
479 /** Replacement for inet_ntop for platforms which lack it. */
480 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
481 /** Replacement for inet_pton for platforms which lack it. */
482 int evutil_inet_pton(int af, const char *src, void *dst);
483 struct sockaddr;
484 
485 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
486 
487     Recognized formats are:
488     - [IPv6Address]:port
489     - [IPv6Address]
490     - IPv6Address
491     - IPv4Address:port
492     - IPv4Address
493 
494     If no port is specified, the port in the output is set to 0.
495 
496     @param str The string to parse.
497     @param out A struct sockaddr to hold the result.  This should probably be
498        a struct sockaddr_storage.
499     @param outlen A pointer to the number of bytes that that 'out' can safely
500        hold.  Set to the number of bytes used in 'out' on success.
501     @return -1 if the address is not well-formed, if the port is out of range,
502        or if out is not large enough to hold the result.  Otherwise returns
503        0 on success.
504 */
505 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
506 
507 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
508  * preceeds sa2, or greater than 0 if sa1 follows sa2.  If include_port is
509  * true, consider the port as well as the address.  Only implemented for
510  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
511  * the same between Libevent versions. */
512 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
513     int include_port);
514 
515 /** As strcasecmp, but always compares the characters in locale-independent
516     ASCII.  That's useful if you're handling data in ASCII-based protocols.
517  */
518 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
519 /** As strncasecmp, but always compares the characters in locale-independent
520     ASCII.  That's useful if you're handling data in ASCII-based protocols.
521  */
522 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
523 
524 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
525  * if this system has no getaddrinfo(). */
526 #ifdef EVENT__HAVE_STRUCT_ADDRINFO
527 #define evutil_addrinfo addrinfo
528 #else
529 /** A definition of struct addrinfo for systems that lack it.
530 
531     (This is just an alias for struct addrinfo if the system defines
532     struct addrinfo.)
533 */
534 struct evutil_addrinfo {
535 	int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
536 	int     ai_family;    /* PF_xxx */
537 	int     ai_socktype;  /* SOCK_xxx */
538 	int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
539 	size_t  ai_addrlen;   /* length of ai_addr */
540 	char   *ai_canonname; /* canonical name for nodename */
541 	struct sockaddr  *ai_addr; /* binary address */
542 	struct evutil_addrinfo  *ai_next; /* next structure in linked list */
543 };
544 #endif
545 /** @name evutil_getaddrinfo() error codes
546 
547     These values are possible error codes for evutil_getaddrinfo() and
548     related functions.
549 
550     @{
551 */
552 #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
553 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
554 #else
555 #define EVUTIL_EAI_ADDRFAMILY -901
556 #endif
557 #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
558 #define EVUTIL_EAI_AGAIN EAI_AGAIN
559 #else
560 #define EVUTIL_EAI_AGAIN -902
561 #endif
562 #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
563 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
564 #else
565 #define EVUTIL_EAI_BADFLAGS -903
566 #endif
567 #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
568 #define EVUTIL_EAI_FAIL EAI_FAIL
569 #else
570 #define EVUTIL_EAI_FAIL -904
571 #endif
572 #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
573 #define EVUTIL_EAI_FAMILY EAI_FAMILY
574 #else
575 #define EVUTIL_EAI_FAMILY -905
576 #endif
577 #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
578 #define EVUTIL_EAI_MEMORY EAI_MEMORY
579 #else
580 #define EVUTIL_EAI_MEMORY -906
581 #endif
582 /* This test is a bit complicated, since some MS SDKs decide to
583  * remove NODATA or redefine it to be the same as NONAME, in a
584  * fun interpretation of RFC 2553 and RFC 3493. */
585 #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
586 #define EVUTIL_EAI_NODATA EAI_NODATA
587 #else
588 #define EVUTIL_EAI_NODATA -907
589 #endif
590 #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
591 #define EVUTIL_EAI_NONAME EAI_NONAME
592 #else
593 #define EVUTIL_EAI_NONAME -908
594 #endif
595 #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
596 #define EVUTIL_EAI_SERVICE EAI_SERVICE
597 #else
598 #define EVUTIL_EAI_SERVICE -909
599 #endif
600 #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
601 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
602 #else
603 #define EVUTIL_EAI_SOCKTYPE -910
604 #endif
605 #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
606 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
607 #else
608 #define EVUTIL_EAI_SYSTEM -911
609 #endif
610 
611 #define EVUTIL_EAI_CANCEL -90001
612 
613 #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
614 #define EVUTIL_AI_PASSIVE AI_PASSIVE
615 #else
616 #define EVUTIL_AI_PASSIVE 0x1000
617 #endif
618 #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
619 #define EVUTIL_AI_CANONNAME AI_CANONNAME
620 #else
621 #define EVUTIL_AI_CANONNAME 0x2000
622 #endif
623 #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
624 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
625 #else
626 #define EVUTIL_AI_NUMERICHOST 0x4000
627 #endif
628 #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
629 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
630 #else
631 #define EVUTIL_AI_NUMERICSERV 0x8000
632 #endif
633 #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
634 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
635 #else
636 #define EVUTIL_AI_V4MAPPED 0x10000
637 #endif
638 #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
639 #define EVUTIL_AI_ALL AI_ALL
640 #else
641 #define EVUTIL_AI_ALL 0x20000
642 #endif
643 #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
644 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
645 #else
646 #define EVUTIL_AI_ADDRCONFIG 0x40000
647 #endif
648 /**@}*/
649 
650 struct evutil_addrinfo;
651 /**
652  * This function clones getaddrinfo for systems that don't have it.  For full
653  * details, see RFC 3493, section 6.1.
654  *
655  * Limitations:
656  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
657  *   gethostbyname, with their attendant issues.
658  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
659  *
660  * For a nonblocking variant, see evdns_getaddrinfo.
661  */
662 int evutil_getaddrinfo(const char *nodename, const char *servname,
663     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
664 
665 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
666 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
667 
668 const char *evutil_gai_strerror(int err);
669 
670 /** Generate n bytes of secure pseudorandom data, and store them in buf.
671  *
672  * By default, Libevent uses an ARC4-based random number generator, seeded
673  * using the platform's entropy source (/dev/urandom on Unix-like systems;
674  * CryptGenRandom on Windows).
675  */
676 void evutil_secure_rng_get_bytes(void *buf, size_t n);
677 
678 /**
679  * Seed the secure random number generator if needed, and return 0 on
680  * success or -1 on failure.
681  *
682  * It is okay to call this function more than once; it will still return
683  * 0 if the RNG has been successfully seeded and -1 if it can't be
684  * seeded.
685  *
686  * Ordinarily you don't need to call this function from your own code;
687  * Libevent will seed the RNG itself the first time it needs good random
688  * numbers.  You only need to call it if (a) you want to double-check
689  * that one of the seeding methods did succeed, or (b) you plan to drop
690  * the capability to seed (by chrooting, or dropping capabilities, or
691  * whatever), and you want to make sure that seeding happens before your
692  * program loses the ability to do it.
693  */
694 int evutil_secure_rng_init(void);
695 
696 /** Seed the random number generator with extra random bytes.
697 
698     You should almost never need to call this function; it should be
699     sufficient to invoke evutil_secure_rng_init(), or let Libevent take
700     care of calling evutil_secure_rng_init() on its own.
701 
702     If you call this function as a _replacement_ for the regular
703     entropy sources, then you need to be sure that your input
704     contains a fairly large amount of strong entropy.  Doing so is
705     notoriously hard: most people who try get it wrong.  Watch out!
706 
707     @param dat a buffer full of a strong source of random numbers
708     @param datlen the number of bytes to read from datlen
709  */
710 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
711 
712 #ifdef __cplusplus
713 }
714 #endif
715 
716 #endif /* EVENT1_EVUTIL_H_INCLUDED_ */
717