2 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef _EVENT_UTIL_INTERNAL_H
27 #define _EVENT_UTIL_INTERNAL_H
29 #include "event2/event-config.h"
32 /* For EVUTIL_ASSERT */
33 #include "log-internal.h"
36 #ifdef _EVENT_HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
39 #include "event2/util.h"
45 /* If we need magic to say "inline", get it for free internally. */
47 #define inline _EVENT_inline
49 #ifdef _EVENT___func__
50 #define __func__ _EVENT___func__
53 /* A good no-op to use in macro definitions. */
54 #define _EVUTIL_NIL_STMT ((void)0)
55 /* Suppresses the compiler's "unused variable" warnings for unused assert. */
56 #define _EVUTIL_NIL_CONDITION(condition) do { \
57 (void)sizeof(condition); \
60 /* Internal use only: macros to match patterns of error codes in a
61 cross-platform way. We need these macros because of two historical
62 reasons: first, nonblocking IO functions are generally written to give an
63 error on the "blocked now, try later" case, so sometimes an error from a
64 read, write, connect, or accept means "no error; just wait for more
65 data," and we need to look at the error code. Second, Windows defines
66 a different set of error codes for sockets. */
70 /* True iff e is an error that means a read/write operation can be retried. */
71 #define EVUTIL_ERR_RW_RETRIABLE(e) \
72 ((e) == EINTR || (e) == EAGAIN)
73 /* True iff e is an error that means an connect can be retried. */
74 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
75 ((e) == EINTR || (e) == EINPROGRESS)
76 /* True iff e is an error that means a accept can be retried. */
77 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
78 ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED)
80 /* True iff e is an error that means the connection was refused */
81 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
86 #define EVUTIL_ERR_RW_RETRIABLE(e) \
87 ((e) == WSAEWOULDBLOCK || \
90 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
91 ((e) == WSAEWOULDBLOCK || \
93 (e) == WSAEINPROGRESS || \
96 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
97 EVUTIL_ERR_RW_RETRIABLE(e)
99 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
100 ((e) == WSAECONNREFUSED)
104 #ifdef _EVENT_socklen_t
105 #define socklen_t _EVENT_socklen_t
108 /* Arguments for shutdown() */
110 #define EVUTIL_SHUT_RD SHUT_RD
112 #define EVUTIL_SHUT_RD 0
115 #define EVUTIL_SHUT_WR SHUT_WR
117 #define EVUTIL_SHUT_WR 1
120 #define EVUTIL_SHUT_BOTH SHUT_BOTH
122 #define EVUTIL_SHUT_BOTH 2
125 /* Locale-independent replacements for some ctypes functions. Use these
126 * when you care about ASCII's notion of character types, because you are about
127 * to send those types onto the wire.
129 int EVUTIL_ISALPHA(char c);
130 int EVUTIL_ISALNUM(char c);
131 int EVUTIL_ISSPACE(char c);
132 int EVUTIL_ISDIGIT(char c);
133 int EVUTIL_ISXDIGIT(char c);
134 int EVUTIL_ISPRINT(char c);
135 int EVUTIL_ISLOWER(char c);
136 int EVUTIL_ISUPPER(char c);
137 char EVUTIL_TOUPPER(char c);
138 char EVUTIL_TOLOWER(char c);
140 /** Helper macro. If we know that a given pointer points to a field in a
141 structure, return a pointer to the structure itself. Used to implement
142 our half-baked C OO. Example:
146 struct supertype common;
150 void fn(struct supertype *super) {
151 struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
155 #define EVUTIL_UPCAST(ptr, type, field) \
156 ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
158 int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
161 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
163 int evutil_socket_finished_connecting(evutil_socket_t fd);
165 int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
167 int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
168 ev_socklen_t *socklen, int port);
170 const char *evutil_getenv(const char *name);
172 long _evutil_weakrand(void);
174 /* Evaluates to the same boolean value as 'p', and hints to the compiler that
175 * we expect this value to be false. */
177 #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
179 #define EVUTIL_UNLIKELY(p) (p)
182 /* Replacement for assert() that calls event_errx on failure. */
184 #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
185 #define EVUTIL_FAILURE_CHECK(cond) 0
187 #define EVUTIL_ASSERT(cond) \
189 if (EVUTIL_UNLIKELY(!(cond))) { \
190 event_errx(_EVENT_ERR_ABORT, \
191 "%s:%d: Assertion %s failed in %s", \
192 __FILE__,__LINE__,#cond,__func__); \
193 /* In case a user-supplied handler tries to */ \
194 /* return control to us, log and abort here. */ \
195 (void)fprintf(stderr, \
196 "%s:%d: Assertion %s failed in %s", \
197 __FILE__,__LINE__,#cond,__func__); \
201 #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
204 /* Internal addrinfo error code. This one is returned from only from
205 * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS
207 #define EVUTIL_EAI_NEED_RESOLVE -90002
210 struct evdns_getaddrinfo_request;
211 typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
212 struct evdns_base *base,
213 const char *nodename, const char *servname,
214 const struct evutil_addrinfo *hints_in,
215 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
217 void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
219 struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
220 ev_socklen_t socklen, const struct evutil_addrinfo *hints);
221 struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
222 struct evutil_addrinfo *append);
223 void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
224 int evutil_getaddrinfo_common(const char *nodename, const char *servname,
225 struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
227 int evutil_getaddrinfo_async(struct evdns_base *dns_base,
228 const char *nodename, const char *servname,
229 const struct evutil_addrinfo *hints_in,
230 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
232 /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
234 int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
238 Formats a sockaddr sa into a string buffer of size outlen stored in out.
239 Returns a pointer to out. Always writes something into out, so it's safe
240 to use the output of this function without checking it for NULL.
242 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
244 long evutil_tv_to_msec(const struct timeval *tv);
246 int evutil_hex_char_to_int(char c);
249 HANDLE evutil_load_windows_system_library(const TCHAR *library_name);