Skip to content
Newer
Older
100644 694 lines (591 sloc) 17.1 KB
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
1 /*
e49e289 Update copyright notices to 2012
Nick Mathewson authored
2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
eda27f9 Update copyright notices, add some missing license statements
Nick Mathewson authored
3 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
ec347b9 Move event-config.h to include/event2
Nick Mathewson authored
29 #include "event2/event-config.h"
0915ca0 @kev009 Include evconfig-private.h in internal files for great good.
kev009 authored
30 #include "evconfig-private.h"
31
32 #include <sys/types.h>
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
33
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
34 #ifdef EVENT__HAVE_SYS_TIME_H
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
35 #include <sys/time.h>
36 #endif
37
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
42 #ifdef EVENT__HAVE_STDARG_H
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
43 #include <stdarg.h>
44 #endif
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
45 #ifdef EVENT__HAVE_UNISTD_H
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
46 #include <unistd.h>
47 #endif
48
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
49 #ifdef _WIN32
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
50 #include <winsock2.h>
6ca32df Add a missing include for win32.
Nick Mathewson authored
51 #include <ws2tcpip.h>
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
52 #endif
53
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
54 #ifdef EVENT__HAVE_SYS_SOCKET_H
659d54d Add new code to make and accept connections.
Nick Mathewson authored
55 #include <sys/socket.h>
56 #endif
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
57 #ifdef EVENT__HAVE_NETINET_IN_H
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
58 #include <netinet/in.h>
59 #endif
68120d9 Convert event-config.h macros to avoid reserved identifiers
Nick Mathewson authored
60 #ifdef EVENT__HAVE_NETINET_IN6_H
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
61 #include <netinet/in6.h>
62 #endif
659d54d Add new code to make and accept connections.
Nick Mathewson authored
63
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
64 #include "event2/util.h"
65 #include "event2/bufferevent.h"
66 #include "event2/buffer.h"
67 #include "event2/bufferevent_struct.h"
68 #include "event2/bufferevent_compat.h"
69 #include "event2/event.h"
70 #include "log-internal.h"
71 #include "mm-internal.h"
72 #include "bufferevent-internal.h"
73 #include "util-internal.h"
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
74 #ifdef _WIN32
879420a Expose a narrow window to the IOCP code.
Nick Mathewson authored
75 #include "iocp-internal.h"
76 #endif
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
77
78 /* prototypes */
79 static int be_socket_enable(struct bufferevent *, short);
80 static int be_socket_disable(struct bufferevent *, short);
81 static void be_socket_destruct(struct bufferevent *);
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
82 static int be_socket_adj_timeouts(struct bufferevent *);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
83 static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
31d89f2 Add a "ctrl" mechanism to bufferevents for property access.
Nick Mathewson authored
84 static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
85
86 static void be_socket_setfd(struct bufferevent *, evutil_socket_t);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
87
23085c9 Add a linked-pair abstraction to bufferevents.
Nick Mathewson authored
88 const struct bufferevent_ops bufferevent_ops_socket = {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
89 "socket",
657d1b6 Set mem_offset for every bufferevent type
Nick Mathewson authored
90 evutil_offsetof(struct bufferevent_private, bev),
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
91 be_socket_enable,
92 be_socket_disable,
93 be_socket_destruct,
94 be_socket_adj_timeouts,
eff09a2 tab/whitespace fixes in bufferevent_sock.c
Nick Mathewson authored
95 be_socket_flush,
31d89f2 Add a "ctrl" mechanism to bufferevents for property access.
Nick Mathewson authored
96 be_socket_ctrl,
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
97 };
98
709c21c Bufferevent support for openssl.
Nick Mathewson authored
99 #define be_socket_add(ev, t) \
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
100 bufferevent_add_event_((ev), (t))
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
101
102 static void
103 bufferevent_socket_outbuf_cb(struct evbuffer *buf,
f1b1bad Make the new evbuffer callbacks use a new struct-based interface.
Nick Mathewson authored
104 const struct evbuffer_cb_info *cbinfo,
105 void *arg)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
106 {
107 struct bufferevent *bufev = arg;
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
108 struct bufferevent_private *bufev_p =
109 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
110
f1b1bad Make the new evbuffer callbacks use a new struct-based interface.
Nick Mathewson authored
111 if (cbinfo->n_added &&
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
112 (bufev->enabled & EV_WRITE) &&
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
113 !event_pending(&bufev->ev_write, EV_WRITE, NULL) &&
114 !bufev_p->write_suspended) {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
115 /* Somebody added data to the buffer, and we would like to
116 * write, and we were not writing. So, start writing. */
7c11e51 Clean up some problems identified by Coverity.
Harlan Stenn authored
117 if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) == -1) {
d84d917 @ghewgill Use C-style comments in C source files (for compatibility with compil…
ghewgill authored
118 /* Should we log this? */
7c11e51 Clean up some problems identified by Coverity.
Harlan Stenn authored
119 }
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
120 }
121 }
122
123 static void
124 bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
125 {
126 struct bufferevent *bufev = arg;
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
127 struct bufferevent_private *bufev_p =
128 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
129 struct evbuffer *input;
130 int res = 0;
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
131 short what = BEV_EVENT_READING;
598d133 Try to clear up more size_t vs int/long issues.
Nick Mathewson authored
132 ev_ssize_t howmuch = -1, readmax=-1;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
133
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
134 bufferevent_incref_and_lock_(bufev);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
135
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
136 if (event == EV_TIMEOUT) {
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
137 what |= BEV_EVENT_TIMEOUT;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
138 goto error;
139 }
140
141 input = bufev->input;
142
143 /*
144 * If we have a high watermark configured then we don't want to
145 * read more data than would make us reach the watermark.
146 */
147 if (bufev->wm_read.high != 0) {
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_com…
Nick Mathewson authored
148 howmuch = bufev->wm_read.high - evbuffer_get_length(input);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
149 /* we somehow lowered the watermark, stop reading */
150 if (howmuch <= 0) {
151 bufferevent_wm_suspend_read(bufev);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
152 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
153 }
154 }
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
155 readmax = bufferevent_get_read_max_(bufev_p);
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
156 if (howmuch < 0 || howmuch > readmax) /* The use of -1 for "unlimited"
7484df6 Fix even more win64 warnings
Nick Mathewson authored
157 * uglifies this code. XXXX */
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
158 howmuch = readmax;
159 if (bufev_p->read_suspended)
160 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
161
72b6ffe Prevent unsupported modifications to bufferevent_sock buffers.
Nick Mathewson authored
162 evbuffer_unfreeze(input, 0);
7484df6 Fix even more win64 warnings
Nick Mathewson authored
163 res = evbuffer_read(input, fd, (int)howmuch); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
72b6ffe Prevent unsupported modifications to bufferevent_sock buffers.
Nick Mathewson authored
164 evbuffer_freeze(input, 0);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
165
166 if (res == -1) {
167 int err = evutil_socket_geterror(fd);
168 if (EVUTIL_ERR_RW_RETRIABLE(err))
169 goto reschedule;
170 /* error case */
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
171 what |= BEV_EVENT_ERROR;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
172 } else if (res == 0) {
173 /* eof case */
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
174 what |= BEV_EVENT_EOF;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
175 }
176
177 if (res <= 0)
178 goto error;
179
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
180 bufferevent_decrement_read_buckets_(bufev_p, res);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
181
182 /* Invoke the user callback - must always be called last */
2e8eeea Fix crash bugs when a bufferevent's eventcb is not set.
Nick Mathewson authored
183 if (evbuffer_get_length(input) >= bufev->wm_read.low)
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
184 bufferevent_run_readcb_(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
185
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
186 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
187
188 reschedule:
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
189 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
190
191 error:
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
192 bufferevent_disable(bufev, EV_READ);
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
193 bufferevent_run_eventcb_(bufev, what);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
194
195 done:
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
196 bufferevent_decref_and_unlock_(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
197 }
198
199 static void
200 bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
201 {
202 struct bufferevent *bufev = arg;
659d54d Add new code to make and accept connections.
Nick Mathewson authored
203 struct bufferevent_private *bufev_p =
204 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
205 int res = 0;
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
206 short what = BEV_EVENT_WRITING;
f65b8b0 On connect, call only one of BEV_EVENT_CONNECTED or writecb.
Nick Mathewson authored
207 int connected = 0;
7484df6 Fix even more win64 warnings
Nick Mathewson authored
208 ev_ssize_t atmost = -1;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
209
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
210 bufferevent_incref_and_lock_(bufev);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
211
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
212 if (event == EV_TIMEOUT) {
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
213 what |= BEV_EVENT_TIMEOUT;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
214 goto error;
215 }
659d54d Add new code to make and accept connections.
Nick Mathewson authored
216 if (bufev_p->connecting) {
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
217 int c = evutil_socket_finished_connecting_(fd);
7bc48bf deal with connect() failing immediately
Niels Provos authored
218 /* we need to fake the error if the connection was refused
219 * immediately - usually connection to localhost on BSD */
220 if (bufev_p->connection_refused) {
221 bufev_p->connection_refused = 0;
222 c = -1;
223 }
25af695 When a bufferevent_connect() call fails, give the client an error cal…
Nick Mathewson authored
224
225 if (c == 0)
226 goto done;
227
659d54d Add new code to make and accept connections.
Nick Mathewson authored
228 bufev_p->connecting = 0;
25af695 When a bufferevent_connect() call fails, give the client an error cal…
Nick Mathewson authored
229 if (c < 0) {
659d54d Add new code to make and accept connections.
Nick Mathewson authored
230 event_del(&bufev->ev_write);
25af695 When a bufferevent_connect() call fails, give the client an error cal…
Nick Mathewson authored
231 event_del(&bufev->ev_read);
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
232 bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
233 goto done;
25af695 When a bufferevent_connect() call fails, give the client an error cal…
Nick Mathewson authored
234 } else {
235 connected = 1;
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
236 #ifdef _WIN32
8fdf09c Clean up formatting: Disallow space-before-tab.
Nick Mathewson authored
237 if (BEV_IS_ASYNC(bufev)) {
d7d1f1d Move responsibility for IOCP callback into bufferevent_async.
Nick Mathewson authored
238 event_del(&bufev->ev_write);
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
239 bufferevent_async_set_connected_(bufev);
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
240 bufferevent_run_eventcb_(bufev,
d7d1f1d Move responsibility for IOCP callback into bufferevent_async.
Nick Mathewson authored
241 BEV_EVENT_CONNECTED);
242 goto done;
243 }
244 #endif
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
245 bufferevent_run_eventcb_(bufev,
d7d1f1d Move responsibility for IOCP callback into bufferevent_async.
Nick Mathewson authored
246 BEV_EVENT_CONNECTED);
4a5b534 Do not ignore bufferevent_enable(EV_READ) before bufferevent_connect().
Nick Mathewson authored
247 if (!(bufev->enabled & EV_WRITE) ||
248 bufev_p->write_suspended) {
25af695 When a bufferevent_connect() call fails, give the client an error cal…
Nick Mathewson authored
249 event_del(&bufev->ev_write);
250 goto done;
251 }
659d54d Add new code to make and accept connections.
Nick Mathewson authored
252 }
253 }
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
254
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
255 atmost = bufferevent_get_write_max_(bufev_p);
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
256
257 if (bufev_p->write_suspended)
258 goto done;
259
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_com…
Nick Mathewson authored
260 if (evbuffer_get_length(bufev->output)) {
eff09a2 tab/whitespace fixes in bufferevent_sock.c
Nick Mathewson authored
261 evbuffer_unfreeze(bufev->output, 1);
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
262 res = evbuffer_write_atmost(bufev->output, fd, atmost);
eff09a2 tab/whitespace fixes in bufferevent_sock.c
Nick Mathewson authored
263 evbuffer_freeze(bufev->output, 1);
264 if (res == -1) {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
265 int err = evutil_socket_geterror(fd);
266 if (EVUTIL_ERR_RW_RETRIABLE(err))
267 goto reschedule;
eff09a2 tab/whitespace fixes in bufferevent_sock.c
Nick Mathewson authored
268 what |= BEV_EVENT_ERROR;
269 } else if (res == 0) {
270 /* eof case
271 XXXX Actually, a 0 on write doesn't indicate
272 an EOF. An ECONNRESET might be more typical.
273 */
274 what |= BEV_EVENT_EOF;
275 }
276 if (res <= 0)
277 goto error;
737c9cd Rate-limiting for bufferevents; group and individual limits are suppo…
Nick Mathewson authored
278
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
279 bufferevent_decrement_write_buckets_(bufev_p, res);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
280 }
281
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
282 if (evbuffer_get_length(bufev->output) == 0) {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
283 event_del(&bufev->ev_write);
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
284 }
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
285
286 /*
287 * Invoke the user callback if our buffer is drained or below the
288 * low watermark.
289 */
2e8eeea Fix crash bugs when a bufferevent's eventcb is not set.
Nick Mathewson authored
290 if ((res || !connected) &&
7515de9 When connect() succeeds immediately, don't invoke the callback immedi…
Nick Mathewson authored
291 evbuffer_get_length(bufev->output) <= bufev->wm_write.low) {
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
292 bufferevent_run_writecb_(bufev);
7515de9 When connect() succeeds immediately, don't invoke the callback immedi…
Nick Mathewson authored
293 }
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
294
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
295 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
296
297 reschedule:
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
298 if (evbuffer_get_length(bufev->output) == 0) {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
299 event_del(&bufev->ev_write);
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
300 }
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
301 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
302
303 error:
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
304 bufferevent_disable(bufev, EV_WRITE);
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
305 bufferevent_run_eventcb_(bufev, what);
a501d68 Add a lock/unlock pair inside the event callbacks in bufferevents.
Nick Mathewson authored
306
307 done:
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
308 bufferevent_decref_and_unlock_(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
309 }
310
311 struct bufferevent *
312 bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
b73ad7b Treat the bitwise OR of two enum values as an int.
Nick Mathewson authored
313 int options)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
314 {
1becc4c Refactor new elements of bufferevent into bufferevent_private structure
Nick Mathewson authored
315 struct bufferevent_private *bufev_p;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
316 struct bufferevent *bufev;
317
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
318 #ifdef _WIN32
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
319 if (base && event_base_get_iocp_(base))
320 return bufferevent_async_new_(base, fd, options);
879420a Expose a narrow window to the IOCP code.
Nick Mathewson authored
321 #endif
322
1becc4c Refactor new elements of bufferevent into bufferevent_private structure
Nick Mathewson authored
323 if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
324 return NULL;
325
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
326 if (bufferevent_init_common_(bufev_p, base, &bufferevent_ops_socket,
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
327 options) < 0) {
1becc4c Refactor new elements of bufferevent into bufferevent_private structure
Nick Mathewson authored
328 mm_free(bufev_p);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
329 return NULL;
330 }
1becc4c Refactor new elements of bufferevent into bufferevent_private structure
Nick Mathewson authored
331 bufev = &bufev_p->bev;
0ba0af9 Prefer mmap to sendfile unless a DRAINS_TO_FD flag is set. Allows add…
Nick Mathewson authored
332 evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
333
334 event_assign(&bufev->ev_read, bufev->ev_base, fd,
335 EV_READ|EV_PERSIST, bufferevent_readcb, bufev);
336 event_assign(&bufev->ev_write, bufev->ev_base, fd,
337 EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev);
338
339 evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
340
72b6ffe Prevent unsupported modifications to bufferevent_sock buffers.
Nick Mathewson authored
341 evbuffer_freeze(bufev->input, 0);
342 evbuffer_freeze(bufev->output, 1);
343
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
344 return bufev;
345 }
346
659d54d Add new code to make and accept connections.
Nick Mathewson authored
347 int
348 bufferevent_socket_connect(struct bufferevent *bev,
349 struct sockaddr *sa, int socklen)
350 {
351 struct bufferevent_private *bufev_p =
352 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
353
354 evutil_socket_t fd;
800f9aa When bufferevent_socket_connect is called with no address, assume tha…
Nick Mathewson authored
355 int r = 0;
709c21c Bufferevent support for openssl.
Nick Mathewson authored
356 int result=-1;
d1a2254 Fix some bugs in bufferevent_socket_connect
Nick Mathewson authored
357 int ownfd = 0;
a62283a Always hold a reference to a bufferevent when calling its callbacks.
Nick Mathewson authored
358
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
359 bufferevent_incref_and_lock_(bev);
659d54d Add new code to make and accept connections.
Nick Mathewson authored
360
361 if (!bufev_p)
a62283a Always hold a reference to a bufferevent when calling its callbacks.
Nick Mathewson authored
362 goto done;
659d54d Add new code to make and accept connections.
Nick Mathewson authored
363
709c21c Bufferevent support for openssl.
Nick Mathewson authored
364 fd = bufferevent_getfd(bev);
d1a2254 Fix some bugs in bufferevent_socket_connect
Nick Mathewson authored
365 if (fd < 0) {
800f9aa When bufferevent_socket_connect is called with no address, assume tha…
Nick Mathewson authored
366 if (!sa)
367 goto done;
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
368 fd = evutil_socket_(sa->sa_family,
33fca62 Save some syscalls when constructing a socket for a bufferevent
Nick Mathewson authored
369 SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
d1a2254 Fix some bugs in bufferevent_socket_connect
Nick Mathewson authored
370 if (fd < 0)
371 goto done;
372 ownfd = 1;
373 }
800f9aa When bufferevent_socket_connect is called with no address, assume tha…
Nick Mathewson authored
374 if (sa) {
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
375 #ifdef _WIN32
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
376 if (bufferevent_async_can_connect_(bev)) {
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
377 bufferevent_setfd(bev, fd);
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
378 r = bufferevent_async_connect_(bev, fd, sa, socklen);
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
379 if (r < 0)
380 goto freesock;
381 bufev_p->connecting = 1;
382 result = 0;
800f9aa When bufferevent_socket_connect is called with no address, assume tha…
Nick Mathewson authored
383 goto done;
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
384 } else
385 #endif
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
386 r = evutil_socket_connect_(&fd, sa, socklen);
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
387 if (r < 0)
388 goto freesock;
659d54d Add new code to make and accept connections.
Nick Mathewson authored
389 }
9f560bf Use "_WIN32", not WIN32: it's standard and we don't need to fake it
Nick Mathewson authored
390 #ifdef _WIN32
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
391 /* ConnectEx() isn't always around, even when IOCP is enabled.
392 * Here, we borrow the socket object's write handler to fall back
393 * on a non-blocking connect() when ConnectEx() is unavailable. */
394 if (BEV_IS_ASYNC(bev)) {
395 event_assign(&bev->ev_write, bev->ev_base, fd,
396 EV_WRITE|EV_PERSIST, bufferevent_writecb, bev);
397 }
398 #endif
709c21c Bufferevent support for openssl.
Nick Mathewson authored
399 bufferevent_setfd(bev, fd);
400 if (r == 0) {
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
401 if (! be_socket_enable(bev, EV_WRITE)) {
709c21c Bufferevent support for openssl.
Nick Mathewson authored
402 bufev_p->connecting = 1;
403 result = 0;
404 goto done;
659d54d Add new code to make and accept connections.
Nick Mathewson authored
405 }
57b7248 Small cleanups on freebsd-connect-refused patch.
Nick Mathewson authored
406 } else if (r == 1) {
7515de9 When connect() succeeds immediately, don't invoke the callback immedi…
Nick Mathewson authored
407 /* The connect succeeded already. How very BSD of it. */
d1a2254 Fix some bugs in bufferevent_socket_connect
Nick Mathewson authored
408 result = 0;
7515de9 When connect() succeeds immediately, don't invoke the callback immedi…
Nick Mathewson authored
409 bufev_p->connecting = 1;
410 event_active(&bev->ev_write, EV_WRITE, 1);
7bc48bf deal with connect() failing immediately
Niels Provos authored
411 } else {
412 /* The connect failed already. How very BSD of it. */
57b7248 Small cleanups on freebsd-connect-refused patch.
Nick Mathewson authored
413 bufev_p->connection_refused = 1;
414 bufev_p->connecting = 1;
415 result = 0;
416 event_active(&bev->ev_write, EV_WRITE, 1);
659d54d Add new code to make and accept connections.
Nick Mathewson authored
417 }
418
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
419 goto done;
420
421 freesock:
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
422 bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR);
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
423 if (ownfd)
899c1dc Replace EVUTIL_CLOSESOCKET macro with a function
Sebastian Sjöberg authored
424 evutil_closesocket(fd);
86db1c8 Commit ConnectEx code to get connect working with async bufferevents.
Nick Mathewson authored
425 /* do something about the error? */
a62283a Always hold a reference to a bufferevent when calling its callbacks.
Nick Mathewson authored
426 done:
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
427 bufferevent_decref_and_unlock_(bev);
a62283a Always hold a reference to a bufferevent when calling its callbacks.
Nick Mathewson authored
428 return result;
659d54d Add new code to make and accept connections.
Nick Mathewson authored
429 }
430
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
431 static void
432 bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
433 void *arg)
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
434 {
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
435 struct bufferevent *bev = arg;
0ef4070 @chris-davis Report DNS error when lookup fails during bufferevent_socket_connect_…
chris-davis authored
436 struct bufferevent_private *bev_p =
437 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
438 int r;
439 BEV_LOCK(bev);
440
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
441 bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
442 bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
db08f64 Suspend read/write on bufferevents during hostname lookup
Nick Mathewson authored
443
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
444 if (result != 0) {
0ef4070 @chris-davis Report DNS error when lookup fails during bufferevent_socket_connect_…
chris-davis authored
445 bev_p->dns_error = result;
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
446 bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR);
447 bufferevent_decref_and_unlock_(bev);
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
448 if (ai)
449 evutil_freeaddrinfo(ai);
450 return;
451 }
452
453 /* XXX use the other addrinfos? */
7bcace2 Fix some irix compilation warnings spotted by Kevin Bowling
Nick Mathewson authored
454 /* XXX use this return value */
598d133 Try to clear up more size_t vs int/long issues.
Nick Mathewson authored
455 r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen);
65707d7 @shahn add some (void) casts for unused variables
shahn authored
456 (void)r;
cb9da0b Fix all identifiers with names beginning with underscore.
Nick Mathewson authored
457 bufferevent_decref_and_unlock_(bev);
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
458 evutil_freeaddrinfo(ai);
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
459 }
460
461 int
462 bufferevent_socket_connect_hostname(struct bufferevent *bev,
463 struct evdns_base *evdns_base, int family, const char *hostname, int port)
464 {
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
465 char portbuf[10];
466 struct evutil_addrinfo hint;
467 int err;
0ef4070 @chris-davis Report DNS error when lookup fails during bufferevent_socket_connect_…
chris-davis authored
468 struct bufferevent_private *bev_p =
469 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
470
471 if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC)
472 return -1;
473 if (port < 1 || port > 65535)
474 return -1;
475
0ef4070 @chris-davis Report DNS error when lookup fails during bufferevent_socket_connect_…
chris-davis authored
476 BEV_LOCK(bev);
477 bev_p->dns_error = 0;
478 BEV_UNLOCK(bev);
479
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
480 evutil_snprintf(portbuf, sizeof(portbuf), "%d", port);
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
481
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
482 memset(&hint, 0, sizeof(hint));
483 hint.ai_family = family;
484 hint.ai_protocol = IPPROTO_TCP;
485 hint.ai_socktype = SOCK_STREAM;
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
486
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
487 bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
488 bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
db08f64 Suspend read/write on bufferevents during hostname lookup
Nick Mathewson authored
489
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
490 bufferevent_incref_(bev);
491 err = evutil_getaddrinfo_async_(evdns_base, hostname, portbuf,
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
492 &hint, bufferevent_connect_getaddrinfo_cb, bev);
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
493
db08f64 Suspend read/write on bufferevents during hostname lookup
Nick Mathewson authored
494 if (err == 0) {
86f5742 Add two implementations of getaddrinfo: one blocking and one nonblock…
Nick Mathewson authored
495 return 0;
db08f64 Suspend read/write on bufferevents during hostname lookup
Nick Mathewson authored
496 } else {
8ac3c4c Have all visible internal function names end with an underscore.
Nick Mathewson authored
497 bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
498 bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
499 return -1;
db08f64 Suspend read/write on bufferevents during hostname lookup
Nick Mathewson authored
500 }
0b9eb1b Add a bufferevent function to resolve a name then connect to it.
Nick Mathewson authored
501 }
502
0ef4070 @chris-davis Report DNS error when lookup fails during bufferevent_socket_connect_…
chris-davis authored
503 int
504 bufferevent_socket_get_dns_error(struct bufferevent *bev)
505 {
506 int rv;
507 struct bufferevent_private *bev_p =
508 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
509
510 BEV_LOCK(bev);
511 rv = bev_p->dns_error;
512 BEV_LOCK(bev);
513
514 return rv;
515 }
516
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
517 /*
518 * Create a new buffered event object.
519 *
520 * The read callback is invoked whenever we read new data.
521 * The write callback is invoked whenever the output buffer is drained.
522 * The error callback is invoked on a write/read error or on EOF.
523 *
524 * Both read and write callbacks maybe NULL. The error callback is not
525 * allowed to be NULL and have to be provided always.
526 */
527
528 struct bufferevent *
83f46e5 Do not use the "evbuffer_" prefix to denote parts of bufferevents.
Nick Mathewson authored
529 bufferevent_new(evutil_socket_t fd,
530 bufferevent_data_cb readcb, bufferevent_data_cb writecb,
5232cfa Consistently say "eventcb" instead of "errorcb"
Nick Mathewson authored
531 bufferevent_event_cb eventcb, void *cbarg)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
532 {
533 struct bufferevent *bufev;
534
535 if (!(bufev = bufferevent_socket_new(NULL, fd, 0)))
536 return NULL;
537
5232cfa Consistently say "eventcb" instead of "errorcb"
Nick Mathewson authored
538 bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
539
540 return bufev;
541 }
542
543
544 static int
545 be_socket_enable(struct bufferevent *bufev, short event)
546 {
547 if (event & EV_READ) {
548 if (be_socket_add(&bufev->ev_read,&bufev->timeout_read) == -1)
549 return -1;
550 }
551 if (event & EV_WRITE) {
552 if (be_socket_add(&bufev->ev_write,&bufev->timeout_write) == -1)
553 return -1;
554 }
555 return 0;
556 }
557
558 static int
559 be_socket_disable(struct bufferevent *bufev, short event)
560 {
659d54d Add new code to make and accept connections.
Nick Mathewson authored
561 struct bufferevent_private *bufev_p =
562 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
563 if (event & EV_READ) {
564 if (event_del(&bufev->ev_read) == -1)
565 return -1;
566 }
659d54d Add new code to make and accept connections.
Nick Mathewson authored
567 /* Don't actually disable the write if we are trying to connect. */
568 if ((event & EV_WRITE) && ! bufev_p->connecting) {
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
569 if (event_del(&bufev->ev_write) == -1)
570 return -1;
571 }
572 return 0;
573 }
574
575 static void
576 be_socket_destruct(struct bufferevent *bufev)
577 {
1becc4c Refactor new elements of bufferevent into bufferevent_private structure
Nick Mathewson authored
578 struct bufferevent_private *bufev_p =
579 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
580 evutil_socket_t fd;
2e36dbe Use EVUTIL_ASSERT() consistently instead of assert.
Nick Mathewson authored
581 EVUTIL_ASSERT(bufev->be_ops == &bufferevent_ops_socket);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
582
583 fd = event_get_fd(&bufev->ev_read);
584
585 event_del(&bufev->ev_read);
586 event_del(&bufev->ev_write);
587
b34abf3 Do not close(-1) when freeing an uninitialized socket bufferevent
Nick Mathewson authored
588 if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
589 EVUTIL_CLOSESOCKET(fd);
590 }
591
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
592 static int
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
593 be_socket_adj_timeouts(struct bufferevent *bufev)
594 {
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
595 int r = 0;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
596 if (event_pending(&bufev->ev_read, EV_READ, NULL))
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
597 if (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0)
598 r = -1;
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
599 if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) {
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
600 if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0)
601 r = -1;
d328829 Provide consistent, tested semantics for bufferevent timeouts
Nick Mathewson authored
602 }
ff3f6cd Check more internal event_add() calls for failure
Nick Mathewson authored
603 return r;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
604 }
605
606 static int
607 be_socket_flush(struct bufferevent *bev, short iotype,
608 enum bufferevent_flush_mode mode)
609 {
eff09a2 tab/whitespace fixes in bufferevent_sock.c
Nick Mathewson authored
610 return 0;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
611 }
612
613
31d89f2 Add a "ctrl" mechanism to bufferevents for property access.
Nick Mathewson authored
614 static void
615 be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
616 {
915193e Locking support for bufferevents.
Nick Mathewson authored
617 BEV_LOCK(bufev);
2e36dbe Use EVUTIL_ASSERT() consistently instead of assert.
Nick Mathewson authored
618 EVUTIL_ASSERT(bufev->be_ops == &bufferevent_ops_socket);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
619
620 event_del(&bufev->ev_read);
621 event_del(&bufev->ev_write);
622
623 event_assign(&bufev->ev_read, bufev->ev_base, fd,
624 EV_READ|EV_PERSIST, bufferevent_readcb, bufev);
625 event_assign(&bufev->ev_write, bufev->ev_base, fd,
626 EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev);
8274379 Do not make bufferevent_setfd implicitly disable EV_READ and EV_WRITE.
Nick Mathewson authored
627
628 if (fd >= 0)
629 bufferevent_enable(bufev, bufev->enabled);
630
915193e Locking support for bufferevents.
Nick Mathewson authored
631 BEV_UNLOCK(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
632 }
633
e3fd294 Spelling fixes in comments and strings.
Nick Mathewson authored
634 /* XXXX Should non-socket bufferevents support this? */
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
635 int
636 bufferevent_priority_set(struct bufferevent *bufev, int priority)
637 {
915193e Locking support for bufferevents.
Nick Mathewson authored
638 int r = -1;
639
640 BEV_LOCK(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
641 if (bufev->be_ops != &bufferevent_ops_socket)
915193e Locking support for bufferevents.
Nick Mathewson authored
642 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
643
644 if (event_priority_set(&bufev->ev_read, priority) == -1)
915193e Locking support for bufferevents.
Nick Mathewson authored
645 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
646 if (event_priority_set(&bufev->ev_write, priority) == -1)
915193e Locking support for bufferevents.
Nick Mathewson authored
647 goto done;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
648
915193e Locking support for bufferevents.
Nick Mathewson authored
649 r = 0;
650 done:
651 BEV_UNLOCK(bufev);
652 return r;
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
653 }
654
e3fd294 Spelling fixes in comments and strings.
Nick Mathewson authored
655 /* XXXX Should non-socket bufferevents support this? */
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
656 int
657 bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
658 {
915193e Locking support for bufferevents.
Nick Mathewson authored
659 int res = -1;
660
661 BEV_LOCK(bufev);
ea4b872 checkpoint work on big bufferevent refactoring
Nick Mathewson authored
662 if (bufev->be_ops != &bufferevent_ops_socket)