MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / libevent / util-internal.h
blob18d6ca75cae3f6a7a9cade32ea240df1b7ac4251
1 /*
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
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.
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"
30 #include <errno.h>
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"
41 #include "ipv6-internal.h"
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
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
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.
62 #define _EVUTIL_NIL_CONDITION(condition) do { \
63 (void)sizeof(!(condition)); \
64 } while(0)
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. */
74 #ifndef WIN32
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)
86 /* True iff e is an error that means the connection was refused */
87 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
88 ((e) == ECONNREFUSED)
90 #else
92 #define EVUTIL_ERR_RW_RETRIABLE(e) \
93 ((e) == WSAEWOULDBLOCK || \
94 (e) == WSAEINTR)
96 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
97 ((e) == WSAEWOULDBLOCK || \
98 (e) == WSAEINTR || \
99 (e) == WSAEINPROGRESS || \
100 (e) == WSAEINVAL)
102 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
103 EVUTIL_ERR_RW_RETRIABLE(e)
105 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
106 ((e) == WSAECONNREFUSED)
108 #endif
110 #ifdef _EVENT_socklen_t
111 #define socklen_t _EVENT_socklen_t
112 #endif
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
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.
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);
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:
150 struct subtype {
151 int x;
152 struct supertype common;
153 int y;
156 void fn(struct supertype *super) {
157 struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
161 #define EVUTIL_UPCAST(ptr, type, field) \
162 ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
164 int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
165 int is_binary);
167 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
169 int evutil_socket_finished_connecting(evutil_socket_t fd);
171 int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
173 int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
174 ev_socklen_t *socklen, int port);
176 const char *evutil_getenv(const char *name);
178 long _evutil_weakrand(void);
180 /* Evaluates to the same boolean value as 'p', and hints to the compiler that
181 * we expect this value to be false. */
182 #ifdef __GNUC__
183 #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
184 #else
185 #define EVUTIL_UNLIKELY(p) (p)
186 #endif
188 /* Replacement for assert() that calls event_errx on failure. */
189 #ifdef NDEBUG
190 #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
191 #define EVUTIL_FAILURE_CHECK(cond) 0
192 #else
193 #define EVUTIL_ASSERT(cond) \
194 do { \
195 if (EVUTIL_UNLIKELY(!(cond))) { \
196 event_errx(_EVENT_ERR_ABORT, \
197 "%s:%d: Assertion %s failed in %s", \
198 __FILE__,__LINE__,#cond,__func__); \
199 /* In case a user-supplied handler tries to */ \
200 /* return control to us, log and abort here. */ \
201 (void)fprintf(stderr, \
202 "%s:%d: Assertion %s failed in %s", \
203 __FILE__,__LINE__,#cond,__func__); \
204 abort(); \
206 } while (0)
207 #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
208 #endif
210 #ifndef _EVENT_HAVE_STRUCT_SOCKADDR_STORAGE
211 /* Replacement for sockaddr storage that we can use internally on platforms
212 * that lack it. It is not space-efficient, but neither is sockaddr_storage.
214 struct sockaddr_storage {
215 union {
216 struct sockaddr ss_sa;
217 struct sockaddr_in ss_sin;
218 struct sockaddr_in6 ss_sin6;
219 char ss_padding[128];
220 } ss_union;
222 #define ss_family ss_union.ss_sa.sa_family
223 #endif
225 /* Internal addrinfo error code. This one is returned from only from
226 * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS
227 * server. */
228 #define EVUTIL_EAI_NEED_RESOLVE -90002
230 struct evdns_base;
231 struct evdns_getaddrinfo_request;
232 typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
233 struct evdns_base *base,
234 const char *nodename, const char *servname,
235 const struct evutil_addrinfo *hints_in,
236 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
238 void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
240 struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
241 ev_socklen_t socklen, const struct evutil_addrinfo *hints);
242 struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
243 struct evutil_addrinfo *append);
244 void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
245 int evutil_getaddrinfo_common(const char *nodename, const char *servname,
246 struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
248 int evutil_getaddrinfo_async(struct evdns_base *dns_base,
249 const char *nodename, const char *servname,
250 const struct evutil_addrinfo *hints_in,
251 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
253 /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
254 * ::1). */
255 int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
259 Formats a sockaddr sa into a string buffer of size outlen stored in out.
260 Returns a pointer to out. Always writes something into out, so it's safe
261 to use the output of this function without checking it for NULL.
263 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
265 long evutil_tv_to_msec(const struct timeval *tv);
267 int evutil_hex_char_to_int(char c);
269 #ifdef WIN32
270 HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
271 #endif
273 #ifdef __cplusplus
275 #endif
277 #endif