Austin Schuh | a273376 | 2015-09-06 17:46:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | #ifndef _EVENT_UTIL_INTERNAL_H |
| 27 | #define _EVENT_UTIL_INTERNAL_H |
| 28 | |
| 29 | #include "event2/event-config.h" |
| 30 | #include <errno.h> |
| 31 | |
| 32 | /* For EVUTIL_ASSERT */ |
| 33 | #include "log-internal.h" |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #ifdef _EVENT_HAVE_SYS_SOCKET_H |
| 37 | #include <sys/socket.h> |
| 38 | #endif |
| 39 | #include "event2/util.h" |
| 40 | |
| 41 | #include "ipv6-internal.h" |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
| 47 | /* If we need magic to say "inline", get it for free internally. */ |
| 48 | #ifdef _EVENT_inline |
| 49 | #define inline _EVENT_inline |
| 50 | #endif |
| 51 | #ifdef _EVENT___func__ |
| 52 | #define __func__ _EVENT___func__ |
| 53 | #endif |
| 54 | |
| 55 | /* A good no-op to use in macro definitions. */ |
| 56 | #define _EVUTIL_NIL_STMT ((void)0) |
| 57 | /* A no-op that tricks the compiler into thinking a condition is used while |
| 58 | * definitely not making any code for it. Used to compile out asserts while |
| 59 | * avoiding "unused variable" warnings. The "!" forces the compiler to |
| 60 | * do the sizeof() on an int, in case "condition" is a bitfield value. |
| 61 | */ |
| 62 | #define _EVUTIL_NIL_CONDITION(condition) do { \ |
| 63 | (void)sizeof(!(condition)); \ |
| 64 | } while(0) |
| 65 | |
| 66 | /* Internal use only: macros to match patterns of error codes in a |
| 67 | cross-platform way. We need these macros because of two historical |
| 68 | reasons: first, nonblocking IO functions are generally written to give an |
| 69 | error on the "blocked now, try later" case, so sometimes an error from a |
| 70 | read, write, connect, or accept means "no error; just wait for more |
| 71 | data," and we need to look at the error code. Second, Windows defines |
| 72 | a different set of error codes for sockets. */ |
| 73 | |
| 74 | #ifndef WIN32 |
| 75 | |
| 76 | /* True iff e is an error that means a read/write operation can be retried. */ |
| 77 | #define EVUTIL_ERR_RW_RETRIABLE(e) \ |
| 78 | ((e) == EINTR || (e) == EAGAIN) |
| 79 | /* True iff e is an error that means an connect can be retried. */ |
| 80 | #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ |
| 81 | ((e) == EINTR || (e) == EINPROGRESS) |
| 82 | /* True iff e is an error that means a accept can be retried. */ |
| 83 | #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ |
| 84 | ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED) |
| 85 | |
| 86 | /* True iff e is an error that means the connection was refused */ |
| 87 | #define EVUTIL_ERR_CONNECT_REFUSED(e) \ |
| 88 | ((e) == ECONNREFUSED) |
| 89 | |
| 90 | #else |
| 91 | |
| 92 | #define EVUTIL_ERR_RW_RETRIABLE(e) \ |
| 93 | ((e) == WSAEWOULDBLOCK || \ |
| 94 | (e) == WSAEINTR) |
| 95 | |
| 96 | #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \ |
| 97 | ((e) == WSAEWOULDBLOCK || \ |
| 98 | (e) == WSAEINTR || \ |
| 99 | (e) == WSAEINPROGRESS || \ |
| 100 | (e) == WSAEINVAL) |
| 101 | |
| 102 | #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \ |
| 103 | EVUTIL_ERR_RW_RETRIABLE(e) |
| 104 | |
| 105 | #define EVUTIL_ERR_CONNECT_REFUSED(e) \ |
| 106 | ((e) == WSAECONNREFUSED) |
| 107 | |
| 108 | #endif |
| 109 | |
| 110 | #ifdef _EVENT_socklen_t |
| 111 | #define socklen_t _EVENT_socklen_t |
| 112 | #endif |
| 113 | |
| 114 | /* Arguments for shutdown() */ |
| 115 | #ifdef SHUT_RD |
| 116 | #define EVUTIL_SHUT_RD SHUT_RD |
| 117 | #else |
| 118 | #define EVUTIL_SHUT_RD 0 |
| 119 | #endif |
| 120 | #ifdef SHUT_WR |
| 121 | #define EVUTIL_SHUT_WR SHUT_WR |
| 122 | #else |
| 123 | #define EVUTIL_SHUT_WR 1 |
| 124 | #endif |
| 125 | #ifdef SHUT_BOTH |
| 126 | #define EVUTIL_SHUT_BOTH SHUT_BOTH |
| 127 | #else |
| 128 | #define EVUTIL_SHUT_BOTH 2 |
| 129 | #endif |
| 130 | |
| 131 | /* Locale-independent replacements for some ctypes functions. Use these |
| 132 | * when you care about ASCII's notion of character types, because you are about |
| 133 | * to send those types onto the wire. |
| 134 | */ |
| 135 | int EVUTIL_ISALPHA(char c); |
| 136 | int EVUTIL_ISALNUM(char c); |
| 137 | int EVUTIL_ISSPACE(char c); |
| 138 | int EVUTIL_ISDIGIT(char c); |
| 139 | int EVUTIL_ISXDIGIT(char c); |
| 140 | int EVUTIL_ISPRINT(char c); |
| 141 | int EVUTIL_ISLOWER(char c); |
| 142 | int EVUTIL_ISUPPER(char c); |
| 143 | char EVUTIL_TOUPPER(char c); |
| 144 | char EVUTIL_TOLOWER(char c); |
| 145 | |
| 146 | /** Helper macro. If we know that a given pointer points to a field in a |
| 147 | structure, return a pointer to the structure itself. Used to implement |
| 148 | our half-baked C OO. Example: |
| 149 | |
| 150 | struct subtype { |
| 151 | int x; |
| 152 | struct supertype common; |
| 153 | int y; |
| 154 | }; |
| 155 | ... |
| 156 | void fn(struct supertype *super) { |
| 157 | struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common); |
| 158 | ... |
| 159 | } |
| 160 | */ |
| 161 | #define EVUTIL_UPCAST(ptr, type, field) \ |
| 162 | ((type *)(((char*)(ptr)) - evutil_offsetof(type, field))) |
| 163 | |
| 164 | /* As open(pathname, flags, mode), except that the file is always opened with |
| 165 | * the close-on-exec flag set. (And the mode argument is mandatory.) |
| 166 | */ |
| 167 | int evutil_open_closeonexec(const char *pathname, int flags, unsigned mode); |
| 168 | |
| 169 | int evutil_read_file(const char *filename, char **content_out, size_t *len_out, |
| 170 | int is_binary); |
| 171 | |
| 172 | int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen); |
| 173 | |
| 174 | int evutil_socket_finished_connecting(evutil_socket_t fd); |
| 175 | |
| 176 | int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]); |
| 177 | |
| 178 | int evutil_resolve(int family, const char *hostname, struct sockaddr *sa, |
| 179 | ev_socklen_t *socklen, int port); |
| 180 | |
| 181 | const char *evutil_getenv(const char *name); |
| 182 | |
| 183 | long _evutil_weakrand(void); |
| 184 | |
| 185 | /* Evaluates to the same boolean value as 'p', and hints to the compiler that |
| 186 | * we expect this value to be false. */ |
| 187 | #if defined(__GNUC__) && __GNUC__ >= 3 /* gcc 3.0 or later */ |
| 188 | #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0) |
| 189 | #else |
| 190 | #define EVUTIL_UNLIKELY(p) (p) |
| 191 | #endif |
| 192 | |
| 193 | /* Replacement for assert() that calls event_errx on failure. */ |
| 194 | #ifdef NDEBUG |
| 195 | #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond) |
| 196 | #define EVUTIL_FAILURE_CHECK(cond) 0 |
| 197 | #else |
| 198 | #define EVUTIL_ASSERT(cond) \ |
| 199 | do { \ |
| 200 | if (EVUTIL_UNLIKELY(!(cond))) { \ |
| 201 | event_errx(_EVENT_ERR_ABORT, \ |
| 202 | "%s:%d: Assertion %s failed in %s", \ |
| 203 | __FILE__,__LINE__,#cond,__func__); \ |
| 204 | /* In case a user-supplied handler tries to */ \ |
| 205 | /* return control to us, log and abort here. */ \ |
| 206 | (void)fprintf(stderr, \ |
| 207 | "%s:%d: Assertion %s failed in %s", \ |
| 208 | __FILE__,__LINE__,#cond,__func__); \ |
| 209 | abort(); \ |
| 210 | } \ |
| 211 | } while (0) |
| 212 | #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond) |
| 213 | #endif |
| 214 | |
| 215 | #ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE |
| 216 | /* Replacement for sockaddr storage that we can use internally on platforms |
| 217 | * that lack it. It is not space-efficient, but neither is sockaddr_storage. |
| 218 | */ |
| 219 | struct sockaddr_storage { |
| 220 | union { |
| 221 | struct sockaddr ss_sa; |
| 222 | struct sockaddr_in ss_sin; |
| 223 | struct sockaddr_in6 ss_sin6; |
| 224 | char ss_padding[128]; |
| 225 | } ss_union; |
| 226 | }; |
| 227 | #define ss_family ss_union.ss_sa.sa_family |
| 228 | #endif |
| 229 | |
| 230 | /* Internal addrinfo error code. This one is returned from only from |
| 231 | * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS |
| 232 | * server. */ |
| 233 | #define EVUTIL_EAI_NEED_RESOLVE -90002 |
| 234 | |
| 235 | struct evdns_base; |
| 236 | struct evdns_getaddrinfo_request; |
| 237 | typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)( |
| 238 | struct evdns_base *base, |
| 239 | const char *nodename, const char *servname, |
| 240 | const struct evutil_addrinfo *hints_in, |
| 241 | void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); |
| 242 | |
| 243 | void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn); |
| 244 | |
| 245 | struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa, |
| 246 | ev_socklen_t socklen, const struct evutil_addrinfo *hints); |
| 247 | struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first, |
| 248 | struct evutil_addrinfo *append); |
| 249 | void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints); |
| 250 | int evutil_getaddrinfo_common(const char *nodename, const char *servname, |
| 251 | struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum); |
| 252 | |
| 253 | int evutil_getaddrinfo_async(struct evdns_base *dns_base, |
| 254 | const char *nodename, const char *servname, |
| 255 | const struct evutil_addrinfo *hints_in, |
| 256 | void (*cb)(int, struct evutil_addrinfo *, void *), void *arg); |
| 257 | |
| 258 | /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or |
| 259 | * ::1). */ |
| 260 | int evutil_sockaddr_is_loopback(const struct sockaddr *sa); |
| 261 | |
| 262 | |
| 263 | /** |
| 264 | Formats a sockaddr sa into a string buffer of size outlen stored in out. |
| 265 | Returns a pointer to out. Always writes something into out, so it's safe |
| 266 | to use the output of this function without checking it for NULL. |
| 267 | */ |
| 268 | const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen); |
| 269 | |
| 270 | long evutil_tv_to_msec(const struct timeval *tv); |
| 271 | |
| 272 | int evutil_hex_char_to_int(char c); |
| 273 | |
| 274 | #ifdef WIN32 |
| 275 | HANDLE evutil_load_windows_system_library(const TCHAR *library_name); |
| 276 | #endif |
| 277 | |
| 278 | #ifndef EV_SIZE_FMT |
| 279 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) |
| 280 | #define EV_U64_FMT "%I64u" |
| 281 | #define EV_I64_FMT "%I64d" |
| 282 | #define EV_I64_ARG(x) ((__int64)(x)) |
| 283 | #define EV_U64_ARG(x) ((unsigned __int64)(x)) |
| 284 | #else |
| 285 | #define EV_U64_FMT "%llu" |
| 286 | #define EV_I64_FMT "%lld" |
| 287 | #define EV_I64_ARG(x) ((long long)(x)) |
| 288 | #define EV_U64_ARG(x) ((unsigned long long)(x)) |
| 289 | #endif |
| 290 | #endif |
| 291 | |
| 292 | #ifdef _WIN32 |
| 293 | #define EV_SOCK_FMT EV_I64_FMT |
| 294 | #define EV_SOCK_ARG(x) EV_I64_ARG((x)) |
| 295 | #else |
| 296 | #define EV_SOCK_FMT "%d" |
| 297 | #define EV_SOCK_ARG(x) (x) |
| 298 | #endif |
| 299 | |
| 300 | #if defined(__STDC__) && defined(__STDC_VERSION__) |
| 301 | #if (__STDC_VERSION__ >= 199901L) |
| 302 | #define EV_SIZE_FMT "%zu" |
| 303 | #define EV_SSIZE_FMT "%zd" |
| 304 | #define EV_SIZE_ARG(x) (x) |
| 305 | #define EV_SSIZE_ARG(x) (x) |
| 306 | #endif |
| 307 | #endif |
| 308 | |
| 309 | #ifndef EV_SIZE_FMT |
| 310 | #if (_EVENT_SIZEOF_SIZE_T <= _EVENT_SIZEOF_LONG) |
| 311 | #define EV_SIZE_FMT "%lu" |
| 312 | #define EV_SSIZE_FMT "%ld" |
| 313 | #define EV_SIZE_ARG(x) ((unsigned long)(x)) |
| 314 | #define EV_SSIZE_ARG(x) ((long)(x)) |
| 315 | #else |
| 316 | #define EV_SIZE_FMT EV_U64_FMT |
| 317 | #define EV_SSIZE_FMT EV_I64_FMT |
| 318 | #define EV_SIZE_ARG(x) EV_U64_ARG(x) |
| 319 | #define EV_SSIZE_ARG(x) EV_I64_ARG(x) |
| 320 | #endif |
| 321 | #endif |
| 322 | |
| 323 | void evutil_memclear_(void *mem, size_t len); |
| 324 | |
| 325 | #ifdef __cplusplus |
| 326 | } |
| 327 | #endif |
| 328 | |
| 329 | #endif |