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