michael@0: /* michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _EVENT_UTIL_INTERNAL_H michael@0: #define _EVENT_UTIL_INTERNAL_H michael@0: michael@0: #include "event2/event-config.h" michael@0: #include michael@0: michael@0: /* For EVUTIL_ASSERT */ michael@0: #include "log-internal.h" michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_SOCKET_H michael@0: #include michael@0: #endif michael@0: #include "event2/util.h" michael@0: michael@0: #include "ipv6-internal.h" michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: /* If we need magic to say "inline", get it for free internally. */ michael@0: #ifdef _EVENT_inline michael@0: #define inline _EVENT_inline michael@0: #endif michael@0: #ifdef _EVENT___func__ michael@0: #define __func__ _EVENT___func__ michael@0: #endif michael@0: michael@0: /* A good no-op to use in macro definitions. */ michael@0: #define _EVUTIL_NIL_STMT ((void)0) michael@0: /* A no-op that tricks the compiler into thinking a condition is used while michael@0: * definitely not making any code for it. Used to compile out asserts while michael@0: * avoiding "unused variable" warnings. The "!" forces the compiler to michael@0: * do the sizeof() on an int, in case "condition" is a bitfield value. michael@0: */ michael@0: #define _EVUTIL_NIL_CONDITION(condition) do { \ michael@0: (void)sizeof(!(condition)); \ michael@0: } while(0) michael@0: michael@0: /* Internal use only: macros to match patterns of error codes in a michael@0: cross-platform way. We need these macros because of two historical michael@0: reasons: first, nonblocking IO functions are generally written to give an michael@0: error on the "blocked now, try later" case, so sometimes an error from a michael@0: read, write, connect, or accept means "no error; just wait for more michael@0: data," and we need to look at the error code. Second, Windows defines michael@0: a different set of error codes for sockets. */ michael@0: michael@0: #ifndef WIN32 michael@0: michael@0: /* True iff e is an error that means a read/write operation can be retried. */ michael@0: #define EVUTIL_ERR_RW_RETRIABLE(e) \ michael@0: ((e) == EINTR || (e) == EAGAIN) michael@0: /* True iff e is an error that means an connect can be retried. */ michael@0: #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ michael@0: ((e) == EINTR || (e) == EINPROGRESS) michael@0: /* True iff e is an error that means a accept can be retried. */ michael@0: #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ michael@0: ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED) michael@0: michael@0: /* True iff e is an error that means the connection was refused */ michael@0: #define EVUTIL_ERR_CONNECT_REFUSED(e) \ michael@0: ((e) == ECONNREFUSED) michael@0: michael@0: #else michael@0: michael@0: #define EVUTIL_ERR_RW_RETRIABLE(e) \ michael@0: ((e) == WSAEWOULDBLOCK || \ michael@0: (e) == WSAEINTR) michael@0: michael@0: #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ michael@0: ((e) == WSAEWOULDBLOCK || \ michael@0: (e) == WSAEINTR || \ michael@0: (e) == WSAEINPROGRESS || \ michael@0: (e) == WSAEINVAL) michael@0: michael@0: #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ michael@0: EVUTIL_ERR_RW_RETRIABLE(e) michael@0: michael@0: #define EVUTIL_ERR_CONNECT_REFUSED(e) \ michael@0: ((e) == WSAECONNREFUSED) michael@0: michael@0: #endif michael@0: michael@0: #ifdef _EVENT_socklen_t michael@0: #define socklen_t _EVENT_socklen_t michael@0: #endif michael@0: michael@0: /* Arguments for shutdown() */ michael@0: #ifdef SHUT_RD michael@0: #define EVUTIL_SHUT_RD SHUT_RD michael@0: #else michael@0: #define EVUTIL_SHUT_RD 0 michael@0: #endif michael@0: #ifdef SHUT_WR michael@0: #define EVUTIL_SHUT_WR SHUT_WR michael@0: #else michael@0: #define EVUTIL_SHUT_WR 1 michael@0: #endif michael@0: #ifdef SHUT_BOTH michael@0: #define EVUTIL_SHUT_BOTH SHUT_BOTH michael@0: #else michael@0: #define EVUTIL_SHUT_BOTH 2 michael@0: #endif michael@0: michael@0: /* Locale-independent replacements for some ctypes functions. Use these michael@0: * when you care about ASCII's notion of character types, because you are about michael@0: * to send those types onto the wire. michael@0: */ michael@0: int EVUTIL_ISALPHA(char c); michael@0: int EVUTIL_ISALNUM(char c); michael@0: int EVUTIL_ISSPACE(char c); michael@0: int EVUTIL_ISDIGIT(char c); michael@0: int EVUTIL_ISXDIGIT(char c); michael@0: int EVUTIL_ISPRINT(char c); michael@0: int EVUTIL_ISLOWER(char c); michael@0: int EVUTIL_ISUPPER(char c); michael@0: char EVUTIL_TOUPPER(char c); michael@0: char EVUTIL_TOLOWER(char c); michael@0: michael@0: /** Helper macro. If we know that a given pointer points to a field in a michael@0: structure, return a pointer to the structure itself. Used to implement michael@0: our half-baked C OO. Example: michael@0: michael@0: struct subtype { michael@0: int x; michael@0: struct supertype common; michael@0: int y; michael@0: }; michael@0: ... michael@0: void fn(struct supertype *super) { michael@0: struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common); michael@0: ... michael@0: } michael@0: */ michael@0: #define EVUTIL_UPCAST(ptr, type, field) \ michael@0: ((type *)(((char*)(ptr)) - evutil_offsetof(type, field))) michael@0: michael@0: /* As open(pathname, flags, mode), except that the file is always opened with michael@0: * the close-on-exec flag set. (And the mode argument is mandatory.) michael@0: */ michael@0: int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode); michael@0: michael@0: int evutil_read_file(const char *filename, char **content_out, size_t *len_out, michael@0: int is_binary); michael@0: michael@0: int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen); michael@0: michael@0: int evutil_socket_finished_connecting(evutil_socket_t fd); michael@0: michael@0: int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]); michael@0: michael@0: int evutil_resolve(int family, const char *hostname, struct sockaddr *sa, michael@0: ev_socklen_t *socklen, int port); michael@0: michael@0: const char *evutil_getenv(const char *name); michael@0: michael@0: long _evutil_weakrand(void); michael@0: michael@0: /* Evaluates to the same boolean value as 'p', and hints to the compiler that michael@0: * we expect this value to be false. */ michael@0: #if defined(__GNUC__) && __GNUC__ >= 3 /* gcc 3.0 or later */ michael@0: #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0) michael@0: #else michael@0: #define EVUTIL_UNLIKELY(p) (p) michael@0: #endif michael@0: michael@0: /* Replacement for assert() that calls event_errx on failure. */ michael@0: #ifdef NDEBUG michael@0: #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond) michael@0: #define EVUTIL_FAILURE_CHECK(cond) 0 michael@0: #else michael@0: #define EVUTIL_ASSERT(cond) \ michael@0: do { \ michael@0: if (EVUTIL_UNLIKELY(!(cond))) { \ michael@0: event_errx(_EVENT_ERR_ABORT, \ michael@0: "%s:%d: Assertion %s failed in %s", \ michael@0: __FILE__,__LINE__,#cond,__func__); \ michael@0: /* In case a user-supplied handler tries to */ \ michael@0: /* return control to us, log and abort here. */ \ michael@0: (void)fprintf(stderr, \ michael@0: "%s:%d: Assertion %s failed in %s", \ michael@0: __FILE__,__LINE__,#cond,__func__); \ michael@0: abort(); \ michael@0: } \ michael@0: } while (0) michael@0: #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond) michael@0: #endif michael@0: michael@0: #ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE michael@0: /* Replacement for sockaddr storage that we can use internally on platforms michael@0: * that lack it. It is not space-efficient, but neither is sockaddr_storage. michael@0: */ michael@0: struct sockaddr_storage { michael@0: union { michael@0: struct sockaddr ss_sa; michael@0: struct sockaddr_in ss_sin; michael@0: struct sockaddr_in6 ss_sin6; michael@0: char ss_padding[128]; michael@0: } ss_union; michael@0: }; michael@0: #define ss_family ss_union.ss_sa.sa_family michael@0: #endif michael@0: michael@0: /* Internal addrinfo error code. This one is returned from only from michael@0: * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS michael@0: * server. */ michael@0: #define EVUTIL_EAI_NEED_RESOLVE -90002 michael@0: michael@0: struct evdns_base; michael@0: struct evdns_getaddrinfo_request; michael@0: typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)( michael@0: struct evdns_base *base, michael@0: const char *nodename, const char *servname, michael@0: const struct evutil_addrinfo *hints_in, michael@0: void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); michael@0: michael@0: void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn); michael@0: michael@0: struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa, michael@0: ev_socklen_t socklen, const struct evutil_addrinfo *hints); michael@0: struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first, michael@0: struct evutil_addrinfo *append); michael@0: void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints); michael@0: int evutil_getaddrinfo_common(const char *nodename, const char *servname, michael@0: struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum); michael@0: michael@0: int evutil_getaddrinfo_async(struct evdns_base *dns_base, michael@0: const char *nodename, const char *servname, michael@0: const struct evutil_addrinfo *hints_in, michael@0: void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); michael@0: michael@0: /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or michael@0: * ::1). */ michael@0: int evutil_sockaddr_is_loopback(const struct sockaddr *sa); michael@0: michael@0: michael@0: /** michael@0: Formats a sockaddr sa into a string buffer of size outlen stored in out. michael@0: Returns a pointer to out. Always writes something into out, so it's safe michael@0: to use the output of this function without checking it for NULL. michael@0: */ michael@0: const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen); michael@0: michael@0: long evutil_tv_to_msec(const struct timeval *tv); michael@0: michael@0: int evutil_hex_char_to_int(char c); michael@0: michael@0: #ifdef WIN32 michael@0: HANDLE evutil_load_windows_system_library(const TCHAR *library_name); michael@0: #endif michael@0: michael@0: #ifndef EV_SIZE_FMT michael@0: #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) michael@0: #define EV_U64_FMT "%I64u" michael@0: #define EV_I64_FMT "%I64d" michael@0: #define EV_I64_ARG(x) ((__int64)(x)) michael@0: #define EV_U64_ARG(x) ((unsigned __int64)(x)) michael@0: #else michael@0: #define EV_U64_FMT "%llu" michael@0: #define EV_I64_FMT "%lld" michael@0: #define EV_I64_ARG(x) ((long long)(x)) michael@0: #define EV_U64_ARG(x) ((unsigned long long)(x)) michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef _WIN32 michael@0: #define EV_SOCK_FMT EV_I64_FMT michael@0: #define EV_SOCK_ARG(x) EV_I64_ARG((x)) michael@0: #else michael@0: #define EV_SOCK_FMT "%d" michael@0: #define EV_SOCK_ARG(x) (x) michael@0: #endif michael@0: michael@0: #if defined(__STDC__) && defined(__STDC_VERSION__) michael@0: #if (__STDC_VERSION__ >= 199901L) michael@0: #define EV_SIZE_FMT "%zu" michael@0: #define EV_SSIZE_FMT "%zd" michael@0: #define EV_SIZE_ARG(x) (x) michael@0: #define EV_SSIZE_ARG(x) (x) michael@0: #endif michael@0: #endif michael@0: michael@0: #ifndef EV_SIZE_FMT michael@0: #if (_EVENT_SIZEOF_SIZE_T <= _EVENT_SIZEOF_LONG) michael@0: #define EV_SIZE_FMT "%lu" michael@0: #define EV_SSIZE_FMT "%ld" michael@0: #define EV_SIZE_ARG(x) ((unsigned long)(x)) michael@0: #define EV_SSIZE_ARG(x) ((long)(x)) michael@0: #else michael@0: #define EV_SIZE_FMT EV_U64_FMT michael@0: #define EV_SSIZE_FMT EV_I64_FMT michael@0: #define EV_SIZE_ARG(x) EV_U64_ARG(x) michael@0: #define EV_SSIZE_ARG(x) EV_I64_ARG(x) michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif