Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (192 sloc) 8.132 kb
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
1 /*
17efc1c Update all our copyright notices to say "2010"
Nick Mathewson authored
2 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
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 */
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
26 #ifndef _EVENT_UTIL_INTERNAL_H
27 #define _EVENT_UTIL_INTERNAL_H
28
29 #include "event-config.h"
30 #include <errno.h>
31
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
32 /* For EVUTIL_ASSERT */
33 #include "log-internal.h"
34 #include <stdio.h>
35 #include <stdlib.h>
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
36 #ifdef _EVENT_HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
47bad8a Implement size limits on HTTP header length and body length.
Nick Mathewson authored
39 #include "event2/util.h"
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
40
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
ebf2945 Compilation fixes for vc++ 2008 express. Not the end of them.
Nick Mathewson authored
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
661b5ee Actually, move EVUTIL_NIL_STMT to util-internal.h
Nick Mathewson authored
53 /* A good no-op to use in macro definitions. */
54 #define _EVUTIL_NIL_STMT ((void)0)
55
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
56 /* Internal use only: macros to match patterns of error codes in a
57 cross-platform way. We need these macros because of two historical
58 reasons: first, nonblocking IO functions are generally written to give an
59 error on the "blocked now, try later" case, so sometimes an error from a
60 read, write, connect, or accept means "no error; just wait for more
61 data," and we need to look at the error code. Second, Windows defines
62 a different set of error codes for sockets. */
63
64 #ifndef WIN32
65
66 /* True iff e is an error that means a read/write operation can be retried. */
67 #define EVUTIL_ERR_RW_RETRIABLE(e) \
68 ((e) == EINTR || (e) == EAGAIN)
7bc48bf deal with connect() failing immediately
Niels Provos authored
69 /* True iff e is an error that means an connect can be retried. */
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
70 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
71 ((e) == EINTR || (e) == EINPROGRESS)
7bc48bf deal with connect() failing immediately
Niels Provos authored
72 /* True iff e is an error that means a accept can be retried. */
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
73 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
74 ((e) == EINTR || (e) == EAGAIN || (e) == ECONNABORTED)
75
7bc48bf deal with connect() failing immediately
Niels Provos authored
76 /* True iff e is an error that means the connection was refused */
77 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
78 ((e) == ECONNREFUSED)
79
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
80 #else
81
bbd6a33 reindent macros in util-internal.h
Nick Mathewson authored
82 #define EVUTIL_ERR_RW_RETRIABLE(e) \
83 ((e) == WSAEWOULDBLOCK || \
84 (e) == WSAEINTR)
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
85
bbd6a33 reindent macros in util-internal.h
Nick Mathewson authored
86 #define EVUTIL_ERR_CONNECT_RETRIABLE(e) \
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
87 ((e) == WSAEWOULDBLOCK || \
bbd6a33 reindent macros in util-internal.h
Nick Mathewson authored
88 (e) == WSAEINTR || \
89 (e) == WSAEINPROGRESS || \
90 (e) == WSAEINVAL)
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
91
92 #define EVUTIL_ERR_ACCEPT_RETRIABLE(e) \
93 EVUTIL_ERR_RW_RETRIABLE(e)
94
7bc48bf deal with connect() failing immediately
Niels Provos authored
95 #define EVUTIL_ERR_CONNECT_REFUSED(e) \
96 ((e) == WSAECONNREFUSED)
97
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
98 #endif
99
7dd362b Have util-internal.h define socklen_t if we need it, and include it appr...
Nick Mathewson authored
100 #ifdef _EVENT_socklen_t
101 #define socklen_t _EVENT_socklen_t
102 #endif
103
cd731b7 Do not use ctypes functions in cases when we need the "net" locale.
Nick Mathewson authored
104 /* Locale-independent replacements for some ctypes functions. Use these
105 * when you care about ASCII's notion of character types, because you are about
106 * to send those types onto the wire.
107 */
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
108 #define DECLARE_CTYPE_FN(name) \
109 static int EVUTIL_##name(char c); \
110 extern const ev_uint32_t EVUTIL_##name##_TABLE[]; \
111 static inline int EVUTIL_##name(char c) { \
112 ev_uint8_t u = c; \
113 return !!(EVUTIL_##name##_TABLE[(u >> 5) & 7] & (1 << (u & 31))); \
114 }
cd731b7 Do not use ctypes functions in cases when we need the "net" locale.
Nick Mathewson authored
115 DECLARE_CTYPE_FN(ISALPHA)
116 DECLARE_CTYPE_FN(ISALNUM)
117 DECLARE_CTYPE_FN(ISSPACE)
118 DECLARE_CTYPE_FN(ISDIGIT)
119 DECLARE_CTYPE_FN(ISXDIGIT)
120 DECLARE_CTYPE_FN(ISPRINT)
121 DECLARE_CTYPE_FN(ISLOWER)
122 DECLARE_CTYPE_FN(ISUPPER)
918e9c5 Fix a number of warnings from gcc -pedantic
Nick Mathewson authored
123 extern const unsigned char EVUTIL_TOUPPER_TABLE[];
124 extern const unsigned char EVUTIL_TOLOWER_TABLE[];
125 #define EVUTIL_TOLOWER(c) ((char)EVUTIL_TOLOWER_TABLE[(ev_uint8_t)c])
126 #define EVUTIL_TOUPPER(c) ((char)EVUTIL_TOUPPER_TABLE[(ev_uint8_t)c])
cd731b7 Do not use ctypes functions in cases when we need the "net" locale.
Nick Mathewson authored
127
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
128 /** Helper macro. If we know that a given pointer points to a field in a
129 structure, return a pointer to the structure itself. Used to implement
130 our half-baked C OO. Example:
131
132 struct subtype {
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
133 int x;
134 struct supertype common;
135 int y;
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
136 };
137 ...
138 void fn(struct supertype *super) {
e5bbd40 Clean up formatting: use tabs, not 8-spaces, to indent.
Nick Mathewson authored
139 struct subtype *sub = EVUTIL_UPCAST(super, struct subtype, common);
140 ...
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
141 }
142 */
0b47b12 Add a new EVUTIL_UPCAST macro so that I do not need to keep figuring out...
Nick Mathewson authored
143 #define EVUTIL_UPCAST(ptr, type, field) \
8283b2f Fix a major parenthesis bug in EVUTIL_UPCAST.
Nick Mathewson authored
144 ((type *)(((char*)(ptr)) - evutil_offsetof(type, field)))
0b47b12 Add a new EVUTIL_UPCAST macro so that I do not need to keep figuring out...
Nick Mathewson authored
145
0f7144f Refactor code from evdns into a new internal "read a file" function.
Nick Mathewson authored
146 int evutil_read_file(const char *filename, char **content_out, size_t *len_out,
147 int is_binary);
709c21c Bufferevent support for openssl.
Nick Mathewson authored
148
149 int evutil_socket_connect(evutil_socket_t *fd_ptr, struct sockaddr *sa, int socklen);
150
25af695 When a bufferevent_connect() call fails, give the client an error callba...
Nick Mathewson authored
151 int evutil_socket_finished_connecting(evutil_socket_t fd);
152
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
153 int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
154 ev_socklen_t *socklen, int port);
155
629a613 When running set[ug]id, don't check the environment.
Nick Mathewson authored
156 const char *evutil_getenv(const char *name);
157
165d30e Fix compilation of rate-limiting code on win32.
Nick Mathewson authored
158 long _evutil_weakrand(void);
159
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
160 /* Evaluates to the same boolean value as 'p', and hints to the compiler that
161 * we expect this value to be false. */
eaaf27f Enable branch-prediction hints with EVUTIL_UNLIKELY.
Nick Mathewson authored
162 #ifdef __GNUC__
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
163 #define EVUTIL_UNLIKELY(p) __builtin_expect(!!(p),0)
164 #else
165 #define EVUTIL_UNLIKELY(p) (p)
166 #endif
167
168 /* Replacement for assert() that calls event_errx on failure. */
169 #define EVUTIL_ASSERT(cond) \
170 do { \
171 if (EVUTIL_UNLIKELY(!(cond))) { \
172 event_errx(_EVENT_ERR_ABORT, \
173 "%s:%d: Assertion %s failed in %s", \
174 __FILE__,__LINE__,#cond,__func__); \
8fdf09c Clean up formatting: Disallow space-before-tab.
Nick Mathewson authored
175 /* In case a user-supplied handler tries to */ \
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
176 /* return control to us, log and abort here. */ \
177 (void)fprintf(stderr, \
178 "%s:%d: Assertion %s failed in %s", \
179 __FILE__,__LINE__,#cond,__func__); \
180 abort(); \
181 } \
2c2618d more whitespace normalization
Nick Mathewson authored
182 } while (0)
37c3456 Add an EVUTIL_ASSERT() to replace our calls to assert().
Nick Mathewson authored
183
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblocking...
Nick Mathewson authored
184 /* Internal addrinfo error code. This one is returned from only from
185 * evutil_getaddrinfo_common, when we are sure that we'll have to hit a DNS
186 * server. */
187 #define EVUTIL_EAI_NEED_RESOLVE -90002
188
189 struct evdns_base;
190 struct evdns_getaddrinfo_request;
191 typedef struct evdns_getaddrinfo_request* (*evdns_getaddrinfo_fn)(
192 struct evdns_base *base,
193 const char *nodename, const char *servname,
194 const struct evutil_addrinfo *hints_in,
195 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
196
197 void evutil_set_evdns_getaddrinfo_fn(evdns_getaddrinfo_fn fn);
198
199 struct evutil_addrinfo *evutil_new_addrinfo(struct sockaddr *sa,
200 ev_socklen_t socklen, const struct evutil_addrinfo *hints);
201 struct evutil_addrinfo *evutil_addrinfo_append(struct evutil_addrinfo *first,
202 struct evutil_addrinfo *append);
203 void evutil_adjust_hints_for_addrconfig(struct evutil_addrinfo *hints);
204 int evutil_getaddrinfo_common(const char *nodename, const char *servname,
205 struct evutil_addrinfo *hints, struct evutil_addrinfo **res, int *portnum);
206
8d4aaf9 Don't use a bind address for nameservers on loopback
Nick Mathewson authored
207 int evutil_getaddrinfo_async(struct evdns_base *dns_base,
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblocking...
Nick Mathewson authored
208 const char *nodename, const char *servname,
209 const struct evutil_addrinfo *hints_in,
210 void (*cb)(int, struct evutil_addrinfo *, void *), void *arg);
211
8d4aaf9 Don't use a bind address for nameservers on loopback
Nick Mathewson authored
212 /** Return true iff sa is a looback address. (That is, it is 127.0.0.1/8, or
213 * ::1). */
214 int evutil_sockaddr_is_loopback(const struct sockaddr *sa);
215
b1c7950 Make evdns logging threadsafe
Nick Mathewson authored
216
217 /**
218 Formats a sockaddr sa into a string buffer of size outlen stored in out.
219 Returns a pointer to out. Always writes something into out, so it's safe
220 to use the output of this function without checking it for NULL.
221 */
222 const char *evutil_format_sockaddr_port(const struct sockaddr *sa, char *out, size_t outlen);
223
850c3ff chris-davis Add evutil_tv_to_msec for safe conversion of timevals to milliseconds.
chris-davis authored
224 long evutil_tv_to_msec(const struct timeval *tv);
225
5ebd23a New EVUTIL_ERR_*_RETRIABLE macros to tell if an errno blocked or failed.
Nick Mathewson authored
226 #ifdef __cplusplus
227 }
228 #endif
229
230 #endif
Something went wrong with that request. Please try again.