xref: /freebsd/contrib/ntp/sntp/libevent/include/event2/util.h (revision 788ca347b816afd83b2885e0c79aeeb88649b2ab)
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 as needed to close a socket upon a
331     successful execution of one of the exec*() functions.
332 
333     @param sock The socket to be closed
334     @return 0 on success, -1 on failure
335  */
336 EVENT2_EXPORT_SYMBOL
337 int evutil_make_socket_closeonexec(evutil_socket_t sock);
338 
339 /** Do the platform-specific call needed to close a socket returned from
340     socket() or accept().
341 
342     @param sock The socket to be closed
343     @return 0 on success, -1 on failure
344  */
345 EVENT2_EXPORT_SYMBOL
346 int evutil_closesocket(evutil_socket_t sock);
347 #define EVUTIL_CLOSESOCKET(s) evutil_closesocket(s)
348 
349 /** Do platform-specific operations, if possible, to make a tcp listener
350  *  socket defer accept()s until there is data to read.
351  *
352  *  Not all platforms support this.  You don't want to do this for every
353  *  listener socket: only the ones that implement a protocol where the
354  *  client transmits before the server needs to respond.
355  *
356  *  @param sock The listening socket to to make deferred
357  *  @return 0 on success (whether the operation is supported or not),
358  *       -1 on failure
359 */
360 EVENT2_EXPORT_SYMBOL
361 int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
362 
363 #ifdef _WIN32
364 /** Return the most recent socket error.  Not idempotent on all platforms. */
365 #define EVUTIL_SOCKET_ERROR() WSAGetLastError()
366 /** Replace the most recent socket error with errcode */
367 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
368 	do { WSASetLastError(errcode); } while (0)
369 /** Return the most recent socket error to occur on sock. */
370 EVENT2_EXPORT_SYMBOL
371 int evutil_socket_geterror(evutil_socket_t sock);
372 /** Convert a socket error to a string. */
373 EVENT2_EXPORT_SYMBOL
374 const char *evutil_socket_error_to_string(int errcode);
375 #elif defined(EVENT_IN_DOXYGEN_)
376 /**
377    @name Socket error functions
378 
379    These functions are needed for making programs compatible between
380    Windows and Unix-like platforms.
381 
382    You see, Winsock handles socket errors differently from the rest of
383    the world.  Elsewhere, a socket error is like any other error and is
384    stored in errno.  But winsock functions require you to retrieve the
385    error with a special function, and don't let you use strerror for
386    the error codes.  And handling EWOULDBLOCK is ... different.
387 
388    @{
389 */
390 /** Return the most recent socket error.  Not idempotent on all platforms. */
391 #define EVUTIL_SOCKET_ERROR() ...
392 /** Replace the most recent socket error with errcode */
393 #define EVUTIL_SET_SOCKET_ERROR(errcode) ...
394 /** Return the most recent socket error to occur on sock. */
395 #define evutil_socket_geterror(sock) ...
396 /** Convert a socket error to a string. */
397 #define evutil_socket_error_to_string(errcode) ...
398 /**@}*/
399 #else
400 #define EVUTIL_SOCKET_ERROR() (errno)
401 #define EVUTIL_SET_SOCKET_ERROR(errcode)		\
402 		do { errno = (errcode); } while (0)
403 #define evutil_socket_geterror(sock) (errno)
404 #define evutil_socket_error_to_string(errcode) (strerror(errcode))
405 #endif
406 
407 
408 /**
409  * @name Manipulation macros for struct timeval.
410  *
411  * We define replacements
412  * for timeradd, timersub, timerclear, timercmp, and timerisset.
413  *
414  * @{
415  */
416 #ifdef EVENT__HAVE_TIMERADD
417 #define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp))
418 #define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp))
419 #else
420 #define evutil_timeradd(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 >= 1000000) {			\
425 			(vvp)->tv_sec++;				\
426 			(vvp)->tv_usec -= 1000000;			\
427 		}							\
428 	} while (0)
429 #define	evutil_timersub(tvp, uvp, vvp)					\
430 	do {								\
431 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
432 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
433 		if ((vvp)->tv_usec < 0) {				\
434 			(vvp)->tv_sec--;				\
435 			(vvp)->tv_usec += 1000000;			\
436 		}							\
437 	} while (0)
438 #endif /* !EVENT__HAVE_TIMERADD */
439 
440 #ifdef EVENT__HAVE_TIMERCLEAR
441 #define evutil_timerclear(tvp) timerclear(tvp)
442 #else
443 #define	evutil_timerclear(tvp)	(tvp)->tv_sec = (tvp)->tv_usec = 0
444 #endif
445 /**@}*/
446 
447 /** Return true iff the tvp is related to uvp according to the relational
448  * operator cmp.  Recognized values for cmp are ==, <=, <, >=, and >. */
449 #define	evutil_timercmp(tvp, uvp, cmp)					\
450 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
451 	 ((tvp)->tv_usec cmp (uvp)->tv_usec) :				\
452 	 ((tvp)->tv_sec cmp (uvp)->tv_sec))
453 
454 #ifdef EVENT__HAVE_TIMERISSET
455 #define evutil_timerisset(tvp) timerisset(tvp)
456 #else
457 #define	evutil_timerisset(tvp)	((tvp)->tv_sec || (tvp)->tv_usec)
458 #endif
459 
460 /** Replacement for offsetof on platforms that don't define it. */
461 #ifdef offsetof
462 #define evutil_offsetof(type, field) offsetof(type, field)
463 #else
464 #define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field))
465 #endif
466 
467 /* big-int related functions */
468 /** Parse a 64-bit value from a string.  Arguments are as for strtol. */
469 EVENT2_EXPORT_SYMBOL
470 ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
471 
472 /** Replacement for gettimeofday on platforms that lack it. */
473 #ifdef EVENT__HAVE_GETTIMEOFDAY
474 #define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
475 #else
476 struct timezone;
477 EVENT2_EXPORT_SYMBOL
478 int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
479 #endif
480 
481 /** Replacement for snprintf to get consistent behavior on platforms for
482     which the return value of snprintf does not conform to C99.
483  */
484 EVENT2_EXPORT_SYMBOL
485 int evutil_snprintf(char *buf, size_t buflen, const char *format, ...)
486 #ifdef __GNUC__
487 	__attribute__((format(printf, 3, 4)))
488 #endif
489 ;
490 /** Replacement for vsnprintf to get consistent behavior on platforms for
491     which the return value of snprintf does not conform to C99.
492  */
493 EVENT2_EXPORT_SYMBOL
494 int evutil_vsnprintf(char *buf, size_t buflen, const char *format, va_list ap)
495 #ifdef __GNUC__
496 	__attribute__((format(printf, 3, 0)))
497 #endif
498 ;
499 
500 /** Replacement for inet_ntop for platforms which lack it. */
501 EVENT2_EXPORT_SYMBOL
502 const char *evutil_inet_ntop(int af, const void *src, char *dst, size_t len);
503 /** Replacement for inet_pton for platforms which lack it. */
504 EVENT2_EXPORT_SYMBOL
505 int evutil_inet_pton(int af, const char *src, void *dst);
506 struct sockaddr;
507 
508 /** Parse an IPv4 or IPv6 address, with optional port, from a string.
509 
510     Recognized formats are:
511     - [IPv6Address]:port
512     - [IPv6Address]
513     - IPv6Address
514     - IPv4Address:port
515     - IPv4Address
516 
517     If no port is specified, the port in the output is set to 0.
518 
519     @param str The string to parse.
520     @param out A struct sockaddr to hold the result.  This should probably be
521        a struct sockaddr_storage.
522     @param outlen A pointer to the number of bytes that that 'out' can safely
523        hold.  Set to the number of bytes used in 'out' on success.
524     @return -1 if the address is not well-formed, if the port is out of range,
525        or if out is not large enough to hold the result.  Otherwise returns
526        0 on success.
527 */
528 EVENT2_EXPORT_SYMBOL
529 int evutil_parse_sockaddr_port(const char *str, struct sockaddr *out, int *outlen);
530 
531 /** Compare two sockaddrs; return 0 if they are equal, or less than 0 if sa1
532  * preceeds sa2, or greater than 0 if sa1 follows sa2.  If include_port is
533  * true, consider the port as well as the address.  Only implemented for
534  * AF_INET and AF_INET6 addresses. The ordering is not guaranteed to remain
535  * the same between Libevent versions. */
536 EVENT2_EXPORT_SYMBOL
537 int evutil_sockaddr_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2,
538     int include_port);
539 
540 /** As strcasecmp, but always compares the characters in locale-independent
541     ASCII.  That's useful if you're handling data in ASCII-based protocols.
542  */
543 EVENT2_EXPORT_SYMBOL
544 int evutil_ascii_strcasecmp(const char *str1, const char *str2);
545 /** As strncasecmp, but always compares the characters in locale-independent
546     ASCII.  That's useful if you're handling data in ASCII-based protocols.
547  */
548 EVENT2_EXPORT_SYMBOL
549 int evutil_ascii_strncasecmp(const char *str1, const char *str2, size_t n);
550 
551 /* Here we define evutil_addrinfo to the native addrinfo type, or redefine it
552  * if this system has no getaddrinfo(). */
553 #ifdef EVENT__HAVE_STRUCT_ADDRINFO
554 #define evutil_addrinfo addrinfo
555 #else
556 /** A definition of struct addrinfo for systems that lack it.
557 
558     (This is just an alias for struct addrinfo if the system defines
559     struct addrinfo.)
560 */
561 struct evutil_addrinfo {
562 	int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
563 	int     ai_family;    /* PF_xxx */
564 	int     ai_socktype;  /* SOCK_xxx */
565 	int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
566 	size_t  ai_addrlen;   /* length of ai_addr */
567 	char   *ai_canonname; /* canonical name for nodename */
568 	struct sockaddr  *ai_addr; /* binary address */
569 	struct evutil_addrinfo  *ai_next; /* next structure in linked list */
570 };
571 #endif
572 /** @name evutil_getaddrinfo() error codes
573 
574     These values are possible error codes for evutil_getaddrinfo() and
575     related functions.
576 
577     @{
578 */
579 #if defined(EAI_ADDRFAMILY) && defined(EVENT__HAVE_GETADDRINFO)
580 #define EVUTIL_EAI_ADDRFAMILY EAI_ADDRFAMILY
581 #else
582 #define EVUTIL_EAI_ADDRFAMILY -901
583 #endif
584 #if defined(EAI_AGAIN) && defined(EVENT__HAVE_GETADDRINFO)
585 #define EVUTIL_EAI_AGAIN EAI_AGAIN
586 #else
587 #define EVUTIL_EAI_AGAIN -902
588 #endif
589 #if defined(EAI_BADFLAGS) && defined(EVENT__HAVE_GETADDRINFO)
590 #define EVUTIL_EAI_BADFLAGS EAI_BADFLAGS
591 #else
592 #define EVUTIL_EAI_BADFLAGS -903
593 #endif
594 #if defined(EAI_FAIL) && defined(EVENT__HAVE_GETADDRINFO)
595 #define EVUTIL_EAI_FAIL EAI_FAIL
596 #else
597 #define EVUTIL_EAI_FAIL -904
598 #endif
599 #if defined(EAI_FAMILY) && defined(EVENT__HAVE_GETADDRINFO)
600 #define EVUTIL_EAI_FAMILY EAI_FAMILY
601 #else
602 #define EVUTIL_EAI_FAMILY -905
603 #endif
604 #if defined(EAI_MEMORY) && defined(EVENT__HAVE_GETADDRINFO)
605 #define EVUTIL_EAI_MEMORY EAI_MEMORY
606 #else
607 #define EVUTIL_EAI_MEMORY -906
608 #endif
609 /* This test is a bit complicated, since some MS SDKs decide to
610  * remove NODATA or redefine it to be the same as NONAME, in a
611  * fun interpretation of RFC 2553 and RFC 3493. */
612 #if defined(EAI_NODATA) && defined(EVENT__HAVE_GETADDRINFO) && (!defined(EAI_NONAME) || EAI_NODATA != EAI_NONAME)
613 #define EVUTIL_EAI_NODATA EAI_NODATA
614 #else
615 #define EVUTIL_EAI_NODATA -907
616 #endif
617 #if defined(EAI_NONAME) && defined(EVENT__HAVE_GETADDRINFO)
618 #define EVUTIL_EAI_NONAME EAI_NONAME
619 #else
620 #define EVUTIL_EAI_NONAME -908
621 #endif
622 #if defined(EAI_SERVICE) && defined(EVENT__HAVE_GETADDRINFO)
623 #define EVUTIL_EAI_SERVICE EAI_SERVICE
624 #else
625 #define EVUTIL_EAI_SERVICE -909
626 #endif
627 #if defined(EAI_SOCKTYPE) && defined(EVENT__HAVE_GETADDRINFO)
628 #define EVUTIL_EAI_SOCKTYPE EAI_SOCKTYPE
629 #else
630 #define EVUTIL_EAI_SOCKTYPE -910
631 #endif
632 #if defined(EAI_SYSTEM) && defined(EVENT__HAVE_GETADDRINFO)
633 #define EVUTIL_EAI_SYSTEM EAI_SYSTEM
634 #else
635 #define EVUTIL_EAI_SYSTEM -911
636 #endif
637 
638 #define EVUTIL_EAI_CANCEL -90001
639 
640 #if defined(AI_PASSIVE) && defined(EVENT__HAVE_GETADDRINFO)
641 #define EVUTIL_AI_PASSIVE AI_PASSIVE
642 #else
643 #define EVUTIL_AI_PASSIVE 0x1000
644 #endif
645 #if defined(AI_CANONNAME) && defined(EVENT__HAVE_GETADDRINFO)
646 #define EVUTIL_AI_CANONNAME AI_CANONNAME
647 #else
648 #define EVUTIL_AI_CANONNAME 0x2000
649 #endif
650 #if defined(AI_NUMERICHOST) && defined(EVENT__HAVE_GETADDRINFO)
651 #define EVUTIL_AI_NUMERICHOST AI_NUMERICHOST
652 #else
653 #define EVUTIL_AI_NUMERICHOST 0x4000
654 #endif
655 #if defined(AI_NUMERICSERV) && defined(EVENT__HAVE_GETADDRINFO)
656 #define EVUTIL_AI_NUMERICSERV AI_NUMERICSERV
657 #else
658 #define EVUTIL_AI_NUMERICSERV 0x8000
659 #endif
660 #if defined(AI_V4MAPPED) && defined(EVENT__HAVE_GETADDRINFO)
661 #define EVUTIL_AI_V4MAPPED AI_V4MAPPED
662 #else
663 #define EVUTIL_AI_V4MAPPED 0x10000
664 #endif
665 #if defined(AI_ALL) && defined(EVENT__HAVE_GETADDRINFO)
666 #define EVUTIL_AI_ALL AI_ALL
667 #else
668 #define EVUTIL_AI_ALL 0x20000
669 #endif
670 #if defined(AI_ADDRCONFIG) && defined(EVENT__HAVE_GETADDRINFO)
671 #define EVUTIL_AI_ADDRCONFIG AI_ADDRCONFIG
672 #else
673 #define EVUTIL_AI_ADDRCONFIG 0x40000
674 #endif
675 /**@}*/
676 
677 struct evutil_addrinfo;
678 /**
679  * This function clones getaddrinfo for systems that don't have it.  For full
680  * details, see RFC 3493, section 6.1.
681  *
682  * Limitations:
683  * - When the system has no getaddrinfo, we fall back to gethostbyname_r or
684  *   gethostbyname, with their attendant issues.
685  * - The AI_V4MAPPED and AI_ALL flags are not currently implemented.
686  *
687  * For a nonblocking variant, see evdns_getaddrinfo.
688  */
689 EVENT2_EXPORT_SYMBOL
690 int evutil_getaddrinfo(const char *nodename, const char *servname,
691     const struct evutil_addrinfo *hints_in, struct evutil_addrinfo **res);
692 
693 /** Release storage allocated by evutil_getaddrinfo or evdns_getaddrinfo. */
694 EVENT2_EXPORT_SYMBOL
695 void evutil_freeaddrinfo(struct evutil_addrinfo *ai);
696 
697 EVENT2_EXPORT_SYMBOL
698 const char *evutil_gai_strerror(int err);
699 
700 /** Generate n bytes of secure pseudorandom data, and store them in buf.
701  *
702  * Current versions of Libevent use an ARC4-based random number generator,
703  * seeded using the platform's entropy source (/dev/urandom on Unix-like
704  * systems; CryptGenRandom on Windows).  This is not actually as secure as it
705  * should be: ARC4 is a pretty lousy cipher, and the current implementation
706  * provides only rudimentary prediction- and backtracking-resistance.  Don't
707  * use this for serious cryptographic applications.
708  */
709 EVENT2_EXPORT_SYMBOL
710 void evutil_secure_rng_get_bytes(void *buf, size_t n);
711 
712 /**
713  * Seed the secure random number generator if needed, and return 0 on
714  * success or -1 on failure.
715  *
716  * It is okay to call this function more than once; it will still return
717  * 0 if the RNG has been successfully seeded and -1 if it can't be
718  * seeded.
719  *
720  * Ordinarily you don't need to call this function from your own code;
721  * Libevent will seed the RNG itself the first time it needs good random
722  * numbers.  You only need to call it if (a) you want to double-check
723  * that one of the seeding methods did succeed, or (b) you plan to drop
724  * the capability to seed (by chrooting, or dropping capabilities, or
725  * whatever), and you want to make sure that seeding happens before your
726  * program loses the ability to do it.
727  */
728 EVENT2_EXPORT_SYMBOL
729 int evutil_secure_rng_init(void);
730 
731 /**
732  * Set a filename to use in place of /dev/urandom for seeding the secure
733  * PRNG. Return 0 on success, -1 on failure.
734  *
735  * Call this function BEFORE calling any other initialization or RNG
736  * functions.
737  *
738  * (This string will _NOT_ be copied internally. Do not free it while any
739  * user of the secure RNG might be running. Don't pass anything other than a
740  * real /dev/...random device file here, or you might lose security.)
741  *
742  * This API is unstable, and might change in a future libevent version.
743  */
744 EVENT2_EXPORT_SYMBOL
745 int evutil_secure_rng_set_urandom_device_file(char *fname);
746 
747 /** Seed the random number generator with extra random bytes.
748 
749     You should almost never need to call this function; it should be
750     sufficient to invoke evutil_secure_rng_init(), or let Libevent take
751     care of calling evutil_secure_rng_init() on its own.
752 
753     If you call this function as a _replacement_ for the regular
754     entropy sources, then you need to be sure that your input
755     contains a fairly large amount of strong entropy.  Doing so is
756     notoriously hard: most people who try get it wrong.  Watch out!
757 
758     @param dat a buffer full of a strong source of random numbers
759     @param datlen the number of bytes to read from datlen
760  */
761 EVENT2_EXPORT_SYMBOL
762 void evutil_secure_rng_add_bytes(const char *dat, size_t datlen);
763 
764 #ifdef __cplusplus
765 }
766 #endif
767 
768 #endif /* EVENT1_EVUTIL_H_INCLUDED_ */
769