]> arthur.barton.de Git - netatalk.git/blob - libevent/util-internal.h
Add libevent
[netatalk.git] / libevent / util-internal.h
1 /*
2  * Copyright (c) 2007-2010 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 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 /* If we need magic to say "inline", get it for free internally. */
46 #ifdef _EVENT_inline
47 #define inline _EVENT_inline
48 #endif
49 #ifdef _EVENT___func__
50 #define __func__ _EVENT___func__
51 #endif
52
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); \
58 } while(0)
59
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. */
67
68 #ifndef WIN32
69
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)
79
80 /* True iff e is an error that means the connection was refused */
81 #define EVUTIL_ERR_CONNECT_REFUSED(e)                                   \
82         ((e) == ECONNREFUSED)
83
84 #else
85
86 #define EVUTIL_ERR_RW_RETRIABLE(e)                                      \
87         ((e) == WSAEWOULDBLOCK ||                                       \
88             (e) == WSAEINTR)
89
90 #define EVUTIL_ERR_CONNECT_RETRIABLE(e)                                 \
91         ((e) == WSAEWOULDBLOCK ||                                       \
92             (e) == WSAEINTR ||                                          \
93             (e) == WSAEINPROGRESS ||                                    \
94             (e) == WSAEINVAL)
95
96 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e)                  \
97         EVUTIL_ERR_RW_RETRIABLE(e)
98
99 #define EVUTIL_ERR_CONNECT_REFUSED(e)                                   \
100         ((e) == WSAECONNREFUSED)
101
102 #endif
103
104 #ifdef _EVENT_socklen_t
105 #define socklen_t _EVENT_socklen_t
106 #endif
107
108 /* Arguments for shutdown() */
109 #ifdef SHUT_RD
110 #define EVUTIL_SHUT_RD SHUT_RD
111 #else
112 #define EVUTIL_SHUT_RD 0
113 #endif
114 #ifdef SHUT_WR
115 #define EVUTIL_SHUT_WR SHUT_WR
116 #else
117 #define EVUTIL_SHUT_WR 1
118 #endif
119 #ifdef SHUT_BOTH
120 #define EVUTIL_SHUT_BOTH SHUT_BOTH
121 #else
122 #define EVUTIL_SHUT_BOTH 2
123 #endif
124
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.
128  */
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);
139
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:
143
144     struct subtype {
145         int x;
146         struct supertype common;
147         int y;
148     };
149     ...
150     void fn(struct supertype *super) {
151         struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
152         ...
153     }
154  */
155 #define EVUTIL_UPCAST(ptr, type, field)                         \
156         ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
157
158 int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
159     int is_binary);
160
161 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
162
163 int evutil_socket_finished_connecting(evutil_socket_t fd);
164
165 int evutil_ersatz_socketpair(int, int , int, evutil_socket_t[]);
166
167 int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
168     ev_socklen_t *socklen, int port);
169
170 const char *evutil_getenv(const char *name);
171
172 long _evutil_weakrand(void);
173
174 /* Evaluates to the same boolean value as 'p', and hints to the compiler that
175  * we expect this value to be false. */
176 #ifdef __GNUC__
177 #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
178 #else
179 #define EVUTIL_UNLIKELY(p) (p)
180 #endif
181
182 /* Replacement for assert() that calls event_errx on failure. */
183 #ifdef NDEBUG
184 #define EVUTIL_ASSERT(cond) _EVUTIL_NIL_CONDITION(cond)
185 #define EVUTIL_FAILURE_CHECK(cond) 0
186 #else
187 #define EVUTIL_ASSERT(cond)                                             \
188         do {                                                            \
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__);          \
198                         abort();                                        \
199                 }                                                       \
200         } while (0)
201 #define EVUTIL_FAILURE_CHECK(cond) EVUTIL_UNLIKELY(cond)
202 #endif
203
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
206  * server. */
207 #define EVUTIL_EAI_NEED_RESOLVE      -90002
208
209 struct evdns_base;
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);
216
217 void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
218
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);
226
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);
231
232 /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
233  * ::1). */
234 int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
235
236
237 /**
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.
241  */
242 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
243
244 long evutil_tv_to_msec(const struct timeval *tv);
245
246 int evutil_hex_char_to_int(char c);
247
248 #ifdef WIN32
249 HANDLE evutil_load_windows_system_library(const TCHAR *library_name);
250 #endif
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif