12b15cb3dSCy Schubert /* 22b15cb3dSCy Schubert * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 32b15cb3dSCy Schubert * 42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without 52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions 62b15cb3dSCy Schubert * are met: 72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright 82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer. 92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the 112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution. 122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products 132b15cb3dSCy Schubert * derived from this software without specific prior written permission. 142b15cb3dSCy Schubert * 152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 252b15cb3dSCy Schubert */ 262b15cb3dSCy Schubert #ifndef UTIL_INTERNAL_H_INCLUDED_ 272b15cb3dSCy Schubert #define UTIL_INTERNAL_H_INCLUDED_ 282b15cb3dSCy Schubert 292b15cb3dSCy Schubert #include "event2/event-config.h" 302b15cb3dSCy Schubert #include "evconfig-private.h" 312b15cb3dSCy Schubert 322b15cb3dSCy Schubert #include <errno.h> 332b15cb3dSCy Schubert 342b15cb3dSCy Schubert /* For EVUTIL_ASSERT */ 352b15cb3dSCy Schubert #include "log-internal.h" 362b15cb3dSCy Schubert #include <stdio.h> 372b15cb3dSCy Schubert #include <stdlib.h> 382b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_SOCKET_H 392b15cb3dSCy Schubert #include <sys/socket.h> 402b15cb3dSCy Schubert #endif 412b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_EVENTFD_H 422b15cb3dSCy Schubert #include <sys/eventfd.h> 432b15cb3dSCy Schubert #endif 442b15cb3dSCy Schubert #include "event2/util.h" 452b15cb3dSCy Schubert 462b15cb3dSCy Schubert #include "time-internal.h" 472b15cb3dSCy Schubert #include "ipv6-internal.h" 482b15cb3dSCy Schubert 492b15cb3dSCy Schubert #ifdef __cplusplus 502b15cb3dSCy Schubert extern "C" { 512b15cb3dSCy Schubert #endif 522b15cb3dSCy Schubert 53*a466cc55SCy Schubert /* __has_attribute() wrapper */ 54*a466cc55SCy Schubert #ifdef __has_attribute 55*a466cc55SCy Schubert # define EVUTIL_HAS_ATTRIBUTE __has_attribute 56*a466cc55SCy Schubert #endif 57*a466cc55SCy Schubert /** clang 3 __has_attribute misbehaves in some versions */ 58*a466cc55SCy Schubert #if defined(__clang__) && __clang__ == 1 59*a466cc55SCy Schubert # if defined(__apple_build_version__) 60*a466cc55SCy Schubert # if __clang_major__ <= 6 61*a466cc55SCy Schubert # undef EVUTIL_HAS_ATTRIBUTE 62*a466cc55SCy Schubert # endif 63*a466cc55SCy Schubert # else /* !__apple_build_version__ */ 64*a466cc55SCy Schubert # if __clang_major__ == 3 && __clang_minor__ >= 2 && __clang_minor__ <= 5 65*a466cc55SCy Schubert # undef EVUTIL_HAS_ATTRIBUTE 66*a466cc55SCy Schubert # endif 67*a466cc55SCy Schubert # endif /* __apple_build_version__ */ 68*a466cc55SCy Schubert #endif /*\ defined(__clang__) && __clang__ == 1 */ 69*a466cc55SCy Schubert #ifndef EVUTIL_HAS_ATTRIBUTE 70*a466cc55SCy Schubert # define EVUTIL_HAS_ATTRIBUTE(x) 0 71*a466cc55SCy Schubert #endif 72*a466cc55SCy Schubert 732b15cb3dSCy Schubert /* If we need magic to say "inline", get it for free internally. */ 742b15cb3dSCy Schubert #ifdef EVENT__inline 752b15cb3dSCy Schubert #define inline EVENT__inline 762b15cb3dSCy Schubert #endif 77*a466cc55SCy Schubert 78*a466cc55SCy Schubert /* Define to appropriate substitute if compiler doesnt have __func__ */ 79*a466cc55SCy Schubert #if defined(EVENT__HAVE___func__) 80*a466cc55SCy Schubert # ifndef __func__ 81*a466cc55SCy Schubert # define __func__ __func__ 82*a466cc55SCy Schubert # endif 83*a466cc55SCy Schubert #elif defined(EVENT__HAVE___FUNCTION__) 84*a466cc55SCy Schubert # define __func__ __FUNCTION__ 85*a466cc55SCy Schubert #else 86*a466cc55SCy Schubert # define __func__ __FILE__ 872b15cb3dSCy Schubert #endif 882b15cb3dSCy Schubert 892b15cb3dSCy Schubert /* A good no-op to use in macro definitions. */ 902b15cb3dSCy Schubert #define EVUTIL_NIL_STMT_ ((void)0) 912b15cb3dSCy Schubert /* A no-op that tricks the compiler into thinking a condition is used while 922b15cb3dSCy Schubert * definitely not making any code for it. Used to compile out asserts while 932b15cb3dSCy Schubert * avoiding "unused variable" warnings. The "!" forces the compiler to 942b15cb3dSCy Schubert * do the sizeof() on an int, in case "condition" is a bitfield value. 952b15cb3dSCy Schubert */ 962b15cb3dSCy Schubert #define EVUTIL_NIL_CONDITION_(condition) do { \ 972b15cb3dSCy Schubert (void)sizeof(!(condition)); \ 982b15cb3dSCy Schubert } while(0) 992b15cb3dSCy Schubert 1002b15cb3dSCy Schubert /* Internal use only: macros to match patterns of error codes in a 1012b15cb3dSCy Schubert cross-platform way. We need these macros because of two historical 1022b15cb3dSCy Schubert reasons: first, nonblocking IO functions are generally written to give an 1032b15cb3dSCy Schubert error on the "blocked now, try later" case, so sometimes an error from a 1042b15cb3dSCy Schubert read, write, connect, or accept means "no error; just wait for more 1052b15cb3dSCy Schubert data," and we need to look at the error code. Second, Windows defines 1062b15cb3dSCy Schubert a different set of error codes for sockets. */ 1072b15cb3dSCy Schubert 1082b15cb3dSCy Schubert #ifndef _WIN32 1092b15cb3dSCy Schubert 1102b15cb3dSCy Schubert #if EAGAIN == EWOULDBLOCK 1112b15cb3dSCy Schubert #define EVUTIL_ERR_IS_EAGAIN(e) \ 1122b15cb3dSCy Schubert ((e) == EAGAIN) 1132b15cb3dSCy Schubert #else 1142b15cb3dSCy Schubert #define EVUTIL_ERR_IS_EAGAIN(e) \ 1152b15cb3dSCy Schubert ((e) == EAGAIN || (e) == EWOULDBLOCK) 1162b15cb3dSCy Schubert #endif 1172b15cb3dSCy Schubert 1182b15cb3dSCy Schubert /* True iff e is an error that means a read/write operation can be retried. */ 1192b15cb3dSCy Schubert #define EVUTIL_ERR_RW_RETRIABLE(e) \ 1202b15cb3dSCy Schubert ((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e)) 1212b15cb3dSCy Schubert /* True iff e is an error that means an connect can be retried. */ 1222b15cb3dSCy Schubert #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ 1232b15cb3dSCy Schubert ((e) == EINTR || (e) == EINPROGRESS) 1242b15cb3dSCy Schubert /* True iff e is an error that means a accept can be retried. */ 1252b15cb3dSCy Schubert #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ 1262b15cb3dSCy Schubert ((e) == EINTR || EVUTIL_ERR_IS_EAGAIN(e) || (e) == ECONNABORTED) 1272b15cb3dSCy Schubert 1282b15cb3dSCy Schubert /* True iff e is an error that means the connection was refused */ 1292b15cb3dSCy Schubert #define EVUTIL_ERR_CONNECT_REFUSED(e) \ 1302b15cb3dSCy Schubert ((e) == ECONNREFUSED) 1312b15cb3dSCy Schubert 1322b15cb3dSCy Schubert #else 1332b15cb3dSCy Schubert /* Win32 */ 1342b15cb3dSCy Schubert 1352b15cb3dSCy Schubert #define EVUTIL_ERR_IS_EAGAIN(e) \ 1362b15cb3dSCy Schubert ((e) == WSAEWOULDBLOCK || (e) == EAGAIN) 1372b15cb3dSCy Schubert 1382b15cb3dSCy Schubert #define EVUTIL_ERR_RW_RETRIABLE(e) \ 1392b15cb3dSCy Schubert ((e) == WSAEWOULDBLOCK || \ 1402b15cb3dSCy Schubert (e) == WSAEINTR) 1412b15cb3dSCy Schubert 1422b15cb3dSCy Schubert #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ 1432b15cb3dSCy Schubert ((e) == WSAEWOULDBLOCK || \ 1442b15cb3dSCy Schubert (e) == WSAEINTR || \ 1452b15cb3dSCy Schubert (e) == WSAEINPROGRESS || \ 1462b15cb3dSCy Schubert (e) == WSAEINVAL) 1472b15cb3dSCy Schubert 1482b15cb3dSCy Schubert #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ 1492b15cb3dSCy Schubert EVUTIL_ERR_RW_RETRIABLE(e) 1502b15cb3dSCy Schubert 1512b15cb3dSCy Schubert #define EVUTIL_ERR_CONNECT_REFUSED(e) \ 1522b15cb3dSCy Schubert ((e) == WSAECONNREFUSED) 1532b15cb3dSCy Schubert 1542b15cb3dSCy Schubert #endif 1552b15cb3dSCy Schubert 1562b15cb3dSCy Schubert /* Arguments for shutdown() */ 1572b15cb3dSCy Schubert #ifdef SHUT_RD 1582b15cb3dSCy Schubert #define EVUTIL_SHUT_RD SHUT_RD 1592b15cb3dSCy Schubert #else 1602b15cb3dSCy Schubert #define EVUTIL_SHUT_RD 0 1612b15cb3dSCy Schubert #endif 1622b15cb3dSCy Schubert #ifdef SHUT_WR 1632b15cb3dSCy Schubert #define EVUTIL_SHUT_WR SHUT_WR 1642b15cb3dSCy Schubert #else 165*a466cc55SCy Schubert #define EVUTIL_SHUT_WR 1 /* SD_SEND */ 1662b15cb3dSCy Schubert #endif 1672b15cb3dSCy Schubert #ifdef SHUT_BOTH 1682b15cb3dSCy Schubert #define EVUTIL_SHUT_BOTH SHUT_BOTH 1692b15cb3dSCy Schubert #else 1702b15cb3dSCy Schubert #define EVUTIL_SHUT_BOTH 2 1712b15cb3dSCy Schubert #endif 1722b15cb3dSCy Schubert 1732b15cb3dSCy Schubert /* Helper: Verify that all the elements in 'dlist' are internally consistent. 1742b15cb3dSCy Schubert * Checks for circular lists and bad prev/next pointers. 1752b15cb3dSCy Schubert * 1762b15cb3dSCy Schubert * Example usage: 1772b15cb3dSCy Schubert * EVUTIL_ASSERT_LIST_OK(eventlist, event, ev_next); 1782b15cb3dSCy Schubert */ 1792b15cb3dSCy Schubert #define EVUTIL_ASSERT_LIST_OK(dlist, type, field) do { \ 1802b15cb3dSCy Schubert struct type *elm1, *elm2, **nextp; \ 1812b15cb3dSCy Schubert if (LIST_EMPTY((dlist))) \ 1822b15cb3dSCy Schubert break; \ 1832b15cb3dSCy Schubert \ 1842b15cb3dSCy Schubert /* Check list for circularity using Floyd's */ \ 1852b15cb3dSCy Schubert /* 'Tortoise and Hare' algorithm */ \ 1862b15cb3dSCy Schubert elm1 = LIST_FIRST((dlist)); \ 1872b15cb3dSCy Schubert elm2 = LIST_NEXT(elm1, field); \ 1882b15cb3dSCy Schubert while (elm1 && elm2) { \ 1892b15cb3dSCy Schubert EVUTIL_ASSERT(elm1 != elm2); \ 1902b15cb3dSCy Schubert elm1 = LIST_NEXT(elm1, field); \ 1912b15cb3dSCy Schubert elm2 = LIST_NEXT(elm2, field); \ 1922b15cb3dSCy Schubert if (!elm2) \ 1932b15cb3dSCy Schubert break; \ 1942b15cb3dSCy Schubert EVUTIL_ASSERT(elm1 != elm2); \ 1952b15cb3dSCy Schubert elm2 = LIST_NEXT(elm2, field); \ 1962b15cb3dSCy Schubert } \ 1972b15cb3dSCy Schubert \ 1982b15cb3dSCy Schubert /* Now check next and prev pointers for consistency. */ \ 1992b15cb3dSCy Schubert nextp = &LIST_FIRST((dlist)); \ 2002b15cb3dSCy Schubert elm1 = LIST_FIRST((dlist)); \ 2012b15cb3dSCy Schubert while (elm1) { \ 2022b15cb3dSCy Schubert EVUTIL_ASSERT(*nextp == elm1); \ 2032b15cb3dSCy Schubert EVUTIL_ASSERT(nextp == elm1->field.le_prev); \ 2042b15cb3dSCy Schubert nextp = &LIST_NEXT(elm1, field); \ 2052b15cb3dSCy Schubert elm1 = *nextp; \ 2062b15cb3dSCy Schubert } \ 2072b15cb3dSCy Schubert } while (0) 2082b15cb3dSCy Schubert 2092b15cb3dSCy Schubert /* Helper: Verify that all the elements in a TAILQ are internally consistent. 2102b15cb3dSCy Schubert * Checks for circular lists and bad prev/next pointers. 2112b15cb3dSCy Schubert * 2122b15cb3dSCy Schubert * Example usage: 2132b15cb3dSCy Schubert * EVUTIL_ASSERT_TAILQ_OK(activelist, event, ev_active_next); 2142b15cb3dSCy Schubert */ 2152b15cb3dSCy Schubert #define EVUTIL_ASSERT_TAILQ_OK(tailq, type, field) do { \ 2162b15cb3dSCy Schubert struct type *elm1, *elm2, **nextp; \ 2172b15cb3dSCy Schubert if (TAILQ_EMPTY((tailq))) \ 2182b15cb3dSCy Schubert break; \ 2192b15cb3dSCy Schubert \ 2202b15cb3dSCy Schubert /* Check list for circularity using Floyd's */ \ 2212b15cb3dSCy Schubert /* 'Tortoise and Hare' algorithm */ \ 2222b15cb3dSCy Schubert elm1 = TAILQ_FIRST((tailq)); \ 2232b15cb3dSCy Schubert elm2 = TAILQ_NEXT(elm1, field); \ 2242b15cb3dSCy Schubert while (elm1 && elm2) { \ 2252b15cb3dSCy Schubert EVUTIL_ASSERT(elm1 != elm2); \ 2262b15cb3dSCy Schubert elm1 = TAILQ_NEXT(elm1, field); \ 2272b15cb3dSCy Schubert elm2 = TAILQ_NEXT(elm2, field); \ 2282b15cb3dSCy Schubert if (!elm2) \ 2292b15cb3dSCy Schubert break; \ 2302b15cb3dSCy Schubert EVUTIL_ASSERT(elm1 != elm2); \ 2312b15cb3dSCy Schubert elm2 = TAILQ_NEXT(elm2, field); \ 2322b15cb3dSCy Schubert } \ 2332b15cb3dSCy Schubert \ 2342b15cb3dSCy Schubert /* Now check next and prev pointers for consistency. */ \ 2352b15cb3dSCy Schubert nextp = &TAILQ_FIRST((tailq)); \ 2362b15cb3dSCy Schubert elm1 = TAILQ_FIRST((tailq)); \ 2372b15cb3dSCy Schubert while (elm1) { \ 2382b15cb3dSCy Schubert EVUTIL_ASSERT(*nextp == elm1); \ 2392b15cb3dSCy Schubert EVUTIL_ASSERT(nextp == elm1->field.tqe_prev); \ 2402b15cb3dSCy Schubert nextp = &TAILQ_NEXT(elm1, field); \ 2412b15cb3dSCy Schubert elm1 = *nextp; \ 2422b15cb3dSCy Schubert } \ 2432b15cb3dSCy Schubert EVUTIL_ASSERT(nextp == (tailq)->tqh_last); \ 2442b15cb3dSCy Schubert } while (0) 2452b15cb3dSCy Schubert 2462b15cb3dSCy Schubert /* Locale-independent replacements for some ctypes functions. Use these 2472b15cb3dSCy Schubert * when you care about ASCII's notion of character types, because you are about 2482b15cb3dSCy Schubert * to send those types onto the wire. 2492b15cb3dSCy Schubert */ 250*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2512b15cb3dSCy Schubert int EVUTIL_ISALPHA_(char c); 252*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2532b15cb3dSCy Schubert int EVUTIL_ISALNUM_(char c); 2542b15cb3dSCy Schubert int EVUTIL_ISSPACE_(char c); 255*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2562b15cb3dSCy Schubert int EVUTIL_ISDIGIT_(char c); 257*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2582b15cb3dSCy Schubert int EVUTIL_ISXDIGIT_(char c); 2592b15cb3dSCy Schubert int EVUTIL_ISPRINT_(char c); 2602b15cb3dSCy Schubert int EVUTIL_ISLOWER_(char c); 2612b15cb3dSCy Schubert int EVUTIL_ISUPPER_(char c); 262*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2632b15cb3dSCy Schubert char EVUTIL_TOUPPER_(char c); 264*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2652b15cb3dSCy Schubert char EVUTIL_TOLOWER_(char c); 2662b15cb3dSCy Schubert 2672b15cb3dSCy Schubert /** Remove all trailing horizontal whitespace (space or tab) from the end of a 2682b15cb3dSCy Schubert * string */ 269*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2702b15cb3dSCy Schubert void evutil_rtrim_lws_(char *); 2712b15cb3dSCy Schubert 2722b15cb3dSCy Schubert 2732b15cb3dSCy Schubert /** Helper macro. If we know that a given pointer points to a field in a 2742b15cb3dSCy Schubert structure, return a pointer to the structure itself. Used to implement 2752b15cb3dSCy Schubert our half-baked C OO. Example: 2762b15cb3dSCy Schubert 2772b15cb3dSCy Schubert struct subtype { 2782b15cb3dSCy Schubert int x; 2792b15cb3dSCy Schubert struct supertype common; 2802b15cb3dSCy Schubert int y; 2812b15cb3dSCy Schubert }; 2822b15cb3dSCy Schubert ... 2832b15cb3dSCy Schubert void fn(struct supertype *super) { 2842b15cb3dSCy Schubert struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common); 2852b15cb3dSCy Schubert ... 2862b15cb3dSCy Schubert } 2872b15cb3dSCy Schubert */ 2882b15cb3dSCy Schubert #define EVUTIL_UPCAST(ptr, type, field) \ 2892b15cb3dSCy Schubert ((type *)(((char*)(ptr)) - evutil_offsetof(type, field))) 2902b15cb3dSCy Schubert 2912b15cb3dSCy Schubert /* As open(pathname, flags, mode), except that the file is always opened with 2922b15cb3dSCy Schubert * the close-on-exec flag set. (And the mode argument is mandatory.) 2932b15cb3dSCy Schubert */ 2942b15cb3dSCy Schubert int evutil_open_closeonexec_(const char *pathname, int flags, unsigned mode); 2952b15cb3dSCy Schubert 296*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 2972b15cb3dSCy Schubert int evutil_read_file_(const char *filename, char **content_out, size_t *len_out, 2982b15cb3dSCy Schubert int is_binary); 2992b15cb3dSCy Schubert 300*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 301*a466cc55SCy Schubert int evutil_socket_connect_(evutil_socket_t *fd_ptr, const struct sockaddr *sa, int socklen); 3022b15cb3dSCy Schubert 3032b15cb3dSCy Schubert int evutil_socket_finished_connecting_(evutil_socket_t fd); 3042b15cb3dSCy Schubert 305*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 3062b15cb3dSCy Schubert int evutil_ersatz_socketpair_(int, int , int, evutil_socket_t[]); 3072b15cb3dSCy Schubert 3082b15cb3dSCy Schubert int evutil_resolve_(int family, const char *hostname, struct sockaddr *sa, 3092b15cb3dSCy Schubert ev_socklen_t *socklen, int port); 3102b15cb3dSCy Schubert 3112b15cb3dSCy Schubert const char *evutil_getenv_(const char *name); 3122b15cb3dSCy Schubert 3132b15cb3dSCy Schubert /* Structure to hold the state of our weak random number generator. 3142b15cb3dSCy Schubert */ 3152b15cb3dSCy Schubert struct evutil_weakrand_state { 3162b15cb3dSCy Schubert ev_uint32_t seed; 3172b15cb3dSCy Schubert }; 3182b15cb3dSCy Schubert 3192b15cb3dSCy Schubert #define EVUTIL_WEAKRAND_MAX EV_INT32_MAX 3202b15cb3dSCy Schubert 3212b15cb3dSCy Schubert /* Initialize the state of a week random number generator based on 'seed'. If 3222b15cb3dSCy Schubert * the seed is 0, construct a new seed based on not-very-strong platform 3232b15cb3dSCy Schubert * entropy, like the PID and the time of day. 3242b15cb3dSCy Schubert * 3252b15cb3dSCy Schubert * This function, and the other evutil_weakrand* functions, are meant for 3262b15cb3dSCy Schubert * speed, not security or statistical strength. If you need a RNG which an 3272b15cb3dSCy Schubert * attacker can't predict, or which passes strong statistical tests, use the 3282b15cb3dSCy Schubert * evutil_secure_rng* functions instead. 3292b15cb3dSCy Schubert */ 330*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 3312b15cb3dSCy Schubert ev_uint32_t evutil_weakrand_seed_(struct evutil_weakrand_state *state, ev_uint32_t seed); 3322b15cb3dSCy Schubert /* Return a pseudorandom value between 0 and EVUTIL_WEAKRAND_MAX inclusive. 3332b15cb3dSCy Schubert * Updates the state in 'seed' as needed -- this value must be protected by a 3342b15cb3dSCy Schubert * lock. 3352b15cb3dSCy Schubert */ 336*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 3372b15cb3dSCy Schubert ev_int32_t evutil_weakrand_(struct evutil_weakrand_state *seed); 3382b15cb3dSCy Schubert /* Return a pseudorandom value x such that 0 <= x < top. top must be no more 3392b15cb3dSCy Schubert * than EVUTIL_WEAKRAND_MAX. Updates the state in 'seed' as needed -- this 3402b15cb3dSCy Schubert * value must be proteced by a lock */ 341*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 3422b15cb3dSCy Schubert ev_int32_t evutil_weakrand_range_(struct evutil_weakrand_state *seed, ev_int32_t top); 3432b15cb3dSCy Schubert 3442b15cb3dSCy Schubert /* Evaluates to the same boolean value as 'p', and hints to the compiler that 3452b15cb3dSCy Schubert * we expect this value to be false. */ 3462b15cb3dSCy Schubert #if defined(__GNUC__) && __GNUC__ >= 3 /* gcc 3.0 or later */ 3472b15cb3dSCy Schubert #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0) 3482b15cb3dSCy Schubert #else 3492b15cb3dSCy Schubert #define EVUTIL_UNLIKELY(p) (p) 3502b15cb3dSCy Schubert #endif 3512b15cb3dSCy Schubert 352*a466cc55SCy Schubert #if EVUTIL_HAS_ATTRIBUTE(fallthrough) 353*a466cc55SCy Schubert #define EVUTIL_FALLTHROUGH __attribute__((fallthrough)) 354*a466cc55SCy Schubert #else 355*a466cc55SCy Schubert #define EVUTIL_FALLTHROUGH /* fallthrough */ 356*a466cc55SCy Schubert #endif 357*a466cc55SCy Schubert 3582b15cb3dSCy Schubert /* Replacement for assert() that calls event_errx on failure. */ 3592b15cb3dSCy Schubert #ifdef NDEBUG 3602b15cb3dSCy Schubert #define EVUTIL_ASSERT(cond) EVUTIL_NIL_CONDITION_(cond) 3612b15cb3dSCy Schubert #define EVUTIL_FAILURE_CHECK(cond) 0 3622b15cb3dSCy Schubert #else 3632b15cb3dSCy Schubert #define EVUTIL_ASSERT(cond) \ 3642b15cb3dSCy Schubert do { \ 3652b15cb3dSCy Schubert if (EVUTIL_UNLIKELY(!(cond))) { \ 3662b15cb3dSCy Schubert event_errx(EVENT_ERR_ABORT_, \ 3672b15cb3dSCy Schubert "%s:%d: Assertion %s failed in %s", \ 3682b15cb3dSCy Schubert __FILE__,__LINE__,#cond,__func__); \ 3692b15cb3dSCy Schubert /* In case a user-supplied handler tries to */ \ 3702b15cb3dSCy Schubert /* return control to us, log and abort here. */ \ 3712b15cb3dSCy Schubert (void)fprintf(stderr, \ 3722b15cb3dSCy Schubert "%s:%d: Assertion %s failed in %s", \ 3732b15cb3dSCy Schubert __FILE__,__LINE__,#cond,__func__); \ 3742b15cb3dSCy Schubert abort(); \ 3752b15cb3dSCy Schubert } \ 3762b15cb3dSCy Schubert } while (0) 3772b15cb3dSCy Schubert #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond) 3782b15cb3dSCy Schubert #endif 3792b15cb3dSCy Schubert 3802b15cb3dSCy Schubert #ifndef EVENT__HAVE_STRUCT_SOCKADDR_STORAGE 3812b15cb3dSCy Schubert /* Replacement for sockaddr storage that we can use internally on platforms 3822b15cb3dSCy Schubert * that lack it. It is not space-efficient, but neither is sockaddr_storage. 3832b15cb3dSCy Schubert */ 3842b15cb3dSCy Schubert struct sockaddr_storage { 3852b15cb3dSCy Schubert union { 3862b15cb3dSCy Schubert struct sockaddr ss_sa; 3872b15cb3dSCy Schubert struct sockaddr_in ss_sin; 3882b15cb3dSCy Schubert struct sockaddr_in6 ss_sin6; 3892b15cb3dSCy Schubert char ss_padding[128]; 3902b15cb3dSCy Schubert } ss_union; 3912b15cb3dSCy Schubert }; 3922b15cb3dSCy Schubert #define ss_family ss_union.ss_sa.sa_family 3932b15cb3dSCy Schubert #endif 3942b15cb3dSCy Schubert 3952b15cb3dSCy Schubert /* Internal addrinfo error code. This one is returned from only from 3962b15cb3dSCy Schubert * evutil_getaddrinfo_common_, when we are sure that we'll have to hit a DNS 3972b15cb3dSCy Schubert * server. */ 3982b15cb3dSCy Schubert #define EVUTIL_EAI_NEED_RESOLVE -90002 3992b15cb3dSCy Schubert 4002b15cb3dSCy Schubert struct evdns_base; 4012b15cb3dSCy Schubert struct evdns_getaddrinfo_request; 4022b15cb3dSCy Schubert typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)( 4032b15cb3dSCy Schubert struct evdns_base *base, 4042b15cb3dSCy Schubert const char *nodename, const char *servname, 4052b15cb3dSCy Schubert const struct evutil_addrinfo *hints_in, 4062b15cb3dSCy Schubert void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); 407*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4082b15cb3dSCy Schubert void evutil_set_evdns_getaddrinfo_fn_(evdns_getaddrinfo_fn fn); 409*a466cc55SCy Schubert typedef void (*evdns_getaddrinfo_cancel_fn)( 410*a466cc55SCy Schubert struct evdns_getaddrinfo_request *req); 411*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 412*a466cc55SCy Schubert void evutil_set_evdns_getaddrinfo_cancel_fn_(evdns_getaddrinfo_cancel_fn fn); 4132b15cb3dSCy Schubert 414*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4152b15cb3dSCy Schubert struct evutil_addrinfo *evutil_new_addrinfo_(struct sockaddr *sa, 4162b15cb3dSCy Schubert ev_socklen_t socklen, const struct evutil_addrinfo *hints); 417*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4182b15cb3dSCy Schubert struct evutil_addrinfo *evutil_addrinfo_append_(struct evutil_addrinfo *first, 4192b15cb3dSCy Schubert struct evutil_addrinfo *append); 420*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4212b15cb3dSCy Schubert void evutil_adjust_hints_for_addrconfig_(struct evutil_addrinfo *hints); 422*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4232b15cb3dSCy Schubert int evutil_getaddrinfo_common_(const char *nodename, const char *servname, 4242b15cb3dSCy Schubert struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum); 4252b15cb3dSCy Schubert 426*a466cc55SCy Schubert struct evdns_getaddrinfo_request *evutil_getaddrinfo_async_( 427*a466cc55SCy Schubert struct evdns_base *dns_base, 4282b15cb3dSCy Schubert const char *nodename, const char *servname, 4292b15cb3dSCy Schubert const struct evutil_addrinfo *hints_in, 4302b15cb3dSCy Schubert void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); 431*a466cc55SCy Schubert void evutil_getaddrinfo_cancel_async_(struct evdns_getaddrinfo_request *data); 4322b15cb3dSCy Schubert 4332b15cb3dSCy Schubert /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or 4342b15cb3dSCy Schubert * ::1). */ 435*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4362b15cb3dSCy Schubert int evutil_sockaddr_is_loopback_(const struct sockaddr *sa); 4372b15cb3dSCy Schubert 4382b15cb3dSCy Schubert 4392b15cb3dSCy Schubert /** 4402b15cb3dSCy Schubert Formats a sockaddr sa into a string buffer of size outlen stored in out. 4412b15cb3dSCy Schubert Returns a pointer to out. Always writes something into out, so it's safe 4422b15cb3dSCy Schubert to use the output of this function without checking it for NULL. 4432b15cb3dSCy Schubert */ 444*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 4452b15cb3dSCy Schubert const char *evutil_format_sockaddr_port_(const struct sockaddr *sa, char *out, size_t outlen); 4462b15cb3dSCy Schubert 4472b15cb3dSCy Schubert int evutil_hex_char_to_int_(char c); 4482b15cb3dSCy Schubert 4492b15cb3dSCy Schubert 4502b15cb3dSCy Schubert void evutil_free_secure_rng_globals_(void); 4512b15cb3dSCy Schubert void evutil_free_globals_(void); 4522b15cb3dSCy Schubert 4532b15cb3dSCy Schubert #ifdef _WIN32 454*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 455a25439b6SCy Schubert HMODULE evutil_load_windows_system_library_(const TCHAR *library_name); 4562b15cb3dSCy Schubert #endif 4572b15cb3dSCy Schubert 4582b15cb3dSCy Schubert #ifndef EV_SIZE_FMT 4592b15cb3dSCy Schubert #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) 4602b15cb3dSCy Schubert #define EV_U64_FMT "%I64u" 4612b15cb3dSCy Schubert #define EV_I64_FMT "%I64d" 4622b15cb3dSCy Schubert #define EV_I64_ARG(x) ((__int64)(x)) 4632b15cb3dSCy Schubert #define EV_U64_ARG(x) ((unsigned __int64)(x)) 4642b15cb3dSCy Schubert #else 4652b15cb3dSCy Schubert #define EV_U64_FMT "%llu" 4662b15cb3dSCy Schubert #define EV_I64_FMT "%lld" 4672b15cb3dSCy Schubert #define EV_I64_ARG(x) ((long long)(x)) 4682b15cb3dSCy Schubert #define EV_U64_ARG(x) ((unsigned long long)(x)) 4692b15cb3dSCy Schubert #endif 4702b15cb3dSCy Schubert #endif 4712b15cb3dSCy Schubert 4722b15cb3dSCy Schubert #ifdef _WIN32 4732b15cb3dSCy Schubert #define EV_SOCK_FMT EV_I64_FMT 4742b15cb3dSCy Schubert #define EV_SOCK_ARG(x) EV_I64_ARG((x)) 4752b15cb3dSCy Schubert #else 4762b15cb3dSCy Schubert #define EV_SOCK_FMT "%d" 4772b15cb3dSCy Schubert #define EV_SOCK_ARG(x) (x) 4782b15cb3dSCy Schubert #endif 4792b15cb3dSCy Schubert 480*a466cc55SCy Schubert #if defined(__STDC__) && defined(__STDC_VERSION__) && !defined(__MINGW64_VERSION_MAJOR) 4812b15cb3dSCy Schubert #if (__STDC_VERSION__ >= 199901L) 4822b15cb3dSCy Schubert #define EV_SIZE_FMT "%zu" 4832b15cb3dSCy Schubert #define EV_SSIZE_FMT "%zd" 4842b15cb3dSCy Schubert #define EV_SIZE_ARG(x) (x) 4852b15cb3dSCy Schubert #define EV_SSIZE_ARG(x) (x) 4862b15cb3dSCy Schubert #endif 4872b15cb3dSCy Schubert #endif 4882b15cb3dSCy Schubert 4892b15cb3dSCy Schubert #ifndef EV_SIZE_FMT 4902b15cb3dSCy Schubert #if (EVENT__SIZEOF_SIZE_T <= EVENT__SIZEOF_LONG) 4912b15cb3dSCy Schubert #define EV_SIZE_FMT "%lu" 4922b15cb3dSCy Schubert #define EV_SSIZE_FMT "%ld" 4932b15cb3dSCy Schubert #define EV_SIZE_ARG(x) ((unsigned long)(x)) 4942b15cb3dSCy Schubert #define EV_SSIZE_ARG(x) ((long)(x)) 4952b15cb3dSCy Schubert #else 4962b15cb3dSCy Schubert #define EV_SIZE_FMT EV_U64_FMT 4972b15cb3dSCy Schubert #define EV_SSIZE_FMT EV_I64_FMT 4982b15cb3dSCy Schubert #define EV_SIZE_ARG(x) EV_U64_ARG(x) 4992b15cb3dSCy Schubert #define EV_SSIZE_ARG(x) EV_I64_ARG(x) 5002b15cb3dSCy Schubert #endif 5012b15cb3dSCy Schubert #endif 5022b15cb3dSCy Schubert 503*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 5042b15cb3dSCy Schubert evutil_socket_t evutil_socket_(int domain, int type, int protocol); 5052b15cb3dSCy Schubert evutil_socket_t evutil_accept4_(evutil_socket_t sockfd, struct sockaddr *addr, 5062b15cb3dSCy Schubert ev_socklen_t *addrlen, int flags); 5072b15cb3dSCy Schubert 5082b15cb3dSCy Schubert /* used by one of the test programs.. */ 5092b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL 5102b15cb3dSCy Schubert int evutil_make_internal_pipe_(evutil_socket_t fd[2]); 5112b15cb3dSCy Schubert evutil_socket_t evutil_eventfd_(unsigned initval, int flags); 5122b15cb3dSCy Schubert 5132b15cb3dSCy Schubert #ifdef SOCK_NONBLOCK 5142b15cb3dSCy Schubert #define EVUTIL_SOCK_NONBLOCK SOCK_NONBLOCK 5152b15cb3dSCy Schubert #else 5162b15cb3dSCy Schubert #define EVUTIL_SOCK_NONBLOCK 0x4000000 5172b15cb3dSCy Schubert #endif 5182b15cb3dSCy Schubert #ifdef SOCK_CLOEXEC 5192b15cb3dSCy Schubert #define EVUTIL_SOCK_CLOEXEC SOCK_CLOEXEC 5202b15cb3dSCy Schubert #else 5212b15cb3dSCy Schubert #define EVUTIL_SOCK_CLOEXEC 0x80000000 5222b15cb3dSCy Schubert #endif 5232b15cb3dSCy Schubert #ifdef EFD_NONBLOCK 5242b15cb3dSCy Schubert #define EVUTIL_EFD_NONBLOCK EFD_NONBLOCK 5252b15cb3dSCy Schubert #else 5262b15cb3dSCy Schubert #define EVUTIL_EFD_NONBLOCK 0x4000 5272b15cb3dSCy Schubert #endif 5282b15cb3dSCy Schubert #ifdef EFD_CLOEXEC 5292b15cb3dSCy Schubert #define EVUTIL_EFD_CLOEXEC EFD_CLOEXEC 5302b15cb3dSCy Schubert #else 5312b15cb3dSCy Schubert #define EVUTIL_EFD_CLOEXEC 0x8000 5322b15cb3dSCy Schubert #endif 5332b15cb3dSCy Schubert 5342b15cb3dSCy Schubert void evutil_memclear_(void *mem, size_t len); 5352b15cb3dSCy Schubert 536*a466cc55SCy Schubert struct in_addr; 537*a466cc55SCy Schubert struct in6_addr; 538*a466cc55SCy Schubert 539*a466cc55SCy Schubert /* This is a any, loopback, link-local, multicast */ 540*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 541*a466cc55SCy Schubert int evutil_v4addr_is_local_(const struct in_addr *in); 542*a466cc55SCy Schubert /* This is a reserved, ipv4compat, ipv4map, loopback, 543*a466cc55SCy Schubert * link-local, multicast, or unspecified address. */ 544*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL 545*a466cc55SCy Schubert int evutil_v6addr_is_local_(const struct in6_addr *in); 546*a466cc55SCy Schubert 5472b15cb3dSCy Schubert #ifdef __cplusplus 5482b15cb3dSCy Schubert } 5492b15cb3dSCy Schubert #endif 5502b15cb3dSCy Schubert 5512b15cb3dSCy Schubert #endif 552