xref: /netbsd-src/external/bsd/libevent/dist/util-internal.h (revision f8570f8a97f412a33574ec77d412b16dbe332965)
1*f8570f8aSmrg /*	$NetBSD: util-internal.h,v 1.8 2023/08/01 07:04:14 mrg Exp $	*/
27e68cdd7Schristos 
36ecf6635Schristos /*
46ecf6635Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
280d738af4Schristos #ifndef UTIL_INTERNAL_H_INCLUDED_
290d738af4Schristos #define UTIL_INTERNAL_H_INCLUDED_
306ecf6635Schristos 
316ecf6635Schristos #include "event2/event-config.h"
320d738af4Schristos #include "evconfig-private.h"
330d738af4Schristos 
346ecf6635Schristos #include <errno.h>
356ecf6635Schristos 
366ecf6635Schristos /* For EVUTIL_ASSERT */
376ecf6635Schristos #include "log-internal.h"
386ecf6635Schristos #include <stdio.h>
396ecf6635Schristos #include <stdlib.h>
400d738af4Schristos #ifdef EVENT__HAVE_SYS_SOCKET_H
416ecf6635Schristos #include <sys/socket.h>
426ecf6635Schristos #endif
430d738af4Schristos #ifdef EVENT__HAVE_SYS_EVENTFD_H
440d738af4Schristos #include <sys/eventfd.h>
450d738af4Schristos #endif
466ecf6635Schristos #include "event2/util.h"
476ecf6635Schristos 
480d738af4Schristos #include "time-internal.h"
496ecf6635Schristos #include "ipv6-internal.h"
506ecf6635Schristos 
516ecf6635Schristos #ifdef __cplusplus
526ecf6635Schristos extern "C" {
536ecf6635Schristos #endif
546ecf6635Schristos 
557e68cdd7Schristos /* __has_attribute() wrapper */
567e68cdd7Schristos #ifdef __has_attribute
577e68cdd7Schristos # define EVUTIL_HAS_ATTRIBUTE __has_attribute
587e68cdd7Schristos #endif
597e68cdd7Schristos /** clang 3 __has_attribute misbehaves in some versions */
607e68cdd7Schristos #if defined(__clang__) && __clang__ == 1
617e68cdd7Schristos # if defined(__apple_build_version__)
627e68cdd7Schristos #  if __clang_major__ <= 6
637e68cdd7Schristos #   undef EVUTIL_HAS_ATTRIBUTE
647e68cdd7Schristos #  endif
657e68cdd7Schristos # else /* !__apple_build_version__ */
667e68cdd7Schristos #  if __clang_major__ == 3 && __clang_minor__ >= 2 && __clang_minor__ <= 5
677e68cdd7Schristos #   undef EVUTIL_HAS_ATTRIBUTE
687e68cdd7Schristos #  endif
697e68cdd7Schristos # endif /* __apple_build_version__ */
707e68cdd7Schristos #endif /*\ defined(__clang__) && __clang__ == 1 */
717e68cdd7Schristos #ifndef EVUTIL_HAS_ATTRIBUTE
727e68cdd7Schristos # define EVUTIL_HAS_ATTRIBUTE(x) 0
737e68cdd7Schristos #endif
747e68cdd7Schristos 
756ecf6635Schristos /* If we need magic to say "inline", get it for free internally. */
760d738af4Schristos #ifdef EVENT__inline
770d738af4Schristos #define inline EVENT__inline
786ecf6635Schristos #endif
797e68cdd7Schristos 
807e68cdd7Schristos /* Define to appropriate substitute if compiler doesnt have __func__ */
817e68cdd7Schristos #if defined(EVENT__HAVE___func__)
827e68cdd7Schristos # ifndef __func__
837e68cdd7Schristos #  define __func__ __func__
847e68cdd7Schristos # endif
857e68cdd7Schristos #elif defined(EVENT__HAVE___FUNCTION__)
867e68cdd7Schristos # define __func__ __FUNCTION__
877e68cdd7Schristos #else
887e68cdd7Schristos # define __func__ __FILE__
896ecf6635Schristos #endif
906ecf6635Schristos 
916ecf6635Schristos /* A good no-op to use in macro definitions. */
920d738af4Schristos #define EVUTIL_NIL_STMT_ ((void)0)
936ecf6635Schristos /* A no-op that tricks the compiler into thinking a condition is used while
946ecf6635Schristos  * definitely not making any code for it.  Used to compile out asserts while
956ecf6635Schristos  * avoiding "unused variable" warnings.  The "!" forces the compiler to
966ecf6635Schristos  * do the sizeof() on an int, in case "condition" is a bitfield value.
976ecf6635Schristos  */
980d738af4Schristos #define EVUTIL_NIL_CONDITION_(condition) do { \
996ecf6635Schristos 	(void)sizeof(!(condition));  \
10081d2345cSrillig } while(0)
1016ecf6635Schristos 
1026ecf6635Schristos /* Internal use only: macros to match patterns of error codes in a
1036ecf6635Schristos    cross-platform way.  We need these macros because of two historical
1046ecf6635Schristos    reasons: first, nonblocking IO functions are generally written to give an
1056ecf6635Schristos    error on the "blocked now, try later" case, so sometimes an error from a
1066ecf6635Schristos    read, write, connect, or accept means "no error; just wait for more
1076ecf6635Schristos    data," and we need to look at the error code.  Second, Windows defines
1086ecf6635Schristos    a different set of error codes for sockets. */
1096ecf6635Schristos 
1100d738af4Schristos #ifndef _WIN32
1110d738af4Schristos 
1120d738af4Schristos #if EAGAIN == EWOULDBLOCK
1130d738af4Schristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1140d738af4Schristos 	((e) == EAGAIN)
1150d738af4Schristos #else
1160d738af4Schristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1170d738af4Schristos 	((e) == EAGAIN || (e) == EWOULDBLOCK)
1180d738af4Schristos #endif
1196ecf6635Schristos 
1206ecf6635Schristos /* True iff e is an error that means a read/write operation can be retried. */
1216ecf6635Schristos #define EVUTIL_ERR_RW_RETRIABLE(e)				\
1220d738af4Schristos 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e))
1236ecf6635Schristos /* True iff e is an error that means an connect can be retried. */
1246ecf6635Schristos #define EVUTIL_ERR_CONNECT_RETRIABLE(e)			\
1256ecf6635Schristos 	((e) == EINTR || (e) == EINPROGRESS)
1266ecf6635Schristos /* True iff e is an error that means a accept can be retried. */
1276ecf6635Schristos #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
1280d738af4Schristos 	((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e) || (e) == ECONNABORTED)
1296ecf6635Schristos 
1306ecf6635Schristos /* True iff e is an error that means the connection was refused */
1316ecf6635Schristos #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
1326ecf6635Schristos 	((e) == ECONNREFUSED)
1336ecf6635Schristos 
1346ecf6635Schristos #else
1350d738af4Schristos /* Win32 */
1360d738af4Schristos 
1370d738af4Schristos #define EVUTIL_ERR_IS_EAGAIN(e) \
1380d738af4Schristos 	((e) == WSAEWOULDBLOCK || (e) == EAGAIN)
1396ecf6635Schristos 
1406ecf6635Schristos #define EVUTIL_ERR_RW_RETRIABLE(e)					\
1416ecf6635Schristos 	((e) == WSAEWOULDBLOCK ||					\
1426ecf6635Schristos 	    (e) == WSAEINTR)
1436ecf6635Schristos 
1446ecf6635Schristos #define EVUTIL_ERR_CONNECT_RETRIABLE(e)					\
1456ecf6635Schristos 	((e) == WSAEWOULDBLOCK ||					\
1466ecf6635Schristos 	    (e) == WSAEINTR ||						\
1476ecf6635Schristos 	    (e) == WSAEINPROGRESS ||					\
1486ecf6635Schristos 	    (e) == WSAEINVAL)
1496ecf6635Schristos 
1506ecf6635Schristos #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)			\
1516ecf6635Schristos 	EVUTIL_ERR_RW_RETRIABLE(e)
1526ecf6635Schristos 
1536ecf6635Schristos #define EVUTIL_ERR_CONNECT_REFUSED(e)					\
1546ecf6635Schristos 	((e) == WSAECONNREFUSED)
1556ecf6635Schristos 
1566ecf6635Schristos #endif
1576ecf6635Schristos 
1586ecf6635Schristos /* Arguments for shutdown() */
1596ecf6635Schristos #ifdef SHUT_RD
1606ecf6635Schristos #define EVUTIL_SHUT_RD SHUT_RD
1616ecf6635Schristos #else
1626ecf6635Schristos #define EVUTIL_SHUT_RD 0
1636ecf6635Schristos #endif
1646ecf6635Schristos #ifdef SHUT_WR
1656ecf6635Schristos #define EVUTIL_SHUT_WR SHUT_WR
1666ecf6635Schristos #else
1670d738af4Schristos #define EVUTIL_SHUT_WR 1 /* SD_SEND */
1686ecf6635Schristos #endif
1696ecf6635Schristos #ifdef SHUT_BOTH
1706ecf6635Schristos #define EVUTIL_SHUT_BOTH SHUT_BOTH
1716ecf6635Schristos #else
1726ecf6635Schristos #define EVUTIL_SHUT_BOTH 2
1736ecf6635Schristos #endif
1746ecf6635Schristos 
1750d738af4Schristos /* Helper: Verify that all the elements in 'dlist' are internally consistent.
1760d738af4Schristos  * Checks for circular lists and bad prev/next pointers.
1770d738af4Schristos  *
1780d738af4Schristos  * Example usage:
1790d738af4Schristos  *    EVUTIL_ASSERT_LIST_OK(eventlist, event, ev_next);
1800d738af4Schristos  */
1810d738af4Schristos #define EVUTIL_ASSERT_LIST_OK(dlist, type, field) do {			\
1820d738af4Schristos 		struct type *elm1, *elm2, **nextp;			\
1830d738af4Schristos 		if (LIST_EMPTY((dlist)))				\
1840d738af4Schristos 			break;						\
1850d738af4Schristos 									\
1860d738af4Schristos 		/* Check list for circularity using Floyd's */		\
1870d738af4Schristos 		/* 'Tortoise and Hare' algorithm */			\
1880d738af4Schristos 		elm1 = LIST_FIRST((dlist));				\
1890d738af4Schristos 		elm2 = LIST_NEXT(elm1, field);				\
1900d738af4Schristos 		while (elm1 && elm2) {					\
1910d738af4Schristos 			EVUTIL_ASSERT(elm1 != elm2);			\
1920d738af4Schristos 			elm1 = LIST_NEXT(elm1, field);			\
1930d738af4Schristos 			elm2 = LIST_NEXT(elm2, field);			\
1940d738af4Schristos 			if (!elm2)					\
1950d738af4Schristos 				break;					\
1960d738af4Schristos 			EVUTIL_ASSERT(elm1 != elm2);			\
1970d738af4Schristos 			elm2 = LIST_NEXT(elm2, field);			\
1980d738af4Schristos 		}							\
1990d738af4Schristos 									\
2000d738af4Schristos 		/* Now check next and prev pointers for consistency. */ \
2010d738af4Schristos 		nextp = &LIST_FIRST((dlist));				\
2020d738af4Schristos 		elm1 = LIST_FIRST((dlist));				\
2030d738af4Schristos 		while (elm1) {						\
2040d738af4Schristos 			EVUTIL_ASSERT(*nextp == elm1);			\
2050d738af4Schristos 			EVUTIL_ASSERT(nextp == elm1->field.le_prev);	\
2060d738af4Schristos 			nextp = &LIST_NEXT(elm1, field);		\
2070d738af4Schristos 			elm1 = *nextp;					\
2080d738af4Schristos 		}							\
2090d738af4Schristos 	} while (0)
2100d738af4Schristos 
2110d738af4Schristos /* Helper: Verify that all the elements in a TAILQ are internally consistent.
2120d738af4Schristos  * Checks for circular lists and bad prev/next pointers.
2130d738af4Schristos  *
2140d738af4Schristos  * Example usage:
2150d738af4Schristos  *    EVUTIL_ASSERT_TAILQ_OK(activelist, event, ev_active_next);
2160d738af4Schristos  */
2170d738af4Schristos #define EVUTIL_ASSERT_TAILQ_OK(tailq, type, field) do {			\
2180d738af4Schristos 		struct type *elm1, *elm2, **nextp;			\
2190d738af4Schristos 		if (TAILQ_EMPTY((tailq)))				\
2200d738af4Schristos 			break;						\
2210d738af4Schristos 									\
2220d738af4Schristos 		/* Check list for circularity using Floyd's */		\
2230d738af4Schristos 		/* 'Tortoise and Hare' algorithm */			\
2240d738af4Schristos 		elm1 = TAILQ_FIRST((tailq));				\
2250d738af4Schristos 		elm2 = TAILQ_NEXT(elm1, field);				\
2260d738af4Schristos 		while (elm1 && elm2) {					\
2270d738af4Schristos 			EVUTIL_ASSERT(elm1 != elm2);			\
2280d738af4Schristos 			elm1 = TAILQ_NEXT(elm1, field);			\
2290d738af4Schristos 			elm2 = TAILQ_NEXT(elm2, field);			\
2300d738af4Schristos 			if (!elm2)					\
2310d738af4Schristos 				break;					\
2320d738af4Schristos 			EVUTIL_ASSERT(elm1 != elm2);			\
2330d738af4Schristos 			elm2 = TAILQ_NEXT(elm2, field);			\
2340d738af4Schristos 		}							\
2350d738af4Schristos 									\
2360d738af4Schristos 		/* Now check next and prev pointers for consistency. */ \
2370d738af4Schristos 		nextp = &TAILQ_FIRST((tailq));				\
2380d738af4Schristos 		elm1 = TAILQ_FIRST((tailq));				\
2390d738af4Schristos 		while (elm1) {						\
2400d738af4Schristos 			EVUTIL_ASSERT(*nextp == elm1);			\
2410d738af4Schristos 			EVUTIL_ASSERT(nextp == elm1->field.tqe_prev);	\
2420d738af4Schristos 			nextp = &TAILQ_NEXT(elm1, field);		\
2430d738af4Schristos 			elm1 = *nextp;					\
2440d738af4Schristos 		}							\
2450d738af4Schristos 		EVUTIL_ASSERT(nextp == (tailq)->tqh_last);		\
2460d738af4Schristos 	} while (0)
2470d738af4Schristos 
2486ecf6635Schristos /* Locale-independent replacements for some ctypes functions.  Use these
2496ecf6635Schristos  * when you care about ASCII's notion of character types, because you are about
2506ecf6635Schristos  * to send those types onto the wire.
2516ecf6635Schristos  */
2527e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2530d738af4Schristos int EVUTIL_ISALPHA_(char c);
2547e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2550d738af4Schristos int EVUTIL_ISALNUM_(char c);
2560d738af4Schristos int EVUTIL_ISSPACE_(char c);
2577e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2580d738af4Schristos int EVUTIL_ISDIGIT_(char c);
2597e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2600d738af4Schristos int EVUTIL_ISXDIGIT_(char c);
2610d738af4Schristos int EVUTIL_ISPRINT_(char c);
2620d738af4Schristos int EVUTIL_ISLOWER_(char c);
2630d738af4Schristos int EVUTIL_ISUPPER_(char c);
2647e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2650d738af4Schristos char EVUTIL_TOUPPER_(char c);
2667e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2670d738af4Schristos char EVUTIL_TOLOWER_(char c);
2680d738af4Schristos 
2690d738af4Schristos /** Remove all trailing horizontal whitespace (space or tab) from the end of a
2700d738af4Schristos  * string */
2717e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2720d738af4Schristos void evutil_rtrim_lws_(char *);
2730d738af4Schristos 
2746ecf6635Schristos 
2756ecf6635Schristos /** Helper macro.  If we know that a given pointer points to a field in a
2766ecf6635Schristos     structure, return a pointer to the structure itself.  Used to implement
2776ecf6635Schristos     our half-baked C OO.  Example:
2786ecf6635Schristos 
2796ecf6635Schristos     struct subtype {
2806ecf6635Schristos 	int x;
2816ecf6635Schristos 	struct supertype common;
2826ecf6635Schristos 	int y;
2836ecf6635Schristos     };
2846ecf6635Schristos     ...
2856ecf6635Schristos     void fn(struct supertype *super) {
2866ecf6635Schristos 	struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
2876ecf6635Schristos 	...
2886ecf6635Schristos     }
2896ecf6635Schristos  */
2906ecf6635Schristos #define EVUTIL_UPCAST(ptr, type, field)				\
2910d738af4Schristos 	((type *)__UNCONST(((const char *)(ptr)) - evutil_offsetof(type, field)))
2926ecf6635Schristos 
2936ecf6635Schristos /* As open(pathname, flags, mode), except that the file is always opened with
2946ecf6635Schristos  * the close-on-exec flag set. (And the mode argument is mandatory.)
2956ecf6635Schristos  */
2960d738af4Schristos int evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode);
2976ecf6635Schristos 
2987e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2990d738af4Schristos int evutil_read_file_(const char *filename, char **content_out, size_t *len_out,
3006ecf6635Schristos     int is_binary);
3016ecf6635Schristos 
3027e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3030d738af4Schristos int evutil_socket_connect_(evutil_socket_t *fd_ptr, const struct sockaddr *sa, int socklen);
3046ecf6635Schristos 
3050d738af4Schristos int evutil_socket_finished_connecting_(evutil_socket_t fd);
3066ecf6635Schristos 
3077e68cdd7Schristos EVENT2_EXPORT_SYMBOL
308*f8570f8aSmrg int evutil_ersatz_socketpair_(int, int , int, evutil_socket_t[2]);
3096ecf6635Schristos 
3100d738af4Schristos int evutil_resolve_(int family, const char *hostname, struct sockaddr *sa,
3116ecf6635Schristos     ev_socklen_t *socklen, int port);
3126ecf6635Schristos 
3130d738af4Schristos const char *evutil_getenv_(const char *name);
3146ecf6635Schristos 
3150d738af4Schristos /* Structure to hold the state of our weak random number generator.
3160d738af4Schristos  */
3170d738af4Schristos struct evutil_weakrand_state {
3180d738af4Schristos 	ev_uint32_t seed;
3190d738af4Schristos };
3200d738af4Schristos 
3210d738af4Schristos #define EVUTIL_WEAKRAND_MAX EV_INT32_MAX
3220d738af4Schristos 
3230d738af4Schristos /* Initialize the state of a week random number generator based on 'seed'.  If
3240d738af4Schristos  * the seed is 0, construct a new seed based on not-very-strong platform
3250d738af4Schristos  * entropy, like the PID and the time of day.
3260d738af4Schristos  *
3270d738af4Schristos  * This function, and the other evutil_weakrand* functions, are meant for
3280d738af4Schristos  * speed, not security or statistical strength.  If you need a RNG which an
3290d738af4Schristos  * attacker can't predict, or which passes strong statistical tests, use the
3300d738af4Schristos  * evutil_secure_rng* functions instead.
3310d738af4Schristos  */
3327e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3330d738af4Schristos ev_uint32_t evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed);
3340d738af4Schristos /* Return a pseudorandom value between 0 and EVUTIL_WEAKRAND_MAX inclusive.
3350d738af4Schristos  * Updates the state in 'seed' as needed -- this value must be protected by a
3360d738af4Schristos  * lock.
3370d738af4Schristos  */
3387e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3390d738af4Schristos ev_int32_t evutil_weakrand_(struct evutil_weakrand_state *seed);
3400d738af4Schristos /* Return a pseudorandom value x such that 0 <= x < top. top must be no more
3410d738af4Schristos  * than EVUTIL_WEAKRAND_MAX. Updates the state in 'seed' as needed -- this
3420d738af4Schristos  * value must be proteced by a lock */
3437e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3440d738af4Schristos ev_int32_t evutil_weakrand_range_(struct evutil_weakrand_state *seed, ev_int32_t top);
3456ecf6635Schristos 
3466ecf6635Schristos /* Evaluates to the same boolean value as 'p', and hints to the compiler that
3476ecf6635Schristos  * we expect this value to be false. */
3486ecf6635Schristos #if defined(__GNUC__) && __GNUC__ >= 3         /* gcc 3.0 or later */
3496ecf6635Schristos #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
3506ecf6635Schristos #else
3516ecf6635Schristos #define EVUTIL_UNLIKELY(p) (p)
3526ecf6635Schristos #endif
3536ecf6635Schristos 
3547e68cdd7Schristos #if EVUTIL_HAS_ATTRIBUTE(fallthrough) && !defined(__lint__)	// XXX: fixme
3557e68cdd7Schristos #define EVUTIL_FALLTHROUGH __attribute__((fallthrough))
3567e68cdd7Schristos #else
3577e68cdd7Schristos #define EVUTIL_FALLTHROUGH /* fallthrough */
3587e68cdd7Schristos #endif
3597e68cdd7Schristos 
3606ecf6635Schristos /* Replacement for assert() that calls event_errx on failure. */
3616ecf6635Schristos #ifdef NDEBUG
3620d738af4Schristos #define EVUTIL_ASSERT(cond) EVUTIL_NIL_CONDITION_(cond)
3636ecf6635Schristos #define EVUTIL_FAILURE_CHECK(cond) 0
3646ecf6635Schristos #else
3656ecf6635Schristos #define EVUTIL_ASSERT(cond)						\
3666ecf6635Schristos 	do {								\
3676ecf6635Schristos 		if (EVUTIL_UNLIKELY(!(cond))) {				\
3680d738af4Schristos 			event_errx(EVENT_ERR_ABORT_,			\
3696ecf6635Schristos 			    "%s:%d: Assertion %s failed in %s",		\
3706ecf6635Schristos 			    __FILE__,__LINE__,#cond,__func__);		\
3716ecf6635Schristos 			/* In case a user-supplied handler tries to */	\
3726ecf6635Schristos 			/* return control to us, log and abort here. */	\
3736ecf6635Schristos 			(void)fprintf(stderr,				\
3746ecf6635Schristos 			    "%s:%d: Assertion %s failed in %s",		\
3756ecf6635Schristos 			    __FILE__,__LINE__,#cond,__func__);		\
3766ecf6635Schristos 			abort();					\
3776ecf6635Schristos 		}							\
378a848e48cSrillig 	} while (0)
3796ecf6635Schristos #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
3806ecf6635Schristos #endif
3816ecf6635Schristos 
3820d738af4Schristos #ifndef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE
3836ecf6635Schristos /* Replacement for sockaddr storage that we can use internally on platforms
3846ecf6635Schristos  * that lack it.  It is not space-efficient, but neither is sockaddr_storage.
3856ecf6635Schristos  */
3866ecf6635Schristos struct sockaddr_storage {
3876ecf6635Schristos 	union {
3886ecf6635Schristos 		struct sockaddr ss_sa;
3896ecf6635Schristos 		struct sockaddr_in ss_sin;
3906ecf6635Schristos 		struct sockaddr_in6 ss_sin6;
3916ecf6635Schristos 		char ss_padding[128];
3926ecf6635Schristos 	} ss_union;
3936ecf6635Schristos };
3946ecf6635Schristos #define ss_family ss_union.ss_sa.sa_family
3956ecf6635Schristos #endif
3966ecf6635Schristos 
3976ecf6635Schristos /* Internal addrinfo error code.  This one is returned from only from
3980d738af4Schristos  * evutil_getaddrinfo_common_, when we are sure that we'll have to hit a DNS
3996ecf6635Schristos  * server. */
4006ecf6635Schristos #define EVUTIL_EAI_NEED_RESOLVE      -90002
4016ecf6635Schristos 
4026ecf6635Schristos struct evdns_base;
4036ecf6635Schristos struct evdns_getaddrinfo_request;
4046ecf6635Schristos typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
4056ecf6635Schristos     struct evdns_base *base,
4066ecf6635Schristos     const char *nodename, const char *servname,
4076ecf6635Schristos     const struct evutil_addrinfo *hints_in,
4086ecf6635Schristos     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
4097e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4100d738af4Schristos void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn);
4110d738af4Schristos typedef void (*evdns_getaddrinfo_cancel_fn)(
4120d738af4Schristos     struct evdns_getaddrinfo_request *req);
4137e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4140d738af4Schristos void evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn);
4156ecf6635Schristos 
4167e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4170d738af4Schristos struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa,
4186ecf6635Schristos     ev_socklen_t socklen, const struct evutil_addrinfo *hints);
4197e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4200d738af4Schristos struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first,
4216ecf6635Schristos     struct evutil_addrinfo *append);
4227e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4230d738af4Schristos void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints);
4247e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4250d738af4Schristos int evutil_getaddrinfo_common_(const char *nodename, const char *servname,
4266ecf6635Schristos     struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
4276ecf6635Schristos 
4280d738af4Schristos struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_(
4290d738af4Schristos     struct evdns_base *dns_base,
4306ecf6635Schristos     const char *nodename, const char *servname,
4316ecf6635Schristos     const struct evutil_addrinfo *hints_in,
4326ecf6635Schristos     void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
4330d738af4Schristos void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data);
4346ecf6635Schristos 
4356ecf6635Schristos /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
4366ecf6635Schristos  * ::1). */
4377e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4380d738af4Schristos int evutil_sockaddr_is_loopback_(const struct sockaddr *sa);
4396ecf6635Schristos 
4406ecf6635Schristos 
4416ecf6635Schristos /**
4426ecf6635Schristos     Formats a sockaddr sa into a string buffer of size outlen stored in out.
4436ecf6635Schristos     Returns a pointer to out.  Always writes something into out, so it's safe
4446ecf6635Schristos     to use the output of this function without checking it for NULL.
4456ecf6635Schristos  */
4467e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4470d738af4Schristos const char *evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen);
4486ecf6635Schristos 
4490d738af4Schristos int evutil_hex_char_to_int_(char c);
4506ecf6635Schristos 
4516ecf6635Schristos 
4520d738af4Schristos void evutil_free_secure_rng_globals_(void);
4530d738af4Schristos void evutil_free_globals_(void);
4540d738af4Schristos 
4550d738af4Schristos #ifdef _WIN32
4567e68cdd7Schristos EVENT2_EXPORT_SYMBOL
4570d738af4Schristos HMODULE evutil_load_windows_system_library_(const TCHAR *library_name);
4586ecf6635Schristos #endif
4596ecf6635Schristos 
4606ecf6635Schristos #ifndef EV_SIZE_FMT
4616ecf6635Schristos #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
4626ecf6635Schristos #define EV_U64_FMT "%I64u"
4636ecf6635Schristos #define EV_I64_FMT "%I64d"
4646ecf6635Schristos #define EV_I64_ARG(x) ((__int64)(x))
4656ecf6635Schristos #define EV_U64_ARG(x) ((unsigned __int64)(x))
4666ecf6635Schristos #else
4676ecf6635Schristos #define EV_U64_FMT "%llu"
4686ecf6635Schristos #define EV_I64_FMT "%lld"
4696ecf6635Schristos #define EV_I64_ARG(x) ((long long)(x))
4706ecf6635Schristos #define EV_U64_ARG(x) ((unsigned long long)(x))
4716ecf6635Schristos #endif
4726ecf6635Schristos #endif
4736ecf6635Schristos 
4746ecf6635Schristos #ifdef _WIN32
4756ecf6635Schristos #define EV_SOCK_FMT EV_I64_FMT
4766ecf6635Schristos #define EV_SOCK_ARG(x) EV_I64_ARG((x))
4776ecf6635Schristos #else
4786ecf6635Schristos #define EV_SOCK_FMT "%d"
4796ecf6635Schristos #define EV_SOCK_ARG(x) (x)
4806ecf6635Schristos #endif
4816ecf6635Schristos 
4820d738af4Schristos #if defined(__STDC__) && defined(__STDC_VERSION__) && !defined(__MINGW64_VERSION_MAJOR)
4836ecf6635Schristos #if (__STDC_VERSION__ >= 199901L)
4846ecf6635Schristos #define EV_SIZE_FMT "%zu"
4856ecf6635Schristos #define EV_SSIZE_FMT "%zd"
4866ecf6635Schristos #define EV_SIZE_ARG(x) (x)
4876ecf6635Schristos #define EV_SSIZE_ARG(x) (x)
4886ecf6635Schristos #endif
4896ecf6635Schristos #endif
4906ecf6635Schristos 
4916ecf6635Schristos #ifndef EV_SIZE_FMT
4920d738af4Schristos #if (EVENT__SIZEOF_SIZE_T <= EVENT__SIZEOF_LONG)
4936ecf6635Schristos #define EV_SIZE_FMT "%lu"
4946ecf6635Schristos #define EV_SSIZE_FMT "%ld"
4956ecf6635Schristos #define EV_SIZE_ARG(x) ((unsigned long)(x))
4966ecf6635Schristos #define EV_SSIZE_ARG(x) ((long)(x))
4976ecf6635Schristos #else
4986ecf6635Schristos #define EV_SIZE_FMT EV_U64_FMT
4996ecf6635Schristos #define EV_SSIZE_FMT EV_I64_FMT
5006ecf6635Schristos #define EV_SIZE_ARG(x) EV_U64_ARG(x)
5016ecf6635Schristos #define EV_SSIZE_ARG(x) EV_I64_ARG(x)
5026ecf6635Schristos #endif
5036ecf6635Schristos #endif
5046ecf6635Schristos 
5057e68cdd7Schristos EVENT2_EXPORT_SYMBOL
5060d738af4Schristos evutil_socket_t evutil_socket_(int domain, int type, int protocol);
5070d738af4Schristos evutil_socket_t evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr,
5080d738af4Schristos     ev_socklen_t *addrlen, int flags);
5090d738af4Schristos 
5100d738af4Schristos     /* used by one of the test programs.. */
5110d738af4Schristos EVENT2_EXPORT_SYMBOL
5120d738af4Schristos int evutil_make_internal_pipe_(evutil_socket_t fd[2]);
5130d738af4Schristos evutil_socket_t evutil_eventfd_(unsigned initval, int flags);
5140d738af4Schristos 
5150d738af4Schristos #ifdef SOCK_NONBLOCK
5160d738af4Schristos #define EVUTIL_SOCK_NONBLOCK SOCK_NONBLOCK
5170d738af4Schristos #else
5180d738af4Schristos #define EVUTIL_SOCK_NONBLOCK 0x4000000
5190d738af4Schristos #endif
5200d738af4Schristos #ifdef SOCK_CLOEXEC
5210d738af4Schristos #define EVUTIL_SOCK_CLOEXEC SOCK_CLOEXEC
5220d738af4Schristos #else
5230d738af4Schristos #define EVUTIL_SOCK_CLOEXEC 0x80000000
5240d738af4Schristos #endif
5250d738af4Schristos #ifdef EFD_NONBLOCK
5260d738af4Schristos #define EVUTIL_EFD_NONBLOCK EFD_NONBLOCK
5270d738af4Schristos #else
5280d738af4Schristos #define EVUTIL_EFD_NONBLOCK 0x4000
5290d738af4Schristos #endif
5300d738af4Schristos #ifdef EFD_CLOEXEC
5310d738af4Schristos #define EVUTIL_EFD_CLOEXEC EFD_CLOEXEC
5320d738af4Schristos #else
5330d738af4Schristos #define EVUTIL_EFD_CLOEXEC 0x8000
5340d738af4Schristos #endif
5350d738af4Schristos 
5366c6fe893Sspz void evutil_memclear_(void *mem, size_t len);
5376c6fe893Sspz 
5387e68cdd7Schristos struct in_addr;
5397e68cdd7Schristos struct in6_addr;
5407e68cdd7Schristos 
5417e68cdd7Schristos /* This is a any, loopback, link-local, multicast */
5427e68cdd7Schristos EVENT2_EXPORT_SYMBOL
5437e68cdd7Schristos int evutil_v4addr_is_local_(const struct in_addr *in);
5447e68cdd7Schristos /* This is a reserved, ipv4compat, ipv4map, loopback,
5457e68cdd7Schristos  * link-local, multicast, or unspecified address. */
5467e68cdd7Schristos EVENT2_EXPORT_SYMBOL
5477e68cdd7Schristos int evutil_v6addr_is_local_(const struct in6_addr *in);
5487e68cdd7Schristos 
5496ecf6635Schristos #ifdef __cplusplus
5506ecf6635Schristos }
5516ecf6635Schristos #endif
5526ecf6635Schristos 
5536ecf6635Schristos #endif
554