xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/util-internal.h (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: util-internal.h,v 1.8 2024/08/18 20:47:21 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*
48585484eSchristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
58585484eSchristos  *
68585484eSchristos  * Redistribution and use in source and binary forms, with or without
78585484eSchristos  * modification, are permitted provided that the following conditions
88585484eSchristos  * are met:
98585484eSchristos  * 1. Redistributions of source code must retain the above copyright
108585484eSchristos  *    notice, this list of conditions and the following disclaimer.
118585484eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
128585484eSchristos  *    notice, this list of conditions and the following disclaimer in the
138585484eSchristos  *    documentation and/or other materials provided with the distribution.
148585484eSchristos  * 3. The name of the author may not be used to endorse or promote products
158585484eSchristos  *    derived from this software without specific prior written permission.
168585484eSchristos  *
178585484eSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188585484eSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198585484eSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208585484eSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218585484eSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228585484eSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238585484eSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248585484eSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258585484eSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268585484eSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278585484eSchristos  */
288585484eSchristos #ifndef UTIL_INTERNAL_H_INCLUDED_
298585484eSchristos #define UTIL_INTERNAL_H_INCLUDED_
308585484eSchristos 
318585484eSchristos #include "event2/event-config.h"
328585484eSchristos #include "evconfig-private.h"
338585484eSchristos 
348585484eSchristos #include <errno.h>
358585484eSchristos 
368585484eSchristos /* For EVUTIL_ASSERT */
378585484eSchristos #include "log-internal.h"
388585484eSchristos #include <stdio.h>
398585484eSchristos #include <stdlib.h>
408585484eSchristos #ifdef EVENT__HAVE_SYS_SOCKET_H
418585484eSchristos #include <sys/socket.h>
428585484eSchristos #endif
438585484eSchristos #ifdef EVENT__HAVE_SYS_EVENTFD_H
448585484eSchristos #include <sys/eventfd.h>
458585484eSchristos #endif
468585484eSchristos #include "event2/util.h"
478585484eSchristos 
488585484eSchristos #include "time-internal.h"
498585484eSchristos #include "ipv6-internal.h"
508585484eSchristos 
518585484eSchristos #ifdef __cplusplus
528585484eSchristos extern "C" {
538585484eSchristos #endif
548585484eSchristos 
55*eabc0478Schristos /* __has_attribute() wrapper */
56*eabc0478Schristos #ifdef __has_attribute
57*eabc0478Schristos # define EVUTIL_HAS_ATTRIBUTE __has_attribute
58*eabc0478Schristos #endif
59*eabc0478Schristos /** clang 3 __has_attribute misbehaves in some versions */
60*eabc0478Schristos #if defined(__clang__) && __clang__ == 1
61*eabc0478Schristos # if defined(__apple_build_version__)
62*eabc0478Schristos #  if __clang_major__ <= 6
63*eabc0478Schristos #   undef EVUTIL_HAS_ATTRIBUTE
64*eabc0478Schristos #  endif
65*eabc0478Schristos # else /* !__apple_build_version__ */
66*eabc0478Schristos #  if __clang_major__ == 3 && __clang_minor__ >= 2 && __clang_minor__ <= 5
67*eabc0478Schristos #   undef EVUTIL_HAS_ATTRIBUTE
68*eabc0478Schristos #  endif
69*eabc0478Schristos # endif /* __apple_build_version__ */
70*eabc0478Schristos #endif /*\ defined(__clang__) && __clang__ == 1 */
71*eabc0478Schristos #ifndef EVUTIL_HAS_ATTRIBUTE
72*eabc0478Schristos # define EVUTIL_HAS_ATTRIBUTE(x) 0
73*eabc0478Schristos #endif
74*eabc0478Schristos 
758585484eSchristos /* If we need magic to say "inline", get it for free internally. */
768585484eSchristos #ifdef EVENT__inline
778585484eSchristos #define inline EVENT__inline
788585484eSchristos #endif
79*eabc0478Schristos 
80*eabc0478Schristos /* Define to appropriate substitute if compiler doesnt have __func__ */
81*eabc0478Schristos #if defined(EVENT__HAVE___func__)
82*eabc0478Schristos # ifndef __func__
83*eabc0478Schristos #  define __func__ __func__
84*eabc0478Schristos # endif
85*eabc0478Schristos #elif defined(EVENT__HAVE___FUNCTION__)
86*eabc0478Schristos # define __func__ __FUNCTION__
87*eabc0478Schristos #else
88*eabc0478Schristos # define __func__ __FILE__
898585484eSchristos #endif
908585484eSchristos 
918585484eSchristos /* A good no-op to use in macro definitions. */
928585484eSchristos #define EVUTIL_NIL_STMT_ ((void)0)
938585484eSchristos /* A no-op that tricks the compiler into thinking a condition is used while
948585484eSchristos  * definitely not making any code for it.  Used to compile out asserts while
958585484eSchristos  * avoiding "unused variable" warnings.  The "!" forces the compiler to
968585484eSchristos  * do the sizeof() on an int, in case "condition" is a bitfield value.
978585484eSchristos  */
988585484eSchristos #define EVUTIL_NIL_CONDITION_(condition) do { \
998585484eSchristos 	(void)sizeof(!(condition));  \
1008585484eSchristos } while(0)
1018585484eSchristos 
1028585484eSchristos /* Internal use only: macros to match patterns of error codes in a
1038585484eSchristos    cross-platform way.  We need these macros because of two historical
1048585484eSchristos    reasons: first, nonblocking IO functions are generally written to give an
1058585484eSchristos    error on the "blocked now, try later" case, so sometimes an error from a
1068585484eSchristos    read, write, connect, or accept means "no error; just wait for more
1078585484eSchristos    data," and we need to look at the error code.  Second, Windows defines
1088585484eSchristos    a different set of error codes for sockets. */
1098585484eSchristos 
1108585484eSchristos #ifndef _WIN32
1118585484eSchristos 
1128585484eSchristos #if EAGAIN == EWOULDBLOCK
1138585484eSchristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1148585484eSchristos 	((e) == EAGAIN)
1158585484eSchristos #else
1168585484eSchristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1178585484eSchristos 	((e) == EAGAIN || (e) == EWOULDBLOCK)
1188585484eSchristos #endif
1198585484eSchristos 
1208585484eSchristos /* True iff e is an error that means a read/write operation can be retried. */
1218585484eSchristos #define EVUTIL_ERR_RW_RETRIABLE(e)				\
1228585484eSchristos 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e))
1238585484eSchristos /* True iff e is an error that means an connect can be retried. */
1248585484eSchristos #define EVUTIL_ERR_CONNECT_RETRIABLE(e)			\
1258585484eSchristos 	((e) == EINTR || (e) == EINPROGRESS)
1268585484eSchristos /* True iff e is an error that means a accept can be retried. */
1278585484eSchristos #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
1288585484eSchristos 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e) || (e) == ECONNABORTED)
1298585484eSchristos 
1308585484eSchristos /* True iff e is an error that means the connection was refused */
1318585484eSchristos #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
1328585484eSchristos 	((e) == ECONNREFUSED)
1338585484eSchristos 
1348585484eSchristos #else
1358585484eSchristos /* Win32 */
1368585484eSchristos 
1378585484eSchristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1388585484eSchristos 	((e) == WSAEWOULDBLOCK || (e) == EAGAIN)
1398585484eSchristos 
1408585484eSchristos #define EVUTIL_ERR_RW_RETRIABLE(e)					\
1418585484eSchristos 	((e) == WSAEWOULDBLOCK ||					\
1428585484eSchristos 	    (e) == WSAEINTR)
1438585484eSchristos 
1448585484eSchristos #define EVUTIL_ERR_CONNECT_RETRIABLE(e)					\
1458585484eSchristos 	((e) == WSAEWOULDBLOCK ||					\
1468585484eSchristos 	    (e) == WSAEINTR ||						\
1478585484eSchristos 	    (e) == WSAEINPROGRESS ||					\
1488585484eSchristos 	    (e) == WSAEINVAL)
1498585484eSchristos 
1508585484eSchristos #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
1518585484eSchristos 	EVUTIL_ERR_RW_RETRIABLE(e)
1528585484eSchristos 
1538585484eSchristos #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
1548585484eSchristos 	((e) == WSAECONNREFUSED)
1558585484eSchristos 
1568585484eSchristos #endif
1578585484eSchristos 
1588585484eSchristos /* Arguments for shutdown() */
1598585484eSchristos #ifdef SHUT_RD
1608585484eSchristos #define EVUTIL_SHUT_RD SHUT_RD
1618585484eSchristos #else
1628585484eSchristos #define EVUTIL_SHUT_RD 0
1638585484eSchristos #endif
1648585484eSchristos #ifdef SHUT_WR
1658585484eSchristos #define EVUTIL_SHUT_WR SHUT_WR
1668585484eSchristos #else
167*eabc0478Schristos #define EVUTIL_SHUT_WR 1 /* SD_SEND */
1688585484eSchristos #endif
1698585484eSchristos #ifdef SHUT_BOTH
1708585484eSchristos #define EVUTIL_SHUT_BOTH SHUT_BOTH
1718585484eSchristos #else
1728585484eSchristos #define EVUTIL_SHUT_BOTH 2
1738585484eSchristos #endif
1748585484eSchristos 
1758585484eSchristos /* Helper: Verify that all the elements in 'dlist' are internally consistent.
1768585484eSchristos  * Checks for circular lists and bad prev/next pointers.
1778585484eSchristos  *
1788585484eSchristos  * Example usage:
1798585484eSchristos  *    EVUTIL_ASSERT_LIST_OK(eventlist, event, ev_next);
1808585484eSchristos  */
1818585484eSchristos #define EVUTIL_ASSERT_LIST_OK(dlist, type, field) do {			\
1828585484eSchristos 		struct type *elm1, *elm2, **nextp;			\
1838585484eSchristos 		if (LIST_EMPTY((dlist)))				\
1848585484eSchristos 			break;						\
1858585484eSchristos 									\
1868585484eSchristos 		/* Check list for circularity using Floyd's */		\
1878585484eSchristos 		/* 'Tortoise and Hare' algorithm */			\
1888585484eSchristos 		elm1 = LIST_FIRST((dlist));				\
1898585484eSchristos 		elm2 = LIST_NEXT(elm1, field);				\
1908585484eSchristos 		while (elm1 && elm2) {					\
1918585484eSchristos 			EVUTIL_ASSERT(elm1 != elm2);			\
1928585484eSchristos 			elm1 = LIST_NEXT(elm1, field);			\
1938585484eSchristos 			elm2 = LIST_NEXT(elm2, field);			\
1948585484eSchristos 			if (!elm2)					\
1958585484eSchristos 				break;					\
1968585484eSchristos 			EVUTIL_ASSERT(elm1 != elm2);			\
1978585484eSchristos 			elm2 = LIST_NEXT(elm2, field);			\
1988585484eSchristos 		}							\
1998585484eSchristos 									\
2008585484eSchristos 		/* Now check next and prev pointers for consistency. */ \
2018585484eSchristos 		nextp = &LIST_FIRST((dlist));				\
2028585484eSchristos 		elm1 = LIST_FIRST((dlist));				\
2038585484eSchristos 		while (elm1) {						\
2048585484eSchristos 			EVUTIL_ASSERT(*nextp == elm1);			\
2058585484eSchristos 			EVUTIL_ASSERT(nextp == elm1->field.le_prev);	\
2068585484eSchristos 			nextp = &LIST_NEXT(elm1, field);		\
2078585484eSchristos 			elm1 = *nextp;					\
2088585484eSchristos 		}							\
2098585484eSchristos 	} while (0)
2108585484eSchristos 
2118585484eSchristos /* Helper: Verify that all the elements in a TAILQ are internally consistent.
2128585484eSchristos  * Checks for circular lists and bad prev/next pointers.
2138585484eSchristos  *
2148585484eSchristos  * Example usage:
2158585484eSchristos  *    EVUTIL_ASSERT_TAILQ_OK(activelist, event, ev_active_next);
2168585484eSchristos  */
2178585484eSchristos #define EVUTIL_ASSERT_TAILQ_OK(tailq, type, field) do {			\
2188585484eSchristos 		struct type *elm1, *elm2, **nextp;			\
2198585484eSchristos 		if (TAILQ_EMPTY((tailq)))				\
2208585484eSchristos 			break;						\
2218585484eSchristos 									\
2228585484eSchristos 		/* Check list for circularity using Floyd's */		\
2238585484eSchristos 		/* 'Tortoise and Hare' algorithm */			\
2248585484eSchristos 		elm1 = TAILQ_FIRST((tailq));				\
2258585484eSchristos 		elm2 = TAILQ_NEXT(elm1, field);				\
2268585484eSchristos 		while (elm1 && elm2) {					\
2278585484eSchristos 			EVUTIL_ASSERT(elm1 != elm2);			\
2288585484eSchristos 			elm1 = TAILQ_NEXT(elm1, field);			\
2298585484eSchristos 			elm2 = TAILQ_NEXT(elm2, field);			\
2308585484eSchristos 			if (!elm2)					\
2318585484eSchristos 				break;					\
2328585484eSchristos 			EVUTIL_ASSERT(elm1 != elm2);			\
2338585484eSchristos 			elm2 = TAILQ_NEXT(elm2, field);			\
2348585484eSchristos 		}							\
2358585484eSchristos 									\
2368585484eSchristos 		/* Now check next and prev pointers for consistency. */ \
2378585484eSchristos 		nextp = &TAILQ_FIRST((tailq));				\
2388585484eSchristos 		elm1 = TAILQ_FIRST((tailq));				\
2398585484eSchristos 		while (elm1) {						\
2408585484eSchristos 			EVUTIL_ASSERT(*nextp == elm1);			\
2418585484eSchristos 			EVUTIL_ASSERT(nextp == elm1->field.tqe_prev);	\
2428585484eSchristos 			nextp = &TAILQ_NEXT(elm1, field);		\
2438585484eSchristos 			elm1 = *nextp;					\
2448585484eSchristos 		}							\
2458585484eSchristos 		EVUTIL_ASSERT(nextp == (tailq)->tqh_last);		\
2468585484eSchristos 	} while (0)
2478585484eSchristos 
2488585484eSchristos /* Locale-independent replacements for some ctypes functions.  Use these
2498585484eSchristos  * when you care about ASCII's notion of character types, because you are about
2508585484eSchristos  * to send those types onto the wire.
2518585484eSchristos  */
252*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2538585484eSchristos int EVUTIL_ISALPHA_(char c);
254*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2558585484eSchristos int EVUTIL_ISALNUM_(char c);
2568585484eSchristos int EVUTIL_ISSPACE_(char c);
257*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2588585484eSchristos int EVUTIL_ISDIGIT_(char c);
259*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2608585484eSchristos int EVUTIL_ISXDIGIT_(char c);
2618585484eSchristos int EVUTIL_ISPRINT_(char c);
2628585484eSchristos int EVUTIL_ISLOWER_(char c);
2638585484eSchristos int EVUTIL_ISUPPER_(char c);
264*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2658585484eSchristos char EVUTIL_TOUPPER_(char c);
266*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2678585484eSchristos char EVUTIL_TOLOWER_(char c);
2688585484eSchristos 
2698585484eSchristos /** Remove all trailing horizontal whitespace (space or tab) from the end of a
2708585484eSchristos  * string */
271*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2728585484eSchristos void evutil_rtrim_lws_(char *);
2738585484eSchristos 
2748585484eSchristos 
2758585484eSchristos /** Helper macro.  If we know that a given pointer points to a field in a
2768585484eSchristos     structure, return a pointer to the structure itself.  Used to implement
2778585484eSchristos     our half-baked C OO.  Example:
2788585484eSchristos 
2798585484eSchristos     struct subtype {
2808585484eSchristos 	int x;
2818585484eSchristos 	struct supertype common;
2828585484eSchristos 	int y;
2838585484eSchristos     };
2848585484eSchristos     ...
2858585484eSchristos     void fn(struct supertype *super) {
2868585484eSchristos 	struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
2878585484eSchristos 	...
2888585484eSchristos     }
2898585484eSchristos  */
2908585484eSchristos #define EVUTIL_UPCAST(ptr, type, field)				\
2918585484eSchristos 	((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
2928585484eSchristos 
2938585484eSchristos /* As open(pathname, flags, mode), except that the file is always opened with
2948585484eSchristos  * the close-on-exec flag set. (And the mode argument is mandatory.)
2958585484eSchristos  */
2968585484eSchristos int evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode);
2978585484eSchristos 
298*eabc0478Schristos EVENT2_EXPORT_SYMBOL
2998585484eSchristos int evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
3008585484eSchristos     int is_binary);
3018585484eSchristos 
302*eabc0478Schristos EVENT2_EXPORT_SYMBOL
303*eabc0478Schristos int evutil_socket_connect_(evutil_socket_t *fd_ptr, const struct sockaddr *sa, int socklen);
3048585484eSchristos 
3058585484eSchristos int evutil_socket_finished_connecting_(evutil_socket_t fd);
3068585484eSchristos 
307*eabc0478Schristos EVENT2_EXPORT_SYMBOL
308*eabc0478Schristos int evutil_ersatz_socketpair_(int, int , int, evutil_socket_t[]);
3098585484eSchristos 
3108585484eSchristos int evutil_resolve_(int family, const char *hostname, struct sockaddr *sa,
3118585484eSchristos     ev_socklen_t *socklen, int port);
3128585484eSchristos 
3138585484eSchristos const char *evutil_getenv_(const char *name);
3148585484eSchristos 
3158585484eSchristos /* Structure to hold the state of our weak random number generator.
3168585484eSchristos  */
3178585484eSchristos struct evutil_weakrand_state {
3188585484eSchristos 	ev_uint32_t seed;
3198585484eSchristos };
3208585484eSchristos 
3218585484eSchristos #define EVUTIL_WEAKRAND_MAX EV_INT32_MAX
3228585484eSchristos 
3238585484eSchristos /* Initialize the state of a week random number generator based on 'seed'.  If
3248585484eSchristos  * the seed is 0, construct a new seed based on not-very-strong platform
3258585484eSchristos  * entropy, like the PID and the time of day.
3268585484eSchristos  *
3278585484eSchristos  * This function, and the other evutil_weakrand* functions, are meant for
3288585484eSchristos  * speed, not security or statistical strength.  If you need a RNG which an
3298585484eSchristos  * attacker can't predict, or which passes strong statistical tests, use the
3308585484eSchristos  * evutil_secure_rng* functions instead.
3318585484eSchristos  */
332*eabc0478Schristos EVENT2_EXPORT_SYMBOL
3338585484eSchristos ev_uint32_t evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed);
3348585484eSchristos /* Return a pseudorandom value between 0 and EVUTIL_WEAKRAND_MAX inclusive.
3358585484eSchristos  * Updates the state in 'seed' as needed -- this value must be protected by a
3368585484eSchristos  * lock.
3378585484eSchristos  */
338*eabc0478Schristos EVENT2_EXPORT_SYMBOL
3398585484eSchristos ev_int32_t evutil_weakrand_(struct evutil_weakrand_state *seed);
3408585484eSchristos /* Return a pseudorandom value x such that 0 <= x < top. top must be no more
3418585484eSchristos  * than EVUTIL_WEAKRAND_MAX. Updates the state in 'seed' as needed -- this
3428585484eSchristos  * value must be proteced by a lock */
343*eabc0478Schristos EVENT2_EXPORT_SYMBOL
3448585484eSchristos ev_int32_t evutil_weakrand_range_(struct evutil_weakrand_state *seed, ev_int32_t top);
3458585484eSchristos 
3468585484eSchristos /* Evaluates to the same boolean value as 'p', and hints to the compiler that
3478585484eSchristos  * we expect this value to be false. */
3488585484eSchristos #if defined(__GNUC__) && __GNUC__ >= 3         /* gcc 3.0 or later */
3498585484eSchristos #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
3508585484eSchristos #else
3518585484eSchristos #define EVUTIL_UNLIKELY(p) (p)
3528585484eSchristos #endif
3538585484eSchristos 
354*eabc0478Schristos #if EVUTIL_HAS_ATTRIBUTE(fallthrough)
355*eabc0478Schristos #define EVUTIL_FALLTHROUGH __attribute__((fallthrough))
356*eabc0478Schristos #else
357*eabc0478Schristos #define EVUTIL_FALLTHROUGH /* fallthrough */
358*eabc0478Schristos #endif
359*eabc0478Schristos 
3608585484eSchristos /* Replacement for assert() that calls event_errx on failure. */
3618585484eSchristos #ifdef NDEBUG
3628585484eSchristos #define EVUTIL_ASSERT(cond) EVUTIL_NIL_CONDITION_(cond)
3638585484eSchristos #define EVUTIL_FAILURE_CHECK(cond) 0
3648585484eSchristos #else
3658585484eSchristos #define EVUTIL_ASSERT(cond)						\
3668585484eSchristos 	do {								\
3678585484eSchristos 		if (EVUTIL_UNLIKELY(!(cond))) {				\
3688585484eSchristos 			event_errx(EVENT_ERR_ABORT_,			\
3698585484eSchristos 			    "%s:%d: Assertion %s failed in %s",		\
3708585484eSchristos 			    __FILE__,__LINE__,#cond,__func__);		\
3718585484eSchristos 			/* In case a user-supplied handler tries to */	\
3728585484eSchristos 			/* return control to us, log and abort here. */	\
3738585484eSchristos 			(void)fprintf(stderr,				\
3748585484eSchristos 			    "%s:%d: Assertion %s failed in %s",		\
3758585484eSchristos 			    __FILE__,__LINE__,#cond,__func__);		\
3768585484eSchristos 			abort();					\
3778585484eSchristos 		}							\
3788585484eSchristos 	} while (0)
3798585484eSchristos #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
3808585484eSchristos #endif
3818585484eSchristos 
3828585484eSchristos #ifndef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE
3838585484eSchristos /* Replacement for sockaddr storage that we can use internally on platforms
3848585484eSchristos  * that lack it.  It is not space-efficient, but neither is sockaddr_storage.
3858585484eSchristos  */
3868585484eSchristos struct sockaddr_storage {
3878585484eSchristos 	union {
3888585484eSchristos 		struct sockaddr ss_sa;
3898585484eSchristos 		struct sockaddr_in ss_sin;
3908585484eSchristos 		struct sockaddr_in6 ss_sin6;
3918585484eSchristos 		char ss_padding[128];
3928585484eSchristos 	} ss_union;
3938585484eSchristos };
3948585484eSchristos #define ss_family ss_union.ss_sa.sa_family
3958585484eSchristos #endif
3968585484eSchristos 
3978585484eSchristos /* Internal addrinfo error code.  This one is returned from only from
3988585484eSchristos  * evutil_getaddrinfo_common_, when we are sure that we'll have to hit a DNS
3998585484eSchristos  * server. */
4008585484eSchristos #define EVUTIL_EAI_NEED_RESOLVE      -90002
4018585484eSchristos 
4028585484eSchristos struct evdns_base;
4038585484eSchristos struct evdns_getaddrinfo_request;
4048585484eSchristos typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
4058585484eSchristos     struct evdns_base *base,
4068585484eSchristos     const char *nodename, const char *servname,
4078585484eSchristos     const struct evutil_addrinfo *hints_in,
4088585484eSchristos     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
409*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4108585484eSchristos void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn);
411*eabc0478Schristos typedef void (*evdns_getaddrinfo_cancel_fn)(
412*eabc0478Schristos     struct evdns_getaddrinfo_request *req);
413*eabc0478Schristos EVENT2_EXPORT_SYMBOL
414*eabc0478Schristos void evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn);
4158585484eSchristos 
416*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4178585484eSchristos struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa,
4188585484eSchristos     ev_socklen_t socklen, const struct evutil_addrinfo *hints);
419*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4208585484eSchristos struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first,
4218585484eSchristos     struct evutil_addrinfo *append);
422*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4238585484eSchristos void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints);
424*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4258585484eSchristos int evutil_getaddrinfo_common_(const char *nodename, const char *servname,
4268585484eSchristos     struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
4278585484eSchristos 
428*eabc0478Schristos struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_(
429*eabc0478Schristos     struct evdns_base *dns_base,
4308585484eSchristos     const char *nodename, const char *servname,
4318585484eSchristos     const struct evutil_addrinfo *hints_in,
4328585484eSchristos     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
433*eabc0478Schristos void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data);
4348585484eSchristos 
4358585484eSchristos /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
4368585484eSchristos  * ::1). */
437*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4388585484eSchristos int evutil_sockaddr_is_loopback_(const struct sockaddr *sa);
4398585484eSchristos 
4408585484eSchristos 
4418585484eSchristos /**
4428585484eSchristos     Formats a sockaddr sa into a string buffer of size outlen stored in out.
4438585484eSchristos     Returns a pointer to out.  Always writes something into out, so it's safe
4448585484eSchristos     to use the output of this function without checking it for NULL.
4458585484eSchristos  */
446*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4478585484eSchristos const char *evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen);
4488585484eSchristos 
4498585484eSchristos int evutil_hex_char_to_int_(char c);
4508585484eSchristos 
4518585484eSchristos 
4528585484eSchristos void evutil_free_secure_rng_globals_(void);
4538585484eSchristos void evutil_free_globals_(void);
4548585484eSchristos 
4558585484eSchristos #ifdef _WIN32
456*eabc0478Schristos EVENT2_EXPORT_SYMBOL
4577476e6e4Schristos HMODULE evutil_load_windows_system_library_(const TCHAR *library_name);
4588585484eSchristos #endif
4598585484eSchristos 
4608585484eSchristos #ifndef EV_SIZE_FMT
4618585484eSchristos #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
4628585484eSchristos #define EV_U64_FMT "%I64u"
4638585484eSchristos #define EV_I64_FMT "%I64d"
4648585484eSchristos #define EV_I64_ARG(x) ((__int64)(x))
4658585484eSchristos #define EV_U64_ARG(x) ((unsigned __int64)(x))
4668585484eSchristos #else
4678585484eSchristos #define EV_U64_FMT "%llu"
4688585484eSchristos #define EV_I64_FMT "%lld"
4698585484eSchristos #define EV_I64_ARG(x) ((long long)(x))
4708585484eSchristos #define EV_U64_ARG(x) ((unsigned long long)(x))
4718585484eSchristos #endif
4728585484eSchristos #endif
4738585484eSchristos 
4748585484eSchristos #ifdef _WIN32
4758585484eSchristos #define EV_SOCK_FMT EV_I64_FMT
4768585484eSchristos #define EV_SOCK_ARG(x) EV_I64_ARG((x))
4778585484eSchristos #else
4788585484eSchristos #define EV_SOCK_FMT "%d"
4798585484eSchristos #define EV_SOCK_ARG(x) (x)
4808585484eSchristos #endif
4818585484eSchristos 
482*eabc0478Schristos #if defined(__STDC__) && defined(__STDC_VERSION__) && !defined(__MINGW64_VERSION_MAJOR)
4838585484eSchristos #if (__STDC_VERSION__ >= 199901L)
4848585484eSchristos #define EV_SIZE_FMT "%zu"
4858585484eSchristos #define EV_SSIZE_FMT "%zd"
4868585484eSchristos #define EV_SIZE_ARG(x) (x)
4878585484eSchristos #define EV_SSIZE_ARG(x) (x)
4888585484eSchristos #endif
4898585484eSchristos #endif
4908585484eSchristos 
4918585484eSchristos #ifndef EV_SIZE_FMT
4928585484eSchristos #if (EVENT__SIZEOF_SIZE_T <= EVENT__SIZEOF_LONG)
4938585484eSchristos #define EV_SIZE_FMT "%lu"
4948585484eSchristos #define EV_SSIZE_FMT "%ld"
4958585484eSchristos #define EV_SIZE_ARG(x) ((unsigned long)(x))
4968585484eSchristos #define EV_SSIZE_ARG(x) ((long)(x))
4978585484eSchristos #else
4988585484eSchristos #define EV_SIZE_FMT EV_U64_FMT
4998585484eSchristos #define EV_SSIZE_FMT EV_I64_FMT
5008585484eSchristos #define EV_SIZE_ARG(x) EV_U64_ARG(x)
5018585484eSchristos #define EV_SSIZE_ARG(x) EV_I64_ARG(x)
5028585484eSchristos #endif
5038585484eSchristos #endif
5048585484eSchristos 
505*eabc0478Schristos EVENT2_EXPORT_SYMBOL
5068585484eSchristos evutil_socket_t evutil_socket_(int domain, int type, int protocol);
5078585484eSchristos evutil_socket_t evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr,
5088585484eSchristos     ev_socklen_t *addrlen, int flags);
509b8ecfcfeSchristos 
510b8ecfcfeSchristos     /* used by one of the test programs.. */
511b8ecfcfeSchristos EVENT2_EXPORT_SYMBOL
5128585484eSchristos int evutil_make_internal_pipe_(evutil_socket_t fd[2]);
5138585484eSchristos evutil_socket_t evutil_eventfd_(unsigned initval, int flags);
5148585484eSchristos 
5158585484eSchristos #ifdef SOCK_NONBLOCK
5168585484eSchristos #define EVUTIL_SOCK_NONBLOCK SOCK_NONBLOCK
5178585484eSchristos #else
5188585484eSchristos #define EVUTIL_SOCK_NONBLOCK 0x4000000
5198585484eSchristos #endif
5208585484eSchristos #ifdef SOCK_CLOEXEC
5218585484eSchristos #define EVUTIL_SOCK_CLOEXEC SOCK_CLOEXEC
5228585484eSchristos #else
5238585484eSchristos #define EVUTIL_SOCK_CLOEXEC 0x80000000
5248585484eSchristos #endif
5258585484eSchristos #ifdef EFD_NONBLOCK
5268585484eSchristos #define EVUTIL_EFD_NONBLOCK EFD_NONBLOCK
5278585484eSchristos #else
5288585484eSchristos #define EVUTIL_EFD_NONBLOCK 0x4000
5298585484eSchristos #endif
5308585484eSchristos #ifdef EFD_CLOEXEC
5318585484eSchristos #define EVUTIL_EFD_CLOEXEC EFD_CLOEXEC
5328585484eSchristos #else
5338585484eSchristos #define EVUTIL_EFD_CLOEXEC 0x8000
5348585484eSchristos #endif
5358585484eSchristos 
536b8ecfcfeSchristos void evutil_memclear_(void *mem, size_t len);
5378585484eSchristos 
538*eabc0478Schristos struct in_addr;
539*eabc0478Schristos struct in6_addr;
540*eabc0478Schristos 
541*eabc0478Schristos /* This is a any, loopback, link-local, multicast */
542*eabc0478Schristos EVENT2_EXPORT_SYMBOL
543*eabc0478Schristos int evutil_v4addr_is_local_(const struct in_addr *in);
544*eabc0478Schristos /* This is a reserved, ipv4compat, ipv4map, loopback,
545*eabc0478Schristos  * link-local, multicast, or unspecified address. */
546*eabc0478Schristos EVENT2_EXPORT_SYMBOL
547*eabc0478Schristos int evutil_v6addr_is_local_(const struct in6_addr *in);
548*eabc0478Schristos 
5498585484eSchristos #ifdef __cplusplus
5508585484eSchristos }
5518585484eSchristos #endif
5528585484eSchristos 
5538585484eSchristos #endif
554