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