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