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